summaryrefslogtreecommitdiff
path: root/include/modules/stats.h
blob: 02b3b9b4aa8cd3c7cae1dce310d47180db9afe80 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*
 * InspIRCd -- Internet Relay Chat Daemon
 *
 *   Copyright (C) 2018 Sadie Powell <sadie@witchery.services>
 *   Copyright (C) 2016 Attila Molnar <attilamolnar@hush.com>
 *
 * This file is part of InspIRCd.  InspIRCd is free software: you can
 * redistribute it and/or modify it under the terms of the GNU General Public
 * License as published by the Free Software Foundation, version 2.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */


#pragma once

#include "event.h"

namespace Stats
{
	class Context;
	class EventListener;
	class Row;
}

class Stats::EventListener : public Events::ModuleEventListener
{
 public:
	EventListener(Module* mod)
		: ModuleEventListener(mod, "event/stats")
	{
	}

	/** Called when the STATS command is executed.
	 * @param stats Context of the /STATS request, contains requesting user, list of answer rows etc.
	 * @return MOD_RES_DENY if the stats request has been fulfilled. Otherwise, MOD_RES_PASSTHRU.
	 */
	virtual ModResult OnStats(Stats::Context& stats) = 0;
};

class Stats::Row : public Numeric::Numeric
{
 public:
	Row(unsigned int num)
		: Numeric(num)
	{
	}
};

class Stats::Context
{
	/** Source user of the STATS request
	 */
	User* const source;

	/** List of reply rows
	 */
	std::vector<Row> rows;

	/** Symbol indicating the type of this STATS request (usually a letter)
	 */
	const char symbol;

 public:
	/** Constructor
	 * @param src Source user of the STATS request, can be a local or remote user
	 * @param sym Symbol (letter) indicating the type of the request
	 */
	Context(User* src, char sym)
		: source(src)
		, symbol(sym)
	{
	}

	/** Get the source user of the STATS request
	 * @return Source user of the STATS request
	 */
	User* GetSource() const { return source; }

	/** Get the list of reply rows
	 * @return List of rows generated as reply for the request
	 */
	const std::vector<Row>& GetRows() const { return rows; }

	/** Get the symbol (letter) indicating what type of STATS was requested
	 * @return Symbol specified by the requesting user
	 */
	char GetSymbol() const { return symbol; }

	/** Add a row to the reply list
	 * @param row Reply to add
	 */
	void AddRow(const Row& row) { rows.push_back(row); }

	template <typename T1>
	void AddRow(unsigned int numeric, T1 p1)
	{
		Row n(numeric);
		n.push(p1);
		AddRow(n);
	}

	template <typename T1, typename T2>
	void AddRow(unsigned int numeric, T1 p1, T2 p2)
	{
		Row n(numeric);
		n.push(p1);
		n.push(p2);
		AddRow(n);
	}

	template <typename T1, typename T2, typename T3>
	void AddRow(unsigned int numeric, T1 p1, T2 p2, T3 p3)
	{
		Row n(numeric);
		n.push(p1);
		n.push(p2);
		n.push(p3);
		AddRow(n);
	}

	template <typename T1, typename T2, typename T3, typename T4>
	void AddRow(unsigned int numeric, T1 p1, T2 p2, T3 p3, T4 p4)
	{
		Row n(numeric);
		n.push(p1);
		n.push(p2);
		n.push(p3);
		n.push(p4);
		AddRow(n);
	}

	template <typename T1, typename T2, typename T3, typename T4, typename T5>
	void AddRow(unsigned int numeric, T1 p1, T2 p2, T3 p3, T4 p4, T5 p5)
	{
		Row n(numeric);
		n.push(p1);
		n.push(p2);
		n.push(p3);
		n.push(p4);
		n.push(p5);
		AddRow(n);
	}

	template <typename T1, typename T2, typename T3, typename T4, typename T5, typename T6>
	void AddRow(unsigned int numeric, T1 p1, T2 p2, T3 p3, T4 p4, T5 p5, T6 p6)
	{
		Row n(numeric);
		n.push(p1);
		n.push(p2);
		n.push(p3);
		n.push(p4);
		n.push(p5);
		n.push(p6);
		AddRow(n);
	}

	template <typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7>
	void AddRow(unsigned int numeric, T1 p1, T2 p2, T3 p3, T4 p4, T5 p5, T6 p6, T7 p7)
	{
		Row n(numeric);
		n.push(p1);
		n.push(p2);
		n.push(p3);
		n.push(p4);
		n.push(p5);
		n.push(p6);
		n.push(p7);
		AddRow(n);
	}

	template <typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7, typename T8>
	void AddRow(unsigned int numeric, T1 p1, T2 p2, T3 p3, T4 p4, T5 p5, T6 p6, T7 p7, T8 p8)
	{
		Row n(numeric);
		n.push(p1);
		n.push(p2);
		n.push(p3);
		n.push(p4);
		n.push(p5);
		n.push(p6);
		n.push(p7);
		n.push(p8);
		AddRow(n);
	}
};