00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #include "inspircd.h"
00015 #include <set>
00016 #include <sstream>
00017 #include <algorithm>
00018
00019
00020
00021 class callerid_data : public classbase
00022 {
00023 public:
00024 time_t lastnotify;
00025 std::set<User*> accepting;
00026
00027 callerid_data() : lastnotify(0) { }
00028 callerid_data(const std::string& str, InspIRCd* ServerInstance)
00029 {
00030 irc::commasepstream s(str);
00031 std::string tok;
00032 if (s.GetToken(tok))
00033 {
00034 lastnotify = ConvToInt(tok);
00035 }
00036 while (s.GetToken(tok))
00037 {
00038 if (tok.empty())
00039 {
00040 continue;
00041 }
00042 User* u = ServerInstance->FindUUID(tok);
00043 if (!u)
00044 {
00045 u = ServerInstance->FindNick(tok);
00046 }
00047 if (!u)
00048 {
00049 continue;
00050 }
00051 accepting.insert(u);
00052 }
00053 }
00054
00055 std::string ToString(bool displayable) const
00056 {
00057 std::ostringstream oss;
00058 oss << lastnotify;
00059 for (std::set<User*>::const_iterator i = accepting.begin(); i != accepting.end(); ++i)
00060 {
00061
00062 oss << "," << (displayable ? (*i)->nick : (*i)->uuid);
00063 }
00064 oss << std::ends;
00065 return oss.str();
00066 }
00067 };
00068
00069 callerid_data* GetData(User* who, bool extend = true)
00070 {
00071 callerid_data* dat;
00072 if (who->GetExt("callerid_data", dat))
00073 return dat;
00074 else
00075 {
00076 if (extend)
00077 {
00078 dat = new callerid_data;
00079 who->Extend("callerid_data", dat);
00080 return dat;
00081 }
00082 else
00083 return NULL;
00084 }
00085 }
00086
00087 void RemoveData(User* who)
00088 {
00089 callerid_data* dat;
00090 who->GetExt("callerid_data", dat);
00091
00092 if (!dat)
00093 return;
00094
00095 who->Shrink("callerid_data");
00096 delete dat;
00097 }
00098
00099 void RemoveFromAllAccepts(InspIRCd* ServerInstance, User* who)
00100 {
00101 for (user_hash::iterator i = ServerInstance->Users->clientlist->begin(); i != ServerInstance->Users->clientlist->end(); ++i)
00102 {
00103 callerid_data* dat = GetData(i->second, false);
00104
00105 if (!dat)
00106 continue;
00107
00108 std::set<User*>::iterator iter = dat->accepting.find(who);
00109
00110 if (iter == dat->accepting.end())
00111 continue;
00112
00113 dat->accepting.erase(iter);
00114 }
00115 }
00116
00117 class User_g : public SimpleUserModeHandler
00118 {
00119 public:
00120 User_g(InspIRCd* Instance) : SimpleUserModeHandler(Instance, 'g') { }
00121 };
00122
00123 class CommandAccept : public Command
00124 {
00125 private:
00126 unsigned int& maxaccepts;
00127 public:
00128 CommandAccept(InspIRCd* Instance, unsigned int& max) : Command(Instance, "ACCEPT", 0, 1), maxaccepts(max)
00129 {
00130 source = "m_callerid.so";
00131 syntax = "{[+|-]<nicks>}|*}";
00132 TRANSLATE2(TR_CUSTOM, TR_END);
00133 }
00134
00135 virtual void EncodeParameter(std::string& parameter, int index)
00136 {
00137 if (index != 0)
00138 return;
00139 std::string out = "";
00140 irc::commasepstream nicks(parameter);
00141 std::string tok;
00142 while (nicks.GetToken(tok))
00143 {
00144 if (tok == "*")
00145 {
00146 continue;
00147 }
00148 if (!out.empty())
00149 out.append(",");
00150 bool dash = false;
00151 if (tok[0] == '-')
00152 {
00153 dash = true;
00154 tok.erase(0, 1);
00155 }
00156 User* u = ServerInstance->FindNick(tok);
00157 if (u)
00158 {
00159 if (dash)
00160 out.append("-");
00161 out.append(u->uuid);
00162 }
00163 else
00164 {
00165 if (dash)
00166 out.append("-");
00167 out.append(tok);
00168 }
00169 }
00170 parameter = out;
00171 }
00172
00178 CmdResult Handle(const std::vector<std::string> ¶meters, User* user)
00179 {
00180 if (ServerInstance->Parser->LoopCall(user, this, parameters, 0))
00181 return CMD_SUCCESS;
00182
00183
00184
00185 std::string tok = parameters[0];
00186
00187 if (tok == "*")
00188 {
00189 if (IS_LOCAL(user))
00190 ListAccept(user);
00191 return CMD_LOCALONLY;
00192 }
00193 else if (tok[0] == '-')
00194 {
00195 User* whotoremove = ServerInstance->FindNick(tok.substr(1));
00196 if (whotoremove)
00197 return (RemoveAccept(user, whotoremove, false) ? CMD_SUCCESS : CMD_FAILURE);
00198 else
00199 return CMD_FAILURE;
00200 }
00201 else
00202 {
00203 User* whotoadd = ServerInstance->FindNick(tok[0] == '+' ? tok.substr(1) : tok);
00204 if (whotoadd)
00205 return (AddAccept(user, whotoadd, false) ? CMD_SUCCESS : CMD_FAILURE);
00206 else
00207 {
00208 user->WriteNumeric(401, "%s %s :No such nick/channel", user->nick.c_str(), tok.c_str());
00209 return CMD_FAILURE;
00210 }
00211 }
00212 }
00213
00214 void ListAccept(User* user)
00215 {
00216 callerid_data* dat = GetData(user, false);
00217 if (dat)
00218 {
00219 for (std::set<User*>::iterator i = dat->accepting.begin(); i != dat->accepting.end(); ++i)
00220 user->WriteNumeric(281, "%s %s", user->nick.c_str(), (*i)->nick.c_str());
00221 }
00222 user->WriteNumeric(282, "%s :End of ACCEPT list", user->nick.c_str());
00223 }
00224
00225 bool AddAccept(User* user, User* whotoadd, bool quiet)
00226 {
00227 callerid_data* dat = GetData(user, true);
00228 if (dat->accepting.size() >= maxaccepts)
00229 {
00230 if (!quiet)
00231 user->WriteNumeric(456, "%s :Accept list is full (limit is %d)", user->nick.c_str(), maxaccepts);
00232
00233 return false;
00234 }
00235 if (!dat->accepting.insert(whotoadd).second)
00236 {
00237 if (!quiet)
00238 user->WriteNumeric(457, "%s %s :is already on your accept list", user->nick.c_str(), whotoadd->nick.c_str());
00239
00240 return false;
00241 }
00242
00243 user->WriteServ("NOTICE %s :%s is now on your accept list", user->nick.c_str(), whotoadd->nick.c_str());
00244 return true;
00245 }
00246
00247 bool RemoveAccept(User* user, User* whotoremove, bool quiet)
00248 {
00249 callerid_data* dat = GetData(user, false);
00250 if (!dat)
00251 {
00252 if (!quiet)
00253 user->WriteNumeric(458, "%s %s :is not on your accept list", user->nick.c_str(), whotoremove->nick.c_str());
00254
00255 return false;
00256 }
00257 std::set<User*>::iterator i = dat->accepting.find(whotoremove);
00258 if (i == dat->accepting.end())
00259 {
00260 if (!quiet)
00261 user->WriteNumeric(458, "%s %s :is not on your accept list", user->nick.c_str(), whotoremove->nick.c_str());
00262
00263 return false;
00264 }
00265
00266 user->WriteServ("NOTICE %s :%s is no longer on your accept list", user->nick.c_str(), whotoremove->nick.c_str());
00267 dat->accepting.erase(i);
00268 return true;
00269 }
00270 };
00271
00272 class ModuleCallerID : public Module
00273 {
00274 private:
00275 CommandAccept *mycommand;
00276 User_g* myumode;
00277
00278
00279 unsigned int maxaccepts;
00280 bool operoverride;
00281 bool tracknick;
00282 unsigned int notify_cooldown;
00283
00284 public:
00285 ModuleCallerID(InspIRCd* Me) : Module(Me)
00286 {
00287 OnRehash(NULL, "");
00288 mycommand = new CommandAccept(ServerInstance, maxaccepts);
00289 myumode = new User_g(ServerInstance);
00290
00291 if (!ServerInstance->Modes->AddMode(myumode))
00292 {
00293 delete mycommand;
00294 delete myumode;
00295 throw ModuleException("Could not add usermode +g");
00296 }
00297 try
00298 {
00299 ServerInstance->AddCommand(mycommand);
00300 }
00301 catch (const ModuleException& e)
00302 {
00303 delete mycommand;
00304 delete myumode;
00305 throw ModuleException("Could not add command!");
00306 }
00307
00308 Implementation eventlist[] = { I_OnRehash, I_OnUserPreNick, I_OnUserQuit, I_On005Numeric, I_OnUserPreNotice, I_OnUserPreMessage, I_OnCleanup };
00309 ServerInstance->Modules->Attach(eventlist, this, 7);
00310 }
00311
00312 virtual ~ModuleCallerID()
00313 {
00314 ServerInstance->Modes->DelMode(myumode);
00315 delete myumode;
00316 }
00317
00318 virtual Version GetVersion()
00319 {
00320 return Version("$Id: m_callerid.cpp 10783 2008-11-01 23:02:23Z w00t $", VF_COMMON | VF_VENDOR, API_VERSION);
00321 }
00322
00323 virtual void On005Numeric(std::string& output)
00324 {
00325 output += " CALLERID=g";
00326 }
00327
00328 int PreText(User* user, User* dest, std::string& text, bool notice)
00329 {
00330 if (!dest->IsModeSet('g'))
00331 return 0;
00332
00333 if (operoverride && IS_OPER(user))
00334 return 0;
00335
00336 callerid_data* dat = GetData(dest, true);
00337 std::set<User*>::iterator i = dat->accepting.find(user);
00338
00339 if (i == dat->accepting.end())
00340 {
00341 time_t now = ServerInstance->Time();
00342
00343 user->WriteNumeric(716, "%s %s :is in +g mode (server-side ignore).", user->nick.c_str(), dest->nick.c_str());
00344 if (now > (dat->lastnotify + (time_t)notify_cooldown))
00345 {
00346 user->WriteNumeric(717, "%s %s :has been informed that you messaged them.", user->nick.c_str(), dest->nick.c_str());
00347 dest->WriteNumeric(718, "%s %s %s@%s :is messaging you, and you have umode +g. Use /ACCEPT +%s to allow.", dest->nick.c_str(), user->nick.c_str(), user->ident.c_str(), user->dhost.c_str(), user->nick.c_str());
00348 dat->lastnotify = now;
00349 }
00350 return 1;
00351 }
00352 return 0;
00353 }
00354
00355 virtual int OnUserPreMessage(User* user, void* dest, int target_type, std::string& text, char status, CUList &exempt_list)
00356 {
00357 if (IS_LOCAL(user) && target_type == TYPE_USER)
00358 return PreText(user, (User*)dest, text, true);
00359
00360 return 0;
00361 }
00362
00363 virtual int OnUserPreNotice(User* user, void* dest, int target_type, std::string& text, char status, CUList &exempt_list)
00364 {
00365 if (IS_LOCAL(user) && target_type == TYPE_USER)
00366 return PreText(user, (User*)dest, text, true);
00367
00368 return 0;
00369 }
00370
00371 virtual void OnCleanup(int type, void* item)
00372 {
00373 if (type != TYPE_USER)
00374 return;
00375
00376 User* u = (User*)item;
00377
00378 RemoveData(u);
00379 }
00380
00381 virtual void OnSyncUserMetaData(User* user, Module* proto, void* opaque, const std::string& extname, bool displayable)
00382 {
00383 if (extname == "callerid_data")
00384 {
00385 callerid_data* dat = GetData(user, false);
00386 if (dat)
00387 {
00388 std::string str = dat->ToString(displayable);
00389 proto->ProtoSendMetaData(opaque, TYPE_USER, user, extname, str);
00390 }
00391 }
00392 }
00393
00394 virtual void OnDecodeMetaData(int target_type, void* target, const std::string& extname, const std::string& extdata)
00395 {
00396 if (target_type == TYPE_USER && extname == "callerid_data")
00397 {
00398 User* u = (User*)target;
00399 callerid_data* dat = new callerid_data(extdata, ServerInstance);
00400 u->Extend("callerid_data", dat);
00401 }
00402 }
00403
00404 virtual int OnUserPreNick(User* user, const std::string& newnick)
00405 {
00406 if (!tracknick)
00407 RemoveFromAllAccepts(ServerInstance, user);
00408 return 0;
00409 }
00410
00411 virtual void OnUserQuit(User* user, const std::string& message, const std::string& oper_message)
00412 {
00413 RemoveData(user);
00414 RemoveFromAllAccepts(ServerInstance, user);
00415 }
00416
00417 virtual void OnRehash(User* user, const std::string& parameter)
00418 {
00419 ConfigReader Conf(ServerInstance);
00420 maxaccepts = Conf.ReadInteger("callerid", "maxaccepts", "16", 0, true);
00421 operoverride = Conf.ReadFlag("callerid", "operoverride", "0", 0);
00422 tracknick = Conf.ReadFlag("callerid", "tracknick", "0", 0);
00423 notify_cooldown = Conf.ReadInteger("callerid", "cooldown", "60", 0, true);
00424 }
00425 };
00426
00427 MODULE_INIT(ModuleCallerID)
00428
00429