d3d9/tests: Avoid a shift overflow in render_state_test_data_init.
[wine] / dlls / winemapi / 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 "mapi.h"
34 #include "winreg.h"
35 #include "shellapi.h"
36 #include "shlwapi.h"
37 #include "wine/debug.h"
38
39 WINE_DEFAULT_DEBUG_CHANNEL(winemapi);
40
41 /**************************************************************************
42  *  MAPISendMail
43  *
44  * Send a message using a native mail client.
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  */
58 ULONG WINAPI MAPISendMail(LHANDLE session, ULONG_PTR uiparam,
59     lpMapiMessage message, FLAGS flags, ULONG reserved)
60 {
61     ULONG ret = MAPI_E_FAILURE;
62     unsigned int i, to_count = 0, cc_count = 0, bcc_count = 0;
63     unsigned int to_size = 0, cc_size = 0, bcc_size = 0, subj_size, body_size;
64
65     char *to = NULL, *cc = NULL, *bcc = NULL;
66     const char *address, *subject, *body;
67     static const char format[] =
68         "mailto:\"%s\"?subject=\"%s\"&cc=\"%s\"&bcc=\"%s\"&body=\"%s\"";
69     static const char smtp[] = "smtp:";
70     char *mailto = NULL, *escape = NULL;
71     char empty_string[] = "";
72     HRESULT res;
73     DWORD size;
74
75     TRACE("(0x%08lx 0x%08lx %p 0x%08x 0x%08x)\n", session, uiparam,
76            message, flags, reserved);
77
78     if (!message)
79         return MAPI_E_FAILURE;
80
81     for (i = 0; i < message->nRecipCount; i++)
82     {
83         if (!message->lpRecips)
84         {
85             WARN("No recipients found\n");
86             return MAPI_E_FAILURE;
87         }
88
89         address = message->lpRecips[i].lpszAddress;
90         if (!strncasecmp(address, smtp, sizeof(smtp) - 1))
91             address += sizeof(smtp) - 1;
92
93         if (address)
94         {
95             switch (message->lpRecips[i].ulRecipClass)
96             {
97                 case MAPI_ORIG:
98                     TRACE("From: %s\n", debugstr_a(address));
99                     break;
100
101                 case MAPI_TO:
102                     TRACE("To: %s\n", debugstr_a(address));
103                     to_size += lstrlenA(address) + 1;
104                     break;
105
106                 case MAPI_CC:
107                     TRACE("Cc: %s\n", debugstr_a(address));
108                     cc_size += lstrlenA(address) + 1;
109                     break;
110
111                 case MAPI_BCC:
112                     TRACE("Bcc: %s\n", debugstr_a(address));
113                     bcc_size += lstrlenA(address) + 1;
114                     break;
115
116                 default:
117                     TRACE("Unknown recipient class: %d\n",
118                            message->lpRecips[i].ulRecipClass);
119             }
120         }
121         else
122             FIXME("Name resolution and entry identifiers not supported\n");
123     }
124
125     if (message->nFileCount)
126         FIXME("Ignoring attachments\n");
127
128     subject = message->lpszSubject ? message->lpszSubject : "";
129     body = message->lpszNoteText ? message->lpszNoteText : "";
130
131     TRACE("Subject: %s\n", debugstr_a(subject));
132     TRACE("Body: %s\n", debugstr_a(body));
133
134     subj_size = lstrlenA(subject);
135     body_size = lstrlenA(body);
136
137     ret = MAPI_E_INSUFFICIENT_MEMORY;
138
139     if (to_size)
140     {
141         to = HeapAlloc(GetProcessHeap(), 0, to_size);
142
143         if (!to)
144             goto exit;
145
146         to[0] = 0;
147     }
148
149     if (cc_size)
150     {
151         cc = HeapAlloc(GetProcessHeap(), 0, cc_size);
152
153         if (!cc)
154             goto exit;
155
156         cc[0] = 0;
157     }
158
159     if (bcc_size)
160     {
161         bcc = HeapAlloc(GetProcessHeap(), 0, bcc_size);
162
163         if (!bcc)
164             goto exit;
165
166         bcc[0] = 0;
167     }
168
169     if (message->lpOriginator)
170         TRACE("From: %s\n", debugstr_a(message->lpOriginator->lpszAddress));
171
172     for (i = 0; i < message->nRecipCount; i++)
173     {
174         address = message->lpRecips[i].lpszAddress;
175         if (!strncasecmp(address, smtp, sizeof(smtp) - 1))
176             address += sizeof(smtp) - 1;
177
178         if (address)
179         {
180             switch (message->lpRecips[i].ulRecipClass)
181             {
182                 case MAPI_TO:
183                     if (to_count)
184                         lstrcatA(to, ",");
185
186                     lstrcatA(to, address);
187                     to_count++;
188                     break;
189
190                 case MAPI_CC:
191                     if (cc_count)
192                         lstrcatA(cc, ",");
193
194                     lstrcatA(cc, address);
195                     cc_count++;
196                     break;
197
198                 case MAPI_BCC:
199                     if (bcc_count)
200                         lstrcatA(bcc, ",");
201
202                     lstrcatA(bcc, address);
203                     bcc_count++;
204                     break;
205             }
206         }
207     }
208     ret = MAPI_E_FAILURE;
209     size = sizeof(format) + to_size + cc_size + bcc_size + subj_size + body_size;
210
211     mailto = HeapAlloc(GetProcessHeap(), 0, size);
212
213     if (!mailto)
214         goto exit;
215
216     sprintf(mailto, format, to ? to : "", subject, cc ? cc : "", bcc ? bcc : "", body);
217
218     size = 1;
219     res = UrlEscapeA(mailto, empty_string, &size, URL_ESCAPE_SPACES_ONLY);
220
221     if (res != E_POINTER)
222         goto exit;
223
224     escape = HeapAlloc(GetProcessHeap(), 0, size);
225
226     if (!escape)
227         goto exit;
228
229     res = UrlEscapeA(mailto, escape, &size, URL_ESCAPE_SPACES_ONLY);
230
231     if (res != S_OK)
232         goto exit;
233
234     TRACE("Executing winebrowser.exe with parameters '%s'\n", debugstr_a(escape));
235
236     if ((UINT_PTR) ShellExecuteA(NULL, "open", "winebrowser.exe", escape, NULL, 0) > 32)
237         ret = SUCCESS_SUCCESS;
238
239 exit:
240     HeapFree(GetProcessHeap(), 0, to);
241     HeapFree(GetProcessHeap(), 0, cc);
242     HeapFree(GetProcessHeap(), 0, bcc);
243     HeapFree(GetProcessHeap(), 0, mailto);
244     HeapFree(GetProcessHeap(), 0, escape);
245
246     return ret;
247 }
248
249 ULONG WINAPI MAPISendDocuments(ULONG_PTR uiparam, LPSTR delim, LPSTR paths,
250     LPSTR filenames, ULONG reserved)
251 {
252     return MAPI_E_NOT_SUPPORTED;
253 }