]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_silence.cpp
Fixed m_helpop.cpp lowercasing the first line of the text
[user/henk/code/inspircd.git] / src / modules / m_silence.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  Inspire is copyright (C) 2002-2004 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 #include <stdio.h>
18 #include <string>
19 #include <vector>
20 #include "users.h"
21 #include "channels.h"
22 #include "modules.h"
23
24 /* $ModDesc: Provides support for the /SILENCE command */
25
26
27 // This typedef holds a silence list. Each user may or may not have a
28 // silencelist, if a silence list is empty for a user, he/she does not
29 // have one of these structures associated with their user record.
30 typedef std::vector<std::string> silencelist;
31
32
33 void handle_silence(char **parameters, int pcnt, userrec *user)
34 {
35         if (!pcnt)
36         {
37                 // no parameters, show the current silence list.
38                 // Use Extensible::GetExt to fetch the silence list
39                 silencelist* sl = (silencelist*)user->GetExt("silence_list");
40                 // if the user has a silence list associated with their user record, show it
41                 if (sl)
42                 {
43                         for (silencelist::const_iterator c = sl->begin(); c < sl->end(); c++)
44                         {
45                                 WriteServ(user->fd,"271 %s %s %s!*@*",user->nick, user->nick,c->c_str());
46                         }
47                 }
48                 WriteServ(user->fd,"272 %s :End of Silence List",user->nick);
49         }
50         else if (pcnt > 0)
51         {
52                 // one or more parameters, add or delete entry from the list (only the first parameter is used)
53                 char *nick = parameters[0];
54                 if (nick[0] == '-')
55                 {
56                         // removing an item from the list
57                         nick++;
58                         // fetch their silence list
59                         silencelist* sl = (silencelist*)user->GetExt("silence_list");
60                         // does it contain any entries and does it exist?
61                         if (sl)
62                         {
63                                 if (sl->size())
64                                 {
65                                         for (silencelist::iterator i = sl->begin(); i != sl->end(); i++)
66                                         {
67                                                 // search through for the item
68                                                 if (!strcasecmp(i->c_str(),nick))
69                                                 {
70                                                         sl->erase(i);
71                                                         WriteServ(user->fd,"950 %s %s :Removed %s!*@* from silence list",user->nick, user->nick,nick);
72                                                         // we have modified the vector from within a loop, we must now bail out
73                                                         return;
74                                                 }
75                                         }
76                                 }
77                                 if (!sl->size())
78                                 {
79                                         // tidy up -- if a user's list is empty, theres no use having it
80                                         // hanging around in the user record.
81                                         delete sl;
82                                         user->Shrink("silence_list");
83                                 }
84                         }
85                 }
86                 else if (nick[0] == '+')
87                 {
88                         nick++;
89                         // fetch the user's current silence list
90                         silencelist* sl = (silencelist*)user->GetExt("silence_list");
91                         // what, they dont have one??? WE'RE ALL GONNA DIE! ...no, we just create an empty one.
92                         if (!sl)
93                         {
94                                 sl = new silencelist;
95                                 user->Extend(std::string("silence_list"),(char*)sl);
96                         }
97                         // add the nick to it -- silence only takes nicks for some reason even though its list shows masks
98                         sl->push_back(std::string(nick));
99                         WriteServ(user->fd,"951 %s %s :Added %s!*@* to silence list",user->nick, user->nick,nick);
100                         return;
101                 }
102         }
103         return;
104 }
105
106
107 class ModuleSilence : public Module
108 {
109         Server *Srv;
110  public:
111  
112         ModuleSilence()
113         {
114                 Srv = new Server;
115                 Srv->AddCommand("SILENCE",handle_silence,0,0);
116         }
117
118         virtual void OnUserQuit(userrec* user)
119         {
120                 // when the user quits tidy up any silence list they might have just to keep things tidy
121                 // and to prevent a HONKING BIG MEMORY LEAK!
122                 silencelist* sl = (silencelist*)user->GetExt("silence_list");
123                 if (sl)
124                 {
125                         delete sl;
126                         user->Shrink("silence_list");
127                 }
128         }
129
130         virtual void On005Numeric(std::string &output)
131         {
132                 // we don't really have a limit...
133                 output = output + " SILENCE=999";
134         }
135         
136         virtual int OnUserPreNotice(userrec* user,void* dest,int target_type, std::string &text)
137         {
138                 // im not sure how unreal's silence operates but ours is sensible. It blocks notices and
139                 // privmsgs from people on the silence list, directed privately at the user.
140                 // channel messages are unaffected (ever tried to follow the flow of conversation in
141                 // a channel when you've set an ignore on the two most talkative people?)
142                 if (target_type == TYPE_USER)
143                 {
144                         userrec* u = (userrec*)dest;
145                         silencelist* sl = (silencelist*)u->GetExt("silence_list");
146                         if (sl)
147                         {
148                                 for (silencelist::const_iterator c = sl->begin(); c != sl->end(); c++)
149                                 {
150                                         if (!strcasecmp(c->c_str(),user->nick))
151                                         {
152                                                 return 1;
153                                         }
154                                 }
155                         }
156                 }
157                 return 0;
158         }
159
160         virtual int OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text)
161         {
162                 if (target_type == TYPE_USER)
163                 {
164                         userrec* u = (userrec*)dest;
165                         silencelist* sl = (silencelist*)u->GetExt("silence_list");
166                         if (sl)
167                         {
168                                 for (silencelist::const_iterator c = sl->begin(); c != sl->end(); c++)
169                                 {
170                                         if (!strcasecmp(c->c_str(),user->nick))
171                                         {
172                                                 return 1;
173                                         }
174                                 }
175                         }
176                 }
177                 return 0;
178         }
179
180         virtual ~ModuleSilence()
181         {
182                 delete Srv;
183         }
184         
185         virtual Version GetVersion()
186         {
187                 return Version(1,0,0,0);
188         }
189 };
190
191
192 class ModuleSilenceFactory : public ModuleFactory
193 {
194  public:
195         ModuleSilenceFactory()
196         {
197         }
198         
199         ~ModuleSilenceFactory()
200         {
201         }
202         
203         virtual Module * CreateModule()
204         {
205                 return new ModuleSilence;
206         }
207         
208 };
209
210
211 extern "C" void * init_module( void )
212 {
213         return new ModuleSilenceFactory;
214 }
215