]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_swhois.cpp
cf14be3e1dde68e4dc12cf4e8fad90765b9f636e
[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, Module* Creator) : Command(Instance, Creator,"SWHOIS","o",2, 2)
25         {
26                 syntax = "<nick> :<swhois>";
27                 TRANSLATE3(TR_NICK, TR_TEXT, TR_END);
28         }
29
30         CmdResult Handle(const std::vector<std::string> &parameters, User* user)
31         {
32                 User* dest = ServerInstance->FindNick(parameters[0]);
33
34                 if (!dest)
35                 {
36                         user->WriteNumeric(ERR_NOSUCHNICK, "%s %s :No such nick/channel", user->nick.c_str(), parameters[0].c_str());
37                         return CMD_FAILURE;
38                 }
39
40                 std::string* text;
41                 if (dest->GetExt("swhois", text))
42                 {
43                         // We already had it set...
44                         if (!ServerInstance->ULine(user->server))
45                                 // Ulines set SWHOISes silently
46                                 ServerInstance->SNO->WriteGlobalSno('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());
47
48                         dest->Shrink("swhois");
49                         delete text;
50                 }
51                 else if (!ServerInstance->ULine(user->server))
52                 {
53                         // Ulines set SWHOISes silently
54                         ServerInstance->SNO->WriteGlobalSno('a', "%s used SWHOIS to set %s's extra whois to '%s'", user->nick.c_str(), dest->nick.c_str(), parameters[1].c_str());
55                 }
56
57                 text = new std::string(parameters[1]);
58                 dest->Extend("swhois", text);
59
60                 /* Bug #376 - feature request -
61                  * To cut down on the amount of commands services etc have to recognise, this only sends METADATA across the network now
62                  * not an actual SWHOIS command. Any SWHOIS command sent from services will be automatically translated to METADATA by this.
63                  * Sorry w00t i know this was your fix, but i got bored and wanted to clear down the tracker :)
64                  * -- Brain
65                  */
66                 ServerInstance->PI->SendMetaData(dest, "swhois", *text);
67
68                 // If it's an empty swhois, unset it (not ideal, but ok)
69                 if (text->empty())
70                 {
71                         dest->Shrink("swhois");
72                         delete text;
73                 }
74
75                 return CMD_LOCALONLY;
76         }
77
78 };
79
80 class ModuleSWhois : public Module
81 {
82         CommandSwhois cmd;
83
84         ConfigReader* Conf;
85
86  public:
87         ModuleSWhois(InspIRCd* Me) : Module(Me), cmd(Me, this)
88         {
89
90                 Conf = new ConfigReader(ServerInstance);
91                 ServerInstance->AddCommand(&cmd);
92                 Implementation eventlist[] = { I_OnDecodeMetaData, I_OnWhoisLine, I_OnSyncUser, I_OnUserQuit, I_OnCleanup, I_OnRehash, I_OnPostCommand };
93                 ServerInstance->Modules->Attach(eventlist, this, 7);
94         }
95
96         void OnRehash(User* user)
97         {
98                 delete Conf;
99                 Conf = new ConfigReader(ServerInstance);
100         }
101
102
103         // :kenny.chatspike.net 320 Brain Azhrarn :is getting paid to play games.
104         ModResult OnWhoisLine(User* user, User* dest, int &numeric, std::string &text)
105         {
106                 /* We use this and not OnWhois because this triggers for remote, too */
107                 if (numeric == 312)
108                 {
109                         /* Insert our numeric before 312 */
110                         std::string* swhois;
111                         if (dest->GetExt("swhois", swhois))
112                         {
113                                 ServerInstance->SendWhoisLine(user, dest, 320, "%s %s :%s",user->nick.c_str(), dest->nick.c_str(), swhois->c_str());
114                         }
115                 }
116
117                 /* Dont block anything */
118                 return MOD_RES_PASSTHRU;
119         }
120
121         // Whenever the linking module wants to send out data, but doesnt know what the data
122         // represents (e.g. it is metadata, added to a User or Channel by a module) then
123         // this method is called. We should use the ProtoSendMetaData function after we've
124         // corrected decided how the data should look, to send the metadata on its way if
125         // it is ours.
126         virtual void OnSyncUser(User* user, Module* proto, void* opaque)
127         {
128                 // check if this user has an swhois field to send
129                 std::string* swhois;
130                 if (user->GetExt("swhois", swhois))
131                         proto->ProtoSendMetaData(opaque,user,"swhois",*swhois);
132         }
133
134         // when a user quits, tidy up their metadata
135         virtual void OnUserQuit(User* user, const std::string &message, const std::string &oper_message)
136         {
137                 std::string* swhois;
138                 if (user->GetExt("swhois", swhois))
139                 {
140                         user->Shrink("swhois");
141                         delete swhois;
142                 }
143         }
144
145         // if the module is unloaded, tidy up all our dangling metadata
146         virtual void OnCleanup(int target_type, void* item)
147         {
148                 if (target_type != TYPE_USER) return;
149                 User* user = static_cast<User*>(item);
150                 std::string* swhois;
151                 if (user && user->GetExt("swhois", swhois))
152                 {
153                         user->Shrink("swhois");
154                         delete swhois;
155                 }
156         }
157
158         // Whenever the linking module receives metadata from another server and doesnt know what
159         // to do with it (of course, hence the 'meta') it calls this method, and it is up to each
160         // module in turn to figure out if this metadata key belongs to them, and what they want
161         // to do with it.
162         // In our case we're only sending a single string around, so we just construct a std::string.
163         // Some modules will probably get much more complex and format more detailed structs and classes
164         // in a textual way for sending over the link.
165         virtual void OnDecodeMetaData(Extensible* target, const std::string &extname, const std::string &extdata)
166         {
167                 User* dest = dynamic_cast<User*>(target);
168                 // check if its our metadata key, and its associated with a user
169                 if (extname != "swhois" || !dest)
170                         return;
171
172                 // if they already have an swhois field, trash it and replace it with the remote one.
173                 std::string* text;
174                 if (dest->GetExt("swhois", text))
175                 {
176                         dest->Shrink("swhois");
177                         delete text;
178                 }
179
180                 if (extdata.empty())
181                         return;
182
183                 text = new std::string(extdata);
184                 dest->Extend("swhois", text);
185         }
186
187         virtual void OnPostCommand(const std::string &command, const std::vector<std::string> &params, User *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                 ServerInstance->PI->SendMetaData(user, "swhois", *text);
232         }
233
234         virtual ~ModuleSWhois()
235         {
236                 delete Conf;
237         }
238
239         virtual Version GetVersion()
240         {
241                 return Version("$Id$", VF_COMMON | VF_VENDOR, API_VERSION);
242         }
243 };
244
245 MODULE_INIT(ModuleSWhois)