]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_silence.cpp
Windows support. Tested and working to compile on freebsd and linux. Next step is...
[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         unsigned int& maxsilence;
34  public:
35         cmd_silence (InspIRCd* Instance, unsigned int &max) : command_t(Instance,"SILENCE", 0, 0), maxsilence(max)
36         {
37                 this->source = "m_silence.so";
38                 syntax = "{[+|-]<mask>}";
39         }
40
41         CmdResult Handle (const char** parameters, int pcnt, userrec *user)
42         {
43                 if (!pcnt)
44                 {
45                         // no parameters, show the current silence list.
46                         // Use Extensible::GetExt to fetch the silence list
47                         silencelist* sl;
48                         user->GetExt("silence_list", sl);
49                         // if the user has a silence list associated with their user record, show it
50                         if (sl)
51                         {
52                                 for (silencelist::const_iterator c = sl->begin(); c != sl->end(); c++)
53                                 {
54                                         user->WriteServ("271 %s %s %s :%lu",user->nick, user->nick, c->first.c_str(), (unsigned long)c->second);
55                                 }
56                         }
57                         user->WriteServ("272 %s :End of Silence List",user->nick);
58
59                         return CMD_SUCCESS;
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                         std::string mask = parameters[0] + 1;
65                         char action = *parameters[0];
66                         
67                         if (!mask.length())
68                         {
69                                 // 'SILENCE +' or 'SILENCE -', assume *!*@*
70                                 mask = "*!*@*";
71                         }
72                         
73                         ModeParser::CleanMask(mask);
74
75                         if (action == '-')
76                         {
77                                 // fetch their silence list
78                                 silencelist* sl;
79                                 user->GetExt("silence_list", sl);
80                                 // does it contain any entries and does it exist?
81                                 if (sl)
82                                 {
83                                         silencelist::iterator i = sl->find(mask.c_str());
84                                         if (i != sl->end())
85                                         {
86                                                 sl->erase(i);
87                                                 user->WriteServ("950 %s %s :Removed %s from silence list",user->nick, user->nick, mask.c_str());
88                                                 if (!sl->size())
89                                                 {
90                                                         // tidy up -- if a user's list is empty, theres no use having it
91                                                         // hanging around in the user record.
92                                                         DELETE(sl);
93                                                         user->Shrink("silence_list");
94                                                 }
95                                         }
96                                         else
97                                                 user->WriteServ("952 %s %s :%s does not exist on your silence list",user->nick, user->nick, mask.c_str());
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                                 if (sl->size() >= maxsilence)
118                                 {
119                                         user->WriteServ("952 %s %s :Your silence list is full",user->nick, user->nick, mask.c_str());
120                                         return CMD_FAILURE;
121                                 }
122                                 sl->insert(std::make_pair<irc::string, time_t>(mask.c_str(), ServerInstance->Time()));
123                                 user->WriteServ("951 %s %s :Added %s to silence list",user->nick, user->nick, mask.c_str());
124                                 return CMD_SUCCESS;
125                         }
126                 }
127                 return CMD_SUCCESS;
128         }
129 };
130
131 class ModuleSilence : public Module
132 {
133         
134         cmd_silence* mycommand;
135         unsigned int maxsilence;
136  public:
137  
138         ModuleSilence(InspIRCd* Me)
139                 : Module(Me), maxsilence(32)
140         {
141                 OnRehash(NULL, "");
142                 mycommand = new cmd_silence(ServerInstance, maxsilence);
143                 ServerInstance->AddCommand(mycommand);
144         }
145
146         void Implements(char* List)
147         {
148                 List[I_OnRehash] = List[I_OnUserQuit] = List[I_On005Numeric] = List[I_OnUserPreNotice] = List[I_OnUserPreMessage] = 1;
149         }
150
151         virtual void OnRehash(userrec* user, const std::string &parameter)
152         {
153                 ConfigReader Conf(ServerInstance);
154                 maxsilence = Conf.ReadInteger("silence", "maxentries", 0, true);
155                 if (!maxsilence)
156                         maxsilence = 32;
157         }
158
159         virtual void OnUserQuit(userrec* user, const std::string &reason, const std::string &oper_message)
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=" + ConvToStr(maxsilence);
176         }
177         
178         virtual int OnUserPreNotice(userrec* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
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) && (IS_LOCAL(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->first.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, CUList &exempt_list)
204         {
205                 return OnUserPreNotice(user,dest,target_type,text,status,exempt_list);
206         }
207
208         virtual ~ModuleSilence()
209         {
210         }
211         
212         virtual Version GetVersion()
213         {
214                 return Version(1,1,0,1,VF_VENDOR,API_VERSION);
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" DllExport void * init_module( void )
239 {
240         return new ModuleSilenceFactory;
241 }
242