~ubuntu-branches/ubuntu/quantal/enigmail/quantal-security

« back to all changes in this revision

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

  • Committer: Package Import Robot
  • Author(s): Chris Coulson
  • Date: 2013-09-13 16:02:15 UTC
  • mfrom: (0.12.16)
  • Revision ID: package-import@ubuntu.com-20130913160215-u3g8nmwa0pdwagwc
Tags: 2:1.5.2-0ubuntu0.12.10.1
* New upstream release v1.5.2 for Thunderbird 24

* Build enigmail using a stripped down Thunderbird 17 build system, as it's
  now quite difficult to build the way we were doing previously, with the
  latest Firefox build system
* Add debian/patches/no_libxpcom.patch - Don't link against libxpcom, as it
  doesn't exist anymore (but exists in the build system)
* Add debian/patches/use_sdk.patch - Use the SDK version of xpt.py and
  friends
* Drop debian/patches/ipc-pipe_rename.diff (not needed anymore)
* Drop debian/patches/makefile_depth.diff (not needed anymore)

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
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/
8
 
 *
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.
13
 
 *
14
 
 * The Original Code is Enigmail.
15
 
 *
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.
19
 
 *
20
 
 * Contributor(s):
21
 
 * Patrick Brunschwig <patrick@mozilla-enigmail.org>
22
 
 *
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 ***** */
35
 
 
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 */
39
 
 
40
 
#include "enigmail.h"
41
 
#include "nsEnigModule.h"
42
 
#include "nsEnigMimeService.h"
43
 
#include "nspr.h"
44
 
#include "plstr.h"
45
 
#include "nsStringAPI.h"
46
 
#include "nsCOMPtr.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"
55
 
 
56
 
#ifdef PR_LOGGING
57
 
PRLogModuleInfo* gEnigMimeServiceLog = NULL;
58
 
#endif
59
 
 
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)
63
 
 
64
 
 
65
 
// nsEnigMimeService implementation
66
 
 
67
 
// nsISupports implementation
68
 
NS_IMPL_THREADSAFE_ISUPPORTS1(nsEnigMimeService,
69
 
                              nsIEnigMimeService)
70
 
 
71
 
 
72
 
// nsEnigMimeService implementation
73
 
nsEnigMimeService::nsEnigMimeService()
74
 
  : mInitialized(PR_TRUE)
75
 
{
76
 
  nsresult rv;
77
 
 
78
 
  NS_INIT_ISUPPORTS();
79
 
 
80
 
#ifdef PR_LOGGING
81
 
  if (gEnigMimeServiceLog == NULL) {
82
 
    gEnigMimeServiceLog = PR_NewLogModule("nsEnigMimeService");
83
 
  }
84
 
#endif
85
 
 
86
 
#ifdef FORCE_PR_LOG
87
 
  nsCOMPtr<nsIThread> myThread;
88
 
  rv = ENIG_GET_THREAD(myThread);
89
 
  DEBUG_LOG(("nsEnigMimeService:: <<<<<<<<< CTOR(%p): myThread=%p\n",
90
 
         this, myThread.get()));
91
 
#endif
92
 
}
93
 
 
94
 
 
95
 
nsEnigMimeService::~nsEnigMimeService()
96
 
{
97
 
  nsresult rv;
98
 
#ifdef FORCE_PR_LOG
99
 
  nsCOMPtr<nsIThread> myThread;
100
 
  rv = ENIG_GET_THREAD(myThread);
101
 
  DEBUG_LOG(("nsEnigMimeService:: >>>>>>>>> DTOR(%p): myThread=%p\n",
102
 
         this, myThread.get()));
103
 
#endif
104
 
 
105
 
}
106
 
 
107
 
 
108
 
///////////////////////////////////////////////////////////////////////////////
109
 
// nsIEnigMimeService methods:
110
 
///////////////////////////////////////////////////////////////////////////////
111
 
 
112
 
NS_IMETHODIMP
113
 
nsEnigMimeService::Init()
114
 
{
115
 
  nsresult rv;
116
 
  DEBUG_LOG(("nsEnigMimeService::Init:\n"));
117
 
 
118
 
  return NS_OK;
119
 
}
120
 
 
121
 
NS_IMETHODIMP
122
 
nsEnigMimeService::GetInitialized(EMBool *_retval)
123
 
{
124
 
  if (!_retval)
125
 
    return NS_ERROR_NULL_POINTER;
126
 
 
127
 
  *_retval = mInitialized;
128
 
 
129
 
  DEBUG_LOG(("nsEnigMimeService::GetInitialized: %d\n", (int) mInitialized));
130
 
 
131
 
  return NS_OK;
132
 
}
133
 
 
134
 
NS_IMETHODIMP
135
 
nsEnigMimeService::GetVersion(char **_retval)
136
 
{
137
 
  *_retval = PL_strdup(ENIGMIME_VERSION);
138
 
  if (!*_retval)
139
 
    return NS_ERROR_OUT_OF_MEMORY;
140
 
 
141
 
  DEBUG_LOG(("nsEnigMimeService::GetVersion: %s\n", *_retval));
142
 
  return NS_OK;
143
 
}
144
 
 
145
 
 
146
 
static void
147
 
__ReplaceSubstring (nsAString &string, nsAString &replace, nsAString &with)
148
 
{
149
 
  PRInt32 i = string.Find (replace);
150
 
  while (i >= 0) {
151
 
    string.Replace (i, replace.Length(), with);
152
 
    i = string.Find (replace);
153
 
  }
154
 
}
155
 
 
156
 
static void
157
 
__ReplaceChar (nsAString &string, const PRUnichar replace, const PRUnichar with)
158
 
{
159
 
  PRInt32 i = string.FindChar (replace);
160
 
  while (i >= 0) {
161
 
    string.Replace (i, 1, &with, 1);
162
 
    i = string.FindChar (replace);
163
 
  }
164
 
}
165
 
 
166
 
NS_IMETHODIMP
167
 
nsEnigMimeService::GetPlainText(nsIDOMNode* domNode,
168
 
                                const PRUnichar* findStr,
169
 
                                nsAString& text)
170
 
{
171
 
  nsresult rv;
172
 
  nsAutoString outStr;
173
 
 
174
 
  //DEBUG_LOG(("nsEnigMimeService::GetPlainText:\n"));
175
 
 
176
 
  PRUint16 nodeType;
177
 
  rv = domNode->GetNodeType(&nodeType);
178
 
  if (NS_FAILED(rv)) return rv;
179
 
 
180
 
  if (nodeType == nsIDOMNode::TEXT_NODE) {
181
 
    // Text node
182
 
    nsCOMPtr<nsIDOMText> domText( do_QueryInterface(domNode) );
183
 
    rv = domText->GetData(outStr);
184
 
    if (NS_FAILED(rv)) return rv;
185
 
 
186
 
  } else {
187
 
    // Iterate over all child nodes
188
 
    nsCOMPtr<nsIDOMNode> child;
189
 
    rv = domNode->GetFirstChild(getter_AddRefs(child));
190
 
    if (NS_FAILED(rv))
191
 
      return NS_OK;
192
 
 
193
 
    while (child) {
194
 
      nsAutoString temStr;
195
 
      rv = GetPlainText(child, NULL, temStr);
196
 
      if (NS_FAILED(rv)) return rv;
197
 
 
198
 
      outStr.Append(temStr);
199
 
 
200
 
      nsCOMPtr<nsIDOMNode> temp = child;
201
 
      rv = temp->GetNextSibling(getter_AddRefs(child));
202
 
      if (NS_FAILED(rv))
203
 
        break;
204
 
    }
205
 
  }
206
 
 
207
 
  if (outStr.FindChar(0xA0) >= 0) {
208
 
    // Replace non-breaking spaces with plain spaces
209
 
    __ReplaceChar(outStr, 0xA0, ' ');
210
 
  }
211
 
 
212
 
  if (findStr &&
213
 
      nsDependentString(findStr).Length() &&
214
 
      (outStr.Find(nsDependentString(findStr)) < 0) ) {
215
 
    // Substring not found; return empty string
216
 
    outStr.Truncate();
217
 
  }
218
 
 
219
 
  text = outStr;
220
 
 
221
 
  return NS_OK;
222
 
}
223
 
 
224
 
NS_IMETHODIMP
225
 
nsEnigMimeService::Sleep(PRUint32 miliSeconds)
226
 
{
227
 
  // Sleep for the specified amount of miliseconds
228
 
  PR_Sleep(miliSeconds);
229
 
  return NS_OK;
230
 
}
231
 
 
232
 
 
233
 
NS_IMETHODIMP
234
 
nsEnigMimeService::GetRandomHex(PRUint32 nDigits, char **_retval)
235
 
{
236
 
  DEBUG_LOG(("nsEnigMimeService::GetRandomHex: %d\n", nDigits));
237
 
 
238
 
  if (!_retval)
239
 
    return NS_ERROR_NULL_POINTER;
240
 
 
241
 
  if (nDigits < 1)
242
 
    return NS_ERROR_FAILURE;
243
 
 
244
 
  // Get random noise
245
 
  PRSize nBytes = (nDigits+1)/2;
246
 
  EMBool discardOneDigit = (nBytes*2 == nDigits+1);
247
 
 
248
 
  unsigned char *randomBuf = (unsigned char*) PR_Malloc(sizeof(char *)
249
 
                                                        * nBytes );
250
 
  PRSize randomBytes = PR_GetRandomNoise((void*)randomBuf, nBytes);
251
 
 
252
 
  if (randomBytes < nBytes) {
253
 
    PR_Free(randomBuf);
254
 
    return NS_ERROR_NOT_AVAILABLE;
255
 
  }
256
 
 
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)) {
262
 
       value = value % 16;
263
 
     } else if (value < 16) {
264
 
       hex.Append("0");
265
 
     }
266
 
     hex.AppendInt(value, 16);
267
 
  }
268
 
 
269
 
  PR_Free(randomBuf);
270
 
 
271
 
  *_retval = ToNewCString(hex);
272
 
 
273
 
  return NS_OK;
274
 
}