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