1
/* ***** BEGIN LICENSE BLOCK *****
2
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
4
* The contents of this file are subject to the Mozilla Public
5
* License Version 1.1 (the "MPL"); you may not use this file
6
* except in compliance with the MPL. You may obtain a copy of
7
* the MPL at http://www.mozilla.org/MPL/
9
* Software distributed under the MPL is distributed on an "AS
10
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
11
* implied. See the MPL for the specific language governing
12
* rights and limitations under the MPL.
14
* The Original Code is Enigmail.
16
* The Initial Developer of the Original Code is Ramalingam Saravanan.
17
* Portions created by Ramalingam Saravanan <sarava@sarava.net> are
18
* Copyright (C) 2002 Ramalingam Saravanan. All Rights Reserved.
21
* Patrick Brunschwig <patrick@mozilla-enigmail.org>
23
* Alternatively, the contents of this file may be used under the terms of
24
* either the GNU General Public License Version 2 or later (the "GPL"), or
25
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
26
* in which case the provisions of the GPL or the LGPL are applicable instead
27
* of those above. If you wish to allow use of your version of this file only
28
* under the terms of either the GPL or the LGPL, and not to allow others to
29
* use your version of this file under the terms of the MPL, indicate your
30
* decision by deleting the provisions above and replace them with the notice
31
* and other provisions required by the GPL or the LGPL. If you do not delete
32
* the provisions above, a recipient may use your version of this file under
33
* the terms of any one of the MPL, the GPL or the LGPL.
34
* ***** END LICENSE BLOCK ***** */
36
// Logging of debug output
37
// The following define statement should occur before any include statements
38
#define FORCE_PR_LOG /* Allow logging even in release build */
41
#include "nsEnigModule.h"
42
#include "nsEnigMimeService.h"
45
#include "nsStringAPI.h"
47
#include "nsIDOMNode.h"
48
#include "nsIDOMText.h"
49
#include "nsIThread.h"
50
#include "nsIComponentManager.h"
51
#include "nsIComponentRegistrar.h"
52
#include "mozilla/ModuleUtils.h"
53
#include "nsXULAppAPI.h"
54
#include "nsServiceManagerUtils.h"
57
PRLogModuleInfo* gEnigMimeServiceLog = NULL;
60
#define ERROR_LOG(args) PR_LOG(gEnigMimeServiceLog,PR_LOG_ERROR,args)
61
#define WARNING_LOG(args) PR_LOG(gEnigMimeServiceLog,PR_LOG_WARNING,args)
62
#define DEBUG_LOG(args) PR_LOG(gEnigMimeServiceLog,PR_LOG_DEBUG,args)
65
// nsEnigMimeService implementation
67
// nsISupports implementation
68
NS_IMPL_THREADSAFE_ISUPPORTS1(nsEnigMimeService,
72
// nsEnigMimeService implementation
73
nsEnigMimeService::nsEnigMimeService()
74
: mInitialized(PR_TRUE)
81
if (gEnigMimeServiceLog == NULL) {
82
gEnigMimeServiceLog = PR_NewLogModule("nsEnigMimeService");
87
nsCOMPtr<nsIThread> myThread;
88
rv = ENIG_GET_THREAD(myThread);
89
DEBUG_LOG(("nsEnigMimeService:: <<<<<<<<< CTOR(%p): myThread=%p\n",
90
this, myThread.get()));
95
nsEnigMimeService::~nsEnigMimeService()
99
nsCOMPtr<nsIThread> myThread;
100
rv = ENIG_GET_THREAD(myThread);
101
DEBUG_LOG(("nsEnigMimeService:: >>>>>>>>> DTOR(%p): myThread=%p\n",
102
this, myThread.get()));
108
///////////////////////////////////////////////////////////////////////////////
109
// nsIEnigMimeService methods:
110
///////////////////////////////////////////////////////////////////////////////
113
nsEnigMimeService::Init()
116
DEBUG_LOG(("nsEnigMimeService::Init:\n"));
122
nsEnigMimeService::GetInitialized(EMBool *_retval)
125
return NS_ERROR_NULL_POINTER;
127
*_retval = mInitialized;
129
DEBUG_LOG(("nsEnigMimeService::GetInitialized: %d\n", (int) mInitialized));
135
nsEnigMimeService::GetVersion(char **_retval)
137
*_retval = PL_strdup(ENIGMIME_VERSION);
139
return NS_ERROR_OUT_OF_MEMORY;
141
DEBUG_LOG(("nsEnigMimeService::GetVersion: %s\n", *_retval));
147
__ReplaceSubstring (nsAString &string, nsAString &replace, nsAString &with)
149
PRInt32 i = string.Find (replace);
151
string.Replace (i, replace.Length(), with);
152
i = string.Find (replace);
157
__ReplaceChar (nsAString &string, const PRUnichar replace, const PRUnichar with)
159
PRInt32 i = string.FindChar (replace);
161
string.Replace (i, 1, &with, 1);
162
i = string.FindChar (replace);
167
nsEnigMimeService::GetPlainText(nsIDOMNode* domNode,
168
const PRUnichar* findStr,
174
//DEBUG_LOG(("nsEnigMimeService::GetPlainText:\n"));
177
rv = domNode->GetNodeType(&nodeType);
178
if (NS_FAILED(rv)) return rv;
180
if (nodeType == nsIDOMNode::TEXT_NODE) {
182
nsCOMPtr<nsIDOMText> domText( do_QueryInterface(domNode) );
183
rv = domText->GetData(outStr);
184
if (NS_FAILED(rv)) return rv;
187
// Iterate over all child nodes
188
nsCOMPtr<nsIDOMNode> child;
189
rv = domNode->GetFirstChild(getter_AddRefs(child));
195
rv = GetPlainText(child, NULL, temStr);
196
if (NS_FAILED(rv)) return rv;
198
outStr.Append(temStr);
200
nsCOMPtr<nsIDOMNode> temp = child;
201
rv = temp->GetNextSibling(getter_AddRefs(child));
207
if (outStr.FindChar(0xA0) >= 0) {
208
// Replace non-breaking spaces with plain spaces
209
__ReplaceChar(outStr, 0xA0, ' ');
213
nsDependentString(findStr).Length() &&
214
(outStr.Find(nsDependentString(findStr)) < 0) ) {
215
// Substring not found; return empty string
225
nsEnigMimeService::Sleep(PRUint32 miliSeconds)
227
// Sleep for the specified amount of miliseconds
228
PR_Sleep(miliSeconds);
234
nsEnigMimeService::GetRandomHex(PRUint32 nDigits, char **_retval)
236
DEBUG_LOG(("nsEnigMimeService::GetRandomHex: %d\n", nDigits));
239
return NS_ERROR_NULL_POINTER;
242
return NS_ERROR_FAILURE;
245
PRSize nBytes = (nDigits+1)/2;
246
EMBool discardOneDigit = (nBytes*2 == nDigits+1);
248
unsigned char *randomBuf = (unsigned char*) PR_Malloc(sizeof(char *)
250
PRSize randomBytes = PR_GetRandomNoise((void*)randomBuf, nBytes);
252
if (randomBytes < nBytes) {
254
return NS_ERROR_NOT_AVAILABLE;
257
// Convert random bytes to hexadecimal string
258
nsCAutoString hex ("");
259
for (PRUint32 j=0; j<nBytes; j++) {
260
PRInt32 value = randomBuf[j];
261
if (discardOneDigit && (j == nBytes-1)) {
263
} else if (value < 16) {
266
hex.AppendInt(value, 16);
271
*_retval = ToNewCString(hex);