]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_swhois.cpp
Merge branch 'insp20' into master.
[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 #include "modules/whois.h"
28
29 enum
30 {
31         // From UnrealIRCd.
32         RPL_WHOISSPECIAL = 320
33 };
34
35 /** Handle /SWHOIS
36  */
37 class CommandSwhois : public Command
38 {
39  public:
40         LocalIntExt operblock;
41         StringExtItem swhois;
42         CommandSwhois(Module* Creator)
43                 : Command(Creator, "SWHOIS", 2, 2)
44                 , operblock("swhois_operblock", ExtensionItem::EXT_USER, Creator)
45                 , swhois("swhois", ExtensionItem::EXT_USER, Creator)
46         {
47                 flags_needed = 'o'; syntax = "<nick> :<swhois>";
48                 TRANSLATE2(TR_NICK, TR_TEXT);
49         }
50
51         CmdResult Handle(User* user, const Params& parameters) CXX11_OVERRIDE
52         {
53                 User* dest = ServerInstance->FindNick(parameters[0]);
54
55                 if (!dest) // allow setting swhois using SWHOIS before reg
56                 {
57                         user->WriteNumeric(Numerics::NoSuchNick(parameters[0]));
58                         return CMD_FAILURE;
59                 }
60
61                 std::string* text = swhois.get(dest);
62                 if (text)
63                 {
64                         // We already had it set...
65                         if (!user->server->IsULine())
66                                 // Ulines set SWHOISes silently
67                                 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());
68                 }
69                 else if (!user->server->IsULine())
70                 {
71                         // Ulines set SWHOISes silently
72                         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());
73                 }
74
75                 operblock.set(user, 0);
76                 if (parameters[1].empty())
77                         swhois.unset(dest);
78                 else
79                         swhois.set(dest, parameters[1]);
80
81                 /* Bug #376 - feature request -
82                  * To cut down on the amount of commands services etc have to recognise, this only sends METADATA across the network now
83                  * not an actual SWHOIS command. Any SWHOIS command sent from services will be automatically translated to METADATA by this.
84                  * Sorry w00t i know this was your fix, but i got bored and wanted to clear down the tracker :)
85                  * -- Brain
86                  */
87                 ServerInstance->PI->SendMetaData(dest, "swhois", parameters[1]);
88
89                 return CMD_SUCCESS;
90         }
91
92 };
93
94 class ModuleSWhois : public Module, public Whois::LineEventListener
95 {
96         CommandSwhois cmd;
97
98  public:
99         ModuleSWhois()
100                 : Whois::LineEventListener(this)
101                 , cmd(this)
102         {
103         }
104
105         // :kenny.chatspike.net 320 Brain Azhrarn :is getting paid to play games.
106         ModResult OnWhoisLine(Whois::Context& whois, Numeric::Numeric& numeric) CXX11_OVERRIDE
107         {
108                 /* We use this and not OnWhois because this triggers for remote, too */
109                 if (numeric.GetNumeric() == 312)
110                 {
111                         /* Insert our numeric before 312 */
112                         std::string* swhois = cmd.swhois.get(whois.GetTarget());
113                         if (swhois)
114                         {
115                                 whois.SendLine(RPL_WHOISSPECIAL, *swhois);
116                         }
117                 }
118
119                 /* Dont block anything */
120                 return MOD_RES_PASSTHRU;
121         }
122
123         void OnPostOper(User* user, const std::string &opertype, const std::string &opername) CXX11_OVERRIDE
124         {
125                 if (!IS_LOCAL(user))
126                         return;
127
128                 std::string swhois = user->oper->getConfig("swhois");
129
130                 if (!swhois.length())
131                         return;
132
133                 cmd.operblock.set(user, 1);
134                 cmd.swhois.set(user, swhois);
135                 ServerInstance->PI->SendMetaData(user, "swhois", swhois);
136         }
137
138         void OnPostDeoper(User* user) CXX11_OVERRIDE
139         {
140                 std::string* swhois = cmd.swhois.get(user);
141                 if (!swhois)
142                         return;
143
144                 if (!cmd.operblock.get(user))
145                         return;
146
147                 cmd.operblock.set(user, 0);
148                 cmd.swhois.unset(user);
149                 ServerInstance->PI->SendMetaData(user, "swhois", "");
150         }
151
152         void OnDecodeMetaData(Extensible* target, const std::string& extname, const std::string&) CXX11_OVERRIDE
153         {
154                 User* dest = static_cast<User*>(target);
155                 if (dest && (extname == "swhois"))
156                         cmd.operblock.set(dest, 0);
157         }
158
159         Version GetVersion() CXX11_OVERRIDE
160         {
161                 return Version("Provides the SWHOIS command which allows setting of arbitrary WHOIS lines", VF_OPTCOMMON | VF_VENDOR);
162         }
163 };
164
165 MODULE_INIT(ModuleSWhois)