1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2008 InspIRCd Development Team
6 * See: http://www.inspircd.org/wiki/index.php/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
17 /* $ModDesc: Provides the TITLE command which allows setting of CUSTOM WHOIS TITLE line */
21 class CommandTitle : public Command
24 CommandTitle (InspIRCd* Instance) : Command(Instance,"TITLE",0,2)
26 this->source = "m_customtitle.so";
27 syntax = "<user> <password>";
28 TRANSLATE3(TR_NICK, TR_TEXT, TR_END);
31 bool OneOfMatches(const char* host, const char* ip, const char* hostlist)
33 std::stringstream hl(hostlist);
37 if (match(host, xhost) || match(ip,xhost, true))
45 CmdResult Handle(const std::vector<std::string> ¶meters, User* user)
53 snprintf(TheHost,MAXBUF,"%s@%s",user->ident.c_str(), user->host.c_str());
54 snprintf(TheIP, MAXBUF,"%s@%s",user->ident.c_str(), user->GetIPString());
56 ConfigReader Conf(ServerInstance);
57 for (int i=0; i<Conf.Enumerate("title"); i++)
59 std::string name = Conf.ReadValue("title", "name", "", i);
60 std::string pass = Conf.ReadValue("title", "password", "", i);
61 std::string hash = Conf.ReadValue("title", "hash", "", i);
62 std::string host = Conf.ReadValue("title", "host", "*@*", i);
63 std::string title = Conf.ReadValue("title", "title", "", i);
64 std::string vhost = Conf.ReadValue("title", "vhost", "", i);
66 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())
69 if (user->GetExt("ctitle", text))
71 user->Shrink("ctitle");
75 text = new std::string(title);
76 user->Extend("ctitle", text);
78 ServerInstance->PI->SendMetaData(user, TYPE_USER, "ctitle", *text);
81 user->ChangeDisplayedHost(vhost.c_str());
83 if (!ServerInstance->ULine(user->server))
84 // Ulines set TITLEs silently
85 ServerInstance->SNO->WriteToSnoMask('A', "%s used TITLE to set custom title '%s'",user->nick.c_str(),title.c_str());
87 user->WriteServ("NOTICE %s :Custom title set to '%s'",user->nick.c_str(), title.c_str());
93 if (!ServerInstance->ULine(user->server))
94 // Ulines also fail TITLEs silently
95 ServerInstance->SNO->WriteToSnoMask('A', "Failed TITLE attempt by %s!%s@%s using login '%s'", user->nick.c_str(), user->ident.c_str(), user->host.c_str(), parameters[0].c_str());
97 user->WriteServ("NOTICE %s :Invalid title credentials",user->nick.c_str());
103 class ModuleCustomTitle : public Module
105 CommandTitle* mycommand;
108 ModuleCustomTitle(InspIRCd* Me) : Module(Me)
111 mycommand = new CommandTitle(ServerInstance);
112 ServerInstance->AddCommand(mycommand);
113 Implementation eventlist[] = { I_OnDecodeMetaData, I_OnWhoisLine, I_OnSyncUserMetaData, I_OnUserQuit, I_OnCleanup };
114 ServerInstance->Modules->Attach(eventlist, this, 5);
118 // :kenny.chatspike.net 320 Brain Azhrarn :is getting paid to play games.
119 int OnWhoisLine(User* user, User* dest, int &numeric, std::string &text)
121 /* We use this and not OnWhois because this triggers for remote, too */
124 /* Insert our numeric before 312 */
126 if (dest->GetExt("ctitle", ctitle))
128 ServerInstance->SendWhoisLine(user, dest, 320, "%s %s :%s",user->nick.c_str(), dest->nick.c_str(), ctitle->c_str());
131 /* Dont block anything */
135 // Whenever the linking module wants to send out data, but doesnt know what the data
136 // represents (e.g. it is metadata, added to a User or Channel by a module) then
137 // this method is called. We should use the ProtoSendMetaData function after we've
138 // corrected decided how the data should look, to send the metadata on its way if
140 virtual void OnSyncUserMetaData(User* user, Module* proto, void* opaque, const std::string &extname, bool displayable)
142 // check if the linking module wants to know about OUR metadata
143 if (extname == "ctitle")
145 // check if this user has an ctitle field to send
147 if (user->GetExt("ctitle", ctitle))
149 // call this function in the linking module, let it format the data how it
150 // sees fit, and send it on its way. We dont need or want to know how.
151 proto->ProtoSendMetaData(opaque,TYPE_USER,user,extname,*ctitle);
156 // when a user quits, tidy up their metadata
157 virtual void OnUserQuit(User* user, const std::string &message, const std::string &oper_message)
160 if (user->GetExt("ctitle", ctitle))
162 user->Shrink("ctitle");
167 // if the module is unloaded, tidy up all our dangling metadata
168 virtual void OnCleanup(int target_type, void* item)
170 if (target_type == TYPE_USER)
172 User* user = (User*)item;
174 if (user->GetExt("ctitle", ctitle))
176 user->Shrink("ctitle");
182 // Whenever the linking module receives metadata from another server and doesnt know what
183 // to do with it (of course, hence the 'meta') it calls this method, and it is up to each
184 // module in turn to figure out if this metadata key belongs to them, and what they want
186 // In our case we're only sending a single string around, so we just construct a std::string.
187 // Some modules will probably get much more complex and format more detailed structs and classes
188 // in a textual way for sending over the link.
189 virtual void OnDecodeMetaData(int target_type, void* target, const std::string &extname, const std::string &extdata)
191 // check if its our metadata key, and its associated with a user
192 if ((target_type == TYPE_USER) && (extname == "ctitle"))
194 User* dest = (User*)target;
195 // if they dont already have an ctitle field, accept the remote server's
197 if (!dest->GetExt("ctitle", text))
199 std::string* ntext = new std::string(extdata);
200 dest->Extend("ctitle",ntext);
205 virtual ~ModuleCustomTitle()
209 virtual Version GetVersion()
211 return Version(1, 2, 0, 0, VF_COMMON | VF_VENDOR, API_VERSION);
215 MODULE_INIT(ModuleCustomTitle)