Back to index

enigmail  1.4.3
nsEnigMimeService.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 // Logging of debug output
00037 // The following define statement should occur before any include statements
00038 #define FORCE_PR_LOG       /* Allow logging even in release build */
00039 
00040 #include "enigmail.h"
00041 #include "pgpmime.h"
00042 #include "mimeenig.h"
00043 #include "nsEnigModule.h"
00044 #include "nsEnigMimeService.h"
00045 #include "nspr.h"
00046 #include "plstr.h"
00047 #include "nsStringAPI.h"
00048 #include "nsCOMPtr.h"
00049 #include "nsIDOMNode.h"
00050 #include "nsIDOMText.h"
00051 #include "nsIThread.h"
00052 #include "nsIComponentManager.h"
00053 #include "nsIComponentRegistrar.h"
00054 #include "mozilla/ModuleUtils.h"
00055 #include "nsXULAppAPI.h"
00056 #include "nsServiceManagerUtils.h"
00057 #include "nsEnigContentHandler.h"
00058 
00059 NS_GENERIC_FACTORY_CONSTRUCTOR(nsEnigContentHandler)
00060 
00061 #ifdef PR_LOGGING
00062 PRLogModuleInfo* gEnigMimeServiceLog = NULL;
00063 #endif
00064 
00065 #define ERROR_LOG(args)    PR_LOG(gEnigMimeServiceLog,PR_LOG_ERROR,args)
00066 #define WARNING_LOG(args)  PR_LOG(gEnigMimeServiceLog,PR_LOG_WARNING,args)
00067 #define DEBUG_LOG(args)    PR_LOG(gEnigMimeServiceLog,PR_LOG_DEBUG,args)
00068 
00069 
00070 // nsEnigMimeService implementation
00071 
00072 // nsISupports implementation
00073 NS_IMPL_THREADSAFE_ISUPPORTS1(nsEnigMimeService,
00074                               nsIEnigMimeService)
00075 
00076 
00077 // nsEnigMimeService implementation
00078 nsEnigMimeService::nsEnigMimeService()
00079   : mDummyHandler(PR_FALSE),
00080     mInitialized(PR_FALSE)
00081 {
00082   nsresult rv;
00083 
00084   NS_INIT_ISUPPORTS();
00085 
00086 #ifdef PR_LOGGING
00087   if (gEnigMimeServiceLog == nsnull) {
00088     gEnigMimeServiceLog = PR_NewLogModule("nsEnigMimeService");
00089   }
00090 #endif
00091 
00092 #ifdef FORCE_PR_LOG
00093   nsCOMPtr<nsIThread> myThread;
00094   rv = ENIG_GET_THREAD(myThread);
00095   DEBUG_LOG(("nsEnigMimeService:: <<<<<<<<< CTOR(%p): myThread=%p\n",
00096          this, myThread.get()));
00097 #endif
00098 
00099   mDummyHandler = PR_TRUE;
00100 }
00101 
00102 
00103 nsEnigMimeService::~nsEnigMimeService()
00104 {
00105   nsresult rv;
00106 #ifdef FORCE_PR_LOG
00107   nsCOMPtr<nsIThread> myThread;
00108   rv = ENIG_GET_THREAD(myThread);
00109   DEBUG_LOG(("nsEnigMimeService:: >>>>>>>>> DTOR(%p): myThread=%p\n",
00110          this, myThread.get()));
00111 #endif
00112 
00113 }
00114 
00115 
00117 // nsIEnigMimeService methods:
00119 
00120 NS_IMETHODIMP
00121 nsEnigMimeService::Init()
00122 {
00123   nsresult rv;
00124   DEBUG_LOG(("nsEnigMimeService::Init:\n"));
00125 
00126 #ifdef EM_OLD_MIME
00127   if (!mimeEncryptedClassP) {
00128     ERROR_LOG(("nsEnigMimeService::Init: ERROR mimeEncryptedClassPis null\n"));
00129     return NS_ERROR_FAILURE;
00130   }
00131 
00132   if (!mDummyHandler) {
00133     ERROR_LOG(("nsEnigMimeService::Init: ERROR content handler for %s not initialized\n", APPLICATION_XENIGMAIL_DUMMY));
00134     return NS_ERROR_FAILURE;
00135   }
00136 
00137   mInitialized = PR_TRUE;
00138 #endif
00139 
00140   return NS_OK;
00141 }
00142 
00143 NS_IMETHODIMP
00144 nsEnigMimeService::GetInitialized(EMBool *_retval)
00145 {
00146   if (!_retval)
00147     return NS_ERROR_NULL_POINTER;
00148 
00149   *_retval = mInitialized;
00150 
00151   DEBUG_LOG(("nsEnigMimeService::GetInitialized: %d\n", (int) mInitialized));
00152 
00153   return NS_OK;
00154 }
00155 
00156 NS_IMETHODIMP
00157 nsEnigMimeService::GetVersion(char **_retval)
00158 {
00159   *_retval = PL_strdup(ENIGMIME_VERSION);
00160   if (!*_retval)
00161     return NS_ERROR_OUT_OF_MEMORY;
00162 
00163   DEBUG_LOG(("nsEnigMimeService::GetVersion: %s\n", *_retval));
00164   return NS_OK;
00165 }
00166 
00167 
00168 static void
00169 __ReplaceSubstring (nsAString &string, nsAString &replace, nsAString &with)
00170 {
00171   PRInt32 i = string.Find (replace);
00172   while (i >= 0) {
00173     string.Replace (i, replace.Length(), with);
00174     i = string.Find (replace);
00175   }
00176 }
00177 
00178 static void
00179 __ReplaceChar (nsAString &string, const PRUnichar replace, const PRUnichar with)
00180 {
00181   PRInt32 i = string.FindChar (replace);
00182   while (i >= 0) {
00183     string.Replace (i, 1, &with, 1);
00184     i = string.FindChar (replace);
00185   }
00186 }
00187 
00188 NS_IMETHODIMP
00189 nsEnigMimeService::GetPlainText(nsIDOMNode* domNode,
00190                                 const PRUnichar* findStr,
00191                                 nsAString& text)
00192 {
00193   nsresult rv;
00194   nsAutoString outStr;
00195 
00196   //DEBUG_LOG(("nsEnigMimeService::GetPlainText:\n"));
00197 
00198   PRUint16 nodeType;
00199   rv = domNode->GetNodeType(&nodeType);
00200   if (NS_FAILED(rv)) return rv;
00201 
00202   if (nodeType == nsIDOMNode::TEXT_NODE) {
00203     // Text node
00204     nsCOMPtr<nsIDOMText> domText( do_QueryInterface(domNode) );
00205     rv = domText->GetData(outStr);
00206     if (NS_FAILED(rv)) return rv;
00207 
00208   } else {
00209     // Iterate over all child nodes
00210     nsCOMPtr<nsIDOMNode> child;
00211     rv = domNode->GetFirstChild(getter_AddRefs(child));
00212     if (NS_FAILED(rv))
00213       return NS_OK;
00214 
00215     while (child) {
00216       nsAutoString temStr;
00217       rv = GetPlainText(child, nsnull, temStr);
00218       if (NS_FAILED(rv)) return rv;
00219 
00220       outStr.Append(temStr);
00221 
00222       nsCOMPtr<nsIDOMNode> temp = child;
00223       rv = temp->GetNextSibling(getter_AddRefs(child));
00224       if (NS_FAILED(rv))
00225         break;
00226     }
00227   }
00228 
00229   if (outStr.FindChar(0xA0) >= 0) {
00230     // Replace non-breaking spaces with plain spaces
00231     __ReplaceChar(outStr, 0xA0, ' ');
00232   }
00233 
00234   if (findStr &&
00235       nsDependentString(findStr).Length() &&
00236       (outStr.Find(nsDependentString(findStr)) < 0) ) {
00237     // Substring not found; return empty string
00238     outStr.Truncate();
00239   }
00240 
00241   text = outStr;
00242 
00243   return NS_OK;
00244 }
00245 
00246 NS_IMETHODIMP
00247 nsEnigMimeService::Sleep(PRUint32 miliSeconds)
00248 {
00249   // Sleep for the specified amount of miliseconds
00250   PR_Sleep(miliSeconds);
00251   return NS_OK;
00252 }
00253 
00254 
00255 NS_IMETHODIMP
00256 nsEnigMimeService::GetRandomHex(PRUint32 nDigits, char **_retval)
00257 {
00258   DEBUG_LOG(("nsEnigMimeService::GetRandomHex: %d\n", nDigits));
00259 
00260   if (!_retval)
00261     return NS_ERROR_NULL_POINTER;
00262 
00263   if (nDigits < 1)
00264     return NS_ERROR_FAILURE;
00265 
00266   // Get random noise
00267   PRSize nBytes = (nDigits+1)/2;
00268   EMBool discardOneDigit = (nBytes*2 == nDigits+1);
00269 
00270   unsigned char *randomBuf = (unsigned char*) PR_Malloc(sizeof(char *)
00271                                                         * nBytes );
00272   PRSize randomBytes = PR_GetRandomNoise((void*)randomBuf, nBytes);
00273 
00274   if (randomBytes < nBytes) {
00275     PR_Free(randomBuf);
00276     return NS_ERROR_NOT_AVAILABLE;
00277   }
00278 
00279   // Convert random bytes to hexadecimal string
00280   nsCAutoString hex ("");
00281   for (PRUint32 j=0; j<nBytes; j++) {
00282      PRInt32 value = randomBuf[j];
00283      if (discardOneDigit && (j == nBytes-1)) {
00284        value = value % 16;
00285      } else if (value < 16) {
00286        hex.Append("0");
00287      }
00288      hex.AppendInt(value, 16);
00289   }
00290 
00291   PR_Free(randomBuf);
00292 
00293   *_retval = ToNewCString(hex);
00294 
00295   return NS_OK;
00296 }