]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_swhois.cpp
15fe19264866e30dfd3727e5c83db9dfb788f747
[user/henk/code/inspircd.git] / src / modules / m_swhois.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  Inspire is copyright (C) 2002-2005 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 using namespace std;
18
19 #include <stdio.h>
20 #include "users.h"
21 #include "channels.h"
22 #include "modules.h"
23 #include "helperfuncs.h"
24
25 /* $ModDesc: Provides the SWHOIS command which allows setting of arbitary WHOIS lines */
26
27 Server *Srv;
28
29 void handle_swhois(char **parameters, int pcnt, userrec *user)
30 {
31         userrec* dest = Srv->FindNick(std::string(parameters[0]));
32         if (dest)
33         {
34                 std::string line = "";
35                 for (int i = 1; i < pcnt; i++)
36                 {
37                         if (i != 1)
38                                 line = line + " ";
39                         line = line + std::string(parameters[i]);
40                 }
41                 char* field = dest->GetExt("swhois");
42                 if (field)
43                 {
44                         std::string* text = (std::string*)field;
45                         dest->Shrink("swhois");
46                         delete text;
47                 }
48                 std::string* text = new std::string(line);
49                 dest->Extend("swhois",(char*)text);
50         }
51 }
52
53 class ModuleSWhois : public Module
54 {
55  public:
56         ModuleSWhois(Server* Me)
57                 : Module::Module(Me)
58         {
59                 Srv = Me;
60                 Srv->AddCommand("SWHOIS",handle_swhois,'o',2,"m_swhois.so");
61         }
62
63         // :kenny.chatspike.net 320 Brain Azhrarn :is getting paid to play games.
64         virtual void OnWhois(userrec* source, userrec* dest)
65         {
66                 char* desc = dest->GetExt("swhois");
67                 if (desc)
68                 {
69                         std::string* swhois = (std::string*)desc;
70                         WriteServ(source->fd,"320 %s %s :%s",source->nick,dest->nick,swhois->c_str());
71                 }
72         }
73
74         // Whenever the linking module wants to send out data, but doesnt know what the data
75         // represents (e.g. it is metadata, added to a userrec or chanrec by a module) then
76         // this method is called. We should use the ProtoSendMetaData function after we've
77         // corrected decided how the data should look, to send the metadata on its way if
78         // it is ours.
79         virtual void OnSyncUserMetaData(userrec* user, Module* proto, void* opaque, std::string extname)
80         {
81                 // check if the linking module wants to know about OUR metadata
82                 if (extname == "swhois")
83                 {
84                         // check if this user has an swhois field to send
85                         char* field = user->GetExt("swhois");
86                         if (field)
87                         {
88                                 // get our extdata out with a cast
89                                 std::string* swhois = (std::string*)field;
90                                 // call this function in the linking module, let it format the data how it
91                                 // sees fit, and send it on its way. We dont need or want to know how.
92                                 proto->ProtoSendMetaData(opaque,TYPE_USER,user,extname,*swhois);
93                         }
94                 }
95         }
96
97         // when a user quits, tidy up their metadata
98         virtual void OnUserQuit(userrec* user, std::string message)
99         {
100                 char* field = user->GetExt("swhois");
101                 if (field)
102                 {
103                         std::string* swhois = (std::string*)field;
104                         user->Shrink("swhois");
105                         delete swhois;
106                 }
107         }
108
109         // if the module is unloaded, tidy up all our dangling metadata
110         virtual void OnCleanup(int target_type, void* item)
111         {
112                 if (target_type == TYPE_USER)
113                 {
114                         userrec* u = (userrec*)item;
115                         char* field = user->GetExt("swhois");
116                         if (field)
117                         {
118                                 std::string* swhois = (std::string*)field;
119                                 user->Shrink("swhois");
120                                 delete swhois;
121                         }
122                 }
123         }
124
125         // Whenever the linking module receives metadata from another server and doesnt know what
126         // to do with it (of course, hence the 'meta') it calls this method, and it is up to each
127         // module in turn to figure out if this metadata key belongs to them, and what they want
128         // to do with it.
129         // In our case we're only sending a single string around, so we just construct a std::string.
130         // Some modules will probably get much more complex and format more detailed structs and classes
131         // in a textual way for sending over the link.
132         virtual void OnDecodeMetaData(int target_type, void* target, std::string extname, std::string extdata)
133         {
134                 // check if its our metadata key, and its associated with a user
135                 if ((target_type == TYPE_USER) && (extname == "swhois"))
136                 {
137                         userrec* dest = (userrec*)target;
138                         // if they dont already have an swhois field, accept the remote server's
139                         if (!dest->GetExt("swhois"))
140                         {
141                                 std::string* text = new std::string(extdata);
142                                 dest->Extend("swhois",(char*)text);
143                         }
144                 }
145         }
146         
147         virtual ~ModuleSWhois()
148         {
149         }
150         
151         virtual Version GetVersion()
152         {
153                 return Version(1,0,0,0,VF_STATIC|VF_VENDOR);
154         }
155         
156         virtual void OnUserConnect(userrec* user)
157         {
158         }
159
160 };
161
162
163 class ModuleSWhoisFactory : public ModuleFactory
164 {
165  public:
166         ModuleSWhoisFactory()
167         {
168         }
169         
170         ~ModuleSWhoisFactory()
171         {
172         }
173         
174         virtual Module * CreateModule(Server* Me)
175         {
176                 return new ModuleSWhois(Me);
177         }
178         
179 };
180
181
182 extern "C" void * init_module( void )
183 {
184         return new ModuleSWhoisFactory;
185 }
186