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