# HG changeset patch # User Matti Hamalainen # Date 1258478425 -7200 # Node ID 218efd2f0641b050dc52b593f7f87a1411b2ce05 # Parent 7c9538e71c894653781231213ae18450db29d342 Rename functions for clarity. diff -r 7c9538e71c89 -r 218efd2f0641 libnnchat.c --- a/libnnchat.c Sun Jul 05 16:55:40 2009 +0300 +++ b/libnnchat.c Tue Nov 17 19:20:25 2009 +0200 @@ -28,31 +28,31 @@ return buf; } -int getSocketErrno(void) +int nn_get_socket_errno(void) { return WSAGetLastError(); } -const char *getSocketErrStr(int err) +const char *nn_get_socket_errstr(int err) { static char buf[64]; snprintf(buf, sizeof(buf), "Error #%d", err); return buf; } #else -int getSocketErrno(void) +int nn_get_socket_errno(void) { return errno; } -const char *getSocketErrStr(int err) +const char *nn_get_socket_errstr(int err) { return strerror(err); } #endif -int openConnection(struct in_addr *addr, const int port) +int nn_open_connection(struct in_addr *addr, const int port) { struct sockaddr_in tmpAddr; int sock = -1; @@ -80,7 +80,7 @@ } -void closeConnection(const int sock) +void nn_close_connection(const int sock) { if (sock >= 0) { #ifdef __WIN32 @@ -92,7 +92,7 @@ } -BOOL initNetwork(void) +BOOL nn_network_init(void) { #ifdef __WIN32 /* Initialize WinSock, if needed */ @@ -107,7 +107,7 @@ } -void closeNetwork(void) +void nn_network_close(void) { #ifdef __WIN32 WSACleanup(); @@ -115,7 +115,7 @@ } -BOOL sendToSocket(const int sock, char *buf, const size_t bufLen) +BOOL nn_send_to_socket(const int sock, char *buf, const size_t bufLen) { size_t bufLeft = bufLen; char *bufPtr = buf; @@ -164,7 +164,7 @@ } -char *encodeStr1(const char *str) +char *nn_encode_str1(const char *str) { const char *s = str; char *result; @@ -218,7 +218,7 @@ } -char *decodeStr1(const char *str) +char *nn_decode_str1(const char *str) { const char *s = str; char *result; @@ -280,7 +280,7 @@ } -char *stripXMLTags(const char *str) +char *nn_strip_tags(const char *str) { const char *s = str; char *result; @@ -305,7 +305,7 @@ } -char *encodeStr2(const char *str) +char *nn_encode_str2(const char *str) { const char *s = str; char *result; @@ -336,7 +336,7 @@ } -char *decodeStr2(const char *str) +char *nn_decode_str2(const char *str) { const char *s = str; char *result; @@ -372,35 +372,35 @@ } -char *doubleDecodeStr(const char *str) +char *nn_dbldecode_str(const char *str) { char *res, *tmp; - if ((tmp = decodeStr1(str)) == NULL) + if ((tmp = nn_decode_str1(str)) == NULL) return NULL; - res = decodeStr2(tmp); + res = nn_decode_str2(tmp); th_free(tmp); return res; } -char *doubleEncodeStr(const char *str) +char *nn_dblencode_str(const char *str) { char *res, *tmp; - if ((tmp = encodeStr2(str)) == NULL) + if ((tmp = nn_encode_str2(str)) == NULL) return NULL; - res = encodeStr1(tmp); + res = nn_encode_str1(tmp); th_free(tmp); return res; } -BOOL sendUserMsg(const int sock, const char *user, const char *fmt, ...) +BOOL nn_send_msg(const int sock, const char *user, const char *fmt, ...) { char tmpBuf[SET_BUFSIZE], tmpBuf2[SET_BUFSIZE + 256]; int n; @@ -416,11 +416,11 @@ "%s%s", user, tmpBuf); - return sendToSocket(sock, tmpBuf2, strlen(tmpBuf2) + 1); + return nn_send_to_socket(sock, tmpBuf2, strlen(tmpBuf2) + 1); } -nn_ringbuf_t * newRingBuf(const size_t size) +nn_ringbuf_t * nn_ringbuf_new(const size_t size) { nn_ringbuf_t *res = th_calloc(1, sizeof(nn_ringbuf_t)); @@ -432,7 +432,7 @@ } -void freeRingBuf(nn_ringbuf_t *buf) +void nn_ringbuf_free(nn_ringbuf_t *buf) { size_t i; @@ -444,7 +444,7 @@ } -void addRingBuf(nn_ringbuf_t *buf, const char *str) +void nn_ringbuf_add(nn_ringbuf_t *buf, const char *str) { if (buf->n < buf->size) { buf->data[buf->n] = th_strdup(str); @@ -457,7 +457,7 @@ } -int writeBuf(nn_editbuf_t *buf, ssize_t pos, int ch) +int nn_editbuf_write(nn_editbuf_t *buf, ssize_t pos, int ch) { /* Check arguments */ if (buf->len+1 >= buf->size) return -3; @@ -473,7 +473,7 @@ } -int insertBuf(nn_editbuf_t *buf, ssize_t pos, int ch) +int nn_editbuf_insert(nn_editbuf_t *buf, ssize_t pos, int ch) { /* Check arguments */ if (buf->len+1 >= buf->size) return -3; @@ -491,7 +491,7 @@ } -int deleteBuf(nn_editbuf_t *buf, ssize_t pos) +int nn_editbuf_delete(nn_editbuf_t *buf, ssize_t pos) { /* Check arguments */ if (pos < 0) @@ -505,14 +505,14 @@ } -void clearBuf(nn_editbuf_t *buf) +void nn_editbuf_clear(nn_editbuf_t *buf) { buf->len = 0; buf->pos = 0; } -nn_editbuf_t * newBuf(ssize_t n) +nn_editbuf_t * nn_editbuf_new(ssize_t n) { nn_editbuf_t *res = th_calloc(1, sizeof(nn_editbuf_t)); @@ -523,7 +523,7 @@ } -void freeBuf(nn_editbuf_t *buf) +void nn_editbuf_free(nn_editbuf_t *buf) { if (buf) { th_free(buf->data); @@ -532,7 +532,7 @@ } -nn_editbuf_t * copyBuf(nn_editbuf_t *src) +nn_editbuf_t * nn_editbuf_copy(nn_editbuf_t *src) { nn_editbuf_t *res; @@ -540,7 +540,7 @@ if (src == NULL) return NULL; - if ((res = newBuf(src->size)) == NULL) + if ((res = nn_editbuf_new(src->size)) == NULL) return NULL; memcpy(res->data, src->data, src->size); @@ -550,7 +550,7 @@ } -void setBufPos(nn_editbuf_t *buf, ssize_t pos) +void nn_editbuf_setpos(nn_editbuf_t *buf, ssize_t pos) { /* Check arguments */ if (pos < 0) @@ -629,7 +629,7 @@ return -3; user->encname = th_strdup(encname); - user->name = doubleDecodeStr(encname); + user->name = nn_dbldecode_str(encname); if (user->encname == NULL || user->name == NULL) return -4; diff -r 7c9538e71c89 -r 218efd2f0641 libnnchat.h --- a/libnnchat.h Sun Jul 05 16:55:40 2009 +0300 +++ b/libnnchat.h Tue Nov 17 19:20:25 2009 +0200 @@ -50,15 +50,15 @@ #ifdef __WIN32 const char *hstrerror(int err); #endif -int getSocketErrno(void); -const char *getSocketErrStr(int err); -BOOL initNetwork(void); -void closeNetwork(void); +int nn_get_socket_errno(void); +const char *nn_get_socket_errstr(int err); +BOOL nn_network_init(void); +void nn_network_close(void); -int openConnection(struct in_addr *addr, const int port); -void closeConnection(const int sock); -BOOL sendToSocket(const int sock, char *buf, const size_t bufLen); -BOOL sendUserMsg(const int sock, const char *user, const char *fmt, ...); +int nn_open_connection(struct in_addr *addr, const int port); +void nn_close_connection(const int sock); +BOOL nn_send_to_socket(const int sock, char *buf, const size_t bufLen); +BOOL nn_send_msg(const int sock, const char *user, const char *fmt, ...); int addUserToHash(nn_userhash_t **, char *encname); void freeUserHash(nn_userhash_t *); @@ -67,27 +67,27 @@ nn_user_t * findUserEnc(nn_userhash_t *list, char *encname); nn_user_t * matchUsersEnc(nn_userhash_t *list, char *encname, int index); -char * encodeStr1(const char *str); -char * decodeStr1(const char *str); -char * encodeStr2(const char *str); -char * decodeStr2(const char *str); -char * stripXMLTags(const char *str); -char * doubleDecodeStr(const char *str); -char * doubleEncodeStr(const char *str); +char * nn_encode_str1(const char *str); +char * nn_decode_str1(const char *str); +char * nn_encode_str2(const char *str); +char * nn_decode_str2(const char *str); +char * nn_strip_tags(const char *str); +char * nn_dbldecode_str(const char *str); +char * nn_dblencode_str(const char *str); -nn_ringbuf_t * newRingBuf(const size_t size); -void freeRingBuf(nn_ringbuf_t *buf); -void addRingBuf(nn_ringbuf_t *buf, const char *str); +nn_ringbuf_t * nn_ringbuf_new(const size_t size); +void nn_ringbuf_free(nn_ringbuf_t *buf); +void nn_ringbuf_add(nn_ringbuf_t *buf, const char *str); -int writeBuf(nn_editbuf_t *buf, ssize_t pos, int ch); -int insertBuf(nn_editbuf_t *buf, ssize_t pos, int ch); -int deleteBuf(nn_editbuf_t *buf, ssize_t pos); -void clearBuf(nn_editbuf_t *buf); -nn_editbuf_t * newBuf(ssize_t n); -void freeBuf(nn_editbuf_t *buf); -nn_editbuf_t * copyBuf(nn_editbuf_t *src); -void setBufPos(nn_editbuf_t *buf, ssize_t pos); +int nn_editbuf_write(nn_editbuf_t *buf, ssize_t pos, int ch); +int nn_editbuf_insert(nn_editbuf_t *buf, ssize_t pos, int ch); +int nn_editbuf_delete(nn_editbuf_t *buf, ssize_t pos); +void nn_editbuf_clear(nn_editbuf_t *buf); +nn_editbuf_t * nn_editbuf_new(ssize_t n); +void nn_editbuf_free(nn_editbuf_t *buf); +nn_editbuf_t * nn_editbuf_copy(nn_editbuf_t *src); +void nn_editbuf_setpos(nn_editbuf_t *buf, ssize_t pos); #endif diff -r 7c9538e71c89 -r 218efd2f0641 nnchat.c --- a/nnchat.c Sun Jul 05 16:55:40 2009 +0300 +++ b/nnchat.c Tue Nov 17 19:20:25 2009 +0200 @@ -342,10 +342,10 @@ if (!q) return 3; *q = 0; - s = decodeStr1(s); + s = nn_decode_str1(s); if (!s) return -1; - p = decodeStr1(p); + p = nn_decode_str1(p); if (!p) { th_free(s); return -2; @@ -353,20 +353,20 @@ if (*s == '/') { - t = stripXMLTags(s + 1); + t = nn_strip_tags(s + 1); if (!strncmp(t, "BPRV", 4)) { - h = decodeStr2(t + 1); + h = nn_decode_str2(t + 1); printMsg("½11½%s½0½\n", h); } else { - h = decodeStr2(t); + h = nn_decode_str2(t); printMsg("½9½* %s½0½\n", h); } th_free(h); th_free(t); } else { BOOL isMine = strcmp(p, optUserName) == 0; - t = stripXMLTags(s); - h = decodeStr2(t); + t = nn_strip_tags(s); + h = nn_decode_str2(t); printMsg("½5½<½%d½%s½5½>½0½ %s\n", isMine ? 14 : 15, p, h); th_free(h); th_free(t); @@ -394,7 +394,7 @@ return -2; } else if (!strncmp(str, "SUCCESS", 7)) { printMsg("½2½Login success½0½ - ½3½%s½0½\n", tmpStr); - sendUserMsg(sock, optUserName2, "%%2FRequestUserList"); + nn_send_msg(sock, optUserName2, "%%2FRequestUserList"); return 0; } else return 1; @@ -410,7 +410,7 @@ if (!s) return 1; *s = 0; - p = doubleDecodeStr(str); + p = nn_dbldecode_str(str); if (!p) return -1; printMsg("! ½3½%s½0½ ½2½ADDED.½0½\n", p); @@ -428,7 +428,7 @@ if (!s) return 1; *s = 0; - p = doubleDecodeStr(str); + p = nn_dbldecode_str(str); if (!p) return -1; printMsg("! ½3½%s½0½ ½1½DELETED.½0½\n", p); @@ -507,7 +507,7 @@ } optUserColor = tmpInt; printMsg("Setting color to #%06x\n", optUserColor); - sendUserMsg(sock, optUserName2, "%%2FSetFontColor%%20%%2Dcolor%%20%06X", optUserColor); + nn_send_msg(sock, optUserName2, "%%2FSetFontColor%%20%%2Dcolor%%20%06X", optUserColor); return 0; } else if (!strncmp(buf, "/flood ", 7)) { int i; @@ -515,12 +515,12 @@ snprintf(tmpBuf, sizeof(tmpBuf), "/prv -to %s -msg . .", buf+7); - tmpStr = doubleEncodeStr(tmpBuf); + tmpStr = nn_dblencode_str(tmpBuf); if (!tmpStr) return -2; result = TRUE; for (i = 0; i < 50 && result; i++) { - result = sendUserMsg(sock, optUserName2, "%s", tmpStr); + result = nn_send_msg(sock, optUserName2, "%s", tmpStr); usleep(250); } @@ -547,10 +547,10 @@ /* Send double-encoded */ - tmpStr = doubleEncodeStr(buf); + tmpStr = nn_dblencode_str(buf); if (!tmpStr) return -2; - result = sendUserMsg(sock, optUserName2, "%s", tmpStr); + result = nn_send_msg(sock, optUserName2, "%s", tmpStr); th_free(tmpStr); if (result) return 0; @@ -593,7 +593,7 @@ struct timeval socktv; fd_set sockfds; char *tmpStr; - nn_editbuf_t *editBuf = newBuf(SET_BUFSIZE); + nn_editbuf_t *editBuf = nn_editbuf_new(SET_BUFSIZE); nn_editbuf_t *histBuf[SET_MAX_HISTORY+2]; int histPos = 0, histMax = 0; @@ -629,7 +629,7 @@ } } - if (!initNetwork()) { + if (!nn_network_init()) { THERR("Could not initialize network subsystem.\n"); goto err_exit; } else @@ -650,13 +650,13 @@ /* To emulate the official client, we first make a request for * policy file, even though we don't use it for anything... */ - if ((tmpSocket = openConnection((struct in_addr *) tmpHost->h_addr, 843)) < 0) { + if ((tmpSocket = nn_open_connection((struct in_addr *) tmpHost->h_addr, 843)) < 0) { THERR("Policy file request connection setup failed!\n"); goto err_exit; } tmpStr = ""; - if (sendToSocket(tmpSocket, tmpStr, strlen(tmpStr) + 1) == FALSE) { + if (nn_send_to_socket(tmpSocket, tmpStr, strlen(tmpStr) + 1) == FALSE) { THERR("Failed to send policy file request.\n"); goto err_exit; } else { @@ -665,20 +665,20 @@ gotBuf = recv(tmpSocket, tmpBuf, sizeof(tmpBuf), 0); tmpBuf[gotBuf-1] = 0; THMSG(2, "Probe got: %s\n", tmpBuf); - closeConnection(tmpSocket); + nn_close_connection(tmpSocket); } #endif /* Okay, now do the proper connection ... */ - if ((tmpSocket = openConnection((struct in_addr *) tmpHost->h_addr, optPort)) < 0) { + if ((tmpSocket = nn_open_connection((struct in_addr *) tmpHost->h_addr, optPort)) < 0) { THERR("Main connection setup failed!\n"); goto err_exit; } THMSG(1, "Connected, logging in as '%s', site '%s'.\n", optUserName, optSite); - optUserName2 = doubleEncodeStr(optUserName); - tmpStr = doubleEncodeStr(optSite); - sendUserMsg(tmpSocket, optUserName2, "%%2Flogin%%20%%2Dsite%%20%s%%20%%2Dpassword%%20%s", tmpStr, optPassword); + optUserName2 = nn_dblencode_str(optUserName); + tmpStr = nn_dblencode_str(optSite); + nn_send_msg(tmpSocket, optUserName2, "%%2Flogin%%20%%2Dsite%%20%s%%20%%2Dpassword%%20%s", tmpStr, optPassword); th_free(tmpStr); /* Initialize NCurses */ @@ -720,7 +720,7 @@ if (!initializeWindows()) goto err_exit; - clearBuf(editBuf); + nn_editbuf_clear(editBuf); printEditBuf("", editBuf); updateStatus(insertMode); } @@ -739,10 +739,10 @@ socktv.tv_usec = SET_DELAY_USEC; tmpfds = sockfds; if ((result = select(tmpSocket+1, &tmpfds, NULL, NULL, &socktv)) == -1) { - int res = getSocketErrno(); + int res = nn_get_socket_errno(); if (res != EINTR) { errorMsg("Error occured in select(sockfds): %d, %s\n", - res, getSocketErrStr(res)); + res, nn_get_socket_errstr(res)); isError = TRUE; } } else if (FD_ISSET(tmpSocket, &tmpfds)) { @@ -752,8 +752,8 @@ gotBuf = recv(tmpSocket, tmpBuf, sizeof(tmpBuf), 0); if (gotBuf < 0) { - int res = getSocketErrno(); - errorMsg("Error in recv: %d, %s\n", res, getSocketErrStr(res)); + int res = nn_get_socket_errno(); + errorMsg("Error in recv: %d, %s\n", res, nn_get_socket_errstr(res)); isError = TRUE; } else if (gotBuf == 0) { errorMsg("Server closed connection.\n"); @@ -824,19 +824,19 @@ if (editBuf->len > 0) { if (histMax > 0) { - freeBuf(histBuf[SET_MAX_HISTORY+1]); + nn_editbuf_free(histBuf[SET_MAX_HISTORY+1]); histBuf[SET_MAX_HISTORY+1] = NULL; memmove(&histBuf[2], &histBuf[1], histMax * sizeof(histBuf[0])); } histPos = 0; - histBuf[1] = copyBuf(editBuf); + histBuf[1] = nn_editbuf_copy(editBuf); if (histMax < SET_MAX_HISTORY) histMax++; - insertBuf(editBuf, editBuf->len, 0); + nn_editbuf_insert(editBuf, editBuf->len, 0); result = handleUserInput(tmpSocket, editBuf->data, editBuf->len); - clearBuf(editBuf); + nn_editbuf_clear(editBuf); if (result < 0) { errorMsg("Fatal error handling user input: %s\n", editBuf->data); @@ -859,13 +859,13 @@ case KEY_UP: /* Backwards in input history */ if (histPos == 0) { - freeBuf(histBuf[0]); - histBuf[0] = copyBuf(editBuf); + nn_editbuf_free(histBuf[0]); + histBuf[0] = nn_editbuf_copy(editBuf); } if (histPos < histMax) { histPos++; - freeBuf(editBuf); - editBuf = copyBuf(histBuf[histPos]); + nn_editbuf_free(editBuf); + editBuf = nn_editbuf_copy(histBuf[histPos]); update = TRUE; } break; @@ -873,8 +873,8 @@ case KEY_DOWN: /* Forwards in input history */ if (histPos > 0) { histPos--; - freeBuf(editBuf); - editBuf = copyBuf(histBuf[histPos]); + nn_editbuf_free(editBuf); + editBuf = nn_editbuf_copy(histBuf[histPos]); update = TRUE; } break; @@ -908,25 +908,25 @@ break; case 0x10a: /* F2 = Clear editbuffer */ - clearBuf(editBuf); + nn_editbuf_clear(editBuf); update = TRUE; break; - case KEY_HOME: setBufPos(editBuf, 0); update = TRUE; break; - case KEY_END: setBufPos(editBuf, editBuf->len); update = TRUE; break; - case KEY_LEFT: setBufPos(editBuf, editBuf->pos - 1); update = TRUE; break; - case KEY_RIGHT: setBufPos(editBuf, editBuf->pos + 1); update = TRUE; break; + case KEY_HOME: nn_editbuf_setpos(editBuf, 0); update = TRUE; break; + case KEY_END: nn_editbuf_setpos(editBuf, editBuf->len); update = TRUE; break; + case KEY_LEFT: nn_editbuf_setpos(editBuf, editBuf->pos - 1); update = TRUE; break; + case KEY_RIGHT: nn_editbuf_setpos(editBuf, editBuf->pos + 1); update = TRUE; break; case KEY_BACKSPACE: case 0x08: - deleteBuf(editBuf, editBuf->pos - 1); - setBufPos(editBuf, editBuf->pos - 1); + nn_editbuf_delete(editBuf, editBuf->pos - 1); + nn_editbuf_setpos(editBuf, editBuf->pos - 1); update = TRUE; break; case 0x14a: /* Delete */ - deleteBuf(editBuf, editBuf->pos); + nn_editbuf_delete(editBuf, editBuf->pos); update = TRUE; break; @@ -944,10 +944,10 @@ default: if (isprint(c) || c == 0xe4 || c == 0xf6 || c == 0xc4 || c == 0xd6) { if (insertMode) - insertBuf(editBuf, editBuf->pos, c); + nn_editbuf_insert(editBuf, editBuf->pos, c); else - writeBuf(editBuf, editBuf->pos, c); - setBufPos(editBuf, editBuf->pos + 1); + nn_editbuf_write(editBuf, editBuf->pos, c); + nn_editbuf_setpos(editBuf, editBuf->pos + 1); update = TRUE; } else { printMsg("Unhandled key: 0x%02x\n", c); @@ -966,7 +966,7 @@ if (++updateCount > 10) { time_t tmpTime = time(NULL); if (tmpTime - prevTime > SET_KEEPALIVE) { - sendUserMsg(tmpSocket, optUserName2, "/listallusers"); + nn_send_msg(tmpSocket, optUserName2, "/listallusers"); prevTime = tmpTime; } @@ -975,7 +975,7 @@ printMsg("%s v%s - %s\n", th_prog_name, th_prog_version, th_prog_fullname); printMsg("%s\n", th_prog_author); printMsg("%s\n", th_prog_license); - sendUserMsg(tmpSocket, optUserName2, "%%2FSetFontColor%%20%%2Dcolor%%20%06X", optUserColor); + nn_send_msg(tmpSocket, optUserName2, "%%2FSetFontColor%%20%%2Dcolor%%20%06X", optUserColor); } updateStatus(insertMode); @@ -986,9 +986,9 @@ /* Shutdown */ err_exit: - freeBuf(editBuf); + nn_editbuf_free(editBuf); for (histPos = 0; histPos <= SET_MAX_HISTORY; histPos++) - freeBuf(histBuf[histPos]); + nn_editbuf_free(histBuf[histPos]); if (cursesInit) { if (curVis != ERR) @@ -1003,10 +1003,10 @@ th_free(optUserName2); - closeConnection(tmpSocket); + nn_close_connection(tmpSocket); if (networkInit) - closeNetwork(); + nn_network_close(); THMSG(1, "Connection terminated.\n");