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