diff main.c @ 622:bb6b07b44800

Network code is being generalized into a th-libs module.
author Matti Hamalainen <ccr@tnsp.org>
date Tue, 27 May 2014 07:31:20 +0300
parents 29b8ff5b625b
children 118276b60667
line wrap: on
line diff
--- a/main.c	Tue May 27 07:27:14 2014 +0300
+++ b/main.c	Tue May 27 07:31:20 2014 +0300
@@ -3,11 +3,11 @@
  * Written by Matti 'ccr' Hämäläinen
  * (C) Copyright 2008-2014 Tecnic Software productions (TNSP)
  */
-#include "util.h"
-#include "network.h"
-#include "ui.h"
 #include "th_args.h"
 #include "th_config.h"
+#include "th_network.h"
+#include "util.h"
+#include "ui.h"
 #include <unistd.h>
 #include <fcntl.h>
 #ifdef __WIN32
@@ -350,6 +350,42 @@
 }
 
 
+BOOL nn_conn_send_msg(th_conn_t *conn, const char *user, const char *str)
+{
+    char *msg;
+
+    if (str == NULL)
+        return FALSE;
+    
+    msg = th_strdup_printf("<USER>%s</USER><MESSAGE>%s</MESSAGE>", user, str);
+
+    if (msg != NULL)
+    {
+        BOOL ret = nn_conn_send_buf(conn, msg, strlen(msg) + 1);
+        th_free(msg);
+        return ret;
+    }
+    else
+        return FALSE;
+}
+
+
+BOOL nn_conn_send_msg_v(th_conn_t *conn, const char *user, const char *fmt, ...)
+{
+    BOOL res;
+    char *tmp;
+    va_list ap;
+
+    va_start(ap, fmt);
+    tmp = th_strdup_vprintf(fmt, ap);
+    va_end(ap);
+
+    res = nn_conn_send_msg(conn, user, tmp);
+    th_free(tmp);
+    return res;
+}
+
+
 int printFile(FILE *outFile, const char *fmt)
 {
     const char *s = fmt;
@@ -530,14 +566,14 @@
 }
 
 
-void nn_network_errfunc(struct _nn_conn_t *conn, const char *msg)
+void nn_network_errfunc(struct _th_conn_t *conn, const char *msg)
 {
     (void) conn;
     errorMsg("%s", msg);
 }
 
 
-void nn_network_msgfunc(struct _nn_conn_t *conn, const char *msg)
+void nn_network_msgfunc(struct _th_conn_t *conn, const char *msg)
 {
     (void) conn;
     printMsgConst(NULL, LOG_STAMP | LOG_WINDOW | LOG_FILE, msg);
@@ -573,23 +609,23 @@
 }
 
 
-int nnproto_parse_user(nn_conn_t *conn)
+int nnproto_parse_user(th_conn_t *conn)
 {
     BOOL isMine, isIgnored = FALSE;
     char *name, *msg, *t;
 
     // Find start of the message
     name = conn->ptr;
-    t = nn_conn_buf_strstr(conn, "</USER>");
+    t = th_conn_buf_strstr(conn, "</USER>");
     if (!t) return 1;
     *t = 0;
 
     // Find end of the message
-    t = nn_conn_buf_strstr(conn, "<MESSAGE>");
+    t = th_conn_buf_strstr(conn, "<MESSAGE>");
     if (!t) return 2;
     msg = conn->ptr;
     
-    t = nn_conn_buf_strstr(conn, "</MESSAGE>");
+    t = th_conn_buf_strstr(conn, "</MESSAGE>");
     if (!t) return 3;
     *t = 0;
 
@@ -715,22 +751,22 @@
 }
 
 
-int nnproto_parse_login(nn_conn_t *conn)
+int nnproto_parse_login(th_conn_t *conn)
 {
     char tmpStr[256];
     str_get_timestamp(tmpStr, sizeof(tmpStr), "%c");
 
-    if (!nn_conn_buf_strcmp(conn, "FAILURE>"))
+    if (!th_conn_buf_strcmp(conn, "FAILURE>"))
     {
-        nn_conn_buf_strstr(conn, "</LOGIN_FAILURE>");
-        nn_conn_buf_strstr(conn, "</USER>");
+        th_conn_buf_strstr(conn, "</LOGIN_FAILURE>");
+        th_conn_buf_strstr(conn, "</USER>");
         printMsg(NULL, "½1½Login failure½0½ - ½3½%s½0½\n", tmpStr);
         return -2;
     }
-    else if (!nn_conn_buf_strcmp(conn, "SUCCESS>"))
+    else if (!th_conn_buf_strcmp(conn, "SUCCESS>"))
     {
-        nn_conn_buf_strstr(conn, "</LOGIN_SUCCESS>");
-        nn_conn_buf_strstr(conn, "</USER>");
+        th_conn_buf_strstr(conn, "</LOGIN_SUCCESS>");
+        th_conn_buf_strstr(conn, "</USER>");
         printMsg(NULL, "½2½Login success½0½ - ½3½%s½0½\n", tmpStr);
         nn_conn_send_msg(conn, optUserNameEnc, "%2FRequestUserList");
         return 0;
@@ -740,12 +776,12 @@
 }
 
 
-int nnproto_parse_add_user(nn_conn_t *conn)
+int nnproto_parse_add_user(th_conn_t *conn)
 {
     char *p, *s, *str = conn->ptr;
     nn_window_t *win;
 
-    s = nn_conn_buf_strstr(conn, "</ADD_USER>");
+    s = th_conn_buf_strstr(conn, "</ADD_USER>");
     if (!s) return 1;
     *s = 0;
 
@@ -768,12 +804,12 @@
 }
 
 
-int nnproto_parse_delete_user(nn_conn_t *conn)
+int nnproto_parse_delete_user(th_conn_t *conn)
 {
     char *p, *s, *str = conn->ptr;
     nn_window_t *win;
 
-    s = nn_conn_buf_strstr(conn, "</DELETE_USER>");
+    s = th_conn_buf_strstr(conn, "</DELETE_USER>");
     if (!s) return 1;
     *s = 0;
 
@@ -796,14 +832,14 @@
 }
 
 
-int nnproto_parse_num_clients(nn_conn_t *conn)
+int nnproto_parse_num_clients(th_conn_t *conn)
 {
-    nn_conn_buf_strstr(conn, "</NUMCLIENTS>");
+    th_conn_buf_strstr(conn, "</NUMCLIENTS>");
     return 0;
 }
 
 
-int nnproto_parse_boot(nn_conn_t *conn)
+int nnproto_parse_boot(th_conn_t *conn)
 {
     (void) conn;
     errorMsg("Booted by server.\n");
@@ -815,7 +851,7 @@
 {
     char *name;
     size_t len;
-    int (*handler)(nn_conn_t *);
+    int (*handler)(th_conn_t *);
 } nn_protocolcmd_t;
 
 
@@ -832,7 +868,7 @@
 static const int nprotoCmds = sizeof(protoCmds) / sizeof(protoCmds[0]);
 
 
-int nn_parse_protocol(nn_conn_t *conn)
+int nn_parse_protocol(th_conn_t *conn)
 {
     static BOOL protoCmdsInit = FALSE;
     int i;
@@ -847,7 +883,7 @@
 
     for (i = 0; i < nprotoCmds; i++)
     {
-        if (!nn_conn_buf_strncmp(conn, protoCmds[i].name, protoCmds[i].len))
+        if (!th_conn_buf_strncmp(conn, protoCmds[i].name, protoCmds[i].len))
             return protoCmds[i].handler(conn);
     }
 
@@ -905,7 +941,7 @@
 }
 
 
-int nncmd_send_raw(nn_conn_t *conn, char *str)
+int nncmd_send_raw(th_conn_t *conn, char *str)
 {
 #if 1
     char *tmp = nn_encode_str1(str);
@@ -920,7 +956,7 @@
 }
 
 
-int nncmd_open_profile(nn_conn_t *conn, char *name)
+int nncmd_open_profile(th_conn_t *conn, char *name)
 {
     char *enc_name = nn_encode_str1(name);
     char *uri = th_strdup_printf(SET_PROFILE_PREFIX, name);
@@ -936,7 +972,7 @@
 }
 
 
-int nncmd_change_list(nn_conn_t *conn, const char *listname, qlist_t **list, const char *name)
+int nncmd_change_list(th_conn_t *conn, const char *listname, qlist_t **list, const char *name)
 {
     (void) conn;
 
@@ -979,19 +1015,19 @@
 }
 
 
-int nncmd_ignore(nn_conn_t *conn, char *name)
+int nncmd_ignore(th_conn_t *conn, char *name)
 {
     return nncmd_change_list(conn, "ignore", &setIgnoreList, name);
 }
 
 
-int nncmd_friend(nn_conn_t *conn, char *name)
+int nncmd_friend(th_conn_t *conn, char *name)
 {
     return nncmd_change_list(conn, "friend", &setFriendList, name);
 }
 
 
-int nncmd_set_color(nn_conn_t *conn, char *arg)
+int nncmd_set_color(th_conn_t *conn, char *arg)
 {
     int val;
     (void) conn;
@@ -1009,7 +1045,7 @@
 }
 
 
-int nncmd_open_query(nn_conn_t *conn, char *name)
+int nncmd_open_query(th_conn_t *conn, char *name)
 {
     (void) conn;
 
@@ -1042,7 +1078,7 @@
 }
 
 
-int nncmd_close_query(nn_conn_t *conn, char *name)
+int nncmd_close_query(th_conn_t *conn, char *name)
 {
     (void) conn;
 
@@ -1078,7 +1114,7 @@
 }
 
 
-int nncmd_window_info(nn_conn_t *conn, char *arg)
+int nncmd_window_info(th_conn_t *conn, char *arg)
 {
     (void) conn;
 
@@ -1103,7 +1139,7 @@
 }
 
 
-int nncmd_list_all_users(nn_conn_t *conn, char *buf)
+int nncmd_list_all_users(th_conn_t *conn, char *buf)
 {
     (void) buf;
 
@@ -1131,10 +1167,10 @@
     size_t len;
     int color;
 
-    if (checkNameList(setFriendList, user->name))
+    if (nn_check_name_list(setFriendList, user->name))
         color = 11;
     else
-    if (checkNameList(setIgnoreList, user->name))
+    if (nn_check_name_list(setIgnoreList, user->name))
         color = 1;
     else
         color = 3;
@@ -1158,7 +1194,7 @@
 }
 
 
-int nncmd_names(nn_conn_t *conn, char *arg)
+int nncmd_names(th_conn_t *conn, char *arg)
 {
     nncmd_namedata_t data;
     (void) conn;
@@ -1180,7 +1216,7 @@
 }
 
 
-int nncmd_save_config(nn_conn_t *conn, char *buf)
+int nncmd_save_config(th_conn_t *conn, char *buf)
 {
     (void) conn;
     (void) buf;
@@ -1217,7 +1253,7 @@
 }
 
 
-int nncmd_quit(nn_conn_t *conn, char *buf)
+int nncmd_quit(th_conn_t *conn, char *buf)
 {
     (void) conn;
     (void) buf;
@@ -1240,7 +1276,7 @@
     char *name;
     int flags;
     size_t len;
-    int (*handler)(nn_conn_t *, char *buf);
+    int (*handler)(th_conn_t *, char *buf);
 } nn_usercmd_t;
 
 
@@ -1289,7 +1325,7 @@
 }
 
 
-int nn_handle_command(nn_conn_t *conn, char *buf)
+int nn_handle_command(th_conn_t *conn, char *buf)
 {
     qlist_t *curr;
 
@@ -1398,7 +1434,7 @@
 }
 
 
-int nn_handle_input(nn_conn_t *conn, char *buf, size_t bufLen)
+int nn_handle_input(th_conn_t *conn, char *buf, size_t bufLen)
 {
     BOOL result;
     char *tmp;
@@ -1936,7 +1972,7 @@
     char *tmpStr;
     int index, updateCount = 0;
     BOOL argsOK, colorSet = FALSE;
-    nn_conn_t *conn = NULL;
+    th_conn_t *conn = NULL;
     nn_editbuf_t *editBuf = nn_editbuf_new(NN_TMPBUF_SIZE);
     nn_editstate_t editState;
     th_cfgitem_t *tmpcfg;
@@ -2170,7 +2206,7 @@
     }
 
     // Create a connection
-    conn = nn_conn_new(nn_network_errfunc, nn_network_msgfunc);
+    conn = th_conn_new(nn_network_errfunc, nn_network_msgfunc);
     if (conn == NULL)
     {
         errorMsg("Could not create connection structure.\n");
@@ -2185,8 +2221,8 @@
         if (optProxyUserID == NULL)
             optProxyUserID = "James Bond";
 
-        if (nn_conn_set_proxy(conn, optProxyType, optProxyPort, optProxyServer, optProxyAuthType) != 0 ||
-            nn_conn_set_proxy_auth_user(conn, optProxyUserID, optProxyPassword) != 0)
+        if (th_conn_set_proxy(conn, optProxyType, optProxyPort, optProxyServer, optProxyAuthType) != 0 ||
+            th_conn_set_proxy_auth_user(conn, optProxyUserID, optProxyPassword) != 0)
         {
             errorMsg("Error setting proxy information.\n");
             goto err_exit;
@@ -2203,21 +2239,21 @@
     /* To emulate the official client, we first make a request for
      * policy file, even though we don't use it for anything...
      */
-    if (nn_conn_open(conn, 843, NULL) != 0)
+    if (th_conn_open(conn, 843, NULL) != 0)
     {
         errorMsg("Policy file request connection setup failed!\n");
         goto err_exit;
     }
 
     tmpStr = "<policy-file-request/>";
-    if (nn_conn_send_buf(conn, tmpStr, strlen(tmpStr) + 1) == FALSE)
+    if (th_conn_send_buf(conn, tmpStr, strlen(tmpStr) + 1) == FALSE)
     {
         errorMsg("Failed to send policy file request.\n");
         goto err_exit;
     }
     else
     {
-        int cres = nn_conn_pull(conn);
+        int cres = th_conn_pull(conn);
         if (cres == 0)
         {
             printMsg(currWin, "Probe got: %s\n", conn->buf);
@@ -2227,11 +2263,11 @@
             printMsg(currWin, "Could not get policy probe.\n");
         }
     }
-    nn_conn_free(conn);
+    th_conn_free(conn);
 #endif
 
     // Okay, now do the proper connection ...
-    if (nn_conn_open(conn, optPort, NULL) != 0)
+    if (th_conn_open(conn, optPort, NULL) != 0)
     {
         errorMsg("Main connection setup failed!\n");
         goto err_exit;
@@ -2251,25 +2287,25 @@
     srandom((int) editState.prevKeepAlive);
 
     // Enter mainloop
-    nn_conn_reset(conn);
+    th_conn_reset(conn);
     while (!editState.isError && !appQuitFlag)
     {
         int retries = 3, cres;
         editState.update = FALSE;
 
 packet_retry:
-        cres = nn_conn_pull(conn);
+        cres = th_conn_pull(conn);
         if (cres == 0)
         {
             while (conn->ptr < conn->in_ptr &&
                    *(conn->in_ptr - 1) == 0 &&
                    retries > 0 && !editState.isError)
             {
-//                nn_conn_dump_buffer(stderr, conn);
+//                th_conn_dump_buffer(stderr, conn);
                 int result = nn_parse_protocol(conn);
                 if (result == 0)
                 {
-                    nn_conn_buf_skip(conn, 1);
+                    th_conn_buf_skip(conn, 1);
                 }
                 else
                 if (result > 0)
@@ -2280,14 +2316,14 @@
 
                     // Couldn't handle the message for some reason
                     printMsg(currWin, "Could not handle: %s\n", conn->ptr);
-                    nn_conn_buf_skip(conn, strlen(conn->ptr) + 1);
+                    th_conn_buf_skip(conn, strlen(conn->ptr) + 1);
                 }
                 else
                     editState.isError = TRUE;
             }
         }
         else
-        if (cres < 0 || !nn_conn_check(conn))
+        if (cres < 0 || !th_conn_check(conn))
             editState.isError = TRUE;
 
         // Handle user input
@@ -2353,7 +2389,7 @@
 #endif
 
     th_free(optUserNameEnc);
-    nn_conn_free(conn);
+    th_conn_free(conn);
     nn_network_close();
 
     THMSG(1, "Connection terminated.\n");