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