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, Module* Creator) : Command(Instance, Creator,"TITLE",0,2)
25 syntax = "<user> <password>";
26 TRANSLATE3(TR_NICK, TR_TEXT, TR_END);
29 bool OneOfMatches(const char* host, const char* ip, const char* hostlist)
31 std::stringstream hl(hostlist);
35 if (InspIRCd::Match(host, xhost, ascii_case_insensitive_map) || InspIRCd::MatchCIDR(ip, xhost, ascii_case_insensitive_map))
43 CmdResult Handle(const std::vector<std::string> ¶meters, User* user)
48 snprintf(TheHost,MAXBUF,"%s@%s",user->ident.c_str(), user->host.c_str());
49 snprintf(TheIP, MAXBUF,"%s@%s",user->ident.c_str(), user->GetIPString());
51 ConfigReader Conf(ServerInstance);
52 for (int i=0; i<Conf.Enumerate("title"); i++)
54 std::string name = Conf.ReadValue("title", "name", "", i);
55 std::string pass = Conf.ReadValue("title", "password", "", i);
56 std::string hash = Conf.ReadValue("title", "hash", "", i);
57 std::string host = Conf.ReadValue("title", "host", "*@*", i);
58 std::string title = Conf.ReadValue("title", "title", "", i);
59 std::string vhost = Conf.ReadValue("title", "vhost", "", i);
61 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())
64 if (user->GetExt("ctitle", text))
66 user->Shrink("ctitle");
70 text = new std::string(title);
71 user->Extend("ctitle", text);
73 ServerInstance->PI->SendMetaData(user, "ctitle", *text);
76 user->ChangeDisplayedHost(vhost.c_str());
78 user->WriteServ("NOTICE %s :Custom title set to '%s'",user->nick.c_str(), title.c_str());
84 user->WriteServ("NOTICE %s :Invalid title credentials",user->nick.c_str());
90 class ModuleCustomTitle : public Module
95 ModuleCustomTitle(InspIRCd* Me) : Module(Me), cmd(Me, this)
97 ServerInstance->AddCommand(&cmd);
98 Implementation eventlist[] = { I_OnDecodeMetaData, I_OnWhoisLine, I_OnSyncUser, I_OnUserQuit, I_OnCleanup };
99 ServerInstance->Modules->Attach(eventlist, this, 5);
103 // :kenny.chatspike.net 320 Brain Azhrarn :is getting paid to play games.
104 ModResult OnWhoisLine(User* user, User* dest, int &numeric, std::string &text)
106 /* We use this and not OnWhois because this triggers for remote, too */
109 /* Insert our numeric before 312 */
111 if (dest->GetExt("ctitle", ctitle))
113 ServerInstance->SendWhoisLine(user, dest, 320, "%s %s :%s",user->nick.c_str(), dest->nick.c_str(), ctitle->c_str());
116 /* Dont block anything */
117 return MOD_RES_PASSTHRU;
120 // Whenever the linking module wants to send out data, but doesnt know what the data
121 // represents (e.g. it is metadata, added to a User or Channel by a module) then
122 // this method is called. We should use the ProtoSendMetaData function after we've
123 // corrected decided how the data should look, to send the metadata on its way if
125 virtual void OnSyncUser(User* user, Module* proto, void* opaque)
127 // check if this user has an ctitle field to send
129 if (user->GetExt("ctitle", ctitle))
130 proto->ProtoSendMetaData(opaque,user,"ctitle",*ctitle);
133 // when a user quits, tidy up their metadata
134 virtual void OnUserQuit(User* user, const std::string &message, const std::string &oper_message)
137 if (user->GetExt("ctitle", ctitle))
139 user->Shrink("ctitle");
144 // if the module is unloaded, tidy up all our dangling metadata
145 virtual void OnCleanup(int target_type, void* item)
147 if (target_type == TYPE_USER)
149 User* user = (User*)item;
151 if (user->GetExt("ctitle", ctitle))
153 user->Shrink("ctitle");
159 // Whenever the linking module receives metadata from another server and doesnt know what
160 // to do with it (of course, hence the 'meta') it calls this method, and it is up to each
161 // module in turn to figure out if this metadata key belongs to them, and what they want
163 // In our case we're only sending a single string around, so we just construct a std::string.
164 // Some modules will probably get much more complex and format more detailed structs and classes
165 // in a textual way for sending over the link.
166 virtual void OnDecodeMetaData(Extensible* target, const std::string &extname, const std::string &extdata)
168 User* dest = dynamic_cast<User*>(target);
169 // check if its our metadata key, and its associated with a user
170 if (dest && (extname == "ctitle"))
173 if (dest->GetExt("ctitle", text))
175 dest->Shrink("ctitle");
178 if (!extdata.empty())
180 text = new std::string(extdata);
181 dest->Extend("ctitle", text);
186 virtual ~ModuleCustomTitle()
190 virtual Version GetVersion()
192 return Version("$Id$", VF_COMMON | VF_VENDOR, API_VERSION);
196 MODULE_INIT(ModuleCustomTitle)