diff options
Diffstat (limited to 'shell/source/win32/simplemail')
-rw-r--r-- | shell/source/win32/simplemail/senddoc.cxx | 474 | ||||
-rw-r--r-- | shell/source/win32/simplemail/smplmail.component | 26 | ||||
-rw-r--r-- | shell/source/win32/simplemail/smplmailclient.cxx | 388 | ||||
-rw-r--r-- | shell/source/win32/simplemail/smplmailclient.hxx | 50 | ||||
-rw-r--r-- | shell/source/win32/simplemail/smplmailmsg.cxx | 102 | ||||
-rw-r--r-- | shell/source/win32/simplemail/smplmailmsg.hxx | 82 | ||||
-rw-r--r-- | shell/source/win32/simplemail/smplmailsuppl.cxx | 84 | ||||
-rw-r--r-- | shell/source/win32/simplemail/smplmailsuppl.hxx | 58 |
8 files changed, 1264 insertions, 0 deletions
diff --git a/shell/source/win32/simplemail/senddoc.cxx b/shell/source/win32/simplemail/senddoc.cxx new file mode 100644 index 0000000000..e34412cbfa --- /dev/null +++ b/shell/source/win32/simplemail/senddoc.cxx @@ -0,0 +1,474 @@ +/* -*- 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 <osl/diagnose.h> +#include <sal/macros.h> + +#include <o3tl/char16_t2wchar_t.hxx> +#include <rtl/bootstrap.hxx> +#include <sfx2/strings.hrc> +#include <unotools/resmgr.hxx> +#include <i18nlangtag/languagetag.hxx> + +#include <wchar.h> +#define WIN32_LEAN_AND_MEAN +#include <windows.h> +#include <mapi.h> +#include <MapiUnicodeHelp.h> + +#include <string> +#include <vector> +#if OSL_DEBUG_LEVEL > 0 +#include <sstream> +#endif +#include <stdexcept> + +#if OSL_DEBUG_LEVEL > 0 + static void dumpParameter(); +#endif + +typedef std::vector<MapiRecipDescW> MapiRecipientList_t; +typedef std::vector<MapiFileDescW> MapiAttachmentList_t; + +const int LEN_SMTP_PREFIX = 5; // "SMTP:" + +namespace /* private */ +{ + OUString gLangTag; + OUString gBootstrap; + std::wstring gFrom; + std::wstring gSubject; + std::wstring gBody; + std::vector<std::wstring> gTo; + std::vector<std::wstring> gCc; + std::vector<std::wstring> gBcc; + // Keep temp filepath, displayed name, and "do not delete" flag + std::vector<std::tuple<std::wstring, std::wstring, bool>> gAttachments; + int gMapiFlags = 0; +} + +/** + Add a prefix to an email address. MAPI requires that + email addresses have an 'SMTP:' prefix. + + @param aEmailAddress + [in] the email address. + + @param aPrefix + [in] the prefix to be added to the email address. + + @returns + the email address prefixed with the specified prefix. +*/ +static std::wstring prefixEmailAddress( + const std::wstring& aEmailAddress, + const std::wstring& aPrefix = L"SMTP:") +{ + return (aPrefix + aEmailAddress); +} + +/** @internal */ +static void addRecipient( + ULONG recipClass, + const std::wstring& recipAddress, + MapiRecipientList_t* pMapiRecipientList) +{ + MapiRecipDescW mrd; + ZeroMemory(&mrd, sizeof(mrd)); + + mrd.ulRecipClass = recipClass; + mrd.lpszName = const_cast<wchar_t*>(recipAddress.c_str()) + LEN_SMTP_PREFIX; + mrd.lpszAddress = const_cast<wchar_t*>(recipAddress.c_str()); + pMapiRecipientList->push_back(mrd); +} + +/** @internal */ +static void initRecipientList(MapiRecipientList_t* pMapiRecipientList) +{ + OSL_ASSERT(pMapiRecipientList->empty()); + + // add to recipients + for (const auto& address : gTo) + addRecipient(MAPI_TO, address, pMapiRecipientList); + + // add cc recipients + for (const auto& address : gCc) + addRecipient(MAPI_CC, address, pMapiRecipientList); + + // add bcc recipients + for (const auto& address : gBcc) + addRecipient(MAPI_BCC, address, pMapiRecipientList); +} + +/** @internal */ +static void initAttachmentList(MapiAttachmentList_t* pMapiAttachmentList) +{ + OSL_ASSERT(pMapiAttachmentList->empty()); + + for (const auto& [filepath, attachname, nodelete] : gAttachments) + { + (void)nodelete; + MapiFileDescW mfd; + ZeroMemory(&mfd, sizeof(mfd)); + mfd.lpszPathName = const_cast<wchar_t*>(filepath.c_str()); + // MapiFileDesc documentation (https://msdn.microsoft.com/en-us/library/hh707272) + // allows using here either nullptr, or a pointer to empty string. However, + // for Outlook 2013, we cannot use nullptr here, and must point to a (possibly + // empty) string: otherwise using MAPI_DIALOG_MODELESS results in MAPI_E_FAILURE. + // See http://peach.ease.lsoft.com/scripts/wa-PEACH.exe?A2=MAPI-L;d2bf3060.1604 + // Since C++11, c_str() must return a pointer to single null character when the + // string is empty, so we are OK here in case when there's no explicit file name + // passed + mfd.lpszFileName = const_cast<wchar_t*>(attachname.c_str()); + mfd.nPosition = sal::static_int_cast<ULONG>(-1); + pMapiAttachmentList->push_back(mfd); + } +} + +/** @internal */ +static void initMapiOriginator(MapiRecipDescW* pMapiOriginator) +{ + ZeroMemory(pMapiOriginator, sizeof(*pMapiOriginator)); + + pMapiOriginator->ulRecipClass = MAPI_ORIG; + pMapiOriginator->lpszName = const_cast<wchar_t*>(L""); + pMapiOriginator->lpszAddress = const_cast<wchar_t*>(gFrom.c_str()); +} + +/** @internal */ +static void initMapiMessage( + MapiRecipDescW* aMapiOriginator, + MapiRecipientList_t& aMapiRecipientList, + MapiAttachmentList_t& aMapiAttachmentList, + MapiMessageW* pMapiMessage) +{ + ZeroMemory(pMapiMessage, sizeof(*pMapiMessage)); + + pMapiMessage->lpszSubject = const_cast<wchar_t*>(gSubject.c_str()); + pMapiMessage->lpszNoteText = (gBody.length() ? const_cast<wchar_t*>(gBody.c_str()) : nullptr); + pMapiMessage->lpOriginator = aMapiOriginator; + pMapiMessage->lpRecips = aMapiRecipientList.size() ? aMapiRecipientList.data() : nullptr; + pMapiMessage->nRecipCount = aMapiRecipientList.size(); + if (!aMapiAttachmentList.empty()) + pMapiMessage->lpFiles = aMapiAttachmentList.data(); + pMapiMessage->nFileCount = aMapiAttachmentList.size(); +} + +const wchar_t* const KnownParameters[] = +{ + L"--to", + L"--cc", + L"--bcc", + L"--from", + L"--subject", + L"--body", + L"--attach", + L"--mapi-dialog", + L"--mapi-logon-ui", + L"--langtag", + L"--bootstrap", +}; + +/** @internal */ +static bool isKnownParameter(const wchar_t* aParameterName) +{ + for (const wchar_t* KnownParameter : KnownParameters) + if (_wcsicmp(aParameterName, KnownParameter) == 0) + return true; + + return false; +} + +/** @internal */ +static void initParameter(int argc, wchar_t* argv[]) +{ + for (int i = 1; i < argc; i++) + { + if (!isKnownParameter(argv[i])) + { + OSL_FAIL("Wrong parameter received"); + continue; + } + + if (_wcsicmp(argv[i], L"--mapi-dialog") == 0) + { + // MAPI_DIALOG_MODELESS has many problems and crashes Outlook 2016. + // see the commit message for a lengthy description. + gMapiFlags |= MAPI_DIALOG; + } + else if (_wcsicmp(argv[i], L"--mapi-logon-ui") == 0) + { + gMapiFlags |= MAPI_LOGON_UI; + } + else if ((i+1) < argc) // is the value of a parameter available too? + { + if (_wcsicmp(argv[i], L"--to") == 0) + gTo.push_back(prefixEmailAddress(argv[i+1])); + else if (_wcsicmp(argv[i], L"--cc") == 0) + gCc.push_back(prefixEmailAddress(argv[i+1])); + else if (_wcsicmp(argv[i], L"--bcc") == 0) + gBcc.push_back(prefixEmailAddress(argv[i+1])); + else if (_wcsicmp(argv[i], L"--from") == 0) + gFrom = prefixEmailAddress(argv[i+1]); + else if (_wcsicmp(argv[i], L"--subject") == 0) + gSubject = argv[i+1]; + else if (_wcsicmp(argv[i], L"--body") == 0) + gBody = argv[i+1]; + else if (_wcsicmp(argv[i], L"--attach") == 0) + { + std::wstring sPath(argv[i + 1]); + // An attachment may optionally be immediately followed by --attach-name and user-visible name + std::wstring sName; + if ((i + 3) < argc && _wcsicmp(argv[i+2], L"--attach-name") == 0) + { + sName = argv[i+3]; + i += 2; + } + // Also there may be --nodelete to keep the attachment on exit + bool nodelete = false; + if ((i + 2) < argc && _wcsicmp(argv[i+2], L"--nodelete") == 0) + { + nodelete = true; + ++i; + } + gAttachments.emplace_back(sPath, sName, nodelete); + } + else if (_wcsicmp(argv[i], L"--langtag") == 0) + gLangTag = o3tl::toU(argv[i+1]); + else if (_wcsicmp(argv[i], L"--bootstrap") == 0) + gBootstrap = o3tl::toU(argv[i+1]); + + i++; + } + } +} + +static void ShowError(ULONG nMAPIResult) +{ + if (!gBootstrap.isEmpty()) + rtl::Bootstrap::setIniFilename(gBootstrap); + LanguageTag aLangTag(gLangTag); + std::locale aLocale = Translate::Create("sfx", aLangTag); + OUString sMessage = Translate::get(STR_ERROR_SEND_MAIL_CODE, aLocale); + OUString sErrorId; + switch (nMAPIResult) + { + case MAPI_E_FAILURE: + sErrorId = "MAPI_E_FAILURE"; + break; + case MAPI_E_LOGON_FAILURE: + sErrorId = "MAPI_E_LOGON_FAILURE"; + break; + case MAPI_E_DISK_FULL: + sErrorId = "MAPI_E_DISK_FULL"; + break; + case MAPI_E_INSUFFICIENT_MEMORY: + sErrorId = "MAPI_E_INSUFFICIENT_MEMORY"; + break; + case MAPI_E_ACCESS_DENIED: + sErrorId = "MAPI_E_ACCESS_DENIED"; + break; + case MAPI_E_TOO_MANY_SESSIONS: + sErrorId = "MAPI_E_ACCESS_DENIED"; + break; + case MAPI_E_TOO_MANY_FILES: + sErrorId = "MAPI_E_TOO_MANY_FILES"; + break; + case MAPI_E_TOO_MANY_RECIPIENTS: + sErrorId = "MAPI_E_TOO_MANY_RECIPIENTS"; + break; + case MAPI_E_ATTACHMENT_NOT_FOUND: + sErrorId = "MAPI_E_ATTACHMENT_NOT_FOUND"; + break; + case MAPI_E_ATTACHMENT_OPEN_FAILURE: + sErrorId = "MAPI_E_ATTACHMENT_OPEN_FAILURE"; + break; + case MAPI_E_ATTACHMENT_WRITE_FAILURE: + sErrorId = "MAPI_E_ATTACHMENT_WRITE_FAILURE"; + break; + case MAPI_E_UNKNOWN_RECIPIENT: + sErrorId = "MAPI_E_UNKNOWN_RECIPIENT"; + break; + case MAPI_E_BAD_RECIPTYPE: + sErrorId = "MAPI_E_BAD_RECIPTYPE"; + break; + case MAPI_E_NO_MESSAGES: + sErrorId = "MAPI_E_NO_MESSAGES"; + break; + case MAPI_E_INVALID_MESSAGE: + sErrorId = "MAPI_E_INVALID_MESSAGE"; + break; + case MAPI_E_TEXT_TOO_LARGE: + sErrorId = "MAPI_E_TEXT_TOO_LARGE"; + break; + case MAPI_E_INVALID_SESSION: + sErrorId = "MAPI_E_INVALID_SESSION"; + break; + case MAPI_E_TYPE_NOT_SUPPORTED: + sErrorId = "MAPI_E_TYPE_NOT_SUPPORTED"; + break; + case MAPI_E_AMBIGUOUS_RECIPIENT: + sErrorId = "MAPI_E_AMBIGUOUS_RECIPIENT"; + break; + case MAPI_E_MESSAGE_IN_USE: + sErrorId = "MAPI_E_MESSAGE_IN_USE"; + break; + case MAPI_E_NETWORK_FAILURE: + sErrorId = "MAPI_E_NETWORK_FAILURE"; + break; + case MAPI_E_INVALID_EDITFIELDS: + sErrorId = "MAPI_E_INVALID_EDITFIELDS"; + break; + case MAPI_E_INVALID_RECIPS: + sErrorId = "MAPI_E_INVALID_RECIPS"; + break; + case MAPI_E_NOT_SUPPORTED: + sErrorId = "MAPI_E_NOT_SUPPORTED"; + break; + case MAPI_E_UNICODE_NOT_SUPPORTED: + sErrorId = "MAPI_E_UNICODE_NOT_SUPPORTED"; + break; + default: + sErrorId = OUString::number(nMAPIResult); + } + sMessage = sMessage.replaceAll("$1", sErrorId); + OUString sTitle(Translate::get(STR_ERROR_SEND_MAIL_HEADER, aLocale)); + + MessageBoxW(nullptr, o3tl::toW(sMessage.getStr()), o3tl::toW(sTitle.getStr()), + MB_OK | MB_ICONINFORMATION); +} + +/** + Main. + NOTE: Because this is program only serves implementation + purposes and should not be used by any end user the + parameter checking is very limited. Every unknown parameter + will be ignored. +*/ +int wmain(int argc, wchar_t* argv[]) +{ + + initParameter(argc, argv); + +#if OSL_DEBUG_LEVEL > 0 + dumpParameter(); +#endif + + ULONG ulRet = MAPI_E_FAILURE; + + try + { + LHANDLE const hSession = 0; + + MapiRecipDescW mapiOriginator; + MapiRecipientList_t mapiRecipientList; + MapiAttachmentList_t mapiAttachmentList; + MapiMessageW mapiMsg; + + initMapiOriginator(&mapiOriginator); + initRecipientList(&mapiRecipientList); + initAttachmentList(&mapiAttachmentList); + initMapiMessage((gFrom.length() ? &mapiOriginator : nullptr), mapiRecipientList, mapiAttachmentList, &mapiMsg); + + ulRet = MAPISendMailHelper(hSession, 0, &mapiMsg, gMapiFlags, 0); + + // There is no point in treating an aborted mail sending + // dialog as an error to be returned as our exit + // status. If the user decided to abort sending a document + // as mail, OK, that is not an error. + + // Also, it seems that GroupWise makes MAPISendMail() + // return MAPI_E_USER_ABORT even if the mail sending + // dialog was not aborted by the user, and the mail was + // actually sent just fine. See bnc#660241 (visible to + // Novell people only, sorry). + + if (ulRet == MAPI_E_USER_ABORT) + ulRet = SUCCESS_SUCCESS; + + } + catch (const std::runtime_error& ex) + { + OSL_FAIL(ex.what()); + } + + // Now cleanup the temporary attachment files + for (const auto& [filepath, attachname, nodelete] : gAttachments) + { + (void)attachname; + if (!nodelete) + DeleteFileW(filepath.c_str()); + } + + // Only show the error message if UI was requested + if ((ulRet != SUCCESS_SUCCESS) && (gMapiFlags & (MAPI_DIALOG | MAPI_LOGON_UI))) + ShowError(ulRet); + + return ulRet; +} + +#if OSL_DEBUG_LEVEL > 0 + void dumpParameter() + { + std::wostringstream oss; + + if (gFrom.length() > 0) + oss << "--from " << gFrom << std::endl; + + if (gSubject.length() > 0) + oss << "--subject " << gSubject << std::endl; + + if (gBody.length() > 0) + oss << "--body " << gBody << std::endl; + + for (const auto& address : gTo) + oss << "--to " << address << std::endl; + + for (const auto& address : gCc) + oss << "--cc " << address << std::endl; + + for (const auto& address : gBcc) + oss << "--bcc " << address << std::endl; + + for (const auto& [filepath, attachname, nodelete] : gAttachments) + { + oss << "--attach " << filepath << std::endl; + if (!attachname.empty()) + oss << "--attach-name " << attachname << std::endl; + if (nodelete) + oss << "--nodelete" << std::endl; + } + + if (gMapiFlags & MAPI_DIALOG) + oss << "--mapi-dialog" << std::endl; + + if (gMapiFlags & MAPI_LOGON_UI) + oss << "--mapi-logon-ui" << std::endl; + + if (!gLangTag.isEmpty()) + oss << "--langtag " << gLangTag << std::endl; + + if (!gBootstrap.isEmpty()) + oss << "--bootstrap " << gBootstrap << std::endl; + + MessageBoxW(nullptr, oss.str().c_str(), L"Arguments", MB_OK | MB_ICONINFORMATION); + } +#endif + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/shell/source/win32/simplemail/smplmail.component b/shell/source/win32/simplemail/smplmail.component new file mode 100644 index 0000000000..32446884dd --- /dev/null +++ b/shell/source/win32/simplemail/smplmail.component @@ -0,0 +1,26 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + * 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 . + --> + +<component loader="com.sun.star.loader.SharedLibrary" environment="@CPPU_ENV@" + xmlns="http://openoffice.org/2010/uno-components"> + <implementation name="com.sun.star.sys.shell.SimpleSystemMail" + constructor="shell_CSmplMailSuppl_get_implementation" single-instance="true"> + <service name="com.sun.star.system.SimpleSystemMail"/> + </implementation> +</component> diff --git a/shell/source/win32/simplemail/smplmailclient.cxx b/shell/source/win32/simplemail/smplmailclient.cxx new file mode 100644 index 0000000000..d40577892c --- /dev/null +++ b/shell/source/win32/simplemail/smplmailclient.cxx @@ -0,0 +1,388 @@ +/* -*- 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::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; + +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(OUString(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, + bool& nodelete) +{ + // 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::TempFileNamed>(&GetBaseTempDirURL())); + maAttachmentFiles.back()->EnableKillingFile(); + INetURLObject aFilePathObj(maAttachmentFiles.back()->GetURL()); + OUString sNewAttachmentURL = aFilePathObj.GetMainURL(INetURLObject::DecodeMechanism::NONE); + OUString sCorrectedOrigAttachURL(sOrigAttachURL); + // Make sure to convert to URL, if a system path was passed to XSimpleMailMessage + // Ignore conversion error, in which case sCorrectedOrigAttachURL is unchanged + osl::FileBase::getFileURLFromSystemPath(sCorrectedOrigAttachURL, sCorrectedOrigAttachURL); + if (osl::File::copy(sCorrectedOrigAttachURL, sNewAttachmentURL) == osl::FileBase::RC::E_None) + { + INetURLObject url(sCorrectedOrigAttachURL, INetURLObject::EncodeMechanism::WasEncoded); + sUserVisibleName = url.getName(INetURLObject::LAST_SEGMENT, true, + INetURLObject::DecodeMechanism::WithCharset); + nodelete = false; + } + 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(); + nodelete = true; // Do not delete a non-temporary in senddoc + } + 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); + } + + const Sequence<OUString> ccRecipients = xSimpleMailMessage->getCcRecipient(); + for (OUString const & s : ccRecipients) + { + rCommandArgs.push_back("--cc"); + rCommandArgs.push_back(s); + } + + const Sequence<OUString> bccRecipients = xSimpleMailMessage->getBccRecipient(); + for (OUString const & s : bccRecipients) + { + rCommandArgs.push_back("--bcc"); + rCommandArgs.push_back(s); + } + + 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; + bool nodelete = false; + OUString sTempFileURL(CopyAttachment(attachment, sDisplayName, nodelete)); + 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 (nodelete) + rCommandArgs.push_back("--nodelete"); + } + + if (!(aFlag & NO_USER_INTERFACE)) + rCommandArgs.push_back("--mapi-dialog"); + + if (!(aFlag & NO_LOGON_DIALOG)) + rCommandArgs.push_back("--mapi-logon-ui"); + + rCommandArgs.push_back("--langtag"); + rCommandArgs.push_back(SvtSysLocale().GetUILanguageTag().getBcp47()); + + rtl::Bootstrap aBootstrap; + OUString sBootstrapPath; + aBootstrap.getIniName(sBootstrapPath); + if (!sBootstrapPath.isEmpty()) + { + rCommandArgs.push_back("--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: */ diff --git a/shell/source/win32/simplemail/smplmailclient.hxx b/shell/source/win32/simplemail/smplmailclient.hxx new file mode 100644 index 0000000000..d2c4bf77a2 --- /dev/null +++ b/shell/source/win32/simplemail/smplmailclient.hxx @@ -0,0 +1,50 @@ +/* -*- 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 . + */ + +#ifndef INCLUDED_SHELL_SOURCE_WIN32_SIMPLEMAIL_SMPLMAILCLIENT_HXX +#define INCLUDED_SHELL_SOURCE_WIN32_SIMPLEMAIL_SMPLMAILCLIENT_HXX + +#include <cppuhelper/implbase.hxx> +#include <com/sun/star/lang/XServiceInfo.hpp> + +#include <com/sun/star/system/XSimpleMailClient.hpp> +#include <unotools/tempfile.hxx> +#include <vector> +#include <memory> + +class CSmplMailClient : public cppu::WeakImplHelper<css::system::XSimpleMailClient> +{ +public: + virtual css::uno::Reference<css::system::XSimpleMailMessage> SAL_CALL createSimpleMailMessage() override; + + virtual void SAL_CALL sendSimpleMailMessage(const css::uno::Reference<css::system::XSimpleMailMessage>& xSimpleMailMessage, sal_Int32 aFlag) override; + +private: + void validateParameter(const css::uno::Reference<css::system::XSimpleMailMessage>& xSimpleMailMessage, sal_Int32 aFlag); + void assembleCommandLine(const css::uno::Reference<css::system::XSimpleMailMessage>& xSimpleMailMessage, sal_Int32 aFlag, std::vector<OUString>& rCommandArgs); + OUString CopyAttachment(const OUString& sOrigAttachURL, OUString& sUserVisibleName, bool& nodelete); + // Don't try to delete the copied attachment files; let the spawned process cleanup them + void ReleaseAttachments(); + + std::vector< std::unique_ptr<utl::TempFileNamed> > maAttachmentFiles; +}; + +#endif + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/shell/source/win32/simplemail/smplmailmsg.cxx b/shell/source/win32/simplemail/smplmailmsg.cxx new file mode 100644 index 0000000000..8cc7c3a46a --- /dev/null +++ b/shell/source/win32/simplemail/smplmailmsg.cxx @@ -0,0 +1,102 @@ +/* -*- 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 <osl/file.h> +#include "smplmailmsg.hxx" + +using com::sun::star::uno::Sequence; + +using namespace cppu; + +CSmplMailMsg::CSmplMailMsg( ) +{ +} + +void SAL_CALL CSmplMailMsg::setBody( const OUString& aBody ) +{ + m_aBody = aBody; +} + +OUString SAL_CALL CSmplMailMsg::getBody( ) +{ + return m_aBody; +} + +void SAL_CALL CSmplMailMsg::setRecipient( const OUString& aRecipient ) +{ + m_aRecipient = aRecipient; +} + +OUString SAL_CALL CSmplMailMsg::getRecipient( ) +{ + return m_aRecipient; +} + +void SAL_CALL CSmplMailMsg::setCcRecipient( const Sequence< OUString >& aCcRecipient ) +{ + m_CcRecipients = aCcRecipient; +} + +Sequence< OUString > SAL_CALL CSmplMailMsg::getCcRecipient( ) +{ + return m_CcRecipients; +} + +void SAL_CALL CSmplMailMsg::setBccRecipient( const Sequence< OUString >& aBccRecipient ) +{ + m_BccRecipients = aBccRecipient; +} + +Sequence< OUString > SAL_CALL CSmplMailMsg::getBccRecipient( ) +{ + return m_BccRecipients; +} + +void SAL_CALL CSmplMailMsg::setOriginator( const OUString& aOriginator ) +{ + m_aOriginator = aOriginator; +} + +OUString SAL_CALL CSmplMailMsg::getOriginator( ) +{ + return m_aOriginator; +} + +void SAL_CALL CSmplMailMsg::setSubject( const OUString& aSubject ) +{ + m_aSubject = aSubject; +} + +OUString SAL_CALL CSmplMailMsg::getSubject( ) +{ + return m_aSubject; +} + +void SAL_CALL CSmplMailMsg::setAttachement( const Sequence< OUString >& aAttachement ) +{ + m_Attachements = aAttachement; +} + +Sequence< OUString > SAL_CALL CSmplMailMsg::getAttachement( ) +{ + return m_Attachements; +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/shell/source/win32/simplemail/smplmailmsg.hxx b/shell/source/win32/simplemail/smplmailmsg.hxx new file mode 100644 index 0000000000..87269910bb --- /dev/null +++ b/shell/source/win32/simplemail/smplmailmsg.hxx @@ -0,0 +1,82 @@ +/* -*- 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 . + */ + +#ifndef INCLUDED_SHELL_SOURCE_WIN32_SIMPLEMAIL_SMPLMAILMSG_HXX +#define INCLUDED_SHELL_SOURCE_WIN32_SIMPLEMAIL_SMPLMAILMSG_HXX + +#include <cppuhelper/implbase.hxx> +#include <com/sun/star/lang/XServiceInfo.hpp> +#include <com/sun/star/lang/IllegalArgumentException.hpp> +#include <com/sun/star/system/XSimpleMailMessage2.hpp> + + + + +class CSmplMailMsg : public cppu::WeakImplHelper< css::system::XSimpleMailMessage2 > +{ +public: + CSmplMailMsg( ); + + virtual void SAL_CALL setBody( const OUString& aBody ) override; + + virtual OUString SAL_CALL getBody( ) override; + + + virtual void SAL_CALL setRecipient( const OUString& aRecipient ) override; + + virtual OUString SAL_CALL getRecipient( ) override; + + + virtual void SAL_CALL setCcRecipient( const css::uno::Sequence< OUString >& aCcRecipient ) override; + + virtual css::uno::Sequence< OUString > SAL_CALL getCcRecipient( ) override; + + + virtual void SAL_CALL setBccRecipient( const css::uno::Sequence< OUString >& aBccRecipient ) override; + + virtual css::uno::Sequence< OUString > SAL_CALL getBccRecipient( ) override; + + + virtual void SAL_CALL setOriginator( const OUString& aOriginator ) override; + + virtual OUString SAL_CALL getOriginator( ) override; + + + virtual void SAL_CALL setSubject( const OUString& aSubject ) override; + + virtual OUString SAL_CALL getSubject( ) override; + + + virtual void SAL_CALL setAttachement( const css::uno::Sequence< OUString >& aAttachement ) override; + + virtual css::uno::Sequence< OUString > SAL_CALL getAttachement( ) override; + +private: + OUString m_aBody; + OUString m_aRecipient; + OUString m_aOriginator; + OUString m_aSubject; + css::uno::Sequence< OUString > m_CcRecipients; + css::uno::Sequence< OUString > m_BccRecipients; + css::uno::Sequence< OUString > m_Attachements; +}; + +#endif + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/shell/source/win32/simplemail/smplmailsuppl.cxx b/shell/source/win32/simplemail/smplmailsuppl.cxx new file mode 100644 index 0000000000..6f4eb0ea09 --- /dev/null +++ b/shell/source/win32/simplemail/smplmailsuppl.cxx @@ -0,0 +1,84 @@ +/* -*- 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 <cppuhelper/supportsservice.hxx> +#include <cppuhelper/weak.hxx> +#include <com/sun/star/uno/XComponentContext.hpp> +#include "smplmailsuppl.hxx" +#include "smplmailclient.hxx" + +#define WIN32_LEAN_AND_MEAN +#include <windows.h> + +using com::sun::star::uno::Reference; +using com::sun::star::uno::Sequence; +using com::sun::star::lang::XServiceInfo; +using com::sun::star::system::XSimpleMailClientSupplier; +using com::sun::star::system::XSimpleMailClient; + +using namespace cppu; + +CSmplMailSuppl::CSmplMailSuppl() : + WeakComponentImplHelper<XSimpleMailClientSupplier, XServiceInfo>(m_aMutex) +{ +} + +CSmplMailSuppl::~CSmplMailSuppl() +{ +} + +Reference<XSimpleMailClient> SAL_CALL CSmplMailSuppl::querySimpleMailClient() +{ + /* We just try to load the MAPI dll as a test + if a mail client is available */ + Reference<XSimpleMailClient> xSmplMailClient; + HMODULE handle = LoadLibraryW(L"mapi32.dll"); + if ((handle != INVALID_HANDLE_VALUE) && (handle != nullptr)) + { + FreeLibrary(handle); + xSmplMailClient.set(new CSmplMailClient); + } + return xSmplMailClient; +} + +// XServiceInfo + +OUString SAL_CALL CSmplMailSuppl::getImplementationName() +{ + return "com.sun.star.sys.shell.SimpleSystemMail"; +} + +sal_Bool SAL_CALL CSmplMailSuppl::supportsService(const OUString& ServiceName) +{ + return cppu::supportsService(this, ServiceName); +} + +Sequence<OUString> SAL_CALL CSmplMailSuppl::getSupportedServiceNames() +{ + return { "com.sun.star.system.SimpleSystemMail" }; +} + +extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface* +shell_CSmplMailSuppl_get_implementation( + css::uno::XComponentContext* , css::uno::Sequence<css::uno::Any> const&) +{ + return cppu::acquire(new CSmplMailSuppl()); +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/shell/source/win32/simplemail/smplmailsuppl.hxx b/shell/source/win32/simplemail/smplmailsuppl.hxx new file mode 100644 index 0000000000..3c7fbe0f20 --- /dev/null +++ b/shell/source/win32/simplemail/smplmailsuppl.hxx @@ -0,0 +1,58 @@ +/* -*- 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 . + */ + +#ifndef INCLUDED_SHELL_SOURCE_WIN32_SIMPLEMAIL_SMPLMAILSUPPL_HXX +#define INCLUDED_SHELL_SOURCE_WIN32_SIMPLEMAIL_SMPLMAILSUPPL_HXX + +#include <cppuhelper/compbase.hxx> +#include <osl/mutex.hxx> +#include <com/sun/star/lang/XServiceInfo.hpp> +#include <com/sun/star/system/XSimpleMailClientSupplier.hpp> + + +class CSmplMailSupplBase +{ +protected: + osl::Mutex m_aMutex; +}; + +class CSmplMailSuppl : + public CSmplMailSupplBase, + public cppu::WeakComponentImplHelper< + css::system::XSimpleMailClientSupplier, + css::lang::XServiceInfo > +{ +public: + CSmplMailSuppl( ); + ~CSmplMailSuppl( ) override; + + // XSimpleMailClientSupplier + virtual css::uno::Reference< css::system::XSimpleMailClient > SAL_CALL querySimpleMailClient( ) override; + + // XServiceInfo + virtual OUString SAL_CALL getImplementationName( ) override; + + virtual sal_Bool SAL_CALL supportsService( const OUString& ServiceName ) override; + + virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames( ) override; +}; + +#endif + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |