[cd7b81d] | 1 | /*
|
---|
| 2 | * Copyright (c) 2009 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/queue.h>
|
---|
[fa271b8] | 18 | #include <sys/tree.h>
|
---|
[cd7b81d] | 19 |
|
---|
[a2fadb4] | 20 | /*
|
---|
| 21 | * ICB packet has the following format: <length><type><data>
|
---|
| 22 | * <lenght> is one byte length of the packet excluding the <lenght> byte;
|
---|
| 23 | * <type> is one byte type of the packet;
|
---|
| 24 | * <data> might not be null-terminated.
|
---|
| 25 | */
|
---|
[cd7b81d] | 26 | #define ICB_MSGSIZE 256
|
---|
| 27 |
|
---|
| 28 | #define ICB_MAXGRPLEN 32
|
---|
| 29 | #define ICB_MAXNICKLEN 32
|
---|
| 30 | #define ICB_MAXPASSLEN 32
|
---|
| 31 | #define ICB_MAXTOPICLEN 160
|
---|
[7289823] | 32 | #define ICB_MAXHOSTLEN 40
|
---|
[1d2125a] | 33 | #define ICB_MTABLEN 50 /* XXX */
|
---|
[cd7b81d] | 34 |
|
---|
| 35 | #define ICB_M_LOGIN 'a'
|
---|
| 36 | #define ICB_M_OPEN 'b'
|
---|
| 37 | #define ICB_M_PERSONAL 'c'
|
---|
| 38 | #define ICB_M_STATUS 'd'
|
---|
| 39 | enum {
|
---|
| 40 | STATUS_ARRIVE,
|
---|
| 41 | STATUS_BOOT,
|
---|
| 42 | STATUS_DEPART,
|
---|
[9195a6a] | 43 | STATUS_HELP,
|
---|
[cd7b81d] | 44 | STATUS_NAME,
|
---|
[bf02a60] | 45 | STATUS_NOBEEP,
|
---|
[cd7b81d] | 46 | STATUS_NOTIFY,
|
---|
| 47 | STATUS_SIGNON,
|
---|
| 48 | STATUS_SIGNOFF,
|
---|
| 49 | STATUS_STATUS,
|
---|
| 50 | STATUS_TOPIC,
|
---|
| 51 | STATUS_WARNING
|
---|
| 52 | };
|
---|
| 53 | #define ICB_M_ERROR 'e'
|
---|
| 54 | #define ICB_M_IMPORTANT 'f'
|
---|
| 55 | #define ICB_M_EXIT 'g'
|
---|
| 56 | #define ICB_M_COMMAND 'h'
|
---|
| 57 | #define ICB_M_CMDOUT 'i'
|
---|
| 58 | enum {
|
---|
| 59 | CMDOUT_CO,
|
---|
| 60 | CMDOUT_EC,
|
---|
| 61 | CMDOUT_WG,
|
---|
[4284008] | 62 | CMDOUT_WH,
|
---|
| 63 | CMDOUT_WL,
|
---|
[cd7b81d] | 64 | };
|
---|
| 65 | #define ICB_M_PROTO 'j'
|
---|
| 66 | #define ICB_M_BEEP 'k'
|
---|
| 67 | #define ICB_M_PING 'l'
|
---|
| 68 | #define ICB_M_PONG 'm'
|
---|
| 69 | #define ICB_M_NOOP 'n'
|
---|
| 70 |
|
---|
| 71 | #define ICB_M_SEP '\001'
|
---|
| 72 |
|
---|
| 73 | struct icb_group;
|
---|
| 74 |
|
---|
| 75 | struct icb_session {
|
---|
[fa271b8] | 76 | uint64_t id;
|
---|
[cd7b81d] | 77 | char nick[ICB_MAXNICKLEN];
|
---|
| 78 | char client[ICB_MAXNICKLEN];
|
---|
[7289823] | 79 | char host[ICB_MAXHOSTLEN];
|
---|
[a2fadb4] | 80 | char buffer[ICB_MSGSIZE];
|
---|
[cd7b81d] | 81 | struct event ev;
|
---|
| 82 | struct bufferevent *bev;
|
---|
| 83 | struct icb_group *group;
|
---|
| 84 | size_t length;
|
---|
[c9402c3] | 85 | size_t rlen;
|
---|
[cd7b81d] | 86 | time_t login;
|
---|
| 87 | time_t last;
|
---|
| 88 | int port;
|
---|
| 89 | uint32_t flags;
|
---|
| 90 | #define SETF(t, f) ((t) |= (f))
|
---|
| 91 | #define CLRF(t, f) ((t) &= ~(f))
|
---|
| 92 | #define ISSETF(t, f) ((t) & (f))
|
---|
| 93 | #define ICB_SF_PROTOSENT 0x01
|
---|
| 94 | #define ICB_SF_LOGGEDIN 0x02
|
---|
| 95 | #define ICB_SF_NOGROUP 0x08
|
---|
[bf02a60] | 96 | #define ICB_SF_NOBEEP 0x10
|
---|
| 97 | #define ICB_SF_NOBEEP2 0x20
|
---|
[fa271b8] | 98 |
|
---|
| 99 | /* session tree */
|
---|
| 100 | RB_ENTRY(icb_session) node;
|
---|
| 101 |
|
---|
| 102 | /* in-group linkage */
|
---|
| 103 | LIST_ENTRY(icb_session) entry;
|
---|
[cd7b81d] | 104 | };
|
---|
| 105 |
|
---|
| 106 | struct icb_group {
|
---|
| 107 | char name[ICB_MAXGRPLEN];
|
---|
| 108 | char mpass[ICB_MAXPASSLEN];
|
---|
| 109 | char topic[ICB_MAXTOPICLEN];
|
---|
| 110 | LIST_ENTRY(icb_group) entry;
|
---|
| 111 | LIST_HEAD(, icb_session) sess;
|
---|
[4284008] | 112 | struct icb_session *mod;
|
---|
[cd7b81d] | 113 | };
|
---|
| 114 |
|
---|
| 115 | LIST_HEAD(icb_grlist, icb_group) groups;
|
---|
| 116 |
|
---|
| 117 | struct icbd_callbacks {
|
---|
| 118 | void (*drop)(struct icb_session *, char *);
|
---|
| 119 | void (*log)(struct icb_session *, int, const char *, ...);
|
---|
| 120 | void (*send)(struct icb_session *, char *, ssize_t);
|
---|
| 121 | };
|
---|
| 122 |
|
---|
| 123 | #ifndef nitems
|
---|
| 124 | #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
|
---|
| 125 | #endif
|
---|
| 126 |
|
---|
| 127 | /* cmd.c */
|
---|
| 128 | void *icb_cmd_lookup(char *);
|
---|
| 129 |
|
---|
| 130 | /* icb.c */
|
---|
| 131 | struct icb_group *icb_addgroup(struct icb_session *, char *, char *);
|
---|
[626f420] | 132 | void icb_cmdout(struct icb_session *, int, char *);
|
---|
| 133 | void icb_delgroup(struct icb_group *);
|
---|
| 134 | void icb_error(struct icb_session *, const char *, ...);
|
---|
| 135 | void icb_init(struct icbd_callbacks *);
|
---|
| 136 | void icb_input(struct icb_session *);
|
---|
| 137 | inline int icb_ismod(struct icb_group *, struct icb_session *);
|
---|
[f3c60e6] | 138 | int icb_modpermit(struct icb_session *, int);
|
---|
[626f420] | 139 | int icb_pass(struct icb_group *, struct icb_session *,
|
---|
| 140 | struct icb_session *);
|
---|
| 141 | void icb_privmsg(struct icb_session *, char *, char *);
|
---|
| 142 | void icb_remove(struct icb_session *, char *);
|
---|
| 143 | void icb_sendfmt(struct icb_session *, const char *, ...);
|
---|
| 144 | void icb_start(struct icb_session *);
|
---|
| 145 | void icb_status(struct icb_session *, int, const char *, ...);
|
---|
| 146 | void icb_status_group(struct icb_group *, struct icb_session *,
|
---|
| 147 | int, const char *, ...);
|
---|
| 148 | void icb_who(struct icb_session *, struct icb_group *);
|
---|
[b7bc432] | 149 | int icb_vis(char *, const char *, size_t, int);
|
---|
[cd7b81d] | 150 |
|
---|
| 151 | /* callbacks from icbd.c */
|
---|
[626f420] | 152 | void (*icb_drop)(struct icb_session *, char *);
|
---|
| 153 | void (*icb_log)(struct icb_session *, int, const char *, ...);
|
---|
| 154 | void (*icb_send)(struct icb_session *, char *, ssize_t);
|
---|