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