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