|
/* wptMAPI.cpp |
|
|
* Copyright (C) 2003, 2004 Timo Schulz |
|
|
* |
|
|
* This file is part of WinPT. |
|
|
* |
|
|
* WinPT is free software; you can redistribute it and/or modify |
|
|
* it under the terms of the GNU General Public License as published by |
|
|
* the Free Software Foundation; either version 2 of the License, or |
|
|
* (at your option) any later version. |
|
|
* |
|
|
* WinPT is distributed in the hope that it will be useful, |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
|
* GNU General Public License for more details. |
|
|
* |
|
|
* You should have received a copy of the GNU General Public License |
|
|
* along with WinPT; if not, write to the Free Software Foundation, |
|
|
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
|
|
*/ |
|
|
#include <windows.h> |
|
|
#include <stdio.h> |
|
|
#include <mapi.h> |
|
|
|
|
|
extern HINSTANCE glob_hinst; |
|
|
|
|
|
#include "../resource.h" |
|
|
#include "wptTypes.h" |
|
|
#include "wptErrors.h" |
|
|
#include "wptW32API.h" |
|
|
#include "wptGPG.h" |
|
|
|
|
|
|
|
|
static LPMAPILOGON mapi_logon = NULL; |
|
|
static LPMAPILOGOFF mapi_logoff = NULL; |
|
|
static LPMAPISENDDOCUMENTS mapi_send_documents = NULL; |
|
|
static LPMAPISENDMAIL mapi_send_mail = NULL; |
|
|
static HINSTANCE hlib = NULL; |
|
|
static int init = 0; |
|
|
|
|
|
#define load_one_fnc(cast, hlib, name) (cast)GetProcAddress ((hlib), name) |
|
|
|
|
|
|
|
|
int |
|
|
mapi_init (void) |
|
|
{ |
|
|
if (init) |
|
|
return 0; |
|
|
|
|
|
hlib = LoadLibrary ("MAPI32.DLL"); |
|
|
if (!hlib) |
|
|
return -1; |
|
|
|
|
|
mapi_logon = load_one_fnc (LPMAPILOGON, hlib, "MAPILogon"); |
|
|
mapi_logoff = load_one_fnc (LPMAPILOGOFF, hlib, "MAPILogoff"); |
|
|
mapi_send_documents = load_one_fnc (LPMAPISENDDOCUMENTS, hlib, "MAPISendDocuments"); |
|
|
mapi_send_mail = load_one_fnc (LPMAPISENDMAIL, hlib, "MAPISendMail"); |
|
|
if (!mapi_logon || !mapi_logoff || !mapi_send_documents || !mapi_send_mail) |
|
|
return -1; |
|
|
init = 1; |
|
|
|
|
|
return 0; |
|
|
} /* mapi_init */ |
|
|
|
|
|
|
|
|
void |
|
|
mapi_deinit (void) |
|
|
{ |
|
|
if (hlib) { |
|
|
FreeLibrary (hlib); |
|
|
hlib = NULL; |
|
|
init = 0; |
|
|
} |
|
|
} /* mapi_deinit */ |
|
|
|
|
|
#if 0 /* low:priority XXX port the code */ |
|
|
int |
|
|
mapi_send_ascfile (char *ascfile) |
|
|
{ |
|
|
LHANDLE hd; |
|
|
int rc; |
|
|
|
|
|
if (!init) |
|
|
return 0; |
|
|
|
|
|
rc = mapi_logon (0, NULL, NULL, MAPI_LOGON_UI, 0, &hd); |
|
|
if (rc != SUCCESS_SUCCESS) { |
|
|
MessageBox (NULL, _("MAPI Login failed."), "MAPI", MB_ICONWARNING|MB_OK); |
|
|
goto fail; |
|
|
} |
|
|
rc = mapi_send_documents (0, ";", ascfile, NULL, 0); |
|
|
if (rc == MAPI_E_USER_ABORT) |
|
|
rc = SUCCESS_SUCCESS; |
|
|
if (rc != SUCCESS_SUCCESS) |
|
|
MessageBox (NULL, _("Could not sent mail."), "MAPI", MB_ICONERROR|MB_OK); |
|
|
|
|
|
fail: |
|
|
mapi_logoff (hd, 0, 0, 0); |
|
|
return rc; |
|
|
} |
|
|
|
|
|
|
|
|
int |
|
|
mapi_send_pubkey (const char *keyid, char *keyfile) |
|
|
{ |
|
|
LHANDLE hd; |
|
|
const char * fmt; |
|
|
char * keyinf = NULL; |
|
|
int rc; |
|
|
|
|
|
if (!init) |
|
|
return 0; |
|
|
|
|
|
fmt = _("GPG Public Key of %s"); |
|
|
keyinf = new char[strlen (fmt) + strlen (keyid) + 2]; |
|
|
if (!keyinf) |
|
|
BUG (0); |
|
|
sprintf (keyinf, fmt, keyid); |
|
|
rc = mapi_logon (0, NULL, NULL, MAPI_LOGON_UI, 0, &hd); |
|
|
if (rc != SUCCESS_SUCCESS) { |
|
|
MessageBox (NULL, _("MAPI Login failed."), "MAPI", MB_ICONWARNING|MB_OK); |
|
|
goto fail; |
|
|
} |
|
|
rc = mapi_send_documents (0, ";", keyfile, keyinf, 0); |
|
|
if (rc == MAPI_E_USER_ABORT) |
|
|
rc = SUCCESS_SUCCESS; |
|
|
if (rc != SUCCESS_SUCCESS) |
|
|
MessageBox (NULL, _("Could not sent mail."), "MAPI", MB_ICONERROR|MB_OK); |
|
|
|
|
|
fail: |
|
|
mapi_logoff (hd, 0, 0, 0); |
|
|
free_if_alloc (keyinf); |
|
|
return rc; |
|
|
} /* mapi_send_pubkey */ |
|
|
|
|
|
|
|
|
static void |
|
|
free_mapi_msg (MapiMessage * msg) |
|
|
{ |
|
|
if (!msg) |
|
|
return; |
|
|
safe_free (msg->lpszSubject); |
|
|
safe_free (msg->lpszNoteText); |
|
|
safe_free (msg); |
|
|
} /* free_mapi_msg */ |
|
|
|
|
|
|
|
|
static void |
|
|
free_recip_tab (MapiRecipDesc *recip, size_t n) |
|
|
{ |
|
|
size_t i; |
|
|
|
|
|
if (!recip) |
|
|
return; |
|
|
if (!n) |
|
|
return; |
|
|
for (i=0; i < n; i++) |
|
|
safe_free (recip[i].lpszName); |
|
|
safe_free (recip); |
|
|
} /* free_recip_tab */ |
|
|
|
|
|
|
|
|
static void |
|
|
free_files_tab (MapiFileDesc * files, size_t n) |
|
|
{ |
|
|
size_t i; |
|
|
|
|
|
if (!files) |
|
|
return; |
|
|
if (!n) |
|
|
return; |
|
|
for (i=0; i < n; i++) { |
|
|
safe_free (files[i].lpszFileName); |
|
|
safe_free (files[i].lpszPathName); |
|
|
} |
|
|
safe_free (files); |
|
|
} /* free_files_tab */ |
|
|
|
|
|
|
|
|
|
|
|
static gpgme_recipients_t |
|
|
conv_recipients (gpgme_recipients_t rset) |
|
|
{ |
|
|
gpgme_recipients_t r; |
|
|
gpgme_error_t rc; |
|
|
void * ctx=NULL; |
|
|
const char * s; |
|
|
|
|
|
/* we need to convert the recipients to email addresses so |
|
|
GPG can handle them. */ |
|
|
rc = gpgme_recipients_new (&r); |
|
|
if (rc) |
|
|
return NULL; |
|
|
gpgme_recipients_enum_open (rset, &ctx); |
|
|
|
|
|
while ((s=gpgme_recipients_enum_read (rset, &ctx))) { |
|
|
char * p, * q, * buf; |
|
|
if (!(p = strchr (s, '<')) || !(q = strchr (s, '>'))) |
|
|
continue; |
|
|
buf = (char * )calloc (1, (q-s)-(p-s)+2); |
|
|
if (!buf) |
|
|
BUG (0); |
|
|
strncpy (buf, s+(p-s)+1, (q-s)-(p-s)-1); |
|
|
gpgme_recipients_add_name (r, buf); |
|
|
safe_free (buf); |
|
|
} |
|
|
return r; |
|
|
} /* conv_recipients */ |
|
|
|
|
|
|
|
|
static char * |
|
|
secure_attachment (gpgme_recipients_t rset, const char *fname) |
|
|
{ |
|
|
char tmpdir[512+32], * p; |
|
|
gpgme_recipients_t addrs; |
|
|
gpgme_ctx_t ctx; |
|
|
gpgme_error_t rc; |
|
|
|
|
|
if (strlen (fname) > 200) |
|
|
BUG (0); |
|
|
GetTempPath (sizeof tmpdir-200, tmpdir); |
|
|
p = strrchr (fname, '\\'); |
|
|
if (!p) |
|
|
strcat (tmpdir, fname); |
|
|
else |
|
|
strcat (tmpdir, fname+(p-fname)+1); |
|
|
strcat (tmpdir, ".asc"); |
|
|
|
|
|
rc = gpgme_new (&ctx); |
|
|
if (rc) |
|
|
return NULL; |
|
|
gpgme_set_armor (ctx, 1); |
|
|
addrs = conv_recipients (rset); |
|
|
if (!addrs) { |
|
|
msg_box (NULL, _("No valid mail addresses found."), _("Secure Attachment"), MB_ERR); |
|
|
gpgme_release (NULL); |
|
|
return NULL; |
|
|
} |
|
|
rc = gpgme_op_file_encrypt (ctx, addrs, fname, tmpdir); |
|
|
if (rc) |
|
|
log_box (_("Secure Attachment"), MB_ERR, _("Could not encrypt '%s'"), fname); |
|
|
gpgme_recipients_release (addrs); |
|
|
gpgme_release (ctx); |
|
|
return strdup (tmpdir); |
|
|
} /* secure_attachment */ |
|
|
|
|
|
|
|
|
static char * |
|
|
secure_message (gpgme_recipients_t rset, const char * data) |
|
|
{ |
|
|
gpgme_recipients_t addrs; |
|
|
gpgme_error_t rc; |
|
|
gpgme_data_t in, out; |
|
|
gpgme_ctx_t ctx; |
|
|
char * p; |
|
|
size_t n=0; |
|
|
|
|
|
rc = gpgme_new (&ctx); |
|
|
if (rc) |
|
|
return NULL; |
|
|
gpgme_set_armor (ctx, 1); |
|
|
|
|
|
addrs = conv_recipients (rset); |
|
|
rc = gpgme_data_new_from_mem (&in, data, strlen (data), 1); |
|
|
if (rc) { |
|
|
gpgme_release (ctx); |
|
|
return NULL; |
|
|
} |
|
|
gpgme_data_new (&out); |
|
|
rc = gpgme_op_encrypt (ctx, addrs, GPGME_ENCRYPT_ALWAYS_TRUST, in, out); |
|
|
if (rc) |
|
|
log_box (_("Secure Message"), MB_ERR, "Could not encrypt the data"); |
|
|
|
|
|
p = gpgme_data_release_and_get_mem (&n); |
|
|
p[n] = 0; |
|
|
|
|
|
gpgme_data_release (in); |
|
|
gpgme_release (ctx); |
|
|
gpgme_recipients_release (addrs); |
|
|
|
|
|
return p; |
|
|
} /* secure_message */ |
|
|
|
|
|
|
|
|
int |
|
|
mapi_send_message (gpgme_recipients_t rset, const char * msgtxt, |
|
|
const char * subject, const char **files, size_t nfiles) |
|
|
{ |
|
|
LHANDLE hd; |
|
|
MapiMessage * msg; |
|
|
MapiRecipDesc * recip; |
|
|
MapiFileDesc * attch; |
|
|
char * p; |
|
|
const char * s; |
|
|
void * ctx=NULL; |
|
|
size_t n, i=0; |
|
|
int rc; |
|
|
|
|
|
if (!init) |
|
|
return 0; |
|
|
|
|
|
rc = mapi_logon (0, NULL, NULL, MAPI_LOGON_UI, 0, &hd); |
|
|
if (rc != SUCCESS_SUCCESS) { |
|
|
MessageBox (NULL, "MAPI Login failed.", "MAPI", MB_ICONWARNING|MB_OK); |
|
|
return rc; |
|
|
} |
|
|
|
|
|
msg = (MapiMessage *)calloc (1, sizeof * msg); |
|
|
if (!msg) |
|
|
BUG (0); |
|
|
p = msg->lpszSubject = strdup (subject); |
|
|
if (!p) |
|
|
BUG (0); |
|
|
p = msg->lpszNoteText = secure_message (rset, msgtxt); |
|
|
if (!p) |
|
|
BUG (0); |
|
|
n = msg->nRecipCount = gpgme_recipients_count (rset); |
|
|
recip = (MapiRecipDesc *)calloc (n+1, sizeof * recip); |
|
|
if (!recip) |
|
|
BUG (0); |
|
|
|
|
|
gpgme_recipients_enum_open (rset, &ctx); |
|
|
while ((s = gpgme_recipients_enum_read (rset, &ctx))) { |
|
|
if (!i) |
|
|
recip[i].ulRecipClass = MAPI_TO; |
|
|
else |
|
|
recip[i].ulRecipClass = MAPI_CC; |
|
|
p = recip[i].lpszName = strdup (s); |
|
|
if (!p) |
|
|
BUG (0); |
|
|
i++; |
|
|
} |
|
|
msg->lpRecips = recip; |
|
|
|
|
|
if (nfiles) { |
|
|
msg->nFileCount = nfiles; |
|
|
attch = (MapiFileDesc *)calloc (nfiles+1, sizeof * attch); |
|
|
if (!attch) |
|
|
BUG (0); |
|
|
for (i=0; i < nfiles; i++) { |
|
|
char * p = secure_attachment (rset, *files); |
|
|
if (!p) |
|
|
continue; |
|
|
attch[i].lpszFileName = strdup (*files); |
|
|
attch[i].lpszPathName = strdup (p); |
|
|
files++; |
|
|
safe_free (p); |
|
|
} |
|
|
msg->lpFiles = attch; |
|
|
} |
|
|
|
|
|
rc = mapi_send_mail (hd, 0, msg, 0, 0); |
|
|
if (rc == MAPI_E_USER_ABORT) |
|
|
rc = SUCCESS_SUCCESS; |
|
|
if (rc != SUCCESS_SUCCESS) |
|
|
MessageBox (NULL, _("Could not sent mail."), "MAPI", MB_ERR); |
|
|
|
|
|
free_recip_tab (recip, n); |
|
|
free_files_tab (attch, nfiles); |
|
|
free_mapi_msg (msg); |
|
|
mapi_logoff (hd, 0, 0, 0); |
|
|
|
|
|
return 0; |
|
|
} /* mapi_send_message */ |
|
|
|
|
|
|
|
|
static int |
|
|
add_recipient (gpgme_recipients_t * r_rset, const char * addr) |
|
|
{ |
|
|
gpgme_keycache_t pub = keycache_get_ctx (1); |
|
|
gpgme_key_t key; |
|
|
gpgme_error_t rc; |
|
|
const char * s; |
|
|
|
|
|
if (!*r_rset) |
|
|
gpgme_recipients_new (&(*r_rset)); |
|
|
rc = gpgme_keycache_find_key (pub, addr, 0, &key); |
|
|
if (rc) { |
|
|
log_box (_("Add Recipient"), MB_ERR, _("Could not find key for '%s'"), addr); |
|
|
return -1; |
|
|
} |
|
|
s = key->uids->uid; |
|
|
if (s) |
|
|
gpgme_recipients_add_name (*r_rset, s); |
|
|
return 0; |
|
|
} /* add_recipient */ |
|
|
|
|
|
|
|
|
static int |
|
|
add_all_recipients (HWND dlg, int itemid, gpgme_recipients_t * r_rset) |
|
|
{ |
|
|
char buf[1024], * p; |
|
|
int n=0; |
|
|
|
|
|
n = GetDlgItemText (dlg, itemid, buf, sizeof buf-10); |
|
|
if (!n) |
|
|
return -1; |
|
|
p = strtok (buf, ";,"); |
|
|
while (p != NULL) { |
|
|
add_recipient (&*r_rset, p); |
|
|
p = strtok (NULL, ";,"); |
|
|
} |
|
|
return 0; |
|
|
} /* add_all_recipients */ |
|
|
|
|
|
|
|
|
BOOL CALLBACK |
|
|
winpt_mail_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam) |
|
|
{ |
|
|
static gpgme_recipients_t rset=NULL; |
|
|
char subject[128]; |
|
|
char * msgbuf; |
|
|
int n; |
|
|
|
|
|
switch (msg) { |
|
|
case WM_INITDIALOG: |
|
|
center_window (dlg, NULL); |
|
|
SetForegroundWindow (dlg); |
|
|
break; |
|
|
|
|
|
case WM_COMMAND: |
|
|
switch (LOWORD (wparam)) { |
|
|
case IDOK: |
|
|
add_all_recipients (dlg, IDC_PMAIL_TO, &rset); |
|
|
add_all_recipients (dlg, IDC_PMAIL_CC, &rset); |
|
|
if (!gpgme_recipients_count (rset)) { |
|
|
msg_box (dlg, _("Please enter a recipient."), _("Mail"), MB_ERR); |
|
|
return FALSE; |
|
|
} |
|
|
n=GetDlgItemText (dlg, IDC_PMAIL_SUBJECT, subject, sizeof subject-1); |
|
|
if (!n) |
|
|
strcpy (subject, ""); |
|
|
n = SendDlgItemMessage (dlg, IDC_PMAIL_MSG, WM_GETTEXTLENGTH, 0, 0); |
|
|
if (!n) { |
|
|
msg_box (dlg, _("Please enter a message."), _("Mail"), MB_ERR); |
|
|
return FALSE; |
|
|
} |
|
|
msgbuf = (char * )calloc (1, n+2); |
|
|
if (!msgbuf) |
|
|
BUG (0); |
|
|
GetDlgItemText (dlg, IDC_PMAIL_MSG, msgbuf, n+1); |
|
|
mapi_send_message (rset, msgbuf, subject, NULL, 0); |
|
|
safe_free (msgbuf); |
|
|
EndDialog (dlg, TRUE); |
|
|
break; |
|
|
|
|
|
case IDCANCEL: |
|
|
EndDialog (dlg, FALSE); |
|
|
break; |
|
|
} |
|
|
break; |
|
|
} |
|
|
|
|
|
return FALSE; |
|
|
} /* winpt_mail_proc */ |
|
|
#endif |
|
1 |
|
/* wptMAPI.cpp - MAPI interface for sending keys. |
2 |
|
* Copyright (C) 2003, 2004, 2005, 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 |
|
/* Load MAPI library and set function pointers. |
49 |
|
Return value: 0 on success. */ |
50 |
|
int |
51 |
|
mapi_init (void) |
52 |
|
{ |
53 |
|
if (init) |
54 |
|
return 0; |
55 |
|
|
56 |
|
hlib = LoadLibrary ("MAPI32.DLL"); |
57 |
|
if (!hlib) |
58 |
|
return -1; |
59 |
|
|
60 |
|
mapi_logon = load_one_fnc (LPMAPILOGON, hlib, "MAPILogon"); |
61 |
|
mapi_logoff = load_one_fnc (LPMAPILOGOFF, hlib, "MAPILogoff"); |
62 |
|
mapi_send_documents = load_one_fnc (LPMAPISENDDOCUMENTS, hlib, "MAPISendDocuments"); |
63 |
|
mapi_send_mail = load_one_fnc (LPMAPISENDMAIL, hlib, "MAPISendMail"); |
64 |
|
if (!mapi_logon || !mapi_logoff || !mapi_send_documents || !mapi_send_mail) |
65 |
|
return -1; |
66 |
|
init = 1; |
67 |
|
|
68 |
|
return 0; |
69 |
|
} |
70 |
|
|
71 |
|
|
72 |
|
/* Free library and cleanup. */ |
73 |
|
void |
74 |
|
mapi_deinit (void) |
75 |
|
{ |
76 |
|
if (hlib) { |
77 |
|
FreeLibrary (hlib); |
78 |
|
hlib = NULL; |
79 |
|
init = 0; |
80 |
|
} |
81 |
|
} |
82 |
|
|
83 |
|
|
84 |
|
/* Send the file given in @ascfile via the MAPI mechanism. */ |
85 |
|
int |
86 |
|
mapi_send_ascfile (char *ascfile) |
87 |
|
{ |
88 |
|
LHANDLE hd; |
89 |
|
int rc; |
90 |
|
|
91 |
|
if (!init) |
92 |
|
return 0; |
93 |
|
|
94 |
|
rc = mapi_logon (0, NULL, NULL, MAPI_LOGON_UI, 0, &hd); |
95 |
|
if (rc != SUCCESS_SUCCESS) { |
96 |
|
MessageBox (NULL, _("MAPI Login failed."), "MAPI", MB_ICONWARNING|MB_OK); |
97 |
|
goto fail; |
98 |
|
} |
99 |
|
rc = mapi_send_documents (0, ";", ascfile, NULL, 0); |
100 |
|
if (rc == MAPI_E_USER_ABORT) |
101 |
|
rc = SUCCESS_SUCCESS; |
102 |
|
if (rc != SUCCESS_SUCCESS) |
103 |
|
MessageBox (NULL, _("Could not sent mail."), "MAPI", MB_ICONERROR|MB_OK); |
104 |
|
|
105 |
|
fail: |
106 |
|
mapi_logoff (hd, 0, 0, 0); |
107 |
|
return rc; |
108 |
|
} |
109 |
|
|
110 |
|
|
111 |
|
static void |
112 |
|
free_mapi_msg (MapiMessage *msg) |
113 |
|
{ |
114 |
|
if (!msg) |
115 |
|
return; |
116 |
|
safe_free (msg->lpszSubject); |
117 |
|
safe_free (msg->lpszNoteText); |
118 |
|
safe_free (msg); |
119 |
|
} |
120 |
|
|
121 |
|
|
122 |
|
static void |
123 |
|
free_recip_tab (MapiRecipDesc *recip, size_t n) |
124 |
|
{ |
125 |
|
size_t i; |
126 |
|
|
127 |
|
if (!recip) |
128 |
|
return; |
129 |
|
if (!n) |
130 |
|
return; |
131 |
|
for (i=0; i < n; i++) |
132 |
|
safe_free (recip[i].lpszName); |
133 |
|
safe_free (recip); |
134 |
|
} |
135 |
|
|
136 |
|
|
137 |
|
static void |
138 |
|
free_files_tab (MapiFileDesc *files, size_t n) |
139 |
|
{ |
140 |
|
size_t i; |
141 |
|
|
142 |
|
if (!files) |
143 |
|
return; |
144 |
|
if (!n) |
145 |
|
return; |
146 |
|
for (i=0; i < n; i++) { |
147 |
|
safe_free (files[i].lpszFileName); |
148 |
|
safe_free (files[i].lpszPathName); |
149 |
|
} |
150 |
|
safe_free (files); |
151 |
|
} |
152 |
|
|
153 |
|
|
154 |
|
/* Same as mapi_send_pubkey but there is an additional note. */ |
155 |
|
int |
156 |
|
mapi_send_pubkey_ext (gpgme_key_t to, const char *keyfile, int flags) |
157 |
|
{ |
158 |
|
LHANDLE hd; |
159 |
|
MapiMessage *msg; |
160 |
|
MapiRecipDesc *recip; |
161 |
|
MapiFileDesc *attch; |
162 |
|
char *p, *kinf; |
163 |
|
const char *s; |
164 |
|
int rc; |
165 |
|
|
166 |
|
if (!init) |
167 |
|
return 0; |
168 |
|
|
169 |
|
rc = mapi_logon (0, NULL, NULL, MAPI_LOGON_UI, 0, &hd); |
170 |
|
if (rc != SUCCESS_SUCCESS) { |
171 |
|
MessageBox (NULL, "MAPI Login failed.", "MAPI", MB_ICONWARNING|MB_OK); |
172 |
|
return rc; |
173 |
|
} |
174 |
|
|
175 |
|
msg = (MapiMessage *)calloc (1, sizeof *msg); |
176 |
|
if (!msg) |
177 |
|
BUG (0); |
178 |
|
p = msg->lpszSubject = strdup ("OpenPGP Public Key"); |
179 |
|
if (!p) |
180 |
|
BUG (0); |
181 |
|
|
182 |
|
s = "Attached is this OpenPGP public key:\n%s\n\n" |
183 |
|
"Import this key via the clipboard or the Key Manager to\n" |
184 |
|
"exchange encrypted mails with the key holder and to be able\n" |
185 |
|
"to verify its signatures.\n" |
186 |
|
"\n" |
187 |
|
"If you don't have WinPT, you can download it at http://www.winpt.org"; |
188 |
|
kinf = km_key_get_info (to, 0); |
189 |
|
p = (char*)malloc (strlen (s) + strlen (kinf) + 2); |
190 |
|
sprintf (p, s, kinf); |
191 |
|
free_if_alloc (kinf); |
192 |
|
|
193 |
|
p = msg->lpszNoteText = p; |
194 |
|
if (!p) |
195 |
|
BUG (0); |
196 |
|
|
197 |
|
/* If the key was signed, we assume it shall be sent back to the owner. */ |
198 |
|
if (flags) { |
199 |
|
recip = (MapiRecipDesc *)calloc (1, sizeof *recip); |
200 |
|
if (!recip) |
201 |
|
BUG (0); |
202 |
|
recip[0].ulRecipClass = MAPI_TO; |
203 |
|
p = recip[0].lpszName = strdup (to->uids->uid); |
204 |
|
if (!p) |
205 |
|
BUG (0); |
206 |
|
msg->lpRecips = recip; |
207 |
|
msg->nRecipCount = 1; |
208 |
|
} |
209 |
|
else { |
210 |
|
msg->lpRecips = recip = NULL; |
211 |
|
msg->nRecipCount = 0; |
212 |
|
} |
213 |
|
|
214 |
|
msg->nFileCount = 1; |
215 |
|
attch = (MapiFileDesc *)calloc (1, sizeof *attch); |
216 |
|
if (!attch) |
217 |
|
BUG (0); |
218 |
|
attch[0].lpszFileName = strdup (keyfile); |
219 |
|
attch[0].lpszPathName = strdup (keyfile); |
220 |
|
msg->lpFiles = attch; |
221 |
|
|
222 |
|
rc = mapi_send_mail (hd, 0, msg, MAPI_DIALOG , 0); |
223 |
|
if (rc == MAPI_E_USER_ABORT) |
224 |
|
rc = SUCCESS_SUCCESS; |
225 |
|
if (rc != SUCCESS_SUCCESS) |
226 |
|
MessageBox (NULL, _("Could not sent mail."), "MAPI", MB_ERR); |
227 |
|
|
228 |
|
free_recip_tab (recip, 1); |
229 |
|
free_files_tab (attch, 1); |
230 |
|
free_mapi_msg (msg); |
231 |
|
mapi_logoff (hd, 0, 0, 0); |
232 |
|
|
233 |
|
return 0; |
234 |
|
} |
235 |
|
|
236 |
|
|
237 |
|
#if 0 |
238 |
|
/* Send a public key stored in @keyfile with the keyid @keyid |
239 |
|
via the MAPI mechanism to a mail recipient. |
240 |
|
Return value: SUCCESS_SUCCESS on succes. */ |
241 |
|
int |
242 |
|
mapi_send_pubkey2 (const char *keyid, char *keyfile) |
243 |
|
{ |
244 |
|
LHANDLE hd; |
245 |
|
const char * fmt; |
246 |
|
char * keyinf = NULL; |
247 |
|
int rc; |
248 |
|
|
249 |
|
if (!init) |
250 |
|
return 0; |
251 |
|
|
252 |
|
fmt = _("GPG Public Key of %s"); |
253 |
|
keyinf = new char[strlen (fmt) + strlen (keyid) + 2]; |
254 |
|
if (!keyinf) |
255 |
|
BUG (0); |
256 |
|
sprintf (keyinf, fmt, keyid); |
257 |
|
rc = mapi_logon (0, NULL, NULL, MAPI_LOGON_UI, 0, &hd); |
258 |
|
if (rc != SUCCESS_SUCCESS) { |
259 |
|
MessageBox (NULL, _("MAPI Login failed."), "MAPI", MB_ICONWARNING|MB_OK); |
260 |
|
goto fail; |
261 |
|
} |
262 |
|
rc = mapi_send_documents (0, ";", keyfile, keyinf, 0); |
263 |
|
if (rc == MAPI_E_USER_ABORT) |
264 |
|
rc = SUCCESS_SUCCESS; |
265 |
|
if (rc != SUCCESS_SUCCESS) |
266 |
|
MessageBox (NULL, _("Could not sent mail."), "MAPI", MB_ICONERROR|MB_OK); |
267 |
|
|
268 |
|
fail: |
269 |
|
mapi_logoff (hd, 0, 0, 0); |
270 |
|
free_if_alloc (keyinf); |
271 |
|
return rc; |
272 |
|
} |
273 |
|
#endif |
274 |
|
|
275 |
|
|
276 |
|
int |
277 |
|
mapi_send_pubkey (const char *keyid, char *keyfile) |
278 |
|
{ |
279 |
|
gpgme_key_t key; |
280 |
|
|
281 |
|
get_pubkey (keyid, &key); |
282 |
|
return mapi_send_pubkey_ext (key, keyfile, 0); |
283 |
|
} |