-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsend_raw.cpp
More file actions
250 lines (210 loc) · 8.9 KB
/
send_raw.cpp
File metadata and controls
250 lines (210 loc) · 8.9 KB
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/*
* Copyright (C) 2004-2025 ZNC, see the NOTICE file for details.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <znc/User.h>
#include <znc/IRCNetwork.h>
#include <znc/IRCSock.h>
using std::vector;
using std::map;
class CSendRaw_Mod : public CModule {
void SendClient(const CString& sLine) {
CUser* pUser =
FindUser(sLine.Token(1));
if (pUser) {
CIRCNetwork* pNetwork =
FindNetwork(pUser, sLine.Token(2));
if (pNetwork) {
pNetwork->PutUser(sLine.Token(3, true));
PutModule(t_f("Sent [{1}] to {2}/{3}")(sLine.Token(3, true),
pUser->GetUsername(),
pNetwork->GetName()));
}
}
}
void SendServer(const CString& sLine) {
CUser* pUser =
FindUser(sLine.Token(1));
if (pUser) {
CIRCNetwork* pNetwork =
FindNetwork(pUser, sLine.Token(2));
if (pNetwork) {
pNetwork->PutIRC(sLine.Token(3, true));
PutModule(t_f("Sent [{1}] to IRC server of {2}/{3}")(
sLine.Token(3, true), pUser->GetUsername(),
pNetwork->GetName()));
}
}
}
void SendZNC(const CString& sLine) {
if (!GetUser()->IsAdmin()) {
PutModule("Access denied!");
return;
}
CUser* pUser = CZNC::Get().FindUser(sLine.Token(1));
if (pUser) {
CIRCNetwork* pNetwork = pUser->FindNetwork(sLine.Token(2));
if (pNetwork) {
// This processes the message as if it came FROM the IRC server
pNetwork->GetIRCSock()->ReadLine(sLine.Token(3, true));
PutModule(t_f("Injected [{1}] from IRC server to {2}/{3}")(
sLine.Token(3, true), pUser->GetUsername(),
pNetwork->GetName()));
} else {
PutModule(t_f("Network {1} not found for user {2}")(
sLine.Token(2), sLine.Token(1)));
}
} else {
PutModule(t_f("User {1} not found")(sLine.Token(1)));
}
}
void CurrentClient(const CString& sLine) {
CString sData = sLine.Token(1, true);
GetClient()->PutClient(sData);
}
CUser* FindUser(const CString& sUsername) {
if (sUsername.Equals("$me") || sUsername.Equals("$user"))
return GetUser();
CUser* pUser = CZNC::Get().FindUser(sUsername);
if (!pUser) {
PutModule(t_f("User [{1}] not found")(sUsername));
return nullptr;
}
// For non-admin users, only allow access to themselves
if (pUser != GetUser() && !GetUser()->IsAdmin()) {
PutModule(t_s(
"You need to have admin rights to modify other users"));
return nullptr;
}
return pUser;
}
CIRCNetwork* FindNetwork(CUser* pUser, const CString& sNetwork) {
if (sNetwork.Equals("$net") || sNetwork.Equals("$network")) {
if (pUser != GetUser()) {
PutModule(t_s(
"You cannot use $network to modify other users!"));
return nullptr;
}
return CModule::GetNetwork();
}
CIRCNetwork* pNetwork = pUser->FindNetwork(sNetwork);
if (!pNetwork) {
PutModule(
t_f("User {1} does not have a network named [{2}].")(
pUser->GetUsername(), sNetwork));
}
return pNetwork;
}
public:
~CSendRaw_Mod() override {}
bool OnLoad(const CString& sArgs, CString& sErrorMsg) override {
return true;
}
CString GetWebMenuTitle() override { return t_s("Send Raw"); }
bool OnWebRequest(CWebSock& WebSock, const CString& sPageName,
CTemplate& Tmpl) override {
if (sPageName == "index") {
if (WebSock.IsPost()) {
CUser* pCurrentUser = GetUser();
// For non-admin users, validate they're only accessing their
// own data
CString sRequestedUser =
WebSock.GetParam("network").Token(0, false, "/");
if (!pCurrentUser->IsAdmin() &&
sRequestedUser != pCurrentUser->GetUsername()) {
WebSock.GetSession()->AddError(
t_s("You can only send to your own user"));
return true;
}
CUser* pUser = CZNC::Get().FindUser(
WebSock.GetParam("network").Token(0, false, "/"));
if (!pUser) {
WebSock.GetSession()->AddError(t_s("User not found"));
return true;
}
CIRCNetwork* pNetwork = pUser->FindNetwork(
WebSock.GetParam("network").Token(1, false, "/"));
if (!pNetwork) {
WebSock.GetSession()->AddError(t_s("Network not found"));
return true;
}
bool bToServer = WebSock.GetParam("send_to") == "server";
const CString sLine = WebSock.GetParam("line");
Tmpl["user"] = pUser->GetUsername();
Tmpl[bToServer ? "to_server" : "to_client"] = "true";
Tmpl["line"] = sLine;
if (bToServer) {
pNetwork->PutIRC(sLine);
} else {
pNetwork->PutUser(sLine);
}
WebSock.GetSession()->AddSuccess(t_s("Line sent"));
}
CUser* pCurrentUser = GetUser();
if (pCurrentUser->IsAdmin()) {
// Admin users can see all users
const map<CString, CUser*>& msUsers = CZNC::Get().GetUserMap();
for (const auto& it : msUsers) {
CTemplate& l = Tmpl.AddRow("UserLoop");
l["Username"] = it.second->GetUsername();
vector<CIRCNetwork*> vNetworks = it.second->GetNetworks();
for (const CIRCNetwork* pNetwork : vNetworks) {
CTemplate& NetworkLoop = l.AddRow("NetworkLoop");
NetworkLoop["Username"] = it.second->GetUsername();
NetworkLoop["Network"] = pNetwork->GetName();
}
}
} else {
// Non-admin users can only see their own networks
CTemplate& l = Tmpl.AddRow("UserLoop");
l["Username"] = pCurrentUser->GetUsername();
vector<CIRCNetwork*> vNetworks = pCurrentUser->GetNetworks();
for (const CIRCNetwork* pNetwork : vNetworks) {
CTemplate& NetworkLoop = l.AddRow("NetworkLoop");
NetworkLoop["Username"] = pCurrentUser->GetUsername();
NetworkLoop["Network"] = pNetwork->GetName();
}
}
return true;
}
return false;
}
MODCONSTRUCTOR(CSendRaw_Mod) {
AddHelpCommand();
CString sUserRef =
GetUser()->IsAdmin() ? t_s("the user's") : t_s("your");
CString sYouRef =
GetUser()->IsAdmin() ? t_s("the user is") : t_s("you are");
AddCommand(
"Client", t_d("[user] [network] [data to send]"),
t_d("The data will be sent to " + sUserRef + " IRC client(s)"),
[=](const CString& sLine) { SendClient(sLine); });
AddCommand("Server", t_d("[user] [network] [data to send]"),
t_d("The data will be sent to the IRC server " + sYouRef +
" connected to"),
[=](const CString& sLine) { SendServer(sLine); });
AddCommand("Current", t_d("[data to send]"),
t_d("The data will be sent to your current client"),
[=](const CString& sLine) { CurrentClient(sLine); });
AddCommand("ZNC", t_d("[user] [network] [data to send]"),
t_d("The data will be sent as if sent from the IRC server"),
[=](const CString& sLine) { SendZNC(sLine); });
}
};
template <>
void TModInfo<CSendRaw_Mod>(CModInfo& Info) {
Info.SetWikiPage("send_raw");
}
USERMODULEDEFS(CSendRaw_Mod, t_s("Lets you send raw IRC lines as yourself (and "
"others if you are admin)"))