]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_swhois.cpp
Fix BanCache entries existing after X-line expiry.
[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 /* $ModDesc: Provides the SWHOIS command which allows setting of arbitrary WHOIS lines */
29
30 /** Handle /SWHOIS
31  */
32 class CommandSwhois : public Command
33 {
34  public:
35         LocalIntExt operblock;
36         StringExtItem swhois;
37         CommandSwhois(Module* Creator)
38                 : Command(Creator,"SWHOIS", 2,2)
39                 , operblock("swhois_operblock", Creator)
40                 , swhois("swhois", Creator)
41         {
42                 flags_needed = 'o'; syntax = "<nick> :<swhois>";
43                 TRANSLATE3(TR_NICK, TR_TEXT, TR_END);
44         }
45
46         CmdResult Handle(const std::vector<std::string> &parameters, User* user)
47         {
48                 User* dest = ServerInstance->FindNick(parameters[0]);
49
50                 if ((!dest) || (IS_SERVER(dest))) // allow setting swhois using SWHOIS before reg
51                 {
52                         user->WriteNumeric(ERR_NOSUCHNICK, "%s %s :No such nick/channel", user->nick.c_str(), parameters[0].c_str());
53                         return CMD_FAILURE;
54                 }
55
56                 std::string* text = swhois.get(dest);
57                 if (text)
58                 {
59                         // We already had it set...
60                         if (!ServerInstance->ULine(user->server))
61                                 // Ulines set SWHOISes silently
62                                 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());
63                 }
64                 else if (!ServerInstance->ULine(user->server))
65                 {
66                         // Ulines set SWHOISes silently
67                         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());
68                 }
69
70                 operblock.set(user, 0);
71                 if (parameters[1].empty())
72                         swhois.unset(dest);
73                 else
74                         swhois.set(dest, parameters[1]);
75
76                 /* Bug #376 - feature request -
77                  * To cut down on the amount of commands services etc have to recognise, this only sends METADATA across the network now
78                  * not an actual SWHOIS command. Any SWHOIS command sent from services will be automatically translated to METADATA by this.
79                  * Sorry w00t i know this was your fix, but i got bored and wanted to clear down the tracker :)
80                  * -- Brain
81                  */
82                 ServerInstance->PI->SendMetaData(dest, "swhois", parameters[1]);
83
84                 return CMD_SUCCESS;
85         }
86
87 };
88
89 class ModuleSWhois : public Module
90 {
91         CommandSwhois cmd;
92
93  public:
94         ModuleSWhois() : cmd(this)
95         {
96         }
97
98         void init()
99         {
100                 ServiceProvider* providerlist[] = { &cmd, &cmd.operblock, &cmd.swhois };
101                 ServerInstance->Modules->AddServices(providerlist, sizeof(providerlist)/sizeof(ServiceProvider*));
102                 Implementation eventlist[] = { I_OnWhoisLine, I_OnPostOper, I_OnPostDeoper, I_OnDecodeMetaData };
103                 ServerInstance->Modules->Attach(eventlist, this, sizeof(eventlist)/sizeof(Implementation));
104         }
105
106         // :kenny.chatspike.net 320 Brain Azhrarn :is getting paid to play games.
107         ModResult OnWhoisLine(User* user, User* dest, int &numeric, std::string &text)
108         {
109                 /* We use this and not OnWhois because this triggers for remote, too */
110                 if (numeric == 312)
111                 {
112                         /* Insert our numeric before 312 */
113                         std::string* swhois = cmd.swhois.get(dest);
114                         if (swhois)
115                         {
116                                 ServerInstance->SendWhoisLine(user, dest, 320, "%s %s :%s",user->nick.c_str(), dest->nick.c_str(), swhois->c_str());
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)
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)
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&)
154         {
155                 // XXX: We use a dynamic_cast in m_services_account so I used one
156                 // here but do we actually need it or is static_cast okay?
157                 User* dest = dynamic_cast<User*>(target);
158                 if (dest && (extname == "swhois"))
159                         cmd.operblock.set(dest, 0);
160         }
161
162         ~ModuleSWhois()
163         {
164         }
165
166         Version GetVersion()
167         {
168                 return Version("Provides the SWHOIS command which allows setting of arbitrary WHOIS lines", VF_OPTCOMMON | VF_VENDOR);
169         }
170 };
171
172 MODULE_INIT(ModuleSWhois)