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