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