]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_silence.cpp
Added m_silence.cpp - implementation of /SILENCE command
[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                         sl->push_back(std::string(nick));
99                         WriteServ(user->fd,"951 %s %s :Added %s!*@* to silence list",user->nick, user->nick,nick);
100                         return;
101                 }
102         }
103         return;
104 }
105
106
107 class ModuleSilence : public Module
108 {
109         Server *Srv;
110  public:
111  
112         ModuleSilence()
113         {
114                 Srv = new Server;
115                 Srv->AddCommand("SILENCE",handle_silence,0,0);
116         }
117
118         virtual void OnUserQuit(userrec* user)
119         {
120                 // when the user quits tidy up any silence list they might have just to keep things tidy
121                 // and to prevent a HONKING BIG MEMORY LEAK!
122                 silencelist* sl = (silencelist*)user->GetExt("silence_list");
123                 if (sl)
124                 {
125                         delete sl;
126                         user->Shrink("silence_list");
127                 }
128         }
129         
130         virtual int OnUserPreNotice(userrec* user,void* dest,int target_type, std::string &text)
131         {
132                 // im not sure how unreal's silence operates but ours is sensible. It blocks notices and
133                 // privmsgs from people on the silence list, directed privately at the user.
134                 // channel messages are unaffected (ever tried to follow the flow of conversation in
135                 // a channel when you've set an ignore on the two most talkative people?)
136                 if (target_type == TYPE_USER)
137                 {
138                         userrec* u = (userrec*)dest;
139                         silencelist* sl = (silencelist*)u->GetExt("silence_list");
140                         if (sl)
141                         {
142                                 for (silencelist::const_iterator c = sl->begin(); c != sl->end(); c++)
143                                 {
144                                         if (!strcasecmp(c->c_str(),user->nick))
145                                         {
146                                                 return 1;
147                                         }
148                                 }
149                         }
150                 }
151                 return 0;
152         }
153
154         virtual int OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text)
155         {
156                 if (target_type == TYPE_USER)
157                 {
158                         userrec* u = (userrec*)dest;
159                         silencelist* sl = (silencelist*)u->GetExt("silence_list");
160                         if (sl)
161                         {
162                                 for (silencelist::const_iterator c = sl->begin(); c != sl->end(); c++)
163                                 {
164                                         if (!strcasecmp(c->c_str(),user->nick))
165                                         {
166                                                 return 1;
167                                         }
168                                 }
169                         }
170                 }
171                 return 0;
172         }
173
174         virtual ~ModuleSilence()
175         {
176                 delete Srv;
177         }
178         
179         virtual Version GetVersion()
180         {
181                 return Version(1,0,0,0);
182         }
183 };
184
185
186 class ModuleSilenceFactory : public ModuleFactory
187 {
188  public:
189         ModuleSilenceFactory()
190         {
191         }
192         
193         ~ModuleSilenceFactory()
194         {
195         }
196         
197         virtual Module * CreateModule()
198         {
199                 return new ModuleSilence;
200         }
201         
202 };
203
204
205 extern "C" void * init_module( void )
206 {
207         return new ModuleSilenceFactory;
208 }
209