]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_swhois.cpp
2178ad97a32829861338181d9cc31df084f44a90
[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 "inspircd.h"
15 #include "users.h"
16 #include "channels.h"
17 #include "modules.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                 
37                 if (!dest)
38                 {
39                         user->WriteServ("401 %s %s :No such nick/channel", user->nick, parameters[0]);
40                         return CMD_FAILURE;
41                 }
42
43                 if (!*parameters[1])
44                 {
45                         user->WriteServ("NOTICE %s :*** SWHOIS: Whois line must be specified", user->nick);
46                         return CMD_FAILURE;
47                 }
48                 
49                 std::string line;
50                 for (int i = 1; i < pcnt; i++)
51                 {
52                         if (i != 1)
53                                 line.append(" ");
54                                 
55                         line.append(parameters[i]);
56                 }
57                 
58                 std::string* text;
59                 dest->GetExt("swhois", text);
60
61                 if (text)
62                 {
63                         // We already had it set...
64                         
65                         if (!ServerInstance->ULine(user->server))
66                                 // Ulines set SWHOISes silently
67                                 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());
68                         
69                         dest->Shrink("swhois");
70                         DELETE(text);
71                 }
72                 else if (!ServerInstance->ULine(user->server))
73                 {
74                         // Ulines set SWHOISes silently
75                         ServerInstance->WriteOpers("*** %s used SWHOIS to set %s's extra whois to '%s'", user->nick, dest->nick, line.c_str());
76                 }
77                 
78                 text = new std::string(line);
79                 dest->Extend("swhois", text);
80
81                 /* Bug #376 - feature request -
82                  * To cut down on the amount of commands services etc have to recognise, this only sends METADATA across the network now
83                  * not an actual SWHOIS command. Any SWHOIS command sent from services will be automatically translated to METADATA by this.
84                  * Sorry w00t i know this was your fix, but i got bored and wanted to clear down the tracker :)
85                  * -- Brain
86                  */
87                 std::deque<std::string>* metadata = new std::deque<std::string>;
88                 metadata->push_back(dest->nick);
89                 metadata->push_back("swhois");          // The metadata id
90                 metadata->push_back(*text);             // The value to send
91                 Event event((char*)metadata,(Module*)this,"send_metadata");
92                 event.Send(ServerInstance);
93                 delete metadata;
94
95                 return CMD_LOCALONLY;
96         }
97
98 };
99
100 class ModuleSWhois : public Module
101 {
102         cmd_swhois* mycommand;
103         
104         ConfigReader* Conf;
105         
106  public:
107         ModuleSWhois(InspIRCd* Me) : Module(Me)
108         {
109                 
110                 Conf = new ConfigReader(ServerInstance);
111                 mycommand = new cmd_swhois(ServerInstance);
112                 ServerInstance->AddCommand(mycommand);
113         }
114
115         void OnRehash(userrec* user, const std::string &parameter)
116         {
117                 DELETE(Conf);
118                 Conf = new ConfigReader(ServerInstance);
119         }
120
121         void Implements(char* List)
122         {
123                 List[I_OnDecodeMetaData] = List[I_OnWhoisLine] = List[I_OnSyncUserMetaData] = List[I_OnUserQuit] = List[I_OnCleanup] = List[I_OnRehash] = List[I_OnPostCommand] = 1;
124         }
125
126         // :kenny.chatspike.net 320 Brain Azhrarn :is getting paid to play games.
127         int OnWhoisLine(userrec* user, userrec* dest, int &numeric, std::string &text)
128         {
129                 /* We use this and not OnWhois because this triggers for remote, too */
130                 if (numeric == 312)
131                 {
132                         /* Insert our numeric before 312 */
133                         std::string* swhois;
134                         dest->GetExt("swhois", swhois);
135                         if (swhois)
136                         {
137                                 ServerInstance->SendWhoisLine(user, dest, 320, "%s %s :%s",user->nick,dest->nick,swhois->c_str());
138                         }
139                 }
140                 /* Dont block anything */
141                 return 0;
142         }
143
144         // Whenever the linking module wants to send out data, but doesnt know what the data
145         // represents (e.g. it is metadata, added to a userrec or chanrec by a module) then
146         // this method is called. We should use the ProtoSendMetaData function after we've
147         // corrected decided how the data should look, to send the metadata on its way if
148         // it is ours.
149         virtual void OnSyncUserMetaData(userrec* user, Module* proto, void* opaque, const std::string &extname, bool displayable)
150         {
151                 // check if the linking module wants to know about OUR metadata
152                 if (extname == "swhois")
153                 {
154                         // check if this user has an swhois field to send
155                         std::string* swhois;
156                         user->GetExt("swhois", swhois);
157                         if (swhois)
158                         {
159                                 // call this function in the linking module, let it format the data how it
160                                 // sees fit, and send it on its way. We dont need or want to know how.
161                                 proto->ProtoSendMetaData(opaque,TYPE_USER,user,extname,*swhois);
162                         }
163                 }
164         }
165
166         // when a user quits, tidy up their metadata
167         virtual void OnUserQuit(userrec* user, const std::string &message, const std::string &oper_message)
168         {
169                 std::string* swhois;
170                 user->GetExt("swhois", swhois);
171                 if (swhois)
172                 {
173                         user->Shrink("swhois");
174                         DELETE(swhois);
175                 }
176         }
177
178         // if the module is unloaded, tidy up all our dangling metadata
179         virtual void OnCleanup(int target_type, void* item)
180         {
181                 if (target_type == TYPE_USER)
182                 {
183                         userrec* user = (userrec*)item;
184                         std::string* swhois;
185                         user->GetExt("swhois", swhois);
186                         if (swhois)
187                         {
188                                 user->Shrink("swhois");
189                                 DELETE(swhois);
190                         }
191                 }
192         }
193
194         // Whenever the linking module receives metadata from another server and doesnt know what
195         // to do with it (of course, hence the 'meta') it calls this method, and it is up to each
196         // module in turn to figure out if this metadata key belongs to them, and what they want
197         // to do with it.
198         // In our case we're only sending a single string around, so we just construct a std::string.
199         // Some modules will probably get much more complex and format more detailed structs and classes
200         // in a textual way for sending over the link.
201         virtual void OnDecodeMetaData(int target_type, void* target, const std::string &extname, const std::string &extdata)
202         {
203                 // check if its our metadata key, and its associated with a user
204                 if ((target_type == TYPE_USER) && (extname == "swhois"))
205                 {
206                         userrec* dest = (userrec*)target;
207                         // if they dont already have an swhois field, accept the remote server's
208                         std::string* text;
209                         if (!dest->GetExt("swhois", text))
210                         {
211                                 std::string* text = new std::string(extdata);
212                                 dest->Extend("swhois",text);
213                         }
214                 }
215         }
216         
217         virtual void OnPostCommand(const std::string &command, const char **params, int pcnt, userrec *user, CmdResult result, const std::string &original_line)
218         {
219                 if ((command != "OPER") || (result != CMD_SUCCESS))
220                         return;
221                 
222                 std::string swhois;
223                 
224                 for (int i = 0; i < Conf->Enumerate("oper"); i++)
225                 {
226                         std::string name = Conf->ReadValue("oper", "name", i);
227                         
228                         if (name == params[0])
229                         {
230                                 swhois = Conf->ReadValue("oper", "swhois", i);
231                                 break;
232                         }
233                 }
234                 
235                 if (!swhois.length())
236                 {
237                         for (int i = 0; i < Conf->Enumerate("type"); i++)
238                         {
239                                 std::string type = Conf->ReadValue("type", "name", i);
240                                 
241                                 if (type == user->oper)
242                                 {
243                                         swhois = Conf->ReadValue("type", "swhois", i);
244                                         break;
245                                 }
246                         }
247                 }
248
249                 std::string *old;
250                 if (user->GetExt("swhois", old))
251                 {
252                         user->Shrink("swhois");
253                         DELETE(old);
254                 }
255                 
256                 if (!swhois.length())
257                         return;
258                 
259                 std::string *text = new std::string(swhois);
260                 user->Extend("swhois", text);
261                 std::deque<std::string>* metadata = new std::deque<std::string>;
262                 metadata->push_back(user->nick);
263                 metadata->push_back("swhois");          // The metadata id
264                 metadata->push_back(*text);             // The value to send
265                 Event event((char*)metadata,(Module*)this,"send_metadata");
266                 event.Send(ServerInstance);
267                 delete metadata;
268         }
269
270         virtual ~ModuleSWhois()
271         {
272                 DELETE(Conf);
273         }
274         
275         virtual Version GetVersion()
276         {
277                 return Version(1, 1, 0, 0, VF_COMMON | VF_VENDOR, API_VERSION);
278         }
279 };
280
281 MODULE_INIT(ModuleSWhois)