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