2 #include "irc_conn.h" |
2 #include "irc_conn.h" |
3 |
3 |
4 #include <stdlib.h> |
4 #include <stdlib.h> |
5 #include <string.h> |
5 #include <string.h> |
6 |
6 |
|
7 /* |
|
8 * PING <server1> [ <server2> ] |
|
9 * |
|
10 * Send a 'PONG <server1>` reply right away. |
|
11 */ |
|
12 static void on_PING (struct irc_conn *conn, const struct irc_line *line) |
|
13 { |
|
14 // just reply |
|
15 irc_conn_PONG(conn, line->args[0]); |
|
16 } |
|
17 |
|
18 /* |
|
19 * Our command handlers |
|
20 */ |
|
21 struct irc_cmd_handler { |
|
22 /* The command name */ |
|
23 const char *command; |
|
24 |
|
25 /* The handler function */ |
|
26 void (*func) (struct irc_conn *conn, const struct irc_line *line); |
|
27 |
|
28 } _cmd_handlers[] = { |
|
29 { "PING", on_PING }, |
|
30 { NULL, NULL, }, |
|
31 }; |
|
32 |
7 void irc_conn_on_line (char *line_buf, void *arg) |
33 void irc_conn_on_line (char *line_buf, void *arg) |
8 { |
34 { |
|
35 struct irc_conn *conn = arg; |
9 struct irc_line line; |
36 struct irc_line line; |
|
37 struct irc_cmd_handler *handler; |
10 int err; |
38 int err; |
11 |
39 |
12 // log |
40 // log |
13 printf("<<< %s\n", line_buf); |
41 printf("<<< %s\n", line_buf); |
14 |
42 |
15 // parse |
43 // parse |
16 if ((err = irc_line_parse(&line, line_buf))) |
44 if ((err = irc_line_parse(&line, line_buf))) { |
17 printf("!!! Invalid line: %s: %s\n", line_buf, error_name(err)); |
45 printf("!!! Invalid line: %s: %s\n", line_buf, error_name(err)); |
|
46 |
|
47 return; |
|
48 } |
18 |
49 |
19 else |
50 // look up appropriate handler |
20 printf("\tprefix=%s, command=%s, args={%s, %s, %s, ...}\n", |
51 for (handler = _cmd_handlers; handler->command; handler++) { |
21 line.prefix, line.command, line.args[0], line.args[1], line.args[2] |
52 // the command is alpha-only, so normal case-insensitive cmp is fine |
22 ); |
53 if (strcasecmp(handler->command, line.command) == 0) { |
|
54 // invoke the func |
|
55 handler->func(conn, &line); |
|
56 break; |
|
57 } |
|
58 } |
23 } |
59 } |
24 |
60 |
25 err_t irc_conn_create (struct irc_conn **conn_ptr, struct sock_stream *sock, const struct irc_conn_config *config, struct error_info *err) |
61 err_t irc_conn_create (struct irc_conn **conn_ptr, struct sock_stream *sock, const struct irc_conn_config *config, struct error_info *err) |
26 { |
62 { |
27 struct irc_conn *conn; |
63 struct irc_conn *conn; |
62 // add CRLF |
98 // add CRLF |
63 strcat(line_buf, "\r\n"); |
99 strcat(line_buf, "\r\n"); |
64 |
100 |
65 // send using line_proto |
101 // send using line_proto |
66 // XXX: ignore output-buffering |
102 // XXX: ignore output-buffering |
67 return (err = line_proto_write(conn->lp, line_buf)) < 0 ? err : SUCCESS; |
103 return (err = line_proto_send(conn->lp, line_buf)) < 0 ? err : SUCCESS; |
68 } |
104 } |
69 |
105 |
70 err_t irc_conn_NICK (struct irc_conn *conn, const char *nickname) |
106 err_t irc_conn_NICK (struct irc_conn *conn, const char *nickname) |
71 { |
107 { |
72 // NICK <nickname> |
108 // NICK <nickname> |
73 struct irc_line line = { |
109 struct irc_line line = { |
74 NULL, "NICK", { nickname, NULL } |
110 NULL, "NICK", { nickname, NULL } |
75 }; |
111 }; |
76 |
112 |
77 // send it |
|
78 return irc_conn_send(conn, &line); |
113 return irc_conn_send(conn, &line); |
79 } |
114 } |
80 |
115 |
81 err_t irc_conn_USER (struct irc_conn *conn, const char *username, const char *realname) |
116 err_t irc_conn_USER (struct irc_conn *conn, const char *username, const char *realname) |
82 { |
117 { |
83 // USER <user> <mode> <unused> <realname> |
118 // USER <user> <mode> <unused> <realname> |
84 struct irc_line line = { |
119 struct irc_line line = { |
85 NULL, "USER", { username, "0", "*", realname } |
120 NULL, "USER", { username, "0", "*", realname, NULL } |
86 }; |
121 }; |
87 |
122 |
88 // send it |
|
89 return irc_conn_send(conn, &line); |
123 return irc_conn_send(conn, &line); |
90 } |
124 } |
91 |
125 |
|
126 err_t irc_conn_PONG (struct irc_conn *conn, const char *target) |
|
127 { |
|
128 // PONG <server> [ <server2> ] |
|
129 // XXX: params are actually the wrong way around now, but nobody cares |
|
130 struct irc_line line = { |
|
131 NULL, "PONG", { target, NULL } |
|
132 }; |
|
133 |
|
134 return irc_conn_send(conn, &line); |
|
135 } |
|
136 |