shell32: PIDLs should be checked recursively in SHChangeNotify.
[wine] / dlls / mapi32 / sendmail.c
1 /*
2  * MAPISendMail implementation
3  *
4  * Copyright 2005 Hans Leidekker
5  * Copyright 2009 Owen Rudge for CodeWeavers
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  */
21
22 #include "config.h"
23 #include "wine/port.h"
24
25 #include <stdio.h>
26 #include <stdarg.h>
27
28 #define COBJMACROS
29
30 #include "windef.h"
31 #include "winbase.h"
32 #include "winerror.h"
33 #include "winuser.h"
34 #include "objbase.h"
35 #include "objidl.h"
36 #include "mapi.h"
37 #include "mapix.h"
38 #include "mapiutil.h"
39 #include "mapidefs.h"
40 #include "winreg.h"
41 #include "shellapi.h"
42 #include "shlwapi.h"
43 #include "wine/debug.h"
44 #include "util.h"
45 #include "res.h"
46
47 WINE_DEFAULT_DEBUG_CHANNEL(mapi);
48
49 #define READ_BUF_SIZE    4096
50
51 /*
52    Internal function to send a message via Extended MAPI. Wrapper around the Simple
53    MAPI function MAPISendMail.
54 */
55 static ULONG sendmail_extended_mapi(LHANDLE mapi_session, ULONG_PTR uiparam, lpMapiMessage message,
56     FLAGS flags, ULONG reserved)
57 {
58     ULONG tags[] = {1, PR_IPM_DRAFTS_ENTRYID};
59     ULONG retval = MAPI_E_FAILURE;
60     IMAPISession *session = NULL;
61     IMAPITable* msg_table;
62     LPSRowSet rows = NULL;
63     IMsgStore* msg_store;
64     IMAPIFolder* folder = NULL, *draft_folder = NULL;
65     LPENTRYID entry_id;
66     LPSPropValue props;
67     ULONG entry_len;
68     DWORD obj_type;
69     IMessage* msg;
70     ULONG values;
71     HRESULT ret;
72
73     TRACE("Using Extended MAPI wrapper for MAPISendMail\n");
74
75     /* Attempt to log on via Extended MAPI */
76
77     ret = MAPILogonEx(0, NULL, NULL, MAPI_EXTENDED | MAPI_USE_DEFAULT | MAPI_NEW_SESSION, &session);
78     TRACE("MAPILogonEx: %x\n", ret);
79
80     if (ret != S_OK)
81     {
82         retval = MAPI_E_LOGIN_FAILURE;
83         goto cleanup;
84     }
85
86     /* Open the default message store */
87
88     if (IMAPISession_GetMsgStoresTable(session, 0, &msg_table) == S_OK)
89     {
90         /* We want the default store */
91         SizedSPropTagArray(2, columns) = {2, {PR_ENTRYID, PR_DEFAULT_STORE}};
92
93         /* Set the columns we want */
94         if (IMAPITable_SetColumns(msg_table, (LPSPropTagArray) &columns, 0) == S_OK)
95         {
96             while (1)
97             {
98                 if (IMAPITable_QueryRows(msg_table, 1, 0, &rows) != S_OK)
99                 {
100                     MAPIFreeBuffer(rows);
101                     rows = NULL;
102                 }
103                 else if (rows->cRows != 1)
104                 {
105                     FreeProws(rows);
106                     rows = NULL;
107                 }
108                 else
109                 {
110                     /* If it's not the default store, try the next row */
111                     if (!rows->aRow[0].lpProps[1].Value.b)
112                     {
113                         FreeProws(rows);
114                         continue;
115                     }
116                 }
117
118                 break;
119             }
120         }
121
122         IMAPITable_Release(msg_table);
123     }
124
125     /* Did we manage to get the right store? */
126     if (!rows)
127         goto logoff;
128
129     /* Open the message store */
130     IMAPISession_OpenMsgStore(session, 0, rows->aRow[0].lpProps[0].Value.bin.cb,
131                               (ENTRYID *) rows->aRow[0].lpProps[0].Value.bin.lpb, NULL,
132                               MDB_NO_DIALOG | MAPI_BEST_ACCESS, &msg_store);
133
134     /* We don't need this any more */
135     FreeProws(rows);
136
137     /* First open the inbox, from which the drafts folder can be opened */
138     if (IMsgStore_GetReceiveFolder(msg_store, NULL, 0, &entry_len, &entry_id, NULL) == S_OK)
139     {
140         IMsgStore_OpenEntry(msg_store, entry_len, entry_id, NULL, 0, &obj_type, (LPUNKNOWN*) &folder);
141         MAPIFreeBuffer(entry_id);
142     }
143
144     /* Open the drafts folder, or failing that, try asking the message store for the outbox */
145     if ((folder == NULL) || ((ret = IMAPIFolder_GetProps(folder, (LPSPropTagArray) tags, 0, &values, &props)) != S_OK))
146     {
147         TRACE("Unable to open Drafts folder; opening Outbox instead\n");
148         tags[1] = PR_IPM_OUTBOX_ENTRYID;
149         ret = IMsgStore_GetProps(msg_store, (LPSPropTagArray) tags, 0, &values, &props);
150     }
151
152     if (ret != S_OK)
153         goto logoff;
154
155     IMsgStore_OpenEntry(msg_store, props[0].Value.bin.cb, (LPENTRYID) props[0].Value.bin.lpb,
156         NULL, MAPI_MODIFY, &obj_type, (LPUNKNOWN *) &draft_folder);
157
158     /* Create a new message */
159     if (IMAPIFolder_CreateMessage(draft_folder, NULL, 0, &msg) == S_OK)
160     {
161         ULONG token;
162         SPropValue p;
163
164         /* Define message properties */
165         p.ulPropTag = PR_MESSAGE_FLAGS;
166         p.Value.l = MSGFLAG_FROMME | MSGFLAG_UNSENT;
167
168         IMessage_SetProps(msg, 1, &p, NULL);
169
170         p.ulPropTag = PR_SENTMAIL_ENTRYID;
171         p.Value.bin.cb = props[0].Value.bin.cb;
172         p.Value.bin.lpb = props[0].Value.bin.lpb;
173         IMessage_SetProps(msg, 1,&p, NULL);
174
175         /* Set message subject */
176         if (message->lpszSubject)
177         {
178             p.ulPropTag = PR_SUBJECT_A;
179             p.Value.lpszA = message->lpszSubject;
180             IMessage_SetProps(msg, 1, &p, NULL);
181         }
182
183         /* Set message body */
184         if (message->lpszNoteText)
185         {
186             LPSTREAM stream = NULL;
187
188             if (IMessage_OpenProperty(msg, PR_BODY_A, &IID_IStream, 0,
189                 MAPI_MODIFY | MAPI_CREATE, (LPUNKNOWN*) &stream) == S_OK)
190             {
191                 IStream_Write(stream, message->lpszNoteText, strlen(message->lpszNoteText)+1, NULL);
192                 IStream_Release(stream);
193             }
194         }
195
196         /* Add message attachments */
197         if (message->nFileCount > 0)
198         {
199             ULONG num_attach = 0;
200             int i, j;
201
202             for (i = 0; i < message->nFileCount; i++)
203             {
204                 IAttach* attachment = NULL;
205                 SPropValue prop[4];
206                 LPCSTR filename;
207                 HANDLE file;
208
209                 if (!message->lpFiles[i].lpszPathName)
210                     continue;
211
212                 /* Open the attachment for reading */
213                 file = CreateFileA(message->lpFiles[i].lpszPathName, GENERIC_READ, FILE_SHARE_READ,
214                     NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
215
216                 if (file == INVALID_HANDLE_VALUE)
217                     continue;
218
219                 /* Check if a display filename has been given; if not, get one ourselves from path name */
220                 filename = message->lpFiles[i].lpszFileName;
221
222                 if (!filename)
223                 {
224                     filename = message->lpFiles[i].lpszPathName;
225
226                     for (j = strlen(message->lpFiles[i].lpszPathName)-1; j >= 0; j--)
227                     {
228                         if (message->lpFiles[i].lpszPathName[i] == '\\' ||
229                             message->lpFiles[i].lpszPathName[i] == '/')
230                         {
231                             filename = &message->lpFiles[i].lpszPathName[i+1];
232                             break;
233                         }
234                     }
235                 }
236
237                 TRACE("Attachment %d path: '%s'; filename: '%s'\n", i, debugstr_a(message->lpFiles[i].lpszPathName),
238                     debugstr_a(filename));
239
240                 /* Create the attachment */
241                 if (IMessage_CreateAttach(msg, NULL, 0, &num_attach, &attachment) != S_OK)
242                 {
243                     TRACE("Unable to create attachment\n");
244                     CloseHandle(file);
245                     continue;
246                 }
247
248                 /* Set the attachment properties */
249                 ZeroMemory(prop, sizeof(prop));
250
251                 prop[0].ulPropTag = PR_ATTACH_METHOD;
252                 prop[0].Value.ul = ATTACH_BY_VALUE;
253                 prop[1].ulPropTag = PR_ATTACH_LONG_FILENAME_A;
254                 prop[1].Value.lpszA = (LPSTR) filename;
255                 prop[2].ulPropTag = PR_ATTACH_FILENAME_A;
256                 prop[2].Value.lpszA = (LPSTR) filename;
257                 prop[3].ulPropTag = PR_RENDERING_POSITION;
258                 prop[3].Value.l = -1;
259
260                 if (IAttach_SetProps(attachment, 4, prop, NULL) == S_OK)
261                 {
262                     LPSTREAM stream = NULL;
263
264                     if (IAttach_OpenProperty(attachment, PR_ATTACH_DATA_BIN, &IID_IStream, 0,
265                         MAPI_MODIFY | MAPI_CREATE, (LPUNKNOWN*) &stream) == S_OK)
266                     {
267                         BYTE data[READ_BUF_SIZE];
268                         DWORD size = 0, read, written;
269
270                         while (ReadFile(file, data, READ_BUF_SIZE, &read, NULL) && (read != 0))
271                         {
272                             IStream_Write(stream, data, read, &written);
273                             size += read;
274                         }
275
276                         TRACE("%d bytes read, %d bytes written of attachment\n", read, written);
277
278                         IStream_Commit(stream, STGC_DEFAULT);
279                         IStream_Release(stream);
280
281                         prop[0].ulPropTag = PR_ATTACH_SIZE;
282                         prop[0].Value.ul = size;
283                         IAttach_SetProps(attachment, 1, prop, NULL);
284
285                         IAttach_SaveChanges(attachment, KEEP_OPEN_READONLY);
286                         num_attach++;
287                     }
288                 }
289
290                 CloseHandle(file);
291                 IAttach_Release(attachment);
292             }
293         }
294
295         IMessage_SaveChanges(msg, KEEP_OPEN_READWRITE);
296
297         /* Prepare the message form */
298
299         if (IMAPISession_PrepareForm(session, NULL, msg, &token) == S_OK)
300         {
301             ULONG access = 0, status = 0, flags = 0, pc = 0;
302             ULONG pT[2] = {1, PR_MSG_STATUS};
303
304             /* Retrieve message status, flags, access rights and class */
305
306             if (IMessage_GetProps(msg, (LPSPropTagArray) pT, 0, &pc, &props) == S_OK)
307             {
308                 status = props->Value.ul;
309                 MAPIFreeBuffer(props);
310             }
311
312             pT[1] = PR_MESSAGE_FLAGS;
313
314             if (IMessage_GetProps(msg, (LPSPropTagArray) pT, 0, &pc, &props) == S_OK)
315             {
316                 flags = props->Value.ul;
317                 MAPIFreeBuffer(props);
318             }
319
320             pT[1] = PR_ACCESS;
321
322             if (IMessage_GetProps(msg, (LPSPropTagArray) pT, 0, &pc, &props) == S_OK)
323             {
324                 access = props->Value.ul;
325                 MAPIFreeBuffer(props);
326             }
327
328             pT[1] = PR_MESSAGE_CLASS_A;
329
330             if (IMessage_GetProps(msg, (LPSPropTagArray) pT, 0, &pc, &props) == S_OK)
331             {
332                 /* Show the message form (edit window) */
333
334                 ret = IMAPISession_ShowForm(session, 0, msg_store, draft_folder, NULL,
335                                             token, NULL, 0, status, flags, access,
336                                             props->Value.lpszA);
337
338                 switch (ret)
339                 {
340                     case S_OK:
341                         retval = SUCCESS_SUCCESS;
342                         break;
343
344                     case MAPI_E_USER_CANCEL:
345                         retval = MAPI_E_USER_ABORT;
346                         break;
347
348                     default:
349                         TRACE("ShowForm failure: %x\n", ret);
350                         break;
351                 }
352             }
353         }
354
355         IMessage_Release(msg);
356     }
357
358     /* Free up the resources we've used */
359     IMAPIFolder_Release(draft_folder);
360     if (folder) IMAPIFolder_Release(folder);
361     IMsgStore_Release(msg_store);
362
363 logoff: ;
364     IMAPISession_Logoff(session, 0, 0, 0);
365     IMAPISession_Release(session);
366
367 cleanup: ;
368     MAPIUninitialize();
369     return retval;
370 }
371
372 /**************************************************************************
373  *  MAPISendMail        (MAPI32.211)
374  *
375  * Send a mail.
376  *
377  * PARAMS
378  *  session  [I] Handle to a MAPI session.
379  *  uiparam  [I] Parent window handle.
380  *  message  [I] Pointer to a MAPIMessage structure.
381  *  flags    [I] Flags.
382  *  reserved [I] Reserved, pass 0.
383  *
384  * RETURNS
385  *  Success: SUCCESS_SUCCESS
386  *  Failure: MAPI_E_FAILURE
387  *
388  */
389 ULONG WINAPI MAPISendMail( LHANDLE session, ULONG_PTR uiparam,
390     lpMapiMessage message, FLAGS flags, ULONG reserved )
391 {
392     WCHAR msg_title[READ_BUF_SIZE], error_msg[READ_BUF_SIZE];
393
394     /* Check to see if we have a Simple MAPI provider loaded */
395     if (mapiFunctions.MAPISendMail)
396         return mapiFunctions.MAPISendMail(session, uiparam, message, flags, reserved);
397
398     /* Check if we have an Extended MAPI provider - if so, use our wrapper */
399     if (MAPIInitialize(NULL) == S_OK)
400         return sendmail_extended_mapi(session, uiparam, message, flags, reserved);
401
402     /* Display an error message since we apparently have no mail clients */
403     LoadStringW(hInstMAPI32, IDS_NO_MAPI_CLIENT, error_msg, sizeof(error_msg) / sizeof(WCHAR));
404     LoadStringW(hInstMAPI32, IDS_SEND_MAIL, msg_title, sizeof(msg_title) / sizeof(WCHAR));
405
406     MessageBoxW((HWND) uiparam, error_msg, msg_title, MB_ICONEXCLAMATION);
407
408     return MAPI_E_NOT_SUPPORTED;
409 }
410
411 ULONG WINAPI MAPISendDocuments(ULONG_PTR uiparam, LPSTR delim, LPSTR paths,
412     LPSTR filenames, ULONG reserved)
413 {
414     if (mapiFunctions.MAPISendDocuments)
415         return mapiFunctions.MAPISendDocuments(uiparam, delim, paths, filenames, reserved);
416
417     return MAPI_E_NOT_SUPPORTED;
418 }