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