*** empty log message ***
[gnus] / lisp / gnus-nocem.el
index 33d7bd8..ce78e1d 100644 (file)
@@ -1,5 +1,5 @@
 ;;; gnus-nocem.el --- NoCeM pseudo-cancellation treatment
-;; Copyright (C) 1995 Free Software Foundation, Inc.
+;; Copyright (C) 1995,96 Free Software Foundation, Inc.
 
 ;; Author: Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
 ;; Keywords: news
 ;; 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.
+;; along with GNU Emacs; see the file COPYING.  If not, write to the
+;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+;; Boston, MA 02111-1307, USA.
 
 ;;; Commentary:
 
 ;;; Code:
 
-(require 'gnus)
-(eval-when-compile 
-  (require 'cl))
+(require 'gnus-load)
+(require 'nnmail)
+(require 'gnus-art)
+(require 'gnus-range)
 
-(defvar gnus-nocem-groups '("alt.nocem.misc" "news.admin.net-abuse.announce")
+(defvar gnus-nocem-groups 
+  '("alt.nocem.misc" "news.admin.net-abuse.announce")
   "*List of groups that will be searched for NoCeM messages.")
 
-(defvar gnus-nocem-issuers '("Automoose-1" "clewis@ferret.ocunix.on.ca;")
+(defvar gnus-nocem-issuers 
+ '("AutoMoose-1" "Automoose-1"   ; CancelMoose[tm]
+   "rbraver@ohww.norman.ok.us"   ; Robert Braver
+   "clewis@ferret.ocunix.on.ca;" ; Chris Lewis
+   "jem@xpat.com;"              ; Despammer from Korea
+   "snowhare@xmission.com"       ; Benjamin "Snowhare" Franz
+   "red@redpoll.mrfs.oh.us (Richard E. Depew)"
+   )
   "*List of NoCeM issuers to pay attention to.")
 
 (defvar gnus-nocem-directory 
-  (concat (file-name-as-directory gnus-article-save-directory) "NoCeM/")
+  (nnheader-concat gnus-article-save-directory "NoCeM/")
   "*Directory where NoCeM files will be stored.")
 
-(defvar gnus-nocem-expiry-wait 30
+(defvar gnus-nocem-expiry-wait 15
   "*Number of days to keep NoCeM headers in the cache.")
 
+(defvar gnus-nocem-verifyer 'mc-verify
+  "*Function called to verify that the NoCeM message is valid.
+One likely value is `mc-verify'.  If the function in this variable
+isn't bound, the message will be used unconditionally.")
+
 ;;; Internal variables
 
 (defvar gnus-nocem-active nil)
 (defvar gnus-nocem-alist nil)
 (defvar gnus-nocem-touched-alist nil)
+(defvar gnus-nocem-hashtb nil)
 
 ;;; Functions
 
@@ -60,8 +76,7 @@
   (interactive)
   (let ((groups gnus-nocem-groups)
        group active gactive articles)
-    (or (file-exists-p gnus-nocem-directory)
-       (make-directory gnus-nocem-directory t))
+    (gnus-make-directory gnus-nocem-directory)
     ;; Load any previous NoCeM headers.
     (gnus-nocem-load-cache)
     ;; Read the active file if it hasn't been read yet.
           (error nil)))
     ;; Go through all groups and see whether new articles have
     ;; arrived.  
-    (while groups
-      (setq group (pop groups))
-      (if (not (gnus-activate-group group))
-         () ; This group doesn't exist.
+    (while (setq group (pop groups))
+      (if (not (setq gactive (gnus-activate-group group)))
+         ()                            ; This group doesn't exist.
        (setq active (nth 1 (assoc group gnus-nocem-active)))
        (when (and (not (< (cdr gactive) (car gactive))) ; Empty group.
                   (or (not active)
-                      (< (cdr active) 
-                         (cdr (setq gactive (gnus-gethash 
-                                             group gnus-newsrc-hashtb))))))
+                      (< (cdr active) (cdr gactive))))
          ;; Ok, there are new articles in this group, se we fetch the
          ;; headers.
-         (let ((gnus-newsgroup-dependencies (make-vector 10))
-               headers)
-           (setq headers
-                 (if (eq 'nov
-                         (gnus-retrieve-headers 
-                          (setq articles
-                                (gnus-uncompress-range
-                                 (cons (1+ (cdr active)) (cdr gactive))))
-                          group))
-                     (gnus-get-newsgroup-headers-xover articles)
-                   (gnus-get-newsgroup-headers)))
-           (while headers
-             ;; We take a closer look on all articles that have
-             ;; "@@NCM" in the subject.  
-             (and (string-match "@@NCM" (mail-header-subject (car headers)))
-                  (gnus-nocem-check-article
-                   (mail-header-number (car headers)) group))
-             (setq headers (cdr headers)))))))
+         (save-excursion
+           (let ((dependencies (make-vector 10 nil))
+                 headers)
+             (nnheader-temp-write nil
+               (setq headers
+                     (if (eq 'nov
+                             (gnus-retrieve-headers 
+                              (setq articles
+                                    (gnus-uncompress-range
+                                     (cons 
+                                      (if active (1+ (cdr active)) 
+                                        (car gactive))
+                                      (cdr gactive))))
+                              group))
+                         (gnus-get-newsgroup-headers-xover 
+                          articles nil dependencies)
+                       (gnus-get-newsgroup-headers dependencies)))
+               (while headers
+                 ;; We take a closer look on all articles that have
+                 ;; "@@NCM" in the subject.  
+                 (when (string-match "@@NCM"
+                                     (mail-header-subject (car headers)))
+                   (gnus-nocem-check-article group (car headers)))
+                 (setq headers (cdr headers)))))))
+       (setq gnus-nocem-active
+             (cons (list group gactive) 
+                   (delq (assoc group gnus-nocem-active)
+                         gnus-nocem-active)))))
     ;; Save the results, if any.
-    (gnus-nocem-save-cache)))
-
-(defun gnus-nocem-check-article (number group)
-  "Check whether the current article is a NCM article and that we want it."
-  (save-excursion
-    (set-buffer nntp-server-buffer)
-    ;; Get the article.
-    (gnus-request-article-this-buffer number group)
-    (goto-char (point-min))
-    (let (issuer b)
+    (gnus-nocem-save-cache)
+    (gnus-nocem-save-active)))
+
+(defun gnus-nocem-check-article (group header)
+  "Check whether the current article is an NCM article and that we want it."
+  ;; Get the article.
+  (gnus-message 7 "Checking article %d in %s for NoCeM..."
+               (mail-header-number header) group)
+  (let ((date (mail-header-date header))
+       issuer b e)
+    (when (or (not date)
+             (nnmail-time-less 
+              (nnmail-time-since (nnmail-date-to-time date))
+              (nnmail-days-to-time gnus-nocem-expiry-wait)))
+      (gnus-request-article-this-buffer (mail-header-number header) group)
+      (goto-char (point-min))
+      (when (re-search-forward "-----BEGIN PGP MESSAGE-----" nil t)
+       (delete-region (point-min) (match-beginning 0)))
+      (when (re-search-forward "-----END PGP MESSAGE-----\n?" nil t)
+       (delete-region (match-end 0) (point-max)))
+      (goto-char (point-min))
       ;; The article has to have proper NoCeM headers.
       (when (and (setq b (search-forward "\n@@BEGIN NCM HEADERS\n" nil t))
-                (search-forward "\n@@BEGIN NCM BODY\n" nil t))
+                (setq e (search-forward "\n@@BEGIN NCM BODY\n" nil t)))
        ;; We get the name of the issuer.
-       (narrow-to-region b (match-beginning 0))
+       (narrow-to-region b e)
        (setq issuer (mail-fetch-field "issuer"))
+       (widen)
        (and (member issuer gnus-nocem-issuers) ; We like her...
-            (gnus-nocem-verify-issuer issuer) ; She is who she says she is...
-            (gnus-nocem-enter-article)))) ; We gobble the message.
-    (widen)))
-
+            (gnus-nocem-verify-issuer issuer) ; She is who she says she is..
+            (gnus-nocem-enter-article)))))) ; We gobble the message.
+  
 (defun gnus-nocem-verify-issuer (person)
   "Verify using PGP that the canceler is who she says she is."
-  t)
+  (if (fboundp gnus-nocem-verifyer)
+      (funcall gnus-nocem-verifyer)
+    ;; If we don't have Mailcrypt, then we use the message anyway.
+    t))
 
 (defun gnus-nocem-enter-article ()
   "Enter the current article into the NoCeM cache."
-  (widen)
   (goto-char (point-min))
   (let ((b (search-forward "\n@@BEGIN NCM BODY\n" nil t))
        (e (search-forward "\n@@END NCM BODY\n" nil t))
       (narrow-to-region b (1+ (match-beginning 0)))
       (goto-char (point-min))
       (while (search-forward "\t" nil t)
-       (when (boundp (let ((obarray gnus-newsrc-hashtb)) (read buf)))
+       (when (condition-case nil
+                 (boundp (let ((obarray gnus-active-hashtb)) (read buf)))
+               (error nil))
          (beginning-of-line)
          (while (= (following-char) ?\t)
            (forward-line -1))
            (forward-line 1))))
       (when ncm
        (setq gnus-nocem-touched-alist t)
-       (push (push (current-time-string) ncm) gnus-nocem-alist)))))
+       (push (cons (let ((time (current-time))) (setcdr (cdr time) nil) time)
+                   ncm) 
+             gnus-nocem-alist)))))
 
 (defun gnus-nocem-load-cache ()
   "Load the NoCeM cache."
-  (if gnus-nocem-alist
-      () ; Do nothing.
+  (unless gnus-nocem-alist
     ;; The buffer doesn't exist, so we create it and load the NoCeM
     ;; cache.  
     (when (file-exists-p (gnus-nocem-cache-file))
   "Save the NoCeM cache."
   (when (and gnus-nocem-alist
             gnus-nocem-touched-alist)
-    (save-excursion
-      (nnheader-set-temp-buffer " *NoCeM*")
-      (insert (prin1-to-string
-              (list 'setq 'gnus-nocem-alist gnus-nocem-alist)))
-      (write-region (point-min) (point-max) 
-                   (gnus-nocem-cache-file) nil 'silent)
-      (kill-buffer (current-buffer))
-      (setq gnus-nocem-touched-alist nil))))
+    (nnheader-temp-write (gnus-nocem-cache-file)
+      (gnus-prin1 `(setq gnus-nocem-alist ',gnus-nocem-alist)))
+    (setq gnus-nocem-touched-alist nil)))
+
+(defun gnus-nocem-save-active ()
+  "Save the NoCeM active file."
+  (nnheader-temp-write (gnus-nocem-active-file)
+    (gnus-prin1 `(setq gnus-nocem-active ',gnus-nocem-active))))
 
 (defun gnus-nocem-alist-to-hashtb ()
   "Create a hashtable from the Message-IDs we have."
-  (let ((alist gnus-nocem-alist)
-       (date (current-time-string))
-       entry)
-    (setq gnus-nocem-hashtb (* (length alist) 51))
-    (while alist
-      (setq entry (pop alist))
-      (if (> (gnus-days-between date (car entry)) gnus-nocem-expiry-wait)
+  (let* ((alist gnus-nocem-alist)
+        (pprev (cons nil alist))
+        (prev pprev)
+        (expiry (nnmail-days-to-time gnus-nocem-expiry-wait))
+        entry)
+    (setq gnus-nocem-hashtb (gnus-make-hashtable (* (length alist) 51)))
+    (while (setq entry (car alist))
+      (if (not (nnmail-time-less (nnmail-time-since (car entry)) expiry))
          ;; This entry has expired, so we remove it.
-         (setq gnus-nocem-alist (delq entry gnus-nocem-alist))
+         (setcdr prev (cdr alist))
+       (setq prev alist)
        ;; This is ok, so we enter it into the hashtable.
        (setq entry (cdr entry))
        (while entry
          (gnus-sethash (car entry) t gnus-nocem-hashtb)
-         (setq entry (cdr entry)))))))
+         (setq entry (cdr entry))))
+      (setq alist (cdr alist)))))
+
+(gnus-add-shutdown 'gnus-nocem-close 'gnus)
 
 (defun gnus-nocem-close ()
   "Clear internal NoCeM variables."
        gnus-nocem-active nil
        gnus-nocem-touched-alist nil))
 
+(defun gnus-nocem-unwanted-article-p (id)
+  "Say whether article ID in the current group is wanted."
+  (gnus-gethash id gnus-nocem-hashtb))
+
+(provide 'gnus-nocem)
+
 ;;; gnus-nocem.el ends here