]> code.citadel.org Git - citadel.git/blobdiff - citadel/imap_tools.c
* Use syslog-compatible logging levels in lprintf(); the loglevel chosen
[citadel.git] / citadel / imap_tools.c
index f0299f3bea104cf16b94c3195c141d84bf27ca62..11b5e38a2ba53de295644163f7accfcc16a501cd 100644 (file)
@@ -89,21 +89,29 @@ int imap_parameterize(char **args, char *buf)
 }
 
 /*
- * Convert a struct room to an IMAP-compatible mailbox name.
+ * Convert a struct ctdlroom to an IMAP-compatible mailbox name.
  */
-void imap_mailboxname(char *buf, int bufsize, struct room *qrbuf)
+void imap_mailboxname(char *buf, int bufsize, struct ctdlroom *qrbuf)
 {
        struct floor *fl;
        int i;
+       char buf2[SIZ];
 
        /*
-        * For mailboxes, just do it straight...
+        * For mailboxes, just do it straight.
+        * Do the Cyrus-compatible thing: all private folders are
+        * subfolders of INBOX.
         */
        if (qrbuf->QRflags & QR_MAILBOX) {
                safestrncpy(buf, qrbuf->QRname, bufsize);
                strcpy(buf, &buf[11]);
-               if (!strcasecmp(buf, MAILROOM))
+               if (!strcasecmp(buf, MAILROOM)) {
                        strcpy(buf, "INBOX");
+               }
+               else {
+                       sprintf(buf2, "INBOX|%s", buf);
+                       strcpy(buf, buf2);
+               }
        }
        /*
         * Otherwise, prefix the floor name as a "public folders" moniker
@@ -149,11 +157,16 @@ int imap_roomname(char *rbuf, int bufsize, char *foldername)
 
        /*
         * Convert the crispy idiot's reserved names to our reserved names.
+        * Also handle Cyrus-compatible folder names.
         */
        if (!strcasecmp(foldername, "INBOX")) {
                safestrncpy(rbuf, MAILROOM, bufsize);
                ret = (0 | IR_MAILBOX);
        }
+       else if (!strncasecmp(foldername, "INBOX|", 6)) {
+               safestrncpy(rbuf, &foldername[6], bufsize);
+               ret = (0 | IR_MAILBOX);
+       }
        else if (levels > 1) {
                extract(floorname, foldername, 0);
                strcpy(roomname, &foldername[strlen(floorname)+1]);
@@ -179,7 +192,7 @@ int imap_roomname(char *rbuf, int bufsize, char *foldername)
                ret = (0 | IR_MAILBOX);
        }
 
-       /* Undelimiterizationalize the room name (change '|') */
+       /* Undelimiterizationalisticize the room name (change '|') */
        for (i=0; i<strlen(rbuf); ++i) {
                if (rbuf[i] == '|') rbuf[i] = FDELIM;
        }
@@ -195,7 +208,7 @@ int imap_roomname(char *rbuf, int bufsize, char *foldername)
        }
  ***/
 
-       lprintf(9, "(That translates to \"%s\")\n", rbuf);
+       lprintf(CTDL_DEBUG, "(That translates to \"%s\")\n", rbuf);
        return(ret);
 }
 
@@ -373,3 +386,54 @@ int imap_mailbox_matches_pattern(char *pattern, char *mailboxname)
        }
        return (do_imap_match(mailboxname, pattern) == WILDMAT_TRUE);
 }
+
+
+
+/*
+ * Compare an IMAP date string (date only, no time) to the date found in
+ * a Unix timestamp.
+ */
+int imap_datecmp(char *datestr, time_t msgtime) {
+       char daystr[SIZ];
+       char monthstr[SIZ];
+       char yearstr[SIZ];
+       int i;
+       int day, month, year;
+       int msgday, msgmonth, msgyear;
+       struct tm msgtm;
+
+       if (datestr == NULL) return(0);
+
+       /* Expecting a date in the form dd-Mmm-yyyy */
+       extract_token(daystr, datestr, 0, '-');
+       extract_token(monthstr, datestr, 1, '-');
+       extract_token(yearstr, datestr, 2, '-');
+
+       day = atoi(daystr);
+       year = atoi(yearstr);
+       month = 0;
+       for (i=0; i<12; ++i) {
+               if (!strcasecmp(monthstr, ascmonths[i])) {
+                       month = i;
+               }
+       }
+
+       /* Extract day/month/year from message timestamp */
+       memcpy(&msgtm, localtime(&msgtime), sizeof(struct tm));
+       msgday = msgtm.tm_mday;
+       msgmonth = msgtm.tm_mon;
+       msgyear = msgtm.tm_year + 1900;
+
+       /* Now start comparing */
+
+       if (year < msgyear) return(+1);
+       if (year > msgyear) return(-1);
+
+       if (month < msgmonth) return(+1);
+       if (month > msgmonth) return(-1);
+
+       if (day < msgday) return(+1);
+       if (day > msgday) return(-1);
+
+       return(0);
+}