Improve IMAP debug messages
[dockapps/wmnotify.git] / src / imap.c
index eae62ce..1c33b80 100644 (file)
@@ -29,6 +29,7 @@
 #include "network.h"
 #include "imap.h"
 
+#define DEBUG_RESPONSE_LINE_INDENT "    "
 
 #define IMAP4_ENDL "\r\n"      /* CRLF */
 
@@ -60,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;
@@ -117,10 +141,6 @@ get_packet:
                goto error;
        }
 
-       if (wmnotify_infos.debug) {
-               printf("%s", rx_buffer);
-       }
-
        /*
         * Converting the last CRLF into a LF followed by a NULL termination
         * character.
@@ -128,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: