1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2009 InspIRCd Development Team
6 * See: http://wiki.inspircd.org/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
16 /* $ModDesc: Provides the TITLE command which allows setting of CUSTOM WHOIS TITLE line */
20 class CommandTitle : public Command
23 CommandTitle (InspIRCd* Instance) : Command(Instance,"TITLE",0,2)
25 this->source = "m_customtitle.so";
26 syntax = "<user> <password>";
27 TRANSLATE3(TR_NICK, TR_TEXT, TR_END);
30 bool OneOfMatches(const char* host, const char* ip, const char* hostlist)
32 std::stringstream hl(hostlist);
36 if (InspIRCd::Match(host, xhost, ascii_case_insensitive_map) || InspIRCd::MatchCIDR(ip, xhost, ascii_case_insensitive_map))
44 CmdResult Handle(const std::vector<std::string> ¶meters, User* user)
49 snprintf(TheHost,MAXBUF,"%s@%s",user->ident.c_str(), user->host.c_str());
50 snprintf(TheIP, MAXBUF,"%s@%s",user->ident.c_str(), user->GetIPString());
52 ConfigReader Conf(ServerInstance);
53 for (int i=0; i<Conf.Enumerate("title"); i++)
55 std::string name = Conf.ReadValue("title", "name", "", i);
56 std::string pass = Conf.ReadValue("title", "password", "", i);
57 std::string hash = Conf.ReadValue("title", "hash", "", i);
58 std::string host = Conf.ReadValue("title", "host", "*@*", i);
59 std::string title = Conf.ReadValue("title", "title", "", i);
60 std::string vhost = Conf.ReadValue("title", "vhost", "", i);
62 if (!strcmp(name.c_str(),parameters[0].c_str()) && !ServerInstance->PassCompare(user, pass.c_str(), parameters[1].c_str(), hash.c_str()) && OneOfMatches(TheHost,TheIP,host.c_str()) && !title.empty())
65 if (user->GetExt("ctitle", text))
67 user->Shrink("ctitle");
71 text = new std::string(title);
72 user->Extend("ctitle", text);
74 ServerInstance->PI->SendMetaData(user, "ctitle", *text);
77 user->ChangeDisplayedHost(vhost.c_str());
79 user->WriteServ("NOTICE %s :Custom title set to '%s'",user->nick.c_str(), title.c_str());
85 user->WriteServ("NOTICE %s :Invalid title credentials",user->nick.c_str());
91 class ModuleCustomTitle : public Module
96 ModuleCustomTitle(InspIRCd* Me) : Module(Me), cmd(Me)
98 ServerInstance->AddCommand(&cmd);
99 Implementation eventlist[] = { I_OnDecodeMetaData, I_OnWhoisLine, I_OnSyncUser, I_OnUserQuit, I_OnCleanup };
100 ServerInstance->Modules->Attach(eventlist, this, 5);
104 // :kenny.chatspike.net 320 Brain Azhrarn :is getting paid to play games.
105 int OnWhoisLine(User* user, User* dest, int &numeric, std::string &text)
107 /* We use this and not OnWhois because this triggers for remote, too */
110 /* Insert our numeric before 312 */
112 if (dest->GetExt("ctitle", ctitle))
114 ServerInstance->SendWhoisLine(user, dest, 320, "%s %s :%s",user->nick.c_str(), dest->nick.c_str(), ctitle->c_str());
117 /* Dont block anything */
121 // Whenever the linking module wants to send out data, but doesnt know what the data
122 // represents (e.g. it is metadata, added to a User or Channel by a module) then
123 // this method is called. We should use the ProtoSendMetaData function after we've
124 // corrected decided how the data should look, to send the metadata on its way if
126 virtual void OnSyncUser(User* user, Module* proto, void* opaque)
128 // check if this user has an ctitle field to send
130 if (user->GetExt("ctitle", ctitle))
131 proto->ProtoSendMetaData(opaque,user,"ctitle",*ctitle);
134 // when a user quits, tidy up their metadata
135 virtual void OnUserQuit(User* user, const std::string &message, const std::string &oper_message)
138 if (user->GetExt("ctitle", ctitle))
140 user->Shrink("ctitle");
145 // if the module is unloaded, tidy up all our dangling metadata
146 virtual void OnCleanup(int target_type, void* item)
148 if (target_type == TYPE_USER)
150 User* user = (User*)item;
152 if (user->GetExt("ctitle", ctitle))
154 user->Shrink("ctitle");
160 // Whenever the linking module receives metadata from another server and doesnt know what
161 // to do with it (of course, hence the 'meta') it calls this method, and it is up to each
162 // module in turn to figure out if this metadata key belongs to them, and what they want
164 // In our case we're only sending a single string around, so we just construct a std::string.
165 // Some modules will probably get much more complex and format more detailed structs and classes
166 // in a textual way for sending over the link.
167 virtual void OnDecodeMetaData(Extensible* target, const std::string &extname, const std::string &extdata)
169 User* dest = dynamic_cast<User*>(target);
170 // check if its our metadata key, and its associated with a user
171 if (dest && (extname == "ctitle"))
174 if (dest->GetExt("ctitle", text))
176 dest->Shrink("ctitle");
179 if (!extdata.empty())
181 text = new std::string(extdata);
182 dest->Extend("ctitle", text);
187 virtual ~ModuleCustomTitle()
191 virtual Version GetVersion()
193 return Version("$Id$", VF_COMMON | VF_VENDOR, API_VERSION);
197 MODULE_INIT(ModuleCustomTitle)