Back to index

citadel  8.12
imap_misc.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 1987-2011 by the citadel.org team
00003  *
00004  * This program is open source software; you can redistribute it and/or modify
00005  * it under the terms of the GNU General Public License as published by
00006  * the Free Software Foundation; either version 3 of the License, or
00007  * (at your option) any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00017  */
00018 
00019 
00020 #include "sysdep.h"
00021 #include <stdlib.h>
00022 #include <unistd.h>
00023 #include <stdio.h>
00024 #include <fcntl.h>
00025 #include <signal.h>
00026 #include <pwd.h>
00027 #include <errno.h>
00028 #include <sys/types.h>
00029 
00030 #if TIME_WITH_SYS_TIME
00031 # include <sys/time.h>
00032 # include <time.h>
00033 #else
00034 # if HAVE_SYS_TIME_H
00035 #  include <sys/time.h>
00036 # else
00037 #  include <time.h>
00038 # endif
00039 #endif
00040 
00041 #include <sys/wait.h>
00042 #include <ctype.h>
00043 #include <string.h>
00044 #include <limits.h>
00045 #include <libcitadel.h>
00046 #include "citadel.h"
00047 #include "server.h"
00048 #include "sysdep_decls.h"
00049 #include "citserver.h"
00050 #include "support.h"
00051 #include "config.h"
00052 #include "user_ops.h"
00053 #include "database.h"
00054 #include "msgbase.h"
00055 #include "internet_addressing.h"
00056 #include "serv_imap.h"
00057 #include "imap_tools.h"
00058 #include "imap_fetch.h"
00059 #include "imap_misc.h"
00060 #include "genstamp.h"
00061 #include "ctdl_module.h"
00062 
00063 
00064 
00065 
00066 
00067 /*
00068  * imap_copy() calls imap_do_copy() to do its actual work, once it's
00069  * validated and boiled down the request a bit.  (returns 0 on success)
00070  */
00071 int imap_do_copy(const char *destination_folder) {
00072        citimap *Imap = IMAP;
00073        int i;
00074        char roomname[ROOMNAMELEN];
00075        struct ctdlroom qrbuf;
00076        long *selected_msgs = NULL;
00077        int num_selected = 0;
00078 
00079        if (Imap->num_msgs < 1) {
00080               return(0);
00081        }
00082 
00083        i = imap_grabroom(roomname, destination_folder, 1);
00084        if (i != 0) return(i);
00085 
00086        /*
00087         * Copy all the message pointers in one shot.
00088         */
00089        selected_msgs = malloc(sizeof(long) * Imap->num_msgs);
00090        if (selected_msgs == NULL) return(-1);
00091 
00092        for (i = 0; i < Imap->num_msgs; ++i) {
00093               if (Imap->flags[i] & IMAP_SELECTED) {
00094                      selected_msgs[num_selected++] = Imap->msgids[i];
00095               }
00096        }
00097 
00098        if (num_selected > 0) {
00099               CtdlSaveMsgPointersInRoom(roomname, selected_msgs, num_selected, 1, NULL, 0);
00100        }
00101        free(selected_msgs);
00102 
00103        /* Don't bother wasting any more time if there were no messages. */
00104        if (num_selected == 0) {
00105               return(0);
00106        }
00107 
00108        /* Enumerate lists of messages for which flags are toggled */
00109        long *seen_yes = NULL;
00110        int num_seen_yes = 0;
00111        long *seen_no = NULL;
00112        int num_seen_no = 0;
00113        long *answ_yes = NULL;
00114        int num_answ_yes = 0;
00115        long *answ_no = NULL;
00116        int num_answ_no = 0;
00117 
00118        seen_yes = malloc(num_selected * sizeof(long));
00119        seen_no = malloc(num_selected * sizeof(long));
00120        answ_yes = malloc(num_selected * sizeof(long));
00121        answ_no = malloc(num_selected * sizeof(long));
00122 
00123        for (i = 0; i < Imap->num_msgs; ++i) {
00124               if (Imap->flags[i] & IMAP_SELECTED) {
00125                      if (Imap->flags[i] & IMAP_SEEN) {
00126                             seen_yes[num_seen_yes++] = Imap->msgids[i];
00127                      }
00128                      if ((Imap->flags[i] & IMAP_SEEN) == 0) {
00129                             seen_no[num_seen_no++] = Imap->msgids[i];
00130                      }
00131                      if (Imap->flags[i] & IMAP_ANSWERED) {
00132                             answ_yes[num_answ_yes++] = Imap->msgids[i];
00133                      }
00134                      if ((Imap->flags[i] & IMAP_ANSWERED) == 0) {
00135                             answ_no[num_answ_no++] = Imap->msgids[i];
00136                      }
00137               }
00138        }
00139 
00140        /* Set the flags... */
00141        i = CtdlGetRoom(&qrbuf, roomname);
00142        if (i == 0) {
00143               CtdlSetSeen(seen_yes, num_seen_yes, 1, ctdlsetseen_seen, NULL, &qrbuf);
00144               CtdlSetSeen(seen_no, num_seen_no, 0, ctdlsetseen_seen, NULL, &qrbuf);
00145               CtdlSetSeen(answ_yes, num_answ_yes, 1, ctdlsetseen_answered, NULL, &qrbuf);
00146               CtdlSetSeen(answ_no, num_answ_no, 0, ctdlsetseen_answered, NULL, &qrbuf);
00147        }
00148 
00149        free(seen_yes);
00150        free(seen_no);
00151        free(answ_yes);
00152        free(answ_no);
00153 
00154        return(0);
00155 }
00156 
00157 
00158 /*
00159  * Output the [COPYUID xxx yyy] response code required by RFC2359
00160  * to tell the client the UID's of the messages that were copied (if any).
00161  * We are assuming that the IMAP_SELECTED flag is still set on any relevant
00162  * messages in our source room.  Since the Citadel system uses UID's that
00163  * are both globally unique and persistent across a room-to-room copy, we
00164  * can get this done quite easily.
00165  */
00166 void imap_output_copyuid_response(void) {
00167        citimap *Imap = IMAP;
00168        int i;
00169        int num_output = 0;
00170   
00171        for (i = 0; i < Imap->num_msgs; ++i) {
00172               if (Imap->flags[i] & IMAP_SELECTED) {
00173                      ++num_output;
00174                      if (num_output == 1) {
00175                             IAPuts("[COPYUID ");
00176                      }
00177                      else if (num_output > 1) {
00178                             IAPuts(",");
00179                      }
00180                      IAPrintf("%ld", Imap->msgids[i]);
00181               }
00182        }
00183        if (num_output > 0) {
00184               IAPuts("] ");
00185        }
00186 }
00187 
00188 
00189 /*
00190  * This function is called by the main command loop.
00191  */
00192 void imap_copy(int num_parms, ConstStr *Params) {
00193        int ret;
00194 
00195        if (num_parms != 4) {
00196               IReply("BAD invalid parameters");
00197               return;
00198        }
00199 
00200        if (imap_is_message_set(Params[2].Key)) {
00201               imap_pick_range(Params[2].Key, 0);
00202        }
00203        else {
00204               IReply("BAD invalid parameters");
00205               return;
00206        }
00207 
00208        ret = imap_do_copy(Params[3].Key);
00209        if (!ret) {
00210               IAPrintf("%s OK ", Params[0].Key);
00211               imap_output_copyuid_response();
00212               IAPuts("COPY completed\r\n");
00213        }
00214        else {
00215               IReplyPrintf("NO COPY failed (error %d)", ret);
00216        }
00217 }
00218 
00219 /*
00220  * This function is called by the main command loop.
00221  */
00222 void imap_uidcopy(int num_parms, ConstStr *Params) {
00223 
00224        if (num_parms != 5) {
00225               IReply("BAD invalid parameters");
00226               return;
00227        }
00228 
00229        if (imap_is_message_set(Params[3].Key)) {
00230               imap_pick_range(Params[3].Key, 1);
00231        }
00232        else {
00233               IReply("BAD invalid parameters");
00234               return;
00235        }
00236 
00237        if (imap_do_copy(Params[4].Key) == 0) {
00238               IAPrintf("%s OK ", Params[0].Key);
00239               imap_output_copyuid_response();
00240               IAPuts("UID COPY completed\r\n");
00241        }
00242        else {
00243               IReply("NO UID COPY failed");
00244        }
00245 }
00246 
00247 
00248 /*
00249  * imap_do_append_flags() is called by imap_append() to set any flags that
00250  * the client specified at append time.
00251  *
00252  * FIXME find a way to do these in bulk so we don't max out our db journal
00253  */
00254 void imap_do_append_flags(long new_msgnum, char *new_message_flags) {
00255        char flags[32];
00256        char this_flag[sizeof flags];
00257        int i;
00258 
00259        if (new_message_flags == NULL) return;
00260        if (IsEmptyStr(new_message_flags)) return;
00261 
00262        safestrncpy(flags, new_message_flags, sizeof flags);
00263 
00264        for (i=0; i<num_tokens(flags, ' '); ++i) {
00265               extract_token(this_flag, flags, i, ' ', sizeof this_flag);
00266               if (this_flag[0] == '\\') strcpy(this_flag, &this_flag[1]);
00267               if (!strcasecmp(this_flag, "Seen")) {
00268                      CtdlSetSeen(&new_msgnum, 1, 1, ctdlsetseen_seen,
00269                             NULL, NULL);
00270               }
00271               if (!strcasecmp(this_flag, "Answered")) {
00272                      CtdlSetSeen(&new_msgnum, 1, 1, ctdlsetseen_answered,
00273                             NULL, NULL);
00274               }
00275        }
00276 }
00277 
00278 
00279 /*
00280  * This function is called by the main command loop.
00281  */
00282 void imap_append(int num_parms, ConstStr *Params) {
00283        struct CitContext *CCC = CC;
00284        long literal_length;
00285        struct CtdlMessage *msg = NULL;
00286        long new_msgnum = (-1L);
00287        int ret = 0;
00288        char roomname[ROOMNAMELEN];
00289        char errbuf[SIZ];
00290        char dummy[SIZ];
00291        char savedroom[ROOMNAMELEN];
00292        int msgs, new;
00293        int i;
00294        char new_message_flags[SIZ];
00295        citimap *Imap;
00296 
00297        if (num_parms < 4) {
00298               IReply("BAD usage error");
00299               return;
00300        }
00301 
00302        if ( (Params[num_parms-1].Key[0] != '{')
00303           || (Params[num_parms-1].Key[Params[num_parms-1].len-1] != '}') )  {
00304               IReply("BAD no message literal supplied");
00305               return;
00306        }
00307 
00308        strcpy(new_message_flags, "");
00309        if (num_parms >= 5) {
00310               for (i=3; i<num_parms; ++i) {
00311                      strcat(new_message_flags, Params[i].Key);
00312                      strcat(new_message_flags, " ");
00313               }
00314               stripallbut(new_message_flags, '(', ')');
00315        }
00316 
00317        /* This is how we'd do this if it were relevant in our data store.
00318         * if (num_parms >= 6) {
00319         *  new_message_internaldate = parms[4];
00320         * }
00321         */
00322 
00323        literal_length = atol(&Params[num_parms-1].Key[1]);
00324        if (literal_length < 1) {
00325               IReply("BAD Message length must be at least 1.");
00326               return;
00327        }
00328 
00329        Imap = IMAP;
00330        imap_free_transmitted_message();   /* just in case. */
00331 
00332        Imap->TransmittedMessage = NewStrBufPlain(NULL, literal_length);
00333 
00334        if (Imap->TransmittedMessage == NULL) {
00335               IReply("NO Cannot allocate memory.");
00336               return;
00337        }
00338        
00339        IAPrintf("+ Transmit message now.\r\n");
00340        
00341        IUnbuffer ();
00342 
00343        client_read_blob(Imap->TransmittedMessage, literal_length, config.c_sleeping);
00344 
00345        if ((ret < 0) || (StrLength(Imap->TransmittedMessage) < literal_length)) {
00346               IReply("NO Read failed.");
00347               return;
00348        }
00349 
00350        /* Client will transmit a trailing CRLF after the literal (the message
00351         * text) is received.  This call to client_getln() absorbs it.
00352         */
00353        flush_output();
00354        client_getln(dummy, sizeof dummy);
00355 
00356        /* Convert RFC822 newlines (CRLF) to Unix newlines (LF) */
00357        IMAPM_syslog(LOG_DEBUG, "Converting CRLF to LF");
00358        StrBufToUnixLF(Imap->TransmittedMessage);
00359 
00360        IMAPM_syslog(LOG_DEBUG, "Converting message format");
00361        msg = convert_internet_message_buf(&Imap->TransmittedMessage);
00362 
00363        ret = imap_grabroom(roomname, Params[2].Key, 1);
00364        if (ret != 0) {
00365               IReply("NO Invalid mailbox name or access denied");
00366               return;
00367        }
00368 
00369        /*
00370         * CtdlUserGoto() formally takes us to the desired room.  (If another
00371         * folder is selected, save its name so we can return there!!!!!)
00372         */
00373        if (Imap->selected) {
00374               strcpy(savedroom, CC->room.QRname);
00375        }
00376        CtdlUserGoto(roomname, 0, 0, &msgs, &new);
00377 
00378        /* If the user is locally authenticated, FORCE the From: header to
00379         * show up as the real sender.  FIXME do we really want to do this?
00380         * Probably should make it site-definable or even room-definable.
00381         *
00382         * For now, we allow "forgeries" if the room is one of the user's
00383         * private mailboxes.
00384         */
00385        if (CC->logged_in) {
00386           if ( ((CC->room.QRflags & QR_MAILBOX) == 0) && (config.c_imap_keep_from == 0)) {
00387               if (msg->cm_fields['A'] != NULL) free(msg->cm_fields['A']);
00388               if (msg->cm_fields['N'] != NULL) free(msg->cm_fields['N']);
00389               if (msg->cm_fields['H'] != NULL) free(msg->cm_fields['H']);
00390               msg->cm_fields['A'] = strdup(CC->user.fullname);
00391               msg->cm_fields['N'] = strdup(config.c_nodename);
00392               msg->cm_fields['H'] = strdup(config.c_humannode);
00393            }
00394        }
00395 
00396        /* 
00397         * Can we post here?
00398         */
00399        ret = CtdlDoIHavePermissionToPostInThisRoom(errbuf, sizeof errbuf, NULL, POST_LOGGED_IN, 0);
00400 
00401        if (ret) {
00402               /* Nope ... print an error message */
00403               IReplyPrintf("NO %s", errbuf);
00404        }
00405 
00406        else {
00407               /* Yes ... go ahead and post! */
00408               if (msg != NULL) {
00409                      new_msgnum = CtdlSubmitMsg(msg, NULL, "", 0);
00410               }
00411               if (new_msgnum >= 0L) {
00412                      IReplyPrintf("OK [APPENDUID %ld %ld] APPEND completed",
00413                                  GLOBAL_UIDVALIDITY_VALUE, new_msgnum);
00414               }
00415               else {
00416                      IReplyPrintf("BAD Error %ld saving message to disk.",
00417                                  new_msgnum);
00418               }
00419        }
00420 
00421        /*
00422         * IMAP protocol response to client has already been sent by now.
00423         *
00424         * If another folder is selected, go back to that room so we can resume
00425         * our happy day without violent explosions.
00426         */
00427        if (Imap->selected) {
00428               CtdlUserGoto(savedroom, 0, 0, &msgs, &new);
00429        }
00430 
00431        /* We don't need this buffer anymore */
00432        CtdlFreeMessage(msg);
00433 
00434        if (new_message_flags != NULL) {
00435               imap_do_append_flags(new_msgnum, new_message_flags);
00436        }
00437 }