source: code/icb.c@ fa271b8

Last change on this file since fa271b8 was a202ff1, checked in by Mike Belopuhov <mike@…>, 11 years ago

strip trailing spaces from all fields; based on the idea by sthen

  • Property mode set to 100644
File size: 13.6 KB
Line 
1/*
2 * Copyright (c) 2009, 2010, 2013 Mike Belopuhov
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16
17#include <sys/param.h>
18#include <sys/queue.h>
19#include <stdarg.h>
20#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
23#include <syslog.h>
24#include <unistd.h>
25#include <event.h>
26
27#include "icb.h"
28#include "icbd.h"
29
30extern int creategroups;
31extern char srvname[MAXHOSTNAMELEN];
32
33void icb_command(struct icb_session *, char *, char *);
34void icb_groupmsg(struct icb_session *, char *);
35void icb_login(struct icb_session *, char *, char *, char *);
36int icb_dowho(struct icb_session *, struct icb_group *);
37int icb_modpermit(struct icb_session *);
38char *icb_nextfield(char **);
39
40/*
41 * icb_init: initializes pointers to callbacks
42 */
43void
44icb_init(struct icbd_callbacks *ic)
45{
46 icb_drop = ic->drop;
47 icb_log = ic->log;
48 icb_send = ic->send;
49
50 LIST_INIT(&groups);
51
52 if (strlen(srvname) == 0)
53 (void)gethostname(srvname, sizeof srvname);
54 /*
55 * MAXHOSTNAMELEN is usually greater than what we
56 * can actually send, hence truncation:
57 */
58 if (strlen(srvname) > 200)
59 srvname[200] = '\0';
60}
61
62/*
63 * icb_start: called upon accepting a new connection, greets new client
64 */
65void
66icb_start(struct icb_session *is)
67{
68 icb_sendfmt(is, "%c%c%c%s%c%s", ICB_M_PROTO, '1', ICB_M_SEP, srvname,
69 ICB_M_SEP, "icbd");
70 SETF(is->flags, ICB_SF_PROTOSENT);
71}
72
73/*
74 * icb_input: main input processing routine
75 */
76void
77icb_input(struct icb_session *is)
78{
79 char *msg = is->buffer;
80 char type;
81
82 is->last = getmonotime();
83 type = msg[1];
84 msg += 2;
85 if (!ISSETF(is->flags, ICB_SF_LOGGEDIN) && type != ICB_M_LOGIN) {
86 icb_error(is, "Not logged in");
87 return;
88 }
89 switch (type) {
90 case ICB_M_LOGIN: {
91 char *nick, *group, *client, *cmd;
92
93 client = icb_nextfield(&msg);
94 nick = icb_nextfield(&msg);
95 group = icb_nextfield(&msg);
96 cmd = icb_nextfield(&msg);
97 if (strlen(cmd) > 0 && cmd[0] == 'w') {
98 icb_error(is, "Command not implemented");
99 icb_drop(is, NULL);
100 return;
101 }
102 if (strlen(cmd) == 0 || strcmp(cmd, "login") != 0) {
103 icb_error(is, "Malformed login packet");
104 icb_drop(is, NULL);
105 return;
106 }
107 icb_login(is, group, nick, client);
108 break;
109 }
110 case ICB_M_OPEN: {
111 char *grpmsg;
112
113 grpmsg = icb_nextfield(&msg);
114 icb_groupmsg(is, grpmsg);
115 break;
116 }
117 case ICB_M_COMMAND: {
118 char *cmd, *arg;
119
120 cmd = icb_nextfield(&msg);
121 arg = icb_nextfield(&msg);
122 icb_command(is, cmd, arg);
123 break;
124 }
125 case ICB_M_PONG: {
126 icb_sendfmt(is, "%c", ICB_M_PING);
127 break;
128 }
129 case ICB_M_PROTO:
130 case ICB_M_NOOP:
131 /* ignore */
132 break;
133 default:
134 /* everything else is not valid */
135 icb_error(is, "Bummer. This is a bummer, man.");
136 }
137}
138
139/*
140 * icb_login: handles login ('a') packets
141 */
142void
143icb_login(struct icb_session *is, char *group, char *nick, char *client)
144{
145 char *defgrp = "1";
146 struct icb_group *ig;
147 struct icb_session *s;
148
149 if (!nick || strlen(nick) == 0) {
150 icb_error(is, "Invalid nick");
151 icb_drop(is, NULL);
152 return;
153 }
154 if (!group || strlen(group) == 0)
155 group = defgrp;
156 LIST_FOREACH(ig, &groups, entry) {
157 if (strcmp(ig->name, group) == 0)
158 break;
159 }
160 if (ig == NULL) {
161 if (!creategroups) {
162 icb_error(is, "Invalid group %s", group);
163 icb_drop(is, NULL);
164 return;
165 } else {
166 if ((ig = icb_addgroup(is, group, NULL)) == NULL) {
167 icb_error(is, "Can't create group %s", group);
168 return;
169 }
170 icb_log(NULL, LOG_DEBUG, "%s created group %s",
171 nick, group);
172 }
173 }
174 LIST_FOREACH(s, &ig->sess, entry) {
175 if (strcmp(s->nick, nick) == 0) {
176 icb_error(is, "Nick is already in use");
177 icb_drop(is, NULL);
178 return;
179 }
180 }
181
182 if (client && strlen(client) > 0)
183 strlcpy(is->client, client, sizeof is->client);
184 strlcpy(is->nick, nick, sizeof is->nick);
185 is->group = ig;
186 is->login = time(NULL);
187 is->last = getmonotime();
188
189 /* notify group */
190 icb_status_group(ig, NULL, STATUS_SIGNON, "%s (%s@%s) entered group",
191 is->nick, is->client, is->host);
192
193 CLRF(is->flags, ICB_SF_PROTOSENT);
194 SETF(is->flags, ICB_SF_LOGGEDIN);
195
196 LIST_INSERT_HEAD(&ig->sess, is, entry);
197
198 /* acknowledge successful login */
199 icb_sendfmt(is, "%c", ICB_M_LOGIN);
200
201 /* notify user */
202 icb_status(is, STATUS_STATUS, "You are now in group %s%s", ig->name,
203 icb_ismod(ig, is) ? " as moderator" : "");
204
205 /* send user a topic name */
206 if (strlen(ig->topic) > 0)
207 icb_status(is, STATUS_TOPIC, "Topic for %s is \"%s\"",
208 ig->name, ig->topic);
209}
210
211/*
212 * icb_groupmsg: handles open message ('b') packets
213 */
214void
215icb_groupmsg(struct icb_session *is, char *msg)
216{
217 char buf[ICB_MSGSIZE];
218 struct icb_group *ig = is->group;
219 struct icb_session *s;
220 int buflen = 1;
221
222 if (strlen(msg) == 0) {
223 icb_error(is, "Empty message");
224 return;
225 }
226
227 buflen += snprintf(&buf[1], sizeof buf - 1, "%c%s%c%s", ICB_M_OPEN,
228 is->nick, ICB_M_SEP, msg);
229 buf[0] = buflen;
230
231 LIST_FOREACH(s, &ig->sess, entry) {
232 if (s == is)
233 continue;
234 icb_send(s, buf, buflen + 1);
235 }
236}
237
238/*
239 * icb_privmsg: handles personal message ('c') packets
240 */
241void
242icb_privmsg(struct icb_session *is, char *whom, char *msg)
243{
244 struct icb_group *ig = is->group;
245 struct icb_session *s;
246
247 LIST_FOREACH(s, &ig->sess, entry) {
248 if (strcmp(s->nick, whom) == 0)
249 break;
250 }
251 if (!s) {
252 icb_error(is, "No such user %s", whom);
253 return;
254 }
255 icb_sendfmt(s, "%c%s%c%s", ICB_M_PERSONAL, is->nick, ICB_M_SEP, msg);
256}
257
258/*
259 * icb_command: handles command ('h') packets
260 */
261void
262icb_command(struct icb_session *is, char *cmd, char *arg)
263{
264 void (*handler)(struct icb_session *, char *);
265
266 if ((handler = icb_cmd_lookup(cmd)) == NULL) {
267 icb_error(is, "Unsupported command: %s", cmd);
268 return;
269 }
270 handler(is, arg);
271}
272
273/*
274 * icb_cmdout: sends out command output ('i') packets, called from the
275 * command handlers
276 */
277void
278icb_cmdout(struct icb_session *is, int type, char *outmsg)
279{
280 char *otype = NULL;
281
282 switch (type) {
283 case CMDOUT_CO:
284 otype = "co";
285 break;
286 case CMDOUT_EC:
287 otype = "ec";
288 break;
289 case CMDOUT_WG:
290 otype = "wg";
291 break;
292 case CMDOUT_WH:
293 otype = "wh";
294 break;
295 case CMDOUT_WL:
296 otype = "wl";
297 break;
298 default:
299 icb_log(is, LOG_ERR, "unknown cmdout type");
300 return;
301 }
302 if (outmsg)
303 icb_sendfmt(is, "%c%s%c%s", ICB_M_CMDOUT, otype, ICB_M_SEP,
304 outmsg);
305 else
306 icb_sendfmt(is, "%c%s", ICB_M_CMDOUT, otype);
307}
308
309/*
310 * icb_status: sends a status message ('d') to the client
311 */
312void
313icb_status(struct icb_session *is, int type, const char *fmt, ...)
314{
315 va_list ap;
316 char buf[ICB_MSGSIZE];
317 int buflen = 1;
318 static const struct {
319 int type;
320 const char *msg;
321 } msgtab[] = {
322 { STATUS_ARRIVE, "Arrive" },
323 { STATUS_BOOT, "Boot" },
324 { STATUS_DEPART, "Depart" },
325 { STATUS_HELP, "Help" },
326 { STATUS_NAME, "Name" },
327 { STATUS_NOBEEP, "No-Beep" },
328 { STATUS_NOTIFY, "Notify" },
329 { STATUS_SIGNON, "Sign-on" },
330 { STATUS_SIGNOFF, "Sign-off" },
331 { STATUS_STATUS, "Status" },
332 { STATUS_TOPIC, "Topic" },
333 { STATUS_WARNING, "Warning" },
334 { 0, NULL }
335 };
336
337 if (type < 0 || type > (int)nitems(msgtab) - 1)
338 return;
339 va_start(ap, fmt);
340 buflen += snprintf(&buf[1], sizeof buf - 1, "%c%s%c", ICB_M_STATUS,
341 msgtab[type].msg, ICB_M_SEP);
342 buflen += vsnprintf(&buf[buflen], sizeof buf - buflen, fmt, ap);
343 buf[0] = buflen;
344 va_end(ap);
345 icb_send(is, buf, buflen + 1);
346}
347
348/*
349 * icb_status: sends a status message ('d') to the group except of the
350 * "ex" if it's not NULL
351 */
352void
353icb_status_group(struct icb_group *ig, struct icb_session *ex, int type,
354 const char *fmt, ...)
355{
356 char buf[ICB_MSGSIZE];
357 va_list ap;
358 struct icb_session *s;
359
360 va_start(ap, fmt);
361 (void)vsnprintf(buf, sizeof buf, fmt, ap);
362 LIST_FOREACH(s, &ig->sess, entry) {
363 if (ex && s == ex)
364 continue;
365 icb_status(s, type, buf);
366 }
367 icb_log(NULL, LOG_DEBUG, "%s", buf);
368 va_end(ap);
369}
370
371/*
372 * icb_error: sends an error message ('e') to the client
373 */
374void
375icb_error(struct icb_session *is, const char *fmt, ...)
376{
377 char buf[ICB_MSGSIZE];
378 va_list ap;
379 int buflen = 1;
380
381 va_start(ap, fmt);
382 buflen += vsnprintf(&buf[2], sizeof buf - 2, fmt, ap);
383 va_end(ap);
384 buf[0] = ++buflen; /* account for ICB_M_ERROR */
385 buf[1] = ICB_M_ERROR;
386 icb_send(is, buf, buflen + 1);
387 icb_log(is, LOG_DEBUG, "%s", buf + 2);
388}
389
390/*
391 * icb_remove: removes a session from the associated group
392 */
393void
394icb_remove(struct icb_session *is, char *reason)
395{
396 if (is->group) {
397 if (icb_ismod(is->group, is))
398 (void)icb_pass(is->group, is, NULL);
399 LIST_REMOVE(is, entry);
400 if (reason)
401 icb_status_group(is->group, NULL, STATUS_SIGNOFF,
402 "%s (%s@%s) just left: %s", is->nick, is->client,
403 is->host, reason);
404 else
405 icb_status_group(is->group, NULL, STATUS_SIGNOFF,
406 "%s (%s@%s) just left", is->nick, is->client,
407 is->host);
408 }
409}
410
411/*
412 * icb_addgroup: adds a new group to the list
413 */
414struct icb_group *
415icb_addgroup(struct icb_session *is, char *name, char *mpass)
416{
417 struct icb_group *ig;
418
419 if ((ig = calloc(1, sizeof *ig)) == NULL)
420 return (NULL);
421 strlcpy(ig->name, name, sizeof ig->name);
422 if (mpass)
423 strlcpy(ig->mpass, mpass, sizeof ig->mpass);
424 if (is)
425 ig->mod = is;
426 LIST_INIT(&ig->sess);
427 LIST_INSERT_HEAD(&groups, ig, entry);
428 return (ig);
429}
430
431#ifdef notused
432/*
433 * icb_delgroup: removes a group from the list
434 */
435void
436icb_delgroup(struct icb_group *ig)
437{
438 struct icb_session *s;
439
440 /* well, i guess we should kick out participants! ;-) */
441 LIST_FOREACH(s, &ig->sess, entry) {
442 icb_status(s, STATUS_WARNING, "Group dismissed");
443 s->group = NULL;
444 }
445 LIST_REMOVE(ig, entry);
446 bzero(ig, sizeof ig); /* paranoic thing, obviously */
447 free(ig);
448}
449#endif
450
451/*
452 * icb_dowho: a helper function that sends out a group header as a command
453 * output and user information in the "wl" format
454 */
455int
456icb_dowho(struct icb_session *is, struct icb_group *ig)
457{
458 char buf[ICB_MSGSIZE];
459 struct icb_session *s;
460 int nusers = 0;
461
462 icb_cmdout(is, CMDOUT_CO, " ");
463 snprintf(buf, sizeof buf, "Group: %-8s (%cvl) Mod: %-13s Topic: %s",
464 ig->name, ig->mod ? 'm' : 'p', ig->mod ? ig->mod->nick : "(None)",
465 strlen(ig->topic) > 0 ? ig->topic : "(None)");
466 icb_cmdout(is, CMDOUT_CO, buf);
467 LIST_FOREACH(s, &ig->sess, entry) {
468 (void)snprintf(buf, sizeof buf,
469 "%c%c%s%c%lld%c0%c%lld%c%s%c%s%c%s",
470 icb_ismod(ig, s) ? 'm' : ' ', ICB_M_SEP,
471 s->nick, ICB_M_SEP, getmonotime() - s->last,
472 ICB_M_SEP, ICB_M_SEP, s->login, ICB_M_SEP,
473 s->client, ICB_M_SEP, s->host, ICB_M_SEP, " ");
474 icb_cmdout(is, CMDOUT_WL, buf);
475 nusers++;
476 }
477 return (nusers);
478}
479
480/*
481 * icb_who: sends a list of users of either the specified group or all
482 * groups found on the server
483 */
484void
485icb_who(struct icb_session *is, struct icb_group *ig)
486{
487 char buf[ICB_MSGSIZE];
488 struct icb_group *g;
489
490 if (!ig) {
491 int nusers = 0, ngroups = 0;
492
493 LIST_FOREACH(g, &groups, entry) {
494 nusers += icb_dowho(is, g);
495 ngroups++;
496 }
497 if (nusers > 0) {
498 (void)snprintf(buf, sizeof buf,
499 "Total: %d %s in %d %s",
500 nusers, nusers > 1 ? "users" : "user",
501 ngroups, ngroups > 1 ? "groups" : "group");
502 } else
503 (void)snprintf(buf, sizeof buf, "No users found.");
504 icb_cmdout(is, CMDOUT_CO, buf);
505 } else
506 (void)icb_dowho(is, ig);
507}
508
509/*
510 * icb_ismod: checks whether group is moderated by "is"
511 */
512int
513icb_ismod(struct icb_group *ig, struct icb_session *is)
514{
515 if (ig->mod && ig->mod == is)
516 return (1);
517 return (0);
518}
519
520/*
521 * icb_modpermit: checks user against the moderators table if it has
522 * been populated
523 */
524int
525icb_modpermit(struct icb_session *is)
526{
527 extern char modtab[ICB_MTABLEN][ICB_MAXNICKLEN];
528 extern int modtabcnt;
529
530 if (modtabcnt == 0 ||
531 bsearch(is->nick, modtab, modtabcnt, ICB_MAXNICKLEN,
532 (int (*)(const void *, const void *))strcmp))
533 return (1);
534 return (0);
535}
536
537/*
538 * icb_pass: passes moderation of group "ig" from "from" to "to",
539 * returns -1 if "from" is not a moderator, 1 if passed
540 * to "to" and 0 otherwise (no moderator or passed to the
541 * internal bot)
542 */
543int
544icb_pass(struct icb_group *ig, struct icb_session *from,
545 struct icb_session *to)
546{
547 if (ig->mod && ig->mod != from)
548 return (-1);
549 if (!from && !to)
550 return (-1);
551 if (to && !icb_modpermit(to))
552 return (-1);
553 ig->mod = to;
554 if (to)
555 icb_status(to, STATUS_NOTIFY, "%s just passed you moderation"
556 " of %s", from ? from->nick : "server", ig->name);
557 icb_status_group(ig, to, STATUS_NOTIFY, "%s has passed moderation "
558 "to %s", from ? from->nick : "server", to ? to->nick : "server");
559 return (1);
560}
561
562/*
563 * icb_nextfield: advances through a given buffer returning pointer to the
564 * beginning of the icb field or an empty string otherwise;
565 * cleans up trailing spaces
566 */
567char *
568icb_nextfield(char **buf)
569{
570 char *start = *buf;
571 char *end = NULL;
572
573 while (*buf && **buf != '\0' && **buf != ICB_M_SEP)
574 (*buf)++;
575 if (*buf && **buf == ICB_M_SEP) {
576 **buf = '\0';
577 end = *buf;
578 (*buf)++;
579 } else
580 end = *buf;
581 while (end && *(--end) == ' ' && end > start)
582 *end = '\0';
583 return (start);
584}
585
586/*
587 * icb_sendfmt: formats a string and sends it over
588 */
589void
590icb_sendfmt(struct icb_session *is, const char *fmt, ...)
591{
592 char buf[ICB_MSGSIZE];
593 va_list ap;
594 int buflen = 1;
595
596 va_start(ap, fmt);
597 buflen += vsnprintf(&buf[1], sizeof buf - 1, fmt, ap);
598 va_end(ap);
599 buf[0] = buflen;
600 icb_send(is, buf, buflen + 1);
601}
Note: See TracBrowser for help on using the repository browser.