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