1 ;;; nnwarchive.el --- interfacing with web archives
2 ;; Copyright (C) 1999 Free Software Foundation, Inc.
4 ;; Author: Shenghuo Zhu <zsh@cs.rochester.edu>
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' and `w3' 0.46 or greater version
27 ;; installed for this backend to work.
29 ;; A lot of codes stolen from mail-source, nnslashdot, nnweb.
32 ;; 1. To support more web archives.
33 ;; 2. Support nnwarchive-xover-is-evil.
34 ;; 3. Generalize webmail to other MHonArc archive.
38 (eval-when-compile (require 'cl))
46 (require 'mail-source)
53 ;; Report failure to find w3 at load time if appropriate.
60 (nnoo-declare nnwarchive)
62 (defvar nnwarchive-type-definition
64 (address . "www.egroups.com")
66 "http://www.egroups.com/register?method=loginAction&email=%s&password=%s"
67 nnwarchive-login nnwarchive-passwd)
69 "http://www.egroups.com/UserGroupsPage?")
70 (list-dissect . nnwarchive-egroups-list)
71 (list-groups . nnwarchive-egroups-list-groups)
73 "http://www.egroups.com/group/%s/?fetchForward=1&start=%d" group aux)
75 "http://www.egroups.com/group/%s/?fetchForward=1" group)
76 (xover-page-size . 13)
77 (xover-dissect . nnwarchive-egroups-xover)
79 "http://www.egroups.com/group/%s/%d.html?raw=1" group article)
80 (article-dissect . nnwarchive-egroups-article)
82 (xover-files . nnwarchive-egroups-xover-files))
84 (address . "www.mail-archive.com")
86 "http://www.mail-archive.com/lists.html")
87 (list-dissect . nnwarchive-mail-archive-list)
88 (list-groups . nnwarchive-mail-archive-list-groups)
90 "http://www.mail-archive.com/%s/mail%d.html" group aux)
92 "http://www.mail-archive.com/%s/maillist.html" group)
93 (xover-dissect . nnwarchive-mail-archive-xover)
95 "http://www.mail-archive.com/%s/msg%05d.html" group article1)
96 (article-dissect . nnwarchive-mail-archive-article)
97 (xover-files . nnwarchive-mail-archive-xover-files)
98 (article-offset . 1))))
100 (defvar nnwarchive-default-type 'egroups)
102 (defvoo nnwarchive-directory (nnheader-concat gnus-directory "warchive/")
103 "Where nnwarchive will save its files.")
105 (defvoo nnwarchive-type nil
106 "The type of nnwarchive.")
108 (defvoo nnwarchive-address ""
109 "The address of nnwarchive.")
111 (defvoo nnwarchive-login nil
112 "Your login name for the group.")
114 (defvoo nnwarchive-passwd nil
115 "Your password for the group.")
117 (defvoo nnwarchive-groups nil)
119 (defvoo nnwarchive-headers-cache nil)
121 (defvoo nnwarchive-opened nil)
123 (defvoo nnwarchive-authentication nil)
125 (defvoo nnwarchive-xover-is-evil nil) ;; not implemented
127 (defconst nnwarchive-version "nnwarchive 1.0")
129 ;;; Internal variables
131 (defvoo nnwarchive-open-url nil)
132 (defvoo nnwarchive-open-dissect nil)
134 (defvoo nnwarchive-list-url nil)
135 (defvoo nnwarchive-list-dissect nil)
136 (defvoo nnwarchive-list-groups nil)
138 (defvoo nnwarchive-xover-files nil)
139 (defvoo nnwarchive-xover-url nil)
140 (defvoo nnwarchive-xover-last-url nil)
141 (defvoo nnwarchive-xover-dissect nil)
142 (defvoo nnwarchive-xover-page-size nil)
144 (defvoo nnwarchive-article-url nil)
145 (defvoo nnwarchive-article-dissect nil)
146 (defvoo nnwarchive-xover-files nil)
147 (defvoo nnwarchive-article-offset 0)
149 (defvoo nnwarchive-buffer nil)
151 (defvar 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 (deffoo nnwarchive-retrieve-headers (articles &optional group server fetch-old)
166 (nnwarchive-possibly-change-server group server)
167 (setq nnwarchive-headers (cdr (assoc group nnwarchive-headers-cache)))
169 (set-buffer nnwarchive-buffer)
171 (funcall nnwarchive-xover-files group articles))
173 (set-buffer nntp-server-buffer)
176 (dolist (art articles)
177 (if (setq header (assq art nnwarchive-headers))
178 (nnheader-insert-nov (cdr header))))))
179 (let ((elem (assoc group nnwarchive-headers-cache)))
181 (setcdr elem nnwarchive-headers)
182 (push (cons group nnwarchive-headers) nnwarchive-headers-cache)))
185 (deffoo nnwarchive-retrieve-groups (groups &optional server)
186 "Retrieve group info on GROUPS."
187 (nnwarchive-possibly-change-server nil server)
188 (if nnwarchive-list-groups
189 (funcall nnwarchive-list-groups groups))
190 (nnwarchive-write-groups)
191 (nnwarchive-generate-active)
194 (deffoo nnwarchive-request-group (group &optional server dont-check)
195 (nnwarchive-possibly-change-server nil server)
196 (if nnwarchive-list-groups
197 (funcall nnwarchive-list-groups (list group)))
198 (nnwarchive-write-groups)
199 (let ((elem (assoc group nnwarchive-groups)))
202 (nnheader-report 'nnwarchive "Group does not exist"))
204 (nnheader-report 'nnwarchive "Opened group %s" group)
206 "211 %d %d %d %s\n" (or (cadr elem) 0) 1 (or (cadr elem) 0)
207 (prin1-to-string group))
210 (deffoo nnwarchive-close-group (group &optional server)
211 (nnwarchive-possibly-change-server group server)
212 (when (gnus-buffer-live-p nnwarchive-buffer)
214 (set-buffer nnwarchive-buffer)
215 (kill-buffer nnwarchive-buffer)))
218 (deffoo nnwarchive-request-article (article &optional group server buffer)
219 (nnwarchive-possibly-change-server group server)
222 (set-buffer nnwarchive-buffer)
223 (goto-char (point-min))
224 (let ((article1 (- article nnwarchive-article-offset)))
225 (nnwarchive-url nnwarchive-article-url))
226 (setq contents (funcall nnwarchive-article-dissect group article)))
229 (set-buffer (or buffer nntp-server-buffer))
232 (nnheader-report 'nnwarchive "Fetched article %s" article)
233 (cons group article)))))
235 (deffoo nnwarchive-close-server (&optional server)
236 (when (and (nnwarchive-server-opened server)
237 (gnus-buffer-live-p nnwarchive-buffer))
239 (set-buffer nnwarchive-buffer)
240 (kill-buffer nnwarchive-buffer)))
241 (nnoo-close-server 'nnwarchive server))
243 (deffoo nnwarchive-request-list (&optional server)
244 (nnwarchive-possibly-change-server nil server)
246 (set-buffer nnwarchive-buffer)
248 (if nnwarchive-list-url
249 (nnwarchive-url nnwarchive-list-url))
250 (if nnwarchive-list-dissect
251 (funcall nnwarchive-list-dissect))
252 (nnwarchive-write-groups)
253 (nnwarchive-generate-active))
256 (deffoo nnwarchive-request-newgroups (date &optional server)
257 (nnwarchive-possibly-change-server nil server)
258 (nnwarchive-write-groups)
259 (nnwarchive-generate-active)
262 (deffoo nnwarchive-asynchronous-p ()
265 (deffoo nnwarchive-server-opened (&optional server)
268 (deffoo nnwarchive-open-server (server &optional defs connectionless)
269 (nnwarchive-init server)
270 (unless (nnwarchive-server-opened server)
271 (when nnwarchive-authentication
272 (setq nnwarchive-login
275 (format "Login at %s: " server)
277 (setq nnwarchive-passwd
278 (or nnwarchive-passwd
279 (mail-source-read-passwd
280 (format "Password for %s at %s: "
281 nnwarchive-login server)))))
282 (unless nnwarchive-groups
283 (nnwarchive-read-groups))
285 (set-buffer nnwarchive-buffer)
287 (if nnwarchive-open-url
288 (nnwarchive-url nnwarchive-open-url))
289 (if nnwarchive-open-dissect
290 (funcall nnwarchive-open-dissect))
291 (setq nnwarchive-opened t)))
294 (nnoo-define-skeleton nnwarchive)
296 ;;; Internal functions
298 (defun nnwarchive-possibly-change-server (&optional group server)
299 (nnwarchive-init server)
301 (not (nnwarchive-server-opened server)))
302 (nnwarchive-open-server server)))
304 (defun nnwarchive-read-groups ()
305 (let ((file (expand-file-name (concat "groups-" nnwarchive-address)
306 nnwarchive-directory)))
307 (when (file-exists-p file)
309 (insert-file-contents file)
310 (goto-char (point-min))
311 (setq nnwarchive-groups (read (current-buffer)))))))
313 (defun nnwarchive-write-groups ()
314 (with-temp-file (expand-file-name (concat "groups-" nnwarchive-address)
315 nnwarchive-directory)
316 (prin1 nnwarchive-groups (current-buffer))))
318 (defun nnwarchive-init (server)
319 "Initialize buffers and such."
320 (let ((type (intern server)) (defs nnwarchive-type-definition) def)
323 (setq type nnwarchive-default-type))
324 ((assq type nnwarchive-type-definition) t)
327 (while (setq def (pop defs))
328 (when (equal (cdr (assq 'address (cdr def))) server)
330 (setq type (car def))))
332 (error "Undefined server %s" server))))
333 (setq nnwarchive-type type))
334 (unless (file-exists-p nnwarchive-directory)
335 (gnus-make-directory nnwarchive-directory))
336 (unless (gnus-buffer-live-p nnwarchive-buffer)
337 (setq nnwarchive-buffer
339 (nnheader-set-temp-buffer
340 (format " *nnwarchive %s %s*" nnwarchive-type server)))))
341 (nnwarchive-set-default nnwarchive-type))
343 (defun nnwarchive-encode-www-form-urlencoded (pairs)
344 "Return PAIRS encoded for forms."
348 (concat (w3-form-encode-xwfu (car data)) "="
349 (w3-form-encode-xwfu (cdr data)))))
352 (defun nnwarchive-fetch-form (url pairs)
353 (let ((url-request-data (nnwarchive-encode-www-form-urlencoded pairs))
354 (url-request-method "POST")
355 (url-request-extra-headers
356 '(("Content-type" . "application/x-www-form-urlencoded"))))
360 (defun nnwarchive-eval (expr)
363 (cons (nnwarchive-eval (car expr)) (nnwarchive-eval (cdr expr))))
369 (defun nnwarchive-url (xurl)
370 (let ((url-confirmation-func 'identity))
372 ((eq (car xurl) 'post)
374 (nnwarchive-fetch-form (car xurl) (nnwarchive-eval (cdr xurl))))
376 (nnweb-insert (apply 'format (nnwarchive-eval xurl)))))))
378 (defun nnwarchive-decode-entities ()
379 (goto-char (point-min))
380 (while (re-search-forward "&\\(#[0-9]+\\|[a-z]+\\);" nil t)
381 (replace-match (char-to-string
382 (if (eq (aref (match-string 1) 0) ?\#)
383 (string-to-number (substring (match-string 1) 1))
384 (or (cdr (assq (intern (match-string 1))
389 (defun nnwarchive-decode-entities-string (str)
392 (nnwarchive-decode-entities)
393 (buffer-substring (point-min) (point-max))))
395 (defun nnwarchive-remove-markup ()
396 (goto-char (point-min))
397 (while (search-forward "<!--" nil t)
398 (delete-region (match-beginning 0)
399 (or (search-forward "-->" nil t)
401 (goto-char (point-min))
402 (while (re-search-forward "<[^>]+>" nil t)
403 (replace-match "" t t)))
405 (defun nnwarchive-date-to-date (sdate)
406 (let ((elem (split-string sdate)))
407 (concat (substring (nth 0 elem) 0 3) " "
408 (substring (nth 1 elem) 0 3) " "
409 (substring (nth 2 elem) 0 2) " "
410 (substring (nth 3 elem) 1 6) " "
411 (format-time-string "%Y") " "
414 (defun nnwarchive-generate-active ()
416 (set-buffer nntp-server-buffer)
418 (dolist (elem nnwarchive-groups)
419 (insert (prin1-to-string (car elem))
420 " " (number-to-string (or (cadr elem) 0)) " 1 y\n"))))
422 (defun nnwarchive-paged (articles)
423 (let (art narts next)
424 (while (setq art (pop articles))
425 (when (and (>= art (or next 0))
426 (not (assq art nnwarchive-headers)))
428 (setq next (+ art nnwarchive-xover-page-size))))
433 (defun nnwarchive-egroups-list-groups (groups)
436 (set-buffer nnwarchive-buffer)
437 (dolist (group groups)
439 (nnwarchive-url nnwarchive-xover-last-url)
440 (goto-char (point-min))
441 (when (re-search-forward "of \\([0-9]+\\)</title>" nil t)
442 (setq articles (string-to-number (match-string 1))))
443 (let ((elem (assoc group nnwarchive-groups)))
445 (setcar (cdr elem) articles)
446 (push (list group articles "") nnwarchive-groups)))
447 (setq nnwarchive-headers (cdr (assoc group nnwarchive-headers-cache)))
448 (nnwarchive-egroups-xover group)
449 (let ((elem (assoc group nnwarchive-headers-cache)))
451 (setcdr elem nnwarchive-headers)
452 (push (cons group nnwarchive-headers) nnwarchive-headers-cache)))))))
454 (defun nnwarchive-egroups-list ()
455 (let ((case-fold-search t)
456 group description elem articles)
457 (goto-char (point-min))
460 "/group/\\([^/]+\\)/info\\.html[^>]+>[^>]+>[\040\t]*-[\040\t]*\\([^<]+\\)<"
462 (setq group (match-string 1)
463 description (match-string 2))
465 (when (re-search-forward ">\\([0-9]+\\)<" nil t)
466 (setq articles (string-to-number (match-string 1))))
467 (if (setq elem (assoc group nnwarchive-groups))
468 (setcar (cdr elem) articles)
469 (push (list group articles description) nnwarchive-groups))))
472 (defun nnwarchive-egroups-xover (group)
473 (let (article subject from date)
474 (goto-char (point-min))
475 (while (re-search-forward
476 "<a href=\"/group/\\([^/]+\\)/\\([0-9]+\\)\\.html[^>]+>\\([^<]+\\)<"
478 (setq group (match-string 1)
479 article (string-to-number (match-string 2))
480 subject (match-string 3))
482 (unless (assq article nnwarchive-headers)
483 (if (looking-at "<td[^>]+><font[^>]+>\\([^<]+\\)</font>")
484 (setq from (match-string 1)))
486 (if (looking-at "<td[^>]+><font[^>]+>\\([^<]+\\)</font>")
487 (setq date (identity (match-string 1))))
490 (make-full-mail-header
492 (nnwarchive-decode-entities-string subject)
493 (nnwarchive-decode-entities-string from)
495 (concat "<" group "%"
496 (number-to-string article)
499 0 0 "")) nnwarchive-headers))))
502 (defun nnwarchive-egroups-article (group articles)
503 (goto-char (point-min))
504 (if (search-forward "<pre>" nil t)
505 (delete-region (point-min) (point)))
506 (goto-char (point-max))
507 (if (search-backward "</pre>" nil t)
508 (delete-region (point) (point-max)))
509 (goto-char (point-min))
510 (while (re-search-forward "<a[^>]+>\\([^<]+\\)</a>" nil t)
511 (replace-match "<\\1>"))
512 (nnwarchive-decode-entities)
515 (defun nnwarchive-egroups-xover-files (group articles)
517 (setq auxs (nnwarchive-paged (sort articles '<)))
518 (while (setq aux (pop auxs))
519 (goto-char (point-max))
520 (nnwarchive-url nnwarchive-xover-url))
521 (if nnwarchive-xover-dissect
522 (nnwarchive-egroups-xover group))))
526 (defun nnwarchive-mail-archive-list-groups (groups)
529 (set-buffer nnwarchive-buffer)
530 (dolist (group groups)
532 (nnwarchive-url nnwarchive-xover-last-url)
533 (goto-char (point-min))
534 (when (re-search-forward "msg\\([0-9]+\\)\\.html" nil t)
535 (setq articles (1+ (string-to-number (match-string 1)))))
536 (let ((elem (assoc group nnwarchive-groups)))
538 (setcar (cdr elem) articles)
539 (push (list group articles "") nnwarchive-groups)))
540 (setq nnwarchive-headers (cdr (assoc group nnwarchive-headers-cache)))
541 (nnwarchive-mail-archive-xover group)
542 (let ((elem (assoc group nnwarchive-headers-cache)))
544 (setcdr elem nnwarchive-headers)
545 (push (cons group nnwarchive-headers)
546 nnwarchive-headers-cache)))))))
548 (defun nnwarchive-mail-archive-list ()
549 (let ((case-fold-search t)
550 group description elem articles)
551 (goto-char (point-min))
552 (while (re-search-forward "<a href=\"\\([^/]+\\)/\">\\([^>]+\\)<" nil t)
553 (setq group (match-string 1)
554 description (match-string 2))
557 (if (setq elem (assoc group nnwarchive-groups))
558 (setcar (cdr elem) articles)
559 (push (list group articles description) nnwarchive-groups))))
562 (defun nnwarchive-mail-archive-xover (group)
563 (let (article subject from date)
564 (goto-char (point-min))
565 (while (re-search-forward
566 "<A[^>]*HREF=\"msg\\([0-9]+\\)\\.html[^>]+>\\([^<]+\\)<"
568 (setq article (1+ (string-to-number (match-string 1)))
569 subject (match-string 2))
571 (unless (assq article nnwarchive-headers)
572 (if (looking-at "<UL><LI><EM>From</EM>:\\([^&]+\\)<\\([^&]+\\)>")
574 (setq from (match-string 1)
575 date (identity (match-string 2))))
576 (setq from "" date ""))
579 (make-full-mail-header
581 (nnwarchive-decode-entities-string subject)
582 (nnwarchive-decode-entities-string from)
584 (format "<%05d%%%s>\n" (1- article) group)
586 0 0 "")) nnwarchive-headers))))
589 (defun nnwarchive-mail-archive-xover-files (group articles)
590 (unless nnwarchive-headers
592 (nnwarchive-url nnwarchive-xover-last-url)
593 (goto-char (point-min))
594 (nnwarchive-mail-archive-xover group))
595 (let ((minart (apply 'min articles))
596 (min (apply 'min (mapcar 'car nnwarchive-headers)))
598 (while (> min minart)
600 (nnwarchive-url nnwarchive-xover-url)
601 (nnwarchive-mail-archive-xover group)
602 (setq min (apply 'min (mapcar 'car nnwarchive-headers))))))
604 (defvar nnwarchive-caesar-translation-table nil
605 "Modified rot13 table. tr/@A-Z[a-z/N-Z[@A-Mn-za-m/.")
607 (defun nnwarchive-make-caesar-translation-table ()
608 "Create modified rot13 table. tr/@A-Z[a-z/N-Z[@A-Mn-za-m/."
610 (table (make-string 256 0))
612 (A (mm-char-int ?A)))
613 (while (< (incf i) 256)
616 (substring table 0 (1- A))
617 (substring table (+ A 13) (+ A 27))
618 (substring table (1- A) (+ A 13))
619 (substring table (+ A 27) a)
620 (substring table (+ a 13) (+ a 26))
621 (substring table a (+ a 13))
622 (substring table (+ a 26) 255))))
624 (defun nnwarchive-from-r13 (from-r13)
628 (let ((message-caesar-translation-table
629 (or nnwarchive-caesar-translation-table
630 (setq nnwarchive-caesar-translation-table
631 (nnwarchive-make-caesar-translation-table)))))
632 (message-caesar-region (point-min) (point-max))
635 (defun nnwarchive-mail-archive-article (group article)
636 (let (p refs url mime e
641 (goto-char (point-min))
642 (when (search-forward "X-Head-End" nil t)
644 (narrow-to-region (point-min) (point))
645 (nnwarchive-decode-entities)
646 (goto-char (point-min))
647 (while (search-forward "<!--X-" nil t)
649 (goto-char (point-min))
650 (while (search-forward " -->" nil t)
653 (or (mail-fetch-field "from")
655 (mail-fetch-field "from-r13"))))
656 (setq date (mail-fetch-field "date"))
657 (setq id (mail-fetch-field "message-id"))
658 (setq subject (mail-fetch-field "subject"))
659 (goto-char (point-max))
661 (when (search-forward "<ul>" nil t)
663 (delete-region (point-min) (point))
664 (search-forward "</ul>" nil t)
666 (narrow-to-region (point-min) (point))
667 (nnwarchive-remove-markup)
668 (nnwarchive-decode-entities)
669 (goto-char (point-min))
672 (message-remove-header "from")
673 (goto-char (point-max))
674 (insert "From: " from "\n"))
676 (message-remove-header "subject")
677 (goto-char (point-max))
678 (insert "Subject: " subject "\n"))
680 (goto-char (point-max))
681 (insert "X-Message-ID: <" id ">\n"))
683 (message-remove-header "date")
684 (goto-char (point-max))
685 (insert "Date: " date "\n"))
686 (goto-char (point-max))
690 (when (search-forward "X-Body-of-Message" nil t)
692 (delete-region p (point))
693 (search-forward "X-Body-of-Message-End" nil t)
696 (narrow-to-region p (point))
697 (goto-char (point-min))
698 (if (> (skip-chars-forward "\040\n\r\t") 0)
699 (delete-region (point-min) (point)))
702 ((looking-at "<PRE>\r?\n?")
703 (delete-region (match-beginning 0) (match-end 0))
705 (when (search-forward "</PRE>" nil t)
706 (delete-region (match-beginning 0) (match-end 0))
708 (narrow-to-region p (point))
709 (nnwarchive-remove-markup)
710 (nnwarchive-decode-entities)
711 (goto-char (point-max)))))
712 ((looking-at "<P><A HREF=\"\\([^\"]+\\)")
713 (setq url (match-string 1))
714 (delete-region (match-beginning 0)
715 (progn (forward-line) (point)))
716 ;; I hate to download the url encode it, then immediately
718 ;; FixMe: Find a better solution to attach the URL.
719 ;; Maybe do some hack in external part of mml-generate-mim-1.
722 (format "<URL:http://www.mail-archive.com/%s/%s>"
729 (insert "<#part type=\"text/html\" disposition=inline>")
731 (if (re-search-forward
732 "[\040\n\r\t]*<PRE>\\|[\040\n\r\t]*<P><A HREF=\""
739 (if (> (skip-chars-forward "\040\n\r\t") 0)
740 (delete-region p (point))))
741 (goto-char (point-max))))
743 (when (search-forward "X-References-End" nil t)
746 (search-backward "X-References" p t)
747 (while (re-search-forward "msg\\([0-9]+\\)\\.html" e t)
748 (push (concat "<" (match-string 1) "%" group ">") refs)))
749 (delete-region p (point-max))
750 (goto-char (point-min))
751 (insert (format "Message-ID: <%05d%%%s>\n" (1- article) group))
753 (insert "References:")
755 (insert " " (pop refs)))
758 (unless (looking-at "$")
759 (search-forward "\n\n" nil t)
761 (narrow-to-region (point) (point-max))
762 (insert "MIME-Version: 1.0\n"
765 (delete-region (point-min) (point-max))))
769 (provide 'nnwarchive)
771 ;;; nnwarchive.el ends here