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