summaryrefslogtreecommitdiff
path: root/shell/source/win32/simplemail/smplmailclient.cxx
blob: 45331098b8356fc2ba3e91e118a09957c6b57149 (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
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * 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/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

#include <config_folders.h>

#include <osl/diagnose.h>
#include <osl/process.h>
#include <rtl/bootstrap.hxx>
#include "smplmailclient.hxx"
#include "smplmailmsg.hxx"
#include <com/sun/star/system/SimpleMailClientFlags.hpp>
#include <com/sun/star/system/XSimpleMailMessage2.hpp>
#include <osl/file.hxx>
#include <o3tl/char16_t2wchar_t.hxx>
#include <tools/urlobj.hxx>
#include <unotools/pathoptions.hxx>
#include <unotools/syslocale.hxx>
#include <i18nlangtag/languagetag.hxx>

#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <mapi.h>
#if defined GetTempPath
#undef GetTempPath
#endif

#include <process.h>
#include <vector>

using css::uno::UNO_QUERY;
using css::uno::Reference;
using css::uno::Exception;
using css::uno::RuntimeException;
using css::uno::Sequence;
using css::lang::IllegalArgumentException;

using css::system::XSimpleMailClient;
using css::system::XSimpleMailMessage;
using css::system::XSimpleMailMessage2;
using css::system::SimpleMailClientFlags::NO_USER_INTERFACE;
using css::system::SimpleMailClientFlags::NO_LOGON_DIALOG;

const OUString TO("--to");
const OUString CC("--cc");
const OUString BCC("--bcc");
const OUString FROM("--from");
const OUString SUBJECT("--subject");
const OUString BODY("--body");
const OUString ATTACH("--attach");
const OUString ATTACH_NAME("--attach-name");
const OUString FLAG_MAPI_DIALOG("--mapi-dialog");
const OUString FLAG_MAPI_LOGON_UI("--mapi-logon-ui");
const OUString FLAG_LANGTAG("--langtag");
const OUString FLAG_BOOTSTRAP("--bootstrap");

namespace /* private */
{
    /** @internal
        look if an alternative program is configured
        which should be used as senddoc executable */
    OUString getAlternativeSenddocUrl()
    {
        OUString altSenddocUrl;
        HKEY hkey;
        LONG lret = RegOpenKeyW(HKEY_CURRENT_USER, L"Software\\LibreOffice\\SendAsEMailClient", &hkey);
        if (lret == ERROR_SUCCESS)
        {
            wchar_t buff[MAX_PATH];
            LONG sz = sizeof(buff);
            lret = RegQueryValueW(hkey, nullptr, buff, &sz);
            if (lret == ERROR_SUCCESS)
            {
                osl::FileBase::getFileURLFromSystemPath(o3tl::toU(buff), altSenddocUrl);
            }
            RegCloseKey(hkey);
        }
        return altSenddocUrl;
    }

    /**
        Returns the absolute file Url of the senddoc executable.

        @returns
        the absolute file Url of the senddoc executable. In case
        of an error an empty string will be returned.
    */
    OUString getSenddocUrl()
    {
        OUString senddocUrl = getAlternativeSenddocUrl();

        if (senddocUrl.isEmpty())
        {
            senddocUrl = "$BRAND_BASE_DIR/" LIBO_LIBEXEC_FOLDER "/senddoc.exe";
            rtl::Bootstrap::expandMacros(senddocUrl); //TODO: detect failure
        }
        return senddocUrl;
    }

    /**
        Execute Senddoc.exe which a MAPI wrapper.

        @param rCommandArgs
        [in] the arguments to be passed to Senddoc.exe

        @returns
        <TRUE/> on success.
    */
    bool executeSenddoc(const std::vector<OUString>& rCommandArgs, bool bWait)
    {
        OUString senddocUrl = getSenddocUrl();
        if (senddocUrl.getLength() == 0)
            return false;

        oslProcessOption nProcOption = osl_Process_DETACHED | (bWait ? osl_Process_WAIT : 0);

        oslProcess proc;

        /* for efficiency reasons we are using a 'bad' cast here
        as a vector or OUStrings is nothing else than
        an array of pointers to rtl_uString's */
        oslProcessError err = osl_executeProcess(
            senddocUrl.pData,
            const_cast<rtl_uString**>(reinterpret_cast<rtl_uString * const *>(rCommandArgs.data())),
            rCommandArgs.size(),
            nProcOption,
            nullptr,
            nullptr,
            nullptr,
            0,
            &proc);

        if (err != osl_Process_E_None)
            return false;

        if (!bWait)
            return true;

        oslProcessInfo procInfo;
        procInfo.Size = sizeof(oslProcessInfo);
        osl_getProcessInfo(proc, osl_Process_EXITCODE, &procInfo);
        osl_freeProcessHandle(proc);
        return (procInfo.Code == SUCCESS_SUCCESS);
    }
} // namespace private

Reference<XSimpleMailMessage> SAL_CALL CSmplMailClient::createSimpleMailMessage()
{
    return Reference<XSimpleMailMessage>(new CSmplMailMsg());
}

namespace {
// We cannot use the session-local temporary directory for the attachment,
// because it will get removed upon program exit; and it must be alive for
// senddoc process lifetime. So we use base temppath for the attachments,
// and let the senddoc to do the cleanup if it was started successfully.
// This function works like Desktop::CreateTemporaryDirectory()
OUString InitBaseTempDirURL()
{
    // No need to intercept an exception here, since
    // Desktop::CreateTemporaryDirectory() has ensured that path manager is available
    SvtPathOptions aOpt;
    OUString aRetURL = aOpt.GetTempPath();
    if (aRetURL.isEmpty())
    {
        osl::File::getTempDirURL(aRetURL);
    }
    if (aRetURL.endsWith("/"))
        aRetURL = aRetURL.copy(0, aRetURL.getLength() - 1);

    return aRetURL;
}

const OUString& GetBaseTempDirURL()
{
    static const OUString aRetURL(InitBaseTempDirURL());
    return aRetURL;
}
}

OUString CSmplMailClient::CopyAttachment(const OUString& sOrigAttachURL, OUString& sUserVisibleName)
{
    // We do two things here:
    // 1. Make the attachment temporary filename to not contain any fancy characters possible in
    // original filename, that could confuse mailer, and extract the original filename to explicitly
    // define it;
    // 2. Allow the copied files be outside of the session's temporary directory, and thus not be
    // removed in Desktop::RemoveTemporaryDirectory() if soffice process gets closed before the
    // mailer finishes using them.

    maAttachmentFiles.emplace_back(std::make_unique<utl::TempFile>(&GetBaseTempDirURL()));
    maAttachmentFiles.back()->EnableKillingFile();
    INetURLObject aFilePathObj(maAttachmentFiles.back()->GetURL());
    OUString sNewAttachmentURL = aFilePathObj.GetMainURL(INetURLObject::DecodeMechanism::NONE);
    if (osl::File::copy(sOrigAttachURL, sNewAttachmentURL) == osl::FileBase::RC::E_None)
    {
        INetURLObject url(sOrigAttachURL, INetURLObject::EncodeMechanism::WasEncoded);
        sUserVisibleName = url.getName(INetURLObject::LAST_SEGMENT, true,
            INetURLObject::DecodeMechanism::WithCharset);
    }
    else
    {
        // Failed to copy original; the best effort is to use original file. It is possible that
        // the file gets deleted before used in spawned process; but let's hope... the worst thing
        // is the absent attachment file anyway.
        sNewAttachmentURL = sOrigAttachURL;
        maAttachmentFiles.pop_back();
    }
    return sNewAttachmentURL;
}

void CSmplMailClient::ReleaseAttachments()
{
    for (auto& pTempFile : maAttachmentFiles)
    {
        if (pTempFile)
            pTempFile->EnableKillingFile(false);
    }
    maAttachmentFiles.clear();
}

/**
    Assemble a command line for SendDoc.exe out of the members
    of the supplied SimpleMailMessage.

    @param xSimpleMailMessage
    [in] the mail message.

    @param aFlags
    [in] different flags to be used with the simple mail service.

    @param rCommandArgs
    [in|out] a buffer for the command line arguments. The buffer
    is assumed to be empty.

    @throws css::lang::IllegalArgumentException
    if an invalid file URL has been detected in the attachment list.
*/
void CSmplMailClient::assembleCommandLine(
    const Reference<XSimpleMailMessage>& xSimpleMailMessage,
    sal_Int32 aFlag, std::vector<OUString>& rCommandArgs)
{
    OSL_ENSURE(rCommandArgs.empty(), "Provided command argument buffer not empty");

    Reference<XSimpleMailMessage2> xMessage( xSimpleMailMessage, UNO_QUERY );
    if (xMessage.is())
    {
        OUString body = xMessage->getBody();
        if (body.getLength()>0)
        {
            rCommandArgs.push_back(BODY);
            rCommandArgs.push_back(body);
        }
    }

    OUString to = xSimpleMailMessage->getRecipient();
    if (to.getLength() > 0)
    {
        rCommandArgs.push_back(TO);
        rCommandArgs.push_back(to);
    }

    Sequence<OUString> ccRecipients = xSimpleMailMessage->getCcRecipient();
    for (int i = 0; i < ccRecipients.getLength(); i++)
    {
        rCommandArgs.push_back(CC);
        rCommandArgs.push_back(ccRecipients[i]);
    }

    Sequence<OUString> bccRecipients = xSimpleMailMessage->getBccRecipient();
    for (int i = 0; i < bccRecipients.getLength(); i++)
    {
        rCommandArgs.push_back(BCC);
        rCommandArgs.push_back(bccRecipients[i]);
    }

    OUString from = xSimpleMailMessage->getOriginator();
    if (from.getLength() > 0)
    {
        rCommandArgs.push_back(FROM);
        rCommandArgs.push_back(from);
    }

    OUString subject = xSimpleMailMessage->getSubject();
    if (subject.getLength() > 0)
    {
        rCommandArgs.push_back(SUBJECT);
        rCommandArgs.push_back(subject);
    }

    auto const attachments = xSimpleMailMessage->getAttachement();
    for (const auto& attachment : attachments)
    {
        OUString sDisplayName;
        OUString sTempFileURL(CopyAttachment(attachment, sDisplayName));
        OUString sysPath;
        osl::FileBase::RC err = osl::FileBase::getSystemPathFromFileURL(sTempFileURL, sysPath);
        if (err != osl::FileBase::E_None)
            throw IllegalArgumentException(
                "Invalid attachment file URL",
                static_cast<XSimpleMailClient*>(this),
                1);

        rCommandArgs.push_back(ATTACH);
        rCommandArgs.push_back(sysPath);
        if (!sDisplayName.isEmpty())
        {
            rCommandArgs.push_back(ATTACH_NAME);
            rCommandArgs.push_back(sDisplayName);
        }
    }

    if (!(aFlag & NO_USER_INTERFACE))
        rCommandArgs.push_back(FLAG_MAPI_DIALOG);

    if (!(aFlag & NO_LOGON_DIALOG))
        rCommandArgs.push_back(FLAG_MAPI_LOGON_UI);

    rCommandArgs.push_back(FLAG_LANGTAG);
    rCommandArgs.push_back(SvtSysLocale().GetUILanguageTag().getBcp47());

    rtl::Bootstrap aBootstrap;
    OUString sBootstrapPath;
    aBootstrap.getIniName(sBootstrapPath);
    if (!sBootstrapPath.isEmpty())
    {
        rCommandArgs.push_back(FLAG_BOOTSTRAP);
        rCommandArgs.push_back(sBootstrapPath);
    }

}

void SAL_CALL CSmplMailClient::sendSimpleMailMessage(
    const Reference<XSimpleMailMessage>& xSimpleMailMessage, sal_Int32 aFlag)
{
    validateParameter(xSimpleMailMessage, aFlag);

    std::vector<OUString> senddocParams;
    assembleCommandLine(xSimpleMailMessage, aFlag, senddocParams);

    const bool bWait = aFlag & NO_USER_INTERFACE;
    if (!executeSenddoc(senddocParams, bWait))
        throw Exception(
            "Send email failed",
            static_cast<XSimpleMailClient*>(this));
    // Let the launched senddoc to cleanup the attachments temporary files
    if (!bWait)
        ReleaseAttachments();
}

void CSmplMailClient::validateParameter(
    const Reference<XSimpleMailMessage>& xSimpleMailMessage, sal_Int32 aFlag )
{
    if (!xSimpleMailMessage.is())
        throw IllegalArgumentException(
            "Empty mail message reference",
            static_cast<XSimpleMailClient*>(this),
            1);

    OSL_ENSURE(!(aFlag & NO_LOGON_DIALOG), "Flag NO_LOGON_DIALOG has currently no effect");

    // check the flags, the allowed range is 0 - (2^n - 1)
    if (aFlag < 0 || aFlag > 3)
        throw IllegalArgumentException(
            "Invalid flag value",
            static_cast<XSimpleMailClient*>(this),
            2);

    // check if a recipient is specified of the flags NO_USER_INTERFACE is specified
    if ((aFlag & NO_USER_INTERFACE) && !xSimpleMailMessage->getRecipient().getLength())
        throw IllegalArgumentException(
            "No recipient specified",
            static_cast<XSimpleMailClient*>(this),
            1);
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */