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