]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_silence.cpp
Added remote rehash (even to a mask)
[user/henk/code/inspircd.git] / src / modules / m_silence.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  Inspire is copyright (C) 2002-2004 ChatSpike-Dev.
6  *                       E-mail:
7  *                <brain@chatspike.net>
8  *                <Craig@chatspike.net>
9  *     
10  * Written by Craig Edwards, Craig McLure, and others.
11  * This program is free but copyrighted software; see
12  *            the file COPYING for details.
13  *
14  * ---------------------------------------------------
15  */
16
17 using namespace std;
18
19 #include <stdio.h>
20 #include <string>
21 #include <vector>
22 #include "users.h"
23 #include "channels.h"
24 #include "modules.h"
25 #include "helperfuncs.h"
26 #include "hashcomp.h"
27
28 /* $ModDesc: Provides support for the /SILENCE command */
29
30
31 // This typedef holds a silence list. Each user may or may not have a
32 // silencelist, if a silence list is empty for a user, he/she does not
33 // have one of these structures associated with their user record.
34 typedef std::vector<std::string> silencelist;
35
36
37 void handle_silence(char **parameters, int pcnt, userrec *user)
38 {
39         if (!pcnt)
40         {
41                 // no parameters, show the current silence list.
42                 // Use Extensible::GetExt to fetch the silence list
43                 silencelist* sl = (silencelist*)user->GetExt("silence_list");
44                 // if the user has a silence list associated with their user record, show it
45                 if (sl)
46                 {
47                         for (silencelist::const_iterator c = sl->begin(); c < sl->end(); c++)
48                         {
49                                 WriteServ(user->fd,"271 %s %s %s!*@*",user->nick, user->nick,c->c_str());
50                         }
51                 }
52                 WriteServ(user->fd,"272 %s :End of Silence List",user->nick);
53         }
54         else if (pcnt > 0)
55         {
56                 // one or more parameters, add or delete entry from the list (only the first parameter is used)
57                 char *nick = parameters[0];
58                 if (nick[0] == '-')
59                 {
60                         // removing an item from the list
61                         nick++;
62                         // fetch their silence list
63                         silencelist* sl = (silencelist*)user->GetExt("silence_list");
64                         // does it contain any entries and does it exist?
65                         if (sl)
66                         {
67                                 if (sl->size())
68                                 {
69                                         for (silencelist::iterator i = sl->begin(); i != sl->end(); i++)
70                                         {
71                                                 // search through for the item
72                                                 irc::string listitem = i->c_str();
73                                                 irc::string target = nick;
74                                                 if (listitem == target)
75                                                 {
76                                                         sl->erase(i);
77                                                         WriteServ(user->fd,"950 %s %s :Removed %s!*@* from silence list",user->nick, user->nick,nick);
78                                                         // we have modified the vector from within a loop, we must now bail out
79                                                         return;
80                                                 }
81                                         }
82                                 }
83                                 if (!sl->size())
84                                 {
85                                         // tidy up -- if a user's list is empty, theres no use having it
86                                         // hanging around in the user record.
87                                         delete sl;
88                                         user->Shrink("silence_list");
89                                 }
90                         }
91                 }
92                 else if (nick[0] == '+')
93                 {
94                         nick++;
95                         // fetch the user's current silence list
96                         silencelist* sl = (silencelist*)user->GetExt("silence_list");
97                         // what, they dont have one??? WE'RE ALL GONNA DIE! ...no, we just create an empty one.
98                         if (!sl)
99                         {
100                                 sl = new silencelist;
101                                 user->Extend(std::string("silence_list"),(char*)sl);
102                         }
103                         // add the nick to it -- silence only takes nicks for some reason even though its list shows masks
104                         for (silencelist::iterator n = sl->begin(); n != sl->end();  n++)
105                         {
106                                 irc::string listitem = n->c_str();
107                                 irc::string target = nick;
108                                 if (listitem == target)
109                                 {
110                                         WriteServ(user->fd,"952 %s %s :%s is already on your silence list",user->nick, user->nick,nick);
111                                         return;
112                                 }
113                         }
114                         sl->push_back(std::string(nick));
115                         WriteServ(user->fd,"951 %s %s :Added %s!*@* to silence list",user->nick, user->nick,nick);
116                         return;
117                 }
118         }
119         return;
120 }
121
122
123 class ModuleSilence : public Module
124 {
125         Server *Srv;
126  public:
127  
128         ModuleSilence()
129         {
130                 Srv = new Server;
131                 Srv->AddCommand("SILENCE",handle_silence,0,0,"m_silence.so");
132         }
133
134         virtual void OnUserQuit(userrec* user, std::string reason)
135         {
136                 // when the user quits tidy up any silence list they might have just to keep things tidy
137                 // and to prevent a HONKING BIG MEMORY LEAK!
138                 silencelist* sl = (silencelist*)user->GetExt("silence_list");
139                 if (sl)
140                 {
141                         delete sl;
142                         user->Shrink("silence_list");
143                 }
144         }
145
146         virtual void On005Numeric(std::string &output)
147         {
148                 // we don't really have a limit...
149                 output = output + " SILENCE=999";
150         }
151         
152         virtual int OnUserPreNotice(userrec* user,void* dest,int target_type, std::string &text)
153         {
154                 // im not sure how unreal's silence operates but ours is sensible. It blocks notices and
155                 // privmsgs from people on the silence list, directed privately at the user.
156                 // channel messages are unaffected (ever tried to follow the flow of conversation in
157                 // a channel when you've set an ignore on the two most talkative people?)
158                 if (target_type == TYPE_USER)
159                 {
160                         userrec* u = (userrec*)dest;
161                         silencelist* sl = (silencelist*)u->GetExt("silence_list");
162                         if (sl)
163                         {
164                                 for (silencelist::const_iterator c = sl->begin(); c != sl->end(); c++)
165                                 {
166                                         irc::string listitem = c->c_str();
167                                         irc::string target = user->nick;
168                                         if (listitem == target)
169                                         {
170                                                 return 1;
171                                         }
172                                 }
173                         }
174                 }
175                 return 0;
176         }
177
178         virtual int OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text)
179         {
180                 if (target_type == TYPE_USER)
181                 {
182                         userrec* u = (userrec*)dest;
183                         silencelist* sl = (silencelist*)u->GetExt("silence_list");
184                         if (sl)
185                         {
186                                 for (silencelist::const_iterator c = sl->begin(); c != sl->end(); c++)
187                                 {
188                                         irc::string listitem = c->c_str();
189                                         irc::string target = user->nick;
190                                         if (listitem == target)
191                                         {
192                                                 return 1;
193                                         }
194                                 }
195                         }
196                 }
197                 return 0;
198         }
199
200         virtual ~ModuleSilence()
201         {
202                 delete Srv;
203         }
204         
205         virtual Version GetVersion()
206         {
207                 return Version(1,0,0,1,VF_VENDOR);
208         }
209 };
210
211
212 class ModuleSilenceFactory : public ModuleFactory
213 {
214  public:
215         ModuleSilenceFactory()
216         {
217         }
218         
219         ~ModuleSilenceFactory()
220         {
221         }
222         
223         virtual Module * CreateModule()
224         {
225                 return new ModuleSilence;
226         }
227         
228 };
229
230
231 extern "C" void * init_module( void )
232 {
233         return new ModuleSilenceFactory;
234 }
235