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-generate-active ()
380 (set-buffer nntp-server-buffer)
382 (dolist (elem nnwarchive-groups)
383 (insert (prin1-to-string (car elem))
384 " " (number-to-string (or (cadr elem) 0)) " 1 y\n"))))
386 (defun nnwarchive-paged (articles)
387 (let (art narts next)
388 (while (setq art (pop articles))
389 (when (and (>= art (or next 0))
390 (not (assq art nnwarchive-headers)))
392 (setq next (+ art nnwarchive-xover-page-size))))
397 (defun nnwarchive-egroups-list-groups (groups)
400 (set-buffer nnwarchive-buffer)
401 (dolist (group groups)
403 (nnwarchive-url nnwarchive-xover-last-url)
404 (goto-char (point-min))
405 (when (re-search-forward "of \\([0-9]+\\)</title>" nil t)
406 (setq articles (string-to-number (match-string 1))))
407 (let ((elem (assoc group nnwarchive-groups)))
409 (setcar (cdr elem) articles)
410 (push (list group articles "") nnwarchive-groups)))
411 (setq nnwarchive-headers (cdr (assoc group nnwarchive-headers-cache)))
412 (nnwarchive-egroups-xover group)
413 (let ((elem (assoc group nnwarchive-headers-cache)))
415 (setcdr elem nnwarchive-headers)
416 (push (cons group nnwarchive-headers) nnwarchive-headers-cache)))))))
418 (defun nnwarchive-egroups-list ()
419 (let ((case-fold-search t)
420 group description elem articles)
421 (goto-char (point-min))
424 "/group/\\([^/]+\\)/info\\.html[^>]+>[^>]+>[\040\t]*-[\040\t]*\\([^<]+\\)<"
426 (setq group (match-string 1)
427 description (match-string 2))
429 (when (re-search-forward ">\\([0-9]+\\)<" nil t)
430 (setq articles (string-to-number (match-string 1))))
431 (if (setq elem (assoc group nnwarchive-groups))
432 (setcar (cdr elem) articles)
433 (push (list group articles description) nnwarchive-groups))))
436 (defun nnwarchive-egroups-xover (group)
437 (let (article subject from date)
438 (goto-char (point-min))
439 (while (re-search-forward
440 "<a href=\"/group/\\([^/]+\\)/\\([0-9]+\\)\\.html[^>]+>\\([^<]+\\)<"
442 (setq group (match-string 1)
443 article (string-to-number (match-string 2))
444 subject (match-string 3))
446 (unless (assq article nnwarchive-headers)
447 (if (looking-at "<td[^>]+><font[^>]+>\\([^<]+\\)</font>")
448 (setq from (match-string 1)))
450 (if (looking-at "<td[^>]+><font[^>]+>\\([^<]+\\)</font>")
451 (setq date (identity (match-string 1))))
454 (make-full-mail-header
456 (nnweb-decode-entities-string subject)
457 (nnweb-decode-entities-string from)
459 (concat "<" group "%"
460 (number-to-string article)
463 0 0 "")) nnwarchive-headers))))
466 (defun nnwarchive-egroups-article (group articles)
467 (goto-char (point-min))
468 (if (search-forward "<pre>" nil t)
469 (delete-region (point-min) (point)))
470 (goto-char (point-max))
471 (if (search-backward "</pre>" nil t)
472 (delete-region (point) (point-max)))
473 (goto-char (point-min))
474 (while (re-search-forward "<a[^>]+>\\([^<]+\\)</a>" nil t)
475 (replace-match "<\\1>"))
476 (nnweb-decode-entities)
479 (defun nnwarchive-egroups-xover-files (group articles)
481 (setq auxs (nnwarchive-paged (sort articles '<)))
482 (while (setq aux (pop auxs))
483 (goto-char (point-max))
484 (nnwarchive-url nnwarchive-xover-url))
485 (if nnwarchive-xover-dissect
486 (nnwarchive-egroups-xover group))))
490 (defun nnwarchive-mail-archive-list-groups (groups)
493 (set-buffer nnwarchive-buffer)
494 (dolist (group groups)
496 (nnwarchive-url nnwarchive-xover-last-url)
497 (goto-char (point-min))
498 (when (re-search-forward "msg\\([0-9]+\\)\\.html" nil t)
499 (setq articles (1+ (string-to-number (match-string 1)))))
500 (let ((elem (assoc group nnwarchive-groups)))
502 (setcar (cdr elem) articles)
503 (push (list group articles "") nnwarchive-groups)))
504 (setq nnwarchive-headers (cdr (assoc group nnwarchive-headers-cache)))
505 (nnwarchive-mail-archive-xover group)
506 (let ((elem (assoc group nnwarchive-headers-cache)))
508 (setcdr elem nnwarchive-headers)
509 (push (cons group nnwarchive-headers)
510 nnwarchive-headers-cache)))))))
512 (defun nnwarchive-mail-archive-list ()
513 (let ((case-fold-search t)
514 group description elem articles)
515 (goto-char (point-min))
516 (while (re-search-forward "<a href=\"\\([^/]+\\)/\">\\([^>]+\\)<" nil t)
517 (setq group (match-string 1)
518 description (match-string 2))
521 (if (setq elem (assoc group nnwarchive-groups))
522 (setcar (cdr elem) articles)
523 (push (list group articles description) nnwarchive-groups))))
526 (defun nnwarchive-mail-archive-xover (group)
527 (let (article subject from date)
528 (goto-char (point-min))
529 (while (re-search-forward
530 "<A[^>]*HREF=\"msg\\([0-9]+\\)\\.html[^>]+>\\([^<]+\\)<"
532 (setq article (1+ (string-to-number (match-string 1)))
533 subject (match-string 2))
535 (unless (assq article nnwarchive-headers)
536 (if (looking-at "<UL><LI><EM>From</EM>:\\([^&]+\\)<\\([^&]+\\)>")
538 (setq from (match-string 1)
539 date (identity (match-string 2))))
540 (setq from "" date ""))
543 (make-full-mail-header
545 (nnweb-decode-entities-string subject)
546 (nnweb-decode-entities-string from)
548 (format "<%05d%%%s>\n" (1- article) group)
550 0 0 "")) nnwarchive-headers))))
553 (defun nnwarchive-mail-archive-xover-files (group articles)
554 (unless nnwarchive-headers
556 (nnwarchive-url nnwarchive-xover-last-url)
557 (goto-char (point-min))
558 (nnwarchive-mail-archive-xover group))
559 (let ((minart (apply 'min articles))
560 (min (apply 'min (mapcar 'car nnwarchive-headers)))
562 (while (> min minart)
564 (nnwarchive-url nnwarchive-xover-url)
565 (nnwarchive-mail-archive-xover group)
566 (setq min (apply 'min (mapcar 'car nnwarchive-headers))))))
568 (defvar nnwarchive-caesar-translation-table nil
569 "Modified rot13 table. tr/@A-Z[a-z/N-Z[@A-Mn-za-m/.")
571 (defun nnwarchive-make-caesar-translation-table ()
572 "Create modified rot13 table. tr/@A-Z[a-z/N-Z[@A-Mn-za-m/."
574 (table (make-string 256 0))
576 (A (mm-char-int ?A)))
577 (while (< (incf i) 256)
580 (substring table 0 (1- A))
581 (substring table (+ A 13) (+ A 27))
582 (substring table (1- A) (+ A 13))
583 (substring table (+ A 27) a)
584 (substring table (+ a 13) (+ a 26))
585 (substring table a (+ a 13))
586 (substring table (+ a 26) 255))))
588 (defun nnwarchive-from-r13 (from-r13)
592 (let ((message-caesar-translation-table
593 (or nnwarchive-caesar-translation-table
594 (setq nnwarchive-caesar-translation-table
595 (nnwarchive-make-caesar-translation-table)))))
596 (message-caesar-region (point-min) (point-max))
599 (defun nnwarchive-mail-archive-article (group article)
600 (let (p refs url mime e
605 (goto-char (point-min))
606 (when (search-forward "X-Head-End" nil t)
608 (narrow-to-region (point-min) (point))
609 (nnweb-decode-entities)
610 (goto-char (point-min))
611 (while (search-forward "<!--X-" nil t)
613 (goto-char (point-min))
614 (while (search-forward " -->" nil t)
617 (or (mail-fetch-field "from")
619 (mail-fetch-field "from-r13"))))
620 (setq date (mail-fetch-field "date"))
621 (setq id (mail-fetch-field "message-id"))
622 (setq subject (mail-fetch-field "subject"))
623 (goto-char (point-max))
625 (when (search-forward "<ul>" nil t)
627 (delete-region (point-min) (point))
628 (search-forward "</ul>" nil t)
630 (narrow-to-region (point-min) (point))
631 (nnweb-remove-markup)
632 (nnweb-decode-entities)
633 (goto-char (point-min))
636 (message-remove-header "from")
637 (goto-char (point-max))
638 (insert "From: " from "\n"))
640 (message-remove-header "subject")
641 (goto-char (point-max))
642 (insert "Subject: " subject "\n"))
644 (goto-char (point-max))
645 (insert "X-Message-ID: <" id ">\n"))
647 (message-remove-header "date")
648 (goto-char (point-max))
649 (insert "Date: " date "\n"))
650 (goto-char (point-max))
654 (when (search-forward "X-Body-of-Message" nil t)
656 (delete-region p (point))
657 (search-forward "X-Body-of-Message-End" nil t)
660 (narrow-to-region p (point))
661 (goto-char (point-min))
662 (if (> (skip-chars-forward "\040\n\r\t") 0)
663 (delete-region (point-min) (point)))
666 ((looking-at "<PRE>\r?\n?")
667 (delete-region (match-beginning 0) (match-end 0))
669 (when (search-forward "</PRE>" nil t)
670 (delete-region (match-beginning 0) (match-end 0))
672 (narrow-to-region p (point))
673 (nnweb-remove-markup)
674 (nnweb-decode-entities)
675 (goto-char (point-max)))))
676 ((looking-at "<P><A HREF=\"\\([^\"]+\\)")
677 (setq url (match-string 1))
678 (delete-region (match-beginning 0)
679 (progn (forward-line) (point)))
680 ;; I hate to download the url encode it, then immediately
682 ;; FixMe: Find a better solution to attach the URL.
683 ;; Maybe do some hack in external part of mml-generate-mim-1.
686 (format "<URL:http://www.mail-archive.com/%s/%s>"
693 (insert "<#part type=\"text/html\" disposition=inline>")
695 (if (re-search-forward
696 "[\040\n\r\t]*<PRE>\\|[\040\n\r\t]*<P><A HREF=\""
703 (if (> (skip-chars-forward "\040\n\r\t") 0)
704 (delete-region p (point))))
705 (goto-char (point-max))))
707 (when (search-forward "X-References-End" nil t)
710 (search-backward "X-References" p t)
711 (while (re-search-forward "msg\\([0-9]+\\)\\.html" e t)
712 (push (concat "<" (match-string 1) "%" group ">") refs)))
713 (delete-region p (point-max))
714 (goto-char (point-min))
715 (insert (format "Message-ID: <%05d%%%s>\n" (1- article) group))
717 (insert "References:")
719 (insert " " (pop refs)))
722 (unless (looking-at "$")
723 (search-forward "\n\n" nil t)
725 (narrow-to-region (point) (point-max))
726 (insert "MIME-Version: 1.0\n"
729 (delete-region (point-min) (point-max))))
733 (provide 'nnwarchive)
735 ;;; nnwarchive.el ends here