]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_swhois.cpp
Made devoice not oper-only (Congrats to the original coder!)
[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 #include "users.h"
18 #include "channels.h"
19 #include "modules.h"
20 #include "helperfuncs.h"
21
22 /* $ModDesc: Provides the SWHOIS command which allows setting of arbitary WHOIS lines */
23
24 class cmd_swhois : public command_t
25 {
26         Server* Srv;
27  public:
28         cmd_swhois(Server* server) : command_t("SWHOIS",'o',2)
29         {
30                 this->Srv = server;
31                 this->source = "m_swhois.so";
32         }
33
34         void Handle(char** parameters, int pcnt, userrec* user)
35         {
36                 userrec* dest = Srv->FindNick(std::string(parameters[0]));
37                 if(dest)
38                 {
39                         std::string line;
40                         for(int i = 1; i < pcnt; i++)
41                         {
42                                 if (i != 1)
43                                         line.append(" ");
44                                         
45                                 line.append(parameters[i]);
46                         }
47                         
48                         std::string* text = (std::string*)dest->GetExt("swhois");
49         
50                         if(text)
51                         {
52                                 // We already had it set...
53                                                                 
54                                 WriteOpers("%s used SWHOIS to set %s's extra whois from '%s' to '%s'", user->nick, dest->nick, text->c_str(), line.c_str());
55                                 
56                                 dest->Shrink("swhois");
57                                 delete text;
58                         }
59                         else
60                         {
61                                 WriteOpers("%s used SWHOIS to set %s's extra whois to '%s'", user->nick, dest->nick, line.c_str());
62                         }
63                         
64                         text = new std::string(line);
65                         dest->Extend("swhois", (char*)text);
66                 }
67         }
68 };
69
70 class ModuleSWhois : public Module
71 {
72         cmd_swhois* mycommand;
73         Server* Srv;
74         ConfigReader* Conf;
75         
76  public:
77         ModuleSWhois(Server* Me) : Module::Module(Me)
78         {
79                 Srv = Me;
80                 Conf = new ConfigReader();
81                 mycommand = new cmd_swhois(Srv);
82                 Srv->AddCommand(mycommand);
83         }
84
85         void OnRehash(std::string parameter)
86         {
87                 delete Conf;
88                 Conf = new ConfigReader();
89         }
90
91         void Implements(char* List)
92         {
93                 List[I_OnWhois] = List[I_OnSyncUserMetaData] = List[I_OnUserQuit] = List[I_OnCleanup] = List[I_OnRehash] = List[I_OnOper] = 1;
94         }
95
96         // :kenny.chatspike.net 320 Brain Azhrarn :is getting paid to play games.
97         virtual void OnWhois(userrec* source, userrec* dest)
98         {
99                 char* desc = dest->GetExt("swhois");
100                 if (desc)
101                 {
102                         std::string* swhois = (std::string*)desc;
103                         WriteServ(source->fd,"320 %s %s :%s",source->nick,dest->nick,swhois->c_str());
104                 }
105         }
106
107         // Whenever the linking module wants to send out data, but doesnt know what the data
108         // represents (e.g. it is metadata, added to a userrec or chanrec by a module) then
109         // this method is called. We should use the ProtoSendMetaData function after we've
110         // corrected decided how the data should look, to send the metadata on its way if
111         // it is ours.
112         virtual void OnSyncUserMetaData(userrec* user, Module* proto, void* opaque, std::string extname)
113         {
114                 // check if the linking module wants to know about OUR metadata
115                 if (extname == "swhois")
116                 {
117                         // check if this user has an swhois field to send
118                         char* field = user->GetExt("swhois");
119                         if (field)
120                         {
121                                 // get our extdata out with a cast
122                                 std::string* swhois = (std::string*)field;
123                                 // call this function in the linking module, let it format the data how it
124                                 // sees fit, and send it on its way. We dont need or want to know how.
125                                 proto->ProtoSendMetaData(opaque,TYPE_USER,user,extname,*swhois);
126                         }
127                 }
128         }
129
130         // when a user quits, tidy up their metadata
131         virtual void OnUserQuit(userrec* user, std::string message)
132         {
133                 char* field = user->GetExt("swhois");
134                 if (field)
135                 {
136                         std::string* swhois = (std::string*)field;
137                         user->Shrink("swhois");
138                         delete swhois;
139                 }
140         }
141
142         // if the module is unloaded, tidy up all our dangling metadata
143         virtual void OnCleanup(int target_type, void* item)
144         {
145                 if (target_type == TYPE_USER)
146                 {
147                         userrec* user = (userrec*)item;
148                         char* field = user->GetExt("swhois");
149                         if (field)
150                         {
151                                 std::string* swhois = (std::string*)field;
152                                 user->Shrink("swhois");
153                                 delete swhois;
154                         }
155                 }
156         }
157
158         // Whenever the linking module receives metadata from another server and doesnt know what
159         // to do with it (of course, hence the 'meta') it calls this method, and it is up to each
160         // module in turn to figure out if this metadata key belongs to them, and what they want
161         // to do with it.
162         // In our case we're only sending a single string around, so we just construct a std::string.
163         // Some modules will probably get much more complex and format more detailed structs and classes
164         // in a textual way for sending over the link.
165         virtual void OnDecodeMetaData(int target_type, void* target, std::string extname, std::string extdata)
166         {
167                 // check if its our metadata key, and its associated with a user
168                 if ((target_type == TYPE_USER) && (extname == "swhois"))
169                 {
170                         userrec* dest = (userrec*)target;
171                         // if they dont already have an swhois field, accept the remote server's
172                         if (!dest->GetExt("swhois"))
173                         {
174                                 std::string* text = new std::string(extdata);
175                                 dest->Extend("swhois",(char*)text);
176                         }
177                 }
178         }
179         
180         virtual void OnOper(userrec* user, std::string opertype)
181         {
182                 for(int i =0; i < Conf->Enumerate("type"); i++)
183                 {
184                         std::string type = Conf->ReadValue("type", "name", i);
185                         
186                         if(strcmp(type.c_str(), user->oper) == 0)
187                         {
188                                 std::string swhois = Conf->ReadValue("type", "swhois", i);
189                                 
190                                 if(swhois.length())
191                                 {
192                                         if(std::string* old = (std::string*)user->GetExt("swhois"))
193                                         {
194                                                 user->Shrink("swhois");
195                                                 delete old;
196                                         }
197                         
198                                         std::string* text = new std::string(swhois);
199                                         user->Extend("swhois", (char*)text);
200                                         
201                                         break;
202                                 }
203                         }
204                 }               
205         }
206         
207         virtual ~ModuleSWhois()
208         {
209                 delete Conf;
210         }
211         
212         virtual Version GetVersion()
213         {
214                 return Version(1,0,0,0,VF_VENDOR);
215         }
216 };
217
218
219 class ModuleSWhoisFactory : public ModuleFactory
220 {
221  public:
222         ModuleSWhoisFactory()
223         {
224         }
225         
226         ~ModuleSWhoisFactory()
227         {
228         }
229         
230         virtual Module * CreateModule(Server* Me)
231         {
232                 return new ModuleSWhois(Me);
233         }
234         
235 };
236
237
238 extern "C" void * init_module( void )
239 {
240         return new ModuleSWhoisFactory;
241 }
242