imap: Added a helper function for upcoming untagged response filtering
authorJiri Hruska <jirka@fud.cz>
Tue, 26 Feb 2013 17:52:19 +0000 (18:52 +0100)
committerSteve Holme <steve_holme@hotmail.com>
Tue, 26 Feb 2013 20:23:13 +0000 (20:23 +0000)
RFC 3501 states that "the client MUST be prepared to accept any response
at all times" yet we assume anything received with "* " at the beginning
is the untagged response we want.

Introduced a helper function that checks whether the input looks like a
response to specified command, so that we may filter the ones we are
interested in according to the current state.

lib/imap.c

index 7738e63..68f765a 100644 (file)
@@ -323,6 +323,41 @@ static char* imap_atom(const char* str)
   return newstr;
 }
 
+/* Determines whether the untagged response is related to a specified
+   command by checking if it is in format "* <command-name> ..." or
+   "* <number> <command-name> ...". The "* " marker is assumed to have
+   already been checked by the caller. */
+static bool imap_matchresp(const char *line, size_t len, const char *cmd)
+{
+  const char *end = line + len;
+  size_t cmd_len = strlen(cmd);
+
+  /* Skip the untagged response marker */
+  line += 2;
+
+  /* Do we have a number after the marker? */
+  if(line < end && ISDIGIT(*line)) {
+    /* Skip the number */
+    do
+      line++;
+    while(line < end && ISDIGIT(*line));
+
+    /* Do we have the space character? */
+    if(line == end || *line != ' ')
+      return FALSE;
+
+    line++;
+  }
+
+  /* Does the command name match and is it followed by a space character or at
+     the end of line? */
+  if(line + cmd_len <= end && Curl_raw_nequal(line, cmd, cmd_len) &&
+     (line[cmd_len] == ' ' || line + cmd_len == end))
+    return TRUE;
+
+  return FALSE;
+}
+
 /* Function that checks whether the given string is a valid tagged, untagged
    or continuation response which can be processed by the response handler. */
 static bool imap_endofresp(struct connectdata *conn, char *line, size_t len,