Trivial commit to test automated builds
[citadel.git] / citadel / modules / imap / imap_tools.c
index 28fb95948eca6528f339ecb46da41449b876f36c..7d7c0805cfebf6f8babc5d81a83e4a0b5c8d7877 100644 (file)
@@ -1,10 +1,10 @@
 /*
  * Utility functions for the IMAP module.
  *
- * Copyright (c) 2001-2009 by the citadel.org team and others, except for
+ * Copyright (c) 2001-2017 by the citadel.org team and others, except for
  * most of the UTF7 and UTF8 handling code which was lifted from Evolution.
  *
- * This program is free software; you can redistribute it and/or modify
+ * This program is open source 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 3 of the License, or
  * (at your option) any later version.
 #include "imap_tools.h"
 #include "ctdl_module.h"
 
-#ifndef HAVE_SNPRINTF
-#include "snprintf.h"
-#endif
-
 /* String handling helpers */
 
 /* This code uses some pretty nasty string manipulation. To make everything
@@ -225,7 +221,7 @@ static char* toimap(char* destp, char* destend, char* src)
 
        *destp = 0;
        string_init(&dest, destp, destend-destp);
-       /* syslog(LOG_DEBUG, "toimap %s\r\n", src); */
+       /* syslog(LOG_DEBUG, "toimap %s", src); */
 
        for (;;)
        {
@@ -291,7 +287,7 @@ static char* toimap(char* destp, char* destend, char* src)
 
        if (state == 1)
                utf7_closeb64(&dest, v, i);
-       /* syslog(LOG_DEBUG, "    -> %s\r\n", destp); */
+       /* syslog(LOG_DEBUG, "    -> %s", destp); */
        return string_end(&dest);
 }
 
@@ -309,7 +305,7 @@ static char* fromimap(char* destp, char* destend, const char* src)
 
        *destp = 0;
        string_init(&dest, destp, destend-destp);
-       /* syslog(LOG_DEBUG, "fromimap %s\r\n", src); */
+       /* syslog(LOG_DEBUG, "fromimap %s", src); */
 
        do {
                c = *p++;
@@ -367,12 +363,11 @@ static char* fromimap(char* destp, char* destend, const char* src)
                        }
        } while (c != '\0');
 
-       /* syslog(LOG_DEBUG, "      -> %s\r\n", destp); */
+       /* syslog(LOG_DEBUG, "      -> %s", destp); */
        return string_end(&dest);
 }
 
 /* Undoes the special character conversion. */
-
 static int cfrommap(int c)
 {
        switch (c)
@@ -384,10 +379,7 @@ static int cfrommap(int c)
 }
 
 
-
-
 /* Break a command down into tokens, unquoting any escaped characters. */
-
 void MakeStringOf(StrBuf *Buf, int skip)
 {
        int i;
@@ -555,12 +547,12 @@ int imap_parameterize(citimap_command *Cmd)
 
 
 /* Convert a struct ctdlroom to an IMAP-compatible mailbox name. */
-
-void imap_mailboxname(char *buf, int bufsize, struct ctdlroom *qrbuf)
+long imap_mailboxname(char *buf, int bufsize, struct ctdlroom *qrbuf)
 {
        char* bufend = buf+bufsize;
        struct floor *fl;
        char* p = buf;
+       const char *pend;
 
        /* For mailboxes, just do it straight.
         * Do the Cyrus-compatible thing: all private folders are
@@ -569,13 +561,17 @@ void imap_mailboxname(char *buf, int bufsize, struct ctdlroom *qrbuf)
        if (qrbuf->QRflags & QR_MAILBOX)
        {
                if (strcasecmp(qrbuf->QRname+11, MAILROOM) == 0)
-                       p = toimap(p, bufend, "INBOX");
+               {
+                       pend = toimap(p, bufend, "INBOX");
+                       return pend - buf;
+               }
                else
                {
                        p = toimap(p, bufend, "INBOX");
                        if (p < bufend)
                                *p++ = '/';
-                       p = toimap(p, bufend, qrbuf->QRname+11);
+                       pend = toimap(p, bufend, qrbuf->QRname+11);
+                       return pend - buf;
                }
        }
        else
@@ -586,7 +582,8 @@ void imap_mailboxname(char *buf, int bufsize, struct ctdlroom *qrbuf)
                p = toimap(p, bufend, fl->f_name);
                if (p < bufend)
                        *p++ = '/';
-               p = toimap(p, bufend, qrbuf->QRname);
+               pend = toimap(p, bufend, qrbuf->QRname);
+               return pend - buf;
        }
 }
 
@@ -601,7 +598,6 @@ void imap_mailboxname(char *buf, int bufsize, struct ctdlroom *qrbuf)
  * including IR_MAILBOX if we're dealing with a personal room.
  *
  */
-
 int imap_roomname(char *rbuf, int bufsize, const char *foldername)
 {
        int levels;
@@ -680,37 +676,10 @@ int imap_roomname(char *rbuf, int bufsize, const char *foldername)
        ret = (0 | IR_MAILBOX);
 
 exit:
-       syslog(LOG_DEBUG, "(That translates to \"%s\")\n", rbuf);
+       syslog(LOG_DEBUG, "(That translates to \"%s\")", rbuf);
        return(ret);
 }
 
-/*
- * Output a struct internet_address_list in the form an IMAP client wants
- */
-void imap_ial_out(struct internet_address_list *ialist)
-{
-       struct internet_address_list *iptr;
-
-       if (ialist == NULL) {
-               IAPuts("NIL");
-               return;
-       }
-       IAPuts("(");
-
-       for (iptr = ialist; iptr != NULL; iptr = iptr->next) {
-               IAPuts("(");
-               plain_imap_strout(iptr->ial_name);
-               IAPuts(" NIL ");
-               plain_imap_strout(iptr->ial_user);
-               IAPuts(" ");
-               plain_imap_strout(iptr->ial_node);
-               IAPuts(")");
-       }
-
-       IAPuts(")");
-}
-
-
 
 /*
  * Determine whether the supplied string is a valid message set.
@@ -839,7 +808,10 @@ star:
                                }
                                return WILDMAT_TRUE;
                        }
-                       while (!IsEmptyStr(text) && (*(text - 1) != WILDMAT_DELIM)) {
+                       while (!IsEmptyStr(text) &&
+                              /* make sure text - 1 isn't before lcase_p */
+                              ((text == lcase_text) || (*(text - 1) != WILDMAT_DELIM)))
+                       {
                                if ((matched = do_imap_match(text++, p))
                                   != WILDMAT_FALSE) {
                                        return matched;
@@ -854,7 +826,6 @@ star:
 }
 
 
-
 /*
  * Support function for mailbox pattern name matching in LIST and LSUB
  * Returns nonzero if the supplied mailbox name matches the supplied pattern.
@@ -924,9 +895,6 @@ int imap_datecmp(const char *datestr, time_t msgtime) {
 }
 
 
-
-
-
 void IAPrintf(const char *Format, ...)
 {
        va_list arg_ptr;
@@ -936,11 +904,13 @@ void IAPrintf(const char *Format, ...)
        va_end(arg_ptr);
 }
 
+
 void iaputs(const char *Str, long Len)
 {
        StrBufAppendBufPlain(IMAP->Reply, Str, Len, 0);
 }
 
+
 void ireply(const char *Msg, long len)
 {
        citimap *Imap = IMAP;
@@ -957,6 +927,7 @@ void ireply(const char *Msg, long len)
        
 }
 
+
 void IReplyPrintf(const char *Format, ...)
 {
        citimap *Imap = IMAP;
@@ -979,46 +950,8 @@ void IReplyPrintf(const char *Format, ...)
 }
 
 
-
 /* Output a string to the IMAP client, either as a literal or quoted.
  * (We do a literal if it has any double-quotes or backslashes.) */
-
-void plain_imap_strout(char *buf)
-{
-       int i;
-       int is_literal = 0;
-       long Len;
-       citimap *Imap = IMAP;
-
-       if (buf == NULL) {      /* yeah, we handle this */
-               IAPuts("NIL");
-               return;
-       }
-
-       Len = strlen(buf);
-       for (i = 0; i < Len; ++i) {
-               if ((buf[i] == '\"') || (buf[i] == '\\'))
-                       is_literal = 1;
-       }
-
-       if (is_literal) {
-               StrBufAppendPrintf(Imap->Reply, "{%ld}\r\n", Len);
-               StrBufAppendBufPlain(Imap->Reply, buf, Len, 0);
-       } else {
-               StrBufAppendBufPlain(Imap->Reply, 
-                                    HKEY("\""), 0);
-               StrBufAppendBufPlain(Imap->Reply, 
-                                    buf, Len, 0);
-               StrBufAppendBufPlain(Imap->Reply, 
-                                    HKEY("\""), 0);
-       }
-}
-
-
-/* Output a string to the IMAP client, either as a literal or quoted.
- * (We do a literal if it has any double-quotes or backslashes.) */
-
-
 void IPutStr(const char *Msg, long Len)
 {
        int i;