1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
7 * <brain@chatspike.net>
8 * <Craig@chatspike.net>
10 * Written by Craig Edwards, Craig McLure, and others.
11 * This program is free but copyrighted software; see
12 * the file COPYING for details.
14 * ---------------------------------------------------
20 #include "helperfuncs.h"
22 /* $ModDesc: Provides the SWHOIS command which allows setting of arbitary WHOIS lines */
24 class cmd_swhois : public command_t
28 cmd_swhois(Server* server) : command_t("SWHOIS",'o',2)
31 this->source = "m_swhois.so";
34 void Handle(char** parameters, int pcnt, userrec* user)
36 userrec* dest = Srv->FindNick(std::string(parameters[0]));
40 for(int i = 1; i < pcnt; i++)
45 line.append(parameters[i]);
48 std::string* text = (std::string*)dest->GetExt("swhois");
52 // We already had it set...
54 if (!Srv->IsUlined(user->server))
55 // Ulines set SWHOISes silently
56 WriteOpers("*** %s used SWHOIS to set %s's extra whois from '%s' to '%s'", user->nick, dest->nick, text->c_str(), line.c_str());
58 dest->Shrink("swhois");
61 else if(!Srv->IsUlined(user->server))
63 // Ulines set SWHOISes silently
64 WriteOpers("*** %s used SWHOIS to set %s's extra whois to '%s'", user->nick, dest->nick, line.c_str());
67 text = new std::string(line);
68 dest->Extend("swhois", (char*)text);
73 class ModuleSWhois : public Module
75 cmd_swhois* mycommand;
80 ModuleSWhois(Server* Me) : Module::Module(Me)
83 Conf = new ConfigReader();
84 mycommand = new cmd_swhois(Srv);
85 Srv->AddCommand(mycommand);
88 void OnRehash(std::string parameter)
91 Conf = new ConfigReader();
94 void Implements(char* List)
96 List[I_OnWhois] = List[I_OnSyncUserMetaData] = List[I_OnUserQuit] = List[I_OnCleanup] = List[I_OnRehash] = List[I_OnOper] = 1;
99 // :kenny.chatspike.net 320 Brain Azhrarn :is getting paid to play games.
100 virtual void OnWhois(userrec* source, userrec* dest)
102 char* desc = dest->GetExt("swhois");
105 std::string* swhois = (std::string*)desc;
106 WriteServ(source->fd,"320 %s %s :%s",source->nick,dest->nick,swhois->c_str());
110 // Whenever the linking module wants to send out data, but doesnt know what the data
111 // represents (e.g. it is metadata, added to a userrec or chanrec by a module) then
112 // this method is called. We should use the ProtoSendMetaData function after we've
113 // corrected decided how the data should look, to send the metadata on its way if
115 virtual void OnSyncUserMetaData(userrec* user, Module* proto, void* opaque, std::string extname)
117 // check if the linking module wants to know about OUR metadata
118 if (extname == "swhois")
120 // check if this user has an swhois field to send
121 char* field = user->GetExt("swhois");
124 // get our extdata out with a cast
125 std::string* swhois = (std::string*)field;
126 // call this function in the linking module, let it format the data how it
127 // sees fit, and send it on its way. We dont need or want to know how.
128 proto->ProtoSendMetaData(opaque,TYPE_USER,user,extname,*swhois);
133 // when a user quits, tidy up their metadata
134 virtual void OnUserQuit(userrec* user, std::string message)
136 char* field = user->GetExt("swhois");
139 std::string* swhois = (std::string*)field;
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)
150 userrec* user = (userrec*)item;
151 char* field = user->GetExt("swhois");
154 std::string* swhois = (std::string*)field;
155 user->Shrink("swhois");
161 // Whenever the linking module receives metadata from another server and doesnt know what
162 // to do with it (of course, hence the 'meta') it calls this method, and it is up to each
163 // module in turn to figure out if this metadata key belongs to them, and what they want
165 // In our case we're only sending a single string around, so we just construct a std::string.
166 // Some modules will probably get much more complex and format more detailed structs and classes
167 // in a textual way for sending over the link.
168 virtual void OnDecodeMetaData(int target_type, void* target, std::string extname, std::string extdata)
170 // check if its our metadata key, and its associated with a user
171 if ((target_type == TYPE_USER) && (extname == "swhois"))
173 userrec* dest = (userrec*)target;
174 // if they dont already have an swhois field, accept the remote server's
175 if (!dest->GetExt("swhois"))
177 std::string* text = new std::string(extdata);
178 dest->Extend("swhois",(char*)text);
183 virtual void OnOper(userrec* user, std::string opertype)
185 for(int i =0; i < Conf->Enumerate("type"); i++)
187 std::string type = Conf->ReadValue("type", "name", i);
189 if(strcmp(type.c_str(), user->oper) == 0)
191 std::string swhois = Conf->ReadValue("type", "swhois", i);
195 if(std::string* old = (std::string*)user->GetExt("swhois"))
197 user->Shrink("swhois");
201 std::string* text = new std::string(swhois);
202 user->Extend("swhois", (char*)text);
210 virtual ~ModuleSWhois()
215 virtual Version GetVersion()
217 return Version(1,0,0,0,VF_VENDOR);
222 class ModuleSWhoisFactory : public ModuleFactory
225 ModuleSWhoisFactory()
229 ~ModuleSWhoisFactory()
233 virtual Module * CreateModule(Server* Me)
235 return new ModuleSWhois(Me);
241 extern "C" void * init_module( void )
243 return new ModuleSWhoisFactory;