* nnmail.el (nnmail-cache-insert): make sure that the
[gnus] / lisp / canlock.el
index f24a88e..55b2e59 100644 (file)
@@ -1,5 +1,6 @@
-;;; canlock.el --- Functions for Cancel-Lock feature
-;; Copyright (C) 1998, 1999, 2001 Free Software Foundation, Inc.
+;;; canlock.el --- functions for Cancel-Lock feature
+
+;; Copyright (C) 1998, 1999, 2001, 2002, 2003 Free Software Foundation, Inc.
 
 ;; Author: Katsumi Yamaoka <yamaoka@jpl.org>
 ;; Keywords: news, cancel-lock, hmac, sha1, rfc2104
 
 ;;; Code:
 
-(defconst canlock-version "0.8")
-
 (eval-when-compile
   (require 'cl))
 
 (autoload 'sha1-binary "sha1-el")
+(autoload 'base64-encode-string "base64")
+(autoload 'mail-fetch-field "mail-utils")
+(defvar mail-header-separator)
 
 (defgroup canlock nil
   "The Cancel-Lock feature."
 
 (defcustom canlock-password nil
   "Password to use when signing a Cancel-Lock or a Cancel-Key header."
-  :type 'string
+  :type '(radio (const :format "Not specified " nil)
+               (string :tag "Password" :size 0))
   :group 'canlock)
 
 (defcustom canlock-password-for-verify canlock-password
   "Password to use when verifying a Cancel-Lock or a Cancel-Key header."
-  :type 'string
+  :type '(radio (const :format "Not specified " nil)
+               (string :tag "Password" :size 0))
   :group 'canlock)
 
 (defcustom canlock-force-insert-header nil
@@ -102,55 +106,45 @@ buffer does not look like a news message."
       (let ((coding-system-for-read 'binary)
            (coding-system-for-write 'binary)
            selective-display
-           (case-fold-search t)
-           (str ""))
+           (case-fold-search t))
        (insert message)
        (apply 'call-process-region (point-min) (point-max)
               canlock-openssl-program t t nil canlock-openssl-args)
        (goto-char (point-min))
        (insert "\"")
-       (while (re-search-forward "[0-9a-f][0-9a-f]" nil t)
-         (replace-match (concat "\\\\x" (match-string 0))))
+       (while (re-search-forward "\\([0-9a-f][0-9a-f]\\)" nil t)
+         (replace-match "\\\\x\\1"))
        (insert "\"")
        (goto-char (point-min))
        (read (current-buffer))))))
 
-(defvar canlock-read-passwd nil)
-(defun canlock-read-passwd (prompt &rest args)
-  "Read a password using PROMPT.
-If ARGS, PROMPT is used as an argument to `format'."
-  (let ((prompt
-        (if args
-            (apply 'format prompt args)
-          prompt)))
-    (unless canlock-read-passwd
-      (if (or (fboundp 'read-passwd) (load "passwd" t))
-         (setq canlock-read-passwd 'read-passwd)
-       (unless (fboundp 'ange-ftp-read-passwd)
-         (autoload 'ange-ftp-read-passwd "ange-ftp"))
-       (setq canlock-read-passwd 'ange-ftp-read-passwd)))
-    (funcall canlock-read-passwd prompt)))
+(eval-when-compile
+  (defmacro canlock-string-as-unibyte (string)
+    "Return a unibyte string with the same individual bytes as STRING."
+    (if (fboundp 'string-as-unibyte)
+       (list 'string-as-unibyte string)
+      string)))
+
+(defun canlock-sha1 (message)
+  "Make a SHA-1 digest of MESSAGE as a unibyte string of length 20 bytes."
+  (canlock-string-as-unibyte (funcall canlock-sha1-function message)))
 
 (defun canlock-make-cancel-key (message-id password)
   "Make a Cancel-Key header."
-  (cond ((> (length password) 20)
-        (setq password (funcall canlock-sha1-function password)))
-       ((< (length password) 20)
-        (setq password (concat
-                        password
-                        (make-string (- 20 (length password)) 0)))))
-  (setq password (concat password (make-string 44 0)))
-  (let ((ipad (mapconcat (lambda (char)
-                          (char-to-string (logxor 54 char)))
+  (when (> (length password) 20)
+    (setq password (canlock-sha1 password)))
+  (setq password (concat password (make-string (- 64 (length password)) 0)))
+  (let ((ipad (mapconcat (lambda (byte)
+                          (char-to-string (logxor 54 byte)))
                         password ""))
-       (opad (mapconcat (lambda (char)
-                          (char-to-string (logxor 92 char)))
+       (opad (mapconcat (lambda (byte)
+                          (char-to-string (logxor 92 byte)))
                         password "")))
-    (base64-encode-string (funcall canlock-sha1-function
-                                  (concat
-                                   opad
-                                   (funcall canlock-sha1-function
-                                            (concat ipad message-id)))))))
+    (base64-encode-string
+     (canlock-sha1
+      (concat opad
+             (canlock-sha1
+              (concat ipad (canlock-string-as-unibyte message-id))))))))
 
 (defun canlock-narrow-to-header ()
   "Narrow the buffer to the head of the message."
@@ -225,7 +219,7 @@ message."
            (message "There are no Message-ID(s)")
          (unless password
            (setq password (or canlock-password
-                              (canlock-read-passwd
+                              (read-passwd
                                "Password for Canlock: "))))
          (if (or (not (stringp password)) (zerop (length password)))
              (message "Password for Canlock is bad")
@@ -241,8 +235,7 @@ message."
                (insert "Cancel-Key: sha1:" key-for-key "\n"))
              (when key-for-lock
                (insert "Cancel-Lock: sha1:"
-                       (base64-encode-string (funcall canlock-sha1-function
-                                                      key-for-lock))
+                       (base64-encode-string (canlock-sha1 key-for-lock))
                        "\n")))))))))
 
 ;;;###autoload
@@ -279,7 +272,7 @@ nil instead of to signal an error by setting the option
          (error "%s" errmsg))
 
       (setq password (or canlock-password-for-verify
-                        (canlock-read-passwd "Password for Canlock: ")))
+                        (read-passwd "Password for Canlock: ")))
       (if (or (not (stringp password)) (zerop (length password)))
          (progn
            (setq errmsg "Password for Canlock is bad")
@@ -298,9 +291,9 @@ nil instead of to signal an error by setting the option
        (when locks
          (when id-for-lock
            (setq key-for-lock
-                 (base64-encode-string (funcall canlock-sha1-function
-                                                (canlock-make-cancel-key
-                                                 id-for-lock password))))
+                 (base64-encode-string
+                  (canlock-sha1 (canlock-make-cancel-key id-for-lock
+                                                         password))))
            (when (and locks (not match))
              (setq match (string-equal key-for-lock (pop locks)))))
          (setq locks (if match "good" "bad")))