]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/u_listmode.h
2d973f40d2ad5570f4872a4d194dd9ce309ddb69
[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 "wildcard.h"
12 #include "inspircd.h"
13
14 /* $ModDesc: Provides support for easily creating listmodes, stores the time set, the user, and a parameter. */
15
16 /* Updated to use the <banlist> config tag if it exists */
17 /* Written by Om <omster@gmail.com>, December 2005. */
18 /* Based on code previously written by Om - April 2005 */
19 /* Updated to new API July 8th 2006 by Brain */
20 /* Originally based on m_chanprotect and m_silence */
21
22 inline std::string stringtime()
23 {
24         std::ostringstream TIME;
25         TIME << time(NULL); 
26         return TIME.str();
27 }
28
29 /** An item in a listmode's list
30  */
31 class ListItem : public classbase
32 {
33 public:
34         std::string nick;
35         std::string mask;
36         std::string time;
37 };
38
39 /** The number of items a listmode's list may contain
40  */
41 class ListLimit : public classbase
42 {
43 public:
44         std::string mask;
45         unsigned int limit;
46 };
47
48 // Just defining the type we use for the exception list here...
49 typedef std::vector<ListItem> modelist;
50 typedef std::vector<ListLimit> limitlist;
51
52 /** The base class for listmodes defined by u_listmode.h
53  */
54 class ListModeBase : public ModeHandler
55 {
56  protected:
57         std::string infokey;
58         std::string listnumeric;
59         std::string endoflistnumeric;
60         std::string endofliststring;
61         bool tidy;
62         std::string configtag;
63         limitlist chanlimits;
64  
65  public:
66         ListModeBase(InspIRCd* Instance, char modechar, const std::string &eolstr, const std::string &lnum, const std::string &eolnum, bool autotidy, const std::string &ctag = "banlist")
67         : ModeHandler(Instance, modechar, 1, 1, true, MODETYPE_CHANNEL, false), listnumeric(lnum), endoflistnumeric(eolnum), endofliststring(eolstr), tidy(autotidy), configtag(ctag)
68         {
69                 this->DoRehash();
70                 infokey = "exceptionbase_mode_" + std::string(1, mode) + "_list";
71         }
72
73         std::pair<bool,std::string> ModeSet(userrec* source, userrec* dest, chanrec* channel, const std::string &parameter)
74         {
75                 modelist* el;
76                 channel->GetExt(infokey, el);
77                 if (el)
78                 {
79                         for (modelist::iterator it = el->begin(); it != el->end(); it++)
80                         {
81                                 if(parameter == it->mask)
82                                 {
83                                         return std::make_pair(true, parameter);
84                                 }
85                         }
86                 }
87                 return std::make_pair(false, parameter);
88         }
89
90         virtual void DisplayList(userrec* user, chanrec* channel)
91         {
92                 modelist* el;
93                 channel->GetExt(infokey, el);
94                 if (el)
95                 {
96                         for(modelist::iterator it = el->begin(); it != el->end(); it++)
97                         {
98                                 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());
99                         }
100                 }
101                 user->WriteServ("%s %s %s %s", endoflistnumeric.c_str(), user->nick, channel->name, endofliststring.c_str());
102         }
103
104         virtual void RemoveMode(chanrec* channel)
105         {
106                 ServerInstance->Log(DEBUG,"Removing listmode base from %s %s",channel->name,infokey.c_str());
107                 modelist* el;
108                 channel->GetExt(infokey, el);
109                 if (el)
110                 {
111                         ServerInstance->Log(DEBUG,"Channel is extended with a list");
112                         char moderemove[MAXBUF];
113                         userrec* n = new userrec(ServerInstance);
114                         n->SetFd(FD_MAGIC_NUMBER);
115                         modelist copy;
116                         /* Make a copy of it, because we cant change the list whilst iterating over it */
117                         for(modelist::iterator it = el->begin(); it != el->end(); it++)
118                         {
119                                 copy.push_back(*it);
120                         }
121                         for(modelist::iterator it = copy.begin(); it != copy.end(); it++)
122                         {
123                                 ServerInstance->Log(DEBUG,"Remove item %s",it->mask.c_str());
124                                 sprintf(moderemove,"-%c",this->GetModeChar());
125                                 const char* parameters[] = { channel->name, moderemove, it->mask.c_str() };
126                                 ServerInstance->SendMode(parameters, 3, n);
127                         }
128                         delete n;
129                 }
130         }
131
132         virtual void RemoveMode(userrec* user)
133         {
134                 /* Listmodes dont get set on users */
135         }
136
137         virtual void DoRehash()
138         {
139                 ConfigReader Conf(ServerInstance);
140
141                 chanlimits.clear();
142
143                 for(int i = 0; i < Conf.Enumerate(configtag); i++)
144                 {
145                         // For each <banlist> tag
146                         ListLimit limit;
147                         limit.mask = Conf.ReadValue(configtag, "chan", i);
148                         limit.limit = Conf.ReadInteger(configtag, "limit", i, true);
149
150                         if(limit.mask.size() && limit.limit > 0)
151                                 chanlimits.push_back(limit);
152                 }
153                 if(chanlimits.size() == 0)
154                 {
155                         ListLimit limit;
156                         limit.mask = "*";
157                         limit.limit = 64;
158                         chanlimits.push_back(limit);
159                 }
160         }
161
162         virtual void DoImplements(char* List)
163         {
164                 List[I_OnChannelDelete] = List[I_OnSyncChannel] = List[I_OnCleanup] = List[I_OnRehash] = 1;
165         }
166
167         virtual ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string &parameter, bool adding)
168         {
169                 // Try and grab the list
170                 modelist* el;
171                 channel->GetExt(infokey, el);
172
173                 if (adding)
174                 {
175                         // If there was no list
176                         if (!el)
177                         {
178                                 // Make one
179                                 el = new modelist;
180                                 channel->Extend(infokey, el);
181                         }
182
183                         // Clean the mask up
184                         if (this->tidy)
185                                 ModeParser::CleanMask(parameter);
186
187                         // Check if the item already exists in the list
188                         for (modelist::iterator it = el->begin(); it != el->end(); it++)
189                         {
190                                 if(parameter == it->mask)
191                                 {
192                                         /* Give a subclass a chance to error about this */
193                                         TellAlreadyOnList(source, channel, parameter);
194                                         
195                                         // it does, deny the change
196                                         return MODEACTION_DENY;
197                                 }
198                         }
199
200                         unsigned int maxsize = 0;
201
202                         for (limitlist::iterator it = chanlimits.begin(); it != chanlimits.end(); it++)
203                         {
204                                 if (match(channel->name, it->mask.c_str()))
205                                 {
206                                         // We have a pattern matching the channel...
207                                         maxsize = el->size();
208                                         if (maxsize < it->limit)
209                                         {
210                                                 /* Ok, it *could* be allowed, now give someone subclassing us
211                                                  * a chance to validate the parameter.
212                                                  * The param is passed by reference, so they can both modify it
213                                                  * and tell us if we allow it or not.
214                                                  *
215                                                  * eg, the subclass could:
216                                                  * 1) allow
217                                                  * 2) 'fix' parameter and then allow
218                                                  * 3) deny
219                                                  */
220                                                 if(ValidateParam(source, channel, parameter))
221                                                 {
222                                                         // And now add the mask onto the list...
223                                                         ListItem e;
224                                                         e.mask = parameter;
225                                                         e.nick = source->nick;
226                                                         e.time = stringtime();
227
228                                                         el->push_back(e);
229                                                         return MODEACTION_ALLOW;
230                                                 }
231                                                 else
232                                                 {
233                                                         /* If they deny it they have the job of giving an error message */
234                                                         return MODEACTION_DENY;
235                                                 }
236                                         }
237                                 }
238                         }
239
240                         /* List is full, give subclass a chance to send a custom message */
241                         if(!TellListTooLong(source, channel, parameter))
242                         {
243                                 source->WriteServ("478 %s %s %s :Channel ban/ignore list is full", source->nick, channel->name, parameter.c_str());
244                         }
245                         
246                         parameter = "";
247                         return MODEACTION_DENY; 
248                 }
249                 else
250                 {
251                         // We're taking the mode off
252                         if (el)
253                         {
254                                 for (modelist::iterator it = el->begin(); it != el->end(); it++)
255                                 {
256                                         if(parameter == it->mask)
257                                         {
258                                                 el->erase(it);
259                                                 if(el->size() == 0)
260                                                 {
261                                                         channel->Shrink(infokey);
262                                                         delete el;
263                                                 }
264                                                 return MODEACTION_ALLOW;
265                                         }
266                                 }
267                                 /* Tried to remove something that wasn't set */
268                                 TellNotSet(source, channel, parameter);
269                                 parameter = "";
270                                 return MODEACTION_DENY;
271                         }
272                         else
273                         {
274                                 /* Hmm, taking an exception off a non-existant list, DIE */
275                                 TellNotSet(source, channel, parameter);
276                                 parameter = "";
277                                 return MODEACTION_DENY;
278                         }
279                 }
280                 return MODEACTION_DENY;
281         }
282
283         virtual std::string& GetInfoKey()
284         {
285                 return infokey;
286         }
287
288         virtual void DoChannelDelete(chanrec* chan)
289         {
290                 modelist* list;
291                 chan->GetExt(infokey, list);
292
293                 if (list)
294                 {
295                         chan->Shrink(infokey);
296                         delete list;
297                 }
298         }
299
300         virtual void DoSyncChannel(chanrec* chan, Module* proto, void* opaque)
301         {
302                 modelist* list;
303                 chan->GetExt(infokey, list);
304                 irc::modestacker modestack(true);
305                 std::deque<std::string> stackresult;
306                 if (list)
307                 {
308                         for (modelist::iterator it = list->begin(); it != list->end(); it++)
309                         {
310                                 modestack.Push(std::string(1, mode)[0], it->mask);
311                         }
312                 }
313                 while (modestack.GetStackedLine(stackresult))
314                 {
315                         irc::stringjoiner mode_join(" ", stackresult, 0, stackresult.size() - 1);
316                         std::string line = mode_join.GetJoined();
317                         proto->ProtoSendMode(opaque,TYPE_CHANNEL,chan, line);
318                 }
319         }
320
321         virtual void DoCleanup(int target_type, void* item)
322         {
323         }
324         
325         virtual bool ValidateParam(userrec* source, chanrec* channel, std::string &parameter)
326         {
327                 return true;
328         }
329         
330         virtual bool TellListTooLong(userrec* source, chanrec* channel, std::string &parameter)
331         {
332                 return false;
333         }
334         
335         virtual void TellAlreadyOnList(userrec* source, chanrec* channel, std::string &parameter)
336         {
337         }
338         
339         virtual void TellNotSet(userrec* source, chanrec* channel, std::string &parameter)
340         {
341                 
342         }
343 };
344
345 #endif