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>
|
---|
18 | #include <sys/socket.h>
|
---|
19 |
|
---|
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 | */
|
---|
26 | #define ICB_MSGSIZE 256
|
---|
27 |
|
---|
28 | #define ICB_MAXGRPLEN 32
|
---|
29 | #define ICB_MAXNICKLEN 32
|
---|
30 | #define ICB_MAXTOPICLEN 160
|
---|
31 | #define ICB_MAXHOSTLEN 40
|
---|
32 | #define ICB_MTABLEN 50 /* XXX */
|
---|
33 |
|
---|
34 | #define ICB_M_LOGIN 'a'
|
---|
35 | #define ICB_M_OPEN 'b'
|
---|
36 | #define ICB_M_PERSONAL 'c'
|
---|
37 | #define ICB_M_STATUS 'd'
|
---|
38 | enum {
|
---|
39 | STATUS_ARRIVE,
|
---|
40 | STATUS_BOOT,
|
---|
41 | STATUS_DEPART,
|
---|
42 | STATUS_HELP,
|
---|
43 | STATUS_NAME,
|
---|
44 | STATUS_NOBEEP,
|
---|
45 | STATUS_NOTIFY,
|
---|
46 | STATUS_SIGNON,
|
---|
47 | STATUS_SIGNOFF,
|
---|
48 | STATUS_STATUS,
|
---|
49 | STATUS_TOPIC,
|
---|
50 | STATUS_WARNING
|
---|
51 | };
|
---|
52 | #define ICB_M_ERROR 'e'
|
---|
53 | #define ICB_M_IMPORTANT 'f'
|
---|
54 | #define ICB_M_EXIT 'g'
|
---|
55 | #define ICB_M_COMMAND 'h'
|
---|
56 | #define ICB_M_CMDOUT 'i'
|
---|
57 | enum {
|
---|
58 | CMDOUT_CO,
|
---|
59 | CMDOUT_EC,
|
---|
60 | CMDOUT_WG,
|
---|
61 | CMDOUT_WH,
|
---|
62 | CMDOUT_WL,
|
---|
63 | };
|
---|
64 | #define ICB_M_PROTO 'j'
|
---|
65 | #define ICB_M_BEEP 'k'
|
---|
66 | #define ICB_M_PING 'l'
|
---|
67 | #define ICB_M_PONG 'm'
|
---|
68 | #define ICB_M_NOOP 'n'
|
---|
69 |
|
---|
70 | #define ICB_M_SEP '\001'
|
---|
71 |
|
---|
72 | struct icb_group;
|
---|
73 |
|
---|
74 | struct icb_session {
|
---|
75 | char nick[ICB_MAXNICKLEN];
|
---|
76 | char client[ICB_MAXNICKLEN];
|
---|
77 | char host[ICB_MAXHOSTLEN];
|
---|
78 | char hostname[NI_MAXHOST];
|
---|
79 | char buffer[ICB_MSGSIZE];
|
---|
80 | struct sockaddr_storage ss;
|
---|
81 | struct event ev;
|
---|
82 | struct bufferevent *bev;
|
---|
83 | struct icb_group *group;
|
---|
84 | size_t length;
|
---|
85 | size_t rlen;
|
---|
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 0x0001
|
---|
94 | #define ICB_SF_LOGGEDIN 0x0002
|
---|
95 | #define ICB_SF_NOGROUP 0x0008
|
---|
96 | #define ICB_SF_NOBEEP 0x0010
|
---|
97 | #define ICB_SF_NOBEEP2 0x0020
|
---|
98 |
|
---|
99 | #define ICB_SF_DNSINPROGRESS 0x1000
|
---|
100 | #define ICB_SF_PENDINGDROP 0x2000
|
---|
101 |
|
---|
102 | /* in-group linkage */
|
---|
103 | LIST_ENTRY(icb_session) entry;
|
---|
104 | };
|
---|
105 |
|
---|
106 | struct icb_group {
|
---|
107 | char name[ICB_MAXGRPLEN];
|
---|
108 | char topic[ICB_MAXTOPICLEN];
|
---|
109 | LIST_ENTRY(icb_group) entry;
|
---|
110 | LIST_HEAD(, icb_session) sess;
|
---|
111 | struct icb_session *mod;
|
---|
112 | };
|
---|
113 |
|
---|
114 | LIST_HEAD(icb_grlist, icb_group) groups;
|
---|
115 |
|
---|
116 | #ifndef nitems
|
---|
117 | #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
|
---|
118 | #endif
|
---|
119 |
|
---|
120 | /* cmd.c */
|
---|
121 | void * icb_cmd_lookup(char *);
|
---|
122 |
|
---|
123 | /* icb.c */
|
---|
124 | struct icb_group *
|
---|
125 | icb_addgroup(struct icb_session *, char *);
|
---|
126 | void icb_cmdout(struct icb_session *, int, char *);
|
---|
127 | void icb_delgroup(struct icb_group *);
|
---|
128 | void icb_error(struct icb_session *, const char *, ...);
|
---|
129 | void icb_init(void);
|
---|
130 | int icb_input(struct icb_session *);
|
---|
131 | inline int icb_ismod(struct icb_group *, struct icb_session *);
|
---|
132 | int icb_modpermit(struct icb_session *, int);
|
---|
133 | int icb_pass(struct icb_group *, struct icb_session *,
|
---|
134 | struct icb_session *);
|
---|
135 | void icb_privmsg(struct icb_session *, char *, char *);
|
---|
136 | void icb_remove(struct icb_session *, char *);
|
---|
137 | void icb_sendfmt(struct icb_session *, const char *, ...);
|
---|
138 | void icb_start(struct icb_session *);
|
---|
139 | void icb_status(struct icb_session *, int, const char *, ...);
|
---|
140 | void icb_status_group(struct icb_group *, struct icb_session *,
|
---|
141 | int, const char *, ...);
|
---|
142 | void icb_who(struct icb_session *, struct icb_group *);
|
---|
143 | int icb_token(char *, int, char **, char *, int, int);
|
---|
144 | int icb_vis(char *, const char *, size_t, int);
|
---|