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