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