1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2007 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 * ---------------------------------------------------
19 /* $ModDesc: Provides the SWHOIS command which allows setting of arbitary WHOIS lines */
23 class cmd_swhois : public command_t
27 cmd_swhois (InspIRCd* Instance) : command_t(Instance,"SWHOIS",'o',2)
29 this->source = "m_swhois.so";
30 syntax = "<nick> <swhois>";
33 CmdResult Handle(const char** parameters, int pcnt, userrec* user)
35 userrec* dest = ServerInstance->FindNick(parameters[0]);
39 user->WriteServ("401 %s %s :No such nick/channel", user->nick, parameters[0]);
45 user->WriteServ("NOTICE %s :*** SWHOIS: Whois line must be specified", user->nick);
50 for (int i = 1; i < pcnt; i++)
55 line.append(parameters[i]);
59 dest->GetExt("swhois", text);
63 // We already had it set...
65 if (!ServerInstance->ULine(user->server))
66 // Ulines set SWHOISes silently
67 ServerInstance->WriteOpers("*** %s used SWHOIS to set %s's extra whois from '%s' to '%s'", user->nick, dest->nick, text->c_str(), line.c_str());
69 dest->Shrink("swhois");
72 else if (!ServerInstance->ULine(user->server))
74 // Ulines set SWHOISes silently
75 ServerInstance->WriteOpers("*** %s used SWHOIS to set %s's extra whois to '%s'", user->nick, dest->nick, line.c_str());
78 text = new std::string(line);
79 dest->Extend("swhois", text);
86 class ModuleSWhois : public Module
88 cmd_swhois* mycommand;
93 ModuleSWhois(InspIRCd* Me) : Module(Me)
96 Conf = new ConfigReader(ServerInstance);
97 mycommand = new cmd_swhois(ServerInstance);
98 ServerInstance->AddCommand(mycommand);
101 void OnRehash(userrec* user, const std::string ¶meter)
104 Conf = new ConfigReader(ServerInstance);
107 void Implements(char* List)
109 List[I_OnDecodeMetaData] = List[I_OnWhoisLine] = List[I_OnSyncUserMetaData] = List[I_OnUserQuit] = List[I_OnCleanup] = List[I_OnRehash] = List[I_OnPostCommand] = 1;
112 // :kenny.chatspike.net 320 Brain Azhrarn :is getting paid to play games.
113 int OnWhoisLine(userrec* user, userrec* dest, int &numeric, std::string &text)
115 /* We use this and not OnWhois because this triggers for remote, too */
118 /* Insert our numeric before 312 */
120 dest->GetExt("swhois", swhois);
123 ServerInstance->SendWhoisLine(user, dest, 320, "%s %s :%s",user->nick,dest->nick,swhois->c_str());
126 /* Dont block anything */
130 // Whenever the linking module wants to send out data, but doesnt know what the data
131 // represents (e.g. it is metadata, added to a userrec or chanrec by a module) then
132 // this method is called. We should use the ProtoSendMetaData function after we've
133 // corrected decided how the data should look, to send the metadata on its way if
135 virtual void OnSyncUserMetaData(userrec* user, Module* proto, void* opaque, const std::string &extname, bool displayable)
137 // check if the linking module wants to know about OUR metadata
138 if (extname == "swhois")
140 // check if this user has an swhois field to send
142 user->GetExt("swhois", swhois);
145 // call this function in the linking module, let it format the data how it
146 // sees fit, and send it on its way. We dont need or want to know how.
147 proto->ProtoSendMetaData(opaque,TYPE_USER,user,extname,*swhois);
152 // when a user quits, tidy up their metadata
153 virtual void OnUserQuit(userrec* user, const std::string &message, const std::string &oper_message)
156 user->GetExt("swhois", swhois);
159 user->Shrink("swhois");
164 // if the module is unloaded, tidy up all our dangling metadata
165 virtual void OnCleanup(int target_type, void* item)
167 if (target_type == TYPE_USER)
169 userrec* user = (userrec*)item;
171 user->GetExt("swhois", swhois);
174 user->Shrink("swhois");
180 // Whenever the linking module receives metadata from another server and doesnt know what
181 // to do with it (of course, hence the 'meta') it calls this method, and it is up to each
182 // module in turn to figure out if this metadata key belongs to them, and what they want
184 // In our case we're only sending a single string around, so we just construct a std::string.
185 // Some modules will probably get much more complex and format more detailed structs and classes
186 // in a textual way for sending over the link.
187 virtual void OnDecodeMetaData(int target_type, void* target, const std::string &extname, const std::string &extdata)
189 // check if its our metadata key, and its associated with a user
190 if ((target_type == TYPE_USER) && (extname == "swhois"))
192 userrec* dest = (userrec*)target;
193 // if they dont already have an swhois field, accept the remote server's
195 if (!dest->GetExt("swhois", text))
197 std::string* text = new std::string(extdata);
198 dest->Extend("swhois",text);
203 virtual void OnPostCommand(const std::string &command, const char **params, int pcnt, userrec *user, CmdResult result, const std::string &original_line)
205 if ((command != "OPER") || (result != CMD_SUCCESS))
210 for (int i = 0; i < Conf->Enumerate("oper"); i++)
212 std::string name = Conf->ReadValue("oper", "name", i);
214 if (name == params[0])
216 swhois = Conf->ReadValue("oper", "swhois", i);
221 if (!swhois.length())
223 for (int i = 0; i < Conf->Enumerate("type"); i++)
225 std::string type = Conf->ReadValue("type", "name", i);
227 if (type == user->oper)
229 swhois = Conf->ReadValue("type", "swhois", i);
236 if (user->GetExt("swhois", old))
238 user->Shrink("swhois");
242 if (!swhois.length())
245 std::string *text = new std::string(swhois);
246 user->Extend("swhois", text);
247 std::deque<std::string>* metadata = new std::deque<std::string>;
248 metadata->push_back(user->nick);
249 metadata->push_back("swhois"); // The metadata id
250 metadata->push_back(*text); // The value to send
251 Event event((char*)metadata,(Module*)this,"send_metadata");
252 event.Send(ServerInstance);
256 virtual ~ModuleSWhois()
261 virtual Version GetVersion()
263 return Version(1, 1, 0, 0, VF_COMMON | VF_VENDOR, API_VERSION);
267 MODULE_INIT(ModuleSWhois)