summaryrefslogtreecommitdiffstats
path: root/comm/mailnews/compose/src/nsMsgCompFields.cpp
blob: 9bfb66a253947665199abed4b65cebd1bfa50ad7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "nsMsgCompose.h"
#include "nsMsgCompFields.h"
#include "nsMsgI18N.h"
#include "nsMsgCompUtils.h"
#include "nsMsgUtils.h"
#include "prmem.h"
#include "nsIFileChannel.h"
#include "nsIMsgAttachment.h"
#include "nsIMsgMdnGenerator.h"
#include "nsServiceManagerUtils.h"
#include "nsMemory.h"
#include "mozilla/ArrayUtils.h"
#include "mozilla/mailnews/MimeHeaderParser.h"

using namespace mozilla::mailnews;

struct HeaderInfo {
  /// Header name
  const char* mName;
  /// If true, nsMsgCompFields should reflect the raw header value instead of
  /// the unstructured header value.
  bool mStructured;
};

// This is a mapping of the m_headers local set to the actual header name we
// store on the structured header object.
static HeaderInfo kHeaders[] = {
    {"From", true},
    {"Reply-To", true},
    {"To", true},
    {"Cc", true},
    {"Bcc", true},
    {nullptr, false},  // FCC
    {nullptr, false},  // FCC2
    {"Newsgroups", true},
    {"Followup-To", true},
    {"Subject", false},
    {"Organization", false},
    {"References", true},
    {"X-Mozilla-News-Host", false},
    {"X-Priority", false},
    {nullptr, false},  // CHARACTER_SET
    {"Message-Id", true},
    {"X-Template", true},
    {nullptr, false},  // DRAFT_ID
    {nullptr, false},  // TEMPLATE_ID
    {"Content-Language", true},
    {nullptr, false}  // CREATOR IDENTITY KEY
};

static_assert(
    MOZ_ARRAY_LENGTH(kHeaders) == nsMsgCompFields::MSG_MAX_HEADERS,
    "These two arrays need to be kept in sync or bad things will happen!");

NS_IMPL_ISUPPORTS(nsMsgCompFields, nsIMsgCompFields, msgIStructuredHeaders,
                  msgIWritableStructuredHeaders)

nsMsgCompFields::nsMsgCompFields()
    : mStructuredHeaders(do_CreateInstance(NS_ISTRUCTUREDHEADERS_CONTRACTID)) {
  m_body.Truncate();

  m_attachVCard = false;
  m_forcePlainText = false;
  m_useMultipartAlternative = false;
  m_returnReceipt = false;
  m_receiptHeaderType = nsIMsgMdnGenerator::eDntType;
  m_DSN = false;
  m_bodyIsAsciiOnly = false;
  m_forceMsgEncoding = false;
  m_needToCheckCharset = true;
  m_attachmentReminder = false;
  m_deliveryFormat = nsIMsgCompSendFormat::Unset;
}

nsMsgCompFields::~nsMsgCompFields() {
  MOZ_LOG(Compose, mozilla::LogLevel::Debug, ("~nsMsgCompFields()"));
}

nsresult nsMsgCompFields::SetAsciiHeader(MsgHeaderID header,
                                         const char* value) {
  NS_ASSERTION(header >= 0 && header < MSG_MAX_HEADERS,
               "Invalid message header index!");

  // If we are storing this on the structured header object, we need to set the
  // value on that object as well. Note that the value may be null, which we'll
  // take as an attempt to delete the header.
  const char* headerName = kHeaders[header].mName;
  if (headerName) {
    if (!value || !*value) return mStructuredHeaders->DeleteHeader(headerName);

    return mStructuredHeaders->SetRawHeader(headerName,
                                            nsDependentCString(value));
  }

  // Not on the structurd header object, so save it locally.
  m_headers[header] = value;

  return NS_OK;
}

const char* nsMsgCompFields::GetAsciiHeader(MsgHeaderID header) {
  NS_ASSERTION(header >= 0 && header < MSG_MAX_HEADERS,
               "Invalid message header index!");

  const char* headerName = kHeaders[header].mName;
  if (headerName) {
    // We may be out of sync with the structured header object. Retrieve the
    // header value.
    if (kHeaders[header].mStructured) {
      mStructuredHeaders->GetRawHeader(headerName, m_headers[header]);
    } else {
      nsString value;
      mStructuredHeaders->GetUnstructuredHeader(headerName, value);
      CopyUTF16toUTF8(value, m_headers[header]);
    }
  }

  return m_headers[header].get();
}

nsresult nsMsgCompFields::SetUnicodeHeader(MsgHeaderID header,
                                           const nsAString& value) {
  return SetAsciiHeader(header, NS_ConvertUTF16toUTF8(value).get());
}

nsresult nsMsgCompFields::GetUnicodeHeader(MsgHeaderID header,
                                           nsAString& aResult) {
  CopyUTF8toUTF16(nsDependentCString(GetAsciiHeader(header)), aResult);
  return NS_OK;
}

NS_IMETHODIMP nsMsgCompFields::SetFrom(const nsAString& value) {
  return SetUnicodeHeader(MSG_FROM_HEADER_ID, value);
}

NS_IMETHODIMP nsMsgCompFields::GetFrom(nsAString& _retval) {
  return GetUnicodeHeader(MSG_FROM_HEADER_ID, _retval);
}

NS_IMETHODIMP nsMsgCompFields::SetReplyTo(const nsAString& value) {
  return SetUnicodeHeader(MSG_REPLY_TO_HEADER_ID, value);
}

NS_IMETHODIMP nsMsgCompFields::GetReplyTo(nsAString& _retval) {
  return GetUnicodeHeader(MSG_REPLY_TO_HEADER_ID, _retval);
}

NS_IMETHODIMP nsMsgCompFields::SetTo(const nsAString& value) {
  return SetUnicodeHeader(MSG_TO_HEADER_ID, value);
}

NS_IMETHODIMP nsMsgCompFields::GetTo(nsAString& _retval) {
  return GetUnicodeHeader(MSG_TO_HEADER_ID, _retval);
}

NS_IMETHODIMP nsMsgCompFields::SetCc(const nsAString& value) {
  return SetUnicodeHeader(MSG_CC_HEADER_ID, value);
}

NS_IMETHODIMP nsMsgCompFields::GetCc(nsAString& _retval) {
  return GetUnicodeHeader(MSG_CC_HEADER_ID, _retval);
}

NS_IMETHODIMP nsMsgCompFields::SetBcc(const nsAString& value) {
  return SetUnicodeHeader(MSG_BCC_HEADER_ID, value);
}

NS_IMETHODIMP nsMsgCompFields::GetBcc(nsAString& _retval) {
  return GetUnicodeHeader(MSG_BCC_HEADER_ID, _retval);
}

NS_IMETHODIMP nsMsgCompFields::SetFcc(const nsAString& value) {
  return SetUnicodeHeader(MSG_FCC_HEADER_ID, value);
}

NS_IMETHODIMP nsMsgCompFields::GetFcc(nsAString& _retval) {
  return GetUnicodeHeader(MSG_FCC_HEADER_ID, _retval);
}

NS_IMETHODIMP nsMsgCompFields::SetFcc2(const nsAString& value) {
  return SetUnicodeHeader(MSG_FCC2_HEADER_ID, value);
}

NS_IMETHODIMP nsMsgCompFields::GetFcc2(nsAString& _retval) {
  return GetUnicodeHeader(MSG_FCC2_HEADER_ID, _retval);
}

NS_IMETHODIMP nsMsgCompFields::SetNewsgroups(const nsAString& aValue) {
  return SetUnicodeHeader(MSG_NEWSGROUPS_HEADER_ID, aValue);
}

NS_IMETHODIMP nsMsgCompFields::GetNewsgroups(nsAString& aGroup) {
  return GetUnicodeHeader(MSG_NEWSGROUPS_HEADER_ID, aGroup);
}

NS_IMETHODIMP nsMsgCompFields::SetFollowupTo(const nsAString& aValue) {
  return SetUnicodeHeader(MSG_FOLLOWUP_TO_HEADER_ID, aValue);
}

NS_IMETHODIMP nsMsgCompFields::GetFollowupTo(nsAString& _retval) {
  return GetUnicodeHeader(MSG_FOLLOWUP_TO_HEADER_ID, _retval);
}

NS_IMETHODIMP nsMsgCompFields::GetHasRecipients(bool* _retval) {
  NS_ENSURE_ARG_POINTER(_retval);

  *_retval = NS_SUCCEEDED(mime_sanity_check_fields_recipients(
      GetTo(), GetCc(), GetBcc(), GetNewsgroups()));

  return NS_OK;
}

NS_IMETHODIMP nsMsgCompFields::SetCreatorIdentityKey(const char* value) {
  return SetAsciiHeader(MSG_CREATOR_IDENTITY_KEY_ID, value);
}

NS_IMETHODIMP nsMsgCompFields::GetCreatorIdentityKey(char** _retval) {
  NS_ENSURE_ARG_POINTER(_retval);
  *_retval = strdup(GetAsciiHeader(MSG_CREATOR_IDENTITY_KEY_ID));
  return *_retval ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
}

NS_IMETHODIMP nsMsgCompFields::SetSubject(const nsAString& value) {
  return SetUnicodeHeader(MSG_SUBJECT_HEADER_ID, value);
}

NS_IMETHODIMP nsMsgCompFields::GetSubject(nsAString& _retval) {
  return GetUnicodeHeader(MSG_SUBJECT_HEADER_ID, _retval);
}

NS_IMETHODIMP nsMsgCompFields::SetOrganization(const nsAString& value) {
  return SetUnicodeHeader(MSG_ORGANIZATION_HEADER_ID, value);
}

NS_IMETHODIMP nsMsgCompFields::GetOrganization(nsAString& _retval) {
  return GetUnicodeHeader(MSG_ORGANIZATION_HEADER_ID, _retval);
}

NS_IMETHODIMP nsMsgCompFields::SetReferences(const char* value) {
  return SetAsciiHeader(MSG_REFERENCES_HEADER_ID, value);
}

NS_IMETHODIMP nsMsgCompFields::GetReferences(char** _retval) {
  *_retval = strdup(GetAsciiHeader(MSG_REFERENCES_HEADER_ID));
  return *_retval ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
}

NS_IMETHODIMP nsMsgCompFields::SetNewspostUrl(const char* value) {
  return SetAsciiHeader(MSG_NEWSPOSTURL_HEADER_ID, value);
}

NS_IMETHODIMP nsMsgCompFields::GetNewspostUrl(char** _retval) {
  *_retval = strdup(GetAsciiHeader(MSG_NEWSPOSTURL_HEADER_ID));
  return *_retval ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
}

NS_IMETHODIMP nsMsgCompFields::SetPriority(const char* value) {
  return SetAsciiHeader(MSG_PRIORITY_HEADER_ID, value);
}

NS_IMETHODIMP nsMsgCompFields::GetPriority(char** _retval) {
  *_retval = strdup(GetAsciiHeader(MSG_PRIORITY_HEADER_ID));
  return *_retval ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
}

NS_IMETHODIMP nsMsgCompFields::SetMessageId(const char* value) {
  return SetAsciiHeader(MSG_MESSAGE_ID_HEADER_ID, value);
}

NS_IMETHODIMP nsMsgCompFields::GetMessageId(char** _retval) {
  *_retval = strdup(GetAsciiHeader(MSG_MESSAGE_ID_HEADER_ID));
  return *_retval ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
}

NS_IMETHODIMP nsMsgCompFields::SetTemplateName(const nsAString& value) {
  return SetUnicodeHeader(MSG_X_TEMPLATE_HEADER_ID, value);
}

NS_IMETHODIMP nsMsgCompFields::GetTemplateName(nsAString& _retval) {
  return GetUnicodeHeader(MSG_X_TEMPLATE_HEADER_ID, _retval);
}

NS_IMETHODIMP nsMsgCompFields::SetDraftId(const nsACString& value) {
  return SetAsciiHeader(MSG_DRAFT_ID_HEADER_ID,
                        PromiseFlatCString(value).get());
}

NS_IMETHODIMP nsMsgCompFields::GetDraftId(nsACString& _retval) {
  _retval.Assign(GetAsciiHeader(MSG_DRAFT_ID_HEADER_ID));
  return NS_OK;
}

NS_IMETHODIMP nsMsgCompFields::SetTemplateId(const nsACString& value) {
  return SetAsciiHeader(MSG_TEMPLATE_ID_HEADER_ID,
                        PromiseFlatCString(value).get());
}

NS_IMETHODIMP nsMsgCompFields::GetTemplateId(nsACString& _retval) {
  _retval.Assign(GetAsciiHeader(MSG_TEMPLATE_ID_HEADER_ID));
  return NS_OK;
}

NS_IMETHODIMP nsMsgCompFields::SetReturnReceipt(bool value) {
  m_returnReceipt = value;
  return NS_OK;
}

NS_IMETHODIMP nsMsgCompFields::GetReturnReceipt(bool* _retval) {
  *_retval = m_returnReceipt;
  return NS_OK;
}

NS_IMETHODIMP nsMsgCompFields::SetReceiptHeaderType(int32_t value) {
  m_receiptHeaderType = value;
  return NS_OK;
}

NS_IMETHODIMP nsMsgCompFields::GetReceiptHeaderType(int32_t* _retval) {
  *_retval = m_receiptHeaderType;
  return NS_OK;
}

NS_IMETHODIMP nsMsgCompFields::SetDSN(bool value) {
  m_DSN = value;
  return NS_OK;
}

NS_IMETHODIMP nsMsgCompFields::GetDSN(bool* _retval) {
  NS_ENSURE_ARG_POINTER(_retval);
  *_retval = m_DSN;
  return NS_OK;
}

NS_IMETHODIMP nsMsgCompFields::SetAttachVCard(bool value) {
  m_attachVCard = value;
  return NS_OK;
}

NS_IMETHODIMP nsMsgCompFields::GetAttachVCard(bool* _retval) {
  *_retval = m_attachVCard;
  return NS_OK;
}

NS_IMETHODIMP nsMsgCompFields::GetAttachmentReminder(bool* _retval) {
  *_retval = m_attachmentReminder;
  return NS_OK;
}

NS_IMETHODIMP nsMsgCompFields::SetAttachmentReminder(bool value) {
  m_attachmentReminder = value;
  return NS_OK;
}

NS_IMETHODIMP nsMsgCompFields::SetDeliveryFormat(int32_t value) {
  switch (value) {
    case nsIMsgCompSendFormat::Auto:
    case nsIMsgCompSendFormat::PlainText:
    case nsIMsgCompSendFormat::HTML:
    case nsIMsgCompSendFormat::Both:
      m_deliveryFormat = value;
      break;
    case nsIMsgCompSendFormat::Unset:
    default:
      m_deliveryFormat = nsIMsgCompSendFormat::Unset;
  }

  return NS_OK;
}

NS_IMETHODIMP nsMsgCompFields::GetDeliveryFormat(int32_t* _retval) {
  NS_ENSURE_ARG_POINTER(_retval);
  *_retval = m_deliveryFormat;
  return NS_OK;
}

NS_IMETHODIMP nsMsgCompFields::SetContentLanguage(const char* value) {
  return SetAsciiHeader(MSG_CONTENT_LANGUAGE_ID, value);
}

NS_IMETHODIMP nsMsgCompFields::GetContentLanguage(char** _retval) {
  NS_ENSURE_ARG_POINTER(_retval);
  *_retval = strdup(GetAsciiHeader(MSG_CONTENT_LANGUAGE_ID));
  return *_retval ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
}

NS_IMETHODIMP nsMsgCompFields::SetForcePlainText(bool value) {
  m_forcePlainText = value;
  return NS_OK;
}

NS_IMETHODIMP nsMsgCompFields::GetForcePlainText(bool* _retval) {
  *_retval = m_forcePlainText;
  return NS_OK;
}

NS_IMETHODIMP nsMsgCompFields::SetForceMsgEncoding(bool value) {
  m_forceMsgEncoding = value;
  return NS_OK;
}

NS_IMETHODIMP nsMsgCompFields::GetForceMsgEncoding(bool* _retval) {
  NS_ENSURE_ARG_POINTER(_retval);
  *_retval = m_forceMsgEncoding;
  return NS_OK;
}

NS_IMETHODIMP nsMsgCompFields::SetUseMultipartAlternative(bool value) {
  m_useMultipartAlternative = value;
  return NS_OK;
}

NS_IMETHODIMP nsMsgCompFields::GetUseMultipartAlternative(bool* _retval) {
  *_retval = m_useMultipartAlternative;
  return NS_OK;
}

NS_IMETHODIMP nsMsgCompFields::SetBodyIsAsciiOnly(bool value) {
  m_bodyIsAsciiOnly = value;
  return NS_OK;
}

NS_IMETHODIMP nsMsgCompFields::GetBodyIsAsciiOnly(bool* _retval) {
  NS_ENSURE_ARG_POINTER(_retval);

  *_retval = m_bodyIsAsciiOnly;
  return NS_OK;
}

NS_IMETHODIMP nsMsgCompFields::SetBody(const nsAString& value) {
  m_body = value;
  return NS_OK;
}

NS_IMETHODIMP nsMsgCompFields::GetBody(nsAString& _retval) {
  _retval = m_body;
  return NS_OK;
}

NS_IMETHODIMP nsMsgCompFields::GetAttachments(
    nsTArray<RefPtr<nsIMsgAttachment>>& attachments) {
  attachments = m_attachments.Clone();
  return NS_OK;
}

NS_IMETHODIMP nsMsgCompFields::AddAttachment(nsIMsgAttachment* attachment) {
  // Don't add the same attachment twice.
  for (nsIMsgAttachment* a : m_attachments) {
    bool sameUrl;
    a->EqualsUrl(attachment, &sameUrl);
    if (sameUrl) return NS_OK;
  }
  m_attachments.AppendElement(attachment);
  return NS_OK;
}

/* void removeAttachment (in nsIMsgAttachment attachment); */
NS_IMETHODIMP nsMsgCompFields::RemoveAttachment(nsIMsgAttachment* attachment) {
  for (uint32_t i = 0; i < m_attachments.Length(); i++) {
    bool sameUrl;
    m_attachments[i]->EqualsUrl(attachment, &sameUrl);
    if (sameUrl) {
      m_attachments.RemoveElementAt(i);
      break;
    }
  }

  return NS_OK;
}

NS_IMETHODIMP nsMsgCompFields::SetOtherHeaders(
    const nsTArray<nsString>& headers) {
  m_otherHeaders = headers.Clone();
  return NS_OK;
}

NS_IMETHODIMP nsMsgCompFields::GetOtherHeaders(nsTArray<nsString>& headers) {
  headers = m_otherHeaders.Clone();
  return NS_OK;
}

/* void removeAttachments (); */
NS_IMETHODIMP nsMsgCompFields::RemoveAttachments() {
  m_attachments.Clear();
  return NS_OK;
}

// This method is called during the creation of a new window.
NS_IMETHODIMP
nsMsgCompFields::SplitRecipients(const nsAString& aRecipients,
                                 bool aEmailAddressOnly,
                                 nsTArray<nsString>& aResult) {
  nsCOMArray<msgIAddressObject> header(EncodedHeaderW(aRecipients));
  if (aEmailAddressOnly)
    ExtractEmails(header, aResult);
  else
    ExtractDisplayAddresses(header, aResult);

  return NS_OK;
}

// This method is called during the sending of message from
// nsMsgCompose::CheckAndPopulateRecipients()
nsresult nsMsgCompFields::SplitRecipientsEx(const nsAString& recipients,
                                            nsTArray<nsMsgRecipient>& aResult) {
  nsTArray<nsString> names, addresses;
  ExtractAllAddresses(EncodedHeaderW(recipients), names, addresses);

  uint32_t numAddresses = names.Length();
  for (uint32_t i = 0; i < numAddresses; ++i) {
    nsMsgRecipient msgRecipient;
    msgRecipient.mEmail = addresses[i];
    msgRecipient.mName = names[i];
    aResult.AppendElement(msgRecipient);
  }

  return NS_OK;
}

NS_IMETHODIMP nsMsgCompFields::ConvertBodyToPlainText() {
  nsresult rv = NS_OK;

  if (!m_body.IsEmpty()) {
    if (NS_SUCCEEDED(rv)) {
      bool flowed, formatted;
      GetSerialiserFlags(&flowed, &formatted);
      rv = ConvertBufToPlainText(m_body, flowed, formatted, true);
    }
  }
  return rv;
}

NS_IMETHODIMP nsMsgCompFields::GetComposeSecure(
    nsIMsgComposeSecure** aComposeSecure) {
  NS_ENSURE_ARG_POINTER(aComposeSecure);
  NS_IF_ADDREF(*aComposeSecure = mSecureCompFields);
  return NS_OK;
}

NS_IMETHODIMP nsMsgCompFields::SetComposeSecure(
    nsIMsgComposeSecure* aComposeSecure) {
  mSecureCompFields = aComposeSecure;
  return NS_OK;
}

NS_IMETHODIMP nsMsgCompFields::GetNeedToCheckCharset(bool* _retval) {
  NS_ENSURE_ARG_POINTER(_retval);
  *_retval = m_needToCheckCharset;
  return NS_OK;
}

NS_IMETHODIMP nsMsgCompFields::SetNeedToCheckCharset(bool aCheck) {
  m_needToCheckCharset = aCheck;
  return NS_OK;
}