Bug 1310738: Improve user experience for email creation on command line. r=jorgk
authorManuel Griessmayr <abspack@posteo.de>
Wed, 19 Oct 2016 07:54:00 +0200
changeset 20581 b34a52928c121eac4e7087c499d580e1e809e6bf
parent 20580 b55be7abda35fbb5ec5809d8c28ee813b85eb454
child 20582 652110fac32ecde08024215d58d45d1c950b69cd
push id12435
push usermozilla@jorgk.com
push dateWed, 19 Oct 2016 22:01:49 +0000
treeherdercomm-central@324728b47409 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjorgk
bugs1310738
Bug 1310738: Improve user experience for email creation on command line. r=jorgk
mail/components/compose/content/MsgComposeCommands.js
mailnews/compose/src/nsMsgComposeService.cpp
--- a/mail/components/compose/content/MsgComposeCommands.js
+++ b/mail/components/compose/content/MsgComposeCommands.js
@@ -2345,26 +2345,45 @@ function ComposeStartup(aParams)
     params.composeFields = Components.classes["@mozilla.org/messengercompose/composefields;1"].createInstance(Components.interfaces.nsIMsgCompFields);
 
     if (args) { //Convert old fashion arguments into params
       var composeFields = params.composeFields;
       if (args.bodyislink == "true")
         params.bodyIsLink = true;
       if (args.type)
         params.type = args.type;
-      // Only use valid values.
-      if (args.format &&
-          (args.format == Components.interfaces.nsIMsgCompFormat.PlainText ||
-           args.format == Components.interfaces.nsIMsgCompFormat.HTML ||
-           args.format == Components.interfaces.nsIMsgCompFormat.OppositeOfDefault))
-        params.format = args.format;
+      if (args.format)
+      {
+        // Only use valid values.
+        if (args.format == Components.interfaces.nsIMsgCompFormat.PlainText ||
+            args.format == Components.interfaces.nsIMsgCompFormat.HTML ||
+            args.format == Components.interfaces.nsIMsgCompFormat.OppositeOfDefault)
+          params.format = args.format;
+        else if (args.format.toLowerCase().trim() == "html")
+          params.format = Components.interfaces.nsIMsgCompFormat.HTML;
+        else if (args.format.toLowerCase().trim() == "text")
+          params.format = Components.interfaces.nsIMsgCompFormat.PlainText;
+      }
       if (args.originalMsg)
         params.originalMsgURI = args.originalMsg;
       if (args.preselectid)
         params.identity = getIdentityForKey(args.preselectid);
+      else if (args.from) {
+        let identities = MailServices.accounts.allIdentities;
+        let enumerator = identities.enumerate();
+        let ident = {};
+
+        while (enumerator.hasMoreElements()) {
+          ident = enumerator.getNext();
+          if (args.from.toLowerCase().trim() == ident.email.toLowerCase()) {
+            params.identity = ident;
+            break;
+          }
+        }
+      }
       if (args.to)
         composeFields.to = args.to;
       if (args.cc)
         composeFields.cc = args.cc;
       if (args.bcc)
         composeFields.bcc = args.bcc;
       if (args.newsgroups)
         composeFields.newsgroups = args.newsgroups;
@@ -2453,18 +2472,22 @@ function ComposeStartup(aParams)
           } finally {
             if (cstream)
               cstream.close();
             if (fstream)
               fstream.close();
           }
 
           if (data) {
+            let pos = data.search(/\S/); // Find first non-whitespace character.
+
             if (params.format != Components.interfaces.nsIMsgCompFormat.PlainText &&
-                (args.message.endsWith(".htm") || args.message.endsWith(".html"))) {
+                (args.message.endsWith(".htm") || args.message.endsWith(".html") ||
+                 data.substr(pos, 14).toLowerCase() == "<!doctype html" ||
+                 data.substr(pos, 5).toLowerCase() == "<html")) {
               // We replace line breaks because otherwise they'll be converted to
               // <br> in nsMsgCompose::BuildBodyMessageAndSignature().
               // Don't do the conversion if the user asked explicitly for plain text.
               data = data.replace(/\r?\n/g," ");
             }
             gBodyFromArgs = true;
             composeFields.body = data;
           }
--- a/mailnews/compose/src/nsMsgComposeService.cpp
+++ b/mailnews/compose/src/nsMsgComposeService.cpp
@@ -301,17 +301,17 @@ nsMsgComposeService::GetOrigWindowSelect
 
       nsCOMPtr<nsILineBreaker> lineBreaker = do_GetService(NS_LBRK_CONTRACTID, &rv);
 
       if (NS_SUCCEEDED(rv))
       {
         const uint32_t length = selPlain.Length();
         const char16_t* unicodeStr = selPlain.get();
         int32_t endWordPos = lineBreaker->Next(unicodeStr, length, 0);
-        
+
         // If there's not even one word, then there's not multiple words
         if (endWordPos == NS_LINEBREAKER_NEED_MORE_TEXT)
           return NS_ERROR_ABORT;
 
         // If after the first word is only space, then there's not multiple words
         const char16_t* end;
         for (end = unicodeStr + endWordPos; NS_IsSpace(*end); end++)
           ;
@@ -923,17 +923,17 @@ NS_IMETHODIMP nsMsgComposeService::Reply
 
   nsCOMPtr <nsIMsgFolder> templateFolder;
   nsCOMPtr <nsIMsgDatabase> templateDB;
   nsCString templateMsgHdrUri;
   const char * query = PL_strstr(templateUri, "?messageId=");
   if (!query)
     return NS_ERROR_FAILURE;
 
-  nsAutoCString folderUri(Substring(templateUri, query)); 
+  nsAutoCString folderUri(Substring(templateUri, query));
   rv = GetExistingFolder(folderUri, getter_AddRefs(templateFolder));
   NS_ENSURE_SUCCESS(rv, rv);
   rv = templateFolder->GetMsgDatabase(getter_AddRefs(templateDB));
   NS_ENSURE_SUCCESS(rv, rv);
 
   const char *subject = PL_strstr(templateUri, "&subject=");
   if (subject)
   {
@@ -1247,17 +1247,17 @@ nsMsgComposeService::LoadDraftOrTemplate
 {
   return RunMessageThroughMimeDraft(aMsgURI, aOutType, aIdentity,
                                     aOriginalMsgURI, aOrigMsgHdr,
                                     aForwardInline, EmptyString(),
                                     overrideComposeFormat, aMsgWindow);
 }
 
 /**
- * Run the aMsgURI message through libmime. We set various attributes of the 
+ * Run the aMsgURI message through libmime. We set various attributes of the
  * nsIMimeStreamConverter so mimedrft.cpp will know what to do with the message
  * when its done streaming. Usually that will be opening a compose window
  * with the contents of the message, but if forwardTo is non-empty, mimedrft.cpp
  * will forward the contents directly.
  *
  * @param aMsgURI URI to stream, which is the msgUri + any extra terms, e.g.,
  *                "redirect=true".
  * @param aOutType  nsMimeOutput::nsMimeMessageDraftOrTemplate or
@@ -1413,17 +1413,18 @@ nsMsgComposeService::Handle(nsICommandLi
         StringBeginsWith(uristr, NS_LITERAL_STRING("to="))  ||
         StringBeginsWith(uristr, NS_LITERAL_STRING("cc="))  ||
         StringBeginsWith(uristr, NS_LITERAL_STRING("bcc=")) ||
         StringBeginsWith(uristr, NS_LITERAL_STRING("newsgroups=")) ||
         StringBeginsWith(uristr, NS_LITERAL_STRING("subject=")) ||
         StringBeginsWith(uristr, NS_LITERAL_STRING("format=")) ||
         StringBeginsWith(uristr, NS_LITERAL_STRING("body="))  ||
         StringBeginsWith(uristr, NS_LITERAL_STRING("attachment=")) ||
-        StringBeginsWith(uristr, NS_LITERAL_STRING("message="))) {
+        StringBeginsWith(uristr, NS_LITERAL_STRING("message=")) ||
+        StringBeginsWith(uristr, NS_LITERAL_STRING("from="))) {
       composeShouldHandle = true; // the -url argument looks like mailto
       end++;
       // mailto: URIs are frequently passed with spaces in them. They should be
       // escaped with %20, but we hack around broken clients. See bug 231032.
       while (end + 1 < count) {
         nsAutoString curarg;
         aCmdLine->GetArgument(end + 1, curarg);
         if (curarg.First() == '-')
@@ -1456,12 +1457,16 @@ nsMsgComposeService::Handle(nsICommandLi
     aCmdLine->SetPreventDefault(true);
   }
   return NS_OK;
 }
 
 NS_IMETHODIMP
 nsMsgComposeService::GetHelpInfo(nsACString& aResult)
 {
-  aResult.Assign(NS_LITERAL_CSTRING("  -compose           Compose a mail or news message.\n"));
+  aResult.AssignLiteral(
+    "  -compose [ <options> ] Compose a mail or news message. Options are specified\n"
+    "                     as string \"option='value,...',option=value,...\" and\n"
+    "                     include: from, to, cc, bcc, newsgroups, subject, body,\n"
+    "                     message (file), attachment (file), format (html | text).\n"
+    "                     Example: \"to=john@example.com,subject='Dinner tonight?'\"\n");
   return NS_OK;
 }
-