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