]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/u_listmode.h
1618754a3845360cff4026b61b026df3dab9a467
[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         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         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                         if (this->tidy)
134                                 ModeParser::CleanMask(parameter);
135
136                         // Check if the item already exists in the list
137                         for (modelist::iterator it = el->begin(); it != el->end(); it++)
138                         {
139                                 if(parameter == it->mask)
140                                 {
141                                         /* Give a subclass a chance to error about this */
142                                         TellAlreadyOnList(source, channel, parameter);
143                                         
144                                         // it does, deny the change
145                                         return MODEACTION_DENY;
146                                 }
147                         }
148
149                         unsigned int maxsize = 0;
150
151                         for (limitlist::iterator it = chanlimits.begin(); it != chanlimits.end(); it++)
152                         {
153                                 if (Srv->MatchText(channel->name, it->mask))
154                                 {
155                                         // We have a pattern matching the channel...
156                                         maxsize = el->size();
157                                         if (maxsize < it->limit)
158                                         {
159                                                 /* Ok, it *could* be allowed, now give someone subclassing us
160                                                  * a chance to validate the parameter.
161                                                  * The param is passed by reference, so they can both modify it
162                                                  * and tell us if we allow it or not.
163                                                  *
164                                                  * eg, the subclass could:
165                                                  * 1) allow
166                                                  * 2) 'fix' parameter and then allow
167                                                  * 3) deny
168                                                  */
169                                                 if(ValidateParam(source, channel, parameter))
170                                                 {
171                                                         // And now add the mask onto the list...
172                                                         ListItem e;
173                                                         e.mask = parameter;
174                                                         e.nick = source->nick;
175                                                         e.time = stringtime();
176
177                                                         el->push_back(e);
178                                                         return MODEACTION_ALLOW;
179                                                 }
180                                                 else
181                                                 {
182                                                         /* If they deny it they have the job of giving an error message */
183                                                         return MODEACTION_DENY;
184                                                 }
185                                         }
186                                 }
187                         }
188
189                         /* List is full, give subclass a chance to send a custom message */
190                         if(!TellListTooLong(source, channel, parameter))
191                         {
192                                 WriteServ(source->fd, "478 %s %s %s :Channel ban/ignore list is full", source->nick, channel->name, parameter.c_str());
193                         }
194                         
195                         parameter = "";
196                         return MODEACTION_DENY; 
197                 }
198                 else
199                 {
200                         // We're taking the mode off
201                         if (el)
202                         {
203                                 for (modelist::iterator it = el->begin(); it != el->end(); it++)
204                                 {
205                                         if(parameter == it->mask)
206                                         {
207                                                 el->erase(it);
208                                                 if(el->size() == 0)
209                                                 {
210                                                         channel->Shrink(infokey);
211                                                         delete el;
212                                                 }
213                                                 return MODEACTION_ALLOW;
214                                         }
215                                         else
216                                         {
217                                                 /* Tried to remove something that wasn't set */
218                                                 TellNotSet(source, channel, parameter);
219                                         }
220                                 }
221                                 parameter = "";
222                                 return MODEACTION_DENY;
223                         }
224                         else
225                         {
226                                 // Hmm, taking an exception off a non-existant list, DIE
227                                 parameter = "";
228                                 return MODEACTION_DENY;
229                         }
230                 }
231                 return MODEACTION_DENY;
232         }
233
234         virtual std::string& GetInfoKey()
235         {
236                 return infokey;
237         }
238
239         virtual void DoChannelDelete(chanrec* chan)
240         {
241                 modelist* list = (modelist*)chan->GetExt(infokey);
242
243                 if (list)
244                 {
245                         chan->Shrink(infokey);
246                         delete list;
247                 }
248         }
249
250         virtual void DoSyncChannel(chanrec* chan, Module* proto, void* opaque)
251         {
252                 modelist* list = (modelist*)chan->GetExt(infokey);
253                 if (list)
254                 {
255                         for (modelist::iterator it = list->begin(); it != list->end(); it++)
256                         {
257                                 proto->ProtoSendMode(opaque, TYPE_CHANNEL, chan, "+" + std::string(1, mode) + " " + it->mask);
258                         }
259                 }
260         }
261
262         virtual void DoCleanup(int target_type, void* item)
263         {
264                 if (target_type == TYPE_CHANNEL)
265                 {
266                         chanrec* chan = (chanrec*)item;
267
268                         modelist* list = (modelist*)chan->GetExt(infokey);
269
270                         if (list)
271                         {
272                                 chan->Shrink(infokey);
273                                 delete list;
274                         }
275                 }
276         }
277         
278         virtual bool ValidateParam(userrec* source, chanrec* channel, std::string &parameter)
279         {
280                 return true;
281         }
282         
283         virtual bool TellListTooLong(userrec* source, chanrec* channel, std::string &parameter)
284         {
285                 return false;
286         }
287         
288         virtual void TellAlreadyOnList(userrec* source, chanrec* channel, std::string &parameter)
289         {
290         }
291         
292         virtual void TellNotSet(userrec* source, chanrec* channel, std::string &parameter)
293         {
294                 
295         }
296 };
297
298 #endif