changeset 417:ccee77fe4525

Fix protocol message parsing.
author Matti Hamalainen <ccr@tnsp.org>
date Thu, 24 May 2012 07:27:28 +0300
parents 8bb69e749d97
children 8ca09a6cca09
files main.c
diffstat 1 files changed, 31 insertions(+), 32 deletions(-) [+]
line wrap: on
line diff
--- a/main.c	Thu May 24 07:02:30 2012 +0300
+++ b/main.c	Thu May 24 07:27:28 2012 +0300
@@ -640,80 +640,79 @@
 
 int nnproto_parse_user(nn_conn_t *conn)
 {
-    static const char *msg = "</USER><MESSAGE>";
-    char *p = conn->ptr;
     BOOL isMine, isIgnored = FALSE;
-    char *s, *t, *userName;
+    char *name, *msg, *t;
 
     /* Find start of the message */
-    s = strstr(p, msg);
-    if (!s) return 1;
-    *s = 0;
-    s += strlen(msg);
+    name = conn->ptr;
+    t = nn_conn_buf_strstr(conn, "</USER><MESSAGE>");
+    if (!t) return 1;
+    *t = 0;
+    msg = conn->ptr;
 
     /* Find end of the message */
-    t = strstr(s, "</MESSAGE>");
+    t = nn_conn_buf_strstr(conn, "</MESSAGE>");
     if (!t) return 3;
     *t = 0;
 
     /* Decode message string */
-    s = nn_decode_str1(s);
-    if (!s) return -1;
+    msg = nn_decode_str1(msg);
+    if (!msg) return -1;
 
     /* Decode username */
-    userName = nn_decode_str1(p);
-    if (!userName)
+    name = nn_decode_str1(name);
+    if (!name)
     {
-        th_free(s);
+        th_free(msg);
         return -2;
     }
 
     /* Check if the username is on our ignore list and
      * that it is not our OWN username!
      */
-    isMine = strcmp(userName, optUserName) == 0;
-    isIgnored = setIgnoreMode && !isMine && checkIgnoreList(userName);
+    isMine = strcmp(name, optUserName) == 0;
+    isIgnored = setIgnoreMode && !isMine && checkIgnoreList(name);
 
     /* Is it a special control message? */
-    if (*s == '/')
+    if (*msg == '/')
     {
         /* Ignore room join/leave messages */
-        if (!optDebug && (strstr(s, "left the room") || strstr(s, "joined the room from")))
+        if (!optDebug && (strstr(msg, "left the room") || strstr(msg, "joined the room from")))
             goto done;
 
-        t = nn_strip_tags(s + 1);
+        t = nn_strip_tags(msg + 1);
         if (!strncmp(t, "BPRV ", 5))
         {
-            char *name, *tmp, *msg, *h;
+            char *in_name, *tmp, *in_msg, *h;
             nn_window_t *win;
             h = nn_decode_str2(t + 1);
 
             if (!strncmp(t, "BPRV from ", 10))
             {
-                name = nn_decode_str2(t + 10);
+                in_name = nn_decode_str2(t + 10);
                 isMine = FALSE;
             }
             else
             {
-                name = nn_decode_str2(t + 8);
+                in_name = nn_decode_str2(t + 8);
                 isMine = TRUE;
             }
 
-            for (tmp = name; *tmp && *tmp != ':'; tmp++);
+            for (tmp = in_name; *tmp && *tmp != ':'; tmp++);
             if (tmp[0] != 0 && tmp[1] == ' ')
-                msg = tmp + 2;
+                in_msg = tmp + 2;
             else
-                msg = "";
+                in_msg = "";
             *tmp = 0;
 
-            isIgnored = setIgnoreMode && checkIgnoreList(name);
-            win = findWindow(name);
+            isIgnored = setIgnoreMode && checkIgnoreList(in_name);
+            win = findWindow(in_name);
 
             if (win != NULL)
             {
                 printMsgF(win, isIgnored ? 0 : LOG_WINDOW,
                     "½5½<½%d½%s½5½>½0½ %s\n",
-                    isMine ? 14 : 15, isMine ? optUserName : name, msg);
+                    isMine ? 14 : 15, isMine ? optUserName : in_name, in_msg);
 
                 printMsgF(NULL, LOG_FILE, "½11½%s½0½\n", h);
             }
@@ -722,7 +721,7 @@
                 printMsgF(NULL, isIgnored ? LOG_FILE : (LOG_WINDOW | LOG_FILE),
                     "½11½%s½0½\n", h);
             }
-            th_free(name);
+            th_free(in_name);
             th_free(h);
         }
         else
@@ -739,17 +738,17 @@
     {
         /* It's a normal message */
         char *h;
-        t = nn_strip_tags(s);
+        t = nn_strip_tags(msg);
         h = nn_decode_str2(t);
         printMsgF(NULL, isIgnored ? LOG_FILE : (LOG_WINDOW | LOG_FILE),
-            "½5½<½%d½%s½5½>½0½ %s\n", isMine ? 14 : 15, userName, h);
+            "½5½<½%d½%s½5½>½0½ %s\n", isMine ? 14 : 15, name, h);
         th_free(h);
         th_free(t);
     }
 
 done:
-    th_free(s);
-    th_free(userName);
+    th_free(msg);
+    th_free(name);
     return 0;
 }