]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/u_listmode.h
Document more classes
[user/henk/code/inspircd.git] / include / u_listmode.h
1 #ifndef INSPIRCD_LISTMODE_PROVIDER
2 #define INSPIRCD_LISTMODE_PROVIDER
3
4 #include <stdio.h>
5 #include <string>
6 #include <sstream>
7 #include <vector>
8 #include "users.h"
9 #include "channels.h"
10 #include "modules.h"
11
12 #include "wildcard.h"
13 #include "inspircd.h"
14
15 /* $ModDesc: Provides support for easily creating listmodes, stores the time set, the user, and a parameter. */
16
17 /* Updated to use the <banlist> config tag if it exists */
18 /* Written by Om <omster@gmail.com>, December 2005. */
19 /* Based on code previously written by Om - April 2005 */
20 /* Updated to new API July 8th 2006 by Brain */
21 /* Originally based on m_chanprotect and m_silence */
22
23 inline std::string stringtime()
24 {
25         std::ostringstream TIME;
26         TIME << time(NULL); 
27         return TIME.str();
28 }
29
30 /** An item in a listmode's list
31  */
32 class ListItem : public classbase
33 {
34 public:
35         std::string nick;
36         std::string mask;
37         std::string time;
38 };
39
40 /** The number of items a listmode's list may contain
41  */
42 class ListLimit : public classbase
43 {
44 public:
45         std::string mask;
46         unsigned int limit;
47 };
48
49 // Just defining the type we use for the exception list here...
50 typedef std::vector<ListItem> modelist;
51 typedef std::vector<ListLimit> limitlist;
52
53 /** The base class for listmodes defined by u_listmode.h
54  */
55 class ListModeBase : public ModeHandler
56 {
57  protected:
58         std::string infokey;
59         std::string listnumeric;
60         std::string endoflistnumeric;
61         std::string endofliststring;
62         bool tidy;
63         std::string configtag;
64         limitlist chanlimits;
65  
66  public:
67         ListModeBase(InspIRCd* Instance, char modechar, const std::string &eolstr, const std::string &lnum, const std::string &eolnum, bool autotidy, const std::string &ctag = "banlist")
68         : ModeHandler(Instance, modechar, 1, 1, true, MODETYPE_CHANNEL, false), listnumeric(lnum), endoflistnumeric(eolnum), endofliststring(eolstr), tidy(autotidy), configtag(ctag)
69         {
70                 this->DoRehash();
71                 infokey = "exceptionbase_mode_" + std::string(1, mode) + "_list";
72         }
73
74         std::pair<bool,std::string> ModeSet(userrec* source, userrec* dest, chanrec* channel, const std::string &parameter)
75         {
76                 modelist* el;
77                 channel->GetExt(infokey, el);
78                 if (el)
79                 {
80                         for (modelist::iterator it = el->begin(); it != el->end(); it++)
81                         {
82                                 if(parameter == it->mask)
83                                 {
84                                         return std::make_pair(true, parameter);
85                                 }
86                         }
87                 }
88                 return std::make_pair(false, parameter);
89         }
90
91         virtual void DisplayList(userrec* user, chanrec* channel)
92         {
93                 modelist* el;
94                 channel->GetExt(infokey, el);
95                 if (el)
96                 {
97                         for(modelist::iterator it = el->begin(); it != el->end(); it++)
98                         {
99                                 user->WriteServ("%s %s %s %s %s %s", listnumeric.c_str(), user->nick, channel->name, it->mask.c_str(), it->nick.c_str(), it->time.c_str());
100                         }
101                 }
102                 user->WriteServ("%s %s %s %s", endoflistnumeric.c_str(), user->nick, channel->name, endofliststring.c_str());
103         }
104
105         virtual void RemoveMode(chanrec* channel)
106         {
107                 ServerInstance->Log(DEBUG,"Removing listmode base from %s %s",channel->name,infokey.c_str());
108                 modelist* el;
109                 channel->GetExt(infokey, el);
110                 if (el)
111                 {
112                         ServerInstance->Log(DEBUG,"Channel is extended with a list");
113                         char moderemove[MAXBUF];
114                         userrec* n = new userrec(ServerInstance);
115                         n->SetFd(FD_MAGIC_NUMBER);
116                         modelist copy;
117                         /* Make a copy of it, because we cant change the list whilst iterating over it */
118                         for(modelist::iterator it = el->begin(); it != el->end(); it++)
119                         {
120                                 copy.push_back(*it);
121                         }
122                         for(modelist::iterator it = copy.begin(); it != copy.end(); it++)
123                         {
124                                 ServerInstance->Log(DEBUG,"Remove item %s",it->mask.c_str());
125                                 sprintf(moderemove,"-%c",this->GetModeChar());
126                                 const char* parameters[] = { channel->name, moderemove, it->mask.c_str() };
127                                 ServerInstance->SendMode(parameters, 3, n);
128                         }
129                         delete n;
130                 }
131         }
132
133         virtual void RemoveMode(userrec* user)
134         {
135                 /* Listmodes dont get set on users */
136         }
137
138         virtual void DoRehash()
139         {
140                 ConfigReader Conf(ServerInstance);
141
142                 chanlimits.clear();
143
144                 for(int i = 0; i < Conf.Enumerate(configtag); i++)
145                 {
146                         // For each <banlist> tag
147                         ListLimit limit;
148                         limit.mask = Conf.ReadValue(configtag, "chan", i);
149                         limit.limit = Conf.ReadInteger(configtag, "limit", i, true);
150
151                         if(limit.mask.size() && limit.limit > 0)
152                                 chanlimits.push_back(limit);
153                 }
154                 if(chanlimits.size() == 0)
155                 {
156                         ListLimit limit;
157                         limit.mask = "*";
158                         limit.limit = 64;
159                         chanlimits.push_back(limit);
160                 }
161         }
162
163         virtual void DoImplements(char* List)
164         {
165                 List[I_OnChannelDelete] = List[I_OnSyncChannel] = List[I_OnCleanup] = List[I_OnRehash] = 1;
166         }
167
168         virtual ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string &parameter, bool adding)
169         {
170                 // Try and grab the list
171                 modelist* el;
172                 channel->GetExt(infokey, el);
173
174                 if (adding)
175                 {
176                         // If there was no list
177                         if (!el)
178                         {
179                                 // Make one
180                                 el = new modelist;
181                                 channel->Extend(infokey, el);
182                         }
183
184                         // Clean the mask up
185                         if (this->tidy)
186                                 ModeParser::CleanMask(parameter);
187
188                         // Check if the item already exists in the list
189                         for (modelist::iterator it = el->begin(); it != el->end(); it++)
190                         {
191                                 if(parameter == it->mask)
192                                 {
193                                         /* Give a subclass a chance to error about this */
194                                         TellAlreadyOnList(source, channel, parameter);
195                                         
196                                         // it does, deny the change
197                                         return MODEACTION_DENY;
198                                 }
199                         }
200
201                         unsigned int maxsize = 0;
202
203                         for (limitlist::iterator it = chanlimits.begin(); it != chanlimits.end(); it++)
204                         {
205                                 if (match(channel->name, it->mask.c_str()))
206                                 {
207                                         // We have a pattern matching the channel...
208                                         maxsize = el->size();
209                                         if (maxsize < it->limit)
210                                         {
211                                                 /* Ok, it *could* be allowed, now give someone subclassing us
212                                                  * a chance to validate the parameter.
213                                                  * The param is passed by reference, so they can both modify it
214                                                  * and tell us if we allow it or not.
215                                                  *
216                                                  * eg, the subclass could:
217                                                  * 1) allow
218                                                  * 2) 'fix' parameter and then allow
219                                                  * 3) deny
220                                                  */
221                                                 if(ValidateParam(source, channel, parameter))
222                                                 {
223                                                         // And now add the mask onto the list...
224                                                         ListItem e;
225                                                         e.mask = parameter;
226                                                         e.nick = source->nick;
227                                                         e.time = stringtime();
228
229                                                         el->push_back(e);
230                                                         return MODEACTION_ALLOW;
231                                                 }
232                                                 else
233                                                 {
234                                                         /* If they deny it they have the job of giving an error message */
235                                                         return MODEACTION_DENY;
236                                                 }
237                                         }
238                                 }
239                         }
240
241                         /* List is full, give subclass a chance to send a custom message */
242                         if(!TellListTooLong(source, channel, parameter))
243                         {
244                                 source->WriteServ("478 %s %s %s :Channel ban/ignore list is full", source->nick, channel->name, parameter.c_str());
245                         }
246                         
247                         parameter = "";
248                         return MODEACTION_DENY; 
249                 }
250                 else
251                 {
252                         // We're taking the mode off
253                         if (el)
254                         {
255                                 for (modelist::iterator it = el->begin(); it != el->end(); it++)
256                                 {
257                                         if(parameter == it->mask)
258                                         {
259                                                 el->erase(it);
260                                                 if(el->size() == 0)
261                                                 {
262                                                         channel->Shrink(infokey);
263                                                         delete el;
264                                                 }
265                                                 return MODEACTION_ALLOW;
266                                         }
267                                 }
268                                 /* Tried to remove something that wasn't set */
269                                 TellNotSet(source, channel, parameter);
270                                 parameter = "";
271                                 return MODEACTION_DENY;
272                         }
273                         else
274                         {
275                                 /* Hmm, taking an exception off a non-existant list, DIE */
276                                 TellNotSet(source, channel, parameter);
277                                 parameter = "";
278                                 return MODEACTION_DENY;
279                         }
280                 }
281                 return MODEACTION_DENY;
282         }
283
284         virtual std::string& GetInfoKey()
285         {
286                 return infokey;
287         }
288
289         virtual void DoChannelDelete(chanrec* chan)
290         {
291                 modelist* list;
292                 chan->GetExt(infokey, list);
293
294                 if (list)
295                 {
296                         chan->Shrink(infokey);
297                         delete list;
298                 }
299         }
300
301         virtual void DoSyncChannel(chanrec* chan, Module* proto, void* opaque)
302         {
303                 modelist* list;
304                 chan->GetExt(infokey, list);
305                 if (list)
306                 {
307                         for (modelist::iterator it = list->begin(); it != list->end(); it++)
308                         {
309                                 proto->ProtoSendMode(opaque, TYPE_CHANNEL, chan, "+" + std::string(1, mode) + " " + it->mask);
310                         }
311                 }
312         }
313
314         virtual void DoCleanup(int target_type, void* item)
315         {
316         }
317         
318         virtual bool ValidateParam(userrec* source, chanrec* channel, std::string &parameter)
319         {
320                 return true;
321         }
322         
323         virtual bool TellListTooLong(userrec* source, chanrec* channel, std::string &parameter)
324         {
325                 return false;
326         }
327         
328         virtual void TellAlreadyOnList(userrec* source, chanrec* channel, std::string &parameter)
329         {
330         }
331         
332         virtual void TellNotSet(userrec* source, chanrec* channel, std::string &parameter)
333         {
334                 
335         }
336 };
337
338 #endif