forked from mbelop/icbd
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcmd.c
424 lines (379 loc) · 10.5 KB
/
cmd.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
/*
* Copyright (c) 2009, 2010, 2013 Mike Belopuhov
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/types.h>
#include <sys/queue.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <unistd.h>
#include <vis.h>
#include <event.h>
#include "icb.h"
#include "icbd.h"
extern int creategroups;
void icb_cmd_help(struct icb_session *, char *);
void icb_cmd_beep(struct icb_session *, char *);
void icb_cmd_boot(struct icb_session *, char *);
void icb_cmd_change(struct icb_session *, char *);
void icb_cmd_name(struct icb_session *, char *);
void icb_cmd_nobeep(struct icb_session *, char *);
void icb_cmd_personal(struct icb_session *, char *);
void icb_cmd_pass(struct icb_session *, char *);
void icb_cmd_topic(struct icb_session *, char *);
void icb_cmd_who(struct icb_session *, char *);
void *
icb_cmd_lookup(char *cmd)
{
struct {
const char *cmd;
void (*handler)(struct icb_session *, char *);
} cmdtab[] = {
{ "?", icb_cmd_help },
{ "beep", icb_cmd_beep },
{ "boot", icb_cmd_boot },
{ "g", icb_cmd_change },
{ "m", icb_cmd_personal },
{ "msg", icb_cmd_personal },
{ "name", icb_cmd_name },
{ "nobeep", icb_cmd_nobeep },
{ "pass", icb_cmd_pass },
{ "topic", icb_cmd_topic },
{ "w", icb_cmd_who },
{ NULL, NULL }
};
int i;
for (i = 0; cmdtab[i].cmd != NULL; i++)
if (strcasecmp(cmdtab[i].cmd, cmd) == 0)
return (cmdtab[i].handler);
return (NULL);
}
void
icb_cmd_help(struct icb_session *is, char *arg __attribute__((unused)))
{
icb_status(is, STATUS_HELP, "Server supports following commands:");
icb_status(is, STATUS_HELP, "beep boot g m name nobeep pass topic w");
}
void
icb_cmd_beep(struct icb_session *is, char *arg)
{
struct icb_group *ig = is->group;
struct icb_session *s;
char whom[ICB_MAXNICKLEN];
if (strlen(arg) == 0) {
icb_error(is, "Invalid user");
return;
}
icb_vis(whom, arg, ICB_MAXNICKLEN, VIS_SP);
/* Search in the same group first */
LIST_FOREACH(s, &ig->sess, entry) {
if (strcmp(s->nick, whom) == 0)
break;
}
if (s == NULL) {
/* See if we can find someone else to beep */
LIST_FOREACH(ig, &groups, entry) {
if (strcmp(is->group->name, ig->name) == 0)
continue;
LIST_FOREACH(s, &ig->sess, entry) {
if (strcmp(s->nick, whom) == 0)
break;
}
if (s != NULL)
break;
}
}
if (s == NULL) {
icb_status(is, STATUS_NOTIFY, "%s is not signed on", whom);
return;
}
if (s == is) {
icb_error(is, "Very funny... Not!");
return;
}
if (ISSETF(s->flags, ICB_SF_NOBEEP | ICB_SF_NOBEEP2)) {
icb_error(is, "User has nobeep enabled");
if (ISSETF(s->flags, ICB_SF_NOBEEP2))
icb_status(s, STATUS_NOBEEP,
"%s attempted to beep you", is->nick);
return;
}
icb_sendfmt(s, "%c%s", ICB_M_BEEP, is->nick);
}
void
icb_cmd_boot(struct icb_session *is, char *arg)
{
struct icb_group *ig;
struct icb_session *s;
char whom[ICB_MAXNICKLEN];
/* to boot or not to boot, that is the question */
ig = is->group;
if (!icb_ismod(ig, is)) {
icb_status(is, STATUS_NOTIFY, "Sorry, booting is a privilege "
"you don't possess");
return;
}
if (strlen(arg) == 0) {
icb_error(is, "Invalid user");
return;
}
icb_vis(whom, arg, ICB_MAXNICKLEN, VIS_SP);
/* who would be a target then? */
LIST_FOREACH(s, &ig->sess, entry) {
if (strcmp(s->nick, whom) == 0)
break;
}
if (s == NULL) {
icb_status(is, STATUS_NOTIFY, "No such user");
return;
}
if (s == is) {
icb_error(is, "Just quit, would you?");
return;
}
/* okay, here we go, but first, be polite and notify a user */
icb_status(s, STATUS_BOOT, "%s booted you", is->nick);
icb_status_group(s->group, s, STATUS_BOOT, "%s was booted", s->nick);
icbd_drop(s, "booted");
}
void
icb_cmd_change(struct icb_session *is, char *arg)
{
struct icb_group *ig;
struct icb_session *s;
char group[ICB_MAXGRPLEN];
int changing = 0;
if (strlen(arg) == 0) {
icb_error(is, "Invalid group name");
return;
}
icb_vis(group, arg, ICB_MAXGRPLEN, VIS_SP);
LIST_FOREACH(ig, &groups, entry) {
if (strcmp(ig->name, group) == 0)
break;
}
if (ig == NULL) {
if (!creategroups) {
icb_error(is, "Can't create new groups");
return;
} else {
if ((ig = icb_addgroup(is, group)) == NULL) {
icb_error(is, "Can't create group %s", group);
return;
}
icbd_log(NULL, LOG_DEBUG, "%s created group %s",
is->nick, group);
}
}
/* see if we're changing to the same group */
if (is->group && is->group == ig) {
icb_error(is, "You are already in that group");
return;
}
LIST_FOREACH(s, &ig->sess, entry) {
if (strcmp(s->nick, is->nick) == 0) {
icb_error(is, "Nick is already in use");
return;
}
}
if (is->group) {
changing = 1;
if (icb_ismod(is->group, is))
(void)icb_pass(is->group, is, NULL);
LIST_REMOVE(is, entry);
icb_status_group(is->group, NULL, STATUS_DEPART,
"%s (%s@%s) just left", is->nick, is->client, is->host);
}
is->group = ig;
LIST_INSERT_HEAD(&ig->sess, is, entry);
/* notify group */
icb_status_group(ig, is, changing ? STATUS_ARRIVE : STATUS_SIGNON,
"%s (%s@%s) entered group", is->nick, is->client, is->host);
/* acknowledge successful join */
icb_status(is, STATUS_STATUS, "You are now in group %s%s", ig->name,
icb_ismod(ig, is) ? " as moderator" : "");
/* send user a topic name */
if (strlen(ig->topic) > 0)
icb_status(is, STATUS_TOPIC, "The topic is: %s", ig->topic);
}
void
icb_cmd_name(struct icb_session *is, char *arg)
{
struct icb_group *ig = is->group;
struct icb_session *s;
char nick[ICB_MAXNICKLEN];
if (strlen(arg) == 0) {
icb_status(is, STATUS_NAME, "Your nickname is %s",
is->nick);
return;
}
if (strcasecmp(arg, "admin") == 0) {
icb_error(is, "Wuff wuff!");
return;
}
/* sanitize user input */
if (strlen(arg) > ICB_MAXNICKLEN)
arg[ICB_MAXNICKLEN - 1] = '\0';
icb_vis(nick, arg, ICB_MAXNICKLEN, VIS_SP);
LIST_FOREACH(s, &ig->sess, entry) {
if (strcmp(s->nick, nick) == 0) {
icb_error(is, "Nick is already in use");
return;
}
}
icb_status_group(ig, NULL, STATUS_NAME,
"%s changed nickname to %s", is->nick, nick);
strlcpy(is->nick, nick, sizeof is->nick);
}
void
icb_cmd_nobeep(struct icb_session *is, char *arg)
{
if (strlen(arg) == 0) {
/* fail if we have verbose turned on */
if (ISSETF(is->flags, ICB_SF_NOBEEP2)) {
icb_error(is, "Can't toggle your nobeep status");
return;
}
/* otherwise toggle the status */
if (ISSETF(is->flags, ICB_SF_NOBEEP))
CLRF(is->flags, ICB_SF_NOBEEP);
else
SETF(is->flags, ICB_SF_NOBEEP);
icb_status(is, STATUS_NOBEEP, "No-Beep %s",
ISSETF(is->flags, ICB_SF_NOBEEP) ? "on" : "off");
return;
}
if (strcmp(arg, "on") == 0) {
SETF(is->flags, ICB_SF_NOBEEP);
CLRF(is->flags, ICB_SF_NOBEEP2); /* can't be on and verbose */
icb_status(is, STATUS_NOBEEP, "No-Beep on");
} else if (strcmp(arg, "verbose") == 0) {
SETF(is->flags, ICB_SF_NOBEEP2);
CLRF(is->flags, ICB_SF_NOBEEP); /* can't be on and verbose */
icb_status(is, STATUS_NOBEEP, "No-Beep on (verbose)");
} else if (strcmp(arg, "off") == 0) {
CLRF(is->flags, ICB_SF_NOBEEP | ICB_SF_NOBEEP2);
icb_status(is, STATUS_NOBEEP, "No-Beep off");
} else
icb_error(is, "Invalid nobeep mode");
}
void
icb_cmd_personal(struct icb_session *is, char *arg)
{
char *p;
if ((p = strchr(arg, ' ')) == 0) {
icb_error(is, "Empty message");
return;
}
*p = '\0';
icb_privmsg(is, arg, ++p);
}
void
icb_cmd_pass(struct icb_session *is, char *arg)
{
struct icb_group *ig = is->group;
struct icb_session *s;
char whom[ICB_MAXNICKLEN];
if (icb_ismod(ig, is)) {
/*
* we're a current group moderator, allow to relinquish
* the right and to pass it down to everybody else.
*/
if (strlen(arg) == 0) {
/* no argument: relinquish moderator */
(void)icb_pass(ig, ig->mod, NULL);
return;
}
icb_vis(whom, arg, ICB_MAXNICKLEN, VIS_SP);
LIST_FOREACH(s, &ig->sess, entry) {
if (strcmp(s->nick, whom) == 0)
break;
}
if (s == NULL) {
icb_status(is, STATUS_NOTIFY, "No such user");
return;
}
if (icb_pass(ig, ig->mod, s) < 0)
icb_error(is, "Failed to pass group moderation.");
} else {
/*
* if group is moderated and we're not the moderator,
* but modtab is enabled, then check the permission
* and pass moderation if successful. if there's no
* current moderator, don't enforce the modtab.
*/
if (!icb_modpermit(is, ig->mod ? 1 : 0)) {
icb_error(is, "Operation not permitted.");
return;
}
if (icb_pass(ig, ig->mod, is) < 0)
icb_error(is, "Failed to acquire group moderation.");
}
}
void
icb_cmd_topic(struct icb_session *is, char *arg)
{
struct icb_group *ig = is->group;
char topic[ICB_MAXTOPICLEN];
if (strlen(arg) == 0) { /* querying the topic */
if (strlen(ig->topic) > 0)
icb_status(is, STATUS_TOPIC, "The topic is: %s",
ig->topic);
else
icb_status(is, STATUS_TOPIC, "The topic is not set.");
} else { /* setting the topic */
if (!icb_ismod(ig, is)) {
icb_status(is, STATUS_NOTIFY, "Setting the topic is "
"only for moderators.");
return;
}
icb_vis(topic, arg, ICB_MAXTOPICLEN, 0);
strlcpy(ig->topic, topic, sizeof ig->topic);
icb_status_group(ig, NULL, STATUS_TOPIC,
"%s changed the topic to \"%s\"", is->nick, ig->topic);
}
}
void
icb_cmd_who(struct icb_session *is, char *arg)
{
struct icb_group *ig;
char group[ICB_MAXGRPLEN];
while (strlen(arg) && arg[0] == '-') { /* ignore options, for now */
/* ircII "set SHOW_CHANNEL_NAMES ON" uses /w -s */
while(arg[0] != ' ' && arg[0] != 0)
arg++;
if(arg[0] == ' ')
arg++;
}
if (strlen(arg) == 0)
return icb_who(is, NULL);
/* pidgin-icb treats '.' as the current group */
if (strlen(arg) == 1 && arg[0] == '.') {
icb_who(is, is->group);
return;
}
icb_vis(group, arg, ICB_MAXGRPLEN, VIS_SP);
LIST_FOREACH(ig, &groups, entry) {
if (strcmp(ig->name, group) == 0)
break;
}
if (ig == NULL) {
icb_error(is, "The group %s doesn't exist.", group);
return;
}
icb_who(is, ig);
}