]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_showwhois.cpp
Merge pull request #215 from attilamolnar/insp20+modfixes
[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 ModeHandler
31 {
32  public:
33         SeeWhois(Module* Creator, bool IsOpersOnly) : ModeHandler(Creator, "showwhois", 'W', PARAM_NONE, MODETYPE_USER)
34         {
35                 oper = IsOpersOnly;
36         }
37
38         ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string &parameter, bool adding)
39         {
40                 if (adding)
41                 {
42                         if (!dest->IsModeSet('W'))
43                         {
44                                 dest->SetMode('W',true);
45                                 return MODEACTION_ALLOW;
46                         }
47                 }
48                 else
49                 {
50                         if (dest->IsModeSet('W'))
51                         {
52                                 dest->SetMode('W',false);
53                                 return MODEACTION_ALLOW;
54                         }
55                 }
56
57                 return MODEACTION_DENY;
58         }
59 };
60
61 class WhoisNoticeCmd : public Command
62 {
63  public:
64         WhoisNoticeCmd(Module* Creator) : Command(Creator,"WHOISNOTICE", 1)
65         {
66                 flags_needed = FLAG_SERVERONLY;
67         }
68
69         void HandleFast(User* dest, User* src)
70         {
71                 dest->WriteServ("NOTICE %s :*** %s (%s@%s) did a /whois on you",
72                         dest->nick.c_str(), src->nick.c_str(), src->ident.c_str(),
73                         dest->HasPrivPermission("users/auspex") ? src->host.c_str() : src->dhost.c_str());
74         }
75
76         CmdResult Handle(const std::vector<std::string> &parameters, User *user)
77         {
78                 User* dest = ServerInstance->FindNick(parameters[0]);
79                 if (!dest)
80                         return CMD_FAILURE;
81
82                 User* source = ServerInstance->FindNick(parameters[1]);
83
84                 if (IS_LOCAL(dest) && source)
85                         HandleFast(dest, source);
86
87                 return CMD_SUCCESS;
88         }
89 };
90
91 class ModuleShowwhois : public Module
92 {
93         bool ShowWhoisFromOpers;
94         SeeWhois* sw;
95         WhoisNoticeCmd cmd;
96
97  public:
98
99         ModuleShowwhois() : cmd(this)
100         {
101                 ConfigReader conf;
102                 bool OpersOnly = conf.ReadFlag("showwhois", "opersonly", "yes", 0);
103                 ShowWhoisFromOpers = conf.ReadFlag("showwhois", "showfromopers", "yes", 0);
104
105                 sw = new SeeWhois(this, OpersOnly);
106                 if (!ServerInstance->Modes->AddMode(sw))
107                         throw ModuleException("Could not add new modes!");
108                 ServerInstance->AddCommand(&cmd);
109                 Implementation eventlist[] = { I_OnWhois };
110                 ServerInstance->Modules->Attach(eventlist, this, 1);
111         }
112
113         ~ModuleShowwhois()
114         {
115                 delete sw;
116         }
117
118         Version GetVersion()
119         {
120                 return Version("Allows opers to set +W to see when a user uses WHOIS on them",VF_OPTCOMMON|VF_VENDOR);
121         }
122
123         void OnWhois(User* source, User* dest)
124         {
125                 if (!dest->IsModeSet('W') || source == dest)
126                         return;
127
128                 if (!ShowWhoisFromOpers && IS_OPER(source))
129                         return;
130
131                 if (IS_LOCAL(dest))
132                 {
133                         cmd.HandleFast(dest, source);
134                 }
135                 else
136                 {
137                         std::vector<std::string> params;
138                         params.push_back(dest->server);
139                         params.push_back("WHOISNOTICE");
140                         params.push_back(dest->uuid);
141                         params.push_back(source->uuid);
142                         ServerInstance->PI->SendEncapsulatedData(params);
143                 }
144         }
145
146 };
147
148 MODULE_INIT(ModuleShowwhois)