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
|
;; 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)
(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))
;; MH-E
(condition-case nil (require 'mh-e) (error nil)))
(eval-and-compile
(condition-case nil (require 'mailalias) (error nil)))
(if (not mc-xemacs-p)
(autoload 'mc-scheme-pgp "mc-pgp" nil t))
;;}}}
;;{{{ Encryption
;;;###autoload
(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
(mc-split "\\([ \t\n]*,[ \t\n]*\\)+" 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))
(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))))
;;;###autoload
(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)))
;;;###autoload
(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)
(while (and 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))
(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)))
;;;###autoload
(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)))
;;;###autoload
(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)
(while (and 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))))))
;;}}}
;;{{{ 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)
(save-excursion
(catch 'done
(while t
(while (and 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"))))))
;;}}}
;;}}}
;;{{{ 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)
(let (decryption-result)
(if (not (equal mode-name "RMAIL"))
(error "mc-rmail-decrypt-message called in a non-RMAIL buffer"))
(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? ")))
(rmail-cease-edit)
(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")
(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)))))
(if (eq major-mode 'rmail-edit-mode)
(rmail-abort-edit)))))
(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)
(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)
(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)
(gnus-eval-in-buffer-window gnus-article-buffer
(save-restriction (widen) (mc-snarf-keys))))
;;;###autoload
(defun mc-gnus-decrypt-message ()
(interactive)
(gnus-summary-select-article)
;; 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-summary-edit-article-postpone))
((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-summary-edit-article-done))
(t
(gnus-summary-edit-article-postpone)))))))
;;}}}
;;{{{ MH
;;;###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)
(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)
(if (setq decrypt-okay (car (mc-decrypt-message)))
(progn
(goto-char (point-min))
(set-buffer-modified-p nil))
(message "Decryption failed.")))
(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)))
;;}}}
;;}}}
|