Back to index

enigmail  1.4.3
mimeenig.cpp
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003  *
00004  * The contents of this file are subject to the Mozilla Public
00005  * License Version 1.1 (the "MPL"); you may not use this file
00006  * except in compliance with the MPL. You may obtain a copy of
00007  * the MPL at http://www.mozilla.org/MPL/
00008  *
00009  * Software distributed under the MPL is distributed on an "AS
00010  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
00011  * implied. See the MPL for the specific language governing
00012  * rights and limitations under the MPL.
00013  *
00014  * The Original Code is Enigmail.
00015  *
00016  * The Initial Developer of the Original Code is Ramalingam Saravanan.
00017  * Portions created by Ramalingam Saravanan <sarava@sarava.net> are
00018  * Copyright (C) 2002 Ramalingam Saravanan. All Rights Reserved.
00019  *
00020  * Contributor(s):
00021  * Patrick Brunschwig <patrick@mozilla-enigmail.org>
00022  *
00023  * Alternatively, the contents of this file may be used under the terms of
00024  * either the GNU General Public License Version 2 or later (the "GPL"), or
00025  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00026  * in which case the provisions of the GPL or the LGPL are applicable instead
00027  * of those above. If you wish to allow use of your version of this file only
00028  * under the terms of either the GPL or the LGPL, and not to allow others to
00029  * use your version of this file under the terms of the MPL, indicate your
00030  * decision by deleting the provisions above and replace them with the notice
00031  * and other provisions required by the GPL or the LGPL. If you do not delete
00032  * the provisions above, a recipient may use your version of this file under
00033  * the terms of any one of the MPL, the GPL or the LGPL.
00034  * ***** END LICENSE BLOCK ***** */
00035 
00036 #define MOZILLA_INTERNAL_API
00037 #include "mimecth.h"
00038 #include "mimemoz2.h"
00039 #include "mimeenig.h"
00040 #include "nspr.h"
00041 #include "plstr.h"
00042 #include "nsCOMPtr.h"
00043 #include "nsIURI.h"
00044 #include "nsIMsgMailNewsUrl.h"
00045 #include "nsIMsgWindow.h"
00046 #include "nsIEnigMimeDecrypt.h"
00047 
00048 /* Set superclass to NULL and initialize by hand later */
00049 MimeDefClass(MimeEncryptedEnig, MimeEncryptedEnigClass,
00050              mimeEncryptedEnigClass, NULL);
00051 
00052 static void *MimeEnig_init(MimeObject *,
00053                            int (*output_fn) (const char *, PRInt32, void *),
00054                            void *);
00055 static int MimeEnig_write (const char *, PRInt32, void *);
00056 static int MimeEnig_eof (void *, EMBool);
00057 static char* MimeEnig_generate (void *);
00058 static void MimeEnig_free (void *);
00059 
00060 // TEMPORARY; MOVE TO CLASS LATER
00061 nsCOMPtr<nsIEnigMimeDecrypt> mMimeDecrypt;
00062 
00063 static int
00064 MimeEncryptedEnigClassInitialize(MimeEncryptedEnigClass *clazz)
00065 {
00066   MimeObjectClass    *oclass = (MimeObjectClass *)    clazz;
00067   MimeEncryptedClass *eclass = (MimeEncryptedClass *) clazz;
00068 
00069   PR_ASSERT(!oclass->class_initialized);
00070 
00071   eclass->crypto_init          = MimeEnig_init;
00072   eclass->crypto_write         = MimeEnig_write;
00073   eclass->crypto_eof           = MimeEnig_eof;
00074   eclass->crypto_generate_html = MimeEnig_generate;
00075   eclass->crypto_free          = MimeEnig_free;
00076 
00077   return 0;
00078 }
00079 
00080 
00081 typedef struct MimeEnigData
00082 {
00083   int (*output_fn) (const char *buf, PRInt32 buf_size, void *output_closure);
00084   void *output_closure;
00085   MimeObject *self;
00086 
00087   nsCOMPtr<nsIEnigMimeDecrypt> mimeDecrypt;
00088 
00089   MimeEnigData()
00090     :output_fn(nsnull),
00091      output_closure(nsnull)
00092   {
00093   }
00094 
00095   ~MimeEnigData()
00096   {
00097     mimeDecrypt = nsnull;
00098   }
00099 } MimeEnigData;
00100 
00101 
00102 static void*
00103 MimeEnig_init(MimeObject *obj,
00104               int (*output_fn) (const char *buf, PRInt32 buf_size,
00105                                 void *output_closure),
00106               void *output_closure)
00107 {
00108   MimeEnigData *data;
00109   MimeDisplayOptions *opts;
00110 
00111   fprintf(stderr, "MimeEnig_init:\n");
00112 
00113   if (!(obj && obj->options && output_fn)) return NULL;
00114 
00115   opts = obj->options;
00116 
00117   data = new MimeEnigData;
00118   if (!data)
00119     return NULL;
00120 
00121   data->self = obj;
00122   data->output_fn = output_fn;
00123   data->output_closure = output_closure;
00124 
00125   // Enigmail stuff
00126   nsresult rv;
00127   data->mimeDecrypt = do_CreateInstance(NS_ENIGMIMEDECRYPT_CONTRACTID, &rv);
00128   if (NS_FAILED(rv))
00129     return NULL;
00130 
00131   EMBool verifyOnly = PR_FALSE;
00132   EMBool rfc2015 = PR_TRUE;
00133   rv = data->mimeDecrypt->Init(verifyOnly, rfc2015,
00134                                output_fn, output_closure);
00135   if (NS_FAILED(rv))
00136     return NULL;
00137 
00138   return data;
00139 }
00140 
00141 
00142 static int
00143 MimeEnig_write(const char *buf, PRInt32 buf_size, void *output_closure)
00144 {
00145   MimeEnigData *data = (MimeEnigData *) output_closure;
00146 
00147   if (!data || !data->output_fn)
00148     return -1;
00149 
00150   //nsCAutoString temStr(buf, buf_size);
00151   //fprintf(stderr, "MimeEnig_write:: aBuf='%s'\n", temStr.get());
00152 
00153   // Enigmail stuff
00154   if (!data->mimeDecrypt)
00155     return -1;
00156 
00157   nsresult rv;
00158   rv = data->mimeDecrypt->Write(buf, buf_size);
00159   if (NS_FAILED(rv))
00160     return -1;
00161 
00162   return 0;
00163 }
00164 
00165 static int
00166 MimeEnig_eof(void* output_closure, EMBool abort_p)
00167 {
00168   MimeEnigData *data = (MimeEnigData *) output_closure;
00169 
00170   fprintf(stderr, "MimeEnig_eof:\n");
00171 
00172   if (!data || !data->output_fn) {
00173     return -1;
00174   }
00175 
00176   if (0) {
00177     // TEST OUTPUT
00178     const char content[] = "content-type: multipart/mixed; boundary=\"ABCD\"\r\n\r\nmultipart\r\n--ABCD\r\ncontent-type: text/html \r\n\r\n<html><body><b>TEST CONTENT1<b></body></html>\r\n\r\n--ABCD\r\ncontent-type: text/plain\r\ncontent-disposition: attachment; filename=\"abcd.txt\"\r\n\r\nFILE CONTENTS\r\n--ABCD--\r\n";
00179 
00180     PR_SetError(0,0);
00181     int status = data->output_fn(content, strlen(content),
00182                                  data->output_closure);
00183     if (status < 0) {
00184       PR_SetError(status, 0);
00185       data->output_fn = 0;
00186       return -1;
00187     }
00188 
00189     return 0;
00190   }
00191 
00192   // Enigmail stuff
00193   if (!data->mimeDecrypt)
00194     return -1;
00195 
00196   mime_stream_data *msd = (mime_stream_data *) (data->self->options->stream_closure);
00197 
00198   nsCOMPtr<nsIURI> uri;
00199   nsCOMPtr<nsIMsgWindow> msgWindow;
00200 
00201   if (msd && msd->channel) {
00202     nsIChannel *channel = msd->channel;
00203 
00204     if (channel)
00205       channel->GetURI(getter_AddRefs(uri));
00206 
00207     nsCOMPtr<nsIMsgMailNewsUrl> msgUrl;
00208     if (uri)
00209       msgUrl = do_QueryInterface(uri);
00210 
00211     if (msgUrl)
00212       msgUrl->GetMsgWindow(getter_AddRefs(msgWindow));
00213   }
00214 
00215   nsresult rv;
00216   rv = data->mimeDecrypt->Finish(msgWindow, uri);
00217   if (NS_FAILED(rv))
00218     return -1;
00219 
00220   data->mimeDecrypt = nsnull;
00221   return 0;
00222 }
00223 
00224 static char*
00225 MimeEnig_generate(void *output_closure)
00226 {
00227   fprintf(stderr, "MimeEnig_generate:\n");
00228 
00229   const char htmlMsg[] = "<html><body><b>GEN MSG<b></body></html>";
00230   char* msg = (char *) PR_MALLOC(strlen(htmlMsg) + 1);
00231   if (msg) {
00232     PL_strcpy(msg, htmlMsg);
00233   }
00234   return msg;
00235 }
00236 
00237 static void
00238 MimeEnig_free(void *output_closure)
00239 {
00240   fprintf(stderr, "MimeEnig_free:\n");
00241 }