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