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