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 |
|
---|
19 | #define ICB_MSGSIZE 256
|
---|
20 |
|
---|
21 | #define ICB_MAXGRPLEN 32
|
---|
22 | #define ICB_MAXNICKLEN 32
|
---|
23 | #define ICB_MAXPASSLEN 32
|
---|
24 | #define ICB_MAXTOPICLEN 160
|
---|
25 |
|
---|
26 | #define ICB_M_LOGIN 'a'
|
---|
27 | #define ICB_M_OPEN 'b'
|
---|
28 | #define ICB_M_PERSONAL 'c'
|
---|
29 | #define ICB_M_STATUS 'd'
|
---|
30 | enum {
|
---|
31 | STATUS_ARRIVE,
|
---|
32 | STATUS_BOOT,
|
---|
33 | STATUS_DEPART,
|
---|
34 | STATUS_HELP,
|
---|
35 | STATUS_NAME,
|
---|
36 | STATUS_NOBEEP,
|
---|
37 | STATUS_NOTIFY,
|
---|
38 | STATUS_SIGNON,
|
---|
39 | STATUS_SIGNOFF,
|
---|
40 | STATUS_STATUS,
|
---|
41 | STATUS_TOPIC,
|
---|
42 | STATUS_WARNING
|
---|
43 | };
|
---|
44 | #define ICB_M_ERROR 'e'
|
---|
45 | #define ICB_M_IMPORTANT 'f'
|
---|
46 | #define ICB_M_EXIT 'g'
|
---|
47 | #define ICB_M_COMMAND 'h'
|
---|
48 | #define ICB_M_CMDOUT 'i'
|
---|
49 | enum {
|
---|
50 | CMDOUT_CO,
|
---|
51 | CMDOUT_EC,
|
---|
52 | CMDOUT_WG,
|
---|
53 | CMDOUT_WH,
|
---|
54 | CMDOUT_WL,
|
---|
55 | };
|
---|
56 | #define ICB_M_PROTO 'j'
|
---|
57 | #define ICB_M_BEEP 'k'
|
---|
58 | #define ICB_M_PING 'l'
|
---|
59 | #define ICB_M_PONG 'm'
|
---|
60 | #define ICB_M_NOOP 'n'
|
---|
61 |
|
---|
62 | #define ICB_M_SEP '\001'
|
---|
63 |
|
---|
64 | struct icb_group;
|
---|
65 |
|
---|
66 | struct icb_session {
|
---|
67 | char nick[ICB_MAXNICKLEN];
|
---|
68 | char client[ICB_MAXNICKLEN];
|
---|
69 | char host[MAXHOSTNAMELEN];
|
---|
70 | char buffer[ICB_MSGSIZE+1];
|
---|
71 | struct event ev;
|
---|
72 | LIST_ENTRY(icb_session) entry;
|
---|
73 | struct bufferevent *bev;
|
---|
74 | struct icb_group *group;
|
---|
75 | size_t length;
|
---|
76 | time_t login;
|
---|
77 | time_t last;
|
---|
78 | int port;
|
---|
79 | uint32_t flags;
|
---|
80 | #define SETF(t, f) ((t) |= (f))
|
---|
81 | #define CLRF(t, f) ((t) &= ~(f))
|
---|
82 | #define ISSETF(t, f) ((t) & (f))
|
---|
83 | #define ICB_SF_PROTOSENT 0x01
|
---|
84 | #define ICB_SF_LOGGEDIN 0x02
|
---|
85 | #define ICB_SF_NOGROUP 0x08
|
---|
86 | #define ICB_SF_NOBEEP 0x10
|
---|
87 | #define ICB_SF_NOBEEP2 0x20
|
---|
88 | };
|
---|
89 |
|
---|
90 | struct icb_group {
|
---|
91 | char name[ICB_MAXGRPLEN];
|
---|
92 | char mpass[ICB_MAXPASSLEN];
|
---|
93 | char topic[ICB_MAXTOPICLEN];
|
---|
94 | LIST_ENTRY(icb_group) entry;
|
---|
95 | LIST_HEAD(, icb_session) sess;
|
---|
96 | struct icb_session *mod;
|
---|
97 | };
|
---|
98 |
|
---|
99 | LIST_HEAD(icb_grlist, icb_group) groups;
|
---|
100 |
|
---|
101 | struct icbd_callbacks {
|
---|
102 | void (*drop)(struct icb_session *, char *);
|
---|
103 | void (*log)(struct icb_session *, int, const char *, ...);
|
---|
104 | void (*send)(struct icb_session *, char *, ssize_t);
|
---|
105 | };
|
---|
106 |
|
---|
107 | #ifndef nitems
|
---|
108 | #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
|
---|
109 | #endif
|
---|
110 |
|
---|
111 | /* cmd.c */
|
---|
112 | void *icb_cmd_lookup(char *);
|
---|
113 |
|
---|
114 | /* icb.c */
|
---|
115 | struct icb_group *icb_addgroup(struct icb_session *, char *, char *);
|
---|
116 | void icb_cmdout(struct icb_session *, int, char *);
|
---|
117 | void icb_delgroup(struct icb_group *);
|
---|
118 | void icb_error(struct icb_session *, const char *, ...);
|
---|
119 | void icb_init(struct icbd_callbacks *);
|
---|
120 | void icb_input(struct icb_session *);
|
---|
121 | int icb_ismod(struct icb_group *, struct icb_session *);
|
---|
122 | int icb_pass(struct icb_group *, struct icb_session *, struct icb_session *);
|
---|
123 | void icb_privmsg(struct icb_session *, char *, char *);
|
---|
124 | void icb_remove(struct icb_session *, char *);
|
---|
125 | void icb_sendfmt(struct icb_session *, const char *, ...);
|
---|
126 | void icb_start(struct icb_session *);
|
---|
127 | void icb_status(struct icb_session *, int, const char *, ...);
|
---|
128 | void icb_status_group(struct icb_group *, struct icb_session *, int ,
|
---|
129 | const char *, ...);
|
---|
130 | void icb_who(struct icb_session *, struct icb_group *);
|
---|
131 |
|
---|
132 | /* callbacks from icbd.c */
|
---|
133 | void (*icb_drop)(struct icb_session *, char *);
|
---|
134 | void (*icb_log)(struct icb_session *, int, const char *, ...);
|
---|
135 | void (*icb_send)(struct icb_session *, char *, ssize_t);
|
---|