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