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