Ejemplo n.º 1
0
// Test a message send????
nsresult nsEudoraCompose::SendTheMessage(nsIFile *pMailImportLocation, nsIFile **pMsg)
{
  nsresult rv = CreateComponents();
  if (NS_FAILED(rv))
    return rv;

  // IMPORT_LOG0("Outlook Compose created necessary components\n");

  nsString bodyType;
  nsString charSet;
  nsString headerVal;
  GetHeaderValue(m_pHeaders, m_headerLen, "From:", headerVal);
  if (!headerVal.IsEmpty())
    m_pMsgFields->SetFrom(headerVal);
  GetHeaderValue(m_pHeaders, m_headerLen, "To:", headerVal);
  if (!headerVal.IsEmpty())
    m_pMsgFields->SetTo(headerVal);
  GetHeaderValue(m_pHeaders, m_headerLen, "Subject:", headerVal);
  if (!headerVal.IsEmpty())
    m_pMsgFields->SetSubject(headerVal);
  GetHeaderValue(m_pHeaders, m_headerLen, "Content-type:", headerVal);
  bodyType = headerVal;
  ExtractType(bodyType);
  ExtractCharset(headerVal);
  // Use platform charset as default if the msg doesn't specify one
  // (ie, no 'charset' param in the Content-Type: header). As the last
  // resort we'll use the mail default charset.
  // (ie, no 'charset' param in the Content-Type: header) or if the
  // charset parameter fails a length sanity check.
  // As the last resort we'll use the mail default charset.
  if (headerVal.IsEmpty() || (headerVal.Length() > kContentTypeLengthSanityCheck))
  {
    headerVal.AssignASCII(nsMsgI18NFileSystemCharset());
    if (headerVal.IsEmpty())
    { // last resort
      if (m_defCharset.IsEmpty())
      {
        nsString defaultCharset;
        NS_GetLocalizedUnicharPreferenceWithDefault(nullptr, "mailnews.view_default_charset",
                                                    NS_LITERAL_STRING("ISO-8859-1"), defaultCharset);
        m_defCharset = defaultCharset;
      }
      headerVal = m_defCharset;
    }
  }
  m_pMsgFields->SetCharacterSet(NS_LossyConvertUTF16toASCII(headerVal).get());
  charSet = headerVal;
  GetHeaderValue(m_pHeaders, m_headerLen, "CC:", headerVal);
  if (!headerVal.IsEmpty())
    m_pMsgFields->SetCc(headerVal);
  GetHeaderValue(m_pHeaders, m_headerLen, "Message-ID:", headerVal);
  if (!headerVal.IsEmpty())
    m_pMsgFields->SetMessageId(NS_LossyConvertUTF16toASCII(headerVal).get());
  GetHeaderValue(m_pHeaders, m_headerLen, "Reply-To:", headerVal);
  if (!headerVal.IsEmpty())
    m_pMsgFields->SetReplyTo(headerVal);

  // what about all of the other headers?!?!?!?!?!?!
  char *pMimeType;
  if (!bodyType.IsEmpty())
    pMimeType = ToNewCString(NS_LossyConvertUTF16toASCII(bodyType));
  else
    pMimeType = ToNewCString(m_bodyType);

  nsCOMPtr<nsIArray> pAttach;
  GetLocalAttachments(getter_AddRefs(pAttach));
  nsEudoraEditor eudoraEditor(m_pBody, pMailImportLocation);
  nsCOMPtr<nsIArray> embeddedObjects;
  if (eudoraEditor.HasEmbeddedContent())
    eudoraEditor.GetEmbeddedObjects(getter_AddRefs(embeddedObjects));

  nsString uniBody;
  NS_CopyNativeToUnicode(nsDependentCString(m_pBody), uniBody);

  /*
    l10n - I have the body of the message in the system charset,
    I need to "encode" it to be the charset for the message
    *UNLESS* of course, I don't know what the charset of the message
    should be?  How do I determine what the charset should
    be if it doesn't exist?

  */

  nsCString body;

  rv = nsMsgI18NConvertFromUnicode(NS_LossyConvertUTF16toASCII(charSet).get(),
                                    uniBody, body);
  if (NS_FAILED(rv) && !charSet.Equals(m_defCharset)) {
    // in this case, if we did not use the default compose
    // charset, then try that.
    body.Truncate();
    rv = nsMsgI18NConvertFromUnicode(NS_LossyConvertUTF16toASCII(charSet).get(),
                                     uniBody, body);
  }
  uniBody.Truncate();


  // See if it's a draft msg (ie, no From: or no To: AND no Cc: AND no Bcc:).
  // Eudora saves sent and draft msgs in Out folder (ie, mixed) and it does
  // store Bcc: header in the msg itself.
  nsAutoString from, to, cc, bcc;
  rv = m_pMsgFields->GetFrom(from);
  rv = m_pMsgFields->GetTo(to);
  rv = m_pMsgFields->GetCc(cc);
  rv = m_pMsgFields->GetBcc(bcc);
  bool createAsDraft = from.IsEmpty() || (to.IsEmpty() && cc.IsEmpty() && bcc.IsEmpty());

  nsCOMPtr<nsIImportService> impService(do_GetService(NS_IMPORTSERVICE_CONTRACTID, &rv));
  NS_ENSURE_SUCCESS(rv, rv);

  rv = impService->CreateRFC822Message(
                        s_pIdentity,                  // dummy identity
                        m_pMsgFields,                 // message fields
                        pMimeType,                    // body type
                        body,                         // body pointer
                        createAsDraft,
                        pAttach,                      // local attachments
                        embeddedObjects,
                        m_pListener);                 // listener

  EudoraSendListener *pListen = (EudoraSendListener *)m_pListener;
  if (NS_FAILED(rv)) {
    IMPORT_LOG1("*** Error, CreateAndSendMessage FAILED: 0x%lx\n", rv);
    // IMPORT_LOG1("Headers: %80s\n", m_pHeaders);
  }
  else {
    // wait for the listener to get done!
    int32_t abortCnt = 0;
    int32_t cnt = 0;
    int32_t sleepCnt = 1;
    while (!pListen->m_done && (abortCnt < kHungAbortCount)) {
      PR_Sleep(sleepCnt);
      cnt++;
      if (cnt > kHungCount) {
        abortCnt++;
        sleepCnt *= 2;
        cnt = 0;
      }
    }

    if (abortCnt >= kHungAbortCount) {
      IMPORT_LOG0("**** Create and send message hung\n");
      IMPORT_LOG1("Headers: %s\n", m_pHeaders);
      IMPORT_LOG1("Body: %s\n", m_pBody);
      rv = NS_ERROR_FAILURE;
    }

  }

  if (pMimeType)
    NS_Free(pMimeType);

  if (pListen->m_location) {
    pListen->m_location->Clone(pMsg);
    rv = NS_OK;
  }
  else {
    rv = NS_ERROR_FAILURE;
    IMPORT_LOG0("*** Error, Outlook compose unsuccessful\n");
  }

  pListen->Reset();

  return rv;
}
nsresult MailEwsMsgCompose::SendTheMessage(ews_msg_item * msg_item,
                                           nsIMsgSendListener *pListener)
{
    nsresult rv = CreateComponents();
    if (NS_FAILED(rv))
        return rv;

    IMPORT_LOG0("Outlook Compose created necessary components\n");
    nsString bodyType;
    nsString charSet;
    nsString headerVal;

    RecipientToString(&msg_item->from, headerVal);
    if (!headerVal.IsEmpty())
        m_pMsgFields->SetFrom(headerVal);
    else {
        RecipientToString(&msg_item->sender, headerVal);
        if (!headerVal.IsEmpty())
            m_pMsgFields->SetFrom(headerVal);
    }
  
    RecipientToString2(msg_item->to_recipients,
                       msg_item->to_recipients_count,
                       headerVal);
    if (!headerVal.IsEmpty())
        m_pMsgFields->SetTo(headerVal);

    if (msg_item->item.subject) {
        char * key = msg_item->item.subject;
        uint32_t l = strlen(key);

        /* strip "Re: " */
        nsCString modifiedSubject;
        if (mailews::NS_MsgStripRE((const char **) &key,
                          &l,
                          modifiedSubject)) {
            mailews::NS_GetLocalizedUnicharPreferenceWithDefault(nullptr,
                                                        "mailnews.localizedRe",
                                                        EmptyString(),
                                                        headerVal);
            if (headerVal.IsEmpty()) {
                headerVal.AppendLiteral("Re");
            }
            
            headerVal.AppendLiteral(": ");
        } else {
            headerVal.AssignLiteral("");
        }

        if (modifiedSubject.IsEmpty()) {
            headerVal.AppendLiteral(key);
        } else {
            headerVal.AppendLiteral(modifiedSubject.get());
        }
        
        m_pMsgFields->SetSubject(headerVal);
    }
  
    if (msg_item->item.body_type == EWS_BODY_TEXT)
        bodyType.AssignLiteral("text/plain");
    else
        bodyType.AssignLiteral("text/html");
    headerVal.AssignLiteral("UTF-8");

    // Use platform charset as default if the msg doesn't specify one
    // (ie, no 'charset' param in the Content-Type: header). As the last
    // resort we'll use the mail default charset.
    // (ie, no 'charset' param in the Content-Type: header) or if the
    // charset parameter fails a length sanity check.
    // As the last resort we'll use the mail default charset.
    if (headerVal.IsEmpty() || (headerVal.Length() > kContentTypeLengthSanityCheck))
    {
        headerVal.AssignASCII(mailews::MsgI18NFileSystemCharset());
        if (headerVal.IsEmpty())
        { // last resort
            if (m_defCharset.IsEmpty())
            {
                nsString defaultCharset;
                mailews::NS_GetLocalizedUnicharPreferenceWithDefault(nullptr, "mailnews.view_default_charset",
                                                            NS_LITERAL_STRING("ISO-8859-1"), defaultCharset);
                m_defCharset = defaultCharset;
            }
            headerVal = m_defCharset;
        }
    }
    m_pMsgFields->SetCharacterSet(NS_LossyConvertUTF16toASCII(headerVal).get());
    charSet = headerVal;

    RecipientToString2(msg_item->cc_recipients,
                       msg_item->cc_recipients_count,
                       headerVal);
    if (!headerVal.IsEmpty())
        m_pMsgFields->SetCc(headerVal);

    RecipientToString2(msg_item->bcc_recipients,
                       msg_item->bcc_recipients_count,
                       headerVal);
    if (!headerVal.IsEmpty())
        m_pMsgFields->SetBcc(headerVal);

    if (msg_item->internet_message_id)
        m_pMsgFields->SetMessageId(msg_item->internet_message_id);
  
    RecipientToString2(msg_item->reply_to,
                       msg_item->reply_to_count,
                       headerVal);
    if (!headerVal.IsEmpty())
        m_pMsgFields->SetReplyTo(headerVal);

    if (msg_item->references)
        m_pMsgFields->SetReferences(msg_item->references);
  
    // what about all of the other headers?!?!?!?!?!?!
    char *pMimeType;
    if (!bodyType.IsEmpty())
        pMimeType = ToNewCString(NS_LossyConvertUTF16toASCII(bodyType));
    else
        pMimeType = strdup("text/plain");

    nsCOMPtr<nsIArray> pAttach;
    GetLocalAttachments(getter_AddRefs(pAttach));

    nsString uniBody;
    NS_CopyNativeToUnicode(nsDependentCString(msg_item->item.body), uniBody);

    /*
      l10n - I have the body of the message in the system charset,
      I need to "encode" it to be the charset for the message
      *UNLESS* of course, I don't know what the charset of the message
      should be?  How do I determine what the charset should
      be if it doesn't exist?

    */

    nsCString body;

    rv = mailews::MsgI18NConvertFromUnicode(NS_LossyConvertUTF16toASCII(charSet).get(),
                                     uniBody, body);
    if (NS_FAILED(rv) && !charSet.Equals(m_defCharset)) {
        // in this case, if we did not use the default compose
        // charset, then try that.
        body.Truncate();
        rv = mailews::MsgI18NConvertFromUnicode(NS_LossyConvertUTF16toASCII(charSet).get(),
                                         uniBody, body);
    }
    uniBody.Truncate();


    // See if it's a draft msg (ie, no From: or no To: AND no Cc: AND no Bcc:).
    // MailEws saves sent and draft msgs in Out folder (ie, mixed) and it does
    // store Bcc: header in the msg itself.
    nsAutoString from, to, cc, bcc;
    rv = m_pMsgFields->GetFrom(from);
    rv = m_pMsgFields->GetTo(to);
    rv = m_pMsgFields->GetCc(cc);
    rv = m_pMsgFields->GetBcc(bcc);
    bool createAsDraft = from.IsEmpty() || (to.IsEmpty() && cc.IsEmpty() && bcc.IsEmpty());

    nsCOMPtr<nsIImportService> impService(do_GetService(NS_IMPORTSERVICE_CONTRACTID, &rv));
    NS_ENSURE_SUCCESS(rv, rv);

    rv = impService->CreateRFC822Message(
        s_pIdentity,                  // dummy identity
        m_pMsgFields,                 // message fields
        pMimeType,                    // body type
        body,                         // body pointer
        createAsDraft,
        pAttach,                      // local attachments
        nullptr, //embededObjects
        pListener);                 // listener

    if (NS_FAILED(rv)) {
        IMPORT_LOG1("*** Error, CreateAndSendMessage FAILED: 0x%lx\n", (int)rv);
    }

    if (pMimeType)
        NS_Free(pMimeType);
    return rv;
}