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

Diff of /trunk/Src/wptMAPI.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 24 by twoaday, Sat Oct 8 10:43:08 2005 UTC revision 48 by werner, Mon Oct 31 21:14:11 2005 UTC
# Line 1  Line 1 
 /* 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
2     *      Copyright (C) 2003, 2004 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    extern HINSTANCE glob_hinst;
29    
30    #include "resource.h"
31    #include "wptTypes.h"
32    #include "wptErrors.h"
33    #include "wptW32API.h"
34    #include "wptGPG.h"
35    
36    
37    static LPMAPILOGON          mapi_logon = NULL;
38    static LPMAPILOGOFF         mapi_logoff = NULL;    
39    static LPMAPISENDDOCUMENTS  mapi_send_documents = NULL;
40    static LPMAPISENDMAIL       mapi_send_mail = NULL;
41    static HINSTANCE            hlib = NULL;
42    static int                  init = 0;
43    
44    #define load_one_fnc(cast, hlib, name) (cast)GetProcAddress ((hlib), name)
45    
46    
47    int
48    mapi_init (void)
49    {
50        if (init)
51            return 0;
52    
53        hlib = LoadLibrary ("MAPI32.DLL");
54        if (!hlib)
55            return -1;
56    
57        mapi_logon = load_one_fnc (LPMAPILOGON, hlib, "MAPILogon");
58        mapi_logoff = load_one_fnc (LPMAPILOGOFF, hlib, "MAPILogoff");
59        mapi_send_documents = load_one_fnc (LPMAPISENDDOCUMENTS, hlib, "MAPISendDocuments");
60        mapi_send_mail = load_one_fnc (LPMAPISENDMAIL, hlib, "MAPISendMail");
61        if (!mapi_logon || !mapi_logoff || !mapi_send_documents || !mapi_send_mail)
62            return -1;
63        init = 1;
64    
65        return 0;
66    } /* mapi_init */
67    
68    
69    void
70    mapi_deinit (void)
71    {
72        if (hlib) {
73            FreeLibrary (hlib);
74            hlib = NULL;
75            init = 0;
76        }
77    } /* mapi_deinit */
78    
79    #if 0 /* low:priority XXX port the code */
80    int
81    mapi_send_ascfile (char *ascfile)
82    {
83        LHANDLE hd;
84        int rc;
85    
86        if (!init)
87            return 0;
88    
89        rc = mapi_logon (0, NULL, NULL, MAPI_LOGON_UI, 0, &hd);
90        if (rc != SUCCESS_SUCCESS) {
91            MessageBox (NULL, _("MAPI Login failed."), "MAPI", MB_ICONWARNING|MB_OK);
92            goto fail;
93        }
94        rc = mapi_send_documents (0, ";", ascfile, NULL, 0);
95        if (rc == MAPI_E_USER_ABORT)
96            rc = SUCCESS_SUCCESS;
97        if (rc != SUCCESS_SUCCESS)
98            MessageBox (NULL, _("Could not sent mail."), "MAPI", MB_ICONERROR|MB_OK);
99    
100    fail:
101        mapi_logoff (hd, 0, 0, 0);
102        return rc;
103    }
104    
105    
106    int
107    mapi_send_pubkey (const char *keyid, char *keyfile)
108    {
109        LHANDLE hd;
110        const char * fmt;
111        char * keyinf = NULL;
112        int rc;
113    
114        if (!init)
115            return 0;
116    
117        fmt = _("GPG Public Key of %s");
118        keyinf = new char[strlen (fmt) + strlen (keyid) + 2];
119        if (!keyinf)
120            BUG (0);
121        sprintf (keyinf, fmt, keyid);
122        rc = mapi_logon (0, NULL, NULL, MAPI_LOGON_UI, 0, &hd);
123        if (rc != SUCCESS_SUCCESS) {
124            MessageBox (NULL, _("MAPI Login failed."), "MAPI", MB_ICONWARNING|MB_OK);
125            goto fail;
126        }
127        rc = mapi_send_documents (0, ";", keyfile, keyinf, 0);
128        if (rc == MAPI_E_USER_ABORT)
129            rc = SUCCESS_SUCCESS;
130        if (rc != SUCCESS_SUCCESS)
131            MessageBox (NULL, _("Could not sent mail."), "MAPI", MB_ICONERROR|MB_OK);
132    
133    fail:
134        mapi_logoff (hd, 0, 0, 0);
135        free_if_alloc (keyinf);
136        return rc;
137    } /* mapi_send_pubkey */
138    
139    
140    static void
141    free_mapi_msg (MapiMessage * msg)
142    {
143        if (!msg)
144            return;
145        safe_free (msg->lpszSubject);
146        safe_free (msg->lpszNoteText);
147        safe_free (msg);
148    } /* free_mapi_msg */
149    
150    
151    static void
152    free_recip_tab (MapiRecipDesc *recip, size_t n)
153    {
154        size_t i;
155    
156        if (!recip)
157            return;
158        if (!n)
159            return;
160        for (i=0; i < n; i++)
161            safe_free (recip[i].lpszName);
162        safe_free (recip);
163    } /* free_recip_tab */
164    
165    
166    static void
167    free_files_tab (MapiFileDesc * files, size_t n)
168    {
169        size_t i;
170    
171        if (!files)
172            return;
173        if (!n)
174            return;
175        for (i=0; i < n; i++) {
176            safe_free (files[i].lpszFileName);
177            safe_free (files[i].lpszPathName);
178        }
179        safe_free (files);
180    } /* free_files_tab */
181    
182    
183    
184    static gpgme_recipients_t
185    conv_recipients (gpgme_recipients_t rset)
186    {
187        gpgme_recipients_t r;
188        gpgme_error_t rc;
189        void * ctx=NULL;
190        const char * s;
191    
192        /* we need to convert the recipients to email addresses so
193           GPG can handle them. */
194        rc = gpgme_recipients_new (&r);
195        if (rc)
196            return NULL;
197        gpgme_recipients_enum_open (rset, &ctx);
198    
199        while ((s=gpgme_recipients_enum_read (rset, &ctx))) {
200            char * p, * q, * buf;
201            if (!(p = strchr (s, '<')) || !(q = strchr (s, '>')))
202                continue;
203            buf = (char * )calloc (1, (q-s)-(p-s)+2);
204            if (!buf)
205                BUG (0);
206            strncpy (buf, s+(p-s)+1, (q-s)-(p-s)-1);
207            gpgme_recipients_add_name (r, buf);
208            safe_free (buf);
209        }
210        return r;
211    } /* conv_recipients */
212    
213    
214    static char *
215    secure_attachment (gpgme_recipients_t rset, const char *fname)
216    {
217        char tmpdir[512+32], * p;
218        gpgme_recipients_t addrs;
219        gpgme_ctx_t ctx;
220        gpgme_error_t rc;
221    
222        if (strlen (fname) > 200)
223            BUG (0);
224        GetTempPath (sizeof tmpdir-200, tmpdir);
225        p = strrchr (fname, '\\');
226        if (!p)
227            strcat (tmpdir, fname);
228        else
229            strcat (tmpdir, fname+(p-fname)+1);
230        strcat (tmpdir, ".asc");
231    
232        rc = gpgme_new (&ctx);
233        if (rc)
234            return NULL;
235        gpgme_set_armor (ctx, 1);
236        addrs = conv_recipients (rset);
237        if (!addrs) {
238            msg_box (NULL, _("No valid mail addresses found."), _("Secure Attachment"), MB_ERR);
239            gpgme_release (NULL);
240            return NULL;
241        }
242        rc = gpgme_op_file_encrypt (ctx, addrs, fname, tmpdir);
243        if (rc)
244            log_box (_("Secure Attachment"), MB_ERR, _("Could not encrypt '%s'"), fname);
245        gpgme_recipients_release (addrs);
246        gpgme_release (ctx);
247        return strdup (tmpdir);
248    } /* secure_attachment */
249    
250    
251    static gpgme_error_t
252    secure_message (gpgme_recipients_t rset, const char *data,
253                    char *enc_msg, size_t *r_enclen)
254    {
255        gpgme_recipients_t addrs;
256        gpgme_error_t rc;
257        gpgme_data_t in, out;
258        gpgme_ctx_t ctx;
259        char * p;
260        size_t n=0;
261    
262        rc = gpgme_new (&ctx);
263        if (rc)
264            return NULL;
265        gpgme_set_armor (ctx, 1);
266    
267        addrs = conv_recipients (rset);
268        rc = gpgme_data_new_from_mem (&in, data, strlen (data), 1);
269        if (rc) {
270            gpgme_release (ctx);
271            return rc;
272        }
273        gpgme_data_new (&out);
274        rc = gpgme_op_encrypt (ctx, addrs, GPGME_ENCRYPT_ALWAYS_TRUST, in, out);
275        if (rc)
276            log_box (_("Secure Message"), MB_ERR, "Could not encrypt the data");
277    
278        *r_enc_msg = gpgme_data_release_and_get_mem (&n);
279        *r_enclen = n;
280    
281        gpgme_data_release (in);
282        gpgme_release (ctx);
283        gpgme_recipients_release (addrs);
284    
285        return rc;
286    } /* secure_message */
287    
288    
289    int
290    mapi_send_message (gpgme_recipients_t rset, const char * msgtxt,
291                       const char * subject, const char **files, size_t nfiles)
292    {
293        LHANDLE hd;
294        MapiMessage * msg;
295        MapiRecipDesc * recip;
296        MapiFileDesc * attch;
297        char * p;
298        const char * s;
299        void * ctx=NULL;
300        size_t n, i=0, encmsg_len=0;
301        int rc;
302    
303        if (!init)
304            return 0;
305    
306        rc = mapi_logon (0, NULL, NULL, MAPI_LOGON_UI, 0, &hd);
307        if (rc != SUCCESS_SUCCESS) {
308            MessageBox (NULL, "MAPI Login failed.", "MAPI", MB_ICONWARNING|MB_OK);
309            return rc;
310        }
311    
312        msg = (MapiMessage *)calloc (1, sizeof * msg);
313        if (!msg)
314            BUG (0);
315        p = msg->lpszSubject = strdup (subject);
316        if (!p)
317            BUG (0);
318        p = msg->lpszNoteText = secure_message (rset, msgtxt, &p, &encmsg_len);
319        if (!p)
320            BUG (0);
321        n = msg->nRecipCount = gpgme_recipients_count (rset);
322        recip = (MapiRecipDesc *)calloc (n+1, sizeof * recip);
323        if (!recip)
324            BUG (0);
325        
326        gpgme_recipients_enum_open (rset, &ctx);
327        while ((s = gpgme_recipients_enum_read (rset, &ctx))) {
328            if (!i)
329                recip[i].ulRecipClass = MAPI_TO;
330            else
331                recip[i].ulRecipClass = MAPI_CC;    
332            p = recip[i].lpszName = strdup (s);
333            if (!p)
334                BUG (0);
335            i++;
336        }
337        msg->lpRecips = recip;
338    
339        if (nfiles) {
340            msg->nFileCount = nfiles;
341            attch = (MapiFileDesc *)calloc (nfiles+1, sizeof * attch);
342            if (!attch)
343                BUG (0);
344            for (i=0; i < nfiles; i++) {
345                char * p = secure_attachment (rset, *files);
346                if (!p)
347                    continue;
348                attch[i].lpszFileName = strdup (*files);        
349                attch[i].lpszPathName = strdup (p);
350                files++;
351                safe_free (p);
352            }
353            msg->lpFiles = attch;
354        }
355    
356        rc = mapi_send_mail (hd, 0, msg, 0, 0);
357        if (rc == MAPI_E_USER_ABORT)
358            rc = SUCCESS_SUCCESS;
359        if (rc != SUCCESS_SUCCESS)
360            MessageBox (NULL, _("Could not sent mail."), "MAPI", MB_ERR);
361    
362        free_recip_tab (recip, n);
363        free_files_tab (attch, nfiles);
364        free_mapi_msg (msg);
365        gpgme_free (p);
366        mapi_logoff (hd, 0, 0, 0);
367    
368        return 0;
369    } /* mapi_send_message */
370    
371    
372    static int
373    add_recipient (gpgme_recipients_t * r_rset, const char * addr)
374    {
375        gpg_keycache_t pub = keycache_get_ctx (1);
376        gpgme_key_t key;
377        gpgme_error_t rc;
378        const char * s;
379    
380        if (!*r_rset)
381            gpgme_recipients_new (&(*r_rset));
382        rc = gpgme_keycache_find_key (pub, addr, 0, &key);
383        if (rc) {
384            log_box (_("Add Recipient"), MB_ERR, _("Could not find key for '%s'"), addr);
385            return -1;
386        }
387        s = key->uids->uid;
388        if (s)
389            gpgme_recipients_add_name (*r_rset, s);
390        return 0;
391    } /* add_recipient */
392    
393    
394    static int
395    add_all_recipients (HWND dlg, int itemid, gpgme_recipients_t * r_rset)
396    {    
397        char buf[1024], * p;
398        int n=0;
399    
400        n = GetDlgItemText (dlg, itemid, buf, sizeof buf-10);
401        if (!n)
402            return -1;
403        p = strtok (buf, ";,");
404        while (p != NULL) {
405            add_recipient (&*r_rset, p);
406            p = strtok (NULL, ";,");
407        }
408        return 0;
409    } /* add_all_recipients */
410    
411    
412    BOOL CALLBACK
413    winpt_mail_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)
414    {
415        static gpgme_recipients_t rset=NULL;
416        char subject[128];
417        char * msgbuf;
418        int n;
419    
420        switch (msg) {
421        case WM_INITDIALOG:
422            center_window (dlg, NULL);
423            SetForegroundWindow (dlg);
424            break;
425    
426        case WM_COMMAND:
427            switch (LOWORD (wparam)) {
428            case IDOK:
429                add_all_recipients (dlg, IDC_PMAIL_TO, &rset);
430                add_all_recipients (dlg, IDC_PMAIL_CC, &rset);
431                if (!gpgme_recipients_count (rset)) {
432                    msg_box (dlg, _("Please enter a recipient."), _("Mail"), MB_ERR);
433                    return FALSE;
434                }
435                n=GetDlgItemText (dlg, IDC_PMAIL_SUBJECT, subject, sizeof subject-1);
436                if (!n)
437                    strcpy (subject, "");
438                n = SendDlgItemMessage (dlg, IDC_PMAIL_MSG, WM_GETTEXTLENGTH, 0, 0);
439                if (!n) {
440                    msg_box (dlg, _("Please enter a message."), _("Mail"), MB_ERR);
441                    return FALSE;
442                }
443                msgbuf = (char * )calloc (1, n+2);
444                if (!msgbuf)
445                    BUG (0);
446                GetDlgItemText (dlg, IDC_PMAIL_MSG, msgbuf, n+1);
447                mapi_send_message (rset, msgbuf, subject, NULL, 0);
448                safe_free (msgbuf);
449                EndDialog (dlg, TRUE);
450                break;
451    
452            case IDCANCEL:
453                EndDialog (dlg, FALSE);
454                break;
455            }
456            break;
457        }
458    
459        return FALSE;
460    } /* winpt_mail_proc */
461    #endif

Legend:
Removed from v.24  
changed lines
  Added in v.48

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26