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