]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/u_listmode.h
banexception, chanfilter and inviteexception should now be unloadable :)
[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 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                 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                                         return std::make_pair(true, parameter);
79                                 }
80                         }
81                 }
82                 return std::make_pair(false, parameter);
83         }
84
85         virtual void DisplayList(userrec* user, chanrec* channel)
86         {
87                 modelist* el;
88                 channel->GetExt(infokey, el);
89                 if (el)
90                 {
91                         for(modelist::iterator it = el->begin(); it != el->end(); it++)
92                         {
93                                 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());
94                         }
95                 }
96                 user->WriteServ( "%s %s %s %s", endoflistnumeric.c_str(), user->nick, channel->name, endofliststring.c_str());
97         }
98
99         virtual void RemoveMode(chanrec* channel)
100         {
101                 modelist* el;
102                 channel->GetExt(infokey, el);
103                 if (el)
104                 {
105                         char moderemove[MAXBUF];
106                         userrec* n = new userrec(ServerInstance);
107                         n->SetFd(FD_MAGIC_NUMBER);
108                         for(modelist::iterator it = el->begin(); it != el->end(); it++)
109                         {
110                                 sprintf(moderemove,"-%c",this->GetModeChar());
111                                 const char* parameters[] = { channel->name, moderemove, it->mask.c_str() };
112                                 ServerInstance->SendMode(parameters, 3, n);
113                         }
114                         delete n;
115                 }
116         }
117
118         virtual void RemoveMode(userrec* user)
119         {
120                 /* Listmodes dont get set on users */
121         }
122
123         virtual void DoRehash()
124         {
125                 ConfigReader Conf(ServerInstance);
126
127                 chanlimits.clear();
128
129                 for(int i = 0; i < Conf.Enumerate(configtag); i++)
130                 {
131                         // For each <banlist> tag
132                         ListLimit limit;
133                         limit.mask = Conf.ReadValue(configtag, "chan", i);
134                         limit.limit = Conf.ReadInteger(configtag, "limit", i, true);
135
136                         if(limit.mask.size() && limit.limit > 0)
137                                 chanlimits.push_back(limit);
138                 }
139                 if(chanlimits.size() == 0)
140                 {
141                         ListLimit limit;
142                         limit.mask = "*";
143                         limit.limit = 64;
144                         chanlimits.push_back(limit);
145                 }
146         }
147
148         virtual void DoImplements(char* List)
149         {
150                 List[I_OnChannelDelete] = List[I_OnSyncChannel] = List[I_OnCleanup] = List[I_OnRehash] = 1;
151         }
152
153         virtual ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string &parameter, bool adding)
154         {
155                 // Try and grab the list
156                 modelist* el;
157                 channel->GetExt(infokey, el);
158
159                 if (adding)
160                 {
161                         // If there was no list
162                         if (!el)
163                         {
164                                 // Make one
165                                 el = new modelist;
166                                 channel->Extend(infokey, el);
167                         }
168
169                         // Clean the mask up
170                         if (this->tidy)
171                                 ModeParser::CleanMask(parameter);
172
173                         // Check if the item already exists in the list
174                         for (modelist::iterator it = el->begin(); it != el->end(); it++)
175                         {
176                                 if(parameter == it->mask)
177                                 {
178                                         /* Give a subclass a chance to error about this */
179                                         TellAlreadyOnList(source, channel, parameter);
180                                         
181                                         // it does, deny the change
182                                         return MODEACTION_DENY;
183                                 }
184                         }
185
186                         unsigned int maxsize = 0;
187
188                         for (limitlist::iterator it = chanlimits.begin(); it != chanlimits.end(); it++)
189                         {
190                                 if (match(channel->name, it->mask.c_str()))
191                                 {
192                                         // We have a pattern matching the channel...
193                                         maxsize = el->size();
194                                         if (maxsize < it->limit)
195                                         {
196                                                 /* Ok, it *could* be allowed, now give someone subclassing us
197                                                  * a chance to validate the parameter.
198                                                  * The param is passed by reference, so they can both modify it
199                                                  * and tell us if we allow it or not.
200                                                  *
201                                                  * eg, the subclass could:
202                                                  * 1) allow
203                                                  * 2) 'fix' parameter and then allow
204                                                  * 3) deny
205                                                  */
206                                                 if(ValidateParam(source, channel, parameter))
207                                                 {
208                                                         // And now add the mask onto the list...
209                                                         ListItem e;
210                                                         e.mask = parameter;
211                                                         e.nick = source->nick;
212                                                         e.time = stringtime();
213
214                                                         el->push_back(e);
215                                                         return MODEACTION_ALLOW;
216                                                 }
217                                                 else
218                                                 {
219                                                         /* If they deny it they have the job of giving an error message */
220                                                         return MODEACTION_DENY;
221                                                 }
222                                         }
223                                 }
224                         }
225
226                         /* List is full, give subclass a chance to send a custom message */
227                         if(!TellListTooLong(source, channel, parameter))
228                         {
229                                 source->WriteServ("478 %s %s %s :Channel ban/ignore list is full", source->nick, channel->name, parameter.c_str());
230                         }
231                         
232                         parameter = "";
233                         return MODEACTION_DENY; 
234                 }
235                 else
236                 {
237                         // We're taking the mode off
238                         if (el)
239                         {
240                                 for (modelist::iterator it = el->begin(); it != el->end(); it++)
241                                 {
242                                         if(parameter == it->mask)
243                                         {
244                                                 el->erase(it);
245                                                 if(el->size() == 0)
246                                                 {
247                                                         channel->Shrink(infokey);
248                                                         delete el;
249                                                 }
250                                                 return MODEACTION_ALLOW;
251                                         }
252                                         else
253                                         {
254                                                 /* Tried to remove something that wasn't set */
255                                                 TellNotSet(source, channel, parameter);
256                                         }
257                                 }
258                                 parameter = "";
259                                 return MODEACTION_DENY;
260                         }
261                         else
262                         {
263                                 // Hmm, taking an exception off a non-existant list, DIE
264                                 parameter = "";
265                                 return MODEACTION_DENY;
266                         }
267                 }
268                 return MODEACTION_DENY;
269         }
270
271         virtual std::string& GetInfoKey()
272         {
273                 return infokey;
274         }
275
276         virtual void DoChannelDelete(chanrec* chan)
277         {
278                 modelist* list;
279                 chan->GetExt(infokey, list);
280
281                 if (list)
282                 {
283                         chan->Shrink(infokey);
284                         delete list;
285                 }
286         }
287
288         virtual void DoSyncChannel(chanrec* chan, Module* proto, void* opaque)
289         {
290                 modelist* list;
291                 chan->GetExt(infokey, list);
292                 if (list)
293                 {
294                         for (modelist::iterator it = list->begin(); it != list->end(); it++)
295                         {
296                                 proto->ProtoSendMode(opaque, TYPE_CHANNEL, chan, "+" + std::string(1, mode) + " " + it->mask);
297                         }
298                 }
299         }
300
301         virtual void DoCleanup(int target_type, void* item)
302         {
303                 if (target_type == TYPE_CHANNEL)
304                 {
305                         chanrec* chan = (chanrec*)item;
306
307                         modelist* list;
308                         chan->GetExt(infokey, list);
309
310                         if (list)
311                         {
312                                 chan->Shrink(infokey);
313                                 delete list;
314                         }
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