]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_swhois.cpp
Updated copyrights in headers etc using perl inplace edit
[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 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         void Implements(char* List)
75         {
76                 List[I_OnWhois] = List[I_OnSyncUserMetaData] = List[I_OnUserQuit] = List[I_OnCleanup] = 1;
77         }
78
79         // :kenny.chatspike.net 320 Brain Azhrarn :is getting paid to play games.
80         virtual void OnWhois(userrec* source, userrec* dest)
81         {
82                 char* desc = dest->GetExt("swhois");
83                 if (desc)
84                 {
85                         std::string* swhois = (std::string*)desc;
86                         WriteServ(source->fd,"320 %s %s :%s",source->nick,dest->nick,swhois->c_str());
87                 }
88         }
89
90         // Whenever the linking module wants to send out data, but doesnt know what the data
91         // represents (e.g. it is metadata, added to a userrec or chanrec by a module) then
92         // this method is called. We should use the ProtoSendMetaData function after we've
93         // corrected decided how the data should look, to send the metadata on its way if
94         // it is ours.
95         virtual void OnSyncUserMetaData(userrec* user, Module* proto, void* opaque, std::string extname)
96         {
97                 // check if the linking module wants to know about OUR metadata
98                 if (extname == "swhois")
99                 {
100                         // check if this user has an swhois field to send
101                         char* field = user->GetExt("swhois");
102                         if (field)
103                         {
104                                 // get our extdata out with a cast
105                                 std::string* swhois = (std::string*)field;
106                                 // call this function in the linking module, let it format the data how it
107                                 // sees fit, and send it on its way. We dont need or want to know how.
108                                 proto->ProtoSendMetaData(opaque,TYPE_USER,user,extname,*swhois);
109                         }
110                 }
111         }
112
113         // when a user quits, tidy up their metadata
114         virtual void OnUserQuit(userrec* user, std::string message)
115         {
116                 char* field = user->GetExt("swhois");
117                 if (field)
118                 {
119                         std::string* swhois = (std::string*)field;
120                         user->Shrink("swhois");
121                         delete swhois;
122                 }
123         }
124
125         // if the module is unloaded, tidy up all our dangling metadata
126         virtual void OnCleanup(int target_type, void* item)
127         {
128                 if (target_type == TYPE_USER)
129                 {
130                         userrec* user = (userrec*)item;
131                         char* field = user->GetExt("swhois");
132                         if (field)
133                         {
134                                 std::string* swhois = (std::string*)field;
135                                 user->Shrink("swhois");
136                                 delete swhois;
137                         }
138                 }
139         }
140
141         // Whenever the linking module receives metadata from another server and doesnt know what
142         // to do with it (of course, hence the 'meta') it calls this method, and it is up to each
143         // module in turn to figure out if this metadata key belongs to them, and what they want
144         // to do with it.
145         // In our case we're only sending a single string around, so we just construct a std::string.
146         // Some modules will probably get much more complex and format more detailed structs and classes
147         // in a textual way for sending over the link.
148         virtual void OnDecodeMetaData(int target_type, void* target, std::string extname, std::string extdata)
149         {
150                 // check if its our metadata key, and its associated with a user
151                 if ((target_type == TYPE_USER) && (extname == "swhois"))
152                 {
153                         userrec* dest = (userrec*)target;
154                         // if they dont already have an swhois field, accept the remote server's
155                         if (!dest->GetExt("swhois"))
156                         {
157                                 std::string* text = new std::string(extdata);
158                                 dest->Extend("swhois",(char*)text);
159                         }
160                 }
161         }
162         
163         virtual ~ModuleSWhois()
164         {
165         }
166         
167         virtual Version GetVersion()
168         {
169                 return Version(1,0,0,0,VF_VENDOR);
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