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