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