Bug 1521007 - Support UTF-8 encoded MAPI data to avoid broken document names when sending documents from MS Word. r+a=jorgk
authorMike Kaganski <mikekaganski@gmail.com>
Thu, 17 Jan 2019 21:31:00 +0100
changeset 33951 4882e4a21d50cf2a76e2fb9087f3d44c6018ce58
parent 33950 d6f7c0483b263905ed42ceebb5d61e0a5e5448a2
child 33952 18b2c9cbcf7c3adc5c1e2c89e401363987c92174
push id388
push userclokep@gmail.com
push dateMon, 28 Jan 2019 20:54:56 +0000
bugs1521007
Bug 1521007 - Support UTF-8 encoded MAPI data to avoid broken document names when sending documents from MS Word. r+a=jorgk
mailnews/mapi/mapihook/src/msgMapiHook.cpp
mailnews/mapi/mapihook/src/msgMapiHook.h
--- a/mailnews/mapi/mapihook/src/msgMapiHook.cpp
+++ b/mailnews/mapi/mapihook/src/msgMapiHook.cpp
@@ -341,17 +341,17 @@ nsresult nsMapiHook::BlindSendMail (unsi
     PR_CExitMonitor(pSendListener);
     NS_ProcessPendingEvents(thread);
   }
 
   return rv ;
 }
 
 nsresult nsMapiHook::HandleAttachments (nsIMsgCompFields * aCompFields, int32_t aFileCount,
-                                        lpnsMapiFileDesc aFiles)
+                                        lpnsMapiFileDesc aFiles, bool bUTF8)
 {
     nsresult rv = NS_OK ;
     // Do nothing if there are no files to process.
     if (!aFiles || aFileCount <= 0)
         return NS_OK;
 
     nsAutoCString Attachments ;
     nsAutoCString TempFiles ;
@@ -390,18 +390,21 @@ nsresult nsMapiHook::HandleAttachments (
 
             nsAutoString leafName ;
             // convert to Unicode using Platform charset
             // leafName already contains a unicode leafName from lpszPathName. If we were given
             // a value for lpszFileName, use it. Otherwise stick with leafName
             if (aFiles[i].lpszFileName)
             {
                 nsAutoString wholeFileName;
-                NS_CopyNativeToUnicode(nsDependentCString((char *)aFiles[i].lpszFileName),
-                                       wholeFileName);
+                if (!bUTF8)
+                    NS_CopyNativeToUnicode(nsDependentCString(aFiles[i].lpszFileName),
+                                           wholeFileName);
+                else
+                    wholeFileName.Append(NS_ConvertUTF8toUTF16(aFiles[i].lpszFileName));
                 // need to find the last '\' and find the leafname from that.
                 int32_t lastSlash = wholeFileName.RFindChar(char16_t('\\'));
                 if (lastSlash != kNotFound)
                   leafName.Assign(Substring(wholeFileName, lastSlash + 1));
                 else
                   leafName.Assign(wholeFileName);
             }
             else
@@ -564,21 +567,25 @@ nsresult nsMapiHook::HandleAttachmentsW(
   return rv ;
 }
 
 // this is used to convert non Unicode data and then populate comp fields
 nsresult nsMapiHook::PopulateCompFieldsWithConversion(lpnsMapiMessage aMessage,
                                     nsIMsgCompFields * aCompFields)
 {
   nsresult rv = NS_OK;
+  bool bUTF8 = aMessage->ulReserved == CP_UTF8;
 
   if (aMessage->lpOriginator)
   {
     nsAutoString From;
-    From.Append(NS_ConvertASCIItoUTF16((char *) aMessage->lpOriginator->lpszAddress));
+    if (!bUTF8)
+        From.Append(NS_ConvertASCIItoUTF16(aMessage->lpOriginator->lpszAddress));
+    else
+        From.Append(NS_ConvertUTF8toUTF16(aMessage->lpOriginator->lpszAddress));
     aCompFields->SetFrom (From);
   }
 
   nsAutoString To;
   nsAutoString Cc;
   nsAutoString Bcc;
   NS_NAMED_LITERAL_STRING(Comma, ",");
   if (aMessage->lpRecips)
@@ -592,29 +599,38 @@ nsresult nsMapiHook::PopulateCompFieldsW
         if (!PL_strncasecmp(addressWithoutType, "SMTP:", 5))
           addressWithoutType += 5;
 
         switch (aMessage->lpRecips[i].ulRecipClass)
         {
         case MAPI_TO :
           if (!To.IsEmpty())
             To += Comma ;
-          To.Append(NS_ConvertASCIItoUTF16(addressWithoutType));
+          if (!bUTF8)
+              To.Append(NS_ConvertASCIItoUTF16(addressWithoutType));
+          else
+              To.Append(NS_ConvertUTF8toUTF16(addressWithoutType));
           break ;
 
         case MAPI_CC :
           if (!Cc.IsEmpty())
             Cc += Comma ;
-          Cc.Append(NS_ConvertASCIItoUTF16(addressWithoutType));
+          if (!bUTF8)
+              Cc.Append(NS_ConvertASCIItoUTF16(addressWithoutType));
+          else
+              Cc.Append(NS_ConvertUTF8toUTF16(addressWithoutType));
           break ;
 
         case MAPI_BCC :
           if (!Bcc.IsEmpty())
               Bcc += Comma ;
-          Bcc.Append(NS_ConvertASCIItoUTF16(addressWithoutType));
+          if (!bUTF8)
+              Bcc.Append(NS_ConvertASCIItoUTF16(addressWithoutType));
+          else
+              Bcc.Append(NS_ConvertUTF8toUTF16(addressWithoutType));
           break ;
         }
       }
     }
   }
 
   // set To, Cc, Bcc
   aCompFields->SetTo (To) ;
@@ -622,32 +638,38 @@ nsresult nsMapiHook::PopulateCompFieldsW
   aCompFields->SetBcc (Bcc) ;
 
   MOZ_LOG(MAPI, mozilla::LogLevel::Debug, ("to: %s cc: %s bcc: %s \n", NS_ConvertUTF16toUTF8(To).get(), NS_ConvertUTF16toUTF8(Cc).get(), NS_ConvertUTF16toUTF8(Bcc).get()));
 
   // set subject
   if (aMessage->lpszSubject)
   {
     nsAutoString Subject ;
-    rv = NS_CopyNativeToUnicode(nsDependentCString((char *) aMessage->lpszSubject),
-                                Subject);
+    if (!bUTF8)
+        rv = NS_CopyNativeToUnicode(nsDependentCString(aMessage->lpszSubject),
+                                    Subject);
+    else
+        Subject.Append(NS_ConvertUTF8toUTF16(aMessage->lpszSubject));
     if (NS_FAILED(rv)) return rv;
     aCompFields->SetSubject(Subject);
   }
 
   // handle attachments as File URL
-  rv = HandleAttachments(aCompFields, aMessage->nFileCount, aMessage->lpFiles);
+  rv = HandleAttachments(aCompFields, aMessage->nFileCount, aMessage->lpFiles, bUTF8);
   if (NS_FAILED(rv)) return rv ;
 
   // set body
   if (aMessage->lpszNoteText)
   {
     nsAutoString Body ;
-    rv = NS_CopyNativeToUnicode(nsDependentCString((char *) aMessage->lpszNoteText),
-                                Body);
+    if (!bUTF8)
+        rv = NS_CopyNativeToUnicode(nsDependentCString(aMessage->lpszNoteText),
+                                    Body);
+    else
+        Body.Append(NS_ConvertUTF8toUTF16(aMessage->lpszNoteText));
     if (NS_FAILED(rv)) return rv ;
     if (Body.IsEmpty() || Body.Last() != '\n')
       Body.AppendLiteral(CRLF);
 
     // This is needed when Simple MAPI is used without a compose window.
     // See bug 1366196.
     if (Body.Find("<html>") == kNotFound)
       aCompFields->SetForcePlainText(true);
--- a/mailnews/mapi/mapihook/src/msgMapiHook.h
+++ b/mailnews/mapi/mapihook/src/msgMapiHook.h
@@ -18,18 +18,18 @@ class nsMapiHook
         static bool IsBlindSendAllowed () ;
         static nsresult BlindSendMail (unsigned long aSession, nsIMsgCompFields * aCompFields) ;
         static nsresult ShowComposerWindow (unsigned long aSession, nsIMsgCompFields * aCompFields) ;
         static nsresult PopulateCompFieldsWithConversion(lpnsMapiMessage aMessage,
                                         nsIMsgCompFields * aCompFields) ;
         static nsresult PopulateCompFieldsW(lpnsMapiMessageW aMessage, nsIMsgCompFields *aCompFields);
         static nsresult PopulateCompFieldsForSendDocs(nsIMsgCompFields * aCompFields,
                                         ULONG aFlags, LPTSTR aDelimChar, LPTSTR aFilePaths) ;
-        static nsresult HandleAttachments (nsIMsgCompFields * aCompFields, int32_t aFileCount,
-                                           lpnsMapiFileDesc aFiles) ;
+        static nsresult HandleAttachments(nsIMsgCompFields *aCompFields, int32_t aFileCount,
+                                          lpnsMapiFileDesc aFiles, bool bUTF8);
         static nsresult HandleAttachmentsW(nsIMsgCompFields *aCompFields, int32_t aFileCount,
                                            lpnsMapiFileDescW aFiles);
         static void CleanUp();
 
         static bool isMapiService;
 };
 
 #endif  // MSG_MAPI_HOOK_H_