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
|
/* -*- 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/thread.h>
#include <rtl/bootstrap.hxx>
#include <osl/file.hxx>
#include <rtl/strbuf.hxx>
#include "cmdmailsuppl.hxx"
#include "cmdmailmsg.hxx"
#include <com/sun/star/system/SimpleMailClientFlags.hpp>
#include <com/sun/star/container/XNameAccess.hpp>
#include <com/sun/star/configuration/theDefaultProvider.hpp>
#include <com/sun/star/beans/PropertyValue.hpp>
#include <com/sun/star/beans/XPropertySet.hpp>
#include <com/sun/star/uno/XComponentContext.hpp>
#include <cppuhelper/supportsservice.hxx>
#include <string.h>
#include <errno.h>
#include <unistd.h>
using com::sun::star::beans::PropertyValue;
using com::sun::star::system::XSimpleMailClientSupplier;
using com::sun::star::system::XSimpleMailClient;
using com::sun::star::system::XSimpleMailMessage;
using com::sun::star::system::XSimpleMailMessage2;
using com::sun::star::container::XNameAccess;
using osl::FileBase;
using namespace cppu;
using namespace com::sun::star::system::SimpleMailClientFlags;
using namespace com::sun::star::uno;
using namespace com::sun::star::lang;
using namespace com::sun::star::configuration;
namespace
{
Sequence< OUString > SAL_CALL Component_getSupportedServiceNames()
{
Sequence< OUString > aRet { "com.sun.star.system.SimpleCommandMail" };
return aRet;
}
}
CmdMailSuppl::CmdMailSuppl( const Reference< XComponentContext >& xContext ) :
WeakImplHelper< XSimpleMailClientSupplier, XSimpleMailClient, XServiceInfo >()
{
m_xConfigurationProvider = theDefaultProvider::get(xContext);
}
// XSimpleMailClientSupplier
Reference< XSimpleMailClient > SAL_CALL CmdMailSuppl::querySimpleMailClient( )
{
return static_cast < XSimpleMailClient * > (this);
}
// XSimpleMailClient
Reference< XSimpleMailMessage > SAL_CALL CmdMailSuppl::createSimpleMailMessage( )
{
return Reference< XSimpleMailMessage >( new CmdMailMsg( ) );
}
namespace {
void appendShellWord(OStringBuffer & buffer, OUString const & word, bool strict)
{
OString sys;
if (!word.convertToString(
&sys, osl_getThreadTextEncoding(),
(strict
? (RTL_UNICODETOTEXT_FLAGS_UNDEFINED_ERROR
| RTL_UNICODETOTEXT_FLAGS_INVALID_ERROR)
: OUSTRING_TO_OSTRING_CVTFLAGS)))
{
throw css::uno::Exception(
("Could not convert \"" + word + "\" to encoding #"
+ OUString::number(osl_getThreadTextEncoding())),
css::uno::Reference<css::uno::XInterface>());
}
buffer.append('\'');
for (sal_Int32 i = 0; i != sys.getLength(); ++i) {
char c = sys[i];
switch (c) {
case 0:
if (strict) {
throw css::uno::Exception(
"Could not convert word containing NUL, \"" + word + "\"",
css::uno::Reference<css::uno::XInterface>());
}
break;
case '\'':
buffer.append("'\\''");
break;
default:
buffer.append(c);
break;
}
}
buffer.append('\'');
}
}
void SAL_CALL CmdMailSuppl::sendSimpleMailMessage( const Reference< XSimpleMailMessage >& xSimpleMailMessage, sal_Int32 /*aFlag*/ )
{
if ( ! xSimpleMailMessage.is() )
{
throw css::lang::IllegalArgumentException( "No message specified" ,
static_cast < XSimpleMailClient * > (this), 1 );
}
if( ! m_xConfigurationProvider.is() )
{
throw css::uno::Exception( "Can not access configuration" ,
static_cast < XSimpleMailClient * > (this) );
}
OUString aProgramURL("$BRAND_BASE_DIR/" LIBO_LIBEXEC_FOLDER "/senddoc");
rtl::Bootstrap::expandMacros(aProgramURL);
OUString aProgram;
if ( FileBase::E_None != FileBase::getSystemPathFromFileURL(aProgramURL, aProgram))
{
throw css::uno::Exception("Cound not convert executable path",
static_cast < XSimpleMailClient * > (this));
}
OStringBuffer aBuffer;
appendShellWord(aBuffer, aProgram, true);
try
{
// Query XNameAccess interface of the org.openoffice.Office.Common/ExternalMailer
// configuration node to retrieve the users preferred email application. This may
// transparently by redirected to e.g. the corresponding GConf setting in GNOME.
OUString aConfigRoot = "org.openoffice.Office.Common/ExternalMailer";
PropertyValue aProperty;
aProperty.Name = "nodepath";
aProperty.Value <<= aConfigRoot;
Sequence< Any > aArgumentList( 1 );
aArgumentList[0] <<= aProperty;
Reference< XNameAccess > xNameAccess =
Reference< XNameAccess > (
m_xConfigurationProvider->createInstanceWithArguments(
"com.sun.star.configuration.ConfigurationAccess",
aArgumentList ),
UNO_QUERY );
if( xNameAccess.is() )
{
OUString aMailer;
// Retrieve the value for "Program" node and append it feed senddoc with it
// using the (undocumented) --mailclient switch
xNameAccess->getByName("Program") >>= aMailer;
if( !aMailer.isEmpty() )
{
// make sure we have a system path
FileBase::getSystemPathFromFileURL( aMailer, aMailer );
aBuffer.append(" --mailclient ");
appendShellWord(aBuffer, aMailer, true);
}
#ifdef MACOSX
else
aBuffer.append(" --mailclient Mail");
#endif
}
}
catch(const RuntimeException &e )
{
m_xConfigurationProvider.clear();
SAL_WARN("shell", "RuntimeException caught accessing configuration provider. " << e.Message );
throw;
}
Reference< XSimpleMailMessage2 > xMessage( xSimpleMailMessage, UNO_QUERY );
if ( xMessage.is() )
{
rtl::OUString sBody = xMessage->getBody();
if ( sBody.getLength() > 0 )
{
aBuffer.append(" --body ");
appendShellWord(aBuffer, sBody, false);
}
}
// Convert from, to, etc. in a best-effort rather than a strict way to the
// system encoding, based on the assumption that the relevant address parts
// of those strings are ASCII anyway and any problematic characters are only
// in the human-readable, informational-only parts:
// Append originator if set in the message
if ( !xSimpleMailMessage->getOriginator().isEmpty() )
{
aBuffer.append(" --from ");
appendShellWord(aBuffer, xSimpleMailMessage->getOriginator(), false);
}
// Append receipient if set in the message
if ( !xSimpleMailMessage->getRecipient().isEmpty() )
{
aBuffer.append(" --to ");
appendShellWord(aBuffer, xSimpleMailMessage->getRecipient(), false);
}
// Append carbon copy receipients set in the message
Sequence< OUString > aStringList = xSimpleMailMessage->getCcRecipient();
sal_Int32 n, nmax = aStringList.getLength();
for ( n = 0; n < nmax; n++ )
{
aBuffer.append(" --cc ");
appendShellWord(aBuffer, aStringList[n], false);
}
// Append blind carbon copy receipients set in the message
aStringList = xSimpleMailMessage->getBccRecipient();
nmax = aStringList.getLength();
for ( n = 0; n < nmax; n++ )
{
aBuffer.append(" --bcc ");
appendShellWord(aBuffer, aStringList[n], false);
}
// Append subject if set in the message
if ( !xSimpleMailMessage->getSubject().isEmpty() )
{
aBuffer.append(" --subject ");
appendShellWord(aBuffer, xSimpleMailMessage->getSubject(), false);
}
// Append attachments set in the message
aStringList = xSimpleMailMessage->getAttachement();
nmax = aStringList.getLength();
for ( n = 0; n < nmax; n++ )
{
OUString aSystemPath;
if ( FileBase::E_None == FileBase::getSystemPathFromFileURL(aStringList[n], aSystemPath) )
{
aBuffer.append(" --attach ");
appendShellWord(aBuffer, aSystemPath, true);
}
}
OString cmd = aBuffer.makeStringAndClear();
FILE * f = popen(cmd.getStr(), "w");
if (f == nullptr || pclose(f) != 0)
{
throw css::uno::Exception("No mail client configured",
static_cast < XSimpleMailClient * > (this) );
}
}
// XServiceInfo
OUString SAL_CALL CmdMailSuppl::getImplementationName( )
{
return OUString("com.sun.star.comp.system.SimpleCommandMail");
}
sal_Bool SAL_CALL CmdMailSuppl::supportsService( const OUString& ServiceName )
{
return cppu::supportsService(this, ServiceName);
}
Sequence< OUString > SAL_CALL CmdMailSuppl::getSupportedServiceNames( )
{
return Component_getSupportedServiceNames();
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|