1 #include "../rocksock.h"
5 #include <ulz/stdio-repl.h>
10 #define chk(x) if((ret = x)) return ret
11 #define sendl(x) rsirc_sendline(r, x)
13 int rsirc_init(struct rsirc
*r
, struct rocksock
*s
) {
18 int rsirc_sendline(struct rsirc
*r
, const char *line
) {
21 // FIXME loop over this when strlen(line) > 510
22 dprintf(2, "wrote %zu bytes\n", snprintf(buf
, sizeof(buf
), "%s\r\n", line
));
23 return rocksock_send(r
->s
, buf
, 0, 0, &written
);
26 int rsirc_sendlinef(struct rsirc
*r
, const char* fmt
, ...) {
30 vsnprintf(buf
, sizeof buf
, fmt
, ap
);
35 int rsirc_privmsg(struct rsirc
*r
, const char *chan
, const char *msg
) {
37 snprintf(buf
, sizeof buf
, "PRIVMSG %s :%s", chan
, msg
);
41 int rsirc_privmsgf(struct rsirc
*r
, const char *dest
, const char *fmt
, ...) {
45 vsnprintf(buf
, sizeof buf
, fmt
, ap
);
47 return rsirc_privmsg(r
, dest
, buf
);
50 int rsirc_handshake(struct rsirc
*r
, const char* host
, const char* nick
, const char* user
) {
53 snprintf(cmdbuf
, sizeof(cmdbuf
), "NICK %s", nick
);
55 snprintf(cmdbuf
, sizeof(cmdbuf
), "USER %s %s %s :%s", user
, user
, host
, nick
);
60 int rsirc_process(struct rsirc
*r
, char linebuf
[512], size_t *rcvd
) {
61 int ret
, has_data
= 0;
63 chk(rocksock_peek(r
->s
, &has_data
));
65 chk(rocksock_readline(r
->s
, linebuf
, 512, rcvd
));
69 assert(linebuf
[(*rcvd
) - 1] == '\r');
70 linebuf
[(*rcvd
) - 1] = 0;