]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/modules/whois.h
Implement support for multi-prefix on WHOIS.
[user/henk/code/inspircd.git] / include / modules / whois.h
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2015-2016 Attila Molnar <attilamolnar@hush.com>
5  *
6  * This file is part of InspIRCd.  InspIRCd is free software: you can
7  * redistribute it and/or modify it under the terms of the GNU General Public
8  * License as published by the Free Software Foundation, version 2.
9  *
10  * This program is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
13  * details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18
19
20 #pragma once
21
22 #include "event.h"
23
24 namespace Whois
25 {
26         class EventListener;
27         class LineEventListener;
28         class Context;
29 }
30
31 enum
32 {
33         // From RFC 1459.
34         RPL_WHOISUSER = 311,
35         RPL_WHOISOPERATOR = 313,
36         RPL_WHOISIDLE = 317,
37         RPL_WHOISCHANNELS = 319,
38
39         // From UnrealIRCd.
40         RPL_WHOISHOST = 378,
41         RPL_WHOISMODES = 379,
42
43         // InspIRCd-specific.
44         RPL_CHANNELSMSG = 651
45 };
46
47 class Whois::EventListener : public Events::ModuleEventListener
48 {
49  public:
50         EventListener(Module* mod)
51                 : ModuleEventListener(mod, "event/whois")
52         {
53         }
54
55         /** Called whenever a /WHOIS is performed by a local user.
56          * @param whois Whois context, can be used to send numerics
57          */
58         virtual void OnWhois(Context& whois) = 0;
59 };
60
61 class Whois::LineEventListener : public Events::ModuleEventListener
62 {
63  public:
64         LineEventListener(Module* mod)
65                 : ModuleEventListener(mod, "event/whoisline")
66         {
67         }
68
69         /** Called whenever a line of WHOIS output is sent to a user.
70          * You may change the numeric and the text of the output by changing
71          * the values numeric and text, but you cannot change the user the
72          * numeric is sent to.
73          * @param whois Whois context, can be used to send numerics
74          * @param numeric Numeric being sent
75          * @return MOD_RES_DENY to drop the line completely so that the user does not
76          * receive it, or MOD_RES_PASSTHRU to allow the line to be sent.
77          */
78         virtual ModResult OnWhoisLine(Context& whois, Numeric::Numeric& numeric) = 0;
79 };
80
81 class Whois::Context
82 {
83  protected:
84         /** User doing the WHOIS
85          */
86         LocalUser* const source;
87
88         /** User being WHOISed
89          */
90         User* const target;
91
92  public:
93         Context(LocalUser* src, User* targ)
94                 : source(src)
95                 , target(targ)
96         {
97         }
98
99         /** Returns true if the user is /WHOISing himself
100          * @return True if whois source is the same user as the whois target, false if they are different users
101          */
102         bool IsSelfWhois() const { return (source == target); }
103
104         /** Returns the LocalUser who has done the /WHOIS
105          * @return LocalUser doing the /WHOIS
106          */
107         LocalUser* GetSource() const { return source; }
108
109         /** Returns the target of the /WHOIS
110          * @return User who was /WHOIS'd
111          */
112         User* GetTarget() const { return target; }
113
114         /** Send a line of WHOIS data to the source of the WHOIS
115          */
116         template <typename T1>
117         void SendLine(unsigned int numeric, T1 p1)
118         {
119                 Numeric::Numeric n(numeric);
120                 n.push(target->nick);
121                 n.push(p1);
122                 SendLine(n);
123         }
124
125         template <typename T1, typename T2>
126         void SendLine(unsigned int numeric, T1 p1, T2 p2)
127         {
128                 Numeric::Numeric n(numeric);
129                 n.push(target->nick);
130                 n.push(p1);
131                 n.push(p2);
132                 SendLine(n);
133         }
134
135         template <typename T1, typename T2, typename T3>
136         void SendLine(unsigned int numeric, T1 p1, T2 p2, T3 p3)
137         {
138                 Numeric::Numeric n(numeric);
139                 n.push(target->nick);
140                 n.push(p1);
141                 n.push(p2);
142                 n.push(p3);
143                 SendLine(n);
144         }
145
146         template <typename T1, typename T2, typename T3, typename T4>
147         void SendLine(unsigned int numeric, T1 p1, T2 p2, T3 p3, T4 p4)
148         {
149                 Numeric::Numeric n(numeric);
150                 n.push(target->nick);
151                 n.push(p1);
152                 n.push(p2);
153                 n.push(p3);
154                 n.push(p4);
155                 SendLine(n);
156         }
157
158         /** Send a line of WHOIS data to the source of the WHOIS
159          * @param numeric Numeric to send
160          */
161         virtual void SendLine(Numeric::Numeric& numeric) = 0;
162 };