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