fltlib: Add a stub dll.
[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 #include "windef.h"
29 #include "winbase.h"
30 #include "winerror.h"
31 #include "objbase.h"
32 #include "mapi.h"
33 #include "winreg.h"
34 #include "shellapi.h"
35 #include "shlwapi.h"
36 #include "wine/debug.h"
37 #include "util.h"
38
39 WINE_DEFAULT_DEBUG_CHANNEL(mapi);
40
41 /**************************************************************************
42  *  MAPISendMail        (MAPI32.211)
43  *
44  * Send a mail.
45  *
46  * PARAMS
47  *  session  [I] Handle to a MAPI session.
48  *  uiparam  [I] Parent window handle.
49  *  message  [I] Pointer to a MAPIMessage structure.
50  *  flags    [I] Flags.
51  *  reserved [I] Reserved, pass 0.
52  *
53  * RETURNS
54  *  Success: SUCCESS_SUCCESS
55  *  Failure: MAPI_E_FAILURE
56  *
57  * NOTES
58  *  The fallback procedure is a temporary hack.
59  */
60 ULONG WINAPI MAPISendMail( LHANDLE session, ULONG_PTR uiparam,
61     lpMapiMessage message, FLAGS flags, ULONG reserved )
62 {
63     ULONG ret = MAPI_E_FAILURE;
64     unsigned int i, to_count = 0, cc_count = 0, bcc_count = 0;
65     unsigned int to_size = 0, cc_size = 0, bcc_size = 0, subj_size, body_size;
66
67     char *to = NULL, *cc = NULL, *bcc = NULL;
68     const char *address, *subject, *body;
69     static const char format[] =
70         "mailto:\"%s\"?subject=\"%s\"&cc=\"%s\"&bcc=\"%s\"&body=\"%s\"";
71     char *mailto = NULL, *escape = NULL;
72     char empty_string[] = "";
73     HRESULT res;
74     DWORD size;
75
76     TRACE( "(0x%08x 0x%08lx %p 0x%08x 0x%08x)\n", session, uiparam,
77            message, flags, reserved );
78
79     /* Check to see if we have a Simple MAPI provider loaded */
80     if (mapiFunctions.MAPISendMail)
81         return mapiFunctions.MAPISendMail(session, uiparam, message, flags, reserved);
82
83     /* TODO: Check if we have an Extended MAPI provider, if so, implement
84              wrapper around that. */
85
86     /* Fall back on our own implementation */
87     if (!message) return MAPI_E_FAILURE;
88
89     for (i = 0; i < message->nRecipCount; i++)
90     {
91         if (!message->lpRecips)
92         {
93             WARN("No recipients found\n");
94             return MAPI_E_FAILURE;
95         }
96
97         address = message->lpRecips[i].lpszAddress;
98         if (address)
99         {
100             switch (message->lpRecips[i].ulRecipClass)
101             {
102             case MAPI_ORIG:
103                 TRACE( "From: %s\n", debugstr_a(address) );
104                 break;
105             case MAPI_TO:
106                 TRACE( "To: %s\n", debugstr_a(address) );
107                 to_size += lstrlenA( address ) + 1;
108                 break;
109             case MAPI_CC:
110                 TRACE( "Cc: %s\n", debugstr_a(address) );
111                 cc_size += lstrlenA( address ) + 1;
112                 break;
113             case MAPI_BCC:
114                 TRACE( "Bcc: %s\n", debugstr_a(address) );
115                 bcc_size += lstrlenA( address ) + 1;
116                 break;
117             default:
118                 TRACE( "Unknown recipient class: %d\n",
119                        message->lpRecips[i].ulRecipClass );
120             }
121         }
122         else
123             FIXME("Name resolution and entry identifiers not supported\n");
124     }
125     if (message->nFileCount) FIXME("Ignoring attachments\n");
126
127     subject = message->lpszSubject ? message->lpszSubject : "";
128     body = message->lpszNoteText ? message->lpszNoteText : "";
129
130     TRACE( "Subject: %s\n", debugstr_a(subject) );
131     TRACE( "Body: %s\n", debugstr_a(body) );
132
133     subj_size = lstrlenA( subject );
134     body_size = lstrlenA( body );
135
136     ret = MAPI_E_INSUFFICIENT_MEMORY;
137     if (to_size)
138     {
139         to = HeapAlloc( GetProcessHeap(), 0, to_size );
140         if (!to) goto exit;
141         to[0] = 0;
142     }
143     if (cc_size)
144     {
145         cc = HeapAlloc( GetProcessHeap(), 0, cc_size );
146         if (!cc) goto exit;
147         cc[0] = 0;
148     }
149     if (bcc_size)
150     {
151         bcc = HeapAlloc( GetProcessHeap(), 0, bcc_size );
152         if (!bcc) goto exit;
153         bcc[0] = 0;
154     }
155
156     if (message->lpOriginator)
157         TRACE( "From: %s\n", debugstr_a(message->lpOriginator->lpszAddress) );
158
159     for (i = 0; i < message->nRecipCount; i++)
160     {
161         address = message->lpRecips[i].lpszAddress;
162         if (address)
163         {
164             switch (message->lpRecips[i].ulRecipClass)
165             {
166             case MAPI_TO:
167                 if (to_count) lstrcatA( to, "," );
168                 lstrcatA( to, address );
169                 to_count++;
170                 break;
171             case MAPI_CC:
172                 if (cc_count) lstrcatA( cc, "," );
173                 lstrcatA( cc, address );
174                 cc_count++;
175                 break;
176             case MAPI_BCC:
177                 if (bcc_count) lstrcatA( bcc, "," );
178                 lstrcatA( bcc, address );
179                 bcc_count++;
180                 break;
181             }
182         }
183     }
184     ret = MAPI_E_FAILURE;
185     size = sizeof(format) + to_size + cc_size + bcc_size + subj_size + body_size;
186     
187     mailto = HeapAlloc( GetProcessHeap(), 0, size );
188     if (!mailto) goto exit;
189
190     sprintf( mailto, format, to ? to : "", subject, cc ? cc : "", bcc ? bcc : "", body );
191
192     size = 1;
193     res = UrlEscapeA( mailto, empty_string, &size, URL_ESCAPE_SPACES_ONLY );
194     if (res != E_POINTER) goto exit;
195
196     escape = HeapAlloc( GetProcessHeap(), 0, size );
197     if (!escape) goto exit;
198
199     res = UrlEscapeA( mailto, escape, &size, URL_ESCAPE_SPACES_ONLY );
200     if (res != S_OK) goto exit;
201
202     if ((UINT_PTR)ShellExecuteA( NULL, "open", escape, NULL, NULL, 0 ) > 32)
203         ret = SUCCESS_SUCCESS;
204
205 exit:
206     HeapFree( GetProcessHeap(), 0, to );
207     HeapFree( GetProcessHeap(), 0, cc );
208     HeapFree( GetProcessHeap(), 0, bcc );
209     HeapFree( GetProcessHeap(), 0, mailto );
210     HeapFree( GetProcessHeap(), 0, escape );
211
212     return ret;
213 }
214
215 ULONG WINAPI MAPISendDocuments(ULONG_PTR uiparam, LPSTR delim, LPSTR paths,
216     LPSTR filenames, ULONG reserved)
217 {
218     if (mapiFunctions.MAPISendDocuments)
219         return mapiFunctions.MAPISendDocuments(uiparam, delim, paths, filenames, reserved);
220
221     return MAPI_E_NOT_SUPPORTED;
222 }