]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_swhois.cpp
80c7ddd3b13d8054dc5ddc587890c6d02b572489
[user/henk/code/inspircd.git] / src / modules / m_swhois.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
5  *   Copyright (C) 2008 Pippijn van Steenhoven <pip88nl@gmail.com>
6  *   Copyright (C) 2007 Robin Burchell <robin+git@viroteck.net>
7  *   Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
8  *   Copyright (C) 2006-2007 John Brooks <john.brooks@dereferenced.net>
9  *   Copyright (C) 2005-2006 Craig Edwards <craigedwards@brainbox.cc>
10  *   Copyright (C) 2006 Oliver Lupton <oliverlupton@gmail.com>
11  *
12  * This file is part of InspIRCd.  InspIRCd is free software: you can
13  * redistribute it and/or modify it under the terms of the GNU General Public
14  * License as published by the Free Software Foundation, version 2.
15  *
16  * This program is distributed in the hope that it will be useful, but WITHOUT
17  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
19  * details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23  */
24
25
26 #include "inspircd.h"
27
28 /** Handle /SWHOIS
29  */
30 class CommandSwhois : public Command
31 {
32  public:
33         StringExtItem swhois;
34         CommandSwhois(Module* Creator)
35                 : Command(Creator, "SWHOIS", 2, 2)
36                 , swhois("swhois", ExtensionItem::EXT_USER, Creator)
37         {
38                 flags_needed = 'o'; syntax = "<nick> :<swhois>";
39                 TRANSLATE2(TR_NICK, TR_TEXT);
40         }
41
42         CmdResult Handle(const std::vector<std::string> &parameters, User* user)
43         {
44                 User* dest = ServerInstance->FindNick(parameters[0]);
45
46                 if (!dest) // allow setting swhois using SWHOIS before reg
47                 {
48                         user->WriteNumeric(ERR_NOSUCHNICK, "%s :No such nick/channel", parameters[0].c_str());
49                         return CMD_FAILURE;
50                 }
51
52                 std::string* text = swhois.get(dest);
53                 if (text)
54                 {
55                         // We already had it set...
56                         if (!user->server->IsULine())
57                                 // Ulines set SWHOISes silently
58                                 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());
59                 }
60                 else if (!user->server->IsULine())
61                 {
62                         // Ulines set SWHOISes silently
63                         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());
64                 }
65
66                 if (parameters[1].empty())
67                         swhois.unset(dest);
68                 else
69                         swhois.set(dest, parameters[1]);
70
71                 /* Bug #376 - feature request -
72                  * To cut down on the amount of commands services etc have to recognise, this only sends METADATA across the network now
73                  * not an actual SWHOIS command. Any SWHOIS command sent from services will be automatically translated to METADATA by this.
74                  * Sorry w00t i know this was your fix, but i got bored and wanted to clear down the tracker :)
75                  * -- Brain
76                  */
77                 ServerInstance->PI->SendMetaData(dest, "swhois", parameters[1]);
78
79                 return CMD_SUCCESS;
80         }
81
82 };
83
84 class ModuleSWhois : public Module, public Whois::LineEventListener
85 {
86         CommandSwhois cmd;
87
88  public:
89         ModuleSWhois()
90                 : Whois::LineEventListener(this)
91                 , cmd(this)
92         {
93         }
94
95         // :kenny.chatspike.net 320 Brain Azhrarn :is getting paid to play games.
96         ModResult OnWhoisLine(Whois::Context& whois, unsigned int& numeric, std::string& text) CXX11_OVERRIDE
97         {
98                 /* We use this and not OnWhois because this triggers for remote, too */
99                 if (numeric == 312)
100                 {
101                         /* Insert our numeric before 312 */
102                         std::string* swhois = cmd.swhois.get(whois.GetTarget());
103                         if (swhois)
104                         {
105                                 whois.SendLine(320, ":%s", swhois->c_str());
106                         }
107                 }
108
109                 /* Dont block anything */
110                 return MOD_RES_PASSTHRU;
111         }
112
113         void OnPostOper(User* user, const std::string &opertype, const std::string &opername) CXX11_OVERRIDE
114         {
115                 if (!IS_LOCAL(user))
116                         return;
117
118                 std::string swhois = user->oper->getConfig("swhois");
119
120                 if (!swhois.length())
121                         return;
122
123                 cmd.swhois.set(user, swhois);
124                 ServerInstance->PI->SendMetaData(user, "swhois", swhois);
125         }
126
127         Version GetVersion() CXX11_OVERRIDE
128         {
129                 return Version("Provides the SWHOIS command which allows setting of arbitrary WHOIS lines", VF_OPTCOMMON | VF_VENDOR);
130         }
131 };
132
133 MODULE_INIT(ModuleSWhois)