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 SWHOIS command which allows setting of arbitary WHOIS lines */
20 class CommandSwhois : public Command
24 CommandSwhois (InspIRCd* Instance, Module* Creator) : Command(Instance, Creator,"SWHOIS","o",2, 2)
26 syntax = "<nick> :<swhois>";
27 TRANSLATE3(TR_NICK, TR_TEXT, TR_END);
30 CmdResult Handle(const std::vector<std::string> ¶meters, User* user)
32 User* dest = ServerInstance->FindNick(parameters[0]);
36 user->WriteNumeric(ERR_NOSUCHNICK, "%s %s :No such nick/channel", user->nick.c_str(), parameters[0].c_str());
41 if (dest->GetExt("swhois", text))
43 // We already had it set...
44 if (!ServerInstance->ULine(user->server))
45 // Ulines set SWHOISes silently
46 ServerInstance->SNO->WriteGlobalSno('a', "%s used SWHOIS to set %s's extra whois from '%s' to '%s'", user->nick.c_str(), dest->nick.c_str(), text->c_str(), parameters[1].c_str());
48 dest->Shrink("swhois");
51 else if (!ServerInstance->ULine(user->server))
53 // Ulines set SWHOISes silently
54 ServerInstance->SNO->WriteGlobalSno('a', "%s used SWHOIS to set %s's extra whois to '%s'", user->nick.c_str(), dest->nick.c_str(), parameters[1].c_str());
57 text = new std::string(parameters[1]);
58 dest->Extend("swhois", text);
60 /* Bug #376 - feature request -
61 * To cut down on the amount of commands services etc have to recognise, this only sends METADATA across the network now
62 * not an actual SWHOIS command. Any SWHOIS command sent from services will be automatically translated to METADATA by this.
63 * Sorry w00t i know this was your fix, but i got bored and wanted to clear down the tracker :)
66 ServerInstance->PI->SendMetaData(dest, "swhois", *text);
68 // If it's an empty swhois, unset it (not ideal, but ok)
71 dest->Shrink("swhois");
80 class ModuleSWhois : public Module
87 ModuleSWhois(InspIRCd* Me) : Module(Me), cmd(Me, this)
90 Conf = new ConfigReader(ServerInstance);
91 ServerInstance->AddCommand(&cmd);
92 Implementation eventlist[] = { I_OnDecodeMetaData, I_OnWhoisLine, I_OnSyncUser, I_OnUserQuit, I_OnCleanup, I_OnRehash, I_OnPostCommand };
93 ServerInstance->Modules->Attach(eventlist, this, 7);
96 void OnRehash(User* user)
99 Conf = new ConfigReader(ServerInstance);
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("swhois", swhois))
113 ServerInstance->SendWhoisLine(user, dest, 320, "%s %s :%s",user->nick.c_str(), dest->nick.c_str(), swhois->c_str());
117 /* Dont block anything */
118 return MOD_RES_PASSTHRU;
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 swhois field to send
130 if (user->GetExt("swhois", swhois))
131 proto->ProtoSendMetaData(opaque,user,"swhois",*swhois);
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("swhois", swhois))
140 user->Shrink("swhois");
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) return;
149 User* user = static_cast<User*>(item);
151 if (user && user->GetExt("swhois", swhois))
153 user->Shrink("swhois");
158 // Whenever the linking module receives metadata from another server and doesnt know what
159 // to do with it (of course, hence the 'meta') it calls this method, and it is up to each
160 // module in turn to figure out if this metadata key belongs to them, and what they want
162 // In our case we're only sending a single string around, so we just construct a std::string.
163 // Some modules will probably get much more complex and format more detailed structs and classes
164 // in a textual way for sending over the link.
165 virtual void OnDecodeMetaData(Extensible* target, const std::string &extname, const std::string &extdata)
167 User* dest = dynamic_cast<User*>(target);
168 // check if its our metadata key, and its associated with a user
169 if (extname != "swhois" || !dest)
172 // if they already have an swhois field, trash it and replace it with the remote one.
174 if (dest->GetExt("swhois", text))
176 dest->Shrink("swhois");
183 text = new std::string(extdata);
184 dest->Extend("swhois", text);
187 virtual void OnPostCommand(const std::string &command, const std::vector<std::string> ¶ms, User *user, CmdResult result, const std::string &original_line)
189 if ((command != "OPER") || (result != CMD_SUCCESS))
194 for (int i = 0; i < Conf->Enumerate("oper"); i++)
196 std::string name = Conf->ReadValue("oper", "name", i);
198 if (name == params[0])
200 swhois = Conf->ReadValue("oper", "swhois", i);
205 if (!swhois.length())
207 for (int i = 0; i < Conf->Enumerate("type"); i++)
209 std::string type = Conf->ReadValue("type", "name", i);
211 if (type == user->oper)
213 swhois = Conf->ReadValue("type", "swhois", i);
220 if (user->GetExt("swhois", old))
222 user->Shrink("swhois");
226 if (!swhois.length())
229 std::string *text = new std::string(swhois);
230 user->Extend("swhois", text);
231 ServerInstance->PI->SendMetaData(user, "swhois", *text);
234 virtual ~ModuleSWhois()
239 virtual Version GetVersion()
241 return Version("$Id$", VF_COMMON | VF_VENDOR, API_VERSION);
245 MODULE_INIT(ModuleSWhois)