[PATCH v2] winemapi: Directly use xdg-email if available, enabling file attachments.

Jeremy White jwhite at codeweavers.com
Thu Nov 17 13:43:19 CST 2016


Signed-off-by: Jeremy White <jwhite at codeweavers.com>
---
This addresses https://bugs.winehq.org/show_bug.cgi?id=11770 for systems with xdg-email.
v2: Remove memory leak, check allocations, do not test for an executable xdg-email prior
to invocation, keep the changes in sendmail.c, and increment a pointer correctly when
combining file paths.
---
 dlls/winemapi/sendmail.c | 230 ++++++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 216 insertions(+), 14 deletions(-)

diff --git a/dlls/winemapi/sendmail.c b/dlls/winemapi/sendmail.c
index 03a29ba..3b3a88c 100644
--- a/dlls/winemapi/sendmail.c
+++ b/dlls/winemapi/sendmail.c
@@ -69,24 +69,102 @@ static char *escape_string(char *in, char *empty_string)
     return escaped ? escaped : empty_string;
 }
 
+static ULONG add_argument(char **argv, int *argc, const char *arg, const char *param)
+{
+    argv[(*argc)] = HeapAlloc(GetProcessHeap(), 0, strlen(arg) + 1);
+    if (!argv[(*argc)])
+        return MAPI_E_INSUFFICIENT_MEMORY;
+    strcpy(argv[(*argc)++], arg);
+
+    if (param)
+    {
+        argv[(*argc)] = HeapAlloc(GetProcessHeap(), 0, strlen(param) + 1);
+        if (!argv[(*argc)])
+            return MAPI_E_INSUFFICIENT_MEMORY;
+        strcpy(argv[(*argc)++], param);
+    }
+
+    return SUCCESS_SUCCESS;
+}
+
+static ULONG add_target(char **argv, int *argc, ULONG class, const char *address)
+{
+    static const char smtp[] = "smtp:";
+
+    if (!strncasecmp(address, smtp, sizeof(smtp) - 1))
+        address += sizeof(smtp) - 1;
+
+    switch (class)
+    {
+        case MAPI_ORIG:
+            TRACE("From: %s\n (unused)", debugstr_a(address));
+            break;
+
+        case MAPI_TO:
+            TRACE("To: %s\n", debugstr_a(address));
+            return add_argument(argv, argc, address, NULL);
+
+        case MAPI_CC:
+            TRACE("CC: %s\n", debugstr_a(address));
+            return add_argument(argv, argc, "--cc", address);
+
+        case MAPI_BCC:
+            TRACE("BCC: %s\n", debugstr_a(address));
+            return add_argument(argv, argc, "--bcc", address);
+
+        default:
+            TRACE("Unknown recipient class: %d\n", class);
+    }
+
+    return SUCCESS_SUCCESS;
+}
+
+static ULONG add_file(char **argv, int *argc, const char *path, const char *file)
+{
+    WCHAR *fullname, *p;
+    char *unixpath;
+    int namelen = 1;
+    ULONG ret;
+
+    if (path)
+        namelen += strlen(path) + 1;
+    if (file)
+        namelen += strlen(file);
+
+    p = fullname = HeapAlloc(GetProcessHeap(), 0, namelen * sizeof(WCHAR));
+    if (!fullname)
+        return MAPI_E_INSUFFICIENT_MEMORY;
+    memset(fullname, 0, namelen * sizeof(WCHAR));
+    if (path)
+    {
+        MultiByteToWideChar(CP_ACP, 0, path, -1, p, namelen);
+        p += strlen(path);
+        MultiByteToWideChar(CP_ACP, 0, "\\", 1, p, 1);
+        p++;
+    }
+    if (file)
+        MultiByteToWideChar(CP_ACP, 0, file, -1, p, namelen - (p - fullname));
+
+    unixpath = wine_get_unix_file_name(fullname);
+    if (!unixpath)
+        TRACE("Cannot find unix path of '%s'; not attaching.\n", debugstr_w(fullname));
+    HeapFree(GetProcessHeap(), 0, fullname);
+    if (!unixpath)
+        return MAPI_E_FAILURE;
+
+    ret = add_argument(argv, argc, "--attach", unixpath);
+    HeapFree(GetProcessHeap(), 0, unixpath);
+
+    return ret;
+}
+
 /**************************************************************************
- *  MAPISendMail
+ *  BrowserSendMail
  *
- * Send a message using a native mail client.
- *
- * PARAMS
- *  session  [I] Handle to a MAPI session.
- *  uiparam  [I] Parent window handle.
- *  message  [I] Pointer to a MAPIMessage structure.
- *  flags    [I] Flags.
- *  reserved [I] Reserved, pass 0.
- *
- * RETURNS
- *  Success: SUCCESS_SUCCESS
- *  Failure: MAPI_E_FAILURE
+ * Send an email by forming a mailto uri and invoking a browser.
  *
  */
-ULONG WINAPI MAPISendMail(LHANDLE session, ULONG_PTR uiparam,
+static ULONG BrowserSendMail(LHANDLE session, ULONG_PTR uiparam,
     lpMapiMessage message, FLAGS flags, ULONG reserved)
 {
     ULONG ret = MAPI_E_FAILURE;
@@ -291,6 +369,130 @@ exit:
     return ret;
 }
 
+/**************************************************************************
+ *  XDGSendMail
+ *
+ * Send a message using xdg-email mail client.
+ *
+ */
+ULONG XDGSendMail(LHANDLE session, ULONG_PTR uiparam,
+    lpMapiMessage message, FLAGS flags, ULONG reserved)
+{
+    int i;
+    int argc = 0;
+    int max_args;
+    char **argv = NULL;
+    ULONG ret;
+
+    TRACE("(0x%08lx 0x%08lx %p 0x%08x 0x%08x)\n", session, uiparam, message, flags, reserved);
+
+    if (!message)
+        return MAPI_E_FAILURE;
+
+    max_args = 1 + (2 + message->nRecipCount + message->nFileCount) * 2;
+    argv = HeapAlloc(GetProcessHeap(), 0, (max_args + 1) * sizeof(*argv));
+    if (!argv)
+        return MAPI_E_INSUFFICIENT_MEMORY;
+
+    memset(argv, 0, (max_args + 1) * sizeof(*argv));
+
+    ret = add_argument(argv, &argc, "xdg-email", NULL);
+    if (ret != SUCCESS_SUCCESS)
+        goto exit;
+
+    if (message->lpOriginator)
+        TRACE("From: %s (unused)\n", debugstr_a(message->lpOriginator->lpszAddress));
+
+    for (i = 0; i < message->nRecipCount; i++)
+    {
+        if (!message->lpRecips)
+        {
+            WARN("Recipient %d missing\n", i);
+            ret = MAPI_E_FAILURE;
+            goto exit;
+        }
+
+        if (message->lpRecips[i].lpszAddress)
+        {
+            ret = add_target(argv, &argc, message->lpRecips[i].ulRecipClass,
+                        message->lpRecips[i].lpszAddress);
+            if (ret != SUCCESS_SUCCESS)
+                goto exit;
+        }
+        else
+            FIXME("Name resolution and entry identifiers not supported\n");
+    }
+
+    for (i = 0; i < message->nFileCount; i++)
+    {
+        TRACE("File Path: %s, name %s\n", debugstr_a(message->lpFiles[i].lpszPathName),
+                debugstr_a(message->lpFiles[i].lpszFileName));
+        ret = add_file(argv, &argc, message->lpFiles[i].lpszPathName, message->lpFiles[i].lpszFileName);
+        if (ret != SUCCESS_SUCCESS)
+            goto exit;
+    }
+
+    if (message->lpszSubject)
+    {
+        TRACE("Subject: %s\n", debugstr_a(message->lpszSubject));
+        ret = add_argument(argv, &argc, "--subject", message->lpszSubject);
+        if (ret != SUCCESS_SUCCESS)
+            goto exit;
+    }
+
+    if (message->lpszNoteText)
+    {
+        TRACE("Body: %s\n", debugstr_a(message->lpszNoteText));
+        ret = add_argument(argv, &argc, "--body", message->lpszNoteText);
+        if (ret != SUCCESS_SUCCESS)
+            goto exit;
+    }
+
+    TRACE("Executing xdg-email; parameters:\n");
+    for (i = 0; i < argc; i++)
+        TRACE(" %d: [%s]\n", i, argv[i]);
+    if (_spawnvp(_P_WAIT, "xdg-email", (const char **) argv) == 0)
+        ret = SUCCESS_SUCCESS;
+    else
+        ret = MAPI_E_FAILURE;
+
+exit:
+    for (i = 0; i < argc; i++)
+        HeapFree(GetProcessHeap(), 0, argv[i]);
+    HeapFree(GetProcessHeap(), 0, argv);
+
+    return ret;
+}
+
+/**************************************************************************
+ *  MAPISendMail
+ *
+ * Send a message using a native mail client.
+ *
+ * PARAMS
+ *  session  [I] Handle to a MAPI session.
+ *  uiparam  [I] Parent window handle.
+ *  message  [I] Pointer to a MAPIMessage structure.
+ *  flags    [I] Flags.
+ *  reserved [I] Reserved, pass 0.
+ *
+ * RETURNS
+ *  Success: SUCCESS_SUCCESS
+ *  Failure: MAPI_E_FAILURE
+ *
+ */
+ULONG WINAPI MAPISendMail(LHANDLE session, ULONG_PTR uiparam,
+    lpMapiMessage message, FLAGS flags, ULONG reserved)
+{
+    ULONG ret;
+
+    ret = XDGSendMail(session, uiparam, message, flags, reserved);
+    if (ret != SUCCESS_SUCCESS)
+        ret = BrowserSendMail(session, uiparam, message, flags, reserved);
+
+    return ret;
+}
+
 ULONG WINAPI MAPISendDocuments(ULONG_PTR uiparam, LPSTR delim, LPSTR paths,
     LPSTR filenames, ULONG reserved)
 {
-- 
2.1.4




More information about the wine-patches mailing list