]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_showwhois.cpp
Merge pull request #109 from Justasic/insp20
[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                 User* source = ServerInstance->FindNick(parameters[1]);
80
81                 if (IS_LOCAL(dest) && source)
82                         HandleFast(dest, source);
83
84                 return CMD_SUCCESS;
85         }
86 };
87
88 class ModuleShowwhois : public Module
89 {
90         bool ShowWhoisFromOpers;
91         SeeWhois* sw;
92         WhoisNoticeCmd cmd;
93
94  public:
95
96         ModuleShowwhois() : cmd(this)
97         {
98                 ConfigReader conf;
99                 bool OpersOnly = conf.ReadFlag("showwhois", "opersonly", "yes", 0);
100                 ShowWhoisFromOpers = conf.ReadFlag("showwhois", "showfromopers", "yes", 0);
101
102                 sw = new SeeWhois(this, OpersOnly);
103                 if (!ServerInstance->Modes->AddMode(sw))
104                         throw ModuleException("Could not add new modes!");
105                 ServerInstance->AddCommand(&cmd);
106                 Implementation eventlist[] = { I_OnWhois };
107                 ServerInstance->Modules->Attach(eventlist, this, 1);
108         }
109
110         ~ModuleShowwhois()
111         {
112                 delete sw;
113         }
114
115         Version GetVersion()
116         {
117                 return Version("Allows opers to set +W to see when a user uses WHOIS on them",VF_OPTCOMMON|VF_VENDOR);
118         }
119
120         void OnWhois(User* source, User* dest)
121         {
122                 if (!dest->IsModeSet('W') || source == dest)
123                         return;
124
125                 if (!ShowWhoisFromOpers && IS_OPER(source))
126                         return;
127
128                 if (IS_LOCAL(dest))
129                 {
130                         cmd.HandleFast(dest, source);
131                 }
132                 else
133                 {
134                         std::vector<std::string> params;
135                         params.push_back(dest->server);
136                         params.push_back("WHOISNOTICE");
137                         params.push_back(dest->uuid);
138                         params.push_back(source->uuid);
139                         ServerInstance->PI->SendEncapsulatedData(params);
140                 }
141         }
142
143 };
144
145 MODULE_INIT(ModuleShowwhois)