]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_callerid.cpp
fix some unitialised vectors and tidy up a bit.
[user/henk/code/inspircd.git] / src / modules / m_callerid.cpp
1 #include "inspircd.h"
2 #include "users.h"
3 #include "channels.h"
4 #include "modules.h"
5
6 #include <set>
7
8 /* $ModDesc: Implementation of callerid (umode +g & /accept, ala hybrid etc) */
9
10 class callerid_data : public classbase
11 {
12  public:
13         time_t lastnotify;
14         std::set<User*> accepting;
15 };
16
17 callerid_data* GetData(User* who, bool extend = true)
18 {
19         callerid_data* dat;
20         if (who->GetExt("callerid_data", dat))
21         {
22                 return dat;
23         }
24         else
25         {
26                 if (extend)
27                 {
28                         dat = new callerid_data;
29                         dat->lastnotify = 0; // Can't init in struct.
30                         who->Extend("callerid_data", dat);
31                         return dat;
32                 }
33                 else
34                 {
35                         return NULL;
36                 }
37         }
38 }
39
40 void RemoveData(User* who)
41 {
42         callerid_data* dat;
43         who->GetExt("callerid_data", dat);
44         if (!dat) return;
45         who->Shrink("callerid_data");
46         delete dat;
47 }
48
49 void RemoveFromAllAccepts(InspIRCd* ServerInstance, User* who)
50 {
51         for (user_hash::iterator i = ServerInstance->Users->clientlist->begin(); i != ServerInstance->Users->clientlist->end(); ++i)
52         {
53                 callerid_data* dat = GetData(i->second, false);
54                 if (!dat) continue;
55                 std::set<User*>& accepting = dat->accepting;
56                 std::set<User*>::iterator iter = accepting.find(who);
57                 if (iter == accepting.end()) continue;
58                 accepting.erase(iter);
59         }
60 }
61
62 class User_g : public SimpleUserModeHandler
63 {
64 private:
65
66 public:
67         User_g(InspIRCd* Instance) : SimpleUserModeHandler(Instance, 'g') { }
68 };
69
70 class CommandAccept : public Command
71 {
72 private:
73         unsigned int& maxaccepts;
74 public:
75         CommandAccept(InspIRCd* Instance, unsigned int& max) : Command(Instance, "ACCEPT", 0, 1), maxaccepts(max)
76         {
77                 source = "m_callerid.so";
78                 syntax = "{[+|-]<nicks>}|*}";
79         }
80
81         /* Will take any number of nicks, which can be seperated by spaces, commas, or a mix.
82          * - in front of any nick removes, and an * lists. This effectively means you can do:
83          * /accept nick1,nick2,nick3 *
84          * to add 3 nicks and then show your list
85          */
86
87         CmdResult Handle(const std::vector<std::string> &parameters, User* user)
88         {
89                 if (parameters.size() < 1)
90                 {
91                         /* Command stuff should've dealt with this already */
92                         return CMD_FAILURE;
93                 }
94                 /* Even if callerid mode is not set, we let them manage their ACCEPT list so that if they go +g they can
95                  * have a list already setup. */
96                 bool atleastonechange = false;
97                 for (int i = 0; i < (int)parameters.size(); ++i)
98                 {
99                         const char* arg = parameters[i].c_str();
100                         irc::commasepstream css(arg);
101                         std::string tok;
102                         while (css.GetToken(tok))
103                         {
104                                 if (tok.length() < 1)
105                                         continue;
106                                 if (tok == "*")
107                                 {
108                                         if (IS_LOCAL(user)) continue;
109                                         ListAccept(user);
110                                 }
111                                 else if (tok[0] == '-')
112                                 {
113                                         User* whotoremove = ServerInstance->FindNick(tok.substr(1));
114                                         if (whotoremove)
115                                         {
116                                                 atleastonechange = RemoveAccept(user, whotoremove, false) || atleastonechange;
117                                         }
118                                 }
119                                 else
120                                 {
121                                         User* whotoadd = ServerInstance->FindNick(tok[0] == '+' ? tok.substr(1) : tok);
122                                         if (whotoadd)
123                                         {
124                                                 atleastonechange = AddAccept(user, whotoadd, false) || atleastonechange;
125                                         }
126                                         else
127                                         {
128                                                 user->WriteNumeric(401, "%s %s :No such nick/channel", user->nick, tok.c_str());
129                                         }
130                                 }
131                         }
132                 }
133                 return atleastonechange ? CMD_FAILURE : CMD_SUCCESS;
134         }
135
136         void ListAccept(User* user)
137         {
138                 callerid_data* dat = GetData(user, false);
139                 if (dat)
140                 {
141                         for (std::set<User*>::iterator i = dat->accepting.begin(); i != dat->accepting.end(); ++i)
142                         {
143                                 user->WriteNumeric(281, "%s %s", user->nick, (*i)->nick);
144                         }
145                 }
146                 user->WriteNumeric(282, "%s :End of ACCEPT list", user->nick);
147         }
148
149         bool AddAccept(User* user, User* whotoadd, bool quiet)
150         {
151                 callerid_data* dat = GetData(user, true);
152                 std::set<User*>& accepting = dat->accepting;
153                 if (accepting.size() >= maxaccepts)
154                 {
155                         if (!quiet) user->WriteNumeric(456, "%s :Accept list is full (limit is %d)", user->nick, maxaccepts);
156                         return false;
157                 }
158                 if (!accepting.insert(whotoadd).second)
159                 {
160                         if (!quiet) user->WriteNumeric(457, "%s %s :is already on your accept list", user->nick, whotoadd->nick);
161                         return false;
162                 }
163                 return true;
164         }
165
166         bool RemoveAccept(User* user, User* whotoremove, bool quiet)
167         {
168                 callerid_data* dat = GetData(user, false);
169                 if (!dat)
170                 {
171                         if (!quiet) user->WriteNumeric(458, "%s %s :is not on your accept list", user->nick, whotoremove->nick);
172                         return false;
173                 }
174                 std::set<User*>& accepting = dat->accepting;
175                 std::set<User*>::iterator i = accepting.find(whotoremove);
176                 if (i == accepting.end())
177                 {
178                         if (!quiet) user->WriteNumeric(458, "%s %s :is not on your accept list", user->nick, whotoremove->nick);
179                         return false;
180                 }
181                 accepting.erase(i);
182                 return true;
183         }
184 };
185
186 class ModuleCallerID : public Module
187 {
188 private:
189         CommandAccept *mycommand;
190         User_g* myumode;
191
192         // Configuration variables:
193         unsigned int maxaccepts; // Maximum ACCEPT entries.
194         bool operoverride; // Operators can override callerid.
195         bool tracknick; // Allow ACCEPT entries to update with nick changes.
196         unsigned int notify_cooldown; // Seconds between notifications.
197
198 public:
199         ModuleCallerID(InspIRCd* Me) : Module(Me)
200         {
201                 OnRehash(NULL, "");
202                 mycommand = new CommandAccept(ServerInstance, maxaccepts);
203                 myumode = new User_g(ServerInstance);
204                 try {
205                         ServerInstance->AddCommand(mycommand);
206                 } catch (const ModuleException& e) {
207                         delete mycommand;
208                         throw;
209                 }
210                 if (!ServerInstance->Modes->AddMode(myumode))
211                 {
212                         delete mycommand;
213                         delete myumode;
214                         throw new ModuleException("Could not add usermode and command!");
215                 }
216                 Implementation eventlist[] = { I_OnRehash, I_OnUserPreNick, I_OnUserQuit, I_On005Numeric, I_OnUserPreNotice, I_OnUserPreMessage, I_OnCleanup };
217                 ServerInstance->Modules->Attach(eventlist, this, 7);
218         }
219
220         ~ModuleCallerID()
221         {
222                 delete myumode;
223         }
224
225         Version GetVersion()
226         {
227                 return Version(1, 0, 0, 0, VF_COMMON | VF_VENDOR, API_VERSION);
228         }
229
230         void On005Numeric(std::string& output)
231         {
232                 output += " CALLERID=g";
233         }
234
235         int PreText(User* user, User* dest, std::string& text, bool notice)
236         {
237                 if (!dest->IsModeSet('g')) return 0;
238                 if (operoverride && IS_OPER(user)) return 0;
239                 callerid_data* dat = GetData(dest, true);
240                 std::set<User*>& accepting = dat->accepting;
241                 time_t& lastnotify = dat->lastnotify;
242                 std::set<User*>::iterator i = accepting.find(dest);
243                 if (i == accepting.end())
244                 {
245                         time_t now = time(NULL);
246                         /* +g and *not* accepted */
247                         user->WriteNumeric(716, "%s %s :is in +g mode (server-side ignore).", user->nick, dest->nick);
248                         if (now > (lastnotify + (time_t)notify_cooldown))
249                         {
250                                 user->WriteNumeric(717, "%s %s :has been informed that you messaged them.", user->nick, dest->nick);
251                                 dest->WriteNumeric(718, "%s %s %s@%s :is messaging you, and you have umode +g", dest->nick, user->nick, user->ident, user->dhost);
252                                 lastnotify = now;
253                         }
254                         return 1;
255                 }
256                 return 0;
257         }
258
259         int OnUserPreMessage(User* user, void* dest, int target_type, std::string& text, char status, CUList &exempt_list)
260         {
261                 if (IS_LOCAL(user) && target_type == TYPE_USER)
262                         return PreText(user, (User*)dest, text, true);
263                 return 0;
264         }
265
266         int OnUserPreNotice(User* user, void* dest, int target_type, std::string& text, char status, CUList &exempt_list)
267         {
268                 if (IS_LOCAL(user) && target_type == TYPE_USER)
269                         return PreText(user, (User*)dest, text, true);
270                 return 0;
271         }
272
273         void OnCleanup(int type, void* item)
274         {
275                 if (type != TYPE_USER) return;
276                 User* u = (User*)item;
277                 /* Cleanup only happens on unload (before dtor), so keep this O(n) instead of O(n^2) which deferring to OnUserQuit would do.  */
278                 RemoveData(u);
279         }
280
281         int OnUserPreNick(User* user, const std::string& newnick)
282         {
283                 if (!tracknick)
284                         RemoveFromAllAccepts(ServerInstance, user);
285                 return 0;
286         }
287
288         void OnUserQuit(User* user, const std::string& message, const std::string& oper_message)
289         {
290                 RemoveData(user);
291                 RemoveFromAllAccepts(ServerInstance, user);
292         }
293
294         void OnRehash(User* user, const std::string& parameter)
295         {
296                 ConfigReader Conf(ServerInstance);
297                 int new_maxaccepts, new_cooldown;
298                 bool new_override, new_track;
299                 new_maxaccepts = Conf.ReadInteger("callerid", "maxaccepts", "16", 0, true);
300                 switch (Conf.GetError())
301                 {
302                         case 0: break;
303                         case CONF_VALUE_NOT_FOUND:
304                                 new_maxaccepts = 16;
305                                 break;
306                         case CONF_NOT_A_NUMBER:
307                                 if (user) user->WriteServ("NOTICE %s :Invalid maxaccepts value '%s', not a number", user->nick, Conf.ReadValue("callerid", "maxaccepts", "", 0).c_str());
308                                 throw ModuleException("Invalid maxaccepts value, not a number");
309                         case CONF_INT_NEGATIVE:
310                                 if (user) user->WriteServ("NOTICE %s :Invalid maxaccepts value '%s', negative", user->nick, Conf.ReadValue("callerid", "maxaccepts", "", 0).c_str());
311                                 throw ModuleException("Invalid maxaccepts value, negative");
312                         default:
313                                 /* Yikes */
314                                 throw ModuleException("Invalid maxaccepts value, unknown config error");
315                 }
316                 new_override = Conf.ReadFlag("callerid", "operoverride", "0", 0);
317                 if (Conf.GetError() == CONF_VALUE_NOT_FOUND) new_override = false;
318                 new_track = Conf.ReadFlag("callerid", "tracknick", "0", 0);
319                 if (Conf.GetError() == CONF_VALUE_NOT_FOUND) new_track = false;
320                 new_cooldown = Conf.ReadInteger("callerid", "cooldown", "60", 0, true);
321                 switch (Conf.GetError())
322                 {
323                         case 0: break;
324                         case CONF_VALUE_NOT_FOUND:
325                                 new_cooldown = 16;
326                                 break;
327                         case CONF_NOT_A_NUMBER:
328                                 if (user) user->WriteServ("NOTICE %s :Invalid cooldown value '%s', not a number", user->nick, Conf.ReadValue("callerid", "maxaccepts", "", 0).c_str());
329                                 throw ModuleException("Invalid cooldown value, not a number");
330                         case CONF_INT_NEGATIVE:
331                                 if (user) user->WriteServ("NOTICE %s :Invalid cooldown value '%s', negative", user->nick, Conf.ReadValue("callerid", "maxaccepts", "", 0).c_str());
332                                 throw ModuleException("Invalid cooldown value, negative");
333                         default:
334                                 /* Yikes */
335                                 throw ModuleException("Invalid cooldown value, unknown config error");
336                 }
337                 maxaccepts = new_maxaccepts;
338                 notify_cooldown = new_cooldown;
339                 operoverride = new_override;
340                 tracknick = new_track;
341         }
342 };
343
344 MODULE_INIT(ModuleCallerID)
345
346