]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_swhois.cpp
MetaData rework
[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->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());
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->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());
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, "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 cmd;
84
85         ConfigReader* Conf;
86
87  public:
88         ModuleSWhois(InspIRCd* Me) : Module(Me), cmd(Me)
89         {
90
91                 Conf = new ConfigReader(ServerInstance);
92                 ServerInstance->AddCommand(&cmd);
93                 Implementation eventlist[] = { I_OnDecodeMetaData, I_OnWhoisLine, I_OnSyncUser, I_OnUserQuit, I_OnCleanup, I_OnRehash, I_OnPostCommand };
94                 ServerInstance->Modules->Attach(eventlist, this, 7);
95         }
96
97         void OnRehash(User* user)
98         {
99                 delete Conf;
100                 Conf = new ConfigReader(ServerInstance);
101         }
102
103
104         // :kenny.chatspike.net 320 Brain Azhrarn :is getting paid to play games.
105         int OnWhoisLine(User* user, User* dest, int &numeric, std::string &text)
106         {
107                 /* We use this and not OnWhois because this triggers for remote, too */
108                 if (numeric == 312)
109                 {
110                         /* Insert our numeric before 312 */
111                         std::string* swhois;
112                         if (dest->GetExt("swhois", swhois))
113                         {
114                                 ServerInstance->SendWhoisLine(user, dest, 320, "%s %s :%s",user->nick.c_str(), dest->nick.c_str(), swhois->c_str());
115                         }
116                 }
117
118                 /* Dont block anything */
119                 return 0;
120         }
121
122         // Whenever the linking module wants to send out data, but doesnt know what the data
123         // represents (e.g. it is metadata, added to a User or Channel by a module) then
124         // this method is called. We should use the ProtoSendMetaData function after we've
125         // corrected decided how the data should look, to send the metadata on its way if
126         // it is ours.
127         virtual void OnSyncUser(User* user, Module* proto, void* opaque)
128         {
129                 // check if this user has an swhois field to send
130                 std::string* swhois;
131                 if (user->GetExt("swhois", swhois))
132                         proto->ProtoSendMetaData(opaque,user,"swhois",*swhois);
133         }
134
135         // when a user quits, tidy up their metadata
136         virtual void OnUserQuit(User* user, const std::string &message, const std::string &oper_message)
137         {
138                 std::string* swhois;
139                 if (user->GetExt("swhois", swhois))
140                 {
141                         user->Shrink("swhois");
142                         delete swhois;
143                 }
144         }
145
146         // if the module is unloaded, tidy up all our dangling metadata
147         virtual void OnCleanup(int target_type, void* item)
148         {
149                 if (target_type != TYPE_USER) return;
150                 User* user = static_cast<User*>(item);
151                 std::string* swhois;
152                 if (user && user->GetExt("swhois", swhois))
153                 {
154                         user->Shrink("swhois");
155                         delete swhois;
156                 }
157         }
158
159         // Whenever the linking module receives metadata from another server and doesnt know what
160         // to do with it (of course, hence the 'meta') it calls this method, and it is up to each
161         // module in turn to figure out if this metadata key belongs to them, and what they want
162         // to do with it.
163         // In our case we're only sending a single string around, so we just construct a std::string.
164         // Some modules will probably get much more complex and format more detailed structs and classes
165         // in a textual way for sending over the link.
166         virtual void OnDecodeMetaData(Extensible* target, const std::string &extname, const std::string &extdata)
167         {
168                 User* dest = dynamic_cast<User*>(target);
169                 // check if its our metadata key, and its associated with a user
170                 if (extname != "swhois" || !dest)
171                         return;
172
173                 // if they already have an swhois field, trash it and replace it with the remote one.
174                 std::string* text;
175                 if (dest->GetExt("swhois", text))
176                 {
177                         dest->Shrink("swhois");
178                         delete text;
179                 }
180
181                 if (extdata.empty())
182                         return;
183
184                 text = new std::string(extdata);
185                 dest->Extend("swhois", text);
186         }
187
188         virtual void OnPostCommand(const std::string &command, const std::vector<std::string> &params, User *user, CmdResult result, const std::string &original_line)
189         {
190                 if ((command != "OPER") || (result != CMD_SUCCESS))
191                         return;
192
193                 std::string swhois;
194
195                 for (int i = 0; i < Conf->Enumerate("oper"); i++)
196                 {
197                         std::string name = Conf->ReadValue("oper", "name", i);
198
199                         if (name == params[0])
200                         {
201                                 swhois = Conf->ReadValue("oper", "swhois", i);
202                                 break;
203                         }
204                 }
205
206                 if (!swhois.length())
207                 {
208                         for (int i = 0; i < Conf->Enumerate("type"); i++)
209                         {
210                                 std::string type = Conf->ReadValue("type", "name", i);
211
212                                 if (type == user->oper)
213                                 {
214                                         swhois = Conf->ReadValue("type", "swhois", i);
215                                         break;
216                                 }
217                         }
218                 }
219
220                 std::string *old;
221                 if (user->GetExt("swhois", old))
222                 {
223                         user->Shrink("swhois");
224                         delete old;
225                 }
226
227                 if (!swhois.length())
228                         return;
229
230                 std::string *text = new std::string(swhois);
231                 user->Extend("swhois", text);
232                 ServerInstance->PI->SendMetaData(user, "swhois", *text);
233         }
234
235         virtual ~ModuleSWhois()
236         {
237                 delete Conf;
238         }
239
240         virtual Version GetVersion()
241         {
242                 return Version("$Id$", VF_COMMON | VF_VENDOR, API_VERSION);
243         }
244 };
245
246 MODULE_INIT(ModuleSWhois)