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