]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_swhois.cpp
Now with binary versioning goodness
[user/henk/code/inspircd.git] / src / modules / m_swhois.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
6  *                       E-mail:
7  *                <brain@chatspike.net>
8  *                <Craig@chatspike.net>
9  *     
10  * Written by Craig Edwards, Craig McLure, and others.
11  * This program is free but copyrighted software; see
12  *            the file COPYING for details.
13  *
14  * ---------------------------------------------------
15  */
16
17 #include "users.h"
18 #include "channels.h"
19 #include "modules.h"
20
21 #include "inspircd.h"
22
23 /* $ModDesc: Provides the SWHOIS command which allows setting of arbitary WHOIS lines */
24
25
26
27 class cmd_swhois : public command_t
28 {
29         
30  public:
31         cmd_swhois (InspIRCd* Instance) : command_t(Instance,"SWHOIS",'o',2)
32         {
33                 this->source = "m_swhois.so";
34                 syntax = "<nick> <swhois>";
35         }
36
37         CmdResult Handle(const char** parameters, int pcnt, userrec* user)
38         {
39                 userrec* dest = ServerInstance->FindNick(parameters[0]);
40                 if(dest)
41                 {
42                         std::string line;
43                         for(int i = 1; i < pcnt; i++)
44                         {
45                                 if (i != 1)
46                                         line.append(" ");
47                                         
48                                 line.append(parameters[i]);
49                         }
50                         
51                         std::string* text;
52                         dest->GetExt("swhois", text);
53         
54                         if(text)
55                         {
56                                 // We already had it set...
57                                 
58                                 if (!ServerInstance->ULine(user->server))
59                                         // Ulines set SWHOISes silently
60                                         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());
61                                 
62                                 dest->Shrink("swhois");
63                                 DELETE(text);
64                         }
65                         else if(!ServerInstance->ULine(user->server))
66                         {
67                                 // Ulines set SWHOISes silently
68                                 ServerInstance->WriteOpers("*** %s used SWHOIS to set %s's extra whois to '%s'", user->nick, dest->nick, line.c_str());
69                         }
70                         
71                         text = new std::string(line);
72                         dest->Extend("swhois", text);
73
74                         return CMD_SUCCESS;
75                 }
76
77                 return CMD_FAILURE;
78         }
79 };
80
81 class ModuleSWhois : public Module
82 {
83         cmd_swhois* mycommand;
84         
85         ConfigReader* Conf;
86         
87  public:
88         ModuleSWhois(InspIRCd* Me) : Module::Module(Me)
89         {
90                 
91                 Conf = new ConfigReader(ServerInstance);
92                 mycommand = new cmd_swhois(ServerInstance);
93                 ServerInstance->AddCommand(mycommand);
94         }
95
96         void OnRehash(const std::string &parameter)
97         {
98                 DELETE(Conf);
99                 Conf = new ConfigReader(ServerInstance);
100         }
101
102         void Implements(char* List)
103         {
104                 List[I_OnWhois] = List[I_OnSyncUserMetaData] = List[I_OnUserQuit] = List[I_OnCleanup] = List[I_OnRehash] = List[I_OnOper] = 1;
105         }
106
107         // :kenny.chatspike.net 320 Brain Azhrarn :is getting paid to play games.
108         virtual void OnWhois(userrec* source, userrec* dest)
109         {
110                 std::string* swhois;
111                 dest->GetExt("swhois", swhois);
112                 if (swhois)
113                 {
114                         source->WriteServ("320 %s %s :%s",source->nick,dest->nick,swhois->c_str());
115                 }
116         }
117
118         // Whenever the linking module wants to send out data, but doesnt know what the data
119         // represents (e.g. it is metadata, added to a userrec or chanrec by a module) then
120         // this method is called. We should use the ProtoSendMetaData function after we've
121         // corrected decided how the data should look, to send the metadata on its way if
122         // it is ours.
123         virtual void OnSyncUserMetaData(userrec* user, Module* proto, void* opaque, const std::string &extname)
124         {
125                 // check if the linking module wants to know about OUR metadata
126                 if (extname == "swhois")
127                 {
128                         // check if this user has an swhois field to send
129                         std::string* swhois;
130                         user->GetExt("swhois", swhois);
131                         if (swhois)
132                         {
133                                 // call this function in the linking module, let it format the data how it
134                                 // sees fit, and send it on its way. We dont need or want to know how.
135                                 proto->ProtoSendMetaData(opaque,TYPE_USER,user,extname,*swhois);
136                         }
137                 }
138         }
139
140         // when a user quits, tidy up their metadata
141         virtual void OnUserQuit(userrec* user, const std::string &message)
142         {
143                 std::string* swhois;
144                 user->GetExt("swhois", swhois);
145                 if (swhois)
146                 {
147                         user->Shrink("swhois");
148                         DELETE(swhois);
149                 }
150         }
151
152         // if the module is unloaded, tidy up all our dangling metadata
153         virtual void OnCleanup(int target_type, void* item)
154         {
155                 if (target_type == TYPE_USER)
156                 {
157                         userrec* user = (userrec*)item;
158                         std::string* swhois;
159                         user->GetExt("swhois", swhois);
160                         if (swhois)
161                         {
162                                 user->Shrink("swhois");
163                                 DELETE(swhois);
164                         }
165                 }
166         }
167
168         // Whenever the linking module receives metadata from another server and doesnt know what
169         // to do with it (of course, hence the 'meta') it calls this method, and it is up to each
170         // module in turn to figure out if this metadata key belongs to them, and what they want
171         // to do with it.
172         // In our case we're only sending a single string around, so we just construct a std::string.
173         // Some modules will probably get much more complex and format more detailed structs and classes
174         // in a textual way for sending over the link.
175         virtual void OnDecodeMetaData(int target_type, void* target, const std::string &extname, const std::string &extdata)
176         {
177                 // check if its our metadata key, and its associated with a user
178                 if ((target_type == TYPE_USER) && (extname == "swhois"))
179                 {
180                         userrec* dest = (userrec*)target;
181                         // if they dont already have an swhois field, accept the remote server's
182                         std::string* text;
183                         if (!dest->GetExt("swhois", text))
184                         {
185                                 std::string* text = new std::string(extdata);
186                                 dest->Extend("swhois",text);
187                         }
188                 }
189         }
190         
191         virtual void OnOper(userrec* user, const std::string &opertype)
192         {
193                 for(int i =0; i < Conf->Enumerate("type"); i++)
194                 {
195                         std::string type = Conf->ReadValue("type", "name", i);
196                         
197                         if(strcmp(type.c_str(), user->oper) == 0)
198                         {
199                                 std::string swhois = Conf->ReadValue("type", "swhois", i);
200                                 
201                                 if(swhois.length())
202                                 {
203                                         std::string* old;
204                                         if(user->GetExt("swhois", old))
205                                         {
206                                                 user->Shrink("swhois");
207                                                 DELETE(old);
208                                         }
209                         
210                                         std::string* text = new std::string(swhois);
211                                         user->Extend("swhois", text);
212                                         
213                                         break;
214                                 }
215                         }
216                 }               
217         }
218         
219         virtual ~ModuleSWhois()
220         {
221                 DELETE(Conf);
222         }
223         
224         virtual Version GetVersion()
225         {
226                 return Version(1,0,0,0,VF_VENDOR,API_VERSION);
227         }
228 };
229
230
231 class ModuleSWhoisFactory : public ModuleFactory
232 {
233  public:
234         ModuleSWhoisFactory()
235         {
236         }
237         
238         ~ModuleSWhoisFactory()
239         {
240         }
241         
242         virtual Module * CreateModule(InspIRCd* Me)
243         {
244                 return new ModuleSWhois(Me);
245         }
246         
247 };
248
249
250 extern "C" void * init_module( void )
251 {
252         return new ModuleSWhoisFactory;
253 }
254