Back to index

courier  0.68.2
newmsg_newdraft.c
Go to the documentation of this file.
00001 /*
00002 ** Copyright 1998 - 2011 Double Precision, Inc.  See COPYING for
00003 ** distribution information.
00004 */
00005 
00006 
00007 /*
00008 */
00009 #include      "config.h"
00010 #include      "cgi/cgi.h"
00011 #include      "sqconfig.h"
00012 #include      "sqwebmail.h"
00013 #include      "auth.h"
00014 #include      "pref.h"
00015 #include      "maildir.h"
00016 #include      "folder.h"
00017 #include      "mailinglist.h"
00018 #include      "maildir/maildirmisc.h"
00019 #include      "rfc822/rfc822.h"
00020 #include      "rfc822/rfc2047.h"
00021 #include      "rfc2045/rfc2045.h"
00022 #include      "rfc2045/rfc2045charset.h"
00023 
00024 #include      <stdlib.h>
00025 #include      <fcntl.h>
00026 #if HAVE_UNISTD_H
00027 #include      <unistd.h>
00028 #endif
00029 
00030 extern const char *sqwebmail_mailboxid;
00031 extern const char *sqwebmail_content_charset;
00032 
00033 extern char *get_msgfilename(const char *, size_t *);
00034 
00035 static int draftfd;
00036 
00037 static int ismyaddr(const char *p, void *dummy)
00038 {
00039        return (strcmp(p, login_returnaddr()) == 0);
00040 }
00041 
00042 static void writefunc(const char *p, size_t l, void *dummy)
00043 {
00044        maildir_writemsg(draftfd, p, l);
00045 }
00046 
00047 char *newmsg_newdraft(const char *folder, const char *pos,
00048                      const char *forwardsep, const char *replysalut)
00049 {
00050 char   *filename=0;
00051 char   *replymode;
00052 size_t pos_n;
00053 FILE   *fp;
00054 
00055 const  char *mimeidptr;
00056 char   *draftfilename;
00057 struct rfc2045 *rfc2045p, *rfc2045partp;
00058 int    x;
00059 
00060        if (*cgi(replymode="reply") ||
00061               *cgi(replymode="replyall") ||
00062               *cgi(replymode="replylist") ||
00063               *cgi(replymode="forward") ||
00064               *cgi(replymode="forwardatt"))
00065        {
00066               pos_n=atol(pos);
00067 
00068               filename=get_msgfilename(folder, &pos_n);
00069        }
00070 
00071        if (!filename)       return (0);
00072 
00073        fp=0;
00074        x=maildir_semisafeopen(filename, O_RDONLY, 0);
00075 
00076        if (x >= 0)
00077               if ((fp=fdopen(x, "r")) == 0)
00078                      close(x);
00079 
00080        if (fp == 0)
00081        {
00082               free(filename);
00083               return (0);
00084        }
00085 
00086        rfc2045p=rfc2045_fromfp(fp);
00087 
00088        if (!rfc2045p)
00089        {
00090               fclose(fp);
00091               enomem();
00092        }
00093 
00094        mimeidptr=cgi("mimeid");
00095 
00096        rfc2045partp=0;
00097 
00098        if (*mimeidptr)
00099        {
00100               rfc2045partp=rfc2045_find(rfc2045p, mimeidptr);
00101               if (rfc2045partp)
00102               {
00103               const char      *content_type, *dummy;
00104 
00105                      rfc2045_mimeinfo(rfc2045partp, &content_type,
00106                             &dummy, &dummy);
00107 
00108                      if (!content_type || strcmp(content_type, "message/rfc822"))
00109                             rfc2045partp=0;
00110                      else
00111                             rfc2045partp=rfc2045partp->firstpart;
00112               }
00113        }
00114 
00115        if (!rfc2045partp)
00116               rfc2045partp=rfc2045p;
00117 
00118 
00119        draftfd=maildir_createmsg(INBOX "." DRAFTS, 0, &draftfilename);
00120        if (draftfd < 0)
00121        {
00122               fclose(fp);
00123               rfc2045_free(rfc2045p);
00124               enomem();
00125        }
00126 
00127        maildir_writemsgstr(draftfd, "From: ");
00128        {
00129        const char *f=pref_from;
00130 
00131               if (!f || !*f)       f=login_fromhdr();
00132               if (!f)       f="";
00133 
00134               f=rfc2047_encode_header_tobuf("to", f,
00135                                          sqwebmail_content_charset);
00136 
00137               maildir_writemsgstr(draftfd, f);
00138               maildir_writemsgstr(draftfd, "\n");
00139        }
00140 
00141        {
00142               char *ml=getmailinglists();
00143               struct rfc2045_mkreplyinfo ri;
00144               struct rfc2045src *src;
00145               int rc;
00146 
00147               src=rfc2045src_init_fd(fileno(fp));
00148               if (src == NULL)
00149                      enomem();
00150 
00151               memset(&ri, 0, sizeof(ri));
00152               ri.src=src;
00153               ri.rfc2045partp=rfc2045partp;
00154               ri.replymode=replymode;
00155               ri.replysalut=replysalut;
00156               ri.forwardsep=forwardsep;
00157               ri.myaddr_func=ismyaddr;
00158               ri.write_func=writefunc;
00159               ri.mailinglists=ml;
00160               ri.charset=sqwebmail_content_charset;
00161 
00162               if (strcmp(replymode, "forward") == 0
00163                   || strcmp(replymode, "forwardatt") == 0)
00164               {
00165                      rc=rfc2045_makereply(&ri);
00166               }
00167               else
00168               {
00169               char *basename=maildir_basename(filename);
00170 
00171                      maildir_writemsgstr(draftfd, "X-Reply-To-Folder: ");
00172                      maildir_writemsgstr(draftfd, folder);
00173                      maildir_writemsgstr(draftfd, "\nX-Reply-To-Msg: ");
00174 
00175                      maildir_writemsgstr(draftfd, basename);
00176                      free(basename);
00177                      maildir_writemsgstr(draftfd, "\n");
00178 
00179                      rc=rfc2045_makereply(&ri);
00180               }
00181               free(ml);
00182               rfc2045src_deinit(src);
00183 
00184               if (rc)
00185               {
00186                      fclose(fp);
00187                      close(draftfd);
00188                      rfc2045_free(rfc2045p);
00189                      enomem();
00190               }
00191        }
00192 
00193        fclose(fp);
00194        if (maildir_closemsg(draftfd, INBOX "." DRAFTS, draftfilename, 1, 0))
00195        {
00196               free(draftfilename);
00197               draftfilename=0;
00198               cgi_put("error", "quota");
00199        }
00200        free(filename);
00201        rfc2045_free(rfc2045p);
00202        return(draftfilename);
00203 }