]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_swhois.cpp
3b150cd7dd5590bedef79ebc6901ef17a9cfccfe
[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         // Whenever the linking module receives metadata from another server and doesnt know what
98         // to do with it (of course, hence the 'meta') it calls this method, and it is up to each
99         // module in turn to figure out if this metadata key belongs to them, and what they want
100         // to do with it.
101         // In our case we're only sending a single string around, so we just construct a std::string.
102         // Some modules will probably get much more complex and format more detailed structs and classes
103         // in a textual way for sending over the link.
104         virtual void OnDecodeMetaData(int target_type, void* target, std::string extname, std::string extdata)
105         {
106                 // check if its our metadata key, and its associated with a user
107                 if ((target_type == TYPE_USER) && (extname == "swhois"))
108                 {
109                         userrec* dest = (userrec*)target;
110                         // if they dont already have an swhois field, accept the remote server's
111                         if (!dest->GetExt("swhois"))
112                         {
113                                 std::string* text = new std::string(extdata);
114                                 dest->Extend("swhois",(char*)text);
115                         }
116                 }
117         }
118         
119         virtual ~ModuleSWhois()
120         {
121         }
122         
123         virtual Version GetVersion()
124         {
125                 return Version(1,0,0,0,VF_STATIC|VF_VENDOR);
126         }
127         
128         virtual void OnUserConnect(userrec* user)
129         {
130         }
131
132 };
133
134
135 class ModuleSWhoisFactory : public ModuleFactory
136 {
137  public:
138         ModuleSWhoisFactory()
139         {
140         }
141         
142         ~ModuleSWhoisFactory()
143         {
144         }
145         
146         virtual Module * CreateModule(Server* Me)
147         {
148                 return new ModuleSWhois(Me);
149         }
150         
151 };
152
153
154 extern "C" void * init_module( void )
155 {
156         return new ModuleSWhoisFactory;
157 }
158