add rocksock_strerror_detailed()
[rofl0r-rocksock.git] / rocksockirc / rsirc.c
blob2f3f703658029f3d619ef594f6f90ee3ed1c4bf1
1 #include "../rocksock.h"
2 #include "rsirc.h"
3 #include <stdio.h>
4 #ifdef USE_LIBULZ
5 #include <ulz/stdio-repl.h>
6 #endif
7 #include <stdarg.h>
8 #include <assert.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) {
14 r->s = s;
15 return 0;
18 int rsirc_sendline(struct rsirc *r, const char *line) {
19 char buf[512];
20 size_t written;
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, ...) {
27 va_list ap;
28 va_start(ap, fmt);
29 char buf[512];
30 vsnprintf(buf, sizeof buf, fmt, ap);
31 va_end(ap);
32 return sendl(buf);
35 int rsirc_privmsg(struct rsirc *r, const char *chan, const char *msg) {
36 char buf[512];
37 snprintf(buf, sizeof buf, "PRIVMSG %s :%s", chan, msg);
38 return sendl(buf);
41 int rsirc_privmsgf(struct rsirc *r, const char *dest, const char *fmt, ...) {
42 va_list ap;
43 va_start(ap, fmt);
44 char buf[512];
45 vsnprintf(buf, sizeof buf, fmt, ap);
46 va_end(ap);
47 return rsirc_privmsg(r, dest, buf);
50 int rsirc_handshake(struct rsirc *r, const char* host, const char* nick, const char* user) {
51 char cmdbuf[512];
52 int ret;
53 snprintf(cmdbuf, sizeof(cmdbuf), "NICK %s", nick);
54 chk(sendl(cmdbuf));
55 snprintf(cmdbuf, sizeof(cmdbuf), "USER %s %s %s :%s", user, user, host, nick);
56 chk(sendl(cmdbuf));
57 return 0;
60 int rsirc_process(struct rsirc *r, char linebuf[512], size_t *rcvd) {
61 int ret, has_data = 0;
62 *rcvd = 0;
63 chk(rocksock_peek(r->s, &has_data));
64 if(has_data) {
65 chk(rocksock_readline(r->s, linebuf, 512, rcvd));
66 assert(*rcvd < 512);
67 if(*rcvd != 0) {
68 assert(*rcvd >= 1);
69 assert(linebuf[(*rcvd) - 1] == '\r');
70 linebuf[(*rcvd) - 1] = 0;
73 return 0;