]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_callerid.cpp
Fix various rline bugs, implement /stats R, and fix the issue where you get no error...
[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 #include <sstream>
9
10 #include <algorithm>
11
12 /* $ModDesc: Implementation of callerid (umode +g & /accept, ala hybrid etc) */
13
14 class callerid_data : public classbase
15 {
16  public:
17         time_t lastnotify;
18         std::set<User*> accepting;
19
20         callerid_data() : lastnotify(0) { }
21         callerid_data(const std::string& str, InspIRCd* ServerInstance)
22         {
23                 irc::commasepstream s(str);
24                 std::string tok;
25                 if (s.GetToken(tok))
26                 {
27                         lastnotify = ConvToInt(tok);
28                 }
29                 while (s.GetToken(tok))
30                 {
31                         if (tok.empty())
32                         {
33                                 continue;
34                         }
35                         User* u = ServerInstance->FindUUID(tok);
36                         if (!u)
37                         {
38                                 u = ServerInstance->FindNick(tok);
39                         }
40                         if (!u)
41                         {
42                                 continue;
43                         }
44                         accepting.insert(u);
45                 }
46         }
47
48         std::string ToString(bool displayable) const
49         {
50                 std::ostringstream oss;
51                 oss << lastnotify;
52                 for (std::set<User*>::const_iterator i = accepting.begin(); i != accepting.end(); ++i)
53                 {
54                         // Encode UIDs.
55                         oss << "," << (displayable ? (*i)->nick : (*i)->uuid);
56                 }
57                 oss << std::ends;
58                 return oss.str();
59         }
60 };
61
62 callerid_data* GetData(User* who, bool extend = true)
63 {
64         callerid_data* dat;
65         if (who->GetExt("callerid_data", dat))
66                 return dat;
67         else
68         {
69                 if (extend)
70                 {
71                         dat = new callerid_data;
72                         who->Extend("callerid_data", dat);
73                         return dat;
74                 }
75                 else
76                         return NULL;
77         }
78 }
79
80 void RemoveData(User* who)
81 {
82         callerid_data* dat;
83         who->GetExt("callerid_data", dat);
84
85         if (!dat)
86                 return;
87
88         who->Shrink("callerid_data");
89         delete dat;
90 }
91
92 void RemoveFromAllAccepts(InspIRCd* ServerInstance, User* who)
93 {
94         for (user_hash::iterator i = ServerInstance->Users->clientlist->begin(); i != ServerInstance->Users->clientlist->end(); ++i)
95         {
96                 callerid_data* dat = GetData(i->second, false);
97
98                 if (!dat)
99                         continue;
100
101                 std::set<User*>::iterator iter = dat->accepting.find(who);
102
103                 if (iter == dat->accepting.end())
104                         continue;
105
106                 dat->accepting.erase(iter);
107         }
108 }
109
110 class User_g : public SimpleUserModeHandler
111 {
112 public:
113         User_g(InspIRCd* Instance) : SimpleUserModeHandler(Instance, 'g') { }
114 };
115
116 class CommandAccept : public Command
117 {
118 private:
119         unsigned int& maxaccepts;
120 public:
121         CommandAccept(InspIRCd* Instance, unsigned int& max) : Command(Instance, "ACCEPT", 0, 1), maxaccepts(max)
122         {
123                 source = "m_callerid.so";
124                 syntax = "{[+|-]<nicks>}|*}";
125                 TRANSLATE2(TR_CUSTOM, TR_END);
126         }
127
128         virtual void EncodeParameter(std::string& parameter, int index)
129         {
130                 if (index != 0)
131                         return;
132                 std::string out = "";
133                 irc::commasepstream nicks(parameter);
134                 std::string tok;
135                 while (nicks.GetToken(tok))
136                 {
137                         if (tok == "*")
138                         {
139                                 continue; // Drop list requests, since remote servers ignore them anyway.
140                         }
141                         if (!out.empty())
142                                 out.append(",");
143                         bool dash = false;
144                         if (tok[0] == '-')
145                         {
146                                 dash = true;
147                                 tok.erase(0, 1); // Remove the dash.
148                         }
149                         User* u = ServerInstance->FindNick(tok);
150                         if (u)
151                         {
152                                 if (dash)
153                                         out.append("-");
154                                 out.append(u->uuid);
155                         }
156                         else
157                         {
158                                 if (dash)
159                                         out.append("-");
160                                 out.append(tok);
161                         }
162                 }
163                 parameter = out;
164         }
165
166         /** Will take any number of nicks (up to MaxTargets), which can be seperated by commas.
167          * - in front of any nick removes, and an * lists. This effectively means you can do:
168          * /accept nick1,nick2,nick3,*
169          * to add 3 nicks and then show your list
170          */
171         CmdResult Handle(const std::vector<std::string> &parameters, User* user)
172         {
173                 if (ServerInstance->Parser->LoopCall(user, this, parameters, 0))
174                         return CMD_SUCCESS;
175                 /* Even if callerid mode is not set, we let them manage their ACCEPT list so that if they go +g they can
176                  * have a list already setup. */
177
178                 std::string tok = parameters[0];
179
180                 if (tok == "*")
181                 {
182                         if (IS_LOCAL(user))
183                                 ListAccept(user);
184                         return CMD_LOCALONLY;
185                 }
186                 else if (tok[0] == '-')
187                 {
188                         User* whotoremove = ServerInstance->FindNick(tok.substr(1));
189                         if (whotoremove)
190                                 return (RemoveAccept(user, whotoremove, false) ? CMD_SUCCESS : CMD_FAILURE);
191                         else
192                                 return CMD_FAILURE;
193                 }
194                 else
195                 {
196                         User* whotoadd = ServerInstance->FindNick(tok[0] == '+' ? tok.substr(1) : tok);
197                         if (whotoadd)
198                                 return (AddAccept(user, whotoadd, false) ? CMD_SUCCESS : CMD_FAILURE);
199                         else
200                         {
201                                 user->WriteNumeric(401, "%s %s :No such nick/channel", user->nick.c_str(), tok.c_str());
202                                 return CMD_FAILURE;
203                         }
204                 }
205         }
206
207         void ListAccept(User* user)
208         {
209                 callerid_data* dat = GetData(user, false);
210                 if (dat)
211                 {
212                         for (std::set<User*>::iterator i = dat->accepting.begin(); i != dat->accepting.end(); ++i)
213                                 user->WriteNumeric(281, "%s %s", user->nick.c_str(), (*i)->nick.c_str());
214                 }
215                 user->WriteNumeric(282, "%s :End of ACCEPT list", user->nick.c_str());
216         }
217
218         bool AddAccept(User* user, User* whotoadd, bool quiet)
219         {
220                 callerid_data* dat = GetData(user, true);
221                 if (dat->accepting.size() >= maxaccepts)
222                 {
223                         if (!quiet)
224                                 user->WriteNumeric(456, "%s :Accept list is full (limit is %d)", user->nick.c_str(), maxaccepts);
225
226                         return false;
227                 }
228                 if (!dat->accepting.insert(whotoadd).second)
229                 {
230                         if (!quiet)
231                                 user->WriteNumeric(457, "%s %s :is already on your accept list", user->nick.c_str(), whotoadd->nick.c_str());
232
233                         return false;
234                 }
235                 return true;
236         }
237
238         bool RemoveAccept(User* user, User* whotoremove, bool quiet)
239         {
240                 callerid_data* dat = GetData(user, false);
241                 if (!dat)
242                 {
243                         if (!quiet)
244                                 user->WriteNumeric(458, "%s %s :is not on your accept list", user->nick.c_str(), whotoremove->nick.c_str());
245
246                         return false;
247                 }
248                 std::set<User*>::iterator i = dat->accepting.find(whotoremove);
249                 if (i == dat->accepting.end())
250                 {
251                         if (!quiet)
252                                 user->WriteNumeric(458, "%s %s :is not on your accept list", user->nick.c_str(), whotoremove->nick.c_str());
253
254                         return false;
255                 }
256                 dat->accepting.erase(i);
257                 return true;
258         }
259 };
260
261 class ModuleCallerID : public Module
262 {
263 private:
264         CommandAccept *mycommand;
265         User_g* myumode;
266
267         // Configuration variables:
268         unsigned int maxaccepts; // Maximum ACCEPT entries.
269         bool operoverride; // Operators can override callerid.
270         bool tracknick; // Allow ACCEPT entries to update with nick changes.
271         unsigned int notify_cooldown; // Seconds between notifications.
272
273 public:
274         ModuleCallerID(InspIRCd* Me) : Module(Me)
275         {
276                 OnRehash(NULL, "");
277                 mycommand = new CommandAccept(ServerInstance, maxaccepts);
278                 myumode = new User_g(ServerInstance);
279
280                 if (!ServerInstance->Modes->AddMode(myumode))
281                 {
282                         delete mycommand;
283                         delete myumode;
284                         throw ModuleException("Could not add usermode +g");
285                 }
286                 try
287                 {
288                         ServerInstance->AddCommand(mycommand);
289                 }
290                 catch (const ModuleException& e)
291                 {
292                         delete mycommand;
293                         delete myumode;
294                         throw ModuleException("Could not add command!");
295                 }
296
297                 Implementation eventlist[] = { I_OnRehash, I_OnUserPreNick, I_OnUserQuit, I_On005Numeric, I_OnUserPreNotice, I_OnUserPreMessage, I_OnCleanup };
298                 ServerInstance->Modules->Attach(eventlist, this, 7);
299         }
300
301         virtual ~ModuleCallerID()
302         {
303                 ServerInstance->Modes->DelMode(myumode);
304                 delete myumode;
305         }
306
307         virtual Version GetVersion()
308         {
309                 return Version("$Id$", VF_COMMON | VF_VENDOR, API_VERSION);
310         }
311
312         virtual void On005Numeric(std::string& output)
313         {
314                 output += " CALLERID=g";
315         }
316
317         int PreText(User* user, User* dest, std::string& text, bool notice)
318         {
319                 if (!dest->IsModeSet('g'))
320                         return 0;
321
322                 if (operoverride && IS_OPER(user))
323                         return 0;
324
325                 callerid_data* dat = GetData(dest, true);
326                 std::set<User*>::iterator i = dat->accepting.find(dest);
327
328                 if (i == dat->accepting.end())
329                 {
330                         time_t now = time(NULL);
331                         /* +g and *not* accepted */
332                         user->WriteNumeric(716, "%s %s :is in +g mode (server-side ignore).", user->nick.c_str(), dest->nick.c_str());
333                         if (now > (dat->lastnotify + (time_t)notify_cooldown))
334                         {
335                                 user->WriteNumeric(717, "%s %s :has been informed that you messaged them.", user->nick.c_str(), dest->nick.c_str());
336                                 dest->WriteNumeric(718, "%s %s %s@%s :is messaging you, and you have umode +g", dest->nick.c_str(), user->nick.c_str(), user->ident.c_str(), user->dhost.c_str());
337                                 dat->lastnotify = now;
338                         }
339                         return 1;
340                 }
341                 return 0;
342         }
343
344         virtual int OnUserPreMessage(User* user, void* dest, int target_type, std::string& text, char status, CUList &exempt_list)
345         {
346                 if (IS_LOCAL(user) && target_type == TYPE_USER)
347                         return PreText(user, (User*)dest, text, true);
348
349                 return 0;
350         }
351
352         virtual int OnUserPreNotice(User* user, void* dest, int target_type, std::string& text, char status, CUList &exempt_list)
353         {
354                 if (IS_LOCAL(user) && target_type == TYPE_USER)
355                         return PreText(user, (User*)dest, text, true);
356
357                 return 0;
358         }
359
360         virtual void OnCleanup(int type, void* item)
361         {
362                 if (type != TYPE_USER)
363                         return;
364
365                 User* u = (User*)item;
366                 /* Cleanup only happens on unload (before dtor), so keep this O(n) instead of O(n^2) which deferring to OnUserQuit would do.  */
367                 RemoveData(u);
368         }
369
370         virtual void OnSyncUserMetaData(User* user, Module* proto, void* opaque, const std::string& extname, bool displayable)
371         {
372                 if (extname == "callerid_data")
373                 {
374                         callerid_data* dat = GetData(user, false);
375                         if (dat)
376                         {
377                                 std::string str = dat->ToString(displayable);
378                                 proto->ProtoSendMetaData(opaque, TYPE_USER, user, extname, str);
379                         }
380                 }
381         }
382
383         virtual void OnDecodeMetaData(int target_type, void* target, const std::string& extname, const std::string& extdata)
384         {
385                 if (target_type == TYPE_USER && extname == "callerid_data")
386                 {
387                         User* u = (User*)target;
388                         callerid_data* dat = new callerid_data(extdata, ServerInstance);
389                         u->Extend("callerid_data", dat);
390                 }
391         }
392
393         virtual int OnUserPreNick(User* user, const std::string& newnick)
394         {
395                 if (!tracknick)
396                         RemoveFromAllAccepts(ServerInstance, user);
397                 return 0;
398         }
399
400         virtual void OnUserQuit(User* user, const std::string& message, const std::string& oper_message)
401         {
402                 RemoveData(user);
403                 RemoveFromAllAccepts(ServerInstance, user);
404         }
405
406         virtual void OnRehash(User* user, const std::string& parameter)
407         {
408                 ConfigReader Conf(ServerInstance);
409                 maxaccepts = Conf.ReadInteger("callerid", "maxaccepts", "16", 0, true);
410                 operoverride = Conf.ReadFlag("callerid", "operoverride", "0", 0);
411                 tracknick = Conf.ReadFlag("callerid", "tracknick", "0", 0);
412                 notify_cooldown = Conf.ReadInteger("callerid", "cooldown", "60", 0, true);
413         }
414 };
415
416 MODULE_INIT(ModuleCallerID)
417
418