]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_showwhois.cpp
Attach to events and register services in init()
[user/henk/code/inspircd.git] / src / modules / m_showwhois.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
5  *   Copyright (C) 2005, 2008 Robin Burchell <robin+git@viroteck.net>
6  *   Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
7  *   Copyright (C) 2006 Craig Edwards <craigedwards@brainbox.cc>
8  *   Copyright (C) 2004 Christopher Hall <typobox43@gmail.com>
9  *
10  * This file is part of InspIRCd.  InspIRCd is free software: you can
11  * redistribute it and/or modify it under the terms of the GNU General Public
12  * License as published by the Free Software Foundation, version 2.
13  *
14  * This program is distributed in the hope that it will be useful, but WITHOUT
15  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
17  * details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  */
22
23
24 #include "inspircd.h"
25
26 /* $ModDesc: Allows opers to set +W to see when a user uses WHOIS on them */
27
28 /** Handle user mode +W
29  */
30 class SeeWhois : public SimpleUserModeHandler
31 {
32  public:
33         SeeWhois(Module* Creator, bool IsOpersOnly) : SimpleUserModeHandler(Creator, "showwhois", 'W')
34         {
35                 oper = IsOpersOnly;
36         }
37 };
38
39 class WhoisNoticeCmd : public Command
40 {
41  public:
42         WhoisNoticeCmd(Module* Creator) : Command(Creator,"WHOISNOTICE", 1)
43         {
44                 flags_needed = FLAG_SERVERONLY;
45         }
46
47         void HandleFast(User* dest, User* src)
48         {
49                 dest->WriteServ("NOTICE %s :*** %s (%s@%s) did a /whois on you",
50                         dest->nick.c_str(), src->nick.c_str(), src->ident.c_str(),
51                         dest->HasPrivPermission("users/auspex") ? src->host.c_str() : src->dhost.c_str());
52         }
53
54         CmdResult Handle(const std::vector<std::string> &parameters, User *user)
55         {
56                 User* dest = ServerInstance->FindNick(parameters[0]);
57                 if (!dest)
58                         return CMD_FAILURE;
59
60                 User* source = ServerInstance->FindNick(parameters[1]);
61
62                 if (IS_LOCAL(dest) && source)
63                         HandleFast(dest, source);
64
65                 return CMD_SUCCESS;
66         }
67 };
68
69 class ModuleShowwhois : public Module
70 {
71         bool ShowWhoisFromOpers;
72         SeeWhois* sw;
73         WhoisNoticeCmd cmd;
74
75  public:
76
77         ModuleShowwhois()
78                 : sw(NULL), cmd(this)
79         {
80         }
81
82         void init()
83         {
84                 ConfigTag* tag = ServerInstance->Config->ConfValue("showwhois");
85
86                 bool OpersOnly = tag->getBool("opersonly", true);
87                 ShowWhoisFromOpers = tag->getBool("showfromopers", true);
88
89                 sw = new SeeWhois(this, OpersOnly);
90                 if (!ServerInstance->Modes->AddMode(sw))
91                         throw ModuleException("Could not add new modes!");
92                 ServerInstance->AddCommand(&cmd);
93                 Implementation eventlist[] = { I_OnWhois };
94                 ServerInstance->Modules->Attach(eventlist, this, 1);
95         }
96
97         ~ModuleShowwhois()
98         {
99                 delete sw;
100         }
101
102         Version GetVersion()
103         {
104                 return Version("Allows opers to set +W to see when a user uses WHOIS on them",VF_OPTCOMMON|VF_VENDOR);
105         }
106
107         void OnWhois(User* source, User* dest)
108         {
109                 if (!dest->IsModeSet('W') || source == dest)
110                         return;
111
112                 if (!ShowWhoisFromOpers && IS_OPER(source))
113                         return;
114
115                 if (IS_LOCAL(dest))
116                 {
117                         cmd.HandleFast(dest, source);
118                 }
119                 else
120                 {
121                         std::vector<std::string> params;
122                         params.push_back(dest->server);
123                         params.push_back("WHOISNOTICE");
124                         params.push_back(dest->uuid);
125                         params.push_back(source->uuid);
126                         ServerInstance->PI->SendEncapsulatedData(params);
127                 }
128         }
129
130 };
131
132 MODULE_INIT(ModuleShowwhois)