SMTP-Relaying: implement a method to choose a relay by sender email address or username
[citadel.git] / citadel / modules / imap / imap_fetch.c
index 86102bf8ed9a2f4ce506ffd7e702cf48b84acac5..396e6070c6808516cdee483925cec7f8bbad8589 100644 (file)
@@ -2,9 +2,9 @@
  * Implements the FETCH command in IMAP.
  * This is a good example of the protocol's gratuitous complexity.
  *
- * Copyright (c) 2001-2009 by the citadel.org team
+ * Copyright (c) 2001-2011 by the citadel.org team
  *
- *  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.
@@ -132,14 +132,14 @@ void imap_fetch_internaldate(struct CtdlMessage *msg) {
  *     "RFC822.TEXT"   body only (without leading blank line)
  */
 void imap_fetch_rfc822(long msgnum, const char *whichfmt) {
-       citimap *Imap = IMAP;
+       CitContext *CCC = CC;
+       citimap *Imap = CCCIMAP;
        const char *ptr = NULL;
        size_t headers_size, text_size, total_size;
        size_t bytes_to_send = 0;
        struct MetaData smi;
        int need_to_rewrite_metadata = 0;
        int need_body = 0;
-       CitContext *CCC = CC;
 
        /* Determine whether this particular fetch operation requires
         * us to fetch the message body from disk.  If not, we can save
@@ -190,7 +190,7 @@ void imap_fetch_rfc822(long msgnum, const char *whichfmt) {
                CCC->redirect_buffer = NewStrBufPlain(NULL, SIZ);
                CtdlOutputMsg(msgnum, MT_RFC822,
                        (need_body ? HEADERS_ALL : HEADERS_FAST),
-                       0, 1, NULL, SUPPRESS_ENV_TO
+                       0, 1, NULL, SUPPRESS_ENV_TO, NULL, NULL
                );
                if (!need_body) IAPuts("\r\n"); /* extra trailing newline */
                Imap->cached_rfc822 = CCC->redirect_buffer;
@@ -209,8 +209,6 @@ void imap_fetch_rfc822(long msgnum, const char *whichfmt) {
         * intervening blank line to be part of the headers, not the text.
         */
        headers_size = 0;
-       text_size = 0;
-       total_size = 0;
 
        if (need_body) {
                StrBuf *Line = NewStrBuf();
@@ -240,11 +238,11 @@ void imap_fetch_rfc822(long msgnum, const char *whichfmt) {
                text_size = 0;
        }
 
-       CtdlLogPrintf(CTDL_DEBUG, 
-               "RFC822: headers=" SIZE_T_FMT 
-               ", text=" SIZE_T_FMT
-               ", total=" SIZE_T_FMT "\n",
-               headers_size, text_size, total_size);
+       IMAP_syslog(LOG_DEBUG, 
+                   "RFC822: headers=" SIZE_T_FMT 
+                   ", text=" SIZE_T_FMT
+                   ", total=" SIZE_T_FMT,
+                   headers_size, text_size, total_size);
 
        if (!strcasecmp(whichfmt, "RFC822.SIZE")) {
                IAPrintf("RFC822.SIZE " SIZE_T_FMT, total_size);
@@ -284,52 +282,53 @@ void imap_load_part(char *name, char *filename, char *partnum, char *disp,
                    void *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
                    char *cbid, void *cbuserdata)
 {
+       struct CitContext *CCC = CC;
        char mimebuf2[SIZ];
-       char *desired_section;
+       StrBuf *desired_section;
 
-       desired_section = (char *)cbuserdata;
-       CtdlLogPrintf(CTDL_DEBUG, "imap_load_part() looking for %s, found %s\n",
-               desired_section,
-               partnum
-       );
+       desired_section = (StrBuf *)cbuserdata;
+       IMAP_syslog(LOG_DEBUG, "imap_load_part() looking for %s, found %s",
+                   ChrPtr(desired_section),
+                   partnum
+               );
 
-       if (!strcasecmp(partnum, desired_section)) {
-               iaputs(content, length);
+       if (!strcasecmp(partnum, ChrPtr(desired_section))) {
+               client_write(content, length);
        }
 
        snprintf(mimebuf2, sizeof mimebuf2, "%s.MIME", partnum);
 
-       if (!strcasecmp(desired_section, mimebuf2)) {
-               IAPuts("Content-type: ");
-               _iaputs(cbtype);
+       if (!strcasecmp(ChrPtr(desired_section), mimebuf2)) {
+               client_write(HKEY("Content-type: "));
+               client_write(cbtype, strlen(cbtype));
                if (!IsEmptyStr(cbcharset)) {
-                       IAPuts("; charset=\"");
-                       _iaputs(cbcharset);
-                       IAPuts("\"");
+                       client_write(HKEY("; charset=\""));
+                       client_write(cbcharset, strlen(cbcharset));
+                       client_write(HKEY("\""));
                }
                if (!IsEmptyStr(name)) {
-                       IAPuts("; name=\"");
-                       _iaputs(name);
-                       IAPuts("\"");
+                       client_write(HKEY("; name=\""));
+                       client_write(name, strlen(name));
+                       client_write(HKEY("\""));
                }
-               IAPuts("\r\n");
+               client_write(HKEY("\r\n"));
                if (!IsEmptyStr(encoding)) {
-                       IAPuts("Content-Transfer-Encoding: ");
-                       _iaputs(encoding);
-                       IAPuts("\r\n");
+                       client_write(HKEY("Content-Transfer-Encoding: "));
+                       client_write(encoding, strlen(encoding));
+                       client_write(HKEY("\r\n"));
                }
                if (!IsEmptyStr(encoding)) {
-                       IAPuts("Content-Disposition: ");
-                       _iaputs(disp);
+                       client_write(HKEY("Content-Disposition: "));
+                       client_write(disp, strlen(disp));
                
                        if (!IsEmptyStr(filename)) {
-                               IAPuts("; filename=\"");
-                               _iaputs(filename);
-                               IAPuts("\"");
+                               client_write(HKEY("; filename=\""));
+                               client_write(filename, strlen(filename));
+                               client_write(HKEY("\""));
                        }
-                       IAPuts("\r\n");
+                       client_write(HKEY("\r\n"));
                }
-               IAPrintf("Content-Length: %ld\r\n\r\n", (long)length);
+               cprintf("Content-Length: %ld\r\n\r\n", (long)length);
        }
 }
 
@@ -603,7 +602,7 @@ void imap_strip_headers(StrBuf *section) {
                StrBufSipLine(Line, CCC->redirect_buffer, &Ptr);
 
                if (!isspace(ChrPtr(Line)[0])) {
-                       ok = 0;
+
                        if (doing_headers == 0) ok = 1;
                        else {
                                /* we're supposed to print all headers that are not matching the filter list */
@@ -663,7 +662,7 @@ void imap_fetch_body(long msgnum, ConstStr item, int is_peek) {
        int need_body = 1;
        int burn_the_cache = 0;
        CitContext *CCC = CC;
-       citimap *Imap = IMAP;
+       citimap *Imap = CCCIMAP;
 
        /* extract section */
        section = NewStrBufPlain(CKEY(item));
@@ -671,8 +670,8 @@ void imap_fetch_body(long msgnum, ConstStr item, int is_peek) {
        if (strchr(ChrPtr(section), '[') != NULL) {
                StrBufStripAllBut(section, '[', ']');
        }
-       CtdlLogPrintf(CTDL_DEBUG, "Section is: [%s]\n", 
-             (StrLength(section) == 0) ? "(empty)" : ChrPtr(section)
+       IMAP_syslog(LOG_DEBUG, "Section is: [%s]", 
+                   (StrLength(section) == 0) ? "(empty)" : ChrPtr(section)
        );
 
        /* Burn the cache if we don't have the same section of the 
@@ -705,7 +704,7 @@ void imap_fetch_body(long msgnum, ConstStr item, int is_peek) {
                is_partial = 1;
        }
        if ( (is_partial == 1) && (StrLength(partial) > 0) ) {
-               CtdlLogPrintf(CTDL_DEBUG, "Partial is <%s>\n", ChrPtr(partial));
+               IMAP_syslog(LOG_DEBUG, "Partial is <%s>", ChrPtr(partial));
        }
 
        if (Imap->cached_body == NULL) {
@@ -754,10 +753,10 @@ void imap_fetch_body(long msgnum, ConstStr item, int is_peek) {
         */
        else {
                mime_parser(msg->cm_fields['M'], NULL,
-                               *imap_load_part, NULL, NULL,
-                               ChrPtr(section),
-                               1
-               );
+                           *imap_load_part, NULL, NULL,
+                           section,
+                           1
+                       );
        }
 
        if (loading_body_now) {
@@ -1133,19 +1132,18 @@ void imap_do_fetch(citimap_command *Cmd) {
 /* debug output the parsed vector */
        {
                int i;
-               CtdlLogPrintf(CTDL_DEBUG, "----- %ld params \n",
-                             Cmd->num_parms);
+               IMAP_syslog(LOG_DEBUG, "----- %ld params", Cmd->num_parms);
 
        for (i=0; i < Cmd->num_parms; i++) {
                if (Cmd->Params[i].len != strlen(Cmd->Params[i].Key))
-                       CtdlLogPrintf(CTDL_DEBUG, "*********** %ld != %ld : %s\n",
-                                     Cmd->Params[i].len, 
-                                     strlen(Cmd->Params[i].Key),
-                                     Cmd->Params[i].Key);
+                       IMAP_syslog(LOG_DEBUG, "*********** %ld != %ld : %s",
+                                   Cmd->Params[i].len, 
+                                   strlen(Cmd->Params[i].Key),
+                                   Cmd->Params[i].Key);
                else
-                       CtdlLogPrintf(CTDL_DEBUG, "%ld : %s\n",
-                                     Cmd->Params[i].len, 
-                                     Cmd->Params[i].Key);
+                       IMAP_syslog(LOG_DEBUG, "%ld : %s",
+                                   Cmd->Params[i].len, 
+                                   Cmd->Params[i].Key);
        }}
 
 #endif
@@ -1259,7 +1257,6 @@ int imap_extract_data_items(citimap_command *Cmd)
        int nArgs;
        int nest = 0;
        const char *pch, *end;
-       long initial_len;
 
        /* Convert all whitespace to ordinary space characters. */
        pch = ChrPtr(Cmd->CmdBuf);
@@ -1294,7 +1291,6 @@ int imap_extract_data_items(citimap_command *Cmd)
         */
        nArgs = StrLength(Cmd->CmdBuf) / 10 + 10;
        nArgs = CmdAdjust(Cmd, nArgs, 0);
-       initial_len = StrLength(Cmd->CmdBuf);
        Cmd->num_parms = 0;
        Cmd->Params[Cmd->num_parms].Key = pch = ChrPtr(Cmd->CmdBuf);
        end = Cmd->Params[Cmd->num_parms].Key + StrLength(Cmd->CmdBuf);
@@ -1468,7 +1464,7 @@ void imap_uidfetch(int num_parms, ConstStr *Params) {
 
        MakeStringOf(Cmd.CmdBuf, 4);
 #if 0
-       CtdlLogPrintf(CTDL_DEBUG, "-------%s--------\n", ChrPtr(Cmd.CmdBuf));
+       IMAP_syslog(LOG_DEBUG, "-------%s--------", ChrPtr(Cmd.CmdBuf));
 #endif
        num_items = imap_extract_data_items(&Cmd);
        if (num_items < 1) {