/[winpt]/trunk/Src/wptMAPI.cpp
ViewVC logotype

Contents of /trunk/Src/wptMAPI.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 317 - (show annotations)
Fri May 25 14:53:02 2007 UTC (17 years, 9 months ago) by twoaday
File size: 7468 byte(s)
Change homepage URL.


1 /* wptMAPI.cpp - MAPI interface for sending keys.
2 * Copyright (C) 2003-2006 Timo Schulz
3 *
4 * This file is part of WinPT.
5 *
6 * WinPT is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * WinPT is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with WinPT; if not, write to the Free Software Foundation,
18 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
19 */
20 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23
24 #include <windows.h>
25 #include <stdio.h>
26 #include <mapi.h>
27
28 #include "resource.h"
29 #include "wptTypes.h"
30 #include "wptErrors.h"
31 #include "wptW32API.h"
32 #include "wptGPG.h"
33 #include "wptVersion.h"
34 #include "wptCommonCtl.h"
35 #include "wptKeyManager.h"
36
37
38 static LPMAPILOGON mapi_logon = NULL;
39 static LPMAPILOGOFF mapi_logoff = NULL;
40 static LPMAPISENDDOCUMENTS mapi_send_documents = NULL;
41 static LPMAPISENDMAIL mapi_send_mail = NULL;
42 static HINSTANCE hlib = NULL;
43 static int init = 0;
44
45 #define load_one_fnc(cast, hlib, name) (cast)GetProcAddress ((hlib), name)
46
47
48 /* Table with all valid MAPI errors. */
49 struct mapi_err_s {
50 int err_no;
51 const char *msg;
52 } mapi_errors[] = {
53 {MAPI_E_FAILURE, "One or more unspecified errors occurred. "},
54 {MAPI_E_AMBIGUOUS_RECIPIENT, "A recipient matched more than one of the recipient descriptor structures and MAPI_DIALOG was not set. "},
55 {MAPI_E_ATTACHMENT_NOT_FOUND, "The specified attachment was not found. "},
56 {MAPI_E_ATTACHMENT_OPEN_FAILURE, "The specified attachment could not be opened. "},
57 {MAPI_E_BAD_RECIPTYPE, "The type of a recipient was not MAPI_TO, MAPI_CC, or MAPI_BCC. "},
58 {MAPI_E_INSUFFICIENT_MEMORY, "There was insufficient memory to proceed." },
59 {MAPI_E_INVALID_RECIPS, "One or more recipients were invalid or did not resolve to any address."},
60 {MAPI_E_LOGIN_FAILURE, "There was no default logon, and the user failed to log on successfully when the logon dialog box was displayed."},
61 {MAPI_E_TEXT_TOO_LARGE, "The text in the message was too large."},
62 {MAPI_E_TOO_MANY_FILES, "There were too many file attachments."},
63 {MAPI_E_TOO_MANY_RECIPIENTS, "There were too many recipients."},
64 {MAPI_E_UNKNOWN_RECIPIENT, "A recipient did not appear in the address list."},
65 {MAPI_E_USER_ABORT, "The user canceled one of the dialog boxes."},
66 {SUCCESS_SUCCESS, "The call succeeded and the message was sent."},
67 {0, NULL}
68 };
69
70
71 /* Return a human readable MAPI error based on the given error @err. */
72 const char*
73 mapi_strerror (int err)
74 {
75 int i;
76
77 for (i=0; mapi_errors[i].msg; i++) {
78 if (err == mapi_errors[i].err_no)
79 return mapi_errors[i].msg;
80 }
81 return mapi_errors[0].msg;
82 }
83
84
85 /* Load MAPI library and set function pointers.
86 Return value: 0 on success. */
87 int
88 mapi_init (void)
89 {
90 if (init)
91 return 0;
92
93 hlib = LoadLibrary ("MAPI32.DLL");
94 if (!hlib)
95 return -1;
96
97 mapi_logon = load_one_fnc (LPMAPILOGON, hlib, "MAPILogon");
98 mapi_logoff = load_one_fnc (LPMAPILOGOFF, hlib, "MAPILogoff");
99 mapi_send_documents = load_one_fnc (LPMAPISENDDOCUMENTS, hlib, "MAPISendDocuments");
100 mapi_send_mail = load_one_fnc (LPMAPISENDMAIL, hlib, "MAPISendMail");
101 if (!mapi_logon || !mapi_logoff || !mapi_send_documents || !mapi_send_mail)
102 return -1;
103 init = 1;
104
105 return 0;
106 }
107
108
109 /* Free library and cleanup. */
110 void
111 mapi_deinit (void)
112 {
113 if (!hlib)
114 return;
115 FreeLibrary (hlib);
116 hlib = NULL;
117 init = 0;
118 }
119
120
121 /* Send the file given in @ascfile via the MAPI mechanism. */
122 int
123 mapi_send_ascfile (char *ascfile)
124 {
125 LHANDLE hd;
126 int rc;
127
128 if (!init)
129 return 0;
130
131 rc = mapi_logon (0, NULL, NULL, MAPI_LOGON_UI, 0, &hd);
132 if (rc != SUCCESS_SUCCESS) {
133 MessageBox (NULL, mapi_strerror (rc), _("MAPI Login failed"), MB_WARN);
134 goto fail;
135 }
136 rc = mapi_send_documents (0, ";", ascfile, NULL, 0);
137 if (rc == MAPI_E_USER_ABORT)
138 rc = SUCCESS_SUCCESS;
139 if (rc != SUCCESS_SUCCESS)
140 MessageBox (NULL, _("Could not send mail."), "MAPI", MB_ICONERROR|MB_OK);
141
142 fail:
143 mapi_logoff (hd, 0, 0, 0);
144 return rc;
145 }
146
147
148 static void
149 free_mapi_msg (MapiMessage *msg)
150 {
151 if (!msg)
152 return;
153 safe_free (msg->lpszSubject);
154 safe_free (msg->lpszNoteText);
155 safe_free (msg);
156 }
157
158
159 static void
160 free_recip_tab (MapiRecipDesc *recip, DWORD n)
161 {
162 DWORD i;
163
164 if (!recip)
165 return;
166 if (!n)
167 return;
168 for (i=0; i < n; i++)
169 safe_free (recip[i].lpszName);
170 safe_free (recip);
171 }
172
173
174 static void
175 free_files_tab (MapiFileDesc *files, DWORD n)
176 {
177 DWORD i;
178
179 if (!files)
180 return;
181 if (!n)
182 return;
183 for (i=0; i < n; i++) {
184 safe_free (files[i].lpszFileName);
185 safe_free (files[i].lpszPathName);
186 }
187 safe_free (files);
188 }
189
190
191 /* Same as mapi_send_pubkey but there is an additional note. */
192 int
193 mapi_send_pubkey_ext (winpt_key_t key, const char *keyfile, int flags)
194 {
195 LHANDLE hd;
196 MapiMessage *msg;
197 MapiRecipDesc *recip;
198 MapiFileDesc *attch;
199 char *p, *kinf;
200 const char *s;
201 int rc;
202
203 if (!init)
204 return 0;
205
206 rc = mapi_logon (0, NULL, NULL, MAPI_LOGON_UI, 0, &hd);
207 if (rc != SUCCESS_SUCCESS) {
208 MessageBox (NULL, mapi_strerror (rc), _("MAPI Login failed"), MB_WARN);
209 return rc;
210 }
211
212 msg = (MapiMessage *)calloc (1, sizeof *msg);
213 if (!msg)
214 BUG (0);
215 p = msg->lpszSubject = strdup ("OpenPGP Public Key");
216 if (!p)
217 BUG (0);
218
219 s = "Attached is this OpenPGP public key:\n%s\n\n"
220 "Import this key via the clipboard or the Key Manager to\n"
221 "exchange encrypted mails with the key holder and to be able\n"
222 "to verify its signatures.\n"
223 "\n"
224 "If you don't have WinPT, you can download it at http://www.winpt.eu";
225 kinf = km_key_get_info (key, 0);
226 p = (char*)malloc (strlen (s) + strlen (kinf) + 2);
227 sprintf (p, s, kinf);
228 free_if_alloc (kinf);
229
230 p = msg->lpszNoteText = p;
231 if (!p)
232 BUG (0);
233
234 /* If the key was signed, we assume it shall be sent back to the owner. */
235 if (flags) {
236 recip = (MapiRecipDesc *)calloc (1, sizeof *recip);
237 if (!recip)
238 BUG (0);
239 recip[0].ulRecipClass = MAPI_TO;
240 p = recip[0].lpszName = strdup (key->ext->uids->uid);
241 if (!p)
242 BUG (0);
243 msg->lpRecips = recip;
244 msg->nRecipCount = 1;
245 }
246 else {
247 msg->lpRecips = recip = NULL;
248 msg->nRecipCount = 0;
249 }
250
251 msg->nFileCount = 1;
252 attch = (MapiFileDesc *)calloc (1, sizeof *attch);
253 if (!attch)
254 BUG (0);
255 attch[0].lpszFileName = strdup (keyfile);
256 attch[0].lpszPathName = strdup (keyfile);
257 msg->lpFiles = attch;
258
259 rc = mapi_send_mail (hd, 0, msg, MAPI_DIALOG , 0);
260 if (rc == MAPI_E_USER_ABORT)
261 rc = SUCCESS_SUCCESS;
262 if (rc != SUCCESS_SUCCESS)
263 MessageBox (NULL, _("Could not sent mail."), "MAPI", MB_ERR);
264
265 free_recip_tab (recip, 1);
266 free_files_tab (attch, 1);
267 free_mapi_msg (msg);
268 mapi_logoff (hd, 0, 0, 0);
269
270 return 0;
271 }
272
273
274
275 int
276 mapi_send_pubkey (const char *keyid, char *keyfile)
277 {
278 winpt_key_s key;
279
280 memset (&key, 0, sizeof (key));
281 if (winpt_get_pubkey (keyid, &key))
282 return -1;
283 return mapi_send_pubkey_ext (&key, keyfile, 0);
284 }

Properties

Name Value
svn:eol-style native

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26