# HG changeset patch # User Matti Hamalainen # Date 1337809651 -10800 # Node ID acea18a741e34cc92706a2533080df306f0b7de8 # Parent 19cb995994c99fc1b10bddc89bb587e36bfb012e Cleanup libnnchat. diff -r 19cb995994c9 -r acea18a741e3 libnnchat.c --- a/libnnchat.c Sun May 20 23:35:00 2012 +0300 +++ b/libnnchat.c Thu May 24 00:47:31 2012 +0300 @@ -96,11 +96,11 @@ nn_conn_t * nn_conn_new( - void (*errfunc)(nn_conn_t *conn, const char *fmt, va_list ap), - void (*msgfunc)(nn_conn_t *conn, const char *fmt, va_list ap)) + void (*errfunc)(nn_conn_t *conn, const char *fmt, va_list ap), + void (*msgfunc)(nn_conn_t *conn, const char *fmt, va_list ap)) { nn_conn_t *conn = th_calloc(1, sizeof(nn_conn_t)); - + if (conn == NULL) return NULL; @@ -137,7 +137,8 @@ { conn->proxy.hst = nn_resolve_host(conn, host); nn_get_addr(&(conn->proxy.addr), conn->proxy.hst); - } else + } + else return -2; return 0; @@ -187,9 +188,9 @@ nn_conn_err(conn, "Could not open socket: %s\n", nn_get_socket_errstr(conn->err)); goto error; } - + nn_conn_msg(conn, "Using socket %d.\n", conn->socket); - + if (connect(conn->socket, (struct sockaddr *) &dest, sizeof(dest)) == -1) { conn->err = nn_get_socket_errno(); @@ -199,7 +200,7 @@ FD_ZERO(&(conn->sockfds)); FD_SET(conn->socket, &(conn->sockfds)); - + /* Proxy-specific setup */ if (conn->proxy.type == NN_PROXY_SOCKS4 || conn->proxy.type == NN_PROXY_SOCKS4A) { @@ -207,10 +208,10 @@ size_t bufsiz = sizeof(struct nn_socks_t) + strlen(userid) + 1; char *ptr, *buf; int tries, status = -1; - + if (conn->proxy.type == NN_PROXY_SOCKS4A) bufsiz += strlen(conn->host) + 1; - + ptr = buf = th_malloc(bufsiz); if (buf == NULL) { @@ -218,8 +219,8 @@ nn_conn_err(conn, "Could not allocate memory for SOCKS negotiation buffer, %d bytes.\n", bufsiz); goto error; } - - /* Create SOCKS 4/4A request */ + + /* Create SOCKS 4/4A request */ nn_conn_msg(conn, "Initializing proxy negotiation.\n"); socksh = (struct nn_socks_t *) buf; socksh->version = 4; @@ -232,13 +233,13 @@ ptr += sizeof(struct nn_socks_t); strcpy(ptr, userid); - + if (conn->proxy.type == NN_PROXY_SOCKS4A) { ptr += strlen(userid) + 1; strcpy(ptr, conn->host); } - + /* Send request */ nn_conn_reset(conn); if (!nn_conn_send_buf(conn, buf, bufsiz)) @@ -248,7 +249,7 @@ goto error; } th_free(buf); - + /* Wait for SOCKS server to reply */ for (status = tries = 1; tries <= 20 && status > 0; tries++) { @@ -260,7 +261,7 @@ nn_conn_reset(conn); status = nn_conn_pull(conn); } - + /* Check results */ if (status == 0) { @@ -311,7 +312,7 @@ { if (conn == NULL) return; - + if (conn->socket >= 0) { #ifdef __WIN32 @@ -324,9 +325,9 @@ th_free(conn->host); th_free(conn->proxy.host); - + conn->status = NN_CONN_CLOSED; - + th_free(conn); } @@ -335,7 +336,7 @@ { size_t bufLeft = len; const char *bufPtr = buf; - + while (bufLeft > 0) { ssize_t bufSent; @@ -385,8 +386,8 @@ socket, conn->err, nn_get_socket_errstr(conn->err)); return -1; } - } else - if (FD_ISSET(conn->socket, &tmpfds)) + } + else if (FD_ISSET(conn->socket, &tmpfds)) { conn->got = recv(conn->socket, conn->ptr, NN_CONNBUF_SIZE - conn->total, 0); if (conn->got < 0) @@ -394,7 +395,8 @@ conn->err = nn_get_socket_errno(); nn_conn_err(conn, "Error in recv: %d, %s\n", conn->err, nn_get_socket_errstr(conn->err)); return -2; - } else if (conn->got == 0) + } + else if (conn->got == 0) { nn_conn_err(conn, "Server closed connection.\n"); conn->status = NN_CONN_CLOSED; @@ -408,7 +410,7 @@ return 0; } } - + return 1; } @@ -454,13 +456,13 @@ const char *s = str; char *result; size_t resSize, resPos = 0; - + if (str == NULL) return NULL; - + resSize = strlen(str) + NN_ALLOC_SIZE; if ((result = th_malloc(resSize)) == NULL) return NULL; - + while (*s) { switch (*s) @@ -468,7 +470,7 @@ case 32: PUSHCHAR('+'); break; - + default: if (th_isalnum(*s)) PUSHCHAR(*s); @@ -484,7 +486,7 @@ s++; } PUSHCHAR(0); - + return result; } @@ -492,7 +494,7 @@ static int getHexDigit(const int c, const int shift) { int i; - + if (c >= 'A' && c <= 'F') i = c - 'A' + 10; else if (c >= 'a' && c <= 'f') @@ -501,7 +503,7 @@ i = c - '0'; else return -1; - + return i << shift; } @@ -512,13 +514,13 @@ char *result; size_t resSize, resPos = 0; int c; - + if (str == NULL) return NULL; - + resSize = strlen(str) + NN_ALLOC_SIZE; if ((result = th_malloc(resSize)) == NULL) return NULL; - + while (*s) { switch (*s) @@ -527,24 +529,25 @@ PUSHCHAR(' '); s++; break; - + case '½': /* Escape these .. */ PUSHCHAR('½'); PUSHCHAR('½'); s++; break; - + case '\r': PUSHCHAR(' '); s++; break; - + case '%': s++; if (*s == '%') PUSHCHAR('%'); - else if ((c = getHexDigit(*s, 4)) >= 0) { + else if ((c = getHexDigit(*s, 4)) >= 0) + { int i = getHexDigit(*(++s), 0); if (i >= 0) { @@ -563,14 +566,14 @@ } s++; break; - + default: PUSHCHAR(*s); s++; } } PUSHCHAR(0); - + return result; } @@ -580,24 +583,25 @@ const char *s = str; char *result; size_t resSize, resPos = 0; - + if (str == NULL) return NULL; - + resSize = strlen(str) + NN_ALLOC_SIZE; if ((result = th_malloc(resSize)) == NULL) return NULL; - + while (*s) { if (*s == '<') { while (*s && *s != '>') s++; if (*s == '>') s++; - } else + } + else PUSHCHAR(*s++); } PUSHCHAR(0); - + return result; } @@ -623,30 +627,30 @@ const char *s = str; char *result; size_t resSize, resPos = 0; - + if (str == NULL) return NULL; - + resSize = strlen(str) + NN_ALLOC_SIZE; if ((result = th_malloc(resSize)) == NULL) return NULL; - + while (*s) { int i; BOOL found = FALSE; for (i = 0; i < numHTMLEntities; i++) - if (HTMLEntities[i].c == *s) - { - PUSHSTR(HTMLEntities[i].ent); - found = TRUE; - break; - } + if (HTMLEntities[i].c == *s) + { + PUSHSTR(HTMLEntities[i].ent); + found = TRUE; + break; + } if (!found) PUSHCHAR(*s); - + s++; } PUSHCHAR(0); - + return result; } @@ -656,13 +660,13 @@ const char *s = str; char *result; size_t resSize, resPos = 0; - + if (str == NULL) return NULL; - + resSize = strlen(str); if ((result = th_malloc(resSize)) == NULL) return NULL; - + while (*s) { if (*s == '&') @@ -682,11 +686,12 @@ } } if (!found) PUSHCHAR(*s++); - } else + } + else PUSHCHAR(*s++); } PUSHCHAR(0); - + return result; } @@ -694,28 +699,28 @@ char *nn_dbldecode_str(const char *str) { char *res, *tmp; - + if ((tmp = nn_decode_str1(str)) == NULL) return NULL; - + res = nn_decode_str2(tmp); th_free(tmp); - - return res; + + return res; } char *nn_dblencode_str(const char *str) { char *res, *tmp; - + if ((tmp = nn_encode_str2(str)) == NULL) return NULL; - + res = nn_encode_str1(tmp); th_free(tmp); - - return res; + + return res; } @@ -723,23 +728,24 @@ { char *tmp, *msg; va_list ap; - + va_start(ap, fmt); tmp = th_strdup_vprintf(fmt, ap); va_end(ap); - + if (tmp == NULL) return FALSE; - + msg = th_strdup_printf("%s%s", user, tmp); th_free(tmp); - + if (msg != NULL) { BOOL ret = nn_conn_send_buf(conn, msg, strlen(msg) + 1); th_free(msg); return ret; - } else + } + else return FALSE; } @@ -747,7 +753,7 @@ int nn_editbuf_write(nn_editbuf_t *buf, ssize_t pos, int ch) { if (buf->len+1 >= buf->size) return -3; - + if (pos < 0) return -1; else if (pos >= buf->len) @@ -762,7 +768,7 @@ int nn_editbuf_insert(nn_editbuf_t *buf, ssize_t pos, int ch) { if (buf->len+1 >= buf->size) return -3; - + if (pos < 0) return -1; else if (pos >= buf->len) @@ -788,7 +794,8 @@ memmove(&(buf->data[pos]), &(buf->data[pos+1]), buf->len - pos); buf->len--; return 0; - } else + } + else return -2; } @@ -803,10 +810,10 @@ nn_editbuf_t * nn_editbuf_new(ssize_t n) { nn_editbuf_t *res = th_calloc(1, sizeof(nn_editbuf_t)); - + res->data = (char *) th_malloc(n); res->size = n; - + return res; } @@ -824,17 +831,17 @@ nn_editbuf_t * nn_editbuf_copy(nn_editbuf_t *src) { nn_editbuf_t *res; - + assert(src != NULL); - + if (src == NULL) return NULL; - + if ((res = nn_editbuf_new(src->size)) == NULL) return NULL; - + memcpy(res->data, src->data, src->size); res->pos = res->len = src->len; - + return res; } @@ -843,7 +850,7 @@ { char *str; ssize_t siz; - + if (buf == NULL) return NULL; @@ -857,7 +864,8 @@ else if (start <= end) { siz = end - start + 1; - } else + } + else return NULL; if ((str = th_malloc(siz + 1)) == NULL) @@ -914,19 +922,19 @@ int i; if (list == NULL) return NULL; - + for (i = 0; i < NN_NUM_BUCKETS; i++) - if (list->buckets[i] != NULL) - { - nn_user_t *curr = list->buckets[i]; - while (curr != NULL) + if (list->buckets[i] != NULL) { - if (func(curr) != 0) - return curr; - curr = curr->next; + nn_user_t *curr = list->buckets[i]; + while (curr != NULL) + { + if (func(curr) != 0) + return curr; + curr = curr->next; + } } - } - + return NULL; } @@ -934,9 +942,9 @@ nn_user_t *nn_user_find(const nn_userhash_t *list, const char *name) { uint8_t hash; - + if (list == NULL) return NULL; - + hash = nn_hash_user(name); if (list->buckets[hash] != NULL) { @@ -948,7 +956,7 @@ curr = curr->next; } } - + return NULL; } @@ -970,7 +978,7 @@ nn_user_t *nn_user_match(const nn_userhash_t *list, const char *pattern, const char *current, BOOL again) { uint8_t hash; - + if (list == NULL || pattern == NULL) return NULL; hash = nn_hash_user(pattern); @@ -993,7 +1001,7 @@ } curr = curr->next; } - + if (found != NULL && (found = nn_user_match_do(found, pattern, len)) != NULL) return found; } @@ -1001,7 +1009,7 @@ if ((curr = nn_user_match_do(list->buckets[hash], pattern, len)) != NULL) return curr; } - + return NULL; } @@ -1016,23 +1024,23 @@ { uint8_t hash; nn_user_t *user; - + /* Check arguments */ if (list == NULL || name == NULL) return -1; - + /* Check if username is already there */ if (nn_user_find(list, name) != NULL) return 1; - + /* No, we'll add it */ if ((user = th_calloc(1, sizeof(nn_user_t))) == NULL) return -3; - + user->name = th_strdup(name); if (user->name == NULL) return -4; - + hash = nn_hash_user(name); nn_user_insert(&(list->buckets[hash]), user); @@ -1043,11 +1051,11 @@ int nn_userhash_delete(nn_userhash_t *list, const char *name) { uint8_t hash; - + /* Check arguments */ if (list == NULL || name == NULL) return -1; - + /* Check if username is already there */ hash = nn_hash_user(name); if (list->buckets[hash] != NULL) @@ -1063,9 +1071,9 @@ prev->next = curr->next; else list->buckets[hash] = curr->next; - + nn_user_free(curr); - + return 0; } else @@ -1083,25 +1091,25 @@ nn_user_t *nn_user_copy(const nn_user_t *src) { nn_user_t *user; - + if (src == NULL) return NULL; - + if ((user = th_calloc(1, sizeof(nn_user_t))) == NULL) return NULL; - + /* Copy relevant data */ user->name = th_strdup(src->name); user->lastspoke = src->lastspoke; user->joined = src->joined; - + return user; } void nn_user_free(nn_user_t *user) { - th_free(user->name); - th_free(user); + th_free(user->name); + th_free(user); } @@ -1122,13 +1130,13 @@ int i; if (hash == NULL) return; - + for (i = 0; i < NN_NUM_BUCKETS; i++) { nn_user_free_list(hash->buckets[i]); hash->buckets[i] = NULL; } - + th_free(hash); } @@ -1137,7 +1145,7 @@ { unsigned char *c = (unsigned char *) str; if (str == NULL) return NULL; - for (; *c ; c++) + for (; *c ; c++) if (*c == ' ') *c = 255; return str; } @@ -1147,7 +1155,7 @@ { unsigned char *c = (unsigned char *) str; if (str == NULL) return NULL; - for (; *c ; c++) + for (; *c ; c++) if (*c == 255) *c = ' '; return str; } @@ -1156,7 +1164,7 @@ nn_window_t *nn_window_new(const char *id) { nn_window_t *res = th_calloc(1, sizeof(nn_window_t)); - + if (res == NULL) return NULL; res->data = th_ringbuf_new(NN_BACKBUF_LEN, th_free); @@ -1167,7 +1175,7 @@ } res->id = th_strdup(id); - + return res; } diff -r 19cb995994c9 -r acea18a741e3 libnnchat.h --- a/libnnchat.h Sun May 20 23:35:00 2012 +0300 +++ b/libnnchat.h Thu May 24 00:47:31 2012 +0300 @@ -35,42 +35,49 @@ #define NN_DELAY_USEC (15 * 1000) #define NN_BACKBUF_LEN (512) /* Backbuffer size (in lines) */ -enum { +enum +{ NN_CONN_UNINIT = 0, NN_CONN_PROXY_NEG, NN_CONN_OPEN, NN_CONN_CLOSED }; -enum { +enum +{ NN_PROXY_NONE = 0, NN_PROXY_SOCKS4, NN_PROXY_SOCKS4A, - + NN_PROXY_LAST }; -enum { +enum +{ SOCKS_CMD_CONNECT = 1, SOCKS_CMD_BIND = 2 }; -struct nn_socks_t { +struct nn_socks_t +{ uint8_t version; uint8_t command; in_port_t port; in_addr_t addr; } __attribute__((__packed__)); -struct nn_socks_res_t { +struct nn_socks_res_t +{ uint8_t nb; uint8_t result; in_port_t port; in_addr_t addr; } __attribute__((__packed__)); -typedef struct _nn_conn_t { - struct { +typedef struct _nn_conn_t +{ + struct + { char *host; struct hostent *hst; int type; @@ -91,7 +98,7 @@ int err; int status; - + char buf[NN_CONNBUF_SIZE + 16]; char *ptr; ssize_t got, total; @@ -104,8 +111,8 @@ struct hostent *nn_resolve_host(nn_conn_t *conn, const char *name); nn_conn_t * nn_conn_new( - void (*errfunc)(nn_conn_t *conn, const char *fmt, va_list ap), - void (*msgfunc)(nn_conn_t *conn, const char *fmt, va_list ap)); + void (*errfunc)(nn_conn_t *conn, const char *fmt, va_list ap), + void (*msgfunc)(nn_conn_t *conn, const char *fmt, va_list ap)); int nn_conn_set_proxy(nn_conn_t *conn, int type, int port, const char *host); int nn_conn_open(nn_conn_t *conn, const int port, const char *host); @@ -117,13 +124,15 @@ BOOL nn_conn_check(nn_conn_t *); -typedef struct _nn_user_t { +typedef struct _nn_user_t +{ char *name; time_t lastspoke, joined; struct _nn_user_t *next; } nn_user_t; -typedef struct { +typedef struct +{ nn_user_t *buckets[NN_NUM_BUCKETS]; } nn_userhash_t; @@ -151,7 +160,8 @@ char * nn_username_decode(char *str); -typedef struct { +typedef struct +{ ssize_t pos, len, size; char *data; } nn_editbuf_t; @@ -167,14 +177,15 @@ char * nn_editbuf_get_string(nn_editbuf_t *buf, ssize_t start, ssize_t end); -typedef struct { +typedef struct +{ qringbuf_t *data; /* "Backbuffer" data for this window */ int pos; /* Current position in the window, 0 = real time */ BOOL dirty; char *id; /* Chatter ID, NULL = main window */ int num; /* Window number */ - + char *buf; size_t len, bufsize; size_t chlen; @@ -184,7 +195,8 @@ void nn_window_free(nn_window_t *); -typedef struct { +typedef struct +{ size_t len; char *str; } nn_strtuple_t;