Improve IMAP debug messages
authorHugo Villeneuve <hugo@hugovil.com>
Sat, 14 Jan 2017 16:02:46 +0000 (11:02 -0500)
committerHugo Villeneuve <hugo@hugovil.com>
Sat, 14 Jan 2017 16:05:48 +0000 (11:05 -0500)
src/imap.c

index 13a3b0b..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;
@@ -125,7 +149,7 @@ get_packet:
        rx_buffer[len - 1] = '\0';
 
        if (wmnotify_infos.debug) {
-               printf("%s", rx_buffer);
+               IMAP4_debug_response(rx_buffer, len);
        }
 
        /*