NULL terminate strncpy destination string
[dockapps/wmnotify.git] / src / imap.c
index 61f83b8..1c33b80 100644 (file)
@@ -3,19 +3,7 @@
  *
  * Copyright (C) 2003 Hugo Villeneuve <hugo@hugovil.com>
  *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
+ * This file is released under the GPLv2
  */
 
 /* Define filename_M */
@@ -41,6 +29,7 @@
 #include "network.h"
 #include "imap.h"
 
+#define DEBUG_RESPONSE_LINE_INDENT "    "
 
 #define IMAP4_ENDL "\r\n"      /* CRLF */
 
@@ -72,6 +61,29 @@ static int unseen_string_found;
 extern char tx_buffer[WMNOTIFY_BUFSIZE + 1];
 extern char rx_buffer[WMNOTIFY_BUFSIZE + 1];
 
+static void
+IMAP4_debug_response(const char *buf, int len)
+{
+       int k, l;
+
+       k = strlen(rx_buffer);
+
+       if (k >= sizeof(rx_buffer)) {
+               k = 0;
+       }
+
+       printf(DEBUG_RESPONSE_LINE_INDENT);
+       for (l = 0; l < k; l++) {
+               if ((rx_buffer[l] == '\n') && (l < (k - 1))) {
+                       printf("\n" DEBUG_RESPONSE_LINE_INDENT);
+               }
+               else
+               {
+                       printf("%c", rx_buffer[l]);
+               }
+       }
+}
+
 static int IMAP4_ReceiveResponse(void)
 {
        int len;
@@ -129,13 +141,6 @@ get_packet:
                goto error;
        }
 
-       if (wmnotify_infos.debug) {
-               printf("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
-               printf("IMAP4 Server Response (size %d bytes):\n", len);
-               printf("%s", rx_buffer);
-               printf("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
-       }
-
        /*
         * Converting the last CRLF into a LF followed by a NULL termination
         * character.
@@ -143,6 +148,10 @@ get_packet:
        rx_buffer[len - 2] = '\n';
        rx_buffer[len - 1] = '\0';
 
+       if (wmnotify_infos.debug) {
+               IMAP4_debug_response(rx_buffer, len);
+       }
+
        /*
         * Check the Server Completion Response returned by the IMAP4 server.
         * There are currently three Server Completion Responses codes:
@@ -257,6 +266,8 @@ static int IMAP4_SendCommand(int argc, char *argv[])
 {
        int len;
        int i;
+       char debug_buffer[sizeof(tx_buffer)];
+       int debug_len;
 
        /* Adding Transaction Label. */
        tlabel++;
@@ -265,16 +276,26 @@ static int IMAP4_SendCommand(int argc, char *argv[])
        len += sprintf(tx_buffer + len, "%d", tlabel);
        tlabel_len = len;
 
+       debug_len = 0;
+       debug_len += sprintf(debug_buffer + debug_len, "%s", tx_buffer);
+
        /* Adding command and it's arguments. */
-       for (i = 0; i < argc; i++)
+       for (i = 0; i < argc; i++) {
                len += sprintf(tx_buffer + len, " %s", argv[i]);
 
+               if (STREQ(argv[0], IMAP4_CMD_LOGIN) && (i == 2)) {
+                       debug_len += sprintf(debug_buffer + debug_len,
+                                            " XXXXXXXX");
+               }
+               else {
+                       debug_len += sprintf(debug_buffer + debug_len,
+                                            " %s", argv[i]);
+               }
+       }
+
        if (wmnotify_infos.debug) {
-               tx_buffer[len] = '\0';
-               printf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
-               printf("IMAP4 Client Command (size %d bytes):\n%s\n", len,
-                      tx_buffer);
-               printf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
+               debug_buffer[debug_len] = '\0';
+               printf("%s\n", debug_buffer);
        }
 
        /* Adding termination characters. */
@@ -312,7 +333,7 @@ int IMAP4_CheckForNewMail(void)
        status = IMAP4_SendCommand(3, argv);
        if (status != EXIT_SUCCESS) {
                new_messages = -1;
-               goto imap4_logout;
+               goto imap4_connection_terminate;
        }
 
        /* Selecting the mailbox first. */
@@ -351,6 +372,7 @@ imap4_logout:
        if (status != EXIT_SUCCESS)
                new_messages = -1;
 
+imap4_connection_terminate:
        status = ConnectionTerminate();
        if (status != EXIT_SUCCESS)
                new_messages = -1;