This file is indexed.

/usr/share/emacs/site-lisp/mailcrypt/mc-remail.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
;; mc-remail.el --- Remailer support for Mailcrypt

;; Copyright (C) 1995 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 required packages

(require 'mail-utils)
(require 'sendmail)
(require 'mailcrypt)

(eval-and-compile
  (autoload 'mc-cleanup-recipient-headers "mc-toplev")
  (autoload 'mc-encrypt-message "mc-toplev"))

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

;;}}}
;;{{{ Functions dealing with remailer structures

(defsubst mc-remailer-create (addr id props pre-encr post-encr)
  "Create a remailer structure.

ADDR is the remailer's Email address, a string.

ID is the remailer's public key ID (a string) or nil if the same as
ADDR.

PROPS is a list of properties, as strings.

PRE-ENCR is a list of pre-encryption functions.  Its elements will be
called with the remailer structure itself as argument.

POST-ENCR is similar, but for post-encryption functions."
(list 'remailer addr id props pre-encr post-encr))

(defsubst mc-remailerp (remailer)
  "Test whether REMAILER is a valid remailer struct."
  (and (listp remailer) (eq 'remailer (car-safe remailer))))

(defsubst mc-remailer-address (remailer)
  "Return the Email address of REMAILER."
  (nth 1 remailer))

(defsubst mc-remailer-userid (remailer)
  "Return the userid with which to look up the public key for REMAILER."
  (or (nth 2 remailer)
      (mc-strip-address (mc-remailer-address remailer))))

(defsubst mc-remailer-properties (remailer)
  "Return the property list for REMAILER"
  (nth 3 remailer))

(defsubst mc-remailer-pre-encrypt-hooks (remailer)
  "Return the list of pre-encryption hooks for REMAILER."
  (nth 4 remailer))

(defsubst mc-remailer-post-encrypt-hooks (remailer)
  "Return the list of post-encryption hooks for REMAILER."
  (nth 5 remailer))

(defun mc-remailer-remove-property (remailer prop)
  (let ((props (append (mc-remailer-properties remailer) nil)))
    (setq props (delete prop props))
    (mc-remailer-create
     (mc-remailer-address remailer)
     (mc-remailer-userid remailer)
     props
     (mc-remailer-pre-encrypt-hooks remailer)
     (mc-remailer-post-encrypt-hooks remailer))))

;;}}}
;;{{{ User variables

(defvar mc-response-block-included-headers
  '("From" "To" "Newsgroups")
  "List of header fields to include in response blocks.

These will be copied into the deepest layer of the response block to
help you identify it when it is used to Email you.")


(defvar mc-remailer-tag "(*REMAILER*)"
  "A string which marks an Email address as belonging to a remailer.")

(defvar mc-levien-file-name "~/.remailers"
  "The file containing a Levien format list of remailers.

The file is read by `mc-read-levien-file' and `mc-reread-levien-file'.

The file should include lines of the following form (other lines
are ignored):

$remailer{\"NAME\"} = \"<EMAIL ADDRESS> PROPERTIES\";

PROPERTIES is a space-separated set of strings.

This format is named after Raphael Levien, who maintained a list of
active remailers. Do \"finger rlist@noisebox.remailer.org\" or
\"finger rlist@mixmaster.shinn.net\" for the latest copy of the
remailer list.")

(defvar mc-remailer-user-chains nil
  "An alist of remailer chains defined by the user.

Format is

((NAME . REMAILER-LIST)
 (NAME . REMAILER-LIST)
 ...)

NAME must be a string.

REMAILER-LIST may be an arbitrary sequence, not just a list.  Its
elements may be any of the following:

1) A remailer structure created by `mc-remailer-create'.  This is
   the base case.

2) A string naming another remailer chain to be spliced in
   at this point.

3) A positive integer N representing a chain to be spliced in at this
   point and consisting of a random permutation of the top N remailers
   as ordered in the file `mc-levien-file-name'.

4) An arbitrary Lisp form to be evaluated, which should
   return another REMAILER-LIST to be recursively processed and
   spliced in at this point.

The complete alist of chains is given by the union of the two lists
`mc-remailer-internal-chains' and `mc-remailer-user-chains'.")

(defvar mc-remailer-internal-chains nil
  "List of \"internal\" remailer chains.

This variable is normally generated automatically from a human-readable
list of remailers; see, for example, the function `mc-reread-levien-file'.

To define your own chains, you probably want to use the variable
`mc-remailer-user-chains'.  See that variable's documentation for
format information.")

(defvar mc-remailer-internal-ranking nil
  "Ordered list of remailers, most reliable first.

This variable is normally generated automatically from a human-readable
list of remailers; see, for example, the function `mc-reread-levien-file'.")

(defvar mc-remailer-user-response-block
  (function
   (lambda (addr lines block)
     (concat
      ";;;\n"
      (format
       "To reply to this message, take the following %d-line block, remove\n"
       lines)
      "leading \"- \" constructs (if any), and place it at the top of a\n"
      (format "message to %s :\n" addr)
      block)))
  "A function called to generate response block text.

Value should be a function taking three arguments (ADDR LINES BLOCK).
ADDR is the address to which the response should be sent.
LINES is the number of lines in the encrypted response block.
BLOCK is the response block itself.
Function should return a string to be inserted into the buffer
by mc-remailer-insert-response-block.")

(defvar mc-remailer-pseudonyms nil
  "*A list of your pseudonyms.

This is a list of strings.  Completion against it will be available
when you are prompted for your pseudonym.")

(defvar mc-remailer-preserved-headers
  '("References" "Followup-to" "In-reply-to" "Subject")
  "*Header fields which are preserved as hashmark headers when rewriting.

This is a list of strings naming the preserved headers.  Note that
\"Newsgroups\" and \"To\" are handled specially and should not be included in
this list.")

;;}}}
;;{{{ Handling Levien format remailer lists

(defun mc-parse-levien-buffer ()
  ;; Parse a buffer in Levien format.
  (goto-char (point-min))
  (let (chains remailer remailer-name ranking)
    (while
	(re-search-forward
	 "^\\$remailer{['\"]\\(.+\\)['\"]}[ \t]*=[ \t]*['\"]\\(.*\\)['\"];"
	 nil t)
      (let ((name (buffer-substring-no-properties
		   (match-beginning 1) (match-end 1)))
	    property-list address
	    (value-start (match-beginning 2))
	    (value-end (match-end 2)))
	(goto-char value-start)
	(while (re-search-forward "[^ \t]+" value-end 'no-error)
	  (setq property-list
		(append
		 property-list
		 (list (buffer-substring-no-properties
			(match-beginning 0) (match-end 0))))))
	(setq address (car property-list)
	      property-list (cdr property-list)
	      remailer-name name)
	(if (not
	     (or (member "mix" property-list)
		 (and (or (member "pgp" property-list)
			  (member "pgp." property-list))
		      (or (member "cpunk" property-list)
			  (member "eric" property-list)))))
	    (setq remailer nil)
	  (setq remailer
		(mc-remailer-create
		 address		; Address
		 (if (member "pgp." property-list)
		     name)		; User ID
		 property-list
		 '(mc-generic-pre-encrypt-function) ; Pre-encrypt hooks
		 '(mc-generic-post-encrypt-function) ; Post-encrypt hooks
		 ))))
      (if (not (null remailer))
	  (setq chains (cons (list remailer-name remailer) chains))))
    (goto-char (point-min))
    (if (re-search-forward "----------" nil t) ; Locate rankings at bottom
	;; Read each word in the rankings section.  Each time we
	;; hit a remailer we've identified, append it to the ranking
	;; list.  Thus we sort remailers according to rank.
	(while (re-search-forward "^\\([a-zA-Z0-9\\-]+\\) " nil t)
	  (setq remailer-name (buffer-substring-no-properties
			       (match-beginning 1) (match-end 1)))
	  (if (assoc remailer-name chains)
	      (setq ranking (append ranking (list remailer-name))))))
    (cons chains ranking)))

(defun mc-read-levien-file ()
  "Read the Levien format file specified in `mc-levien-file-name'.
Return an alist of length-1 chains, one for each remailer, named
after the remailer.  Only include remailers supporting PGP
encryption."
  (save-excursion
    (if (file-readable-p mc-levien-file-name)
	(prog2
	    (find-file-read-only mc-levien-file-name)
	    (mc-parse-levien-buffer)
	  (bury-buffer)))))

;;;###autoload
(defun mc-reread-levien-file ()
  "Read the Levien format file specified in `mc-levien-file-name'.

Place result in `mc-remailer-internal-chains' and `mc-remailer-internal-ranking'.

See the documentation for the variable `mc-levien-file-name' for
a description of Levien file format."
  (interactive)
  (let ((parsed-levien-file (mc-read-levien-file)))
    (setq mc-remailer-internal-chains (car parsed-levien-file)
	  mc-remailer-internal-ranking (cdr parsed-levien-file))))

;;}}}
;;{{{ Arbitrary chain choice

(defun mc-remailer-choose-first (n &optional l)
  (cond
   ((= n 0) nil)
   ((null l) (mc-remailer-choose-first n mc-remailer-internal-ranking))
   (t (cons (car l) (mc-remailer-choose-first (1- n) (cdr l))))))

(defun mc-remailer-choose-chain (n)
  (if (null mc-remailer-internal-ranking)
      (error "No ranking information, cannot choose the %d best remailer%s"
	     n (if (> n 1) "s" "")))
  (append (shuffle-vector (vconcat (mc-remailer-choose-first n)))
	  nil))

;;}}}
;;{{{ Canonicalization function

(defun mc-remailer-canonicalize-elmt (elmt chains-alist)
  (cond
   ((mc-remailerp elmt) (list elmt))
   ((stringp elmt)
    (mc-remailer-canonicalize-chain (cdr (assoc elmt chains-alist))
				    chains-alist))
   ((integerp elmt)
    (mc-remailer-canonicalize-chain (mc-remailer-choose-chain elmt)
				    chains-alist))
   (t (mc-remailer-canonicalize-chain (eval elmt) chains-alist))))

(defun mc-remailer-canonicalize-chain (chain &optional chains-alist)
  ;; Canonicalize a remailer chain with respect to CHAINS-ALIST.
  ;; That is, use CHAINS-ALIST to resolve strings.
  ;; Here is where we implement the functionality described in
  ;; the documentation for the variable `mc-remailer-user-chains'.
  (if (null chains-alist)
      (setq chains-alist (mc-remailer-make-chains-alist)))
  (cond
   ((null chain) nil)
   ;; Handle case where chain is actually a string or a single
   ;; remailer.
   ((or (stringp chain) (mc-remailerp chain) (integerp chain))
    (mc-remailer-canonicalize-elmt chain chains-alist))
   (t
    (let ((first (elt chain 0))
	  (rest (cdr (append chain nil))))
      (append
       (mc-remailer-canonicalize-elmt first chains-alist)
       (mc-remailer-canonicalize-chain rest chains-alist))))))

;;}}}
;;{{{ Auxiliaries for mail header munging

(defsubst mc-nuke-field (field &optional bounds)
  ;; Delete all fields exactly matching regexp FIELD from header,
  ;; bounded by BOUNDS.  Default is entire visible region of buffer.
  (mc-get-fields field bounds t))

(defun mc-replace-field (field-name replacement header)
  (save-excursion
    (save-restriction
      (if (not (string-match "^[ \t]" replacement))
	  (setq replacement (concat " " replacement)))
      (if (not (string-match "\n$" replacement))
	  (setq replacement (concat replacement "\n")))
      (let ((case-fold-search t)
	    (field-regexp (regexp-quote field-name)))
	(narrow-to-region (car header) (cdr header))
	(goto-char (point-min))
	(re-search-forward
	 (concat "^" field-regexp ":" mc-field-body-regexp)
	 nil t)
	(mc-nuke-field field-regexp header)
	(insert field-name ":" replacement)))))

(defun mc-find-main-header (&optional ignored)
  ;; Find the main header of the mail message; return as a pair of
  ;; markers (START . END).
  (save-excursion
    (goto-char (point-min))
    (re-search-forward
     (concat "^" (regexp-quote mail-header-separator) "\n"))
    (forward-line -1)
    (cons (copy-marker (point-min)) (copy-marker (point)))))

(defun mc-find-colon-header (&optional insert)
  ;; Find the header with a "::" immediately after the
  ;; mail-header-separator.  Return region enclosing header.  Optional
  ;; arg INSERT means insert the header if it does not exist already.
  (save-excursion
    (goto-char (point-min))
    (re-search-forward
     (concat "^" (regexp-quote mail-header-separator) "\n"))
    (if (or (and (looking-at "::\n") (forward-line 1))
	    (and insert
		 (progn
		   (insert-before-markers "::\n\n")
		   (forward-line -1))))
	(let ((start (point)))
	  (re-search-forward "^$" nil 'move)
	  (cons (copy-marker start) (copy-marker (point)))))))

(defun mc-find-hash-header (&optional insert)
  (save-excursion
    (goto-char (point-min))
    (re-search-forward
     (concat "^" (regexp-quote mail-header-separator) "\n"))
    (if (or (and (looking-at "##\n") (forward-line 1))
	    (and (looking-at "::\n")
		 (re-search-forward "^\n" nil 'move)
		 (looking-at "##\n")
		 (forward-line 1))
	    (and insert
		 (progn
		   (insert-before-markers "##\n\n")
		   (forward-line -1))))
	(let ((start (point)))
	  (re-search-forward "^$" nil 'move)
	  (cons (copy-marker start) (copy-marker (point)))))))


(defsubst mc-replace-main-field (field replacement)
  (mc-replace-field field replacement (mc-find-main-header t)))

(defsubst mc-replace-hash-field (field replacement)
  (mc-replace-field field replacement (mc-find-hash-header t)))

(defsubst mc-replace-colon-field (field replacement)
  (mc-replace-field field replacement (mc-find-colon-header t)))

(defun mc-recipient-is-remailerp ()
  (let ((to (mc-get-fields "To" (mc-find-main-header))))
    (and to
	 (string-match (regexp-quote mc-remailer-tag) (cdr (car to))))))

;;}}}
;;{{{ Pre-encryption and post-encryption hook defaults

(defun mc-generic-post-encrypt-function (remailer)
  (let ((main-header (mc-find-main-header))
	(colon-header (mc-find-colon-header t)))
    (mc-replace-field "Encrypted" "PGP" colon-header)
    (mc-replace-field
     "To"
     (concat (mc-remailer-address remailer) " " mc-remailer-tag)
     main-header)))

(defun mc-generic-pre-encrypt-function (remailer)
  (let ((addr (mc-remailer-address remailer))
	(props (mc-remailer-properties remailer))
	(main-header (mc-find-main-header))
	(colon-header (mc-find-colon-header t))
	to to-field preserved-regexp preserved)

    (setq preserved-regexp
	  (mc-disjunction-regexp mc-remailer-preserved-headers))
    (setq preserved (mc-get-fields preserved-regexp main-header t))
    (if preserved (goto-char (cdr (mc-find-hash-header t))))
    (mapcar (function
	     (lambda (c)
	       (insert (car c) ":"
		       (mc-eliminate-continuation-lines (cdr c)))))
	    preserved)

    (if (and (mc-find-hash-header) (not (member "hash" props)))
	(error "Remailer %s does not support hashmarks" addr))

    (if (mc-get-fields "Newsgroups" main-header)
	(cond ((not (member "post" props))
	       (error "Remailer %s does not support posting" addr))
	      ((not (member "hash" props))
	       (error "Remailer %s does not support hashmarks" addr))
	      (t (mc-rewrite-news-to-mail remailer)))
      (and (featurep 'mailalias)
	   (not (featurep 'mail-abbrevs))
	   mail-aliases
	   (expand-mail-aliases (car main-header) (cdr main-header)))
      (setq to (mc-strip-addresses
		(mapcar 'cdr (mc-get-fields "To" main-header))))
      (if (string-match "," to)
	  (error "Remailer %s does not support multiple recipients." addr))
      (setq to-field
	    (if (mc-get-fields "From" colon-header)
		"Send-To"
	      (cond
	       ((member "cpunk" props) "Anon-To")
	       (t (error "Remailer %s is not type-1" addr)))))
      (mc-replace-field to-field to colon-header)
      (mc-nuke-field "Reply-to" main-header))))

;;}}}
;;{{{ Misc. random

(defun mc-disjunction-regexp (regexps)
  ;; Take a list of regular expressions and return a single
  ;; regular expression which matches anything that any of the
  ;; original regexps match.
  (concat "\\("
	  (mapconcat 'identity regexps "\\)\\|\\(")
	  "\\)"))

; Quiet a warning message when the user hasn't set this.
(defvar gnus-user-from-line nil)

(defun mc-user-mail-address ()
  "Figure out the user's Email address as best we can."
  (mc-strip-address
   (cond ((and (boundp 'gnus-user-from-line)
	       (stringp gnus-user-from-line))
	  gnus-user-from-line)
	 ((stringp mail-default-reply-to) mail-default-reply-to)
	 ((boundp 'user-mail-address) user-mail-address)
	 (t (concat (user-login-name) "@" (system-name))))))

(defun mc-eliminate-continuation-lines (string)
  (while (string-match "\n[\t ]+" string)
    (setq string (replace-match " " t nil string)))
  string)

(defun mc-remailer-make-chains-alist ()
  (if (null mc-remailer-internal-chains)
      (mc-reread-levien-file))
  (append mc-remailer-internal-chains mc-remailer-user-chains))

;;;###autoload
(defun mc-remailer-insert-pseudonym ()
  "Insert pseudonym as a From field in the hash-mark header.

See the documentation for the variable `mc-remailer-pseudonyms' for
more information."
  (interactive)
  (let ((completion-ignore-case t)
	pseudonym)
    (setq pseudonym
	  (cond ((null mc-remailer-pseudonyms)
		 (read-from-minibuffer "Pseudonym: "))
		(t
		 (completing-read "Pseudonym: "
				  (mapcar 'list mc-remailer-pseudonyms)))))
    (if (not (string-match "\\S +@\\S +" pseudonym))
	(setq pseudonym (concat pseudonym " <x@x.x>")))
    (mc-replace-colon-field "From" pseudonym)))

;;}}}
;;{{{ Mixmaster support
(defvar mc-mixmaster-path nil
  "*Path to the Mixmaster binary.  If defined, Mixmaster chains will
be passed to this program for rewriting.")

(defvar mc-mixmaster-list-path nil
  "*Path to the Mixmaster type2.list file.")

(defun mc-demix (&rest chain)
  "Use arguments as a remailer-list and return a new list with the
\"mix\" property removed from all the elements."
  (mapcar (function (lambda (r) (mc-remailer-remove-property r "mix")))
	  (mc-remailer-canonicalize-chain chain)))

(defun mc-mixmaster-process (beg end recipients preserved mix-chain)
  ;; Run a region through Mixmaster.
  (let (ret)
    (if (not (markerp end))
	(setq end (copy-marker end)))
    (goto-char beg)
    (mapcar (function (lambda (x) (insert x ?\n))) recipients)
    (insert ?\n)
    (mapcar (function (lambda (x) (insert x))) preserved)
    (insert ?\n)
    (setq mix-chain (mapcar (function (lambda (x) (format "%d" x))) mix-chain))
    ;; Handle case of empty message
    (if (< end (point)) (setq end (point)))
    (setq ret
	  (apply 'call-process-region beg end mc-mixmaster-path t t nil
		 "-f" "-o" "stdout" "-l" mix-chain))
    (if (not (eq ret 0)) (error "Mixmaster barfed."))
    (goto-char beg)
    (re-search-forward "^::$")
    (delete-region beg (match-beginning 0))))

(defun mc-mixmaster-build-alist (&optional n)
  ;; Construct an alist mapping Mixmaster Email addresses to integers.
  ;; FIXME; this is terrible
  (let (buf)
    (save-excursion
      (unwind-protect
	  (progn
	    (setq n (or n 1))
	    (setq buf (find-file-noselect mc-mixmaster-list-path))
	    (set-buffer buf)
	    (if (re-search-forward "^[^ \t]+[ \t]+\\([^ \t]+\\)" nil t)
		(cons (cons (buffer-substring-no-properties
			     (match-beginning 1) (match-end 1))
			    n)
		    (mc-mixmaster-build-alist (+ n 1)))))
	(if buf (kill-buffer buf))))))

(defvar mc-mixmaster-alist nil)

(defsubst mc-mixmaster-alist ()
  (or mc-mixmaster-alist
      (setq mc-mixmaster-alist (mc-mixmaster-build-alist))))

(defun mc-mixmaster-translate-chain (chain)
  ;; Take a chain of Mixmaster remailers and convert it to the list
  ;; of integers which represents them.
  (if (or (null chain)
	  (not (member "mix" (mc-remailer-properties (car chain)))))
      nil
    (cons (cdr (assoc (mc-strip-address (mc-remailer-address (car chain)))
		      (mc-mixmaster-alist)))
	  (mc-mixmaster-translate-chain (cdr chain)))))

(defun mc-mixmaster-skip (chain)
  ;; Return the largest possible suffix of CHAIN whose first element
  ;; is not a Mixmaster.
  (cond ((null chain) nil)
	((not (member "mix" (mc-remailer-properties (car chain))))
	 chain)
	(t (mc-mixmaster-skip (cdr chain)))))

(defun mc-rewrite-for-mixmaster (chain &optional pause)
  ;; Rewrite the current mail buffer for a chain of Mixmasters.
  (let ((mix-chain (mc-mixmaster-translate-chain chain))
	(main-header (mc-find-main-header))
	(colon-header (mc-find-colon-header))
	(hash-header (mc-find-hash-header))
        (newsgroups (mc-get-fields "Newsgroups" nil t))
	recipients preserved newsgroups first last rest preserved-regexp)

    ;; Figure out FIRST and LAST. FIRST is the first Mixmaster in the
    ;; chain.  LAST is the last.
    (setq first (car chain)
	  rest chain)
    (while (and rest (member "mix" (mc-remailer-properties (car rest))))
      (setq last (car rest)
	    rest (cdr rest)))

    ;; If recipient is not a remailer, deal with hashmark and colon
    ;; headers and get rid of them.
    (if (mc-recipient-is-remailerp)
	nil
      (if hash-header
	  (progn
	    (setq preserved (mc-get-fields nil hash-header))
	    (goto-char (car hash-header))
	    (forward-line -1)
	    (delete-region (point) (+ (cdr hash-header) 1))))
      ;; Preserve pseduonym line...
      (if colon-header
	  (progn
	    (setq preserved
		  (append (mc-get-fields "From" colon-header) preserved))
	    (goto-char (car colon-header))
	    (forward-line -1)
	    (delete-region (point) (+ (cdr colon-header) 1)))))

    ;; Expand aliases and get recipients.
    (and (featurep 'mailalias)
	 (not (featurep 'mail-abbrevs))
	 mail-aliases
	 (expand-mail-aliases (car main-header) (cdr main-header)))
    (setq recipients
	  (mc-cleanup-recipient-headers
	   (mapconcat 'cdr (mc-get-fields "To" main-header t) ", ")))
    (if newsgroups
	(setq newsgroups
	  (mc-cleanup-recipient-headers
	   (cdr
	    (assoc "Newsgroups" newsgroups)))))
    ;; Mixmaster does not support posting...
;    (if newsgroups
    ;; Now they do! (1998)
    (if (and newsgroups
	     (not (member "post" (mc-remailer-properties last))))
	(error "Remailer %s does not support posting"
	       (mc-remailer-address last)))
    (setq
     recipients
     (append (mapcar
	      (function (lambda (c) (concat "Post: " c))) newsgroups)
	     recipients))

    (mapcar
     (function (lambda (c) (message c))) recipients)

    (setq
     preserved-regexp
     (mc-disjunction-regexp (cons "Subject" mc-remailer-preserved-headers)))

    (setq preserved
	  (append (mc-get-fields preserved-regexp main-header t) preserved))

    ;; Convert preserved header alist to simple list of strings
    (setq preserved
	  (mapcar
	   (function
	    (lambda (c)
	      (concat (car c) ":"
		      (mc-eliminate-continuation-lines (cdr c)))))
	   preserved))

    ;; Do the conversion
    (goto-char (cdr main-header))
    (forward-line 1)
    (mc-mixmaster-process (point) (point-max) recipients preserved
			  mix-chain)

    (mc-replace-field "To"
		      (concat
		       (mc-remailer-address first) " " mc-remailer-tag)
		      main-header)))

;;}}}
;;{{{ High level message rewriting

(defun mc-rewrite-news-to-mail (remailer)
  (let ((main-header (mc-find-main-header))
	newsgroups)
    (setq newsgroups (mc-get-fields "Newsgroups" main-header t))
    (mc-replace-colon-field "Post-To" (cdr (car newsgroups)))
    (mail-mode)))

(defun mc-rewrite-for-remailer (remailer &optional pause)
  ;; Rewrite the current mail buffer for a single remailer.  This
  ;; includes running the pre-encryption hooks, modifying the To:
  ;; field, encrypting with the remailer's public key, and running the
  ;; post-encryption hooks.
  (let ((addr (mc-remailer-address remailer))
	(main-header (mc-find-main-header)))
    ;; If recipient is already a remailer, make sure the "::" and "##"
    ;; headers get to it
    (if (mc-recipient-is-remailerp)
	(progn
	  (goto-char (cdr main-header))
	  (forward-line 1)
	  (insert "::\n\n")))

    (mapcar
     (function (lambda (hook) (funcall hook remailer)))
     (mc-remailer-pre-encrypt-hooks remailer))

    (if pause
	(let ((cursor-in-echo-area t))
	  (message "SPC to encrypt for %s : " addr)
	  (read-char-exclusive)))
    (setq main-header (mc-find-main-header))
    (goto-char (cdr main-header))
    (forward-line 1)
    (if (let ((mc-pgp-always-sign 'never)
	      (mc-encrypt-for-me nil))
	  (mc-encrypt-message (mc-remailer-userid remailer) nil (point)))
	(progn
	  (mapcar
	   (function (lambda (hook) (funcall hook remailer)))
	   (mc-remailer-post-encrypt-hooks remailer))
	  (mc-nuke-field "Comment")
	  (mc-nuke-field "From"))
      (error "Unable to encrypt message to %s"
	     (mc-remailer-userid remailer)))))

(defun mc-rewrite-for-chain (chain &optional pause)
  ;; Rewrite the current buffer for a chain of remailers.
  ;; CHAIN must be in canonical form.
  (let (rest)
    (if mc-mixmaster-path
	(setq rest (mc-mixmaster-skip chain))
      (setq rest chain))
    (if (null chain) nil
      (mc-rewrite-for-chain
       (if (eq rest chain) (cdr rest) rest) pause)
      (if (eq rest chain)
	  (mc-rewrite-for-remailer (car chain) pause)
	(mc-rewrite-for-mixmaster chain pause)))))

(defun mc-unparse-chain (chain)
  ;; Unparse CHAIN into a string suitable for printing.
  (if (null chain)
      nil
    (concat (mc-remailer-address (car chain)) "\n"
	    (mc-unparse-chain (cdr chain)))))

(defun mc-disallow-field (field &optional header)
  (let ((case-fold-search t))
    (if (null header)
	(setq header (mc-find-main-header)))
    (goto-char (car header))
    (if (re-search-forward (concat "^" (regexp-quote field) ":")
			  (cdr header) t)

	(progn
	  (goto-char (match-beginning 0))
	  (error "Cannot use a %s field." field)))))

;;;###autoload
(defun mc-remailer-encrypt-for-chain (&optional pause)
  "Encrypt message for a remailer chain, prompting for chain to use.

With \\[universal-argument] \\[universal-argument], pause before each
encryption."
  (interactive "P")
  (let ((chains (mc-remailer-make-chains-alist))
	(buffer (get-buffer-create mc-buffer-name))
	chain-name chain)
    (mc-disallow-field "CC")
    (mc-disallow-field "FCC")
    (mc-disallow-field "BCC")
    (setq chain-name
	  (completing-read
	   "Choose a remailer or chain: " chains nil 'strict-match))
    (setq chain
	  (mc-remailer-canonicalize-chain
	   (cdr (assoc chain-name chains))
	   chains))
    (mc-rewrite-for-chain chain pause)
    (if chain
	(save-excursion
	  (set-buffer buffer)
	  (erase-buffer)
	  (insert "Rewritten for chain `" chain-name "':\n\n"
		  (mc-unparse-chain chain))
	  (message "Done.  See %s buffer for details." mc-buffer-name)))))

;;}}}
;;{{{ Response block generation

;;;###autoload
(defun mc-remailer-insert-response-block (&optional arg)
  "Insert response block at point, prompting for chain to use.

With \\[universal-argument], enter a recursive edit of the innermost
layer of the block before encrypting it."
  (interactive "p")
  (let (buf main-header to addr block lines)
    (save-excursion
      (setq buf
	    (mc-remailer-make-response-block (if (> arg 1) t)))
      (set-buffer buf)
      (setq main-header (mc-find-main-header))
      (setq to (cdr (car (mc-get-fields "To" main-header))))
      (setq addr (concat "<" (mc-strip-address to) ">"))
      (goto-char (cdr main-header))
      (forward-line 1)
      (setq block (buffer-substring-no-properties
		   (point) (point-max))
	    lines (count-lines (point) (point-max)))
      (kill-buffer buf))
    (let ((opoint (point)))
      (insert (funcall mc-remailer-user-response-block
		       addr lines block))
      (goto-char opoint))
    (mc-nuke-field "Reply-to" (mc-find-main-header))
    (mc-replace-hash-field "Reply-to" addr)))

(defun mc-remailer-make-response-block (&optional recurse)
  ;; Return a buffer which contains a response block
  ;; for the user, and a To: header for the remailer to use.
  (let ((buf (generate-new-buffer " *Remailer Response Block*"))
	(original-buf (current-buffer))
	(mc-mixmaster-path nil)
	all-headers included-regexp included)
    (setq all-headers (mc-find-main-header))
    (setcdr all-headers
	    (max
	     (cdr all-headers)
	     (or (cdr-safe (mc-find-colon-header)) 0)
	     (or (cdr-safe (mc-find-hash-header)) 0)))
    (save-excursion
      (setq
       included-regexp
       (mc-disjunction-regexp mc-response-block-included-headers))
      (setq included (mc-get-fields included-regexp all-headers))
      (set-buffer buf)
      (insert "To: " (mc-user-mail-address) "\n" mail-header-separator "\n")
      (insert ";; Response block created " (current-time-string) "\n")
      (mapcar (function (lambda (c) (insert "; " (car c) ":" (cdr c))))
	      included)
      (if recurse
	  (progn
	    (switch-to-buffer buf)
	    (message "Editing response block ; %s when done."
		     (substitute-command-keys "\\[exit-recursive-edit]"))
	    (recursive-edit)))
      (set-buffer buf)
      (mc-remailer-encrypt-for-chain)
      (switch-to-buffer original-buf))
    buf))

;;}}}

(defun mc-remailer-type1-send (recipients start end &optional verbose)
  ;; this function is to provide compatibility with the mc-remailer-scheme
  ;; scheme. For type1/old-mixmaster we ignore the arguments and let the old
  ;; functions figure them out on their own. This means
  ;; (mc-remailer-encrypt) from non-mail buffers will only work with type2/3
  ;; schemes. type1/old-mixmaster will get upset when it can't find the
  ;; header-delimiter and thus can't figure out the intended recipients

  (mc-remailer-encrypt-for-chain verbose))

(defun mc-remailer-scheme-type1 ()
  (list
   (cons 'encryption-func   'mc-remailer-type1-send)
))