]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_swhois.cpp
Sync helpop chmodes s and p with docs
[user/henk/code/inspircd.git] / src / modules / m_swhois.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2012-2016 Attila Molnar <attilamolnar@hush.com>
5  *   Copyright (C) 2012-2013, 2017-2018, 2020 Sadie Powell <sadie@witchery.services>
6  *   Copyright (C) 2012 Robby <robby@chatbelgie.be>
7  *   Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
8  *   Copyright (C) 2007-2008 Robin Burchell <robin+git@viroteck.net>
9  *   Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
10  *   Copyright (C) 2006 Oliver Lupton <om@inspircd.org>
11  *   Copyright (C) 2005, 2007, 2010 Craig Edwards <brain@inspircd.org>
12  *
13  * This file is part of InspIRCd.  InspIRCd is free software: you can
14  * redistribute it and/or modify it under the terms of the GNU General Public
15  * License as published by the Free Software Foundation, version 2.
16  *
17  * This program is distributed in the hope that it will be useful, but WITHOUT
18  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
20  * details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24  */
25
26
27 #include "inspircd.h"
28 #include "modules/whois.h"
29
30 enum
31 {
32         // From UnrealIRCd.
33         RPL_WHOISSPECIAL = 320
34 };
35
36 /** Handle /SWHOIS
37  */
38 class CommandSwhois : public Command
39 {
40  public:
41         LocalIntExt operblock;
42         StringExtItem swhois;
43         CommandSwhois(Module* Creator)
44                 : Command(Creator, "SWHOIS", 2, 2)
45                 , operblock("swhois_operblock", ExtensionItem::EXT_USER, Creator)
46                 , swhois("swhois", ExtensionItem::EXT_USER, Creator)
47         {
48                 flags_needed = 'o';
49                 syntax = "<nick> :<swhois>";
50                 TRANSLATE2(TR_NICK, TR_TEXT);
51         }
52
53         CmdResult Handle(User* user, const Params& parameters) CXX11_OVERRIDE
54         {
55                 User* dest = ServerInstance->FindNick(parameters[0]);
56
57                 if (!dest) // allow setting swhois using SWHOIS before reg
58                 {
59                         user->WriteNumeric(Numerics::NoSuchNick(parameters[0]));
60                         return CMD_FAILURE;
61                 }
62
63                 std::string* text = swhois.get(dest);
64                 if (text)
65                 {
66                         // We already had it set...
67                         if (!user->server->IsULine())
68                                 // Ulines set SWHOISes silently
69                                 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());
70                 }
71                 else if (!user->server->IsULine())
72                 {
73                         // Ulines set SWHOISes silently
74                         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());
75                 }
76
77                 operblock.set(user, 0);
78                 if (parameters[1].empty())
79                         swhois.unset(dest);
80                 else
81                         swhois.set(dest, parameters[1]);
82
83                 /* Bug #376 - feature request -
84                  * To cut down on the amount of commands services etc have to recognise, this only sends METADATA across the network now
85                  * not an actual SWHOIS command. Any SWHOIS command sent from services will be automatically translated to METADATA by this.
86                  * Sorry w00t i know this was your fix, but i got bored and wanted to clear down the tracker :)
87                  * -- Brain
88                  */
89                 ServerInstance->PI->SendMetaData(dest, "swhois", parameters[1]);
90
91                 return CMD_SUCCESS;
92         }
93
94 };
95
96 class ModuleSWhois : public Module, public Whois::LineEventListener
97 {
98         CommandSwhois cmd;
99
100  public:
101         ModuleSWhois()
102                 : Whois::LineEventListener(this)
103                 , cmd(this)
104         {
105         }
106
107         // :kenny.chatspike.net 320 Brain Azhrarn :is getting paid to play games.
108         ModResult OnWhoisLine(Whois::Context& whois, Numeric::Numeric& numeric) CXX11_OVERRIDE
109         {
110                 /* We use this and not OnWhois because this triggers for remote, too */
111                 if (numeric.GetNumeric() == 312)
112                 {
113                         /* Insert our numeric before 312 */
114                         std::string* swhois = cmd.swhois.get(whois.GetTarget());
115                         if (swhois)
116                         {
117                                 whois.SendLine(RPL_WHOISSPECIAL, *swhois);
118                         }
119                 }
120
121                 /* Dont block anything */
122                 return MOD_RES_PASSTHRU;
123         }
124
125         void OnPostOper(User* user, const std::string &opertype, const std::string &opername) CXX11_OVERRIDE
126         {
127                 if (!IS_LOCAL(user))
128                         return;
129
130                 std::string swhois = user->oper->getConfig("swhois");
131
132                 if (!swhois.length())
133                         return;
134
135                 cmd.operblock.set(user, 1);
136                 cmd.swhois.set(user, swhois);
137                 ServerInstance->PI->SendMetaData(user, "swhois", swhois);
138         }
139
140         void OnPostDeoper(User* user) CXX11_OVERRIDE
141         {
142                 std::string* swhois = cmd.swhois.get(user);
143                 if (!swhois)
144                         return;
145
146                 if (!cmd.operblock.get(user))
147                         return;
148
149                 cmd.operblock.set(user, 0);
150                 cmd.swhois.unset(user);
151                 ServerInstance->PI->SendMetaData(user, "swhois", "");
152         }
153
154         void OnDecodeMetaData(Extensible* target, const std::string& extname, const std::string&) CXX11_OVERRIDE
155         {
156                 User* dest = static_cast<User*>(target);
157                 if (dest && (extname == "swhois"))
158                         cmd.operblock.set(dest, 0);
159         }
160
161         Version GetVersion() CXX11_OVERRIDE
162         {
163                 return Version("Adds the /SWHOIS command which adds custom lines to a user's WHOIS response.", VF_OPTCOMMON | VF_VENDOR);
164         }
165 };
166
167 MODULE_INIT(ModuleSWhois)