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