forked from fchat-pidgin/fchat-pidgin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
f-list_admin.c
433 lines (348 loc) · 16.1 KB
/
f-list_admin.c
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
/*
* F-List Pidgin - a libpurple protocol plugin for F-Chat
*
* Copyright 2011 F-List Pidgin developers.
*
* This file is part of F-List Pidgin.
*
* F-List Pidgin 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, either version 2 of the License, or
* (at your option) any later version.
*
* F-List Pidgin 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 F-List Pidgin. If not, see <http://www.gnu.org/licenses/>.
*/
#include "f-list_admin.h"
static void flist_admin_request_cancel_string_cb(gpointer user_data, const gchar *name) {
PurpleConnection *pc = user_data;
FListAccount *fla = purple_connection_get_protocol_data(pc);
g_return_if_fail(fla);
fla->input_request = FALSE;
}
static void flist_create_public_channel_cb(gpointer user_data, const gchar *name) {
PurpleConnection *pc = user_data;
FListAccount *fla = purple_connection_get_protocol_data(pc);
JsonObject *json;
g_return_if_fail(fla);
json = json_object_new();
json_object_set_string_member(json, "channel", name);
flist_request(fla, FLIST_PUBLIC_CHANNEL_CREATE, json);
json_object_unref(json);
fla->input_request = FALSE;
}
void flist_create_public_channel_action(PurplePluginAction *action) {
PurpleConnection *pc = action->context;
PurpleAccount *pa = purple_connection_get_account(pc);
FListAccount *fla = purple_connection_get_protocol_data(pc);
g_return_if_fail(fla);
if(fla->input_request) return;
purple_request_input(pc, _("Create Public Channel"), _("Create a public channel on the F-List server."),
_("Please enter a title for your new channel."), "",
FALSE, FALSE, NULL,
_("OK"), G_CALLBACK(flist_create_public_channel_cb),
_("Cancel"), G_CALLBACK(flist_admin_request_cancel_string_cb),
pa, NULL, NULL, pc);
fla->input_request = TRUE;
}
static void flist_delete_public_channel_cb(gpointer user_data, const gchar *name) {
PurpleConnection *pc = user_data;
FListAccount *fla = purple_connection_get_protocol_data(pc);
JsonObject *json;
g_return_if_fail(fla);
json = json_object_new();
json_object_set_string_member(json, "channel", name);
flist_request(fla, FLIST_PUBLIC_CHANNEL_DELETE, json);
json_object_unref(json);
fla->input_request = FALSE;
}
void flist_delete_public_channel_action(PurplePluginAction *action) {
PurpleConnection *pc = action->context;
PurpleAccount *pa = purple_connection_get_account(pc);
FListAccount *fla = purple_connection_get_protocol_data(pc);
g_return_if_fail(fla);
if(fla->input_request) return;
purple_request_input(pc, _("Delete Public Channel"), _("Delete a public channel from the F-List server."),
_("Please enter the title of the channel to delete."), "",
FALSE, FALSE, NULL,
_("OK"), G_CALLBACK(flist_delete_public_channel_cb),
_("Cancel"), G_CALLBACK(flist_admin_request_cancel_string_cb),
pa, NULL, NULL, pc); //TODO: give a list of public channels instead!
fla->input_request = TRUE;
}
static void flist_add_global_operator_cb(gpointer user_data, const gchar *name) {
PurpleConnection *pc = user_data;
FListAccount *fla = purple_connection_get_protocol_data(pc);
JsonObject *json;
g_return_if_fail(fla);
json = json_object_new();
json_object_set_string_member(json, "character", name);
flist_request(fla, FLIST_ADD_GLOBAL_OPERATOR, json);
json_object_unref(json);
fla->input_request = FALSE;
}
void flist_add_global_operator_action(PurplePluginAction *action) {
PurpleConnection *pc = action->context;
PurpleAccount *pa = purple_connection_get_account(pc);
FListAccount *fla = purple_connection_get_protocol_data(pc);
g_return_if_fail(fla);
if(fla->input_request) return;
purple_request_input(pc, _("Add Global Operator"), _("Add a global operator to the F-List server."),
_("Please enter the name of the character to promote."), "",
FALSE, FALSE, NULL,
_("OK"), G_CALLBACK(flist_add_global_operator_cb),
_("Cancel"), G_CALLBACK(flist_admin_request_cancel_string_cb),
pa, NULL, NULL, pc);
fla->input_request = TRUE;
}
static void flist_remove_global_operator_cb(gpointer user_data, const gchar *name) {
PurpleConnection *pc = user_data;
FListAccount *fla = purple_connection_get_protocol_data(pc);
JsonObject *json;
g_return_if_fail(fla);
json = json_object_new();
json_object_set_string_member(json, "character", name);
flist_request(fla, FLIST_REMOVE_GLOBAL_OPERATOR, json);
json_object_unref(json);
fla->input_request = FALSE;
}
void flist_remove_global_operator_action(PurplePluginAction *action) {
PurpleConnection *pc = action->context;
PurpleAccount *pa = purple_connection_get_account(pc);
FListAccount *fla = purple_connection_get_protocol_data(pc);
g_return_if_fail(fla);
if(fla->input_request) return;
purple_request_input(pc, _("Remove Global Operator"), _("Remove a global operator from the F-List server."),
_("Please enter the name of the character to demote."), "",
FALSE, FALSE, NULL,
_("OK"), G_CALLBACK(flist_remove_global_operator_cb),
_("Cancel"), G_CALLBACK(flist_admin_request_cancel_string_cb),
pa, NULL, NULL, pc); //TODO: give a list of global operators instead!
fla->input_request = TRUE;
}
static void flist_broadcast_action_cb(gpointer user_data, const gchar *message) {
PurpleConnection *pc = user_data;
FListAccount *fla = purple_connection_get_protocol_data(pc);
JsonObject *json;
g_return_if_fail(fla);
json = json_object_new();
json_object_set_string_member(json, "message", message);
flist_request(fla, FLIST_BROADCAST, json);
json_object_unref(json);
fla->input_request = FALSE;
}
void flist_broadcast_action(PurplePluginAction *action) {
PurpleConnection *pc = action->context;
PurpleAccount *pa = purple_connection_get_account(pc);
FListAccount *fla = purple_connection_get_protocol_data(pc);
g_return_if_fail(fla);
if(fla->input_request) return;
purple_request_input(pc, _("Broadcast"), _("Send a broadcast to the entire F-List server."),
_("Please enter the message you want to broadcast."), "",
FALSE, FALSE, NULL,
_("OK"), G_CALLBACK(flist_broadcast_action_cb),
_("Cancel"), G_CALLBACK(flist_admin_request_cancel_string_cb),
pa, NULL, NULL, pc);
fla->input_request = TRUE;
}
PurpleCmdRet flist_admin_op_deop_cmd(PurpleConversation *convo, const gchar *cmd, gchar **args, gchar **error, void *data) {
FListAccount *fla = purple_connection_get_protocol_data(purple_conversation_get_gc(convo));
g_return_val_if_fail(fla, PURPLE_CMD_RET_FAILED);
const gchar *character;
const gchar *code = NULL;
JsonObject *json;
if(!purple_utf8_strcasecmp(cmd, "op")) code = FLIST_ADD_GLOBAL_OPERATOR;
if(!purple_utf8_strcasecmp(cmd, "deop")) code = FLIST_REMOVE_GLOBAL_OPERATOR;
if(!code) return PURPLE_CMD_RET_FAILED;
character = args[0];
json = json_object_new();
json_object_set_string_member(json, "character", character);
flist_request(fla, code, json);
json_object_unref(json);
return PURPLE_CMD_RET_OK;
}
PurpleCmdRet flist_global_kick_ban_unban_cmd(PurpleConversation *convo, const gchar *cmd, gchar **args, gchar **error, void *data) {
FListAccount *fla = purple_connection_get_protocol_data(purple_conversation_get_gc(convo));
g_return_val_if_fail(fla, PURPLE_CMD_RET_FAILED);
const gchar *character;
const gchar *code = NULL;
JsonObject *json;
if(!FLIST_HAS_MIN_PERMISSION(flist_get_permissions(fla, fla->character, NULL), FLIST_PERMISSION_GLOBAL_OP)) {
*error = g_strdup(_("You must be a global operator to globally kick, ban, or unban."));
return PURPLE_CMD_RET_FAILED;
}
if(!purple_utf8_strcasecmp(cmd, "gkick")) code = FLIST_GLOBAL_KICK;
if(!purple_utf8_strcasecmp(cmd, "ipban")) code = FLIST_GLOBAL_IP_BAN;
if(!purple_utf8_strcasecmp(cmd, "accountban")) code = FLIST_GLOBAL_ACCOUNT_BAN;
if(!purple_utf8_strcasecmp(cmd, "gunban")) code = FLIST_GLOBAL_UNBAN;
if(!code) return PURPLE_CMD_RET_FAILED;
character = args[0];
json = json_object_new();
json_object_set_string_member(json, "character", character);
flist_request(fla, code, json);
json_object_unref(json);
return PURPLE_CMD_RET_OK;
}
PurpleCmdRet flist_create_kill_channel_cmd(PurpleConversation *convo, const gchar *cmd, gchar **args, gchar **error, void *data) {
FListAccount *fla = purple_connection_get_protocol_data(purple_conversation_get_gc(convo));
g_return_val_if_fail(fla, PURPLE_CMD_RET_FAILED);
const gchar *channel;
const gchar *code = NULL;
JsonObject *json;
if(!FLIST_HAS_MIN_PERMISSION(flist_get_permissions(fla, fla->character, NULL), FLIST_PERMISSION_GLOBAL_OP)) {
*error = g_strdup(_("You must be a global operator to create or delete public channels."));
return PURPLE_CMD_RET_FAILED;
}
if(!purple_utf8_strcasecmp(cmd, "createchannel")) code = FLIST_PUBLIC_CHANNEL_CREATE;
if(!purple_utf8_strcasecmp(cmd, "killchannel")) code = FLIST_PUBLIC_CHANNEL_DELETE;
if(!code) return PURPLE_CMD_RET_FAILED;
channel = args[0];
json = json_object_new();
json_object_set_string_member(json, "channel", channel);
flist_request(fla, code, json);
json_object_unref(json);
return PURPLE_CMD_RET_OK;
}
PurpleCmdRet flist_broadcast_cmd(PurpleConversation *convo, const gchar *cmd, gchar **args, gchar **error, void *data) {
FListAccount *fla = purple_connection_get_protocol_data(purple_conversation_get_gc(convo));
g_return_val_if_fail(fla, PURPLE_CMD_RET_FAILED);
const gchar *message;
JsonObject *json;
// We can't check if a user is an admin anyway, so just let it up to the server
message = args[0];
json = json_object_new();
json_object_set_string_member(json, "message", message);
flist_request(fla, FLIST_BROADCAST, json);
json_object_unref(json);
return PURPLE_CMD_RET_OK;
}
PurpleCmdRet flist_timeout_cmd(PurpleConversation *convo, const gchar *cmd, gchar **args, gchar **error, void *data) {
FListAccount *fla = purple_connection_get_protocol_data(purple_conversation_get_gc(convo));
g_return_val_if_fail(fla, PURPLE_CMD_RET_FAILED);
gchar **split; guint count;
const gchar *character, *timestr, *reason;
JsonObject *json;
if(!FLIST_HAS_MIN_PERMISSION(flist_get_permissions(fla, fla->character, NULL), FLIST_PERMISSION_GLOBAL_OP)) {
*error = g_strdup(_("You must be a global operator to timeban."));
return PURPLE_CMD_RET_FAILED;
}
split = g_strsplit(args[0], ",", 3);
count = g_strv_length(split);
if(count < 3) {
g_strfreev(split);
*error = g_strdup(_("You must enter a character, a time, and a reason."));
return PURPLE_CMD_RET_FAILED;
}
character = split[0];
timestr = g_strchug(split[1]);
reason = g_strchug(split[2]);
guint64 time_parsed = flist_parse_duration_str(timestr);
if(time_parsed == 0) {
g_strfreev(split);
*error = g_strdup("You must enter a valid duration (e.g.: 4d3h12m, or just 1440).");
return PURPLE_CMD_RET_FAILED;
}
gchar *time_formatted = flist_format_duration_str(time_parsed);
gchar *message = g_strdup_printf("You have timed out %s for %s from the server, giving the reason: %s.", character, time_formatted, reason);
purple_conv_chat_write(PURPLE_CONV_CHAT(convo), "", message, PURPLE_MESSAGE_SYSTEM, time(NULL));
g_free(message);
g_free(time_formatted);
json = json_object_new();
json_object_set_string_member(json, "character", character);
json_object_set_string_member(json, "reason", reason);
json_object_set_int_member(json, "time", time_parsed);
flist_request(fla, FLIST_GLOBAL_TIMEOUT, json);
json_object_unref(json);
g_strfreev(split);
return PURPLE_CMD_RET_OK;
}
PurpleCmdRet flist_reward_cmd(PurpleConversation *convo, const gchar *cmd, gchar **args, gchar **error, void *data) {
FListAccount *fla = purple_connection_get_protocol_data(purple_conversation_get_gc(convo));
g_return_val_if_fail(fla, PURPLE_CMD_RET_FAILED);
const gchar *character;
JsonObject *json;
if(!FLIST_HAS_MIN_PERMISSION(flist_get_permissions(fla, fla->character, NULL), FLIST_PERMISSION_GLOBAL_OP)) {
*error = g_strdup(_("You must be a global operator to reward a user."));
return PURPLE_CMD_RET_FAILED;
}
character = args[0];
json = json_object_new();
json_object_set_string_member(json, "character", character);
flist_request(fla, FLIST_REWARD, json);
json_object_unref(json);
return PURPLE_CMD_RET_OK;
}
void flist_send_sfc_confirm(FListAccount *fla, const gchar *callid) {
JsonObject *json;
json = json_object_new();
json_object_set_string_member(json, "action", "confirm");
json_object_set_string_member(json, "moderator", fla->character);
json_object_set_string_member(json, "callid", callid);
flist_request(fla, "SFC", json);
json_object_unref(json);
}
static void flist_sfc_report(FListAccount *fla, JsonObject *root) {
PurpleAccount *pa = fla->pa;
const gchar *callid, *reporter, *report;
gchar *s, *escaped_reporter, *escaped_report, *message;
GString *message_str;
gint logid; gboolean has_logid;
callid = json_object_get_string_member(root, "callid");
reporter = json_object_get_string_member(root, "character");
report = json_object_get_string_member(root, "report");
logid = json_object_get_parse_int_member(root, "logid", &has_logid);
g_return_if_fail(callid);
g_return_if_fail(reporter);
g_return_if_fail(report);
message_str = g_string_new(NULL);
s = g_strdup(purple_url_encode(flist_serialize_account(pa)));
escaped_reporter = purple_markup_escape_text(reporter, -1);
escaped_report = purple_markup_escape_text(report, -1);
g_string_append_printf(message_str, "Moderator Alert. <a href=\"" FLIST_URL_CHARACTER "\">%s</a> writes:\n", escaped_reporter, escaped_reporter);
g_string_append_printf(message_str, "%s\n", escaped_report);
g_string_append_printf(message_str, "<a href=\"flistsfc://%s/%s\">Confirm Alert</a>", s, purple_url_encode(callid));
g_string_append(message_str, ", ");
if(has_logid) {
g_string_append_printf(message_str, "<a href=\"" FLIST_URL_GETLOG "\">View Log</a>", logid);
} else {
g_string_append_printf(message_str, "(No Log)");
}
message = g_string_free(message_str, FALSE);
serv_got_im(fla->pc, GLOBAL_NAME, message, PURPLE_MESSAGE_RECV, time(NULL));
g_free(escaped_report);
g_free(escaped_reporter);
g_free(message);
g_free(s);
}
static void flist_sfc_confirm(FListAccount *fla, JsonObject *root) {
const gchar *moderator, *reporter;
gchar *message, *escaped_message, *bbcode_message;
moderator = json_object_get_string_member(root, "moderator");
reporter = json_object_get_string_member(root, "character");
g_return_if_fail(moderator);
g_return_if_fail(reporter);
message = g_strdup_printf("Alert Confirmed. [b]%s[/b] is handling [b]%s[/b]'s report.", moderator, reporter);
escaped_message = purple_markup_escape_text(message, -1);
bbcode_message = flist_bbcode_to_html(fla, NULL, escaped_message);
serv_got_im(fla->pc, GLOBAL_NAME, bbcode_message, PURPLE_MESSAGE_RECV, time(NULL));
g_free(bbcode_message);
g_free(escaped_message);
g_free(message);
}
gboolean flist_process_SFC(FListAccount *fla, JsonObject *root) {
const gchar *action;
action = json_object_get_string_member(root, "action");
g_return_val_if_fail(action, TRUE);
if(flist_str_equal(action, "report")) {
flist_sfc_report(fla, root);
}
if(flist_str_equal(action, "confirm")) {
flist_sfc_confirm(fla, root);
}
return TRUE;
}