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