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

Annotation of /trunk/Src/wptMAPI.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 328 - (hide annotations)
Fri Sep 25 16:07:38 2009 UTC (15 years, 5 months ago) by twoaday
File size: 7107 byte(s)


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

Properties

Name Value
svn:eol-style native

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26