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