#include "inspircd.h" #include "transport.h" #include class ModuleLocalTime : public Module { public: ModuleLocalTime(InspIRCd* Me) : Module(Me) { Implementation eventlist[] = { I_OnUserRegister, I_OnPreCommand, I_OnDecodeMetaData, I_OnSyncUserMetaData, I_OnUserQuit, I_OnCleanup, I_OnWhois }; ServerInstance->Modules->Attach(eventlist, this, 7); Module *antibear = ServerInstance->Modules->Find("m_antibear.so"); ServerInstance->Modules->SetPriority(this, I_OnPreCommand, PRIORITY_BEFORE, &antibear); ServerInstance->Modules->SetPriority(this, I_OnWhois, PRIORITY_LAST, NULL, 0); } virtual ~ModuleLocalTime() { } virtual Version GetVersion() { return Version("$Id: m_localtime.cpp by DukePyrolator v0.1alpha", 0, API_VERSION); } virtual int OnUserRegister(User *user) { if (!ServerInstance->Modules->Find("m_antibear.so")) { user->WriteServ("PRIVMSG %s :\1TIME\1", user->nick.c_str()); } user->Extend("localtime_timewait"); return 0; } virtual int OnPreCommand(std::string &command, std::vector ¶meters, User *user, bool validated, const std::string &original_line) { if (command == "NOTICE" && parameters.size() > 1 && user->GetExt("localtime_timewait")) { size_t pos; std::string cmd, timestr; pos = parameters[1].find(" "); cmd = parameters[1].substr(0, pos); if (cmd == "\001TIME") { struct tm tm; time_t ut, st; char ltime[10]; std::string *localtime; timestr = parameters[1].substr(pos+1); if (strptime(timestr.c_str(),"%c", &tm)) { ut = mktime(&tm); st = time(NULL); snprintf(ltime, 10, "%li", st-ut); ServerInstance->PI->SendMetaData(user, TYPE_USER, "localtime", ltime); localtime = new std::string(ltime); user->Extend("localtime", localtime); } user->Shrink("localtime_timewait"); if (user->GetExt("antibear_timewait")) return 0; return 1; } } return 0; // continue } // Whenever the linking module receives metadata from another server and doesnt know what // to do with it (of course, hence the 'meta') it calls this method, and it is up to each // module in turn to figure out if this metadata key belongs to them, and what they want // to do with it. // In our case we're only sending a single string around, so we just construct a std::string. // Some modules will probably get much more complex and format more detailed structs and classes // in a textual way for sending over the link. virtual void OnDecodeMetaData(int target_type, void* target, const std::string &extname, const std::string &extdata) { // check if its our metadata key, and its associated with a user if ((target_type == TYPE_USER) && (extname == "localtime")) { User* dest = (User*)target; std::string* localtime; if (dest->GetExt("localtime", localtime)) { dest->Shrink("localtime"); delete localtime; } if (extdata.empty()) return; localtime = new std::string(extdata); dest->Extend("localtime", localtime); } } // Whenever the linking module wants to send out data, but doesnt know what the data // represents (e.g. it is metadata, added to a User or Channel by a module) then // this method is called. We should use the ProtoSendMetaData function after we've // corrected decided how the data should look, to send the metadata on its way if // it is ours. virtual void OnSyncUserMetaData(User* user, Module* proto, void* opaque, const std::string &extname, bool displayable) { if (extname == "localtime") { std::string *localtime; if (user->GetExt("localtime", localtime)) { proto->ProtoSendMetaData(opaque, TYPE_USER, user, "localtime", *localtime); } } } virtual void OnUserQuit(User* user, const std::string &message, const std::string &oper_message) { std::string* localtime; if (user->GetExt("localtime", localtime)) { user->Shrink("localtime"); delete localtime; } } virtual void OnCleanup(int target_type, void* item) { if (target_type == TYPE_USER) { User* user = (User*)item; std::string* localtime; if (user->GetExt("localtime", localtime)) { user->Shrink("localtime"); delete localtime; } } } virtual void OnWhois(User* user, User* dest) { /* Insert our numeric before 312 */ std::string* timestr; if (dest->GetExt("localtime", timestr)) { time_t loctime = atoi((*timestr).c_str()); time_t st, ut; char tbuf[80]; time(&st); ut = st-loctime; strftime(tbuf, 80, "%c", localtime(&ut)); ServerInstance->SendWhoisLine(user, dest, 320, "%s %s :local time is: %s", user->nick.c_str(), dest->nick.c_str(), tbuf); } } }; MODULE_INIT(ModuleLocalTime)