]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/u_listmode.h
Add a parameter to set what config tag to parse (defaults to what was previously...
[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  
52         std::string infokey;
53         std::string listnumeric;
54         std::string endoflistnumeric;
55         std::string endofliststring;
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, const std::string &ctag = "banlist")
61         : ModeHandler(modechar, 1, 1, true, MODETYPE_CHANNEL, false), Srv(serv), listnumeric(lnum), endoflistnumeric(eolnum), endofliststring(eolstr), 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 = (modelist*)channel->GetExt(infokey);
70                 if (el)
71                 {
72                         for(modelist::iterator it = el->begin(); it != el->end(); it++)
73                         {
74                                 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());
75                         }
76                 }
77                 WriteServ(user->fd, "%s %s %s %s", endoflistnumeric.c_str(), user->nick, channel->name, endofliststring.c_str());
78         }
79
80         virtual void DoRehash()
81         {
82                 ConfigReader Conf;
83
84                 chanlimits.clear();
85
86                 for(int i = 0; i < Conf.Enumerate(configtag); i++)
87                 {
88                         // For each <banlist> tag
89                         ListLimit limit;
90                         limit.mask = Conf.ReadValue(configtag, "chan", i);
91                         limit.limit = Conf.ReadInteger(configtag, "limit", i, true);
92
93                         if(limit.mask.size() && limit.limit > 0)
94                         {
95                                 chanlimits.push_back(limit);
96                                 log(DEBUG, "Read channel listmode limit of %u for mask '%s'", limit.limit, limit.mask.c_str());
97                         }
98                         else
99                         {
100                                 log(DEBUG, "Invalid tag");
101                         }
102                 }
103                 if(chanlimits.size() == 0)
104                 {
105                         ListLimit limit;
106                         limit.mask = "*";
107                         limit.limit = 64;
108                         chanlimits.push_back(limit);
109                 }
110         }
111
112         virtual void DoImplements(char* List)
113         {
114                 List[I_OnChannelDelete] = List[I_OnSyncChannel] = List[I_OnCleanup] = List[I_OnRehash] = 1;
115         }
116
117         virtual ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string &parameter, bool adding)
118         {
119                 // Try and grab the list
120                 modelist* el = (modelist*)channel->GetExt(infokey);
121
122                 if (adding)
123                 {
124                         // If there was no list
125                         if (!el)
126                         {
127                                 // Make one
128                                 el = new modelist;
129                                 channel->Extend(infokey, (char*)el);
130                         }
131
132                         // Clean the mask up
133                         ModeParser::CleanMask(parameter);
134
135                         // Check if the item already exists in the list
136                         for (modelist::iterator it = el->begin(); it != el->end(); it++)
137                         {
138                                 if(parameter == it->mask)
139                                 {
140                                         // it does, deny the change
141                                         parameter = "";
142                                         return MODEACTION_DENY;
143                                 }
144                         }
145
146                         unsigned int maxsize = 0;
147
148                         for (limitlist::iterator it = chanlimits.begin(); it != chanlimits.end(); it++)
149                         {
150                                 if (Srv->MatchText(channel->name, it->mask))
151                                 {
152                                         // We have a pattern matching the channel...
153                                         maxsize = el->size();
154                                         if (maxsize < it->limit)
155                                         {
156                                                 // And now add the mask onto the list...
157                                                 ListItem e;
158                                                 e.mask = parameter;
159                                                 e.nick = source->nick;
160                                                 e.time = stringtime();
161
162                                                 el->push_back(e);
163                                                 return MODEACTION_ALLOW;
164                                         }
165                                 }
166                         }
167
168                         // List is full
169                         WriteServ(source->fd, "478 %s %s %s :Channel ban/ignore list is full", source->nick, channel->name, parameter.c_str());
170                         parameter = "";
171                         return MODEACTION_DENY;
172                 }
173                 else
174                 {
175                         // We're taking the mode off
176                         if (el)
177                         {
178                                 for (modelist::iterator it = el->begin(); it != el->end(); it++)
179                                 {
180                                         if(parameter == it->mask)
181                                         {
182                                                 el->erase(it);
183                                                 if(el->size() == 0)
184                                                 {
185                                                         channel->Shrink(infokey);
186                                                         delete el;
187                                                 }
188                                                 return MODEACTION_ALLOW;
189                                         }
190                                 }
191                                 parameter = "";
192                                 return MODEACTION_DENY;
193                         }
194                         else
195                         {
196                                 // Hmm, taking an exception off a non-existant list, DIE
197                                 parameter = "";
198                                 return MODEACTION_DENY;
199                         }
200                 }
201                 return MODEACTION_DENY;
202         }
203
204         virtual std::string& GetInfoKey()
205         {
206                 return infokey;
207         }
208
209         virtual void DoChannelDelete(chanrec* chan)
210         {
211                 modelist* list = (modelist*)chan->GetExt(infokey);
212
213                 if (list)
214                 {
215                         chan->Shrink(infokey);
216                         delete list;
217                 }
218         }
219
220         virtual void DoSyncChannel(chanrec* chan, Module* proto, void* opaque)
221         {
222                 modelist* list = (modelist*)chan->GetExt(infokey);
223                 if (list)
224                 {
225                         for (modelist::iterator it = list->begin(); it != list->end(); it++)
226                         {
227                                 proto->ProtoSendMode(opaque, TYPE_CHANNEL, chan, "+" + std::string(1, mode) + " " + it->mask);
228                         }
229                 }
230         }
231
232         virtual void DoCleanup(int target_type, void* item)
233         {
234                 if (target_type == TYPE_CHANNEL)
235                 {
236                         chanrec* chan = (chanrec*)item;
237
238                         modelist* list = (modelist*)chan->GetExt(infokey);
239
240                         if (list)
241                         {
242                                 chan->Shrink(infokey);
243                                 delete list;
244                         }
245                 }
246         }
247 };
248
249 #endif