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