author | Tero Marttila <terom@fixme.fi> |
Tue, 10 Mar 2009 02:34:11 +0200 | |
changeset 28 | 9c1050bc8709 |
parent 27 | e6639132bead |
child 29 | 3f0f2898fea3 |
permissions | -rw-r--r-- |
18
dedf137b504f
add initial irc_conn code that can register
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
1 |
#include "irc_conn.h" |
21 | 2 |
#include "irc_cmd.h" |
22
c339c020fd33
add missing irc_cmd.h, and modify line_proto/irc_conn to use log
Tero Marttila <terom@fixme.fi>
parents:
21
diff
changeset
|
3 |
#include "log.h" |
18
dedf137b504f
add initial irc_conn code that can register
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
4 |
|
dedf137b504f
add initial irc_conn code that can register
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
5 |
#include <stdlib.h> |
dedf137b504f
add initial irc_conn code that can register
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
6 |
#include <string.h> |
dedf137b504f
add initial irc_conn code that can register
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
7 |
|
20
d9c4c2980a0d
irc_conn PING/PONG code, and line_proto fixups
Tero Marttila <terom@fixme.fi>
parents:
19
diff
changeset
|
8 |
/* |
21 | 9 |
* "Welcome to the Internet Relay Network <nick>!<user>@<host>" |
10 |
*/ |
|
23
542c73d07d3c
add a simple irc_log module (with evsql code) that joins a channel and log_info's PRIVMSGs
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
11 |
static void on_RPL_WELCOME (struct irc_conn *conn, const struct irc_line *line, void *arg) |
21 | 12 |
{ |
24
08a26d0b9afd
fix ret/err bug in irc_conn_send, supress unused-argument warnings
Tero Marttila <terom@fixme.fi>
parents:
23
diff
changeset
|
13 |
(void) line; |
08a26d0b9afd
fix ret/err bug in irc_conn_send, supress unused-argument warnings
Tero Marttila <terom@fixme.fi>
parents:
23
diff
changeset
|
14 |
(void) arg; |
08a26d0b9afd
fix ret/err bug in irc_conn_send, supress unused-argument warnings
Tero Marttila <terom@fixme.fi>
parents:
23
diff
changeset
|
15 |
|
21 | 16 |
// update state |
27
e6639132bead
add irc_conn_callbacks, and delay irc_chan_join until on_registered
Tero Marttila <terom@fixme.fi>
parents:
24
diff
changeset
|
17 |
conn->registering = false; |
21 | 18 |
conn->registered = true; |
22
c339c020fd33
add missing irc_cmd.h, and modify line_proto/irc_conn to use log
Tero Marttila <terom@fixme.fi>
parents:
21
diff
changeset
|
19 |
|
27
e6639132bead
add irc_conn_callbacks, and delay irc_chan_join until on_registered
Tero Marttila <terom@fixme.fi>
parents:
24
diff
changeset
|
20 |
// trigger callback |
e6639132bead
add irc_conn_callbacks, and delay irc_chan_join until on_registered
Tero Marttila <terom@fixme.fi>
parents:
24
diff
changeset
|
21 |
if (conn->callbacks.on_registered) |
e6639132bead
add irc_conn_callbacks, and delay irc_chan_join until on_registered
Tero Marttila <terom@fixme.fi>
parents:
24
diff
changeset
|
22 |
conn->callbacks.on_registered(conn, conn->cb_arg); |
21 | 23 |
} |
24 |
||
25 |
/* |
|
20
d9c4c2980a0d
irc_conn PING/PONG code, and line_proto fixups
Tero Marttila <terom@fixme.fi>
parents:
19
diff
changeset
|
26 |
* PING <server1> [ <server2> ] |
d9c4c2980a0d
irc_conn PING/PONG code, and line_proto fixups
Tero Marttila <terom@fixme.fi>
parents:
19
diff
changeset
|
27 |
* |
d9c4c2980a0d
irc_conn PING/PONG code, and line_proto fixups
Tero Marttila <terom@fixme.fi>
parents:
19
diff
changeset
|
28 |
* Send a 'PONG <server1>` reply right away. |
d9c4c2980a0d
irc_conn PING/PONG code, and line_proto fixups
Tero Marttila <terom@fixme.fi>
parents:
19
diff
changeset
|
29 |
*/ |
23
542c73d07d3c
add a simple irc_log module (with evsql code) that joins a channel and log_info's PRIVMSGs
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
30 |
static void on_PING (struct irc_conn *conn, const struct irc_line *line, void *arg) |
20
d9c4c2980a0d
irc_conn PING/PONG code, and line_proto fixups
Tero Marttila <terom@fixme.fi>
parents:
19
diff
changeset
|
31 |
{ |
24
08a26d0b9afd
fix ret/err bug in irc_conn_send, supress unused-argument warnings
Tero Marttila <terom@fixme.fi>
parents:
23
diff
changeset
|
32 |
(void) arg; |
08a26d0b9afd
fix ret/err bug in irc_conn_send, supress unused-argument warnings
Tero Marttila <terom@fixme.fi>
parents:
23
diff
changeset
|
33 |
|
20
d9c4c2980a0d
irc_conn PING/PONG code, and line_proto fixups
Tero Marttila <terom@fixme.fi>
parents:
19
diff
changeset
|
34 |
// just reply |
d9c4c2980a0d
irc_conn PING/PONG code, and line_proto fixups
Tero Marttila <terom@fixme.fi>
parents:
19
diff
changeset
|
35 |
irc_conn_PONG(conn, line->args[0]); |
d9c4c2980a0d
irc_conn PING/PONG code, and line_proto fixups
Tero Marttila <terom@fixme.fi>
parents:
19
diff
changeset
|
36 |
} |
d9c4c2980a0d
irc_conn PING/PONG code, and line_proto fixups
Tero Marttila <terom@fixme.fi>
parents:
19
diff
changeset
|
37 |
|
d9c4c2980a0d
irc_conn PING/PONG code, and line_proto fixups
Tero Marttila <terom@fixme.fi>
parents:
19
diff
changeset
|
38 |
/* |
d9c4c2980a0d
irc_conn PING/PONG code, and line_proto fixups
Tero Marttila <terom@fixme.fi>
parents:
19
diff
changeset
|
39 |
* Our command handlers |
d9c4c2980a0d
irc_conn PING/PONG code, and line_proto fixups
Tero Marttila <terom@fixme.fi>
parents:
19
diff
changeset
|
40 |
*/ |
23
542c73d07d3c
add a simple irc_log module (with evsql code) that joins a channel and log_info's PRIVMSGs
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
41 |
struct irc_cmd_handler _cmd_handlers[] = { |
21 | 42 |
{ IRC_RPL_WELCOME, on_RPL_WELCOME }, |
20
d9c4c2980a0d
irc_conn PING/PONG code, and line_proto fixups
Tero Marttila <terom@fixme.fi>
parents:
19
diff
changeset
|
43 |
{ "PING", on_PING }, |
d9c4c2980a0d
irc_conn PING/PONG code, and line_proto fixups
Tero Marttila <terom@fixme.fi>
parents:
19
diff
changeset
|
44 |
{ NULL, NULL, }, |
d9c4c2980a0d
irc_conn PING/PONG code, and line_proto fixups
Tero Marttila <terom@fixme.fi>
parents:
19
diff
changeset
|
45 |
}; |
d9c4c2980a0d
irc_conn PING/PONG code, and line_proto fixups
Tero Marttila <terom@fixme.fi>
parents:
19
diff
changeset
|
46 |
|
23
542c73d07d3c
add a simple irc_log module (with evsql code) that joins a channel and log_info's PRIVMSGs
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
47 |
/* |
542c73d07d3c
add a simple irc_log module (with evsql code) that joins a channel and log_info's PRIVMSGs
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
48 |
* Incoming line handler |
542c73d07d3c
add a simple irc_log module (with evsql code) that joins a channel and log_info's PRIVMSGs
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
49 |
*/ |
18
dedf137b504f
add initial irc_conn code that can register
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
50 |
void irc_conn_on_line (char *line_buf, void *arg) |
dedf137b504f
add initial irc_conn code that can register
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
51 |
{ |
20
d9c4c2980a0d
irc_conn PING/PONG code, and line_proto fixups
Tero Marttila <terom@fixme.fi>
parents:
19
diff
changeset
|
52 |
struct irc_conn *conn = arg; |
18
dedf137b504f
add initial irc_conn code that can register
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
53 |
struct irc_line line; |
23
542c73d07d3c
add a simple irc_log module (with evsql code) that joins a channel and log_info's PRIVMSGs
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
54 |
struct irc_cmd_chain *chain; |
20
d9c4c2980a0d
irc_conn PING/PONG code, and line_proto fixups
Tero Marttila <terom@fixme.fi>
parents:
19
diff
changeset
|
55 |
struct irc_cmd_handler *handler; |
18
dedf137b504f
add initial irc_conn code that can register
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
56 |
int err; |
dedf137b504f
add initial irc_conn code that can register
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
57 |
|
dedf137b504f
add initial irc_conn code that can register
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
58 |
// log |
22
c339c020fd33
add missing irc_cmd.h, and modify line_proto/irc_conn to use log
Tero Marttila <terom@fixme.fi>
parents:
21
diff
changeset
|
59 |
log_debug("%s", line_buf); |
18
dedf137b504f
add initial irc_conn code that can register
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
60 |
|
dedf137b504f
add initial irc_conn code that can register
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
61 |
// parse |
20
d9c4c2980a0d
irc_conn PING/PONG code, and line_proto fixups
Tero Marttila <terom@fixme.fi>
parents:
19
diff
changeset
|
62 |
if ((err = irc_line_parse(&line, line_buf))) { |
23
542c73d07d3c
add a simple irc_log module (with evsql code) that joins a channel and log_info's PRIVMSGs
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
63 |
log_warn("invalid line: %s: %s\n", line_buf, error_name(err)); |
20
d9c4c2980a0d
irc_conn PING/PONG code, and line_proto fixups
Tero Marttila <terom@fixme.fi>
parents:
19
diff
changeset
|
64 |
return; |
d9c4c2980a0d
irc_conn PING/PONG code, and line_proto fixups
Tero Marttila <terom@fixme.fi>
parents:
19
diff
changeset
|
65 |
} |
23
542c73d07d3c
add a simple irc_log module (with evsql code) that joins a channel and log_info's PRIVMSGs
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
66 |
|
542c73d07d3c
add a simple irc_log module (with evsql code) that joins a channel and log_info's PRIVMSGs
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
67 |
// run each handler chain |
542c73d07d3c
add a simple irc_log module (with evsql code) that joins a channel and log_info's PRIVMSGs
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
68 |
STAILQ_FOREACH(chain, &conn->handlers, node) { |
542c73d07d3c
add a simple irc_log module (with evsql code) that joins a channel and log_info's PRIVMSGs
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
69 |
// look up appropriate handler |
542c73d07d3c
add a simple irc_log module (with evsql code) that joins a channel and log_info's PRIVMSGs
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
70 |
for (handler = chain->handlers; handler->command; handler++) { |
542c73d07d3c
add a simple irc_log module (with evsql code) that joins a channel and log_info's PRIVMSGs
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
71 |
// the command is alpha-only, so normal case-insensitive cmp is fine |
542c73d07d3c
add a simple irc_log module (with evsql code) that joins a channel and log_info's PRIVMSGs
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
72 |
if (strcasecmp(handler->command, line.command) == 0) { |
542c73d07d3c
add a simple irc_log module (with evsql code) that joins a channel and log_info's PRIVMSGs
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
73 |
// invoke the func |
542c73d07d3c
add a simple irc_log module (with evsql code) that joins a channel and log_info's PRIVMSGs
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
74 |
handler->func(conn, &line, chain->arg); |
18
dedf137b504f
add initial irc_conn code that can register
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
75 |
|
23
542c73d07d3c
add a simple irc_log module (with evsql code) that joins a channel and log_info's PRIVMSGs
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
76 |
// ...only one per chain |
542c73d07d3c
add a simple irc_log module (with evsql code) that joins a channel and log_info's PRIVMSGs
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
77 |
break; |
542c73d07d3c
add a simple irc_log module (with evsql code) that joins a channel and log_info's PRIVMSGs
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
78 |
} |
20
d9c4c2980a0d
irc_conn PING/PONG code, and line_proto fixups
Tero Marttila <terom@fixme.fi>
parents:
19
diff
changeset
|
79 |
} |
d9c4c2980a0d
irc_conn PING/PONG code, and line_proto fixups
Tero Marttila <terom@fixme.fi>
parents:
19
diff
changeset
|
80 |
} |
18
dedf137b504f
add initial irc_conn code that can register
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
81 |
} |
dedf137b504f
add initial irc_conn code that can register
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
82 |
|
27
e6639132bead
add irc_conn_callbacks, and delay irc_chan_join until on_registered
Tero Marttila <terom@fixme.fi>
parents:
24
diff
changeset
|
83 |
err_t irc_conn_create (struct irc_conn **conn_ptr, struct sock_stream *sock, const struct irc_conn_callbacks *callbacks, |
e6639132bead
add irc_conn_callbacks, and delay irc_chan_join until on_registered
Tero Marttila <terom@fixme.fi>
parents:
24
diff
changeset
|
84 |
void *cb_arg, struct error_info *err) |
18
dedf137b504f
add initial irc_conn code that can register
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
85 |
{ |
dedf137b504f
add initial irc_conn code that can register
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
86 |
struct irc_conn *conn; |
dedf137b504f
add initial irc_conn code that can register
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
87 |
|
dedf137b504f
add initial irc_conn code that can register
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
88 |
// alloc new state struct |
dedf137b504f
add initial irc_conn code that can register
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
89 |
if ((conn = calloc(1, sizeof(struct irc_conn))) == NULL) |
dedf137b504f
add initial irc_conn code that can register
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
90 |
return SET_ERROR(err, ERR_CALLOC); |
dedf137b504f
add initial irc_conn code that can register
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
91 |
|
27
e6639132bead
add irc_conn_callbacks, and delay irc_chan_join until on_registered
Tero Marttila <terom@fixme.fi>
parents:
24
diff
changeset
|
92 |
// init state |
e6639132bead
add irc_conn_callbacks, and delay irc_chan_join until on_registered
Tero Marttila <terom@fixme.fi>
parents:
24
diff
changeset
|
93 |
conn->callbacks = *callbacks; |
e6639132bead
add irc_conn_callbacks, and delay irc_chan_join until on_registered
Tero Marttila <terom@fixme.fi>
parents:
24
diff
changeset
|
94 |
conn->cb_arg = cb_arg; |
e6639132bead
add irc_conn_callbacks, and delay irc_chan_join until on_registered
Tero Marttila <terom@fixme.fi>
parents:
24
diff
changeset
|
95 |
|
23
542c73d07d3c
add a simple irc_log module (with evsql code) that joins a channel and log_info's PRIVMSGs
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
96 |
// initialize command handlers |
542c73d07d3c
add a simple irc_log module (with evsql code) that joins a channel and log_info's PRIVMSGs
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
97 |
STAILQ_INIT(&conn->handlers); |
542c73d07d3c
add a simple irc_log module (with evsql code) that joins a channel and log_info's PRIVMSGs
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
98 |
|
542c73d07d3c
add a simple irc_log module (with evsql code) that joins a channel and log_info's PRIVMSGs
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
99 |
// add the core handlers |
542c73d07d3c
add a simple irc_log module (with evsql code) that joins a channel and log_info's PRIVMSGs
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
100 |
if ((ERROR_CODE(err) = irc_conn_register_handler_chain(conn, _cmd_handlers, NULL))) |
28
9c1050bc8709
add sock_stream_release/line_proto_release/irc_conn_release functions, and add proper cleanup to irc_net_create
Tero Marttila <terom@fixme.fi>
parents:
27
diff
changeset
|
101 |
goto error; |
23
542c73d07d3c
add a simple irc_log module (with evsql code) that joins a channel and log_info's PRIVMSGs
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
102 |
|
18
dedf137b504f
add initial irc_conn code that can register
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
103 |
// create the line_proto, with our on_line handler |
dedf137b504f
add initial irc_conn code that can register
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
104 |
if (line_proto_create(&conn->lp, sock, IRC_LINE_MAX * 1.5, &irc_conn_on_line, conn, err)) |
28
9c1050bc8709
add sock_stream_release/line_proto_release/irc_conn_release functions, and add proper cleanup to irc_net_create
Tero Marttila <terom@fixme.fi>
parents:
27
diff
changeset
|
105 |
goto error; |
18
dedf137b504f
add initial irc_conn code that can register
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
106 |
|
dedf137b504f
add initial irc_conn code that can register
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
107 |
// ok |
dedf137b504f
add initial irc_conn code that can register
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
108 |
*conn_ptr = conn; |
dedf137b504f
add initial irc_conn code that can register
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
109 |
|
dedf137b504f
add initial irc_conn code that can register
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
110 |
return SUCCESS; |
28
9c1050bc8709
add sock_stream_release/line_proto_release/irc_conn_release functions, and add proper cleanup to irc_net_create
Tero Marttila <terom@fixme.fi>
parents:
27
diff
changeset
|
111 |
|
9c1050bc8709
add sock_stream_release/line_proto_release/irc_conn_release functions, and add proper cleanup to irc_net_create
Tero Marttila <terom@fixme.fi>
parents:
27
diff
changeset
|
112 |
error: |
9c1050bc8709
add sock_stream_release/line_proto_release/irc_conn_release functions, and add proper cleanup to irc_net_create
Tero Marttila <terom@fixme.fi>
parents:
27
diff
changeset
|
113 |
// release |
9c1050bc8709
add sock_stream_release/line_proto_release/irc_conn_release functions, and add proper cleanup to irc_net_create
Tero Marttila <terom@fixme.fi>
parents:
27
diff
changeset
|
114 |
irc_conn_destroy(conn); |
9c1050bc8709
add sock_stream_release/line_proto_release/irc_conn_release functions, and add proper cleanup to irc_net_create
Tero Marttila <terom@fixme.fi>
parents:
27
diff
changeset
|
115 |
|
9c1050bc8709
add sock_stream_release/line_proto_release/irc_conn_release functions, and add proper cleanup to irc_net_create
Tero Marttila <terom@fixme.fi>
parents:
27
diff
changeset
|
116 |
return ERROR_CODE(err); |
9c1050bc8709
add sock_stream_release/line_proto_release/irc_conn_release functions, and add proper cleanup to irc_net_create
Tero Marttila <terom@fixme.fi>
parents:
27
diff
changeset
|
117 |
} |
9c1050bc8709
add sock_stream_release/line_proto_release/irc_conn_release functions, and add proper cleanup to irc_net_create
Tero Marttila <terom@fixme.fi>
parents:
27
diff
changeset
|
118 |
|
9c1050bc8709
add sock_stream_release/line_proto_release/irc_conn_release functions, and add proper cleanup to irc_net_create
Tero Marttila <terom@fixme.fi>
parents:
27
diff
changeset
|
119 |
void irc_conn_destroy (struct irc_conn *conn) |
9c1050bc8709
add sock_stream_release/line_proto_release/irc_conn_release functions, and add proper cleanup to irc_net_create
Tero Marttila <terom@fixme.fi>
parents:
27
diff
changeset
|
120 |
{ |
9c1050bc8709
add sock_stream_release/line_proto_release/irc_conn_release functions, and add proper cleanup to irc_net_create
Tero Marttila <terom@fixme.fi>
parents:
27
diff
changeset
|
121 |
struct irc_cmd_chain *next = STAILQ_FIRST(&conn->handlers); |
9c1050bc8709
add sock_stream_release/line_proto_release/irc_conn_release functions, and add proper cleanup to irc_net_create
Tero Marttila <terom@fixme.fi>
parents:
27
diff
changeset
|
122 |
|
9c1050bc8709
add sock_stream_release/line_proto_release/irc_conn_release functions, and add proper cleanup to irc_net_create
Tero Marttila <terom@fixme.fi>
parents:
27
diff
changeset
|
123 |
// release the line_proto |
9c1050bc8709
add sock_stream_release/line_proto_release/irc_conn_release functions, and add proper cleanup to irc_net_create
Tero Marttila <terom@fixme.fi>
parents:
27
diff
changeset
|
124 |
if (conn->lp) |
9c1050bc8709
add sock_stream_release/line_proto_release/irc_conn_release functions, and add proper cleanup to irc_net_create
Tero Marttila <terom@fixme.fi>
parents:
27
diff
changeset
|
125 |
line_proto_release(conn->lp); |
9c1050bc8709
add sock_stream_release/line_proto_release/irc_conn_release functions, and add proper cleanup to irc_net_create
Tero Marttila <terom@fixme.fi>
parents:
27
diff
changeset
|
126 |
|
9c1050bc8709
add sock_stream_release/line_proto_release/irc_conn_release functions, and add proper cleanup to irc_net_create
Tero Marttila <terom@fixme.fi>
parents:
27
diff
changeset
|
127 |
// clean up any handler chains |
9c1050bc8709
add sock_stream_release/line_proto_release/irc_conn_release functions, and add proper cleanup to irc_net_create
Tero Marttila <terom@fixme.fi>
parents:
27
diff
changeset
|
128 |
while (next) { |
9c1050bc8709
add sock_stream_release/line_proto_release/irc_conn_release functions, and add proper cleanup to irc_net_create
Tero Marttila <terom@fixme.fi>
parents:
27
diff
changeset
|
129 |
struct irc_cmd_chain *node = next; |
9c1050bc8709
add sock_stream_release/line_proto_release/irc_conn_release functions, and add proper cleanup to irc_net_create
Tero Marttila <terom@fixme.fi>
parents:
27
diff
changeset
|
130 |
|
9c1050bc8709
add sock_stream_release/line_proto_release/irc_conn_release functions, and add proper cleanup to irc_net_create
Tero Marttila <terom@fixme.fi>
parents:
27
diff
changeset
|
131 |
// update next |
9c1050bc8709
add sock_stream_release/line_proto_release/irc_conn_release functions, and add proper cleanup to irc_net_create
Tero Marttila <terom@fixme.fi>
parents:
27
diff
changeset
|
132 |
next = STAILQ_NEXT(node, node); |
9c1050bc8709
add sock_stream_release/line_proto_release/irc_conn_release functions, and add proper cleanup to irc_net_create
Tero Marttila <terom@fixme.fi>
parents:
27
diff
changeset
|
133 |
|
9c1050bc8709
add sock_stream_release/line_proto_release/irc_conn_release functions, and add proper cleanup to irc_net_create
Tero Marttila <terom@fixme.fi>
parents:
27
diff
changeset
|
134 |
// free |
9c1050bc8709
add sock_stream_release/line_proto_release/irc_conn_release functions, and add proper cleanup to irc_net_create
Tero Marttila <terom@fixme.fi>
parents:
27
diff
changeset
|
135 |
free(node); |
9c1050bc8709
add sock_stream_release/line_proto_release/irc_conn_release functions, and add proper cleanup to irc_net_create
Tero Marttila <terom@fixme.fi>
parents:
27
diff
changeset
|
136 |
} |
9c1050bc8709
add sock_stream_release/line_proto_release/irc_conn_release functions, and add proper cleanup to irc_net_create
Tero Marttila <terom@fixme.fi>
parents:
27
diff
changeset
|
137 |
|
9c1050bc8709
add sock_stream_release/line_proto_release/irc_conn_release functions, and add proper cleanup to irc_net_create
Tero Marttila <terom@fixme.fi>
parents:
27
diff
changeset
|
138 |
// free the irc_conn itself |
9c1050bc8709
add sock_stream_release/line_proto_release/irc_conn_release functions, and add proper cleanup to irc_net_create
Tero Marttila <terom@fixme.fi>
parents:
27
diff
changeset
|
139 |
free(conn); |
18
dedf137b504f
add initial irc_conn code that can register
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
140 |
} |
dedf137b504f
add initial irc_conn code that can register
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
141 |
|
23
542c73d07d3c
add a simple irc_log module (with evsql code) that joins a channel and log_info's PRIVMSGs
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
142 |
err_t irc_conn_register_handler_chain (struct irc_conn *conn, struct irc_cmd_handler *handlers, void *arg) |
542c73d07d3c
add a simple irc_log module (with evsql code) that joins a channel and log_info's PRIVMSGs
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
143 |
{ |
542c73d07d3c
add a simple irc_log module (with evsql code) that joins a channel and log_info's PRIVMSGs
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
144 |
struct irc_cmd_chain *item; |
542c73d07d3c
add a simple irc_log module (with evsql code) that joins a channel and log_info's PRIVMSGs
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
145 |
|
542c73d07d3c
add a simple irc_log module (with evsql code) that joins a channel and log_info's PRIVMSGs
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
146 |
// allocate the chain item |
542c73d07d3c
add a simple irc_log module (with evsql code) that joins a channel and log_info's PRIVMSGs
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
147 |
if ((item = calloc(1, sizeof(*item))) == NULL) |
542c73d07d3c
add a simple irc_log module (with evsql code) that joins a channel and log_info's PRIVMSGs
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
148 |
return ERR_CALLOC; |
542c73d07d3c
add a simple irc_log module (with evsql code) that joins a channel and log_info's PRIVMSGs
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
149 |
|
542c73d07d3c
add a simple irc_log module (with evsql code) that joins a channel and log_info's PRIVMSGs
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
150 |
// store |
542c73d07d3c
add a simple irc_log module (with evsql code) that joins a channel and log_info's PRIVMSGs
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
151 |
item->handlers = handlers; |
542c73d07d3c
add a simple irc_log module (with evsql code) that joins a channel and log_info's PRIVMSGs
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
152 |
item->arg = arg; |
542c73d07d3c
add a simple irc_log module (with evsql code) that joins a channel and log_info's PRIVMSGs
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
153 |
|
542c73d07d3c
add a simple irc_log module (with evsql code) that joins a channel and log_info's PRIVMSGs
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
154 |
// append |
542c73d07d3c
add a simple irc_log module (with evsql code) that joins a channel and log_info's PRIVMSGs
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
155 |
STAILQ_INSERT_TAIL(&conn->handlers, item, node); |
542c73d07d3c
add a simple irc_log module (with evsql code) that joins a channel and log_info's PRIVMSGs
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
156 |
|
542c73d07d3c
add a simple irc_log module (with evsql code) that joins a channel and log_info's PRIVMSGs
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
157 |
// ok |
542c73d07d3c
add a simple irc_log module (with evsql code) that joins a channel and log_info's PRIVMSGs
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
158 |
return SUCCESS; |
542c73d07d3c
add a simple irc_log module (with evsql code) that joins a channel and log_info's PRIVMSGs
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
159 |
} |
542c73d07d3c
add a simple irc_log module (with evsql code) that joins a channel and log_info's PRIVMSGs
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
160 |
|
27
e6639132bead
add irc_conn_callbacks, and delay irc_chan_join until on_registered
Tero Marttila <terom@fixme.fi>
parents:
24
diff
changeset
|
161 |
err_t irc_conn_register (struct irc_conn *conn, const struct irc_conn_register_info *info) |
e6639132bead
add irc_conn_callbacks, and delay irc_chan_join until on_registered
Tero Marttila <terom@fixme.fi>
parents:
24
diff
changeset
|
162 |
{ |
e6639132bead
add irc_conn_callbacks, and delay irc_chan_join until on_registered
Tero Marttila <terom@fixme.fi>
parents:
24
diff
changeset
|
163 |
err_t err; |
e6639132bead
add irc_conn_callbacks, and delay irc_chan_join until on_registered
Tero Marttila <terom@fixme.fi>
parents:
24
diff
changeset
|
164 |
|
e6639132bead
add irc_conn_callbacks, and delay irc_chan_join until on_registered
Tero Marttila <terom@fixme.fi>
parents:
24
diff
changeset
|
165 |
// assert state |
e6639132bead
add irc_conn_callbacks, and delay irc_chan_join until on_registered
Tero Marttila <terom@fixme.fi>
parents:
24
diff
changeset
|
166 |
if (conn->registering || conn->registered) |
e6639132bead
add irc_conn_callbacks, and delay irc_chan_join until on_registered
Tero Marttila <terom@fixme.fi>
parents:
24
diff
changeset
|
167 |
// XXX: stupid error code |
e6639132bead
add irc_conn_callbacks, and delay irc_chan_join until on_registered
Tero Marttila <terom@fixme.fi>
parents:
24
diff
changeset
|
168 |
return ERR_IRC_CONN_REGISTER_STATE; |
e6639132bead
add irc_conn_callbacks, and delay irc_chan_join until on_registered
Tero Marttila <terom@fixme.fi>
parents:
24
diff
changeset
|
169 |
|
e6639132bead
add irc_conn_callbacks, and delay irc_chan_join until on_registered
Tero Marttila <terom@fixme.fi>
parents:
24
diff
changeset
|
170 |
// send the initial messages |
e6639132bead
add irc_conn_callbacks, and delay irc_chan_join until on_registered
Tero Marttila <terom@fixme.fi>
parents:
24
diff
changeset
|
171 |
if ( |
e6639132bead
add irc_conn_callbacks, and delay irc_chan_join until on_registered
Tero Marttila <terom@fixme.fi>
parents:
24
diff
changeset
|
172 |
(err = irc_conn_NICK(conn, info->nickname)) |
e6639132bead
add irc_conn_callbacks, and delay irc_chan_join until on_registered
Tero Marttila <terom@fixme.fi>
parents:
24
diff
changeset
|
173 |
|| (err = irc_conn_USER(conn, info->username, info->realname)) |
e6639132bead
add irc_conn_callbacks, and delay irc_chan_join until on_registered
Tero Marttila <terom@fixme.fi>
parents:
24
diff
changeset
|
174 |
) |
e6639132bead
add irc_conn_callbacks, and delay irc_chan_join until on_registered
Tero Marttila <terom@fixme.fi>
parents:
24
diff
changeset
|
175 |
return err; |
e6639132bead
add irc_conn_callbacks, and delay irc_chan_join until on_registered
Tero Marttila <terom@fixme.fi>
parents:
24
diff
changeset
|
176 |
|
e6639132bead
add irc_conn_callbacks, and delay irc_chan_join until on_registered
Tero Marttila <terom@fixme.fi>
parents:
24
diff
changeset
|
177 |
// set state |
e6639132bead
add irc_conn_callbacks, and delay irc_chan_join until on_registered
Tero Marttila <terom@fixme.fi>
parents:
24
diff
changeset
|
178 |
conn->registering = true; |
e6639132bead
add irc_conn_callbacks, and delay irc_chan_join until on_registered
Tero Marttila <terom@fixme.fi>
parents:
24
diff
changeset
|
179 |
|
e6639132bead
add irc_conn_callbacks, and delay irc_chan_join until on_registered
Tero Marttila <terom@fixme.fi>
parents:
24
diff
changeset
|
180 |
// ok |
e6639132bead
add irc_conn_callbacks, and delay irc_chan_join until on_registered
Tero Marttila <terom@fixme.fi>
parents:
24
diff
changeset
|
181 |
return SUCCESS; |
e6639132bead
add irc_conn_callbacks, and delay irc_chan_join until on_registered
Tero Marttila <terom@fixme.fi>
parents:
24
diff
changeset
|
182 |
} |
e6639132bead
add irc_conn_callbacks, and delay irc_chan_join until on_registered
Tero Marttila <terom@fixme.fi>
parents:
24
diff
changeset
|
183 |
|
18
dedf137b504f
add initial irc_conn code that can register
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
184 |
err_t irc_conn_send (struct irc_conn *conn, const struct irc_line *line) |
dedf137b504f
add initial irc_conn code that can register
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
185 |
{ |
dedf137b504f
add initial irc_conn code that can register
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
186 |
char line_buf[IRC_LINE_MAX + 2]; |
dedf137b504f
add initial irc_conn code that can register
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
187 |
err_t err; |
24
08a26d0b9afd
fix ret/err bug in irc_conn_send, supress unused-argument warnings
Tero Marttila <terom@fixme.fi>
parents:
23
diff
changeset
|
188 |
int ret; |
18
dedf137b504f
add initial irc_conn code that can register
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
189 |
|
dedf137b504f
add initial irc_conn code that can register
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
190 |
// format |
dedf137b504f
add initial irc_conn code that can register
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
191 |
if ((err = irc_line_build(line, line_buf))) |
dedf137b504f
add initial irc_conn code that can register
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
192 |
return err; |
dedf137b504f
add initial irc_conn code that can register
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
193 |
|
dedf137b504f
add initial irc_conn code that can register
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
194 |
// log |
22
c339c020fd33
add missing irc_cmd.h, and modify line_proto/irc_conn to use log
Tero Marttila <terom@fixme.fi>
parents:
21
diff
changeset
|
195 |
log_debug("%s", line_buf); |
18
dedf137b504f
add initial irc_conn code that can register
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
196 |
|
dedf137b504f
add initial irc_conn code that can register
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
197 |
// add CRLF |
dedf137b504f
add initial irc_conn code that can register
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
198 |
strcat(line_buf, "\r\n"); |
dedf137b504f
add initial irc_conn code that can register
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
199 |
|
dedf137b504f
add initial irc_conn code that can register
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
200 |
// send using line_proto |
19
8c80580ccde9
improve line_proto output buffering slightly
Tero Marttila <terom@fixme.fi>
parents:
18
diff
changeset
|
201 |
// XXX: ignore output-buffering |
24
08a26d0b9afd
fix ret/err bug in irc_conn_send, supress unused-argument warnings
Tero Marttila <terom@fixme.fi>
parents:
23
diff
changeset
|
202 |
return (ret = line_proto_send(conn->lp, line_buf)) < 0 ? -ret : SUCCESS; |
18
dedf137b504f
add initial irc_conn code that can register
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
203 |
} |
dedf137b504f
add initial irc_conn code that can register
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
204 |
|
dedf137b504f
add initial irc_conn code that can register
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
205 |
err_t irc_conn_NICK (struct irc_conn *conn, const char *nickname) |
dedf137b504f
add initial irc_conn code that can register
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
206 |
{ |
dedf137b504f
add initial irc_conn code that can register
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
207 |
// NICK <nickname> |
dedf137b504f
add initial irc_conn code that can register
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
208 |
struct irc_line line = { |
dedf137b504f
add initial irc_conn code that can register
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
209 |
NULL, "NICK", { nickname, NULL } |
dedf137b504f
add initial irc_conn code that can register
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
210 |
}; |
dedf137b504f
add initial irc_conn code that can register
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
211 |
|
dedf137b504f
add initial irc_conn code that can register
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
212 |
return irc_conn_send(conn, &line); |
dedf137b504f
add initial irc_conn code that can register
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
213 |
} |
dedf137b504f
add initial irc_conn code that can register
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
214 |
|
dedf137b504f
add initial irc_conn code that can register
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
215 |
err_t irc_conn_USER (struct irc_conn *conn, const char *username, const char *realname) |
dedf137b504f
add initial irc_conn code that can register
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
216 |
{ |
dedf137b504f
add initial irc_conn code that can register
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
217 |
// USER <user> <mode> <unused> <realname> |
dedf137b504f
add initial irc_conn code that can register
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
218 |
struct irc_line line = { |
20
d9c4c2980a0d
irc_conn PING/PONG code, and line_proto fixups
Tero Marttila <terom@fixme.fi>
parents:
19
diff
changeset
|
219 |
NULL, "USER", { username, "0", "*", realname, NULL } |
18
dedf137b504f
add initial irc_conn code that can register
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
220 |
}; |
dedf137b504f
add initial irc_conn code that can register
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
221 |
|
dedf137b504f
add initial irc_conn code that can register
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
222 |
return irc_conn_send(conn, &line); |
dedf137b504f
add initial irc_conn code that can register
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
223 |
} |
dedf137b504f
add initial irc_conn code that can register
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
224 |
|
20
d9c4c2980a0d
irc_conn PING/PONG code, and line_proto fixups
Tero Marttila <terom@fixme.fi>
parents:
19
diff
changeset
|
225 |
err_t irc_conn_PONG (struct irc_conn *conn, const char *target) |
d9c4c2980a0d
irc_conn PING/PONG code, and line_proto fixups
Tero Marttila <terom@fixme.fi>
parents:
19
diff
changeset
|
226 |
{ |
d9c4c2980a0d
irc_conn PING/PONG code, and line_proto fixups
Tero Marttila <terom@fixme.fi>
parents:
19
diff
changeset
|
227 |
// PONG <server> [ <server2> ] |
d9c4c2980a0d
irc_conn PING/PONG code, and line_proto fixups
Tero Marttila <terom@fixme.fi>
parents:
19
diff
changeset
|
228 |
// XXX: params are actually the wrong way around now, but nobody cares |
d9c4c2980a0d
irc_conn PING/PONG code, and line_proto fixups
Tero Marttila <terom@fixme.fi>
parents:
19
diff
changeset
|
229 |
struct irc_line line = { |
d9c4c2980a0d
irc_conn PING/PONG code, and line_proto fixups
Tero Marttila <terom@fixme.fi>
parents:
19
diff
changeset
|
230 |
NULL, "PONG", { target, NULL } |
d9c4c2980a0d
irc_conn PING/PONG code, and line_proto fixups
Tero Marttila <terom@fixme.fi>
parents:
19
diff
changeset
|
231 |
}; |
d9c4c2980a0d
irc_conn PING/PONG code, and line_proto fixups
Tero Marttila <terom@fixme.fi>
parents:
19
diff
changeset
|
232 |
|
d9c4c2980a0d
irc_conn PING/PONG code, and line_proto fixups
Tero Marttila <terom@fixme.fi>
parents:
19
diff
changeset
|
233 |
return irc_conn_send(conn, &line); |
d9c4c2980a0d
irc_conn PING/PONG code, and line_proto fixups
Tero Marttila <terom@fixme.fi>
parents:
19
diff
changeset
|
234 |
} |
d9c4c2980a0d
irc_conn PING/PONG code, and line_proto fixups
Tero Marttila <terom@fixme.fi>
parents:
19
diff
changeset
|
235 |
|
23
542c73d07d3c
add a simple irc_log module (with evsql code) that joins a channel and log_info's PRIVMSGs
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
236 |
err_t irc_conn_JOIN (struct irc_conn *conn, const char *channel) |
542c73d07d3c
add a simple irc_log module (with evsql code) that joins a channel and log_info's PRIVMSGs
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
237 |
{ |
542c73d07d3c
add a simple irc_log module (with evsql code) that joins a channel and log_info's PRIVMSGs
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
238 |
// JOIN ( <channel> *( "," <channel> ) [ <key> *( "," <key> ) ] ) / "0" |
542c73d07d3c
add a simple irc_log module (with evsql code) that joins a channel and log_info's PRIVMSGs
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
239 |
struct irc_line line = { |
542c73d07d3c
add a simple irc_log module (with evsql code) that joins a channel and log_info's PRIVMSGs
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
240 |
NULL, "JOIN", { channel, NULL } |
542c73d07d3c
add a simple irc_log module (with evsql code) that joins a channel and log_info's PRIVMSGs
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
241 |
}; |
542c73d07d3c
add a simple irc_log module (with evsql code) that joins a channel and log_info's PRIVMSGs
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
242 |
|
542c73d07d3c
add a simple irc_log module (with evsql code) that joins a channel and log_info's PRIVMSGs
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
243 |
return irc_conn_send(conn, &line); |
542c73d07d3c
add a simple irc_log module (with evsql code) that joins a channel and log_info's PRIVMSGs
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
244 |
} |
542c73d07d3c
add a simple irc_log module (with evsql code) that joins a channel and log_info's PRIVMSGs
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
245 |