8 /* $ModDesc: Implementation of callerid (umode +g & /accept, ala hybrid etc) */
14 std::set<User*> accepting;
17 callerid_data* GetData(User* who, bool extend = true)
20 if (who->GetExt("callerid_data", dat))
28 dat = new callerid_data;
29 dat->lastnotify = 0; // Can't init in struct.
30 who->Extend("callerid_data", dat);
40 void RemoveData(User* who)
43 who->GetExt("callerid_data", dat);
45 who->Shrink("callerid_data");
49 void RemoveFromAllAccepts(InspIRCd* ServerInstance, User* who)
51 for (user_hash::iterator i = ServerInstance->clientlist->begin(); i != ServerInstance->clientlist->end(); ++i)
53 callerid_data* dat = GetData(i->second, false);
55 std::set<User*>& accepting = dat->accepting;
56 std::set<User*>::iterator i = accepting.find(who);
57 if (i == accepting.end()) continue;
62 class CommandAccept : public Command
65 unsigned int& maxaccepts;
67 CommandAccept(InspIRCd* Instance, unsigned int& max) : Command(Instance, "ACCEPT", 0, 1), maxaccepts(max)
69 source = "m_callerid.so";
70 syntax = "{[+|-]<nicks>}|*}";
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
78 CmdResult Handle(const char** parameters, int pcnt, User* user)
82 /* Command stuff should've dealt with this already */
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)
90 const char* arg = parameters[i];
91 irc::commasepstream css(arg);
93 while (css.GetToken(tok))
99 if (IS_LOCAL(user)) continue;
102 else if (tok[0] == '-')
104 User* whotoremove = ServerInstance->FindNick(tok.substr(1));
107 atleastonechange = RemoveAccept(user, whotoremove, false) || atleastonechange;
112 User* whotoadd = ServerInstance->FindNick(tok[0] == '+' ? tok.substr(1) : tok);
115 atleastonechange = AddAccept(user, whotoadd, false) || atleastonechange;
119 user->WriteServ("401 %s %s :No such nick/channel", user->nick, tok.c_str());
124 return atleastonechange ? CMD_FAILURE : CMD_SUCCESS;
127 void ListAccept(User* user)
129 callerid_data* dat = GetData(user, false);
132 for (std::set<User*>::iterator i = dat->accepting.begin(); i != dat->accepting.end(); ++i)
134 user->WriteServ("281 %s %s", user->nick, (*i)->nick);
137 user->WriteServ("282 %s :End of ACCEPT list", user->nick);
140 bool AddAccept(User* user, User* whotoadd, bool quiet)
142 callerid_data* dat = GetData(user, true);
143 std::set<User*>& accepting = dat->accepting;
144 if (accepting.size() >= maxaccepts)
146 if (!quiet) user->WriteServ("456 %s :Accept list is full (limit is %d)", user->nick, maxaccepts);
149 if (!accepting.insert(whotoadd).second)
151 if (!quiet) user->WriteServ("457 %s %s :is already on your accept list", user->nick, whotoadd->nick);
157 bool RemoveAccept(User* user, User* whotoremove, bool quiet)
159 callerid_data* dat = GetData(user, false);
162 if (!quiet) user->WriteServ("458 %s %s :is not on your accept list", user->nick, whotoremove->nick);
165 std::set<User*>& accepting = dat->accepting;
166 std::set<User*>::iterator i = accepting.find(whotoremove);
167 if (i == accepting.end())
169 if (!quiet) user->WriteServ("458 %s %s :is not on your accept list", user->nick, whotoremove->nick);
177 class ModuleCallerID : public Module
180 CommandAccept *mycommand;
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.
189 ModuleCallerID(InspIRCd* Me) : Module(Me)
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);
205 return Version(1, 0, 0, 0, VF_COMMON | VF_VENDOR, API_VERSION);
208 void On005Numeric(std::string& output)
210 output += " CALLERID=g";
213 int PreText(User* user, User* dest, std::string& text, bool notice)
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())
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))
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);
237 int OnUserPreMessage(User* user, void* dest, int target_type, std::string& text, char status, CUList &exempt_list)
239 if (IS_LOCAL(user) && target_type == TYPE_USER)
240 return PreText(user, (User*)dest, text, true);
244 int OnUserPreNotice(User* user, void* dest, int target_type, std::string& text, char status, CUList &exempt_list)
246 if (IS_LOCAL(user) && target_type == TYPE_USER)
247 return PreText(user, (User*)dest, text, true);
251 int OnUserPreNick(User* user, const std::string& newnick)
254 RemoveFromAllAccepts(ServerInstance, user);
258 void OnUserQuit(User* user, const std::string& message, const std::string& oper_message)
261 RemoveFromAllAccepts(ServerInstance, user);
264 void OnRehash(User* user, const std::string& parameter)
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())
273 case CONF_VALUE_NOT_FOUND:
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");
284 throw ModuleException("Invalid maxaccepts value, unknown config error");
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())
294 case CONF_VALUE_NOT_FOUND:
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");
305 throw ModuleException("Invalid maxaccepts value, unknown config error");
307 maxaccepts = new_maxaccepts;
308 notify_cooldown = new_cooldown;
309 operoverride = new_override;
310 tracknick = new_track;
314 MODULE_INIT(ModuleCallerID)