~ubuntu-branches/ubuntu/oneiric/enigmail/oneiric-updates

« back to all changes in this revision

Viewing changes to extensions/enigmail/src/nsEnigMimeService.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Alexander Sack
  • Date: 2010-04-10 01:42:24 UTC
  • Revision ID: james.westby@ubuntu.com-20100410014224-fbq9ui5x3b0h2t36
Tags: 2:1.0.1-0ubuntu1
* First releaase of enigmail 1.0.1 for tbird/icedove 3
  (LP: #527138)
* redo packaging from scratch 
  + add debian/make-orig target that uses xulrunner provided
    buildsystem + enigmail tarball to produce a proper orig.tar.gz
  + use debhelper 7 with mozilla-devscripts
  + use debian source format 3.0 (quilt)
  + patch enigmail to use frozen API only
    - add debian/patches/frozen_api.diff
  + patch build system to not link against -lxul - which isnt
    available for sdks produced by all-static apps like tbird
    - add debian/patches/build_system_dont_link_libxul.diff
  + add minimal build-depends to control

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* ***** BEGIN LICENSE BLOCK *****
 
2
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 
3
 *
 
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/
 
8
 *
 
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
 
12
 * MPL.
 
13
 *
 
14
 * The Original Code is Enigmail.
 
15
 *
 
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.
 
20
 *
 
21
 * Contributor(s):
 
22
 * Patrick Brunschwig <patrick.brunschwig@gmx.net>
 
23
 *
 
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.
 
35
 *
 
36
 * ***** END LICENSE BLOCK ***** */
 
37
 
 
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 */
 
41
 
 
42
#include "enigmail.h"
 
43
#include "mimeenig.h"
 
44
#include "nsEnigModule.h"
 
45
#include "nsEnigMimeService.h"
 
46
#include "nspr.h"
 
47
#include "plstr.h"
 
48
#include "nsString.h"
 
49
#include "nsCOMPtr.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
 
59
 
 
60
NS_GENERIC_FACTORY_CONSTRUCTOR(nsEnigContentHandler)
 
61
 
 
62
#ifdef PR_LOGGING
 
63
PRLogModuleInfo* gEnigMimeServiceLog = NULL;
 
64
#endif
 
65
 
 
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)
 
69
 
 
70
 
 
71
// nsEnigMimeService implementation
 
72
 
 
73
// nsISupports implementation
 
74
NS_IMPL_THREADSAFE_ISUPPORTS1(nsEnigMimeService,
 
75
                              nsIEnigMimeService)
 
76
 
 
77
 
 
78
// nsEnigMimeService implementation
 
79
nsEnigMimeService::nsEnigMimeService()
 
80
  : mDummyHandler(PR_FALSE),
 
81
    mInitialized(PR_FALSE)
 
82
{
 
83
  nsresult rv;
 
84
 
 
85
  NS_INIT_ISUPPORTS();
 
86
 
 
87
#ifdef PR_LOGGING
 
88
  if (gEnigMimeServiceLog == nsnull) {
 
89
    gEnigMimeServiceLog = PR_NewLogModule("nsEnigMimeService");
 
90
  }
 
91
#endif
 
92
 
 
93
#ifdef FORCE_PR_LOG
 
94
  nsCOMPtr<nsIThread> myThread;
 
95
  rv = ENIG_GET_THREAD(myThread);
 
96
  DEBUG_LOG(("nsEnigMimeService:: <<<<<<<<< CTOR(%p): myThread=%p\n",
 
97
         this, myThread.get()));
 
98
#endif
 
99
 
 
100
  static const nsModuleComponentInfo info =
 
101
  { NS_ENIGCONTENTHANDLER_CLASSNAME,
 
102
    NS_ENIGCONTENTHANDLER_CID,
 
103
    NS_ENIGDUMMYHANDLER_CONTRACTID,
 
104
    nsEnigContentHandlerConstructor,
 
105
  };
 
106
 
 
107
  // Create a generic factory for the dummy content handler
 
108
  nsCOMPtr<nsIGenericFactory> factory;
 
109
  rv = NS_NewGenericFactory(getter_AddRefs(factory), &info);
 
110
 
 
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;
 
116
 
 
117
    rv = registrar->RegisterFactory(info.mCID, info.mDescription,
 
118
                                             info.mContractID, factory);
 
119
    if (NS_SUCCEEDED(rv)) {
 
120
      mDummyHandler = PR_TRUE;
 
121
    }
 
122
  }
 
123
}
 
124
 
 
125
 
 
126
nsEnigMimeService::~nsEnigMimeService()
 
127
{
 
128
  nsresult rv;
 
129
#ifdef FORCE_PR_LOG
 
130
  nsCOMPtr<nsIThread> myThread;
 
131
  rv = ENIG_GET_THREAD(myThread);
 
132
  DEBUG_LOG(("nsEnigMimeService:: >>>>>>>>> DTOR(%p): myThread=%p\n",
 
133
         this, myThread.get()));
 
134
#endif
 
135
 
 
136
}
 
137
 
 
138
 
 
139
///////////////////////////////////////////////////////////////////////////////
 
140
// nsIEnigMimeService methods:
 
141
///////////////////////////////////////////////////////////////////////////////
 
142
 
 
143
NS_IMETHODIMP
 
144
nsEnigMimeService::Init()
 
145
{
 
146
  nsresult rv;
 
147
  DEBUG_LOG(("nsEnigContenthandler::Init:\n"));
 
148
 
 
149
  if (!mimeEncryptedClassP) {
 
150
    ERROR_LOG(("nsEnigContenthandler::Init: ERROR mimeEncryptedClassPis null\n"));
 
151
    return NS_ERROR_FAILURE;
 
152
  }
 
153
 
 
154
  if (!mDummyHandler) {
 
155
    ERROR_LOG(("nsEnigContenthandler::Init: ERROR content handler for %s not initialized\n", APPLICATION_XENIGMAIL_DUMMY));
 
156
    return NS_ERROR_FAILURE;
 
157
  }
 
158
 
 
159
  static const nsModuleComponentInfo info =
 
160
  { NS_ENIGCONTENTHANDLER_CLASSNAME,
 
161
    NS_ENIGCONTENTHANDLER_CID,
 
162
    NS_ENIGENCRYPTEDHANDLER_CONTRACTID,
 
163
    nsEnigContentHandlerConstructor,
 
164
  };
 
165
 
 
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;
 
170
 
 
171
  nsCOMPtr<nsIComponentRegistrar> registrar;
 
172
  rv = NS_GetComponentRegistrar(getter_AddRefs(registrar));
 
173
  if (NS_FAILED(rv)) return rv;
 
174
 
 
175
  // Register factory
 
176
  rv = registrar->RegisterFactory(info.mCID, info.mDescription,
 
177
                                           info.mContractID, factory);
 
178
 
 
179
  if (NS_FAILED(rv)) return rv;
 
180
 
 
181
  DEBUG_LOG(("nsEnigMimeService::Init: registered %s\n", info.mContractID));
 
182
 
 
183
  mInitialized = PR_TRUE;
 
184
 
 
185
  return NS_OK;
 
186
}
 
187
 
 
188
NS_IMETHODIMP
 
189
nsEnigMimeService::GetInitialized(PRBool *_retval)
 
190
{
 
191
  if (!_retval)
 
192
    return NS_ERROR_NULL_POINTER;
 
193
 
 
194
  *_retval = mInitialized;
 
195
 
 
196
  DEBUG_LOG(("nsEnigMimeService::GetInitialized: %d\n", (int) mInitialized));
 
197
 
 
198
  return NS_OK;
 
199
}
 
200
 
 
201
NS_IMETHODIMP
 
202
nsEnigMimeService::GetVersion(char **_retval)
 
203
{
 
204
  *_retval = PL_strdup(ENIGMIME_VERSION);
 
205
  if (!*_retval)
 
206
    return NS_ERROR_OUT_OF_MEMORY;
 
207
 
 
208
  DEBUG_LOG(("nsEnigMimeService::GetVersion: %s\n", *_retval));
 
209
  return NS_OK;
 
210
}
 
211
 
 
212
NS_IMETHODIMP
 
213
nsEnigMimeService::GetPlainText(nsIDOMNode* domNode,
 
214
                                const PRUnichar* findStr,
 
215
                                nsAString& text)
 
216
{
 
217
  nsresult rv;
 
218
  nsAutoString outStr;
 
219
 
 
220
  //DEBUG_LOG(("nsEnigMimeService::GetPlainText:\n"));
 
221
 
 
222
  PRUint16 nodeType;
 
223
  rv = domNode->GetNodeType(&nodeType);
 
224
  if (NS_FAILED(rv)) return rv;
 
225
 
 
226
  if (nodeType == nsIDOMNode::TEXT_NODE) {
 
227
    // Text node
 
228
    nsCOMPtr<nsIDOMText> domText( do_QueryInterface(domNode) );
 
229
    rv = domText->GetData(outStr);
 
230
    if (NS_FAILED(rv)) return rv;
 
231
 
 
232
  } else {
 
233
    // Iterate over all child nodes
 
234
    nsCOMPtr<nsIDOMNode> child;
 
235
    rv = domNode->GetFirstChild(getter_AddRefs(child));
 
236
    if (NS_FAILED(rv))
 
237
      return NS_OK;
 
238
 
 
239
    while (child) {
 
240
      nsAutoString temStr;
 
241
      rv = GetPlainText(child, nsnull, temStr);
 
242
      if (NS_FAILED(rv)) return rv;
 
243
 
 
244
      outStr.Append(temStr);
 
245
 
 
246
      nsCOMPtr<nsIDOMNode> temp = child;
 
247
      rv = temp->GetNextSibling(getter_AddRefs(child));
 
248
      if (NS_FAILED(rv))
 
249
        break;
 
250
    }
 
251
  }
 
252
 
 
253
  if (outStr.FindChar(0xA0) >= 0) {
 
254
    // Replace non-breaking spaces with plain spaces
 
255
    outStr.ReplaceChar(0xA0, ' ');
 
256
  }
 
257
 
 
258
  if (findStr &&
 
259
      nsCharTraits<PRUnichar>::length(findStr) &&
 
260
      (outStr.Find(findStr) < 0) ) {
 
261
    // Substring not found; return empty string
 
262
    outStr.Truncate(0);
 
263
  }
 
264
 
 
265
  text = outStr;
 
266
 
 
267
  return NS_OK;
 
268
}
 
269
 
 
270
NS_IMETHODIMP
 
271
nsEnigMimeService::RememberEncrypted(const nsACString & uri)
 
272
{
 
273
  // Assuming duplicates are allowed.
 
274
  mEncryptedURIs.AppendCString(nsCString(uri));
 
275
  return NS_OK;
 
276
}
 
277
 
 
278
NS_IMETHODIMP
 
279
nsEnigMimeService::ForgetEncrypted(const nsACString & uri)
 
280
{
 
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));
 
284
  return NS_OK;
 
285
}
 
286
 
 
287
NS_IMETHODIMP
 
288
nsEnigMimeService::IsEncrypted(const nsACString & uri, PRBool *_retval)
 
289
{
 
290
  *_retval = (mEncryptedURIs.IndexOf(nsCString(uri)) != -1);
 
291
  return NS_OK;
 
292
}
 
293
 
 
294
NS_IMETHODIMP
 
295
nsEnigMimeService::Sleep(PRUint32 miliSeconds)
 
296
{
 
297
  // Sleep for the specified amount of miliseconds
 
298
  PR_Sleep(miliSeconds);
 
299
  return NS_OK;
 
300
}
 
301
 
 
302
 
 
303
NS_IMETHODIMP
 
304
nsEnigMimeService::GetRandomHex(PRUint32 nDigits, char **_retval)
 
305
{
 
306
  DEBUG_LOG(("nsIPCService::GetRandomHex: %d\n", nDigits));
 
307
 
 
308
  if (!_retval)
 
309
    return NS_ERROR_NULL_POINTER;
 
310
 
 
311
  if (nDigits < 1)
 
312
    return NS_ERROR_FAILURE;
 
313
 
 
314
  // Get random noise
 
315
  PRSize nBytes = (nDigits+1)/2;
 
316
  PRBool discardOneDigit = (nBytes*2 == nDigits+1);
 
317
 
 
318
  unsigned char *randomBuf = (unsigned char*) PR_Malloc(sizeof(char *)
 
319
                                                        * nBytes );
 
320
  PRSize randomBytes = PR_GetRandomNoise((void*)randomBuf, nBytes);
 
321
 
 
322
  if (randomBytes < nBytes) {
 
323
    PR_Free(randomBuf);
 
324
    return NS_ERROR_NOT_AVAILABLE;
 
325
  }
 
326
 
 
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)) {
 
332
       value = value % 16;
 
333
     } else if (value < 16) {
 
334
       hex.Append("0");
 
335
     }
 
336
     hex.AppendInt(value, 16);
 
337
  }
 
338
 
 
339
  PR_Free(randomBuf);
 
340
 
 
341
  *_retval = ToNewCString(hex);
 
342
 
 
343
  return NS_OK;
 
344
}
 
345
 
 
346
 
 
347
 
 
348
/*
 
349
NS_IMETHODIMP nsPipeTransport::InitCommand(const char *command,
 
350
                                           const char **env,
 
351
                                           PRUint32 envCount,
 
352
                                           PRUint32 timeoutMS,
 
353
                                           const char *killString,
 
354
                                           PRBool noProxy,
 
355
                                           PRBool mergeStderr,
 
356
                                           nsIPipeListener* console)
 
357
{
 
358
  nsresult rv;
 
359
 
 
360
  DEBUG_LOG(("nsPipeTransport::InitCommand: command=%s [%d]\n",
 
361
             command, envCount));
 
362
 
 
363
  if (!command)
 
364
    return NS_ERROR_FAILURE;
 
365
 
 
366
  mCommand = command;
 
367
 
 
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) );
 
371
 
 
372
  // Parse command arguments separated by whitespace
 
373
  PRUint32 j;
 
374
  char quote = '\0';
 
375
  PRBool backquote = PR_FALSE;
 
376
  PRBool inArg = PR_FALSE;
 
377
  PRUint32 bufCount = 0;
 
378
  PRUint32 argCount = 0;
 
379
 
 
380
  for (j=0; j<len; j++) {
 
381
    char ch = command[j];
 
382
    if (!quote && !backquote) {
 
383
      // Unquoted character
 
384
 
 
385
      if ((ch == ' ') || (ch == '\t') || (ch == '\r') || (ch == '\n')) {
 
386
        // Whitespace (skip)
 
387
 
 
388
        if (inArg) {
 
389
          // End argument parsing; insert null character in buffer
 
390
          buf[bufCount++] = '\0';
 
391
          inArg = PR_FALSE;
 
392
        }
 
393
 
 
394
      } else if (!inArg) {
 
395
        // Non-whitespace character; start parsing new argument
 
396
        inArg = PR_TRUE;
 
397
        argCount++;
 
398
      }
 
399
    }
 
400
 
 
401
    if (inArg) {
 
402
      // Argument parsing
 
403
 
 
404
      if (backquote) {
 
405
        // Backquoted character; append to buffer
 
406
        buf[bufCount++] = ch;
 
407
        backquote = PR_FALSE;
 
408
 
 
409
      } else if (ch == '\\') {
 
410
        // Backquote following character
 
411
        backquote = PR_TRUE;
 
412
 
 
413
      } else if (quote == ch) {
 
414
        // Matching end quote
 
415
        quote = '\0';
 
416
 
 
417
      } else if (!quote && ((ch == '"') || (ch == '\'')) ) {
 
418
        // Start new quote
 
419
        quote = ch;
 
420
 
 
421
      } else {
 
422
        // Append character to buffer (quoted/unquoted)
 
423
        buf[bufCount++] = ch;
 
424
      }
 
425
    }
 
426
  }
 
427
 
 
428
  if (inArg)
 
429
    buf[bufCount++] = '\0';   // End argument parsing
 
430
 
 
431
  PR_ASSERT(bufCount <= (len+1)); // No buffer overflow
 
432
 
 
433
  if (quote) {
 
434
    ERROR_LOG(("nsPipeTransport::InitCommand: Unmatched quote in command string\n"));
 
435
    PR_Free(buf);
 
436
    return NS_ERROR_FAILURE;
 
437
  }
 
438
 
 
439
  if (!argCount) {
 
440
    ERROR_LOG(("nsPipeTransport::InitCommand: Blank/null command string\n"));
 
441
    PR_Free(buf);
 
442
    return NS_ERROR_FAILURE;
 
443
  }
 
444
 
 
445
  DEBUG_LOG(("nsPipeTransport::InitCommand: argCount=%d\n", argCount));
 
446
 
 
447
  // Argument list (includes command path as the first argument)
 
448
  char** args = (char **) PR_Malloc(sizeof(char *) * (argCount+1) );
 
449
  if (!args)
 
450
    return NS_ERROR_OUT_OF_MEMORY;
 
451
 
 
452
  PRUint32 argOffset = 0;
 
453
  for (j=0; j<argCount; j++) {
 
454
    args[j] = buf + argOffset;
 
455
    argOffset += strlen(args[j]) + 1;
 
456
  }
 
457
 
 
458
  PR_ASSERT(argOffset == bufCount);
 
459
 
 
460
  args[argCount] = NULL;
 
461
 
 
462
  rv = Init((const char*) args[0],
 
463
            (const char**) args+1, argCount-1, env, envCount,
 
464
            timeoutMS, killString, noProxy, mergeStderr,
 
465
            console);
 
466
 
 
467
  PR_Free(buf);
 
468
 
 
469
  return rv;
 
470
}
 
471
*/
 
 
b'\\ No newline at end of file'