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