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

Diff of /trunk/Src/wptFileManager.cpp

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

revision 28 by twoaday, Thu Oct 20 12:35:59 2005 UTC revision 271 by twoaday, Sun Nov 5 08:57:45 2006 UTC
# Line 1  Line 1 
1  /* wptFileManager.cpp - File Manager routines  /* wptFileManager.cpp - File Manager routines
2   *      Copyright (C) 2001-2005 Timo Schulz   *      Copyright (C) 2001-2006 Timo Schulz
3   *      Copyright (C) 2005 g10 Code GmbH   *      Copyright (C) 2005 g10 Code GmbH
4   *   *
5   * This file is part of WinPT.   * This file is part of WinPT.
6   *   *
7   * WinPT is free software; you can redistribute it and/or   * WinPT is free software; you can redistribute it and/or
8   * modify it under the terms of the GNU General Public License   * modify it under the terms of the GNU General Public License
9   * as published by the Free Software Foundation; either version 2   * as published by the Free Software Foundation; either version 2
10   * of the License, or (at your option) any later version.   * of the License, or (at your option) any later version.
11   *     *  
12   * WinPT is distributed in the hope that it will be useful,   * WinPT is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * General Public License for more details.   * General Public License for more details.
16   *   *
17   * You should have received a copy of the GNU General Public License   * You should have received a copy of the GNU General Public License
18   * along with WinPT; if not, write to the Free Software Foundation,   * along with WinPT; if not, write to the Free Software Foundation,
19   * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA   * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20   */   */
21  /* TODO:  
22   *    check_armor_type: we should check the whole file and not only the first line!  #ifdef HAVE_CONFIG_H
23   */  #include <config.h>
24    #endif
25  #include <sys/types.h>  
26  #include <windows.h>  #include <sys/types.h>
27  #include <commdlg.h>  #include <windows.h>
28  #include <io.h>  #include <commdlg.h>
29    #include <io.h>
30  #include "../resource.h"  #include <stdio.h>
31  #include "wptTypes.h"  
32  #include "wptGPG.h"  #include "resource.h"
33  #include "wptAgent.h"  #include "wptTypes.h"
34  #include "wptCommonCtl.h"  #include "wptGPG.h"
35  #include "wptContext.h"  #include "wptAgent.h"
36  #include "wptErrors.h"  #include "wptCommonCtl.h"
37  #include "wptKeylist.h"  #include "wptContext.h"
38  #include "wptFileManager.h"  #include "wptErrors.h"
39  #include "wptNLS.h"  #include "wptKeylist.h"
40  #include "wptW32API.h"  #include "wptFileManager.h"
41  #include "wptVersion.h"  #include "wptNLS.h"
42  #include "wptDlgs.h"  #include "wptW32API.h"
43  #include "wptGPGZIP.h"  #include "wptVersion.h"
44  #include "wptUTF8.h"  #include "wptDlgs.h"
45  #include "wptRegistry.h"  #include "wptZIP.h"
46  #include "wptImport.h"  #include "wptUTF8.h"
47    #include "wptRegistry.h"
48  #include "openpgp.h"  #include "wptImport.h"
49    #include "wptCrypto.h"
50  void progress_cleanup (progress_filter_s *pfx);  #include "wptKeyManager.h"
51    #include "wptKeyserver.h"
52  char* gpg_keylist_to_pattern (gpgme_key_t *rset, int n);  #include "openpgp.h"
53  gpgme_error_t sym_passphrase_cb (void *hook, const char *hint, const char *pass_inf,  
54                                   int prev_was_bad, int fd);  
55    void progress_cleanup (progress_filter_s *pfx);
56  /*-- wptFileVerifyDlg.cpp --*/  BOOL CALLBACK file_secdel_confirm_dlg_proc (HWND dlg, UINT msg,
57  int  file_verify_add_state (file_sig_ctx_t c);                                              WPARAM wparam, LPARAM lparam);
58  void file_verify_use_event (void);  char* gpg_keylist_to_pattern (gpgme_key_t *rset, size_t n);
59  void file_verify_wait (void);  gpgme_error_t sym_passphrase_cb (void *hook, const char *hint, const char *pass_inf,
60                                     int prev_was_bad, int fd);
61  static const char * mm_files[] = {".mov", ".avi", ".mpg", ".mpeg",  void verify_show_signature_state (gpgme_signature_t sig);
62                                    ".mp3", ".wav", ".mid", ".wma",  
63                                    ".gif", ".jpg", ".png", ".jpeg", ".dib", 0};  
64    static const char *mm_files[] = {".mov", ".avi", ".mpg", ".mpeg",
65                                      ".mp3", ".wav", ".mid", ".wma",
66  char *                                    ".gif", ".jpg", ".png", ".jpeg", ".dib", 0};
67  fm_quote_file (const char * name)  
68  {  
69      char * p;  /* Add a new file to the model @fm. */
70      size_t len = strlen (name) + 8;  static void
71    fm_model_add_file (fm_model_t *fm, fm_model_t file)
72      if (*name == '"')  {
73          return m_strdup (name); /* avoid double quotes */      fm_model_t m;
74      p = new char[len + 1];  
75      if (!p)      if (!*fm) {
76          BUG (0);          *fm = file;
77      _snprintf (p, len, "\"%s\"", name);          return;
78        }
79      return p;      for (m = *fm; m->next; m = m->next)
80  } /* fm_quote_file */          ;
81        m->next = file;
82    }
83  int  
84  overwrite_file (const char * fname)  
85  {  /* Search for a file model based on the name @name. */
86      int id;  static fm_model_t
87    fm_model_find_file (fm_model_t fm, const char *name)
88      if (file_exist_check (fname))  {
89          return -1;      fm_model_t m;
90      id = log_box (_("File Manager"), MB_YESNO,  
91                    _("\"%s\" already exists.\n"      for (m=fm; m; m=m->next) {
92                      "Replace existing file?"), fname);          if (!stricmp (m->name, name))
93      return id == IDNO ? 0 : -1;              return m;
94  } /* overwrite_file */      }
95        return NULL;
96    }
97  static void  
98  remove_crit_file_attrs (const char * fname, int force)  
99  {  /* Check if the drive given by @fname is a floppy disc.
100      u32 f_attr;     Return value: -1 for success. */
101      int id;  static int
102    is_floppy_disc (const char *fname)
103      if (file_exist_check (fname))  {
104          return; /* Does not exist */      char drv[32] = {0};    
105                int max = DIM (drv)-1;
106      f_attr = GetFileAttributes (fname);      int i=0;
107      if ((f_attr & FILE_ATTRIBUTE_READONLY) && force)  
108          SetFileAttributes (fname, FILE_ATTRIBUTE_NORMAL);      if (!strstr (fname, ":\\"))
109      else if (f_attr & FILE_ATTRIBUTE_READONLY) {          return 0;
110          id = log_box (_("File Manager"), MB_YESNO,            
111                        _("\"%s\" has read-only attribute.\n"      while (fname && *fname && *fname != '\\' && i < max)
112                          "Set attribute to normal?"), fname);          drv[i++] = *fname++;
113          if (id == IDYES)      drv[i++] = '\\';
114              SetFileAttributes (fname, FILE_ATTRIBUTE_NORMAL);      drv[i++] = '\0';
115      }      i = GetDriveType (drv);
116  } /* remove_crit_file_attrs */      if (i == DRIVE_REMOVABLE)
117            return -1;
118        return 0;
119  static int inline  }
120  is_directory (const char * fname)  
121  {      
122      return GetFileAttributes (fname) & FILE_ATTRIBUTE_DIRECTORY? 1 : 0;  /* Ask the user to overwrite file @fname.
123  } /* is_directory */     Return value: 0 for cancel. */
124    static int
125    overwrite_file (const char *fname)
126  static int inline  {
127  is_openpgp_ext (const char * name)      int id;
128  {  
129      if (strstr (name, ".gpg") || strstr (name, ".asc")      if (file_exist_check (fname))
130          || strstr (name, ".sig") || strstr (name, ".pgp"))          return -1;
131          return -1;      id = log_box (_("File Manager"), MB_YESNO,
132      return 0;                    _("\"%s\" already exists.\n"
133  }                      "Replace existing file?"), fname);
134        return id == IDNO ? 0 : -1;
135    }
136  static int  
137  is_multi_media (const char * name)  
138  {      /* Removes 'critical' attributes from the file @fname.
139      const char * val;     If @force is 1, the user is not asked for permission. */
140      char * p;  void
141      int i;  remove_crit_file_attrs (const char *fname, int force)
142      int ans=0;  {
143        DWORD fattr;
144      i = get_reg_winpt_single (CFG_NOZIP_MMEDIA);      int id = 0;
145      if (i == -1)  
146      {      if (file_exist_check (fname))
147          ans = msg_box (NULL, _("Multi-Media files are already compressed, GPG would compress\n"          return; /* Does not exist */
148                                 "them anyway and this costs a lot of time.\n"          
149                                 "It is possible to disable compression for these files.\n"      fattr = GetFileAttributes (fname);
150                                 "Do you want to disable it?"),      if ((fattr & FILE_ATTRIBUTE_READONLY) && force)
151                                 _("File Manager"), MB_YESNO|MB_INFO);          id = IDYES;
152          set_reg_winpt_single (CFG_NOZIP_MMEDIA, ans == IDYES? 1 : 0);      else if (fattr & FILE_ATTRIBUTE_READONLY)
153          if (ans == IDNO)          id = log_box (_("File Manager"), MB_YESNO,          
154              return 0;                        _("\"%s\" has read-only attribute.\n"
155      }                          "Set attribute to normal?"), fname);
156      else if (i == 0)      if (id == IDYES) {
157          return 0;          if (!SetFileAttributes (fname, FILE_ATTRIBUTE_NORMAL))
158                msg_box (NULL, _("Could not reset file attribute to normal."),
159      p = strrchr (name, '.');                       _("File Manager"), MB_ERR);
160      if (!p)      }
161          return 0;  }
162      for (i=0; (val = mm_files[i]); i++)  
163      {  
164          if (!stricmp (p, val))  /* Return 1 if the given path @fname is a directory, 0 otherwise. */
165              return -1;  static int
166      }  is_directory (const char *fname)
167      return 0;  {
168  }      return GetFileAttributes (fname) & FILE_ATTRIBUTE_DIRECTORY? 1 : 0;
169    }
170    
171  const char*  
172  file_get_extension (gpgme_ctx_t ctx, gpgme_sig_mode_t sigmode)  /* Return -1 if the given name @name is a valid GPG extension. */
173  {  int
174      int use_armor = gpgme_get_armor (ctx);  is_openpgp_ext (const char *name)
175    {
176      if (use_armor || sigmode == GPGME_SIG_MODE_CLEAR)      if (stristr (name, ".gpg") ||
177          return ".asc";          stristr (name, ".asc") ||
178      if (!use_armor && sigmode == GPGME_SIG_MODE_DETACH)          stristr (name, ".sig") ||
179          return ".sig";          stristr (name, ".pgp"))
180      return ".gpg";          return -1;
181  } /* file_get_extension */      return 0;
182    }
183    
184  int  
185  fm_build( listview_ctrl_t *lv, HWND ctrl )  static int
186  {  is_multi_media (const char * name)
187      int i, rc = 0;  {    
188      listview_ctrl_t c;      const char * val;
189      struct listview_column_s col[] =      char * p;
190      {      int i;
191          {0,  80, (char *)_("Status") },      int ans=0;
192          {1, 256, (char *)_("Name") },  
193          {2, 128, (char *)_("Operation") },      i = get_reg_winpt_single (CFG_NOZIP_MMEDIA);
194          {0,   0, NULL }      if (i == -1) {
195      };          ans = msg_box (NULL, _("Multi-Media files are already compressed, GPG would compress\n"
196                                           "them anyway and this takes a lot of time.\n"
197      rc = listview_new( &c );                                 "It is possible to disable compression for these files.\n"
198      if( rc )                                 "Do you want to disable it?"),
199          BUG( NULL );                                 _("File Manager"), MB_YESNO|MB_INFO);
200      c->ctrl = ctrl;          set_reg_winpt_single (CFG_NOZIP_MMEDIA, ans == IDYES? 1 : 0);
201      for ( i = 0; col[i].width; i++ )          if (ans == IDNO)
202          listview_add_column( c, &col[i] );              return 0;
203      listview_set_ext_style( c );      }
204      if( lv )      else if (i == 0)
205          *lv = c;          return 0;
206      return 0;  
207  } /* fm_build */      p = strrchr (name, '.');
208        if (!p)
209            return 0;
210  void      for (i=0; (val = mm_files[i]); i++) {
211  fm_delete( listview_ctrl_t lv )          if (!stricmp (p, val))
212  {              return -1;
213      if( lv ) {      }
214          listview_release( lv );      return 0;
215      }  }
216  } /* fm_delete */  
217    
218    /* Return a GPG file extension which depends on the operation
219  int     mode in @ctx and the sig mode @sigmode. */
220  fm_state_new (fm_state_t * ctx)  const char*
221  {  file_get_extension (gpgme_ctx_t ctx, gpgme_sig_mode_t sigmode)
222      gpgme_error_t rc;  {
223      fm_state_s * c;      int use_armor = gpgme_get_armor (ctx);
224    
225      c = new fm_state_s;      if (use_armor || sigmode == GPGME_SIG_MODE_CLEAR)
226      if (!c)          return ".asc";
227          BUG (0);      if (!use_armor && sigmode == GPGME_SIG_MODE_DETACH)
228      memset (c, 0, sizeof * c);          return ".sig";
229      rc = gpgme_new (&c->ctx);      return reg_prefs.default_ext == 1? ".pgp" : ".gpg";
230      if (rc)  }
231          BUG (0);  
232      /* XXX rc = gpgme_recipients_new (&c->recp);*/  
233      /* XXX gpgme_set_comment (c->ctx, "Generated by WinPT "PGM_VERSION); */  /* Quote a file to avoid shell problems with spaces in the files. */
234      *ctx = c;  char*
235      return 0;  fm_quote_file (const char * name)
236  } /* fm_state_new */  {
237        char * p;
238        size_t len = strlen (name) + 8;
239  /* Release the FM state handle @c. */  
240  void      if (*name == '"')
241  fm_state_release (fm_state_t c)          return m_strdup (name); /* avoid double quotes */
242  {      p = new char[len + 1];
243      if (!c)      if (!p)
244          return;          BUG (0);
245      if (c->recp)      _snprintf (p, len, "\"%s\"", name);
246          free (c->recp);  
247      if (c->ctx) {      return p;
248          gpgme_release (c->ctx);  }
249          c->ctx = NULL;    
250      }  
251      free_if_alloc (c->opaque);  
252      free_if_alloc (c->output);  /* Check the armor type of the file @fname and return
253      delete c; c = NULL;         a string representation of it. */
254  }  static const char*
255    fm_check_armor_type (const char *fname, int *r_type)
256  static int  {
257  fm_check_for_entry( listview_ctrl_t lv, const char *file )      FILE *fp;
258  {      char header[512], *p;
259      char name[512];      
260      int i;      if (r_type)
261            *r_type = PGP_NONE;
262      memset (name, 0, sizeof (name));      fp = fopen (fname, "rb");
263      for( i = 0; i < listview_count_items( lv, 0 ); i++ )      if (!fp)
264      {          return "UNKNOWN";
265          listview_get_item_text( lv, i, 1, name, sizeof (name) - 1 );      p = fgets (header, DIM (header) - 1, fp);
266          if( !strcmp( name, file ) )      fclose (fp);
267              return 1; /* found */            if (!p)
268      }          return "UNKNOWN";
269    
270      return 0;      if (strncmp (header, "-----", 5))
271  } /* fm_check_for_entry */          goto leave;
272        if (strstr (header, "BEGIN PGP PUBLIC KEY" )) {
273            if (r_type) *r_type = PGP_PUBKEY;
274  static int          return "PUBKEY";
275  fm_set_ftype (listview_ctrl_t lv, const char * name)      }
276  {      else if (strstr (header, "BEGIN PGP PRIVATE KEY") ||
277      const char *type;               strstr (header, "BEGIN PGP SECRET KEY")) {
278      int rc;          if (r_type) *r_type = PGP_SECKEY;
279            return "SECKEY";
280      rc = fm_check_for_entry (lv, name);      }
281      if (rc)      else if (strstr (header, "BEGIN PGP MESSAGE")) {
282          return 0;          if (r_type) *r_type = PGP_MESSAGE;
283      type = fm_get_file_type (name);          return "ENCRYPTED";
284      if (!type || !strcmp (type, "UNKNOWN"))      }
285          type = gnupg_check_file_ext (name);          else if (strstr( header, "BEGIN PGP SIGNED MESSAGE")) {
286      rc = listview_add_item (lv, " ");          if (r_type) *r_type = PGP_CLEARSIG;
287      if (rc)          return "SIGNED-CLEAR";
288          return -1;      }
289      listview_add_sub_item (lv, 0, 0, type);      else if (strstr(header, "BEGIN PGP SIGNATURE")) {
290      listview_add_sub_item (lv, 0, 1, name);          if (r_type) *r_type = PGP_SIG;
291      return 0;          return "SIGNED-DETACH";
292  }      }
293    
294    leave:
295  static int      return "UNKNOWN";
296  fm_add_dir_files (listview_ctrl_t lv, char *path)  }
297  {  
298      struct _finddata_t fd;  
299      char * p;  /* Extract file type from @fname. If @r_type is valid,
300      long hd;     it contains the PGP type on success. */
301    static const char*
302      strcat (path, "\\*");  fm_get_file_type (const char *fname, int *r_type)
303      hd = _findfirst (path, &fd);  {        
304      do {      gpg_iobuf_t inp;
305          p = new char [(strlen (path) + strlen (fd.name))+1];      armor_filter_context_t afx;
306          if (!p)      PACKET *pkt;
307              BUG (0);      const char *s = NULL;
308          memcpy (p, path, strlen (path)-1);      size_t count = 0, compr = 0;
309          p[strlen (path)-1] = 0;      int rc = 0;
310          strcat (p, fd.name);  
311          if (!is_directory (p))      if (r_type)
312              fm_set_ftype (lv, p);          *r_type = PGP_NONE;
313          free_if_alloc (p);      if (!fname) {
314                    log_debug ("fm_get_file_type: !fname\r\n");
315      } while (_findnext (hd, &fd) == 0);          return NULL;
316      _findclose (hd);      }
317      return 0;  
318  }      /* to avoid lengthy operations on floppy disks, we use a quick check. */
319        if (is_floppy_disc (fname))
320            return fm_check_armor_type (fname, r_type);
321  /* Add the drag & drop files from @dd_files to the  
322     list view control @lv. */      inp = gpg_iobuf_open (fname);
323  int      if (!inp) {
324  fm_add_dropped_files (listview_ctrl_t lv, HDROP dd_files)          const char *err = winpt_strerror (WPTERR_FILE_OPEN);
325  {          log_box (_("File Manager"), MB_ERR, "\"%s\": %s", fname, err);
326      char name[384+4];          return NULL;
327      int nfiles, rc, i;      }
328            gpg_iobuf_ioctl (inp, 3, 1, NULL); /* disable cache */
329      memset (name, 0, sizeof (name));      if (gpg_iobuf_get_filelength (inp) > 32000000 /* 32MB */
330      nfiles = DragQueryFile (dd_files, 0xFFFFFFFF, NULL, 0);          && !is_openpgp_ext (fname)) {
331      for (i = 0;  i < nfiles; i++) {          gpg_iobuf_close (inp);
332          DragQueryFile (dd_files, i, name, sizeof (name) -1);          return "UNKNOWN";
333          if (is_directory (name))      }
334              rc = fm_add_dir_files (lv, name);  
335          else      if (gpg_use_armor_filter(inp)) {
336              rc = fm_set_ftype (lv, name);          memset (&afx, 0, sizeof (afx));
337          if (rc == -1)          gpg_iobuf_push_filter (inp, gpg_armor_filter, &afx);
338              break;      }
339      }      pkt = (PACKET *)calloc (1, sizeof *pkt);
340      return rc;      if (!pkt)
341  }          BUG (NULL);
342        gpg_init_packet (pkt);
343        /* XXX: for larger files, we could just look for the first ~3 packets
344  int              and stop if we found a packet which were sufficient to idenitfy
345  fm_add_opened_files (listview_ctrl_t lv, HWND dlg)              the file contents. */
346  {      while (!(rc = gpg_parse_packet (inp, pkt))) {
347      OPENFILENAME open;          switch (pkt->pkttype) {
348      const char *type;            case PKT_PUBKEY_ENC:
349      char file[1024] = "";              s = "ENCRYPTED";rc = -2;
350      int rc;              if (r_type) *r_type = PGP_MESSAGE;
351                    break;
352      memset( &open, 0, sizeof (open) );  
353      open.lStructSize = sizeof (OPENFILENAME);          case PKT_SYMKEY_ENC:
354      open.hInstance = glob_hinst;          case PKT_ENCRYPTED:
355      open.lpstrTitle = _("File Open");              s = "SYMKEYENC";rc = -2;
356      open.lpstrFilter = _("All Files (*.*)\0*.*\0\0");              if (r_type) *r_type = PGP_MESSAGE;
357      open.hwndOwner = dlg;              break;
358      open.lpstrFile = file;  
359      open.nMaxFile = sizeof (file) - 1;          case PKT_SIGNATURE:
360      open.Flags = 0;          case PKT_ONEPASS_SIG:
361                    s = "SIGNED";   rc = -2;
362      if (GetOpenFileName (&open)) {              if (r_type) *r_type = PGP_SIG;
363          type = fm_get_file_type (open.lpstrFile);              break;
364          if (!type)  
365              return WPTERR_FILE_OPEN;          case PKT_PUBLIC_KEY:
366          if (!strcmp (type, "UNKNOWN"))              s = "PUBKEY";   rc = -2;
367              type = gnupg_check_file_ext (open.lpstrFile);              if (r_type) *r_type = PGP_PUBKEY;
368          rc = listview_add_item (lv, "");              break;
369          if( !rc ) {  
370              listview_add_sub_item (lv, 0, 0, type);          case PKT_SECRET_KEY:
371              listview_add_sub_item (lv, 0, 1, open.lpstrFile);              s = "SECKEY";   rc = -2;
372          }              if (r_type) *r_type = PGP_SECKEY;
373      }              break;
374        
375      return rc;          case PKT_COMPRESSED:
376  }              /* If we only find 1 packet and it is compressed,
377                   we assume a compressed one-pass signature. */
378                if (count != 0)
379  /* Check the armor type of the file @fname and return                  break;
380     a string representation of it. */              s = "SIGNED"; rc = -2;
381  static const char *              compr = 1;
382  fm_check_armor_type (const char *fname)              break;
383  {  
384      FILE * fp;          default:
385      char header[768], * p;              break;
386                }
387      fp = fopen (fname, "rb");          count++;
388      if (!fp)          gpg_free_packet (pkt);
389          return "UNKNOWN";          gpg_init_packet (pkt);
390      p = fgets (header, sizeof (header) - 1, fp);          if (rc == -2)
391      fclose (fp);              break; /* found */
392      if (!p)      }
393          return "UNKNOWN";      safe_free (pkt);
394        gpg_iobuf_close (inp);
395      if( strncmp( header, "-----", 5 ) )      if (!s)
396          goto leave;          s = fm_check_armor_type (fname, r_type);
397      if( strstr( header, "BEGIN PGP PUBLIC KEY" ) )      if (!s)
398          return "PUBKEY";          s = "UNKNOWN";
399      else if( strstr( header, "BEGIN PGP PRIVATE KEY" ) )      if (!strcmp (s, "SIGNED") && !compr
400          return "SECKEY";          && strcmp (fm_check_armor_type (fname, r_type), "SIGNED-CLEAR")) {
401      else if( strstr( header, "BEGIN PGP SECRET KEY" ) )          if (r_type) *r_type = PGP_SIG;
402          return "SECKEY";          s = "SIGNED-DETACH";
403      else if( strstr( header, "BEGIN PGP MESSAGE" ) )      }
404          return "ENCRYPTED";      return s;
405      else if( strstr( header, "BEGIN PGP SIGNED MESSAGE" ) )  }
406          return "SIGNED-CLEAR";  
407      else if( strstr(header, "BEGIN PGP SIGNATURE" ) )  
408          return "SIGNED-DETACH";  /* Build the File Manager dialog context. */
409    void
410  leave:  fm_build (fm_info_t *r_fm, HWND ctrl)
411      return "UNKNOWN";  {
412  }      
413        fm_info_t fm;
414        struct listview_column_s col[] = {
415  int          {0,  80, (char *)_("Status") },
416  fm_assume_onepass_sig (const char * fname)          {1, 256, (char *)_("Name") },
417  {          {2, 128, (char *)_("Operation") },
418      gpgme_data_t dat;          {0,   0, NULL}  
419      armor_filter_context_t afx;      };
420      gpg_iobuf_t fp;      int i;
421      PACKET * pkt = (PACKET *)calloc (1, sizeof *pkt);  
422      int check = 0;      fm = new fm_info_s;
423        memset (fm, 0, sizeof *fm);
424      if (!fname)      listview_new (&fm->lv, ctrl);
425      {      for (i = 0; col[i].width; i++)
426          gpg_data_new_from_clipboard (&dat, 0);          listview_add_column (fm->lv, &col[i]);
427          gpg_data_release_and_set_file (dat, "gpgme.tmp");      listview_set_ext_style (fm->lv);
428        fm->model = NULL; /*init*/
429          fp = gpg_iobuf_open ("gpgme.tmp");      *r_fm = fm;
430          if (!fp)  }
431              return 0;  
432          gpg_iobuf_ioctl (fp, 3, 1, NULL);  
433          if (gpg_use_armor_filter(fp))  /* Release the file model in @mod. */
434          {  static void
435              memset (&afx, 0, sizeof (afx));  fm_model_release (fm_model_t mod)
436              gpg_iobuf_push_filter (fp, gpg_armor_filter, &afx);  {
437          }      fm_model_t m;
438          gpg_init_packet (pkt);  
439          if (!gpg_parse_packet (fp, pkt)      while (mod) {
440              && pkt->pkttype == PKT_COMPRESSED)          m = mod->next;
441              check = 1;            free_if_alloc (mod->name);
442          gpg_free_packet (pkt);          free_if_alloc (mod->op);
443          safe_free (pkt);          free_if_alloc (mod->status);
444          gpg_iobuf_close (fp);          free_if_alloc (mod);
445          unlink ("gpgme.tmp");          mod = m;
446      }      }
447      /* XXX: implement it for real files */  }
448      return check;  
449  }  
450    /* Reset the File Manager info context @fm. */
451    void
452  static int  fm_reset (fm_info_t fm)
453  is_floppy_disc (const char * fname)  {
454  {      listview_del_all_items (fm->lv);
455      char drv[32] = {0};      fm_model_release (fm->model);
456      int i=0;      fm->model = NULL;
457    }
458      if (!strstr (fname, ":\\"))  
459          return 0;  
460    /* Release the File Manager dialog context @fm. */
461      while (fname && *fname && *fname != '\\')  void
462          drv[i++] = *fname++;  fm_delete (fm_info_t fm)
463      drv[i++] = '\\';  {
464      drv[i++] = '\0';      if (!fm)
465      i = GetDriveType (drv);          return;
466      if (i == DRIVE_REMOVABLE)      if (fm->menu)
467          return -1;          DestroyMenu (fm->menu);
468      return 0;      listview_release (fm->lv);
469  }      fm_model_release (fm->model);
470        free_if_alloc (fm);  
471    }
472  const char *  
473  fm_get_file_type (const char * fname)  
474  {          int
475      gpg_iobuf_t inp;  fm_state_new (fm_state_t * ctx)
476      armor_filter_context_t afx;  {
477      PACKET * pkt = (PACKET *)calloc (1, sizeof *pkt);      fm_state_s *c;
478      int i = 0, rc = 0;  
479      const char * s = NULL;      c = new fm_state_s;
480        if (!c)
481      if (!fname) {          BUG (0);
482          safe_free (pkt);      memset (c, 0, sizeof *c);
483          return NULL;      if (gpgme_new (&c->ctx))
484      }          BUG (0);
485        *ctx = c;
486      if (is_floppy_disc (fname))      return 0;
487          return fm_check_armor_type (fname);  }
488    
489      inp = gpg_iobuf_open (fname);  
490      if (!inp) {  /* Release the FM state handle @c. */
491          const char *s = winpt_strerror (WPTERR_FILE_OPEN);  void
492          log_box( _("File Manager"), MB_ERR, "\"%s\": %s", fname, s );  fm_state_release (fm_state_t c)
493          safe_free( pkt );  {
494          return NULL;      if (!c)
495      }          return;
496      gpg_iobuf_ioctl (inp, 3, 1, NULL); /* disable cache */      if (c->ctx) {
497      if (gpg_iobuf_get_filelength (inp) > 32000000 /* 32MB */          gpgme_release (c->ctx);
498          && !is_openpgp_ext (fname)) {          c->ctx = NULL;  
499          gpg_iobuf_close (inp);      }
500          return "UNKNOWN";      safe_free (c->recp);
501      }      free_if_alloc (c->opaque);
502        free_if_alloc (c->output);
503      if (gpg_use_armor_filter(inp)) {      delete c;
504          memset (&afx, 0, sizeof (afx));  }
505          gpg_iobuf_push_filter (inp, gpg_armor_filter, &afx);  
506      }  
507        /* Check if file @file is already in the list view. */
508      gpg_init_packet (pkt);  static int
509      while (!(rc = gpg_parse_packet (inp, pkt))) {  fm_check_for_entry (listview_ctrl_t lv, const char *file)
510          switch (pkt->pkttype) {  {
511          case PKT_PUBKEY_ENC:  s = "ENCRYPTED";rc = -2; break;      char name[MAX_PATH+128];
512          case PKT_SYMKEY_ENC:      int i;
513          case PKT_ENCRYPTED:   s = "SYMKEYENC";rc = -2; break;  
514          case PKT_SIGNATURE:      memset (name, 0, sizeof (name));
515          case PKT_ONEPASS_SIG: s = "SIGNED";   rc = -2; break;      for (i = 0; i < listview_count_items (lv, 0); i++) {
516          case PKT_PUBLIC_KEY:  s = "PUBKEY";   rc = -2; break;          listview_get_item_text (lv, i, FM_COL_NAME, name, DIM (name) - 1);
517          case PKT_SECRET_KEY:  s = "SECKEY";   rc = -2; break;          if (!stricmp (name, file))
518          }              return 1; /* found */      
519          gpg_free_packet (pkt);      }
520          gpg_init_packet (pkt);  
521          if (rc == -2)      return 0;
522              break; /* found */  }
523      }  
524      safe_free (pkt);  
525      gpg_iobuf_close (inp);  static int
526      if (!s)  fm_set_ftype (listview_ctrl_t lv, fm_model_t *fm, const char *name)
527          s = fm_check_armor_type (fname);  {
528      if (!s)      fm_model_t m;
529          s = "UNKNOWN";      const char *type;
530      if (!strcmp( s, "SIGNED")      int rc;
531          && strcmp (fm_check_armor_type (fname), "SIGNED-CLEAR "))  
532          s = "SIGNED-DETACH";      rc = fm_check_for_entry (lv, name);
533      return s;      if (rc)
534  } /* fm_get_file_type */          return 0; /* already present. */
535        m = new fm_model_s;
536        memset (m, 0, sizeof *m);
537  int      type = fm_get_file_type (name, NULL);
538  fm_get_current_pos (listview_ctrl_t lv)      if (!type || !strcmp (type, "UNKNOWN"))
539  {          type = gnupg_check_file_ext (name, NULL);
540      int i = 0, items;      listview_add_item2 (lv, " ", (void*)m);
541        listview_add_sub_item (lv, 0, FM_COL_STAT, type);
542      items = listview_count_items (lv, 0);      listview_add_sub_item (lv, 0, FM_COL_NAME, name);
543      if (!items)      m->name = m_strdup (name);
544          return -1;      m->status = m_strdup (type);
545      else if (items == 1)      m->op = NULL;
546      {      fm_model_add_file (fm, m);
547          listview_select_one (lv, 0);      return 0;
548          return 0;  }
549      }  
550      else if (items > 1)  
551      {  /* Add all files from the directory @path to the list view @lv. */
552          i = listview_get_curr_pos (lv);  static int
553          if (i == -1)  fm_add_dir_files (listview_ctrl_t lv, fm_model_t *fm, char *path)
554          {  {
555              msg_box (lv->ctrl, _("Please select a file."), _("File Manager"), MB_ERR);      WIN32_FIND_DATA fd;
556              return -1;      HANDLE hd;
557          }      char *p;
558          return i;  
559      }      strcat (path, "\\*");
560        hd = FindFirstFile (path, &fd);
561      return -1;      if (hd == INVALID_HANDLE_VALUE)
562  } /* fm_get_current_pos */          return -1;
563        do {
564            if (fd.cFileName[0] == '.' && strlen (fd.cFileName) < 3)
565  static int              continue;
566  fm_check_detached_sig( listview_ctrl_t lv, int pos )          p = new char [strlen (path) + strlen (fd.cFileName)+1];
567  {          if (!p)
568      char type[128];              BUG (0);
569            memcpy (p, path, strlen (path)-1);
570      listview_get_item_text( lv, pos, 0, type, 127 );          p[strlen (path)-1] = 0;
571      return !strcmp( type, "SIGNED-DETACH" )? 1 : 0;          strcat (p, fd.cFileName);
572  } /* fm_check_detached_sig */          if (!is_directory (p))
573                fm_set_ftype (lv, fm, p);
574            free_if_alloc (p);
575  int      } while (FindNextFile (hd, &fd));
576  fm_check_file_type (listview_ctrl_t lv, int pos, int fm_cmd)      FindClose (hd);
577  {      return 0;
578      char status[128];  }
579      int rc = 0;  
580        
581      listview_get_item_text (lv, pos, 0, status, sizeof (status) - 1);  /* Add the drag & drop files from @dd_files to the
582           list view control @lv. */
583      switch (fm_cmd) {  int
584      case FM_ENCRYPT:  fm_add_dropped_files (fm_info_t fm, HDROP dd_files)
585      case FM_ENCRYPT_DIR:  {
586      case FM_SIGNENCRYPT:        char name[MAX_PATH+32+4];
587          if (strcmp (status, "ENCRYPTED")      int rc = 0;
588              && strcmp (status, "SYMKEYENC"))      UINT i;
589              rc = 1;  
590          break;      for (i = 0;  i < DragQueryFile (dd_files, 0xFFFFFFFF, NULL, 0); i++) {
591                    memset (name, 0, sizeof (name));
592      case FM_DECRYPT:          DragQueryFile (dd_files, i, name, DIM (name) -1);
593          if (!strcmp (status, "DATA")          if (is_directory (name))
594              || !strcmp (status, "ENCRYPTED")              rc = fm_add_dir_files (fm->lv, &fm->model, name);
595              || !strcmp (status, "SYMKEYENC")          else
596              || !strcmp (status, "ARMORED"))              rc = fm_set_ftype (fm->lv, &fm->model, name);
597              rc = 1;          if (rc == -1)
598          break;              break; /* XXX: fixme? */
599                }
600      case FM_SIGN:      DragFinish (dd_files);
601          if( strncmp( status, "SIGNED", 6 ) )      return rc;
602              rc = 1;  }
603          break;  
604            
605      case FM_VERIFY:  /* Add a single file @name to the list view and before
606          if( !strncmp( status, "SIGNED", 6 )     figure out the type of it.
607               || !strcmp( status, "COMPRESSED" ) )     Return value: 0 on success. */
608              rc = 1;  static int
609          break;  add_single_file (listview_ctrl_t lv, fm_model_t *fm, const char *name)
610            {
611      case FM_SYMENC:      fm_model_t m;
612          if( strcmp( status, "SYMKEYENC" ) )      const char *type;
613              rc = 1;      int rc;
614          break;      
615                type = fm_get_file_type (name, NULL);
616      case FM_IMPORT:      if (!type)
617          if( !strcmp( status, "PUBKEY" )          return WPTERR_FILE_OPEN;
618              || !strcmp( status, "SECKEY" ) )      m = new fm_model_s;
619              rc = 1;      memset (m, 0, sizeof *m);
620          break;      if (!strcmp (type, "UNKNOWN"))      
621                    type = gnupg_check_file_ext (name, NULL);
622      case FM_WIPE:      rc = listview_add_item2 (lv, "", (void*)m);
623      case FM_LIST:      if (!rc) {
624          rc = 1;          listview_add_sub_item (lv, 0, FM_COL_STAT, type);
625          break;            listview_add_sub_item (lv, 0, FM_COL_NAME, name);
626      }      }
627            m->status = m_strdup (type);
628      return rc;      m->name = m_strdup (name);
629  } /* fm_check_file_type */      m->op = NULL;
630        fm_model_add_file (fm, m);
631        return rc;
632  static void  }
633  fm_set_status (listview_ctrl_t lv, int pos, int fm_cmd, int success,  
634                 const char * output)  
635  {  /* Use the common Open-File-Dialog to allow the user to
636      char status[128], operat[128];     add one ore more selected files to the listview @lv. */
637      int update = 1;  int
638      const char *s;  fm_add_opened_files (fm_info_t fm, HWND dlg)
639    {
640      if ( fm_cmd == FM_LIST )      OPENFILENAME open;
641          return;      char file[512], name[MAX_PATH+1];
642      success ? s = "SUCCESS" : s = "FAILED";      char *path = NULL;
643      strcpy( operat, s );      const char *s;
644        int i, len=0, n=0;
645      switch (fm_cmd) {      int rc=0;
646      case FM_ENCRYPT:  
647      case FM_ENCRYPT_DIR:      memset (&open, 0, sizeof (open));
648      case FM_SIGNENCRYPT: strcpy( status, "ENCRYPTED" ); break;      open.lStructSize = sizeof (OPENFILENAME);
649      case FM_DECRYPT:     strcpy( status, "UNKNOWN" );   break;      open.hInstance = glob_hinst;
650      case FM_SIGN:        strcpy( status, "SIGNED" );    break;      open.lpstrTitle = _("File Open");
651      case FM_VERIFY:      update = 0;                    break;      open.lpstrFilter = "All Files (*.*)\0*.*\0\0";
652      case FM_SYMENC:      strcpy( status, "SYMKEYENC" ); break;      open.hwndOwner = dlg;
653      case FM_IMPORT:      update = 0;                    break;      open.lpstrFile = file;
654      case FM_WIPE:        strcpy( status, "WIPED" );     break;      open.nMaxFile = DIM (file) - 1;
655      default:             strcpy( status, "UNKNOWN");    break;      open.Flags = OFN_ALLOWMULTISELECT|OFN_EXPLORER ;
656      }      
657        memset (file, 0, sizeof file);
658      if (success) {      if (!GetOpenFileName (&open))
659          if (update) {          return 0;
660              listview_add_sub_item (lv, pos, 0, status);  
661              listview_add_sub_item (lv, pos, 1, output);      /* It is possible that multiple files are returned
662          }         and then they are separated by \0 chars. */
663      }      s = file;
664      listview_add_sub_item( lv, pos, 2, operat );      len = DIM (file)-1;
665  } /* fm_set_status */      for (;;) {
666            if (len < 2 || (*s == '\0' && *(s+1) == '\0'))
667                break;
668  int          memset (name, 0, sizeof (name));
669  fm_clearsign_8bit (listview_ctrl_t lv, fm_state_s *ctx)          for (i=0; len > 0; len--, i++) {
670  {              if (*s == '\0') {
671      FILE *f;                  name[i] = *s++;
672      byte buf[32];                  break;
673      char name[256];              }
674      int i, n, cnt=0;              name[i] = *s++;
675            }
676      if (ctx->sigmode != GPGME_SIG_MODE_CLEAR)          if (n == 0)
677          return 0;              path = m_strdup (name);
678      listview_get_item_text (lv, -1, 1, name, sizeof (name)-1);          else {
679      if (stristr (name, ".TXT"))              char *p = make_filename (path, name, NULL);
680          return 0;              rc = add_single_file (fm->lv, &fm->model, p);
681      f = fopen (name, "rb");              free_if_alloc (p);
682      if (!f)          }
683          return -1; /* should never happen */          n++;
684      n = fread (buf, 1, 32, f);      }
685      for (i = 0; i < n; i++) {      if (n == 1) /* single file selected. */
686          if (buf[i] == 0x00 || buf[i] > 170)          rc = add_single_file (fm->lv, &fm->model, path);
687              cnt++;      free_if_alloc (path);
688      }      return rc;
689      fclose (f);  }
690      if (!cnt)  
691          return 0;  
692      n = -1;  int
693      i = log_box (_("File Manager"), MB_WARN|MB_YESNO,  fm_assume_onepass_sig (const char *fname)
694                   _("\"%s\" does not seems to be a text file.\n"  {    
695                     "Do you really want to clearsign it?"), name);      armor_filter_context_t afx;
696      if (i == IDYES)      gpg_iobuf_t fp;
697          n = 0;      gpgme_data_t dat;
698      return n;      PACKET *pkt;
699  }      char tmpfile[MAX_PATH+1];
700        int check = 0;
701  int  
702  fm_parse_files (listview_ctrl_t lv, HWND dlg, int cmd)      pkt = (PACKET *)calloc (1, sizeof *pkt);
703  {      if (!fname) {
704      struct secdel_confirm_s confirm = {0};          get_temp_name (tmpfile, DIM (tmpfile)-1, "gpgme.tmp");
705      struct progress_filter_s pfx, pfx2;          gpg_data_new_from_clipboard (&dat, 0);
706      fm_state_s * ctx;          gpg_data_release_and_set_file (dat, tmpfile);
707      int fm_cmd, sig_detached = 0;  
708      int rc = 0, i, n, ndel = 0;          fp = gpg_iobuf_open (tmpfile);
709      char fname[512], status[128];          if (!fp)
710                    return 0;
711      switch (cmd) {          gpg_iobuf_ioctl (fp, 3, 1, NULL); /* disable cache. */
712      case ID_FILEMISC_ENCRYPT: fm_cmd = FM_ENCRYPT; break;          if (gpg_use_armor_filter(fp)) {
713      case ID_FILEMISC_DECRYPT: fm_cmd = FM_DECRYPT; break;              memset (&afx, 0, sizeof (afx));
714      case ID_FILEMISC_SYMENC:  fm_cmd = FM_SYMENC;  break;              gpg_iobuf_push_filter (fp, gpg_armor_filter, &afx);
715      case ID_FILEMISC_SIGN:    fm_cmd = FM_SIGN;    break;              }
716      case ID_FILEMISC_VERIFY:  fm_cmd = FM_VERIFY;  break;          gpg_init_packet (pkt);
717      case ID_FILEMISC_IMPORT:  fm_cmd = FM_IMPORT;  break;          if (!gpg_parse_packet (fp, pkt)
718      case ID_FILEMISC_WIPE:    fm_cmd = FM_WIPE;    break;              && pkt->pkttype == PKT_COMPRESSED)
719      case ID_FILEMISC_LIST:    fm_cmd = FM_LIST;    break;              check = 1;  
720      case ID_FILEMISC_SIGNENC: fm_cmd = FM_SIGNENCRYPT; break;          gpg_free_packet (pkt);
721      default: return 1; /* unknown command */          gpg_iobuf_close (fp);
722      }          DeleteFile (tmpfile);
723            }
724      if (fm_get_current_pos (lv) == -1)      /* XXX: implement it for real files */
725          return WPTERR_GENERAL;          safe_free (pkt);
726      rc = fm_state_new (&ctx);      return check;
727      if (rc)  }
728          BUG (0);  
729      ctx->dlg = dlg;  
730    int
731      // XXX: for file operations the progress dialog will be  fm_get_current_pos (listview_ctrl_t lv)
732      //       reloaded somewhere and thus a 'dummy' dialog remains  {
733        int i = 0;
734      /* we use it here to make sure that pfx_cleanup will not use      int items;
735         any weird values. */  
736      memset (&pfx, 0, sizeof (pfx));      items = listview_count_items (lv, 0);
737      ctx->prog_cb = NULL;      if (!items)
738      if (cmd != FM_VERIFY && cmd != FM_SIGN /*&& reg_prefs.fm.progress > 0*/) {          return -1;
739          pfx.hwnd = dlg;      else if (items == 1) {
740          /*gpgme_set_progress_cb (ctx->ctx, progress_callback, &pfx);*/          listview_select_one (lv, 0);
741          /*ctx->prog_cb = &pfx;*/          return 0;
742      }      }
743            else if (items > 1) {
744      /* Commands we need before we can perform the main command */          i = listview_get_curr_pos (lv);
745      switch (fm_cmd) {          if (i == -1) {
746      case FM_ENCRYPT:              msg_box (lv->ctrl, _("Please select a file."),
747      case FM_SIGNENCRYPT:                       _("File Manager"), MB_ERR);
748          if (fm_cmd == FM_SIGNENCRYPT)              return -1;
749              ctx->req_signer = 1;          }
750          DialogBoxParam (glob_hinst, (LPCTSTR)IDD_WINPT_FILE_ENCRYPT, ctx->dlg,          return i;
751                          file_encrypt_dlg_proc, (LPARAM)ctx);      }
752          if (ctx->cancel == 1) {  
753              rc = WPTERR_GENERAL;      return -1;
754              goto leave;  }
755          }  
756          break;  
757            static int
758      case FM_SIGN:  fm_check_detached_sig (listview_ctrl_t lv, int pos)
759          DialogBoxParam (glob_hinst, (LPCSTR)IDD_WINPT_FILE_SIGN, dlg,  {
760                          file_sign_dlg_proc, (LPARAM) ctx);      char type[64];
761          if (ctx->cancel == 1 || fm_clearsign_8bit (lv, ctx)) {  
762              rc = WPTERR_GENERAL;      listview_get_item_text (lv, pos, 0, type, DIM (type)-1);
763              goto leave;      return !strcmp (type, "SIGNED-DETACH")? 1 : 0;
764          }  }
765          break;  
766    
767      case FM_WIPE:  /* Check if the given file on position @pos is useful for
768          memset (&pfx2, 0, sizeof (pfx2));     command @fmd_cmd. For example sign can be used for all files
769          secure_unlink_set_cb (progress_callback, &pfx2);     which are not already signed. */
770          break;  int
771      }  fm_check_file_type (listview_ctrl_t lv, int pos, int fm_cmd)
772        {
773      for( i = 0, n = 0;  i < listview_count_items( lv, 0 ); i++ ) {      char status[64];
774          if( !listview_get_item_state( lv, i ) )      int rc = 0;
775              continue;      
776          listview_get_item_text( lv, i, 0, status, sizeof (status) -1 );      listview_get_item_text (lv, pos, 0, status, DIM (status) - 1);
777          if (!strcmp( status, "ENCRYPTED" ) && fm_cmd == FM_DECRYPT)      
778              n++;      switch (fm_cmd) {
779          if (!strcmp( status, "UNKNOWN" ) && fm_cmd == FM_SIGN)      case FM_ENCRYPT:
780              n++;      case FM_ENCRYPT_DIR:
781          if (fm_cmd == FM_WIPE)      case FM_SIGNENCRYPT:  
782              ndel++;          if (strcmp (status, "ENCRYPTED")
783      }              && strcmp (status, "SYMKEYENC"))
784                    rc = 1;
785      if (n > 1 && fm_cmd != FM_SYMENC)          break;
786          ctx->cache_cb = 1;          
787        case FM_DECRYPT:
788      if (fm_cmd == FM_WIPE && ndel > 0) {          if (!strcmp (status, "DATA") ||
789          memset (&confirm, 0, sizeof confirm);              !strcmp (status, "ENCRYPTED") ||
790          confirm.lv_files = lv;              !strcmp (status, "SYMKEYENC") ||
791          DialogBoxParam (glob_hinst, (LPCTSTR)IDD_WINPT_FILES_SECDEL, ctx->dlg,              !strcmp (status, "ARMORED"))
792                              file_secdel_confirm_dlg_proc, (LPARAM)&confirm);              rc = 1;
793          if (!confirm.yes)          break;
794              goto leave;          
795      }      case FM_SIGN:
796                if (strncmp( status, "SIGNED", 6))
797      for( i = 0; i < listview_count_items( lv, 0 ); i++ ) {              rc = 1;
798          if( !listview_get_item_state( lv, i ) )          break;
799              continue;          
800          listview_get_item_text( lv, i, 1, fname, sizeof (fname) - 1 );      case FM_VERIFY:
801          if( file_exist_check( fname ) && !is_directory( fname ) ) {          if(!strncmp (status, "SIGNED", 6) ||
802              log_box( _("File Manager"), MB_ERR, _("\"%s\" does not exist"), fname );             !strcmp (status, "COMPRESSED"))
803              continue;              rc = 1;
804          }          break;
805          if( is_directory( fname ) )          
806              fm_cmd = FM_ENCRYPT_DIR;              case FM_SYMENC:
807          if( !fm_check_file_type( lv, i, fm_cmd ) )          if (strcmp (status, "SYMKEYENC"))
808              continue;              rc = 1;
809          sig_detached = fm_check_detached_sig( lv, i );          break;
810          switch( fm_cmd ) {          
811          case FM_LIST:        rc = fm_list( fname, dlg );       break;      case FM_IMPORT:
812          case FM_WIPE:        rc = fm_wipe( fname );            break;          if (!strcmp (status, "PUBKEY") ||
813          case FM_ENCRYPT:     rc = fm_encrypt( ctx, fname, 0 ); break;              !strcmp (status, "SECKEY"))
814          case FM_ENCRYPT_DIR: rc = fm_encrypt_directory( ctx, fname ); break;              rc = 1;
815          case FM_SIGNENCRYPT: rc = fm_encrypt( ctx, fname, 1 ); break;          break;
816          case FM_DECRYPT:     rc = fm_decrypt( ctx, fname );    break;          
817          case FM_SIGN:        rc = fm_sign( ctx, fname );       break;      case FM_WIPE:
818          case FM_SYMENC:      rc = fm_sym_encrypt( ctx, fname );break;      case FM_LIST:
819          case FM_VERIFY:      rc = fm_verify (ctx, sig_detached, fname);break;          rc = 1;
820          case FM_IMPORT:          break;  
821              free_if_alloc (ctx->opaque);      }
822              ctx->opaque = m_strdup (fname);      
823              if (!ctx->opaque)      return rc;
824                  BUG (0);  }
825              DialogBoxParam( glob_hinst, (LPCSTR)IDD_WINPT_IMPORT, dlg,  
826                             file_import_dlg_proc, (LPARAM)ctx );  
827              if (ctx->cancel == 1)  /* Set the file status of the given command @fm_cmd.
828                  continue;     @success is 0 on success. */
829              rc = fm_import (ctx, fname);  static void
830              break;  fm_set_status (listview_ctrl_t lv, int pos, int fm_cmd,
831          }                 gpgme_sig_mode_t sigmode, int success, const char *output)
832          fm_set_status (lv, i, fm_cmd, !rc, ctx->output);  {
833          free_if_alloc (ctx->output);      char status[128], operat[128];
834          progress_cleanup (&pfx);      int update = 1;
835      }      const char *s;
836      if (fm_cmd == FM_WIPE) {  
837          secure_unlink_set_cb (NULL, NULL);      if (fm_cmd == FM_LIST)
838          progress_cleanup (&pfx2);          return;
839      }      success ? s = "SUCCESS" : s = "FAILED";
840      if (ctx->cache_cb) {      strcpy (operat, s);
841          release_gpg_passphrase_cb (&ctx->pass_cb);  
842          ctx->cache_cb = 0; /* make sure it's only used for this session! */      switch (fm_cmd) {
843      }      case FM_ENCRYPT:
844            case FM_ENCRYPT_DIR:
845      /* remove wipe files from the list */      case FM_SIGNENCRYPT:
846      n = listview_count_items (lv, 0);          strcpy (status, "ENCRYPTED");
847      while (n--) {          break;
848          char status[128];  
849          listview_get_item_text (lv, n, 0, status, sizeof (status) - 1);      case FM_DECRYPT:
850          if( !strcmp (status, "WIPED"))          strcpy (status, "UNKNOWN");
851              listview_del_item (lv, n);          break;
852      }  
853            case FM_SIGN:
854  leave:          if (sigmode == GPGME_SIG_MODE_DETACH)
855      if (!rc)              strcpy (status, "SIGNED-DETACH");
856          fm_state_release (ctx);          else if (sigmode == GPGME_SIG_MODE_CLEAR)
857      progress_cleanup (&pfx);              strcpy (status, "SIGNED-CLEAR");
858      return rc;          else
859  } /* fm_parse_files */              strcpy (status, "SIGNED");
860            break;
861        case FM_VERIFY:
862  /* Wipe the given file @name with the delete mode          update = 0;
863     from the configuration.          break;
864     Return value: 0 on success. */  
865  int      case FM_SYMENC:
866  fm_wipe (const char *name)          strcpy (status, "SYMKEYENC");
867  {          break;
868      int rc;  
869        case FM_IMPORT:
870      SetCursor (LoadCursor (NULL, IDC_WAIT));          update = 0;                    
871      remove_crit_file_attrs (name, 1);          break;
872      rc = secure_unlink (name, reg_prefs.wipe_mode);  
873      SetCursor (LoadCursor (NULL, IDC_ARROW));      case FM_WIPE:
874      return rc;          strcpy (status, "WIPED");    
875  }          break;
876    
877        default:
878  /* Dump out the given PGP packets from file @name in a dialog. */          strcpy (status, "UNKNOWN");
879  int          break;
880  fm_list (const char *name, HWND dlg)      }
881  {  
882      dialog_box_param( glob_hinst, (LPCTSTR)IDD_WINPT_FILE_STAT, dlg,      if (success && update) {
883                        file_stat_dlg_proc, (LPARAM)name, _("File Status"),          listview_add_sub_item (lv, pos, FM_COL_STAT, status);
884                        IDS_WINPT_FILE_STAT );          listview_add_sub_item (lv, pos, FM_COL_NAME, output);
885      return 0;      }
886  }      listview_add_sub_item (lv, pos, FM_COL_OP, operat);
887    }
888    
889  static int  
890  ask_filename (fm_state_t c, const char *msg, char **dst)  int
891  {  fm_clearsign_8bit (listview_ctrl_t lv, fm_state_s *ctx)
892      const char * s;  {
893        FILE *f;
894      s = get_filename_dlg (c->dlg, FILE_SAVE, msg, NULL, NULL);      BYTE buf[32];
895      if (!s)      char name[MAX_PATH+1];
896          return WPTERR_GENERAL;      int i, n, cnt=0;
897    
898      if (dst != NULL)      if (ctx->sigmode != GPGME_SIG_MODE_CLEAR)
899          free_if_alloc (*dst);          return 0;
900      free_if_alloc (c->output);      listview_get_item_text (lv, -1, FM_COL_NAME, name, DIM (name)-1);
901      c->output = m_strdup (s);      if (stristr (name, ".txt"))
902      if (!c->output)          return 0;
903          BUG (0);      f = fopen (name, "rb");
904      if (dst)      if (!f)
905          *dst = fm_quote_file (s);          return -1; /* should never happen */
906      return 0;      n = fread (buf, 1, 32, f);
907  }      fclose (f);
908        for (i = 0; i < n; i++) {
909            if (buf[i] == 0x00 || buf[i] > 170)
910  int              cnt++;
911  fm_encrypt (fm_state_t c, const char *name, int sign)      }
912  {      if (!cnt)
913      gpgme_error_t err;          return 0;
914      gpgme_key_t key = NULL;      n = -1;
915      gpgme_ctx_t ctx = c->ctx;      i = log_box (_("File Manager"), MB_WARN|MB_YESNO,
916      file_data_t in=NULL, out=NULL;                   _("\"%s\" does not seems to be a text file.\n"
917      char *keyid = NULL, ext[5];                     "Do you really want to clearsign it?"), name);
918      int no_compr = 0;      if (i == IDYES)
919      int rc = 0;          n = 0;
920        return n;
921      c->output = new char[strlen (name) + 5 + 1];  }
922      if (!c->output)  
923          BUG (0);  
924      strcpy (ext, file_get_extension (ctx, c->sigmode));  int
925      strcpy (c->output, name);  fm_parse_files (listview_ctrl_t lv, HWND dlg, int cmd)
926      strcat (c->output, ext);          {
927            struct secdel_confirm_s confirm = {0};
928      if (!overwrite_file (c->output)) {      struct progress_filter_s pfx, pfx2;
929          rc = ask_filename (c, _("Enter filename for encrypted file"), NULL);      fm_state_s * ctx;
930          if (rc)      int fm_cmd, sig_detached = 0;
931              goto leave;      int rc = 0, i, n, ndel = 0;
932      }      char fname[2*MAX_PATH+1], status[128];
933        
934      err = gpg_file_data_new (name, 1, &in);      switch (cmd) {
935      if (err)      case ID_FILEMISC_ENCRYPT: fm_cmd = FM_ENCRYPT; break;
936          goto leave;      case ID_FILEMISC_ENCRYPT_ZIP:fm_cmd = FM_ENCRYPT_ZIP; break;
937      err = gpg_file_data_new (c->output, 0, &out);      case ID_FILEMISC_DECRYPT: fm_cmd = FM_DECRYPT; break;
938      if (err)      case ID_FILEMISC_SYMENC:  fm_cmd = FM_SYMENC;  break;
939          goto leave;      case ID_FILEMISC_SIGN:    fm_cmd = FM_SIGN;    break;    
940        case ID_FILEMISC_VERIFY:  fm_cmd = FM_VERIFY;  break;
941      /*      case ID_FILEMISC_IMPORT:  fm_cmd = FM_IMPORT;  break;
942      if (c->prog_cb) {      case ID_FILEMISC_WIPE:    fm_cmd = FM_WIPE;    break;
943          c->prog_cb->what = name;      case ID_FILEMISC_LIST:    fm_cmd = FM_LIST;    break;
944          gpg_file_data_set_cb (in, c->prog_cb);      case ID_FILEMISC_SIGNENC: fm_cmd = FM_SIGNENCRYPT; break;
945      }      default: return 1; /* unknown command */
946      */      }
947            
948      /* XXX      if (fm_get_current_pos (lv) == -1)
949      no_compr = is_multi_media (name);          return WPTERR_GENERAL;
950      gpgme_control (ctx, GPGME_CTRL_NO_COMPR, no_compr);      rc = fm_state_new (&ctx);
951      */      if (rc)
952            BUG (0);
953      if (sign) {      ctx->dlg = dlg;
954          if (gpgme_signers_enum (ctx, 0) == NULL) {  
955              keyid = get_gnupg_default_key ();      // XXX: for file operations the progress dialog will be
956              if (!keyid) {      //       reloaded somewhere and thus a 'dummy' dialog remains
957                  msg_box (c->dlg, _("Could not get default secret key."),  
958                           _("Signing"), MB_ERR);      /* we use it here to make sure that pfx_cleanup will not use
959                  rc = WPTERR_GENERAL;         any weird values. */
960                  goto leave;      memset (&pfx, 0, sizeof (pfx));
961              }      ctx->prog_cb = NULL;
962              if (get_seckey (keyid, &key))      if (cmd != FM_VERIFY && cmd != FM_SIGN /*&& reg_prefs.fm.progress > 0*/) {
963                  BUG (0);          pfx.hwnd = dlg;
964              gpgme_signers_add (ctx, key);          /*gpgme_set_progress_cb (ctx->ctx, progress_callback, &pfx);*/
965          }          /*ctx->prog_cb = &pfx;*/
966          else {      }
967              gpgme_key_t key = gpgme_signers_enum (ctx, 0);      
968              if (key && key->subkeys) {      /* Commands we need before we can perform the main command */
969                  keyid = m_strdup (key->subkeys->keyid);      switch (fm_cmd) {
970                  if (!keyid)      case FM_ENCRYPT_ZIP:
971                      BUG (NULL);      case FM_ENCRYPT:
972              }      case FM_SIGNENCRYPT:
973          }          if (fm_cmd == FM_SIGNENCRYPT)
974          if (!c->init_cb || !c->cache_cb) {              ctx->req_signer = 1;
975              set_gpg_passphrase_cb (&c->pass_cb, c->ctx, GPG_CMD_SIGN,          DialogBoxParam (glob_hinst, (LPCTSTR)IDD_WINPT_FILE_ENCRYPT,
976                                     c->dlg, _("Signing"));                          ctx->dlg, file_encrypt_dlg_proc, (LPARAM)ctx);
977              c->init_cb = 1;          if (ctx->cancel == 1) {
978          }              rc = WPTERR_GENERAL;
979          err = gpgme_op_encrypt_sign (ctx, c->recp, GPGME_ENCRYPT_ALWAYS_TRUST,              goto leave;
980                                       in->dat, out->dat);          }
981          if (!c->cache_cb)          break;
982              release_gpg_passphrase_cb (&c->pass_cb);          
983          if (c->pass_cb.cancel) {      case FM_SIGN:
984              rc = WPTERR_GENERAL;          DialogBoxParam (glob_hinst, (LPCSTR)IDD_WINPT_FILE_SIGN, dlg,
985              goto leave;                          file_sign_dlg_proc, (LPARAM) ctx);
986          }          if (ctx->cancel == 1 || fm_clearsign_8bit (lv, ctx)) {
987          if (err) {              rc = WPTERR_GENERAL;
988              msg_box (c->dlg, gpgme_strerror (err), _("Sign"), MB_ERR);              goto leave;
989              if (gpgme_err_code (err) == GPG_ERR_BAD_PASSPHRASE)          }
990                  agent_del_cache (keyid);          break;
991              rc = WPTERR_GENERAL;  
992              goto leave;      case FM_WIPE:
993          }          memset (&pfx2, 0, sizeof (pfx2));
994      }          secure_unlink_set_cb (progress_callback, &pfx2);
995      else {          break;
996          err = gpgme_op_encrypt (ctx, c->recp, GPGME_ENCRYPT_ALWAYS_TRUST,      }
997                                  in->dat, out->dat);      
998          if (err) {      for( i = 0, n = 0;  i < listview_count_items (lv, 0); i++ ) {
999              msg_box (c->dlg, gpgme_strerror (err), _("Encrypt"), MB_ERR);          if (!listview_get_item_state (lv, i))
1000              rc = WPTERR_GENERAL;              continue;
1001              goto leave;          listview_get_item_text (lv, i, FM_COL_STAT, status, DIM (status) -1);
1002          }          if (!strcmp (status, "ENCRYPTED") && fm_cmd == FM_DECRYPT)
1003      }              n++;
1004      if (c->wipe)          if (!strcmp (status, "UNKNOWN") && fm_cmd == FM_SIGN)
1005          secure_unlink (name, WIPE_MODE_SIMPLE);              n++;
1006                if (fm_cmd == FM_WIPE)
1007  leave:              ndel++;
1008      if (in)      }
1009          gpg_file_data_release (in);      
1010      if (out)      if (n > 1 && fm_cmd != FM_SYMENC)
1011          gpg_file_data_release (out);          ctx->cache_cb = 1;
1012      free_if_alloc (keyid);  
1013      return rc;      if (fm_cmd == FM_WIPE && ndel > 0) {
1014  }          memset (&confirm, 0, sizeof confirm);
1015            confirm.lv_files = lv;
1016            DialogBoxParam (glob_hinst, (LPCTSTR)IDD_WINPT_FILES_SECDEL, ctx->dlg,
1017  int                          file_secdel_confirm_dlg_proc, (LPARAM)&confirm);
1018  fm_sym_encrypt (fm_state_t c, const char * name)          if (!confirm.yes)
1019  {              goto leave;
1020      gpgme_ctx_t ctx = c->ctx;      }
1021      gpgme_error_t err;      
1022      file_data_t in=NULL, out=NULL;      if (fm_cmd == FM_ENCRYPT_ZIP)
1023      int rc = 0, cancel = 0;          fm_encrypt_into_zip (ctx, lv);
1024      char * src = NULL, * dst = NULL;  
1025      char ext[5], * pass;          for (i = 0; i < listview_count_items (lv, 0); i++) {
1026                if (!listview_get_item_state (lv, i))
1027      pass = request_passphrase2 (_("Symmetric"), 0, &cancel);              continue;
1028      if (cancel)          listview_get_item_text (lv, i, FM_COL_NAME, fname, DIM (fname) - 1);
1029          return 0;          if (file_exist_check (fname) && !is_directory (fname)) {
1030                    log_box (_("File Manager"), MB_ERR,
1031      /* XXX gpgme_control (ctx, GPGME_CTRL_CIPHER, -1);*/                       _("\"%s\" does not exist"), fname);
1032      c->output = new char[strlen (name) + 5 + 1];              continue;
1033      if (!c->output)          }
1034          BUG (0);          if (is_directory (fname))
1035      strcpy (ext, file_get_extension (ctx, c->sigmode));              fm_cmd = FM_ENCRYPT_DIR;        
1036      strcpy (c->output, name);          if (!fm_check_file_type (lv, i, fm_cmd))
1037      strcat (c->output, ext);              continue;
1038            sig_detached = fm_check_detached_sig (lv, i);
1039      if (overwrite_file (c->output) == 0) {          switch (fm_cmd) {
1040          rc = WPTERR_GENERAL;          case FM_LIST:        rc = fm_list( fname, dlg );       break;
1041          goto leave;              case FM_WIPE:        rc = fm_wipe( fname );            break;
1042      }          case FM_ENCRYPT:     rc = fm_encrypt( ctx, fname, 0 ); break;
1043            case FM_ENCRYPT_DIR: rc = fm_encrypt_directory( ctx, fname ); break;
1044      gpgme_set_passphrase_cb (ctx, sym_passphrase_cb, pass);              case FM_SIGNENCRYPT: rc = fm_encrypt( ctx, fname, 1 ); break;
1045            case FM_DECRYPT:     rc = fm_decrypt( ctx, fname );    break;
1046      err = gpg_file_data_new (name, 1, &in);          case FM_SIGN:        rc = fm_sign( ctx, fname );       break;
1047      if (err)          case FM_SYMENC:      rc = fm_sym_encrypt (ctx, fname); break;
1048          goto leave;          case FM_VERIFY:      rc = fm_verify (ctx, sig_detached, fname);break;
1049      err = gpg_file_data_new (c->output, 0, &out);          case FM_IMPORT:
1050      if (err)              free_if_alloc (ctx->opaque);
1051          goto leave;              ctx->opaque = m_strdup (fname);
1052                DialogBoxParam (glob_hinst, (LPCSTR)IDD_WINPT_IMPORT, dlg,
1053      err = gpgme_op_encrypt (ctx, NULL, GPGME_ENCRYPT_ALWAYS_TRUST, in->dat, out->dat);                              file_import_dlg_proc, (LPARAM)ctx);
1054      if (err) {              if (ctx->cancel == 1)
1055          msg_box (c->dlg, gpgme_strerror (err), _("Symmetric"), MB_ERR);                  continue;
1056          rc = WPTERR_GENERAL;              rc = fm_import (ctx, fname);
1057          goto leave;              break;
1058      }          }
1059      if (file_exist_check (c->output)) {          if (ctx->cancel == 1) {
1060          msg_box (c->dlg, _("Encryption failed."), _("Symmetric"), MB_ERR);              ctx->cancel = 0;
1061          rc = WPTERR_GENERAL;              continue;
1062      }          }
1063                fm_set_status (lv, i, fm_cmd, ctx->sigmode, !rc, ctx->output);
1064  leave:          free_if_alloc (ctx->output);
1065      if (in)          progress_cleanup (&pfx);
1066          gpg_file_data_release (in);      }
1067      if (out)  
1068          gpg_file_data_release (out);      if (fm_cmd == FM_WIPE) {
1069      sfree_if_alloc (pass);          secure_unlink_set_cb (NULL, NULL);
1070      return rc;          progress_cleanup (&pfx2);
1071  } /* fm_sym_encrypt */      }
1072        if (ctx->cache_cb) {
1073            release_gpg_passphrase_cb (&ctx->pass_cb);
1074  /* Show the human readable verify result from @sigres. */          ctx->cache_cb = 0; /* make sure it's only used for this session! */
1075  static void      }
1076  show_verify_result (gpgme_verify_result_t sigres)      
1077  {      /* remove wipe files from the list */
1078      gpgme_key_t key=NULL;      n = listview_count_items (lv, 0);
1079      gpgme_signature_t sig=sigres->signatures;      while (n--) {
1080      const char *s, *keyid;          listview_get_item_text (lv, n, FM_COL_STAT, status, DIM (status) - 1);
1081      int sigok = 0;          if (!strcmp (status, "WIPED"))
1082      int type;              listview_del_item (lv, n);
1083      char buf[384];      }
1084        
1085      sig = sigres->signatures;  leave:
1086      sigok = sig->summary & GPGME_SIGSUM_GREEN;      if (!rc)
1087      s = sigok? _("Good signature") : _("BAD signature");          fm_state_release (ctx);
1088      type = sigok? MB_OK: MB_ICONWARNING|MB_OK;      progress_cleanup (&pfx);
1089      keyid = sig->fpr;        return rc;
1090      if (!keyid)  }
1091          return;  
1092    
1093      keyid = strlen (sig->fpr) == 40? sig->fpr+32 : sig->fpr + 24;  /* Wipe the given file @name with the delete mode
1094      get_pubkey (sig->fpr, &key);     from the configuration.
1095      _snprintf (buf, sizeof (buf)-1, "Signature made %s using %s key ID %s\n"     Return value: 0 on success. */
1096                                      "%s from \"%s\"",  int
1097                  strtimestamp (sig->timestamp), get_key_pubalgo (sig->pubkey_algo),  fm_wipe (const char *name)
1098                  keyid, s, key? key->uids->uid : _("user ID not found"));  {
1099      msg_box (NULL, buf, _("Decrypt Verify"), type);      int rc;
1100  }  
1101        SetCursor (LoadCursor (NULL, IDC_WAIT));
1102        remove_crit_file_attrs (name, 1);
1103  /* Check the recipients if we have at least one secret key. */      rc = secure_unlink (name, reg_prefs.wipe_mode);
1104  bool      SetCursor (LoadCursor (NULL, IDC_ARROW));
1105  secret_key_available (gpgme_recipient_t rset)      return rc;
1106  {  }
1107      gpgme_recipient_t r;  
1108      gpgme_key_t key;  
1109    /* Dump out the given PGP packets from file @name in a dialog. */
1110      for (r=rset; r; r = r->next) {        int
1111          if (gpgme_err_code (r->status) == GPG_ERR_NO_SECKEY)  fm_list (const char *name, HWND dlg)
1112              continue;  {
1113          else {      dialog_box_param( glob_hinst, (LPCTSTR)IDD_WINPT_FILE_STAT, dlg,
1114              /* extra check to make sure the key is available right now. */                        file_stat_dlg_proc, (LPARAM)name, _("File Status"),
1115              if (!get_seckey (r->keyid, &key))                        IDS_WINPT_FILE_STAT );
1116                  return true;      return 0;
1117          }  }
1118      }  
1119      return false;  
1120  }  static int
1121    ask_filename (fm_state_t c, const char *msg, char **dst)
1122    {
1123  /* Decrypt the file @name. */      const char *s;
1124  int  
1125  fm_decrypt (fm_state_t c, const char *name)      s = get_filesave_dlg (c->dlg, msg, NULL, NULL);
1126  {      if (!s)
1127      gpgme_error_t err;          return WPTERR_GENERAL;
1128      gpgme_ctx_t ctx = c->ctx;      
1129      gpgme_decrypt_result_t res;      if (dst != NULL)
1130      gpgme_verify_result_t sigres;          free_if_alloc (*dst);
1131      file_data_t in =NULL, out=NULL;      free_if_alloc (c->output);
1132      int is_signed = 0;      c->output = m_strdup (s);
1133      int rc = 0;      if (dst)
1134                *dst = fm_quote_file (s);
1135      if (!c->init_cb || !c->cache_cb) {      return 0;
1136          set_gpg_passphrase_cb (&c->pass_cb, c->ctx, GPG_CMD_DECRYPT,  }
1137                                 c->dlg, _("Decryption"));  
1138          c->init_cb = 1;  
1139      }      int
1140        fm_encrypt (fm_state_t c, const char *name, int sign)
1141      c->output = m_strdup (name);  {
1142      if (!c->output)      gpgme_error_t err;
1143          BUG (0);      gpgme_key_t key = NULL;
1144      if (is_openpgp_ext (c->output))      gpgme_ctx_t ctx = c->ctx;
1145          c->output[strlen (c->output)-4] = '\0';      file_data_t in=NULL, out=NULL;
1146      else {      char *keyid = NULL, ext[5];
1147          const char *s = get_filesave_dlg (c->dlg, _("Choose Filename for Output"),      int rc = 0;
1148                                            NULL, NULL);  
1149          if (s) {      c->output = new char[strlen (name) + 5 + 1];
1150              free_if_alloc (c->output);      if (!c->output)
1151              c->output = m_strdup (s);          BUG (0);
1152              if (!c->output)      strcpy (ext, file_get_extension (ctx, c->sigmode));
1153                  BUG (NULL);      strcpy (c->output, name);
1154          }      strcat (c->output, ext);
1155      }      
1156        if (!overwrite_file (c->output)) {
1157      if (overwrite_file (c->output) == 0) {          rc = ask_filename (c, _("Enter Filename for Encrypted File"), NULL);
1158          rc = ask_filename (c, _("Please enter filename for plaintext file"), NULL);          if (rc)
1159          if (rc)              goto leave;
1160              goto leave;      }
1161      }      
1162        err = gpg_file_data_new (name, F_DATA_READ, &in);
1163      remove_crit_file_attrs (c->output, 0);      if (err)
1164            goto leave;
1165      err = gpg_file_data_new (name, 1, &in);      remove_crit_file_attrs (c->output, 0);
1166      if (err)      err = gpg_file_data_new (c->output, F_DATA_WRITE, &out);
1167          goto leave;      if (err)
1168      err = gpg_file_data_new (c->output, 0, &out);          goto leave;
1169      if (err)  
1170          goto leave;      /*
1171        int no_compr = 0;
1172      err = gpgme_op_decrypt_verify (ctx, in->dat, out->dat);      if (c->prog_cb) {
1173      if (!c->cache_cb)          c->prog_cb->what = name;
1174          release_gpg_passphrase_cb (&c->pass_cb);          gpg_file_data_set_cb (in, c->prog_cb);
1175      if (c->pass_cb.cancel) {      }
1176          rc = WPTERR_GENERAL;      */
1177          goto leave;      
1178      }      /* XXX: disable compression for multi-media files.
1179        no_compr = is_multi_media (name);
1180      res = gpgme_op_decrypt_result (ctx);      gpgme_control (ctx, GPGME_CTRL_NO_COMPR, no_compr);
1181      if (res && res->recipients && !secret_key_available (res->recipients)) {      */
1182          const char *keyid = res->recipients->keyid;  
1183          char *p = get_key_userid (keyid+8);      if (sign) {
1184          gpgme_pubkey_algo_t pkalgo = res->recipients->pubkey_algo;          if (gpgme_signers_enum (ctx, 0) == NULL) {
1185                        keyid = get_gnupg_default_key ();
1186          log_box( _("Decryption"), MB_ERR,              if (!keyid) {
1187                   _("Encrypted with %s key, ID %s.%s\n"                  msg_box (c->dlg, _("Could not get default secret key."),
1188                     "Decryption failed: secret key not available."),                           _("Signing"), MB_ERR);
1189                     get_key_pubalgo (pkalgo), keyid+8, p);                  rc = WPTERR_GENERAL;
1190          rc = WPTERR_GENERAL;                  goto leave;
1191          free_if_alloc (p);              }
1192          goto leave;              if (get_seckey (keyid, &key))
1193      }                  BUG (0);
1194      else if (err) {              gpgme_signers_add (ctx, key);
1195          msg_box (c->dlg, gpgme_strerror (err), _("Decrypt"), MB_ERR);          }
1196          rc = WPTERR_GENERAL;          else {
1197          goto leave;              gpgme_key_t sigkey = gpgme_signers_enum (ctx, 0);
1198      }              if (sigkey && sigkey->subkeys) {
1199      if (file_exist_check (c->output)) {                  keyid = m_strdup (sigkey->subkeys->keyid);
1200          log_box ("Decrypt", MB_ERR, _("Decryption failed.\n%s: does not exist."), c->output);              }
1201          rc = WPTERR_GENERAL;          }
1202      }          if (!c->init_cb || !c->cache_cb) {
1203                    set_gpg_passphrase_cb (&c->pass_cb, c->ctx, GPG_CMD_SIGN,
1204      sigres = gpgme_op_verify_result (ctx);                                     c->dlg, _("Signing"));
1205      if (sigres && sigres->signatures)              c->init_cb = 1;
1206          show_verify_result (sigres);          }
1207            op_begin ();
1208                err = gpgme_op_encrypt_sign (ctx, c->recp, GPGME_ENCRYPT_ALWAYS_TRUST,
1209  leave:                                       in->dat, out->dat);
1210      if (in)          op_end ();
1211          gpg_file_data_release (in);          if (!c->cache_cb)
1212      if (out)              release_gpg_passphrase_cb (&c->pass_cb);
1213          gpg_file_data_release (out);          if (c->pass_cb.cancel) {
1214      return rc;              rc = WPTERR_GENERAL;
1215  }              goto leave;
1216            }
1217            if (err) {
1218  int              msg_box (c->dlg, gpgme_strerror (err), _("Sign"), MB_ERR);
1219  fm_sign (fm_state_t c, const char * name)              if (gpgme_err_code (err) == GPG_ERR_BAD_PASSPHRASE)
1220  {                        agent_del_cache (keyid);
1221      int rc = 0;              rc = WPTERR_GENERAL;
1222      gpgme_ctx_t ctx = c->ctx;              goto leave;
1223      gpgme_error_t err;          }
1224      file_data_t in=NULL, out=NULL;      }
1225      char ext[5];      else {
1226            op_begin ();
1227      if (!c->init_cb || !c->cache_cb) {          err = gpgme_op_encrypt (ctx, c->recp, GPGME_ENCRYPT_ALWAYS_TRUST,
1228          set_gpg_passphrase_cb (&c->pass_cb, c->ctx, GPG_CMD_SIGN, c->dlg, _("Signing") );                                  in->dat, out->dat);
1229          c->init_cb = 1;          op_end ();
1230      }          if (err) {
1231                    msg_box (c->dlg, gpgme_strerror (err), _("Encrypt"), MB_ERR);
1232      free_if_alloc (c->output);              rc = WPTERR_GENERAL;
1233      c->output = new char[strlen (name) + 5 + 1];              goto leave;
1234      if( !c->output)          }
1235          BUG( NULL );      }
1236      strcpy (ext, file_get_extension (ctx, c->sigmode));      
1237      strcpy (c->output, name);  leave:
1238      strcat (c->output, ext);      if (in)
1239                gpg_file_data_release (in);
1240      if (!overwrite_file (c->output)) {      if (out)
1241          rc = ask_filename (c, _("Enter filename for signed file"), NULL);          gpg_file_data_release (out);
1242          if (rc)      free_if_alloc (keyid);
1243              goto leave;      if (!rc && c->wipe)
1244      }          secure_unlink (name, WIPE_MODE_SIMPLE);
1245      remove_crit_file_attrs (c->output, 0);      return rc;
1246    }
1247      err = gpg_file_data_new (name, 1, &in);  
1248      if (err)  
1249          goto leave;  int
1250      err = gpg_file_data_new (c->output, 0, &out);  fm_sym_encrypt (fm_state_t c, const char * name)
1251      if (err)  {
1252          goto leave;      gpgme_ctx_t ctx = c->ctx;
1253        gpgme_error_t err;    
1254      err = gpgme_op_sign (ctx, in->dat, out->dat, c->sigmode);      file_data_t in=NULL, out=NULL;
1255      if( !c->cache_cb )      int rc = 0, cancel = 0;
1256          release_gpg_passphrase_cb (&c->pass_cb);      char ext[5], *pass;    
1257      if( c->pass_cb.cancel ) {      
1258          rc = WPTERR_GENERAL;      pass = request_passphrase2 (_("Symmetric Encryption"), 0, &cancel);
1259          goto leave;      if (cancel) {
1260      }          c->cancel = 1;
1261      if( err ) {          return 0;
1262          msg_box( c->dlg, gpgme_strerror( err ), _("Sign"), MB_ERR );      }
1263          rc = WPTERR_GENERAL;      
1264          goto leave;          /* XXX: a convenient feature could be to select the preferred
1265      }              symmetric algorithm. */
1266        c->output = new char[strlen (name) + 5 + 1];
1267  leave:      if (!c->output)
1268      if (in)          BUG (0);
1269          gpg_file_data_release (in);      strcpy (ext, file_get_extension (ctx, c->sigmode));
1270      if (out)      strcpy (c->output, name);
1271          gpg_file_data_release (out);      strcat (c->output, ext);
1272      return rc;  
1273  }      if (overwrite_file (c->output) == 0) {
1274            rc = WPTERR_GENERAL;
1275            goto leave;    
1276  static int      }
1277  fm_add_sig_stat (file_sig_ctx_t log)  
1278  {      gpgme_set_passphrase_cb (ctx, sym_passphrase_cb, pass);
1279      gpgme_key_t key;          err = gpg_file_data_new (name, 1, &in);
1280      const char *kid;      if (err)
1281      int not_found = 0;          goto leave;
1282        err = gpg_file_data_new (c->output, 0, &out);
1283      kid = log->sig->fpr;      if (err)
1284      if (!kid)          goto leave;
1285          BUG (NULL);  
1286      if (strlen (kid) == 40)      op_begin ();
1287          kid += 32;            err = gpgme_op_encrypt (ctx, NULL, GPGME_ENCRYPT_ALWAYS_TRUST,
1288      else if (strlen (kid) == 32)                              in->dat, out->dat);
1289          kid += 24;      op_end ();
1290      if (get_pubkey (kid, &key))      if (err) {
1291          log->use_uid = 0;          msg_box (c->dlg, gpgme_strerror (err), _("Symmetric"), MB_ERR);
1292      else {          rc = WPTERR_GENERAL;
1293          log->user_id = key->uids->uid;          goto leave;
1294          log->use_uid = 1;      }
1295      }      if (file_exist_check (c->output)) {
1296      file_verify_add_state (log);          msg_box (c->dlg, _("Encryption failed."), _("Symmetric"), MB_ERR);
1297      return 0;          rc = WPTERR_GENERAL;
1298  }      }
1299        
1300    leave:
1301  static int      if (in)
1302  verify_pasted (listview_ctrl_t lv, fm_state_t ctx, const char * dat,          gpg_file_data_release (in);
1303                 int i, HWND dlg)      if (out)
1304  {          gpg_file_data_release (out);
1305      FILE * fp;      sfree_if_alloc (pass);
1306      char stat[32];      return rc;
1307      char file[256], * fname = NULL;  }
1308      int del_end=0;  
1309    
1310      listview_get_item_text (lv, i, 0, stat, sizeof (stat)-1);  /* Check the recipients if we have at least one secret key. */
1311      listview_get_item_text (lv, i, 1, file, sizeof (file)-1);  bool
1312      if (strcmp (stat, "UNKNOWN"))  is_seckey_available (gpgme_recipient_t rset)
1313          return 0;  {
1314      fname = make_filename (NULL, file, "asc");      gpgme_recipient_t r;
1315      if (file_exist_check (fname) != 0) {      winpt_key_s key;
1316          fp = fopen (fname, "wb");  
1317          if (fp == NULL) {      for (r=rset; r; r = r->next) {
1318              log_box (_("File Manager"), MB_ERR, "could not create '%s'", fname);          if (gpgme_err_code (r->status) == GPG_ERR_NO_SECKEY)
1319              free_if_alloc (fname);              continue;
1320              return WPTERR_GENERAL;                else {
1321          }                    memset (&key, 0, sizeof (key));
1322          fwrite (dat, 1, strlen (dat), fp);              /* extra check to make sure the key is available right now. */
1323          fclose (fp);              if (!winpt_get_seckey (r->keyid, &key)) {
1324          del_end = 1;                  winpt_release_pubkey (&key);
1325      }                  return true;
1326      fm_verify (ctx, 1, fname);              }
1327      if (del_end)              winpt_release_pubkey (&key);
1328          unlink (fname);          }
1329      free_if_alloc (fname);      }
1330      return 0;      return false;
1331  }  }
1332    
1333    
1334  int  /* If the decrypt result contains the original file name,
1335  fm_verify_pasted_detsig (listview_ctrl_t lv, HWND dlg)     we use it instead of the artificial "output - .gpg" string. */
1336  {  static int
1337      fm_state_t ctx = NULL;  restore_original_name (const char *output, const char *file_name)
1338      char * dat=NULL;  {
1339      int i, fnd = 0;      char *dir;
1340        char *orig;
1341      dat = get_clip_text (NULL);      int rc = 0;
1342      if (!dat || !strstr (dat, "BEGIN PGP SIGNATURE")) {  
1343          msg_box (dlg, _("Could not find detached signature in the clipboard."),      dir = strrchr (output, '\\');
1344                   _("File Manager"), MB_ERR);      if (!dir)
1345          free_if_alloc (dat);          orig = strdup (file_name);
1346          return WPTERR_GENERAL;      else {
1347      }          orig = (char*)calloc (1, strlen (file_name)+ 1 +
1348      /* XXX find a way to filter out bad signatures or just ignore all in                                   strlen (output)+1);
1349             this case */          if (!orig)
1350      fm_state_new (&ctx);              BUG (0);
1351      if ((i=listview_get_curr_pos (lv)) != -1) {          memcpy (orig, output, (dir-output)+1);
1352          verify_pasted (lv, ctx, dat, i, dlg);          strcat (orig, file_name);
1353          fnd = 1;      }
1354      }      /* XXX: we need to find out if the string needs to be utf8 decoded. */
1355      else {      if (overwrite_file (orig)) {
1356          for (i=0; i < listview_count_items (lv, 0); i++) {          DeleteFile (orig);
1357              verify_pasted (lv, ctx, dat, i, dlg);          if (!MoveFile (output, orig))
1358              fnd = 1;              rc = -1;
1359          }      }
1360      }      safe_free (orig);
1361      if (!fnd)      return rc;
1362          msg_box (dlg, _("No files to check."), _("File Manager"), MB_INFO);  }
1363      free_if_alloc (dat);  
1364      fm_state_release (ctx);  
1365      return 0;  /* Decrypt the file @name. */
1366  }  int
1367    fm_decrypt (fm_state_t c, const char *name)
1368    {
1369  /* Extract automatically the output file name from @name.      gpgme_error_t err;
1370     If @detached is 1, a detached sig is assumed. */      gpgme_ctx_t ctx = c->ctx;    
1371  static int      gpgme_decrypt_result_t res;
1372  get_output_file (fm_state_t c, const char *name, int detached)      gpgme_verify_result_t sigres;
1373  {      file_data_t in = NULL, out = NULL;
1374      const char *file = NULL;      int rc = 0;
1375      const char *title;      
1376      char fname[384];      if (!c->init_cb || !c->cache_cb) {
1377                set_gpg_passphrase_cb (&c->pass_cb, c->ctx, GPG_CMD_DECRYPT,
1378      if (detached)                                 c->dlg, _("Decryption"));
1379          title = _("Select Data File");          c->init_cb = 1;
1380      else      }
1381          title = _("Selected Output File");      
1382        c->output = m_strdup (name);
1383      if (strstr (name, ".sig") || strstr (name, ".asc") || strstr (name, ".gpg")) {      if (is_openpgp_ext (c->output))
1384          _snprintf (fname, sizeof (fname) - 1, "%s", name);          c->output[strlen (c->output)-4] = '\0';
1385          fname[strlen (fname) - 4] = '\0';      else {
1386          if (file_exist_check (fname) == 0 && detached)            const char *s;
1387              file = fname;          s = get_filesave_dlg (c->dlg, _("Choose Filename for Output"),
1388          else if (!detached) {                                NULL, NULL);
1389              /* If the signature is clear or normal, make sure we do not          if (s) {
1390                 overwrite the original file if it exists. */              free_if_alloc (c->output);
1391              if (file_exist_check (fname) == 0 && !overwrite_file (fname)) {              c->output = m_strdup (s);
1392                  file = get_filesave_dlg (c->dlg, title, NULL, NULL);          }
1393                  if (!file)      }
1394                      return WPTERR_GENERAL;  
1395              }      if (overwrite_file (c->output) == 0) {
1396              else          rc = ask_filename (c, _("Enter Filename for Plaintext File"), NULL);
1397                  file = fname;          if (rc)
1398          }              goto leave;
1399      }      }
1400      if (!file)  
1401          file = get_fileopen_dlg (c->dlg, title, NULL, NULL);      /* we fetch all recipients here to make sure they list is complete. */
1402      if (file) {          release_gpg_recipients (&c->pass_cb.recipients);
1403          free_if_alloc (c->output);          gpg_get_recipients (name, &c->pass_cb.recipients);
1404          c->output = m_strdup (file);  
1405          if (!c->output)      err = gpg_file_data_new (name, F_DATA_READ, &in);
1406              BUG (NULL);      if (err)
1407      }          goto leave;
1408      else {      remove_crit_file_attrs (c->output, 0);
1409          msg_box (c->dlg, _("Invalid file name. Exit"), _("Verify"), MB_ERR);      err = gpg_file_data_new (c->output, F_DATA_WRITE, &out);
1410          return WPTERR_GENERAL;        if (err)
1411      }          goto leave;
1412      if (detached)      
1413          c->sigmode = GPGME_SIG_MODE_DETACH;      op_begin ();
1414      else {      err = gpgme_op_decrypt_verify (ctx, in->dat, out->dat);
1415          if (strstr (name, ".asc"))      op_end ();
1416              c->sigmode = GPGME_SIG_MODE_CLEAR;      if (!c->cache_cb)
1417          else          release_gpg_passphrase_cb (&c->pass_cb);
1418              c->sigmode = GPGME_SIG_MODE_NORMAL;      if (c->pass_cb.cancel) {
1419      }          rc = WPTERR_GENERAL;
1420      return 0;          goto leave;
1421  }      }
1422    
1423        res = gpgme_op_decrypt_result (ctx);
1424  /* Verify the signature from the file @name. If @detached 1,      if (res && res->recipients && !is_seckey_available (res->recipients)) {
1425     it is assumed that a detached signature should be checked. */          const char *keyid = get_keyid_from_fpr (res->recipients->keyid);
1426  int          char *p = get_key_userid (keyid);
1427  fm_verify (fm_state_t c, int detached, const char *name)          gpgme_pubkey_algo_t pkalgo = res->recipients->pubkey_algo;
1428  {          
1429      gpgme_ctx_t ctx = c->ctx;          log_box (_("Decryption"), MB_ERR,
1430      gpgme_error_t err;                   _("Encrypted with %s key, ID 0x%s.%s\n"
1431      gpgme_signature_t s;                     "Decryption failed: secret key not available."),
1432      gpgme_verify_result_t res;                     get_key_pubalgo (pkalgo), keyid, p);
1433      struct file_sig_ctx_s log;          rc = WPTERR_GENERAL;
1434      file_data_t in=NULL, out=NULL;          free_if_alloc (p);
1435      int rc = 0;          goto leave;
1436        }
1437      if (strstr (name, ".sig"))      else if (err) {
1438          detached = 1;          msg_box (c->dlg, gpgme_strerror (err), _("Decrypt"), MB_ERR);
1439            rc = WPTERR_GENERAL;
1440      if (get_output_file (c, name, detached))          goto leave;
1441          return WPTERR_GENERAL;      }
1442        if (file_exist_check (c->output)) {
1443      memset (&log, 0, sizeof (log));          log_box ("Decrypt", MB_ERR,
1444      log.file = m_strdup (name);                   _("Decryption failed.\n%s: does not exist."), c->output);
1445      if (!log.file)          rc = WPTERR_GENERAL;
1446          BUG (NULL);          goto leave;
1447      file_verify_create_dlg ();      }
1448        else if (res && res->file_name) {
1449      err = gpg_file_data_new (name, 1, &in);          char *file;
1450      if (err)          int id = IDNO;
1451          goto leave;  
1452      err = gpg_file_data_new (c->output, detached? 1 : 0, &out);          file = strrchr (c->output, '\\');
1453      if (err)          if (!file)
1454          goto leave;              file = c->output;
1455            else
1456      if (c->sigmode == GPGME_SIG_MODE_DETACH)              file++;
1457          err = gpgme_op_verify (ctx, in->dat, out->dat, NULL);          if (strcmp (res->file_name, file))
1458      else              id = log_box (_("Decrypt"), MB_QUEST_ASK,
1459          err = gpgme_op_verify (ctx, in->dat, in->dat, out->dat);                            _("The original file name is '%s'.\n\n"
1460      if (err) {                              "Do you want to use this instead of '%s'?"),
1461          msg_box (c->dlg, gpgme_strerror (err), _("Verify"), MB_ERR);                        res->file_name, file);
1462          rc = WPTERR_GENERAL;          if (id == IDYES) {
1463          goto leave;              /* before we can move the file, it needs to be closed first. */
1464      }                  gpg_file_data_release (out);
1465                out = NULL;
1466      res = gpgme_op_verify_result (ctx);              restore_original_name (c->output, res->file_name);
1467      for (s=res->signatures; s; s=s->next) {          }
1468          log.sig = s;      }
1469          fm_add_sig_stat (&log);      sigres = gpgme_op_verify_result (ctx);
1470      }      if (sigres && sigres->signatures)
1471      if (!c->output)          verify_show_signature_state (sigres->signatures);
1472          c->output = m_strdup (name); /* for later use */      
1473    leave:
1474  leave:      if (in)
1475      if (in)          gpg_file_data_release (in);
1476          gpg_file_data_release (in);      if (out)
1477      if (out)          gpg_file_data_release (out);
1478          gpg_file_data_release (out);  
1479      if (log.file)      return rc;
1480          delete []log.file;  }
1481      return rc;  
1482  }  
1483    int
1484    fm_sign (fm_state_t c, const char * name)
1485  int  {
1486  fm_import (fm_state_t c, const char *name)      gpgme_ctx_t ctx = c->ctx;
1487  {      gpgme_error_t err;
1488      gpgme_ctx_t ctx = c->ctx;      file_data_t in=NULL, out=NULL;
1489      gpgme_error_t err;      char ext[5];
1490      gpgme_import_result_t res;      int rc = 0;
1491      file_data_t keydata = NULL;  
1492      int rc = 0;      if (!c->init_cb || !c->cache_cb) {
1493            set_gpg_passphrase_cb (&c->pass_cb, c->ctx,
1494      free_if_alloc (c->output);                                 GPG_CMD_SIGN, c->dlg, _("Signing"));
1495      c->output = m_strdup (name);          c->init_cb = 1;
1496      if (!c->output)      }
1497          BUG (NULL);      
1498        free_if_alloc (c->output);
1499      err = gpg_file_data_new (name, 1, &keydata);      c->output = new char[strlen (name) + 5 + 1];
1500      if (err)      if (!c->output)
1501          goto leave;          BUG (NULL);
1502        strcpy (ext, file_get_extension (ctx, c->sigmode));
1503      err = gpgme_op_import (ctx, keydata->dat);      strcpy (c->output, name);
1504      if (err) {      strcat (c->output, ext);
1505          msg_box (c->dlg, gpgme_strerror (err), _("Import"), MB_ERR);      
1506          rc = WPTERR_GENERAL;      if (!overwrite_file (c->output)) {
1507          goto leave;          rc = ask_filename (c, _("Enter Filename for Signed File"), NULL);
1508      }          if (rc)
1509                goto leave;
1510      res = gpgme_op_import_result (ctx);      }
1511      print_import_status (res);      
1512      if (res->no_user_id > 0) {      err = gpg_file_data_new (name, F_DATA_READ, &in);
1513          msg_box (c->dlg, _("Key without a self signature was dectected!\n"            if (err)
1514                             "(This key is NOT usable for encryption, etc)\n"          goto leave;
1515                             "\n"      remove_crit_file_attrs (c->output, 0);
1516                             "Cannot import these key(s)!"), _("Import"), MB_INFO);      err = gpg_file_data_new (c->output, F_DATA_WRITE, &out);
1517      }      if (err)
1518            goto leave;
1519  leave:  
1520      if (keydata)      op_begin ();
1521          gpg_file_data_release (keydata);      err = gpgme_op_sign (ctx, in->dat, out->dat, c->sigmode);
1522      return rc;      op_end ();
1523  } /* fm_import */      if (!c->cache_cb)
1524            release_gpg_passphrase_cb (&c->pass_cb);
1525        if (c->pass_cb.cancel) {
1526  /* Export the selected keys from the File Manager to a file. */          rc = WPTERR_GENERAL;
1527  int          goto leave;
1528  fm_export (fm_state_t c)      }
1529  {      if (err) {
1530      int rc = 0, id = 0;          msg_box (c->dlg, gpgme_strerror (err), _("Sign"), MB_ERR);
1531      gpgme_ctx_t ctx = c->ctx;          rc = WPTERR_GENERAL;
1532      gpgme_error_t err;          goto leave;    
1533      gpgme_key_t *rset = c->recp;      }
1534      file_data_t keydata = NULL;  
1535      const char *name, *s = NULL;  leave:
1536      char *p = NULL, *patt = NULL;      if (in)
1537            gpg_file_data_release (in);
1538      if (!rset || !rset[0]) {      if (out)
1539          msg_box (c->dlg, _("No key was selected for export."), _("Export"), MB_ERR);          gpg_file_data_release (out);
1540          rc = WPTERR_GENERAL;      return rc;
1541          goto leave;  }
1542      }  
1543    
1544      if (rset[1] == NULL) { /* count == 1*/  static void
1545          gpgme_key_t k = rset[0];  fm_add_sig_stat (file_sig_ctx_t log)
1546          const char *s = k->uids->name;  {
1547          p = new char[strlen (s)+1+8];      struct winpt_key_s key;
1548          if (!p)      const char *kid;
1549              BUG (NULL);  
1550          strcpy (p, s );      memset (&key, 0, sizeof (key));
1551          strcat (p, ".asc");      kid = get_keyid_from_fpr (log->sig->fpr);
1552      }      log->use_uid = 0;
1553        if (!winpt_get_pubkey (kid, &key)) {
1554      name = get_filename_dlg (c->dlg, FILE_SAVE, _("Choose Name for Key File"),          log->user_id = key.ext->uids->uid;
1555                               NULL, p? p : NULL);          log->use_uid = 1;
1556                                    }
1557      if (!name)      file_verify_add_state (log);
1558          name = "keys.gpg";      winpt_release_pubkey (&key);
1559    }
1560      patt = gpg_keylist_to_pattern (rset, c->n_recp);  
1561    
1562      err = gpg_file_data_new (name, 0, &keydata);  /* Verify a detached signature from the clipboard. */  
1563      if (err)  static int
1564          goto leave;  verify_pasted (listview_ctrl_t lv, fm_state_t ctx,
1565                   const char *dat, int pos, HWND dlg)
1566      err = gpgme_op_export (ctx, patt, 0, keydata->dat);  {
1567      if (err) {      FILE *fp;
1568          msg_box (c->dlg, gpgme_strerror (err), _("Export"), MB_ERR);      char stat[32];
1569          rc = WPTERR_GENERAL;      char file[256], *fname = NULL;
1570          goto leave;          int del_end = 0;
1571      }  
1572      log_box (_("GnuPG status"), MB_OK, _("Finished (Output: %s)"),  name);      listview_get_item_text (lv, pos, FM_COL_STAT, stat, DIM (stat)-1);
1573        listview_get_item_text (lv, pos, FM_COL_NAME, file, DIM (file)-1);
1574  leave:      if (strcmp (stat, "UNKNOWN"))
1575      if (keydata)          return 0;
1576          gpg_file_data_release (keydata);      fname = make_filename (NULL, file, "asc");
1577      if (patt)      if (file_exist_check (fname) != 0) {
1578          free (patt);          fp = fopen (fname, "wb");
1579      free_if_alloc (p);          if (fp == NULL) {
1580                        log_box (_("File Manager"), MB_ERR, "Could not create '%s'", fname);
1581      return rc;              free_if_alloc (fname);
1582  }              return WPTERR_GENERAL;
1583            }
1584            fwrite (dat, 1, strlen (dat), fp);
1585  int          fclose (fp);
1586  fm_parse_command_line (char *cmdl)          del_end = 1;
1587  {      }
1588      fm_state_t ctx;      fm_verify (ctx, 1, fname);
1589      const char *s;      if (del_end)
1590      char *p, *fn = NULL;          DeleteFile (fname);
1591      int count = 0, detached = 0;      free_if_alloc (fname);
1592            return 0;
1593      if( !cmdl || !*cmdl )  }
1594          return 0;  
1595    
1596      fm_state_new( &ctx );      /* Figure out if the clipboard contains a detached signature. */
1597      ctx->dlg = GetActiveWindow( );  int
1598      ctx->cache_cb = 1;          fm_verify_pasted_detsig (listview_ctrl_t lv, HWND dlg)
1599        {
1600      p = cmdl;      fm_state_t ctx = NULL;
1601      if( p && *p > 32 && !stristr( p, "winpt.exe" )      char * dat=NULL;
1602                       && !strstr( p, "--" ) ) {      int i, fnd = 0;
1603          count++;  
1604          if (*p == '"') { /* need to remove quotes */      dat = get_clip_text (NULL);
1605              fn = new char[strlen( p )];      if (!dat || !strstr (dat, "BEGIN PGP SIGNATURE")) {
1606              if (!fn)          msg_box (dlg, _("Could not find detached signature in the clipboard."),
1607                  BUG( NULL );                   _("File Manager"), MB_ERR);
1608              memcpy( fn, p+1, strlen( p ) - 2 );          free_if_alloc (dat);
1609              fn[strlen( p ) -2] = '\0';          return WPTERR_GENERAL;
1610          }      }
1611          else      /* XXX find a way to filter out bad signatures or just ignore all in
1612              fn = m_strdup (p);             this case */
1613          s = fm_get_file_type (fn);      fm_state_new (&ctx);
1614          if (!s || !strcmp (s, "UNKNOWN"))      i = listview_get_curr_pos (lv);
1615              s = gnupg_check_file_ext (fn);      if (i == -1) {
1616          if (*s == 'U') {          verify_pasted (lv, ctx, dat, i, dlg);
1617              log_box( _("File Manager"), MB_ERR, _("%s: no valid OpenPGP data found."), p );          fnd = 1;
1618              return count;      }
1619          }      else {
1620                    for (i=0; i < listview_count_items (lv, 0); i++) {
1621          switch( *s ) {              verify_pasted (lv, ctx, dat, i, dlg);
1622          case 'E': fm_decrypt (ctx, fn); break;              fnd = 1;
1623          case 'P': fm_import (ctx, fn); break;          }
1624          case 'S':      }
1625              if (s[1] == 'Y') {      if (!fnd)
1626                  fm_decrypt (ctx, fn);          msg_box (dlg, _("No files to check."), _("File Manager"), MB_INFO);
1627                  break;      free_if_alloc (dat);
1628              }      fm_state_release (ctx);
1629              file_verify_use_event ();      return 0;
1630              if (s[1] == 'I') {  }
1631                  if (strlen (s) == 13 && s[7] == 'D')  
1632                      detached = 1;  
1633                  fm_verify( ctx, detached, fn );  /* Extract automatically the output file name from @name.
1634              }     If @detached is 1, a detached sig is assumed. */
1635              file_verify_wait( );  static int
1636              break;  get_output_file (fm_state_t c, const char *name, int detached)
1637          }  {
1638      }      const char *file = NULL;
1639        const char *title;
1640      memset( &ctx->pass_cb, 0, sizeof (ctx->pass_cb) );      char fname[384];
1641      safe_free( fn );      
1642      fm_state_release( ctx );      if (detached)
1643      return count;          title = _("Select Data File");
1644  } /* fm_parse_command_line */      else
1645            title = _("Selected Output File");
1646    
1647  const char *      if (is_openpgp_ext (name)) {
1648  default_dirname( const char * name )          _snprintf (fname, DIM (fname) - 1, "%s", name);
1649  {          fname[strlen (fname) - 4] = '\0';
1650      char * p = strrchr( name, '\\' );          if (file_exist_check (fname) == 0 && detached)  
1651      if( !p )              file = fname;
1652          return NULL;          else if (!detached) {
1653      return p+1;              /* If the signature is clear or normal, make sure we do not
1654  } /* default_dirname */                 overwrite the original file if it exists. */
1655                if (file_exist_check (fname) == 0 && !overwrite_file (fname)) {
1656                    file = get_filesave_dlg (c->dlg, title, NULL, NULL);
1657  int                  if (!file)
1658  fm_encrypt_directory( fm_state_t c, const char * name )                      return WPTERR_GENERAL;
1659  {              }
1660      PK_FILE_LIST list = NULL;              else
1661      WIN32_FIND_DATA findbuf;                  file = fname;
1662      HANDLE hd;          }
1663      const char * s;      }
1664      char * patt = NULL, * p;      if (!file)
1665      int rc = 0;          file = get_fileopen_dlg (c->dlg, title, NULL, NULL);
1666            if (file) {    
1667      if( !is_directory( name ) )              free_if_alloc (c->output);    
1668          return -1;          c->output = m_strdup (file);
1669      patt = new char[strlen( name ) + 4];      }
1670      if( !patt )      else {
1671          BUG( NULL );          msg_box (c->dlg, _("Invalid file name. Exit"), _("Verify"), MB_ERR);
1672      strcpy( patt, name );          return WPTERR_GENERAL;  
1673      strcat( patt, "\\*" );      }
1674      hd = FindFirstFile( patt, &findbuf );          if (detached)
1675      if( !hd ) {          c->sigmode = GPGME_SIG_MODE_DETACH;
1676          free_if_alloc( patt );        else {
1677          return -1;                if (stristr (name, ".asc"))
1678      }              c->sigmode = GPGME_SIG_MODE_CLEAR;
1679      if( strcmp( findbuf.cFileName, "." ) && strcmp( findbuf.cFileName, ".." ) ) {          else
1680          p = make_filename( name, findbuf.cFileName, NULL );              c->sigmode = GPGME_SIG_MODE_NORMAL;
1681          pk_list_add( &list, p );      }
1682          free_if_alloc( p );      return 0;
1683      }  }
1684      while( FindNextFile( hd, &findbuf ) ) {  
1685          if( strcmp( findbuf.cFileName, "." ) && strcmp( findbuf.cFileName, ".." ) ) {  
1686              p = make_filename( name, findbuf.cFileName, NULL );  /* Handy function to kick of the gpg verify process. */
1687              pk_list_add( &list, p );  static gpgme_error_t
1688              free_if_alloc( p );  fm_gpg_verify (gpgme_sig_mode_t sigmode, gpgme_ctx_t ctx,
1689          }                 file_data_t in, file_data_t out)
1690      }  {
1691      s = get_filename_dlg( c->dlg, FILE_SAVE, _("Choose a Name for the Archive"),      gpgme_error_t err;
1692                            NULL, default_dirname( name ) );  
1693      if( !s ) {      op_begin ();
1694          msg_box( c->dlg, _("Invalid archive name. Exit."), _("Encrypt Directory"), MB_ERR );      if (sigmode == GPGME_SIG_MODE_DETACH)
1695          rc = -1;          err = gpgme_op_verify (ctx, in->dat, out->dat, NULL);
1696          goto leave;      else
1697      }          err = gpgme_op_verify (ctx, in->dat, in->dat, out->dat);
1698        op_end ();
1699      rc = pk_archiv_create( list, s );      return err;
1700      if( rc )  }
1701          msg_box( c->dlg, _("Could not create zip archive."), _("Encrypt Directory"), MB_ERR );  
1702      else {  
1703          fm_encrypt( c, s, 0 );  /* Verify the signature from the file @name. If @detached 1,
1704          unlink( s );     it is assumed that a detached signature should be checked. */
1705      }  int
1706  leave:  fm_verify (fm_state_t c, int detached, const char *name)
1707      pk_list_free( list );  {
1708      free_if_alloc( patt );      gpgme_error_t err;
1709      return rc;      gpgme_signature_t s;
1710  } /* fm_encrypt_directory */      gpgme_verify_result_t res;
1711        struct file_sig_ctx_s log;
1712        file_data_t in=NULL, out=NULL;
1713  static int CALLBACK      int rc = 0;
1714  fm_cmp_cb( LPARAM first, LPARAM second, LPARAM sortby )  
1715  {      if (stristr (name, ".sig"))
1716      const char * a = 0, * b = 0;          detached = 1;
1717    
1718      switch( (int)sortby ) {      if (get_output_file (c, name, detached))
1719      case FM_SORT_STAT:          return WPTERR_GENERAL;
1720          break;  
1721      case FM_SORT_NAME:      file_verify_create_dlg ();
1722          break;  
1723      case FM_SORT_OP:      memset (&log, 0, sizeof (log));
1724          break;      log.file = m_strdup (name);    
1725      }  
1726      return stricmp( a, b );      err = gpg_file_data_new (name, F_DATA_READ, &in);
1727  } /* fm_cmp_cb */      if (err)
1728                    goto leave;
1729        err = gpg_file_data_new (c->output,
1730  int                               detached? F_DATA_READ : F_DATA_WRITE, &out);
1731  fm_sort( listview_ctrl_t lv, int sortby )      if (err)
1732  {          goto leave;
1733      return listview_sort_items( lv, sortby, fm_cmp_cb );  
1734  } /* fm_sort */      err = fm_gpg_verify (c->sigmode, c->ctx, in, out);
1735        if (err) {
1736            msg_box (c->dlg, gpgme_strerror (err), _("Verify"), MB_ERR);
1737  void          rc = WPTERR_GENERAL;
1738  fm_print_md( listview_ctrl_t lv, HWND dlg, int mdalgo )          goto leave;
1739  {      }    
1740      struct md_file_s mdctx;  
1741        res = gpgme_op_verify_result (c->ctx);
1742      if( listview_count_items( lv, 0 ) == 0 )      if (!res)
1743          return;          goto leave;
1744      memset( &mdctx, 0, sizeof (mdctx) );      if (res->signatures != NULL &&
1745      mdctx.lv = lv;          gpgme_err_code (res->signatures->status) == GPG_ERR_NO_PUBKEY) {
1746      mdctx.mdalgo = mdalgo;          /* We need to call gpg again to get the actual sig state
1747      DialogBoxParam( glob_hinst, (LPCTSTR)IDD_WINPT_FILE_MDSUM, dlg,             and the data files must be rewinded also. */
1748                      mdsum_dlg_proc, (LPARAM)&mdctx );          if (fetch_key_from_keyserver (c->dlg, res->signatures)) {
1749  } /* fm_print_md */              rc = WPTERR_GENERAL;
1750                goto leave;
1751            }
1752  int          gpg_file_data_rewind (in);
1753  fm_send_file (listview_ctrl_t lv)          gpg_file_data_rewind (out);
1754  {          err = fm_gpg_verify (c->sigmode, c->ctx, in, out);
1755      char buf[128];          if (err) {
1756      int rc;              msg_box (c->dlg, gpgme_strerror (err), _("Verify"), MB_ERR);
1757                rc = WPTERR_GENERAL;
1758      rc = listview_get_item_text (lv, -1, 1, buf, sizeof (buf)-1);              goto leave;
1759      if (rc == -1)          }
1760          return 0;          /* Refresh the pointer because a new GPG instance has been
1761      /*mapi_send_ascfile (buf); XXX */             created for the new verify operation. */
1762      return 0;          res = gpgme_op_verify_result (c->ctx);
1763  }      }
1764        
1765        for (s=res->signatures; s; s=s->next) {
1766            log.sig = s;
1767            fm_add_sig_stat (&log);
1768        }
1769        if (!c->output)
1770            c->output = m_strdup (name); /* for later use */
1771    
1772    leave:
1773        if (in)
1774            gpg_file_data_release (in);
1775        if (out)
1776            gpg_file_data_release (out);
1777        free_if_alloc (log.file);
1778        return rc;
1779    }
1780    
1781    
1782    /* Import the keys from the file @name.
1783       Return value: 0 on success. */
1784    int
1785    fm_import (fm_state_t c, const char *name)
1786    {
1787        gpgme_ctx_t ctx = c->ctx;
1788        gpgme_error_t err;
1789        gpgme_import_result_t res;
1790        file_data_t keydata = NULL;
1791        int rc = 0;
1792    
1793        free_if_alloc (c->output);
1794        c->output = m_strdup (name);
1795    
1796        err = gpg_file_data_new (name, F_DATA_READ, &keydata);
1797        if (err)
1798            goto leave;
1799    
1800        op_begin ();
1801        err = gpgme_op_import (ctx, keydata->dat);
1802        op_end ();
1803        if (err) {
1804            msg_box (c->dlg, gpgme_strerror (err), _("Import"), MB_ERR);
1805            rc = WPTERR_GENERAL;
1806            goto leave;
1807        }
1808    
1809        res = gpgme_op_import_result (ctx);
1810        print_import_status (res);
1811        if (res->no_user_id > 0) {
1812            msg_box (c->dlg, _("Key without a self signature was dectected!\n"
1813                               "(This key is NOT usable for encryption, etc)\n"
1814                               "\n"
1815                               "Cannot import these key(s)!"), _("Import"), MB_INFO);
1816        }
1817    
1818    leave:
1819        if (keydata)
1820            gpg_file_data_release (keydata);
1821        return rc;
1822    }
1823    
1824    
1825    /* Export the selected keys from the File Manager to a file. */
1826    int
1827    fm_export (fm_state_t c)
1828    {    
1829        gpgme_ctx_t ctx = c->ctx;
1830        gpgme_error_t err;
1831        gpgme_key_t *rset = c->recp;
1832        file_data_t keydata = NULL;
1833        const char *name;
1834        char *p = NULL, *patt = NULL;
1835        int rc = 0;
1836    
1837        if (!rset || !rset[0]) {
1838            msg_box (c->dlg, _("No key was selected for export."),
1839                     _("Export"), MB_ERR);
1840            rc = WPTERR_GENERAL;
1841            goto leave;
1842        }
1843    
1844        if (rset[1] == NULL) /* count == 1*/
1845            p = km_gen_export_filename (rset[0]->subkeys->keyid+8, 0);
1846    
1847        name = get_filesave_dlg (c->dlg, _("Choose Name for Key File"),
1848                                 NULL, p? p : NULL);
1849        if (!name)
1850            name = reg_prefs.default_ext? "keys.pgp" : "keys.gpg";
1851    
1852        patt = gpg_keylist_to_pattern (rset, c->n_recp);
1853    
1854        err = gpg_file_data_new (name, F_DATA_WRITE, &keydata);
1855        if (err)
1856            goto leave;
1857    
1858        op_begin ();
1859        err = gpgme_op_export (ctx, patt, 0, keydata->dat);
1860        op_end ();
1861        if (err) {
1862            msg_box (c->dlg, gpgme_strerror (err), _("Export"), MB_ERR);
1863            rc = WPTERR_GENERAL;
1864            goto leave;    
1865        }
1866        log_box (_("GnuPG status"), MB_OK, _("Finished (Output: %s)"),  name);
1867    
1868    leave:
1869        if (keydata)
1870            gpg_file_data_release (keydata);
1871        safe_free (patt);
1872        free_if_alloc (p);
1873        return rc;
1874    }
1875    
1876    
1877    /* Parse the command line and process the given file. */  
1878    int
1879    fm_parse_command_line (char *cmdl)
1880    {
1881        fm_state_t ctx;
1882        const char *s;
1883        char *p, *fn = NULL;
1884        int count = 0, detached = 0;
1885        int type;
1886        
1887        if (!cmdl || !*cmdl)
1888            return 0;
1889    
1890        fm_state_new (&ctx);    
1891        ctx->dlg = GetActiveWindow ();
1892        ctx->cache_cb = 1;
1893        
1894        p = cmdl;
1895        if (p && *p > 32 && !stristr (p, "winpt.exe")
1896                         && !strstr (p, "--" )) {
1897            count++;
1898            if (*p == '"') { /* need to remove quotes */
1899                fn = new char[strlen (p)];
1900                if (!fn)
1901                    BUG (NULL);
1902                memcpy (fn, p+1, strlen (p) - 2);
1903                fn[strlen (p) -2] = '\0';
1904            }
1905            else
1906                fn = m_strdup (p);
1907            s = fm_get_file_type (fn, &type);
1908            if (!s || !strcmp (s, "UNKNOWN"))
1909                s = gnupg_check_file_ext (fn, &type);
1910            if (type == PGP_NONE) {
1911                log_box (_("File Manager"), MB_ERR,
1912                         _("%s: no valid OpenPGP data found."), p);
1913                free_if_alloc (fn);
1914                return count;
1915            }
1916            switch (type) {
1917            case PGP_MESSAGE:
1918                fm_decrypt (ctx, fn);
1919                break;
1920    
1921            case PGP_PUBKEY:
1922            case PGP_SECKEY:
1923                fm_import (ctx, fn);
1924                break;
1925    
1926            case PGP_SIG:
1927            case PGP_CLEARSIG:
1928                if (type == PGP_SIG)    
1929                    detached = 1;
1930                fm_verify (ctx, detached, fn);
1931                file_verify_wait ();
1932                break;
1933    
1934            default:
1935                break;
1936            }
1937        }
1938    
1939        wipememory (&ctx->pass_cb, sizeof (ctx->pass_cb));
1940        free_if_alloc (fn);
1941        fm_state_release (ctx);
1942        return count;
1943    }
1944    
1945    
1946    /* Extract the last folder name from @name. */
1947    const char*
1948    default_dirname (const char *name)
1949    {
1950        char *p = strrchr (name, '\\');
1951        if (!p)
1952            return NULL;
1953        return p+1;
1954    }
1955    
1956    
1957    /* Store all selected files from @lv in a zip archive
1958       and encrypt the zip archive then.
1959       Return value: 0 on success. */
1960    int
1961    fm_encrypt_into_zip (fm_state_t ctx, listview_ctrl_t lv)
1962    {
1963        PK_FILE_LIST list=NULL;
1964        const char *outfile, *ext;
1965        char *out_enc;
1966        int nitems;
1967        int i, idx = -1;
1968        int rc;
1969    
1970        nitems = listview_count_items (lv, 0);
1971        if (!nitems) {
1972            msg_box (NULL, _("Encrypting into a ZIP archive makes sense with multiple files"),
1973                     _("File Manager"), MB_ERR);
1974            return WPTERR_GENERAL;
1975        }
1976    
1977        outfile = get_filesave_dlg (ctx->dlg, _("Choose File Name for Output"),
1978                                    NULL, "Encrypted_Files.zip");
1979        if (!outfile)
1980            return WPTERR_GENERAL;
1981    
1982        for (i=0; i < nitems; i++) {
1983            char name[MAX_PATH+32];
1984    
1985            if (!listview_get_item_state (lv, i))
1986                continue;
1987            if (idx == -1)
1988                idx = i;
1989            listview_get_item_text (lv, i, 1, name, DIM (name)-1);
1990            pk_list_add (&list, name);
1991        }    
1992    
1993        pk_archiv_create (list, outfile);
1994        pk_list_free (list);
1995    
1996        rc = fm_encrypt (ctx, outfile, 0);
1997        DeleteFile (outfile);
1998        if (rc)
1999            return rc;
2000    
2001        ext = file_get_extension (ctx->ctx, ctx->sigmode)+1;
2002        out_enc = make_filename (NULL, outfile, ext);
2003        fm_set_status (lv, idx, FM_ENCRYPT, (gpgme_sig_mode_t)0, 1, out_enc);
2004        free_if_alloc (out_enc);
2005    
2006        for (i=nitems; i > -1; i--) {
2007            if (i != idx && listview_get_item_state (lv, i))
2008                listview_del_item (lv, i);
2009        }
2010        return 0;
2011    }
2012    
2013    
2014    int
2015    fm_encrypt_directory (fm_state_t c, const char *name)
2016    {    
2017        WIN32_FIND_DATA findbuf;
2018        HANDLE hd;
2019        PK_FILE_LIST list = NULL;
2020        const char *s;
2021        char *patt = NULL, *p;
2022        int rc = 0;
2023        
2024        if (!is_directory (name))
2025            return -1;
2026        patt = new char[strlen (name) + 4];
2027        if (!patt)
2028            BUG (NULL);
2029        strcpy (patt, name);
2030        strcat (patt, "\\*");
2031        hd = FindFirstFile (patt, &findbuf);
2032        if (!hd) {
2033            free_if_alloc (patt);
2034            return WPTERR_GENERAL;
2035        }
2036        if (strcmp (findbuf.cFileName, ".") && strcmp (findbuf.cFileName, "..")) {
2037            p = make_filename (name, findbuf.cFileName, NULL);
2038            pk_list_add (&list, p);
2039            free_if_alloc (p);
2040        }
2041        /* XXX: support to scan sub directories also. */
2042        while (FindNextFile (hd, &findbuf)) {
2043            if (strcmp (findbuf.cFileName, ".") && strcmp (findbuf.cFileName, "..")) {
2044                p = make_filename (name, findbuf.cFileName, NULL);
2045                pk_list_add (&list, p);
2046                free_if_alloc (p);
2047            }
2048        }
2049        s = get_filesave_dlg (c->dlg, _("Choose a Name for the Archive"),
2050                              NULL, default_dirname (name));
2051        if (!s) {
2052            msg_box (c->dlg, _("Invalid archive name. Exit."),
2053                     _("Encrypt Directory"), MB_ERR);
2054            rc = -1;
2055            goto leave;
2056        }
2057    
2058        rc = pk_archiv_create (list, s);
2059        if (rc)
2060            msg_box (c->dlg, _("Could not create zip archive."),
2061                     _("Encrypt Directory"), MB_ERR);
2062        else {
2063            fm_encrypt (c, s, 0);
2064            DeleteFile (s);
2065        }
2066    leave:
2067        FindClose (hd);
2068        pk_list_free (list);
2069        free_if_alloc (patt);
2070        return rc;
2071    }
2072    
2073    
2074    static int CALLBACK
2075    fm_cmp_cb (LPARAM first, LPARAM second, LPARAM sortby)
2076    {
2077        fm_model_t a, b;
2078        int cmpres = 0;
2079    
2080        a = (fm_model_t)first;
2081        b = (fm_model_t)second;
2082        if (!a || !b)
2083            return 0;
2084    
2085        switch ((int)sortby) {
2086        case FM_COL_STAT:
2087            cmpres = stricmp (a->status, b->status);
2088            break;
2089    
2090        case FM_COL_NAME:
2091            cmpres = stricmp (a->name, b->name);
2092            break;
2093    
2094        case FM_COL_OP:
2095            if (a->op && b->op)
2096                cmpres = stricmp (a->op, b->op);
2097            break;
2098        }
2099        return cmpres;
2100    }
2101    
2102    
2103    /* Sort the list items from @lv with the mode given by @sortby. */
2104    int
2105    fm_sort (listview_ctrl_t lv, int sortby)
2106    {
2107        return ListView_SortItems (lv->ctrl, fm_cmp_cb, (LPARAM)sortby);
2108    }
2109    
2110    
2111    /* Start the 'print md' dialog. Pass over the listview control
2112       @lv and the digest algo @mdalgo. */
2113    void
2114    fm_print_md (listview_ctrl_t lv, HWND dlg, int mdalgo)
2115    {
2116        struct md_file_s mdctx;
2117    
2118        if (listview_count_items (lv, 0) == 0)
2119            return;
2120        memset (&mdctx, 0, sizeof (mdctx));
2121        mdctx.lv = lv;
2122        mdctx.mdalgo = mdalgo;
2123        DialogBoxParam (glob_hinst, (LPCTSTR)IDD_WINPT_FILE_MDSUM, dlg,
2124                        mdsum_dlg_proc, (LPARAM)&mdctx);
2125    }
2126    
2127    
2128    /* Send the selected file in @lv via MAPI to a mail recipient. */
2129    int
2130    fm_send_file (listview_ctrl_t lv)
2131    {
2132        char name[MAX_PATH+1];
2133        int rc;
2134    
2135        rc = listview_get_item_text (lv, -1, FM_COL_NAME, name, DIM (name)-1);
2136        if (rc != -1)
2137            mapi_send_ascfile (name);
2138        return 0;
2139    }

Legend:
Removed from v.28  
changed lines
  Added in v.271

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26