This file is indexed.

/usr/share/emacs/site-lisp/mailcrypt/mc-toplev.el is in mailcrypt 3.5.9-7.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
;; mc-toplev.el, entry point functions for Mailcrypt
;; Copyright (C) 1995  Jin Choi <jsc@mit.edu>
;;                     Patrick LoPresti <patl@lcs.mit.edu>

;;{{{ Licensing
;; This file is intended to be used with GNU Emacs.

;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.

;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; see the file COPYING.  If not, write to
;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
;;}}}
;;{{{ Load some required packages
(require 'mailcrypt)
(require 'mail-utils)
(require 'rfc822)

(eval-when-compile
  ;; RMAIL
  (condition-case nil (require 'rmail) (error nil))
  (autoload 'rmail-abort-edit "rmailedit")
  (autoload 'rmail-cease-edit "rmailedit")
  ;; Is this a good idea?
  (defvar rmail-buffer nil)

  ;; VM
  (condition-case nil (require 'vm) (error nil))

  ;; GNUS
  (condition-case nil (require 'gnus) (error nil))
  (autoload 'gnus-summary-select-article "gnus-sum")
  (autoload 'gnus-summary-edit-article "gnus-sum")
  (autoload 'gnus-article-edit-exit "gnus-art")
  (autoload 'gnus-article-edit-done "gnus-art")

  ;; MH-E
  (condition-case nil (require 'mh-e) (error nil))

  ;; Mew
  (condition-case nil (require 'mew) (error nil))
  (autoload 'mew-summary-display "mew-summary")
  (autoload 'mew-buffer-message "mew")

)

(eval-and-compile
  (condition-case nil (require 'mailalias) (error nil)))

(autoload 'mc-scheme-pgp   "mc-pgp"  nil t)
(autoload 'mc-scheme-pgp50 "mc-pgp5" nil t)
(autoload 'mc-scheme-pgp65 "mc-pgp6" nil t)
(autoload 'mc-scheme-gpg   "mc-gpg"  nil t)

(autoload 'mc-remailer-scheme-type1 "mc-remail" nil t)
(autoload 'mc-remailer-scheme-mixmaster "mc-remail2" nil t)
(autoload 'mc-remailer-scheme-mixminion "mc-remail2" nil t)

;;}}}

;;{{{ Encryption

(defun mc-cleanup-recipient-headers (str)
  ;; Takes a comma separated string of recipients to encrypt for and,
  ;; assuming they were possibly extracted from the headers of a reply,
  ;; returns a list of the address components.
  (mapcar 'mc-strip-address
          (rfc822-addresses str)))

(defun mc-find-headers-end ()
  (save-excursion
    (goto-char (point-min))
    (re-search-forward
     (concat "^" (regexp-quote mail-header-separator) "\n"))
    (if (looking-at "^::\n")
        (re-search-forward "^\n" nil t))
    (if (looking-at "^##\n")
        (re-search-forward "^\n" nil t))
    (point-marker)))

;;;###autoload
(defun mc-encrypt (arg)
  "*Encrypt the current buffer.

Exact behavior depends on current major mode.

With \\[universal-argument], prompt for User ID to sign as.

With \\[universal-argument] \\[universal-argument], prompt for encryption scheme to use."
  (interactive "p")
  (mc-encrypt-region arg nil nil))

;;;###autoload
(defun mc-encrypt-region (arg start end)
  "*Encrypt the current region."
  (interactive "p\nr")
  (let* ((mode-alist (cdr-safe (assq major-mode mc-modes-alist)))
         (func (or (cdr-safe (assq 'encrypt mode-alist))
                   'mc-encrypt-generic))
         sign scheme from)
    (if (>= arg 4)
        (setq from (read-string "User ID: ")
              sign t))
    (if (>= arg 16)
        (setq scheme
              (cdr (assoc
                    (completing-read "Encryption Scheme: " mc-schemes)
                    mc-schemes))))
    (funcall func nil scheme start end from sign)))

(defun mc-encrypt-generic (&optional recipients scheme start end from sign)
  "*Generic function to encrypt a region of data."
  (save-excursion
    (or start (setq start (point-min-marker)))
    (or (markerp start) (setq start (copy-marker start)))
    (or end (setq end (point-max-marker)))
    (or (markerp end) (setq end (copy-marker end)))
    (run-hooks 'mc-pre-encryption-hook)
    (cond ((stringp recipients)
           (setq recipients
                 (mc-split "\\([ \t\n]*,[ \t\n]*\\)+" recipients)))
          ((null recipients)
           (setq recipients
                 (mc-cleanup-recipient-headers (read-string "Recipients: "))))
          (t (error "mc-encrypt-generic: recipients not string or nil")))
    (or scheme (setq scheme mc-default-scheme))
    (if (funcall (cdr (assoc 'encryption-func (funcall scheme)))
                 recipients start end from sign)
        (progn
          (run-hooks 'mc-post-encryption-hook)
          t))))

(defun mc-encrypt-message (&optional recipients scheme start end from sign)
  "*Encrypt a message for RECIPIENTS using the given encryption SCHEME.
RECIPIENTS is a comma separated string. If SCHEME is nil, use the value
of `mc-default-scheme'.  Returns t on success, nil otherwise."
  (save-excursion
    (let ((headers-end (mc-find-headers-end))
          default-recipients)

      (setq default-recipients
            (save-restriction
              (goto-char (point-min))
              (re-search-forward
               (concat "^" (regexp-quote mail-header-separator) "$"))
              (narrow-to-region (point-min) (point))
              (and (featurep 'mailalias)
                   (not (featurep 'mail-abbrevs))
                   mail-aliases
                   (expand-mail-aliases (point-min) (point-max)))
              (mc-strip-addresses
               (mapcar 'cdr
                       (mc-get-fields "to\\|cc\\|bcc")))))

      (if (not from)
          (save-restriction
            (goto-char (point-min))
            (re-search-forward
             (concat "^" (regexp-quote mail-header-separator) "\n"))
            (narrow-to-region (point) headers-end)
            (setq from (mail-fetch-field "From"))))

      (if (not recipients)
          (setq recipients
                (if mc-use-default-recipients
                    default-recipients
                  (read-from-minibuffer "Recipients: " default-recipients))))

      (or start (setq start headers-end))
      (or end (setq end (point-max-marker)))

      (mc-encrypt-generic recipients scheme start end from sign))))


;;}}}
;;{{{ Decryption

;;;###autoload
(defun mc-decrypt ()
  "*Decrypt a message in the current buffer.

Exact behavior depends on current major mode."
  (interactive)
  (let* ((mode-alist (cdr-safe (assq major-mode mc-modes-alist)))
         (func (or (cdr-safe (assq 'decrypt mode-alist))
                   'mc-decrypt-message)))
    (funcall func)))

(defun mc-decrypt-message ()
  "Decrypt whatever message is in the current buffer.
Returns a pair (SUCCEEDED . VERIFIED) where SUCCEEDED is t if the encryption
succeeded and VERIFIED is t if it had a valid signature."
  (save-excursion
    (let ((schemes mc-schemes)
          limits
          (scheme mc-default-scheme))

      ; Attempt to find a message signed according to the default
      ; scheme.
      (if mc-default-scheme
          (setq
           limits
           (mc-message-delimiter-positions
            (cdr (assoc 'msg-begin-line (funcall mc-default-scheme)))
            (cdr (assoc 'msg-end-line (funcall mc-default-scheme))))))

      ; We can't find a message signed in the default scheme.
      ; Step through all the schemes we know, trying to identify
      ; the applicable one by examining headers.
      (while (and (null limits)
                  schemes
                  (setq scheme (cdr (car schemes)))
                  (not (setq
                        limits
                        (mc-message-delimiter-positions
                         (cdr (assoc 'msg-begin-line (funcall scheme)))
                         (cdr (assoc 'msg-end-line (funcall scheme)))))))
        (setq schemes (cdr schemes)))

      (if (null limits)
          (error "Found no encrypted message in this buffer.")
        (run-hooks 'mc-pre-decryption-hook)
        (let ((resultval (funcall (cdr (assoc 'decryption-func
                                              (funcall scheme)))
                                  (car limits) (cdr limits))))
          (goto-char (point-min))
          (if (car resultval) ; decryption succeeded
              (run-hooks 'mc-post-decryption-hook))
          resultval)))))
;;}}}
;;{{{ Signing
;;;###autoload
(defun mc-sign (arg)
  "*Sign a message in the current buffer.

Exact behavior depends on current major mode.

With one prefix arg, prompts for private key to use, with two prefix args,
also prompts for encryption scheme to use.  With negative prefix arg,
inhibits clearsigning (pgp)."
  (interactive "p")
  (mc-sign-region arg nil nil))

;;;###autoload
(defun mc-sign-region (arg start end)
  "*Sign the current region."
  (interactive "p\nr")
  (let* ((mode-alist (cdr-safe (assq major-mode mc-modes-alist)))
         (func (or (cdr-safe (assq 'sign mode-alist))
                   'mc-sign-generic))
         from scheme)
    (if (>= arg 16)
        (setq scheme
              (cdr (assoc
                    (completing-read "Encryption Scheme: " mc-schemes)
                    mc-schemes))))
    (if (>= arg 4)
        (setq from (read-string "User ID: ")))

    (funcall func from scheme start end (< arg 0))))

(defun mc-sign-generic (withkey scheme start end unclearsig)
  (or scheme (setq scheme mc-default-scheme))
  (or start (setq start (point-min-marker)))
  (or (markerp start) (setq start (copy-marker start)))
  (or end (setq end (point-max-marker)))
  (or (markerp end) (setq end (copy-marker end)))
  (run-hooks 'mc-pre-signature-hook)
  (if (funcall (cdr (assoc 'signing-func (funcall scheme)))
               start end withkey unclearsig)
      (progn
        (run-hooks 'mc-post-signature-hook)
        t)))

(defun mc-sign-message (&optional withkey scheme start end unclearsig)
  "Clear sign the message."
  (save-excursion
    (let ((headers-end (mc-find-headers-end)))
      (or withkey
          (progn
            (goto-char (point-min))
            (re-search-forward
             (concat "^" (regexp-quote mail-header-separator) "\n"))
            (save-restriction
              (narrow-to-region (point) headers-end)
              (setq withkey (mail-fetch-field "From")))))
      (or start (setq start headers-end))
      (or end (setq end (point-max-marker)))
      (mc-sign-generic withkey scheme start end unclearsig))))

;;}}}
;;{{{ Signature verification

;;;###autoload
(defun mc-verify ()
  "*Verify a message in the current buffer.

Exact behavior depends on current major mode."
  (interactive)
  (let* ((mode-alist (cdr-safe (assq major-mode mc-modes-alist)))
         (func (or (cdr-safe (assq 'verify mode-alist))
                   'mc-verify-signature)))
    (funcall func)))

(defun mc-verify-signature ()
  "*Verify the signature of the signed message in the current buffer.
Show the result as a message in the minibuffer. Returns t if the signature
is verified."
  (save-excursion
    (let ((schemes mc-schemes)
          limits
          (scheme mc-default-scheme))

      ; Attempt to find a message signed according to the default
      ; scheme.
      (if mc-default-scheme
          (setq
           limits
           (mc-message-delimiter-positions
            (cdr (assoc 'signed-begin-line (funcall mc-default-scheme)))
            (cdr (assoc 'signed-end-line (funcall mc-default-scheme))))))

      ; We can't find a message signed in the default scheme.
      ; Step through all the schemes we know, trying to identify
      ; the applicable one by examining headers.
      (while (and (null limits)
                  schemes
                  (setq scheme (cdr (car schemes)))
                  (not
                   (setq
                    limits
                    (mc-message-delimiter-positions
                     (cdr (assoc 'signed-begin-line (funcall scheme)))
                     (cdr (assoc 'signed-end-line (funcall scheme)))))))
        (setq schemes (cdr schemes)))

      (if (null limits)
          (error "Found no signed message in this buffer.")
        (funcall (cdr (assoc 'verification-func (funcall scheme)))
                 (car limits) (cdr limits))))))


;;}}}
;;{{{ Remailer interface


;;;###autoload
(defun mc-remail (arg)
  "*Prepare the current buffer for delivery through an anonymous remailer.

Exact behavior depends on current major mode.

With \\[universal-argument], show verbose progress during remailing.

With \\[universal-argument] \\[universal-argument], prompt for remailer scheme to use."
  (interactive "p")
  (mc-remail-region arg nil nil))

;;;###autoload
(defun mc-remail-region (arg start end)
  "*Deliver the region to an anonymous remailer."
  (interactive "p\nr")
  (let* ((mode-alist (cdr-safe (assq major-mode mc-modes-alist)))
         (func (cdr-safe (assq 'remailer-encrypt mode-alist)))
         (verbose nil)
         scheme)
    (unless func
      (error "No remailer-encryption function defined for mode '%s'"
             major-mode))
    ;; func will usually be mc-remail-message, below
    (if (>= arg 4)
        (setq verbose t))
    (if (>= arg 16)
        (setq scheme
              (cdr (assoc
                    (completing-read "Remailer Scheme: " mc-remailer-schemes)
                    mc-remailer-schemes))))
    (funcall func nil scheme start end verbose)))

(defun mc-remail-generic (&optional recipients scheme start end verbose)
  "*Generic function to send a region of data through a remailer."
  (save-excursion
    (or start (setq start (point-min-marker)))
    (or (markerp start) (setq start (copy-marker start)))
    (or end (setq end (point-max-marker)))
    (or (markerp end) (setq end (copy-marker end)))
    (run-hooks 'mc-pre-remail-hook)
    (cond ((stringp recipients)
           (setq recipients
                 (mc-split "\\([ \t\n]*,[ \t\n]*\\)+" recipients)))
          ((null recipients)
           (setq recipients
                 (mc-cleanup-recipient-headers (read-string "Recipients: "))))
          (t (error
              "mc-remail-generic: recipients not string or nil")))
    (or scheme (setq scheme mc-default-remailer-scheme))
    ;; this will be one of (mc-mixmaster-send mc-mixminion-send
    ;; mc-remailer-type1-send)
    (if (funcall (cdr (assoc 'encryption-func (funcall scheme)))
                 recipients start end verbose)
        (progn
          (run-hooks 'mc-post-remail-hook)
          t))))

(defun mc-remail-message (&optional recipients scheme start end verbose)
  "*Prepare a message for anonymous delivery to RECIPIENTS using the given
remailer SCHEME. RECIPIENTS is a comma separated string. If SCHEME is nil,
use the value of `mc-default-remailer-scheme'. Returns t on success, nil
otherwise."
  (save-excursion
    (let ((headers-end (mc-find-headers-end))
          default-recipients)

      (setq default-recipients
            (save-restriction
              (goto-char (point-min))
              (re-search-forward
               (concat "^" (regexp-quote mail-header-separator) "$"))
              (narrow-to-region (point-min) (point))
              (and (featurep 'mailalias)
                   (not (featurep 'mail-abbrevs))
                   mail-aliases
                   (expand-mail-aliases (point-min) (point-max)))
              (mc-strip-addresses
               (mapcar 'cdr
                       (mc-get-fields "to\\|cc\\|bcc")))))

      (if (not recipients)
          (setq recipients
                (if mc-use-default-recipients
                    default-recipients
                  (read-from-minibuffer "Recipients: " default-recipients))))

      (or start (setq start headers-end))
      (or end (setq end (point-max-marker)))

      (mc-remail-generic recipients scheme start end verbose))))

;;}}}

;;{{{ Key management

;;{{{ mc-insert-public-key

;;;###autoload
(defun mc-insert-public-key (&optional userid scheme)
  "*Insert your public key at point.
With one prefix arg, prompts for user id to use. With two prefix
args, prompts for encryption scheme."
  (interactive
   (let (arglist)
     (if (not (and (listp current-prefix-arg)
                   (numberp (car current-prefix-arg))))
         nil
       (if (>= (car current-prefix-arg) 16)
           (setq arglist
                 (cons (cdr (assoc (completing-read "Encryption Scheme: "
                                                    mc-schemes)
                                   mc-schemes))
                       arglist)))
       (if (>= (car current-prefix-arg) 4)
           (setq arglist (cons (read-string "User ID: ") arglist))))
     arglist))

;  (if (< (point) (mc-find-headers-end))
;      (error "Can't insert key inside message header"))
  (or scheme (setq scheme mc-default-scheme))
  (or userid (setq userid (cdr (assoc 'user-id (funcall scheme)))))

  ;; (goto-char (point-max))
  (if (not (bolp))
      (insert "\n"))
  (funcall (cdr (assoc 'key-insertion-func (funcall scheme))) userid))

;;}}}
;;{{{ mc-snarf-keys

;;;###autoload
(defun mc-snarf ()
  "*Add all public keys in the buffer to your keyring.

Exact behavior depends on current major mode."
  (interactive)
  (let* ((mode-alist (cdr-safe (assq major-mode mc-modes-alist)))
         (func (or (cdr-safe (assq 'snarf mode-alist))
                   'mc-snarf-keys)))
    (funcall func)))

;;;###autoload
(defun mc-snarf-keys ()
  "*Add all public keys in the buffer to your keyring."
  (interactive)
  (let ((schemes mc-schemes)
        (start (point-min))
        (found 0)
        limits
        (scheme mc-default-scheme))
    (save-excursion
      (catch 'done
        (while t

          ; Attempt to find a message signed according to the default
          ; scheme.
          (if mc-default-scheme
              (setq
               limits
               (mc-message-delimiter-positions
                (cdr (assoc 'key-begin-line (funcall mc-default-scheme)))
                (cdr (assoc 'key-end-line (funcall mc-default-scheme)))
                start)))
          ; We can't find a message signed in the default scheme.
          ; Step through all the schemes we know, trying to identify
          ; the applicable one by examining headers.
          (while (and (null limits)
                      schemes
                      (setq scheme (cdr (car schemes)))
                      (not
                       (setq
                        limits
                        (mc-message-delimiter-positions
                         (cdr (assoc 'key-begin-line (funcall scheme)))
                         (cdr (assoc 'key-end-line (funcall scheme)))
                         start))))
            (setq schemes (cdr schemes)))
          (if (null limits)
              (throw 'done found)
            (setq start (cdr limits))
            (setq found (+ found (funcall (cdr (assoc 'snarf-func
                                                      (funcall scheme)))
                                          (car limits) (cdr limits)))))))
      (message (format "%d new key%s found" found
                       (if (eq 1 found) "" "s"))))))
;;}}}
;;{{{ Key fetching
;;;###autoload
(defun mc-fetch-key (&optional id)
  "Fetch key with specified id from a server."
  (interactive)
  (if mc-default-scheme
      (funcall (cdr (assoc 'key-fetch-func (funcall mc-default-scheme)))
               id)
    (error "mc-default-scheme not set")))
;;}}}


;;}}}
;;{{{ Mode specific functions

;;{{{ RMAIL
;;;###autoload
(defun mc-rmail-summary-verify-signature ()
  "*Verify the signature in the current message."
  (interactive)
  (if (not (eq major-mode 'rmail-summary-mode))
      (error
       "mc-rmail-summary-verify-signature called in inappropriate buffer"))
  (save-excursion
    (set-buffer rmail-buffer)
    (mc-verify)))

;;;###autoload
(defun mc-rmail-summary-decrypt-message ()
  "*Decrypt the contents of this message"
  (interactive)
  (if (not (eq major-mode 'rmail-summary-mode))
      (error
       "mc-rmail-summary-decrypt-message called in inappropriate buffer"))
  (save-excursion
    (set-buffer rmail-buffer)
    (mc-decrypt)))

;;;###autoload
(defun mc-rmail-summary-snarf-keys ()
  "*Adds keys from current message to public key ring"
  (interactive)
  (if (not (eq major-mode 'rmail-summary-mode))
      (error
       "mc-rmail-summary-snarf-keys called in inappropriate buffer"))
  (save-excursion
    (set-buffer rmail-buffer)
    (mc-snarf)))

;;;###autoload
(defun mc-rmail-verify-signature ()
  "*Verify the signature in the current message."
  (interactive)
  (if (not (equal mode-name "RMAIL"))
      (error "mc-rmail-verify-signature called in a non-RMAIL buffer"))
  ;; Hack to load rmailkwd before verifying sig
  (rmail-add-label "verified")
  (rmail-kill-label "verified")
  (if (mc-verify-signature)
      (rmail-add-label "verified")))

;;;###autoload
(defun mc-rmail-decrypt-message ()
  "*Decrypt the contents of this message"
  (interactive)
  (if (not (equal mode-name "RMAIL"))
      (error "mc-rmail-decrypt-message called in a non-RMAIL buffer"))
  (let (decryption-result)
    (unwind-protect
        (progn
          (rmail-edit-current-message)
          (setq decryption-result (mc-decrypt-message))
          (cond ((not (car decryption-result))
                 (rmail-abort-edit))
                ((and (not (eq mc-always-replace 'never))
                      (or mc-always-replace
                          (y-or-n-p
                           "Replace encrypted message with decrypted? ")))
		 ;; This "let" works around a bug in GNU Emacs 23.1
		 ;; rmail/mbox rmail-cease-edit nulling the buffer
		 ;; local value of rmail-buffer, so rmail-set-label
		 ;; fails with "Wrong type argument: stringp, nil".
		 ;; See Emacs bug report 5594,
		 ;; http://debbugs.gnu.org/db/55/5594.html
		 (let ((rmail-buffer--saved rmail-buffer))
		   (rmail-cease-edit)
		   (unless rmail-buffer
		     (message "Workaround rmail-cease-edit nulling buffer local rmail-buffer")
		     (when (or (> emacs-major-version 23)
			       (and (= emacs-major-version 23)
				    (> emacs-minor-version 1)))
		       (warning "Should not occur in GNU Emacs >23.1, but you are using %s.%s"
				emacs-major-version
				emacs-minor-version))
		     (setq rmail-buffer rmail-buffer--saved)))
                 (rmail-kill-label "edited")
                 (rmail-add-label "decrypted")
                 (if (cdr decryption-result)
                     (rmail-add-label "verified")))
                (t
                 (let ((tmp (generate-new-buffer "*Mailcrypt Viewing*")))
                   (copy-to-buffer tmp (point-min) (point-max))
                   (rmail-abort-edit)
                   (switch-to-buffer tmp t)
                   (goto-char (point-min))
                   (insert "From Mailcrypt-" mc-version " "
                           (current-time-string) "\n")
		   (if (fboundp 'rmail-convert-file-maybe)
		       (rmail-convert-file-maybe)
		     (rmail-convert-file))
                   (rmail-mode)
                   (use-local-map (copy-keymap (current-local-map)))
                   (local-set-key "q" 'mc-rmail-view-quit)
                   (set-buffer-modified-p nil)
                   (rmail-add-label "decrypted")
                   (if (cdr decryption-result)
                       (rmail-add-label "verified"))))))
      (if (eq major-mode 'rmail-edit-mode)
          (rmail-abort-edit)))))

;;;###autoload
(defun mc-rmail-view-quit ()
  (interactive)
  (let ((buf (current-buffer)))
    (set-buffer-modified-p nil)
    (rmail-quit)
    (kill-buffer buf)))

;;}}}
;;{{{ VM
;;;###autoload
(defun mc-vm-verify-signature ()
  "*Verify the signature in the current VM message"
  (interactive)
  (if (interactive-p)
      (vm-follow-summary-cursor))
  (vm-select-folder-buffer)
  (vm-check-for-killed-summary)
  (vm-error-if-folder-empty)
  (save-restriction
    (vm-widen-page)
    (mc-verify-signature)))

;;;###autoload
(defun mc-vm-decrypt-message ()
  "*Decrypt the contents of the current VM message"
  (interactive)
  (let ((vm-frame-per-edit nil)
        from-line)
    (if (interactive-p)
        (vm-follow-summary-cursor))
;   (vm-select-folder-buffer) ;; TNX Eric C. Newton for commenting out.
    (vm-check-for-killed-summary)
    (vm-error-if-folder-read-only)
    (vm-error-if-folder-empty)

    ;; store away a valid "From " line for possible later use.
    (setq from-line (vm-leading-message-separator))
    (vm-edit-message)
    (cond ((not (condition-case condition-data
                    (car (mc-decrypt-message))
                  (error
                   (vm-edit-message-abort)
                   (error (message "Decryption failed: %s"
                                   (car (cdr condition-data)))))))
           (vm-edit-message-abort)
           (error "Decryption failed."))
          ((and (not (eq mc-always-replace 'never))
                (or mc-always-replace
                    (y-or-n-p "Replace encrypted message with decrypted? ")))
           (let ((this-command 'vm-edit-message-end))
             (vm-edit-message-end)))
          (t
           (let ((tmp (generate-new-buffer "*Mailcrypt Viewing*")))
             (copy-to-buffer tmp (point-min) (point-max))
             (vm-edit-message-abort)
             (switch-to-buffer tmp t)
             (goto-char (point-min))
             (insert from-line)
             (set-buffer-modified-p nil)
             (vm-mode t))))))

;;;###autoload
(defun mc-vm-snarf-keys ()
  "*Snarf public key from the contents of the current VM message"
  (interactive)
  (if (interactive-p)
      (vm-follow-summary-cursor))
  (vm-select-folder-buffer)
  (vm-check-for-killed-summary)
  (vm-error-if-folder-empty)
  (save-restriction
    (vm-widen-page)
    (mc-snarf-keys)))

;;}}}
;;{{{ GNUS

;;;###autoload
(defun mc-gnus-verify-signature ()
  (interactive)
  (gnus-summary-select-article t)
  (save-excursion
    (set-buffer gnus-original-article-buffer)
    (save-restriction (widen) (mc-verify-signature))))

;;;###autoload
(defun mc-gnus-snarf-keys ()
  (interactive)
  (gnus-summary-select-article t)
  (gnus-eval-in-buffer-window gnus-original-article-buffer
    (save-restriction (widen) (mc-snarf-keys))))

;;;###autoload
(defun mc-gnus-decrypt-message ()
  (interactive)
  (gnus-summary-select-article t)
  ;; Gnus 5 has the string "Gnus" instead of "GNUS" in gnus-version.
  (if (not (let ((case-fold-search nil))
             (string-match "Gnus" gnus-version)))
      (gnus-eval-in-buffer-window
       gnus-article-buffer
       (save-restriction (widen) (mc-decrypt-message)))
    ;; Gnus 5 allows editing of articles.  (Actually, it makes a great
    ;; mail reader.)
    (gnus-eval-in-buffer-window gnus-article-buffer
      (gnus-summary-edit-article t)
      (save-restriction
        (widen)
        (cond ((not (car (mc-decrypt-message)))
               (gnus-article-edit-exit))
              ((and (not (gnus-group-read-only-p))
                    (not (eq mc-always-replace 'never))
                    (or mc-always-replace
                        (y-or-n-p
                         "Replace encrypted message on disk? ")))
               (gnus-article-edit-done))
              (t
               (gnus-article-edit-exit)))))))

;;}}}
;;{{{ MH
(defvar mc-mh-backup-msg 3
  "If 0, never back up MH messages.  If 3, always back up messages.")

;;;###autoload
(defun mc-mh-decrypt-message ()
  "Decrypt the contents of the current MH message in the show buffer."
  (interactive "P")
  (let* ((msg (mh-get-msg-num t))
         (msg-filename (mh-msg-filename msg))
         (show-buffer (get-buffer mh-show-buffer))
         decrypt-okay decrypt-on-disk)
    (setq
     decrypt-on-disk
     (and (not (eq mc-always-replace 'never))
          (or mc-always-replace
              (y-or-n-p "Replace encrypted message on disk? "))))
    (if decrypt-on-disk
        (progn
          (save-excursion
            (set-buffer (create-file-buffer msg-filename))
            (insert-file-contents msg-filename t)
            (if (setq decrypt-okay (car (mc-decrypt-message)))
                (save-buffer mc-mh-backup-msg)
              (message "Decryption failed.")
              (set-buffer-modified-p nil))
            (kill-buffer nil))
          (if decrypt-okay
              (if (and show-buffer
                       (equal msg-filename (buffer-file-name show-buffer)))
                  (save-excursion
                    (save-window-excursion
                      (mh-invalidate-show-buffer)))))
          (mh-show msg))
      (mh-show msg)
      (save-excursion
        (set-buffer mh-show-buffer)
        (let ((read-only buffer-read-only))
          (unwind-protect
              (progn
                (setq buffer-read-only nil)
                (if (setq decrypt-okay (car (mc-decrypt-message)))
                    (progn
                      (goto-char (point-min))
                      (set-buffer-modified-p nil))
                  (message "Decryption failed.")))
            (setq buffer-read-only read-only)
            )))
      (if (not decrypt-okay)
          (progn
            (mh-invalidate-show-buffer)
            (mh-show msg))))))

;;;###autoload
(defun mc-mh-verify-signature ()
  "*Verify the signature in the current MH message."
  (interactive)
  (mh-show)
  (mh-in-show-buffer (mh-show-buffer)
    (mc-verify-signature)))


;;;###autoload
(defun mc-mh-snarf-keys ()
  (interactive)
  (mh-show)
  (mh-in-show-buffer (mh-show-buffer)
    (mc-snarf-keys)))

;;}}}

;;{{{ mew
;; decrypt, verify, snarf, encrypt, sign
;; in summary mode, decrypt/verify/snarf must switch to correct buffer first
;; in correct buffer, generic functions are probably sufficient.
;;  mew doesn't have any labels to be added (though I really wish it did)
;;  could have a "replace encrypted with decrypted" hook for decrypt

;; autoloads:
;;  mew-message-mode
;;   (add-hook 'mew-message-mode-hook 'mc-install-read-mode)
;;  mew-summary-mode
;;   (add-hook 'mew-summary-mode-hook 'mc-install-read-mode)
;;  mew-draft-mode
;;   (add-hook 'mew-draft-mode-hook 'mc-install-write-mode)

;;;###autoload
(defun mc-mew-summary-decrypt-message()
  "*Decrypt the current message"
  (interactive)
  (if (not (eq major-mode 'mew-summary-mode))
      (error
       "mc-mew-summary-decrypt-message called in inappropriate buffer"))
  (save-excursion
    (mew-summary-display t)
    (set-buffer (mew-buffer-message))
    (mc-decrypt)))

;;;###autoload
(defun mc-mew-summary-verify-signature()
  "*Verify the signature in the current message."
  (interactive)
  (if (not (eq major-mode 'mew-summary-mode))
      (error
       "mc-mew-summary-verify-signature called in inappropriate buffer"))
  (save-excursion
    (mew-summary-display t)
    (set-buffer (mew-buffer-message))
    (mc-verify)))

;;;###autoload
(defun mc-mew-summary-snarf-keys()
  "*Add keys from the current message to the public keyring."
  (interactive)
  (if (not (eq major-mode 'mew-summary-mode))
      (error
       "mc-mew-summary-snarf-keys called in inappropriate buffer"))
  (save-excursion
    (mew-summary-display t)
    (set-buffer (mew-buffer-message))
    (mc-snarf)))

;;;###autoload
(defun mc-mew-decrypt-message ()
  "*Decrypt the contents of this message."
  ;; This is a hack to deal with the fact that mew-message buffers are
  ;; generally read-only. For now, there is no option to replace the
  ;; encrypted message in-place; it simply disappears when you move to a
  ;; different message.
  (interactive)
  (let ((read-only buffer-read-only))
    (unwind-protect
        (save-excursion
          (setq buffer-read-only nil)
          (mc-decrypt-message))
      (setq buffer-read-only read-only))))

;;}}}

;;}}}