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
25 CommandTitle (InspIRCd* Instance) : Command(Instance,"TITLE",0,2)
27 this->source = "m_customtitle.so";
28 syntax = "<user> <password>";
29 TRANSLATE3(TR_NICK, TR_TEXT, TR_END);
32 bool OneOfMatches(const char* host, const char* ip, const char* hostlist)
34 std::stringstream hl(hostlist);
38 if (match(host,xhost.c_str()) || match(ip,xhost.c_str(),true))
46 CmdResult Handle(const char* const* parameters, int pcnt, User* user)
54 snprintf(TheHost,MAXBUF,"%s@%s",user->ident,user->host);
55 snprintf(TheIP, MAXBUF,"%s@%s",user->ident,user->GetIPString());
57 ConfigReader Conf(ServerInstance);
58 for (int i=0; i<Conf.Enumerate("title"); i++)
60 std::string name = Conf.ReadValue("title", "name", "", i);
61 std::string pass = Conf.ReadValue("title", "password", "", i);
62 std::string hash = Conf.ReadValue("title", "hash", "", i);
63 std::string host = Conf.ReadValue("title", "host", "*@*", i);
64 std::string title = Conf.ReadValue("title", "title", "", i);
65 std::string vhost = Conf.ReadValue("title", "vhost", "", i);
67 if (!strcmp(name.c_str(),parameters[0]) && !ServerInstance->PassCompare(user, pass.c_str(), parameters[1], hash.c_str()) && OneOfMatches(TheHost,TheIP,host.c_str()) && !title.empty())
70 user->GetExt("ctitle", text);
74 user->Shrink("ctitle");
78 text = new std::string(title);
79 user->Extend("ctitle", text);
81 std::deque<std::string>* metadata = new std::deque<std::string>;
82 metadata->push_back(user->nick);
83 metadata->push_back("ctitle"); // The metadata id
84 metadata->push_back(*text); // The value to send
85 Event event((char*)metadata,(Module*)this,"send_metadata");
86 event.Send(ServerInstance);
90 user->ChangeDisplayedHost(vhost.c_str());
92 if (!ServerInstance->ULine(user->server))
93 // Ulines set TITLEs silently
94 ServerInstance->SNO->WriteToSnoMask('A', "%s used TITLE to set custom title '%s'",user->nick,title.c_str());
96 user->WriteServ("NOTICE %s :Custom title set to '%s'",user->nick, title.c_str());
102 if (!ServerInstance->ULine(user->server))
103 // Ulines also fail TITLEs silently
104 ServerInstance->SNO->WriteToSnoMask('A', "Failed TITLE attempt by %s!%s@%s using login '%s'",user->nick,user->ident,user->host,parameters[0]);
106 user->WriteServ("NOTICE %s :Invalid title credentials",user->nick);
112 class ModuleCustomTitle : public Module
114 CommandTitle* mycommand;
117 ModuleCustomTitle(InspIRCd* Me) : Module(Me)
120 mycommand = new CommandTitle(ServerInstance);
121 ServerInstance->AddCommand(mycommand);
122 Implementation eventlist[] = { I_OnDecodeMetaData, I_OnWhoisLine, I_OnSyncUserMetaData, I_OnUserQuit, I_OnCleanup };
123 ServerInstance->Modules->Attach(eventlist, this, 5);
127 // :kenny.chatspike.net 320 Brain Azhrarn :is getting paid to play games.
128 int OnWhoisLine(User* user, User* dest, int &numeric, std::string &text)
130 /* We use this and not OnWhois because this triggers for remote, too */
133 /* Insert our numeric before 312 */
135 dest->GetExt("ctitle", ctitle);
138 ServerInstance->SendWhoisLine(user, dest, 320, "%s %s :%s",user->nick,dest->nick,ctitle->c_str());
141 /* Dont block anything */
145 // Whenever the linking module wants to send out data, but doesnt know what the data
146 // represents (e.g. it is metadata, added to a User or Channel by a module) then
147 // this method is called. We should use the ProtoSendMetaData function after we've
148 // corrected decided how the data should look, to send the metadata on its way if
150 virtual void OnSyncUserMetaData(User* user, Module* proto, void* opaque, const std::string &extname, bool displayable)
152 // check if the linking module wants to know about OUR metadata
153 if (extname == "ctitle")
155 // check if this user has an ctitle field to send
157 user->GetExt("ctitle", ctitle);
160 // call this function in the linking module, let it format the data how it
161 // sees fit, and send it on its way. We dont need or want to know how.
162 proto->ProtoSendMetaData(opaque,TYPE_USER,user,extname,*ctitle);
167 // when a user quits, tidy up their metadata
168 virtual void OnUserQuit(User* user, const std::string &message, const std::string &oper_message)
171 user->GetExt("ctitle", ctitle);
174 user->Shrink("ctitle");
179 // if the module is unloaded, tidy up all our dangling metadata
180 virtual void OnCleanup(int target_type, void* item)
182 if (target_type == TYPE_USER)
184 User* user = (User*)item;
186 user->GetExt("ctitle", ctitle);
189 user->Shrink("ctitle");
195 // Whenever the linking module receives metadata from another server and doesnt know what
196 // to do with it (of course, hence the 'meta') it calls this method, and it is up to each
197 // module in turn to figure out if this metadata key belongs to them, and what they want
199 // In our case we're only sending a single string around, so we just construct a std::string.
200 // Some modules will probably get much more complex and format more detailed structs and classes
201 // in a textual way for sending over the link.
202 virtual void OnDecodeMetaData(int target_type, void* target, const std::string &extname, const std::string &extdata)
204 // check if its our metadata key, and its associated with a user
205 if ((target_type == TYPE_USER) && (extname == "ctitle"))
207 User* dest = (User*)target;
208 // if they dont already have an ctitle field, accept the remote server's
210 if (!dest->GetExt("ctitle", text))
212 std::string* ntext = new std::string(extdata);
213 dest->Extend("ctitle",ntext);
218 virtual ~ModuleCustomTitle()
222 virtual Version GetVersion()
224 return Version(1, 1, 0, 0, VF_COMMON | VF_VENDOR, API_VERSION);
228 MODULE_INIT(ModuleCustomTitle)