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 License
5
* Version 1.1 (the "MPL"); you may not use this file except in
6
* compliance with the MPL. You may obtain a copy of the MPL at
7
* http://www.mozilla.org/MPL/
9
* Software distributed under the MPL is distributed on an "AS IS" basis,
10
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the MPL
11
* for the specific language governing rights and limitations under the
14
* The Original Code is Enigmail.
16
* The Initial Developer of the Original Code is
17
* Ramalingam Saravanan <sarava@sarava.net>
18
* Portions created by the Initial Developer are Copyright (C) 2002
19
* the Initial Developer. All Rights Reserved.
22
* Patrick Brunschwig <patrick.brunschwig@gmx.net>
24
* Alternatively, the contents of this file may be used under the terms of
25
* either the GNU General Public License Version 2 or later (the "GPL"), or
26
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27
* in which case the provisions of the GPL or the LGPL are applicable instead
28
* of those above. If you wish to allow use of your version of this file only
29
* under the terms of either the GPL or the LGPL, and not to allow others to
30
* use your version of this file under the terms of the MPL, indicate your
31
* decision by deleting the provisions above and replace them with the notice
32
* and other provisions required by the GPL or the LGPL. If you do not delete
33
* the provisions above, a recipient may use your version of this file under
34
* the terms of any one of the MPL, the GPL or the LGPL.
36
* ***** END LICENSE BLOCK ***** */
38
// Logging of debug output
39
// The following define statement should occur before any include statements
40
#define FORCE_PR_LOG /* Allow logging even in release build */
44
#include "nsEnigModule.h"
45
#include "nsEnigMimeService.h"
50
#include "nsIDOMNode.h"
51
#include "nsIDOMText.h"
52
#include "nsIThread.h"
53
#include "nsIComponentManager.h"
54
#include "nsIComponentRegistrar.h"
55
#include "nsIGenericFactory.h"
56
#include "nsEnigContentHandler.h"
57
#include "nsReadableUtils.h"
58
#undef MOZILLA_INTERNAL_API
60
NS_GENERIC_FACTORY_CONSTRUCTOR(nsEnigContentHandler)
63
PRLogModuleInfo* gEnigMimeServiceLog = NULL;
66
#define ERROR_LOG(args) PR_LOG(gEnigMimeServiceLog,PR_LOG_ERROR,args)
67
#define WARNING_LOG(args) PR_LOG(gEnigMimeServiceLog,PR_LOG_WARNING,args)
68
#define DEBUG_LOG(args) PR_LOG(gEnigMimeServiceLog,PR_LOG_DEBUG,args)
71
// nsEnigMimeService implementation
73
// nsISupports implementation
74
NS_IMPL_THREADSAFE_ISUPPORTS1(nsEnigMimeService,
78
// nsEnigMimeService implementation
79
nsEnigMimeService::nsEnigMimeService()
80
: mDummyHandler(PR_FALSE),
81
mInitialized(PR_FALSE)
88
if (gEnigMimeServiceLog == nsnull) {
89
gEnigMimeServiceLog = PR_NewLogModule("nsEnigMimeService");
94
nsCOMPtr<nsIThread> myThread;
95
rv = ENIG_GET_THREAD(myThread);
96
DEBUG_LOG(("nsEnigMimeService:: <<<<<<<<< CTOR(%p): myThread=%p\n",
97
this, myThread.get()));
100
static const nsModuleComponentInfo info =
101
{ NS_ENIGCONTENTHANDLER_CLASSNAME,
102
NS_ENIGCONTENTHANDLER_CID,
103
NS_ENIGDUMMYHANDLER_CONTRACTID,
104
nsEnigContentHandlerConstructor,
107
// Create a generic factory for the dummy content handler
108
nsCOMPtr<nsIGenericFactory> factory;
109
rv = NS_NewGenericFactory(getter_AddRefs(factory), &info);
111
if (NS_SUCCEEDED(rv)) {
112
// Register factory for dummy handler
113
nsCOMPtr<nsIComponentRegistrar> registrar;
114
rv = NS_GetComponentRegistrar(getter_AddRefs(registrar));
115
if (NS_FAILED(rv)) return;
117
rv = registrar->RegisterFactory(info.mCID, info.mDescription,
118
info.mContractID, factory);
119
if (NS_SUCCEEDED(rv)) {
120
mDummyHandler = PR_TRUE;
126
nsEnigMimeService::~nsEnigMimeService()
130
nsCOMPtr<nsIThread> myThread;
131
rv = ENIG_GET_THREAD(myThread);
132
DEBUG_LOG(("nsEnigMimeService:: >>>>>>>>> DTOR(%p): myThread=%p\n",
133
this, myThread.get()));
139
///////////////////////////////////////////////////////////////////////////////
140
// nsIEnigMimeService methods:
141
///////////////////////////////////////////////////////////////////////////////
144
nsEnigMimeService::Init()
147
DEBUG_LOG(("nsEnigContenthandler::Init:\n"));
149
if (!mimeEncryptedClassP) {
150
ERROR_LOG(("nsEnigContenthandler::Init: ERROR mimeEncryptedClassPis null\n"));
151
return NS_ERROR_FAILURE;
154
if (!mDummyHandler) {
155
ERROR_LOG(("nsEnigContenthandler::Init: ERROR content handler for %s not initialized\n", APPLICATION_XENIGMAIL_DUMMY));
156
return NS_ERROR_FAILURE;
159
static const nsModuleComponentInfo info =
160
{ NS_ENIGCONTENTHANDLER_CLASSNAME,
161
NS_ENIGCONTENTHANDLER_CID,
162
NS_ENIGENCRYPTEDHANDLER_CONTRACTID,
163
nsEnigContentHandlerConstructor,
166
// Create a generic factory for the content handler
167
nsCOMPtr<nsIGenericFactory> factory;
168
rv = NS_NewGenericFactory(getter_AddRefs(factory), &info);
169
if (NS_FAILED(rv)) return rv;
171
nsCOMPtr<nsIComponentRegistrar> registrar;
172
rv = NS_GetComponentRegistrar(getter_AddRefs(registrar));
173
if (NS_FAILED(rv)) return rv;
176
rv = registrar->RegisterFactory(info.mCID, info.mDescription,
177
info.mContractID, factory);
179
if (NS_FAILED(rv)) return rv;
181
DEBUG_LOG(("nsEnigMimeService::Init: registered %s\n", info.mContractID));
183
mInitialized = PR_TRUE;
189
nsEnigMimeService::GetInitialized(PRBool *_retval)
192
return NS_ERROR_NULL_POINTER;
194
*_retval = mInitialized;
196
DEBUG_LOG(("nsEnigMimeService::GetInitialized: %d\n", (int) mInitialized));
202
nsEnigMimeService::GetVersion(char **_retval)
204
*_retval = PL_strdup(ENIGMIME_VERSION);
206
return NS_ERROR_OUT_OF_MEMORY;
208
DEBUG_LOG(("nsEnigMimeService::GetVersion: %s\n", *_retval));
213
nsEnigMimeService::GetPlainText(nsIDOMNode* domNode,
214
const PRUnichar* findStr,
220
//DEBUG_LOG(("nsEnigMimeService::GetPlainText:\n"));
223
rv = domNode->GetNodeType(&nodeType);
224
if (NS_FAILED(rv)) return rv;
226
if (nodeType == nsIDOMNode::TEXT_NODE) {
228
nsCOMPtr<nsIDOMText> domText( do_QueryInterface(domNode) );
229
rv = domText->GetData(outStr);
230
if (NS_FAILED(rv)) return rv;
233
// Iterate over all child nodes
234
nsCOMPtr<nsIDOMNode> child;
235
rv = domNode->GetFirstChild(getter_AddRefs(child));
241
rv = GetPlainText(child, nsnull, temStr);
242
if (NS_FAILED(rv)) return rv;
244
outStr.Append(temStr);
246
nsCOMPtr<nsIDOMNode> temp = child;
247
rv = temp->GetNextSibling(getter_AddRefs(child));
253
if (outStr.FindChar(0xA0) >= 0) {
254
// Replace non-breaking spaces with plain spaces
255
outStr.ReplaceChar(0xA0, ' ');
259
nsCharTraits<PRUnichar>::length(findStr) &&
260
(outStr.Find(findStr) < 0) ) {
261
// Substring not found; return empty string
271
nsEnigMimeService::RememberEncrypted(const nsACString & uri)
273
// Assuming duplicates are allowed.
274
mEncryptedURIs.AppendCString(nsCString(uri));
279
nsEnigMimeService::ForgetEncrypted(const nsACString & uri)
281
// Assuming, this will only remove one copy of the string, if the array
282
// contains multiple copies of the same string.
283
mEncryptedURIs.RemoveCString(nsCString(uri));
288
nsEnigMimeService::IsEncrypted(const nsACString & uri, PRBool *_retval)
290
*_retval = (mEncryptedURIs.IndexOf(nsCString(uri)) != -1);
295
nsEnigMimeService::Sleep(PRUint32 miliSeconds)
297
// Sleep for the specified amount of miliseconds
298
PR_Sleep(miliSeconds);
304
nsEnigMimeService::GetRandomHex(PRUint32 nDigits, char **_retval)
306
DEBUG_LOG(("nsIPCService::GetRandomHex: %d\n", nDigits));
309
return NS_ERROR_NULL_POINTER;
312
return NS_ERROR_FAILURE;
315
PRSize nBytes = (nDigits+1)/2;
316
PRBool discardOneDigit = (nBytes*2 == nDigits+1);
318
unsigned char *randomBuf = (unsigned char*) PR_Malloc(sizeof(char *)
320
PRSize randomBytes = PR_GetRandomNoise((void*)randomBuf, nBytes);
322
if (randomBytes < nBytes) {
324
return NS_ERROR_NOT_AVAILABLE;
327
// Convert random bytes to hexadecimal string
328
nsCAutoString hex ("");
329
for (PRUint32 j=0; j<nBytes; j++) {
330
PRInt32 value = randomBuf[j];
331
if (discardOneDigit && (j == nBytes-1)) {
333
} else if (value < 16) {
336
hex.AppendInt(value, 16);
341
*_retval = ToNewCString(hex);
349
NS_IMETHODIMP nsPipeTransport::InitCommand(const char *command,
353
const char *killString,
356
nsIPipeListener* console)
360
DEBUG_LOG(("nsPipeTransport::InitCommand: command=%s [%d]\n",
364
return NS_ERROR_FAILURE;
368
// Create a buffer of same size as the command string
369
PRUint32 len = strlen(command);
370
char* buf = (char*) PR_Malloc(sizeof(char) * (len+1) );
372
// Parse command arguments separated by whitespace
375
PRBool backquote = PR_FALSE;
376
PRBool inArg = PR_FALSE;
377
PRUint32 bufCount = 0;
378
PRUint32 argCount = 0;
380
for (j=0; j<len; j++) {
381
char ch = command[j];
382
if (!quote && !backquote) {
383
// Unquoted character
385
if ((ch == ' ') || (ch == '\t') || (ch == '\r') || (ch == '\n')) {
389
// End argument parsing; insert null character in buffer
390
buf[bufCount++] = '\0';
395
// Non-whitespace character; start parsing new argument
405
// Backquoted character; append to buffer
406
buf[bufCount++] = ch;
407
backquote = PR_FALSE;
409
} else if (ch == '\\') {
410
// Backquote following character
413
} else if (quote == ch) {
414
// Matching end quote
417
} else if (!quote && ((ch == '"') || (ch == '\'')) ) {
422
// Append character to buffer (quoted/unquoted)
423
buf[bufCount++] = ch;
429
buf[bufCount++] = '\0'; // End argument parsing
431
PR_ASSERT(bufCount <= (len+1)); // No buffer overflow
434
ERROR_LOG(("nsPipeTransport::InitCommand: Unmatched quote in command string\n"));
436
return NS_ERROR_FAILURE;
440
ERROR_LOG(("nsPipeTransport::InitCommand: Blank/null command string\n"));
442
return NS_ERROR_FAILURE;
445
DEBUG_LOG(("nsPipeTransport::InitCommand: argCount=%d\n", argCount));
447
// Argument list (includes command path as the first argument)
448
char** args = (char **) PR_Malloc(sizeof(char *) * (argCount+1) );
450
return NS_ERROR_OUT_OF_MEMORY;
452
PRUint32 argOffset = 0;
453
for (j=0; j<argCount; j++) {
454
args[j] = buf + argOffset;
455
argOffset += strlen(args[j]) + 1;
458
PR_ASSERT(argOffset == bufCount);
460
args[argCount] = NULL;
462
rv = Init((const char*) args[0],
463
(const char**) args+1, argCount-1, env, envCount,
464
timeoutMS, killString, noProxy, mergeStderr,
b'\\ No newline at end of file'