1 ;;; nnwarchive.el --- interfacing with web archives
2 ;; Copyright (C) 1999 Free Software Foundation, Inc.
4 ;; Author: Shenghuo Zhu <zsh@cs.rochester.edu>
5 ;; Keywords: news egroups mail-archive
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published
11 ;; by the Free Software Foundation; either version 2, or (at your
12 ;; option) any later version.
14 ;; GNU Emacs is distributed in the hope that it will be useful, but
15 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 ;; General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING. If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
26 ;; Note: You need to have `url' (w3 0.46) or greater version
27 ;; installed for this backend to work.
30 ;; 1. To support more web archives.
31 ;; 2. Generalize webmail to other MHonArc archive.
35 (eval-when-compile (require 'cl))
44 (require 'mail-source)
51 ;; Report failure to find w3 at load time if appropriate.
58 (nnoo-declare nnwarchive)
60 (defvar nnwarchive-type-definition
62 (address . "www.egroups.com")
64 "http://www.egroups.com/register?method=loginAction&email=%s&password=%s"
65 nnwarchive-login nnwarchive-passwd)
67 "http://www.egroups.com/UserGroupsPage?")
68 (list-dissect . nnwarchive-egroups-list)
69 (list-groups . nnwarchive-egroups-list-groups)
71 "http://www.egroups.com/group/%s/?fetchForward=1&start=%d" group aux)
73 "http://www.egroups.com/group/%s/?fetchForward=1" group)
74 (xover-page-size . 13)
75 (xover-dissect . nnwarchive-egroups-xover)
77 "http://www.egroups.com/group/%s/%d.html?raw=1" group article)
78 (article-dissect . nnwarchive-egroups-article)
80 (xover-files . nnwarchive-egroups-xover-files))
82 (address . "www.mail-archive.com")
84 "http://www.mail-archive.com/lists.html")
85 (list-dissect . nnwarchive-mail-archive-list)
86 (list-groups . nnwarchive-mail-archive-list-groups)
88 "http://www.mail-archive.com/%s/mail%d.html" group aux)
90 "http://www.mail-archive.com/%s/maillist.html" group)
91 (xover-dissect . nnwarchive-mail-archive-xover)
93 "http://www.mail-archive.com/%s/msg%05d.html" group article1)
94 (article-dissect . nnwarchive-mail-archive-article)
95 (xover-files . nnwarchive-mail-archive-xover-files)
96 (article-offset . 1))))
98 (defvar nnwarchive-default-type 'egroups)
100 (defvoo nnwarchive-directory (nnheader-concat gnus-directory "warchive/")
101 "Where nnwarchive will save its files.")
103 (defvoo nnwarchive-type nil
104 "The type of nnwarchive.")
106 (defvoo nnwarchive-address ""
107 "The address of nnwarchive.")
109 (defvoo nnwarchive-login nil
110 "Your login name for the group.")
112 (defvoo nnwarchive-passwd nil
113 "Your password for the group.")
115 (defvoo nnwarchive-groups nil)
117 (defvoo nnwarchive-headers-cache nil)
119 (defvoo nnwarchive-authentication nil)
121 (defvoo nnwarchive-nov-is-evil nil)
123 (defconst nnwarchive-version "nnwarchive 1.0")
125 ;;; Internal variables
127 (defvoo nnwarchive-open-url nil)
128 (defvoo nnwarchive-open-dissect nil)
130 (defvoo nnwarchive-list-url nil)
131 (defvoo nnwarchive-list-dissect nil)
132 (defvoo nnwarchive-list-groups nil)
134 (defvoo nnwarchive-xover-files nil)
135 (defvoo nnwarchive-xover-url nil)
136 (defvoo nnwarchive-xover-last-url nil)
137 (defvoo nnwarchive-xover-dissect nil)
138 (defvoo nnwarchive-xover-page-size nil)
140 (defvoo nnwarchive-article-url nil)
141 (defvoo nnwarchive-article-dissect nil)
142 (defvoo nnwarchive-xover-files nil)
143 (defvoo nnwarchive-article-offset 0)
145 (defvoo nnwarchive-buffer nil)
147 (defvoo nnwarchive-keep-backlog 300)
148 (defvar nnwarchive-backlog-articles nil)
149 (defvar nnwarchive-backlog-hashtb nil)
151 (defvoo nnwarchive-headers nil)
154 ;;; Interface functions
156 (nnoo-define-basics nnwarchive)
158 (defun nnwarchive-set-default (type)
159 (let ((defs (cdr (assq type nnwarchive-type-definition)))
162 (set (intern (concat "nnwarchive-" (symbol-name (car def))))
165 (defmacro nnwarchive-backlog (&rest form)
166 `(let ((gnus-keep-backlog nnwarchive-keep-backlog)
168 (format " *nnwarchive backlog %s*" nnwarchive-address))
169 (gnus-backlog-articles nnwarchive-backlog-articles)
170 (gnus-backlog-hashtb nnwarchive-backlog-hashtb))
173 (setq nnwarchive-backlog-articles gnus-backlog-articles
174 nnwarchive-backlog-hashtb gnus-backlog-hashtb))))
175 (put 'nnwarchive-backlog 'lisp-indent-function 0)
176 (put 'nnwarchive-backlog 'edebug-form-spec '(form body))
178 (defun nnwarchive-backlog-enter-article (group number buffer)
180 (gnus-backlog-enter-article group number buffer)))
182 (defun nnwarchive-get-article (article &optional group server buffer)
183 (if (numberp article)
184 (if (nnwarchive-backlog
185 (gnus-backlog-request-article group article
186 (or buffer nntp-server-buffer)))
190 (set-buffer nnwarchive-buffer)
191 (goto-char (point-min))
192 (let ((article1 (- article nnwarchive-article-offset)))
193 (nnwarchive-url nnwarchive-article-url))
194 (setq contents (funcall nnwarchive-article-dissect group article)))
197 (set-buffer (or buffer nntp-server-buffer))
200 (nnwarchive-backlog-enter-article group article (current-buffer))
201 (nnheader-report 'nnwarchive "Fetched article %s" article)
202 (cons group article)))))
205 (deffoo nnwarchive-retrieve-headers (articles &optional group server fetch-old)
206 (nnwarchive-possibly-change-server group server)
207 (if (or gnus-nov-is-evil nnwarchive-nov-is-evil)
209 (with-current-buffer nntp-server-buffer
211 (let ((buf (current-buffer)) b e)
212 (dolist (art articles)
213 (nnwarchive-get-article art group server buf)
214 (setq b (goto-char (point-min)))
215 (if (search-forward "\n\n" nil t)
217 (goto-char (point-max)))
219 (with-current-buffer nntp-server-buffer
220 (insert (format "221 %d Article retrieved.\n" art))
221 (insert-buffer-substring buf b e)
224 (setq nnwarchive-headers (cdr (assoc group nnwarchive-headers-cache)))
226 (set-buffer nnwarchive-buffer)
228 (funcall nnwarchive-xover-files group articles))
230 (set-buffer nntp-server-buffer)
233 (dolist (art articles)
234 (if (setq header (assq art nnwarchive-headers))
235 (nnheader-insert-nov (cdr header))))))
236 (let ((elem (assoc group nnwarchive-headers-cache)))
238 (setcdr elem nnwarchive-headers)
239 (push (cons group nnwarchive-headers) nnwarchive-headers-cache)))
242 (deffoo nnwarchive-request-group (group &optional server dont-check)
243 (nnwarchive-possibly-change-server nil server)
244 (when (and (not dont-check) nnwarchive-list-groups)
245 (funcall nnwarchive-list-groups (list group))
246 (nnwarchive-write-groups))
247 (let ((elem (assoc group nnwarchive-groups)))
250 (nnheader-report 'nnwarchive "Group does not exist"))
252 (nnheader-report 'nnwarchive "Opened group %s" group)
254 "211 %d %d %d %s\n" (or (cadr elem) 0) 1 (or (cadr elem) 0)
255 (prin1-to-string group))
258 (deffoo nnwarchive-request-article (article &optional group server buffer)
259 (nnwarchive-possibly-change-server group server)
260 (nnwarchive-get-article article group server buffer))
262 (deffoo nnwarchive-close-server (&optional server)
263 (when (and (nnwarchive-server-opened server)
264 (gnus-buffer-live-p nnwarchive-buffer))
266 (set-buffer nnwarchive-buffer)
267 (kill-buffer nnwarchive-buffer)))
269 (gnus-backlog-shutdown))
270 (nnoo-close-server 'nnwarchive server))
272 (deffoo nnwarchive-request-list (&optional server)
273 (nnwarchive-possibly-change-server nil server)
275 (set-buffer nnwarchive-buffer)
277 (if nnwarchive-list-url
278 (nnwarchive-url nnwarchive-list-url))
279 (if nnwarchive-list-dissect
280 (funcall nnwarchive-list-dissect))
281 (nnwarchive-write-groups)
282 (nnwarchive-generate-active))
285 (deffoo nnwarchive-open-server (server &optional defs connectionless)
286 (nnwarchive-init server)
287 (if (nnwarchive-server-opened server)
289 (nnoo-change-server 'nnwarchive server defs)
290 (when nnwarchive-authentication
291 (setq nnwarchive-login
294 (format "Login at %s: " server)
296 (setq nnwarchive-passwd
297 (or nnwarchive-passwd
298 (mail-source-read-passwd
299 (format "Password for %s at %s: "
300 nnwarchive-login server)))))
301 (unless nnwarchive-groups
302 (nnwarchive-read-groups))
304 (set-buffer nnwarchive-buffer)
306 (if nnwarchive-open-url
307 (nnwarchive-url nnwarchive-open-url))
308 (if nnwarchive-open-dissect
309 (funcall nnwarchive-open-dissect)))
312 (nnoo-define-skeleton nnwarchive)
314 ;;; Internal functions
316 (defun nnwarchive-possibly-change-server (&optional group server)
317 (nnwarchive-init server)
319 (not (nnwarchive-server-opened server)))
320 (nnwarchive-open-server server)))
322 (defun nnwarchive-read-groups ()
323 (let ((file (expand-file-name (concat "groups-" nnwarchive-address)
324 nnwarchive-directory)))
325 (when (file-exists-p file)
327 (insert-file-contents file)
328 (goto-char (point-min))
329 (setq nnwarchive-groups (read (current-buffer)))))))
331 (defun nnwarchive-write-groups ()
332 (with-temp-file (expand-file-name (concat "groups-" nnwarchive-address)
333 nnwarchive-directory)
334 (prin1 nnwarchive-groups (current-buffer))))
336 (defun nnwarchive-init (server)
337 "Initialize buffers and such."
338 (let ((type (intern server)) (defs nnwarchive-type-definition) def)
341 (setq type nnwarchive-default-type))
342 ((assq type nnwarchive-type-definition) t)
345 (while (setq def (pop defs))
346 (when (equal (cdr (assq 'address (cdr def))) server)
348 (setq type (car def))))
350 (error "Undefined server %s" server))))
351 (setq nnwarchive-type type))
352 (unless (file-exists-p nnwarchive-directory)
353 (gnus-make-directory nnwarchive-directory))
354 (unless (gnus-buffer-live-p nnwarchive-buffer)
355 (setq nnwarchive-buffer
357 (nnheader-set-temp-buffer
358 (format " *nnwarchive %s %s*" nnwarchive-type server)))))
359 (nnwarchive-set-default nnwarchive-type))
361 (defun nnwarchive-encode-www-form-urlencoded (pairs)
362 "Return PAIRS encoded for forms."
366 (concat (w3-form-encode-xwfu (car data)) "="
367 (w3-form-encode-xwfu (cdr data)))))
370 (defun nnwarchive-fetch-form (url pairs)
371 (let ((url-request-data (nnwarchive-encode-www-form-urlencoded pairs))
372 (url-request-method "POST")
373 (url-request-extra-headers
374 '(("Content-type" . "application/x-www-form-urlencoded"))))
378 (defun nnwarchive-eval (expr)
381 (cons (nnwarchive-eval (car expr)) (nnwarchive-eval (cdr expr))))
387 (defun nnwarchive-url (xurl)
388 (let ((url-confirmation-func 'identity))
390 ((eq (car xurl) 'post)
392 (nnwarchive-fetch-form (car xurl) (nnwarchive-eval (cdr xurl))))
394 (nnweb-insert (apply 'format (nnwarchive-eval xurl)))))))
396 (defun nnwarchive-generate-active ()
398 (set-buffer nntp-server-buffer)
400 (dolist (elem nnwarchive-groups)
401 (insert (prin1-to-string (car elem))
402 " " (number-to-string (or (cadr elem) 0)) " 1 y\n"))))
404 (defun nnwarchive-paged (articles)
405 (let (art narts next)
406 (while (setq art (pop articles))
407 (when (and (>= art (or next 0))
408 (not (assq art nnwarchive-headers)))
410 (setq next (+ art nnwarchive-xover-page-size))))
415 (defun nnwarchive-egroups-list-groups (groups)
418 (set-buffer nnwarchive-buffer)
419 (dolist (group groups)
421 (nnwarchive-url nnwarchive-xover-last-url)
422 (goto-char (point-min))
423 (when (re-search-forward "of \\([0-9]+\\)</title>" nil t)
424 (setq articles (string-to-number (match-string 1))))
425 (let ((elem (assoc group nnwarchive-groups)))
427 (setcar (cdr elem) articles)
428 (push (list group articles "") nnwarchive-groups)))
429 (setq nnwarchive-headers (cdr (assoc group nnwarchive-headers-cache)))
430 (nnwarchive-egroups-xover group)
431 (let ((elem (assoc group nnwarchive-headers-cache)))
433 (setcdr elem nnwarchive-headers)
434 (push (cons group nnwarchive-headers) nnwarchive-headers-cache)))))))
436 (defun nnwarchive-egroups-list ()
437 (let ((case-fold-search t)
438 group description elem articles)
439 (goto-char (point-min))
442 "/group/\\([^/]+\\)/info\\.html[^>]+>[^>]+>[\040\t]*-[\040\t]*\\([^<]+\\)<"
444 (setq group (match-string 1)
445 description (match-string 2))
447 (when (re-search-forward ">\\([0-9]+\\)<" nil t)
448 (setq articles (string-to-number (match-string 1))))
449 (if (setq elem (assoc group nnwarchive-groups))
450 (setcar (cdr elem) articles)
451 (push (list group articles description) nnwarchive-groups))))
454 (defun nnwarchive-egroups-xover (group)
455 (let (article subject from date)
456 (goto-char (point-min))
457 (while (re-search-forward
458 "<a href=\"/group/\\([^/]+\\)/\\([0-9]+\\)\\.html[^>]+>\\([^<]+\\)<"
460 (setq group (match-string 1)
461 article (string-to-number (match-string 2))
462 subject (match-string 3))
464 (unless (assq article nnwarchive-headers)
465 (if (looking-at "<td[^>]+><font[^>]+>\\([^<]+\\)</font>")
466 (setq from (match-string 1)))
468 (if (looking-at "<td[^>]+><font[^>]+>\\([^<]+\\)</font>")
469 (setq date (identity (match-string 1))))
472 (make-full-mail-header
474 (nnweb-decode-entities-string subject)
475 (nnweb-decode-entities-string from)
477 (concat "<" group "%"
478 (number-to-string article)
481 0 0 "")) nnwarchive-headers))))
484 (defun nnwarchive-egroups-article (group articles)
485 (goto-char (point-min))
486 (if (search-forward "<pre>" nil t)
487 (delete-region (point-min) (point)))
488 (goto-char (point-max))
489 (if (search-backward "</pre>" nil t)
490 (delete-region (point) (point-max)))
491 (goto-char (point-min))
492 (while (re-search-forward "<a[^>]+>\\([^<]+\\)</a>" nil t)
493 (replace-match "<\\1>"))
494 (nnweb-decode-entities)
497 (defun nnwarchive-egroups-xover-files (group articles)
499 (setq auxs (nnwarchive-paged (sort articles '<)))
500 (while (setq aux (pop auxs))
501 (goto-char (point-max))
502 (nnwarchive-url nnwarchive-xover-url))
503 (if nnwarchive-xover-dissect
504 (nnwarchive-egroups-xover group))))
508 (defun nnwarchive-mail-archive-list-groups (groups)
511 (set-buffer nnwarchive-buffer)
512 (dolist (group groups)
514 (nnwarchive-url nnwarchive-xover-last-url)
515 (goto-char (point-min))
516 (when (re-search-forward "msg\\([0-9]+\\)\\.html" nil t)
517 (setq articles (1+ (string-to-number (match-string 1)))))
518 (let ((elem (assoc group nnwarchive-groups)))
520 (setcar (cdr elem) articles)
521 (push (list group articles "") nnwarchive-groups)))
522 (setq nnwarchive-headers (cdr (assoc group nnwarchive-headers-cache)))
523 (nnwarchive-mail-archive-xover group)
524 (let ((elem (assoc group nnwarchive-headers-cache)))
526 (setcdr elem nnwarchive-headers)
527 (push (cons group nnwarchive-headers)
528 nnwarchive-headers-cache)))))))
530 (defun nnwarchive-mail-archive-list ()
531 (let ((case-fold-search t)
532 group description elem articles)
533 (goto-char (point-min))
534 (while (re-search-forward "<a href=\"\\([^/]+\\)/\">\\([^>]+\\)<" nil t)
535 (setq group (match-string 1)
536 description (match-string 2))
539 (if (setq elem (assoc group nnwarchive-groups))
540 (setcar (cdr elem) articles)
541 (push (list group articles description) nnwarchive-groups))))
544 (defun nnwarchive-mail-archive-xover (group)
545 (let (article subject from date)
546 (goto-char (point-min))
547 (while (re-search-forward
548 "<A[^>]*HREF=\"msg\\([0-9]+\\)\\.html[^>]+>\\([^<]+\\)<"
550 (setq article (1+ (string-to-number (match-string 1)))
551 subject (match-string 2))
553 (unless (assq article nnwarchive-headers)
554 (if (looking-at "<UL><LI><EM>From</EM>:\\([^&]+\\)<\\([^&]+\\)>")
556 (setq from (match-string 1)
557 date (identity (match-string 2))))
558 (setq from "" date ""))
561 (make-full-mail-header
563 (nnweb-decode-entities-string subject)
564 (nnweb-decode-entities-string from)
566 (format "<%05d%%%s>\n" (1- article) group)
568 0 0 "")) nnwarchive-headers))))
571 (defun nnwarchive-mail-archive-xover-files (group articles)
572 (unless nnwarchive-headers
574 (nnwarchive-url nnwarchive-xover-last-url)
575 (goto-char (point-min))
576 (nnwarchive-mail-archive-xover group))
577 (let ((minart (apply 'min articles))
578 (min (apply 'min (mapcar 'car nnwarchive-headers)))
580 (while (> min minart)
582 (nnwarchive-url nnwarchive-xover-url)
583 (nnwarchive-mail-archive-xover group)
584 (setq min (apply 'min (mapcar 'car nnwarchive-headers))))))
586 (defvar nnwarchive-caesar-translation-table nil
587 "Modified rot13 table. tr/@A-Z[a-z/N-Z[@A-Mn-za-m/.")
589 (defun nnwarchive-make-caesar-translation-table ()
590 "Create modified rot13 table. tr/@A-Z[a-z/N-Z[@A-Mn-za-m/."
592 (table (make-string 256 0))
594 (A (mm-char-int ?A)))
595 (while (< (incf i) 256)
598 (substring table 0 (1- A))
599 (substring table (+ A 13) (+ A 27))
600 (substring table (1- A) (+ A 13))
601 (substring table (+ A 27) a)
602 (substring table (+ a 13) (+ a 26))
603 (substring table a (+ a 13))
604 (substring table (+ a 26) 255))))
606 (defun nnwarchive-from-r13 (from-r13)
610 (let ((message-caesar-translation-table
611 (or nnwarchive-caesar-translation-table
612 (setq nnwarchive-caesar-translation-table
613 (nnwarchive-make-caesar-translation-table)))))
614 (message-caesar-region (point-min) (point-max))
617 (defun nnwarchive-mail-archive-article (group article)
618 (let (p refs url mime e
623 (goto-char (point-min))
624 (when (search-forward "X-Head-End" nil t)
626 (narrow-to-region (point-min) (point))
627 (nnweb-decode-entities)
628 (goto-char (point-min))
629 (while (search-forward "<!--X-" nil t)
631 (goto-char (point-min))
632 (while (search-forward " -->" nil t)
635 (or (mail-fetch-field "from")
637 (mail-fetch-field "from-r13"))))
638 (setq date (mail-fetch-field "date"))
639 (setq id (mail-fetch-field "message-id"))
640 (setq subject (mail-fetch-field "subject"))
641 (goto-char (point-max))
643 (when (search-forward "<ul>" nil t)
645 (delete-region (point-min) (point))
646 (search-forward "</ul>" nil t)
648 (narrow-to-region (point-min) (point))
649 (nnweb-remove-markup)
650 (nnweb-decode-entities)
651 (goto-char (point-min))
654 (message-remove-header "from")
655 (goto-char (point-max))
656 (insert "From: " from "\n"))
658 (message-remove-header "subject")
659 (goto-char (point-max))
660 (insert "Subject: " subject "\n"))
662 (goto-char (point-max))
663 (insert "X-Message-ID: <" id ">\n"))
665 (message-remove-header "date")
666 (goto-char (point-max))
667 (insert "Date: " date "\n"))
668 (goto-char (point-max))
672 (when (search-forward "X-Body-of-Message" nil t)
674 (delete-region p (point))
675 (search-forward "X-Body-of-Message-End" nil t)
678 (narrow-to-region p (point))
679 (goto-char (point-min))
680 (if (> (skip-chars-forward "\040\n\r\t") 0)
681 (delete-region (point-min) (point)))
684 ((looking-at "<PRE>\r?\n?")
685 (delete-region (match-beginning 0) (match-end 0))
687 (when (search-forward "</PRE>" nil t)
688 (delete-region (match-beginning 0) (match-end 0))
690 (narrow-to-region p (point))
691 (nnweb-remove-markup)
692 (nnweb-decode-entities)
693 (goto-char (point-max)))))
694 ((looking-at "<P><A HREF=\"\\([^\"]+\\)")
695 (setq url (match-string 1))
696 (delete-region (match-beginning 0)
697 (progn (forward-line) (point)))
698 ;; I hate to download the url encode it, then immediately
700 ;; FixMe: Find a better solution to attach the URL.
701 ;; Maybe do some hack in external part of mml-generate-mim-1.
704 (format "<URL:http://www.mail-archive.com/%s/%s>"
711 (insert "<#part type=\"text/html\" disposition=inline>")
713 (if (re-search-forward
714 "[\040\n\r\t]*<PRE>\\|[\040\n\r\t]*<P><A HREF=\""
721 (if (> (skip-chars-forward "\040\n\r\t") 0)
722 (delete-region p (point))))
723 (goto-char (point-max))))
725 (when (search-forward "X-References-End" nil t)
728 (search-backward "X-References" p t)
729 (while (re-search-forward "msg\\([0-9]+\\)\\.html" e t)
730 (push (concat "<" (match-string 1) "%" group ">") refs)))
731 (delete-region p (point-max))
732 (goto-char (point-min))
733 (insert (format "Message-ID: <%05d%%%s>\n" (1- article) group))
735 (insert "References:")
737 (insert " " (pop refs)))
740 (unless (looking-at "$")
741 (search-forward "\n\n" nil t)
743 (narrow-to-region (point) (point-max))
744 (insert "MIME-Version: 1.0\n"
747 (delete-region (point-min) (point-max))))
751 (provide 'nnwarchive)
753 ;;; nnwarchive.el ends here