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