home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / languages / elisp / misc / md4.el < prev    next >
Encoding:
Text File  |  1992-07-05  |  5.9 KB  |  195 lines

  1. ;; md4.el -- MD4 support for GNUS
  2. ;;
  3. ;; SCCS Status     : @(#)@ md4    1.5
  4. ;; Author          : Johan Vromans
  5. ;; Created On      : Sat May 11 09:10:04 1991
  6. ;; Last Modified By: Dave Brennan
  7. ;; Last Modified On: Mon Jul  6 16:42:14 1992
  8. ;; Update Count    : 13
  9. ;; Status          : OK
  10. ;;
  11. ;; LCD Archive Entry:
  12. ;; md4|Johan Vromans|jv@mh.nl|
  13. ;; MD4 support for GNUS|
  14. ;; 92-06-16|1.5|~/misc/md4.el.Z|
  15. ;;
  16. ;; This file defines functions to calculate a MD4 signature, add
  17. ;; it to outgoing postings, and validate it on incoming postings.
  18. ;;
  19. ;; It uses "gnus-Inews-article-hook", called by GNUS just before passing
  20. ;; the articel to inews, to install the signature.
  21. ;;
  22. ;; "gnus-Article-prepare-hook" is used to validate the signature on
  23. ;; an article if you read it.
  24. ;;
  25. ;; Advised usage: load this file after loading gnus, e.g. from the
  26. ;; gnus-Startup-hook.
  27. ;; You also can do this explicitly:
  28. ;;
  29. ;;      (load "gnus" nil t)
  30. ;;    (load "md4" t nil)    ; optional
  31. ;;    (gnus)
  32. ;;
  33. ;; This file, if useful, is covered by the GPL.
  34. ;;
  35. ;;    Johan Vromans <jv@mh.nl>
  36.  
  37. ;; HISTORY 
  38. ;; 16-Jun-1992        Johan Vromans    
  39. ;;    Added LCD entry.
  40. ;; 22-May-1991        Johan Vromans    
  41. ;;    Enhanced comments and improved customization.
  42. ;;    Added provide 'md4 and require 'add-hook.
  43. ;;    Normalized .signature file inclusion.
  44.  
  45. (provide 'md4)
  46. (or (fboundp 'add-hook)
  47.     (require 'add-hook))    ; by Dan LaLiberte <liberte@cs.uiuc.edu>
  48.  
  49. ;; Customizations
  50. ;;
  51. ;; This function only uses program md4; it doesn't need md4hash nor 
  52. ;; md4check.
  53. ;; The md4 programs can be retrieved from your nearest comp.sources.misc
  54. ;; archive site. Contact the moderator of comp.sources.misc for details.
  55. ;;
  56. (defvar md4-command "md4"
  57.   "*Where to find the md4 program. This program should be in your PATH.")
  58.  
  59. (defvar md4-insertion t
  60.   "*Controls MD4 signature insertion. If nil, no signature is inserted.")
  61.  
  62. (defvar md4-validation 1
  63.   "*Controls MD4 signature validation. If nil, no validation is
  64.   performed. If t, validation is performed, and failures are reported.
  65.   Any other value causes validation to be performed, and failures as
  66.   well as successes to be reported.")
  67.  
  68. ;; If the variable gnus-signature-file is not null, GNUS will append
  69. ;; this file to the article before posting. If null, your .signature
  70. ;; is supposed to be added by your news system. In this case, the md4
  71. ;; calculation will temporary insert the signature to make sure a
  72. ;; correct checksum is calculated.
  73. ;; You may have to change the md4-signature-separator if needed.
  74.  
  75. (defvar md4-signature-separator "-- \n"
  76.   "*If your news posting system appends your .signature file for you, 
  77.   then set this variable to the separator string used. In this case, 
  78.   the signature will be added on behalf of the calculation of the MD4
  79.   checksum, and removed before the article is transferred to the news
  80.   system.
  81.   In general, set it to "--\\n" or "-- \\n" for classic B-news or C-news.")
  82.  
  83. ;;
  84. ;; End of customizations
  85. ;;
  86.  
  87. (defvar md4-signature-header "X-Md4-Signature")
  88.  
  89. ;; Hook definitions and insertions.
  90.  
  91. (add-hook 'gnus-Inews-article-hook 'md4-add-signature)
  92. (add-hook 'gnus-Article-prepare-hook 'md4-validate-signature)
  93. ;;
  94. ;; Calcuates the MD4 signature for the article to be posted, which
  95. ;; is assumed to be in the current buffer.
  96. ;;
  97. (defun md4-add-signature ()
  98.   "Adds a MD4-signature to the article being posted. Must be called
  99. from gnus-Inews-article-hook."
  100.   (interactive)
  101.  
  102.   (if (null md4-insertion)
  103.       nil
  104.     (let (start-of-body end-of-body sigfile)
  105.  
  106.       ;; .signature handling. may be system specific
  107.       (goto-char (point-max))
  108.       (setq end-of-body (point-marker))
  109.       (if (not gnus-signature-file)    ;skip if gnus-sig set
  110.       (if (file-exists-p
  111.            (setq sigfile (expand-file-name "~/.signature")))
  112.           (progn
  113.         (insert md4-signature-separator)
  114.         (insert-file sigfile))
  115.         (setq sigfile nil)))    ;signal 'no file'
  116.  
  117.       (goto-char (point-min))
  118.       (search-forward "\n\n")
  119.       (setq start-of-body (point-marker))    ; remember where
  120.       
  121.       ;; Run md4 and add the signature.
  122.       (forward-line -1)
  123.       (insert md4-signature-header ": ")
  124.       (insert (md4-signature-region start-of-body (point-max)))
  125.       (insert "\n")
  126.  
  127.       (if sigfile
  128.       (delete-region end-of-body (point-max)))
  129.       )))
  130.  
  131. ;;
  132. ;; Validate MD4 signature. A message is shown with the result.
  133. ;; If the signature does not match, buffer "*MD4 Buffer*" holds more
  134. ;; information.
  135. ;;
  136. (defun md4-validate-signature ()
  137.   "Checks a MD4-signature in the article being read. May be called
  138. from gnus-article-prepare-hook."
  139.   (interactive)
  140.  
  141.   (if (null md4-validation)
  142.       nil
  143.     (let (start-of-body)
  144.       (goto-char (point-min))
  145.       (search-forward "\n\n")
  146.       (setq start-of-body (point-marker))    ; remember where
  147.  
  148.       ;; Check if a signature header is present
  149.       (goto-char (point-min))
  150.       (if (search-forward 
  151.        (concat "\n" md4-signature-header ": ")
  152.        start-of-body t)
  153.       (let (signature (here (point)))
  154.         (forward-line 1)
  155.         (setq signature (buffer-substring here (1- (point))))
  156.  
  157.         ;; Validate
  158.         (if (string= 
  159.          signature
  160.          (md4-signature-region start-of-body (point-max)))
  161.         (progn
  162.           (if (not (equal md4-validation t))
  163.               (message "MD4 signature valid."))
  164.           (bury-buffer md4-buffer))
  165.           (beep)
  166.           (save-excursion
  167.         (set-buffer md4-buffer)
  168.         (goto-char (point-min))
  169.         (insert (message "MD4 signature mismatch!")
  170.             "\nPosted:     " signature
  171.             "\nCalculated: ")
  172.         (goto-char (point-min))))
  173.         )))))
  174.  
  175. (defun md4-signature-region (start end)
  176.   "Calculates MD4 signature."
  177.  
  178.   ;; Get buffer and clear it
  179.   (setq md4-buffer (get-buffer-create "*MD4 Buffer*"))
  180.   (save-excursion
  181.     (set-buffer md4-buffer)
  182.     (erase-buffer))
  183.  
  184.   ;; Run md4
  185.   (call-process-region start end
  186.                md4-command nil md4-buffer nil)
  187.  
  188.   ;; Verify normal result
  189.   (save-excursion
  190.     (set-buffer md4-buffer)
  191.     (if (= (buffer-size) 33)
  192.     (buffer-substring (point-min) (1- (point-max)))
  193.       (error "Unexpected result from %s: %s" md4-command
  194.          (buffer-substring (point-min) (point-max))))))
  195.