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