1 ;;; nnwarchive.el --- interfacing with web archives
2 ;; Copyright (C) 1999, 2000 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 some functions of 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)
47 (nnoo-declare nnwarchive)
49 (defvar nnwarchive-type-definition
51 (address . "www.egroups.com")
53 "http://www.egroups.com/login.cgi?&login_email=%s&login_password=%s"
54 nnwarchive-login nnwarchive-passwd)
56 "http://www.egroups.com/mygroups")
57 (list-dissect . nnwarchive-egroups-list)
58 (list-groups . nnwarchive-egroups-list-groups)
60 "http://www.egroups.com/messages/%s/%d" group aux)
62 "http://www.egroups.com/messages/%s/" group)
63 (xover-page-size . 13)
64 (xover-dissect . nnwarchive-egroups-xover)
66 "http://www.egroups.com/message/%s/%d?source=1" group article)
67 (article-dissect . nnwarchive-egroups-article)
70 (xover-files . nnwarchive-egroups-xover-files))
72 (address . "www.mail-archive.com")
75 "http://www.mail-archive.com/lists.html")
76 (list-dissect . nnwarchive-mail-archive-list)
77 (list-groups . nnwarchive-mail-archive-list-groups)
79 "http://www.mail-archive.com/%s/mail%d.html" group aux)
81 "http://www.mail-archive.com/%s/maillist.html" group)
83 (xover-dissect . nnwarchive-mail-archive-xover)
85 "http://www.mail-archive.com/%s/msg%05d.html" group article1)
86 (article-dissect . nnwarchive-mail-archive-article)
87 (xover-files . nnwarchive-mail-archive-xover-files)
89 (article-offset . 1))))
91 (defvar nnwarchive-default-type 'egroups)
93 (defvoo nnwarchive-directory (nnheader-concat gnus-directory "warchive/")
94 "Where nnwarchive will save its files.")
96 (defvoo nnwarchive-type nil
97 "The type of nnwarchive.")
99 (defvoo nnwarchive-address ""
100 "The address of nnwarchive.")
102 (defvoo nnwarchive-login nil
103 "Your login name for the group.")
105 (defvoo nnwarchive-passwd nil
106 "Your password for the group.")
108 (defvoo nnwarchive-groups nil)
110 (defvoo nnwarchive-headers-cache nil)
112 (defvoo nnwarchive-authentication nil)
114 (defvoo nnwarchive-nov-is-evil nil)
116 (defconst nnwarchive-version "nnwarchive 1.0")
118 ;;; Internal variables
120 (defvoo nnwarchive-open-url nil)
121 (defvoo nnwarchive-open-dissect nil)
123 (defvoo nnwarchive-list-url nil)
124 (defvoo nnwarchive-list-dissect nil)
125 (defvoo nnwarchive-list-groups nil)
127 (defvoo nnwarchive-xover-files nil)
128 (defvoo nnwarchive-xover-url nil)
129 (defvoo nnwarchive-xover-last-url nil)
130 (defvoo nnwarchive-xover-dissect nil)
131 (defvoo nnwarchive-xover-page-size nil)
133 (defvoo nnwarchive-article-url nil)
134 (defvoo nnwarchive-article-dissect nil)
135 (defvoo nnwarchive-xover-files nil)
136 (defvoo nnwarchive-article-offset 0)
138 (defvoo nnwarchive-buffer nil)
140 (defvoo nnwarchive-keep-backlog 300)
141 (defvar nnwarchive-backlog-articles nil)
142 (defvar nnwarchive-backlog-hashtb nil)
144 (defvoo nnwarchive-headers nil)
147 ;;; Interface functions
149 (nnoo-define-basics nnwarchive)
151 (defun nnwarchive-set-default (type)
152 (let ((defs (cdr (assq type nnwarchive-type-definition)))
155 (set (intern (concat "nnwarchive-" (symbol-name (car def))))
158 (defmacro nnwarchive-backlog (&rest form)
159 `(let ((gnus-keep-backlog nnwarchive-keep-backlog)
161 (format " *nnwarchive backlog %s*" nnwarchive-address))
162 (gnus-backlog-articles nnwarchive-backlog-articles)
163 (gnus-backlog-hashtb nnwarchive-backlog-hashtb))
166 (setq nnwarchive-backlog-articles gnus-backlog-articles
167 nnwarchive-backlog-hashtb gnus-backlog-hashtb))))
168 (put 'nnwarchive-backlog 'lisp-indent-function 0)
169 (put 'nnwarchive-backlog 'edebug-form-spec '(form body))
171 (defun nnwarchive-backlog-enter-article (group number buffer)
173 (gnus-backlog-enter-article group number buffer)))
175 (defun nnwarchive-get-article (article &optional group server buffer)
176 (if (numberp article)
177 (if (nnwarchive-backlog
178 (gnus-backlog-request-article group article
179 (or buffer nntp-server-buffer)))
183 (set-buffer nnwarchive-buffer)
184 (goto-char (point-min))
185 (let ((article1 (- article nnwarchive-article-offset)))
186 (nnwarchive-url nnwarchive-article-url))
187 (setq contents (funcall nnwarchive-article-dissect group article)))
190 (set-buffer (or buffer nntp-server-buffer))
193 (nnwarchive-backlog-enter-article group article (current-buffer))
194 (nnheader-report 'nnwarchive "Fetched article %s" article)
195 (cons group article)))))
198 (deffoo nnwarchive-retrieve-headers (articles &optional group server fetch-old)
199 (nnwarchive-possibly-change-server group server)
200 (if (or gnus-nov-is-evil nnwarchive-nov-is-evil)
202 (with-current-buffer nntp-server-buffer
204 (let ((buf (current-buffer)) b e)
205 (dolist (art articles)
206 (nnwarchive-get-article art group server buf)
207 (setq b (goto-char (point-min)))
208 (if (search-forward "\n\n" nil t)
210 (goto-char (point-max)))
212 (with-current-buffer nntp-server-buffer
213 (insert (format "221 %d Article retrieved.\n" art))
214 (insert-buffer-substring buf b e)
217 (setq nnwarchive-headers (cdr (assoc group nnwarchive-headers-cache)))
219 (set-buffer nnwarchive-buffer)
221 (funcall nnwarchive-xover-files group articles))
223 (set-buffer nntp-server-buffer)
226 (dolist (art articles)
227 (if (setq header (assq art nnwarchive-headers))
228 (nnheader-insert-nov (cdr header))))))
229 (let ((elem (assoc group nnwarchive-headers-cache)))
231 (setcdr elem nnwarchive-headers)
232 (push (cons group nnwarchive-headers) nnwarchive-headers-cache)))
235 (deffoo nnwarchive-request-group (group &optional server dont-check)
236 (nnwarchive-possibly-change-server nil server)
237 (when (and (not dont-check) nnwarchive-list-groups)
238 (funcall nnwarchive-list-groups (list group))
239 (nnwarchive-write-groups))
240 (let ((elem (assoc group nnwarchive-groups)))
243 (nnheader-report 'nnwarchive "Group does not exist"))
245 (nnheader-report 'nnwarchive "Opened group %s" group)
247 "211 %d %d %d %s\n" (or (cadr elem) 0) 1 (or (cadr elem) 0)
248 (prin1-to-string group))
251 (deffoo nnwarchive-request-article (article &optional group server buffer)
252 (nnwarchive-possibly-change-server group server)
253 (nnwarchive-get-article article group server buffer))
255 (deffoo nnwarchive-close-server (&optional server)
256 (when (and (nnwarchive-server-opened server)
257 (gnus-buffer-live-p nnwarchive-buffer))
259 (set-buffer nnwarchive-buffer)
260 (kill-buffer nnwarchive-buffer)))
262 (gnus-backlog-shutdown))
263 (nnoo-close-server 'nnwarchive server))
265 (deffoo nnwarchive-request-list (&optional server)
266 (nnwarchive-possibly-change-server nil server)
268 (set-buffer nnwarchive-buffer)
270 (if nnwarchive-list-url
271 (nnwarchive-url nnwarchive-list-url))
272 (if nnwarchive-list-dissect
273 (funcall nnwarchive-list-dissect))
274 (nnwarchive-write-groups)
275 (nnwarchive-generate-active))
278 (deffoo nnwarchive-open-server (server &optional defs connectionless)
279 (nnoo-change-server 'nnwarchive server defs)
280 (nnwarchive-init server)
281 (when nnwarchive-authentication
282 (setq nnwarchive-login
285 (format "Login at %s: " server)
287 (setq nnwarchive-passwd
288 (or nnwarchive-passwd
289 (mail-source-read-passwd
290 (format "Password for %s at %s: "
291 nnwarchive-login server)))))
292 (unless nnwarchive-groups
293 (nnwarchive-read-groups))
295 (set-buffer nnwarchive-buffer)
297 (if nnwarchive-open-url
298 (nnwarchive-url nnwarchive-open-url))
299 (if nnwarchive-open-dissect
300 (funcall nnwarchive-open-dissect)))
303 (nnoo-define-skeleton nnwarchive)
305 ;;; Internal functions
307 (defun nnwarchive-possibly-change-server (&optional group server)
308 (nnwarchive-init server)
310 (not (nnwarchive-server-opened server)))
311 (nnwarchive-open-server server)))
313 (defun nnwarchive-read-groups ()
314 (let ((file (expand-file-name (concat "groups-" nnwarchive-address)
315 nnwarchive-directory)))
316 (when (file-exists-p file)
318 (insert-file-contents file)
319 (goto-char (point-min))
320 (setq nnwarchive-groups (read (current-buffer)))))))
322 (defun nnwarchive-write-groups ()
323 (with-temp-file (expand-file-name (concat "groups-" nnwarchive-address)
324 nnwarchive-directory)
325 (prin1 nnwarchive-groups (current-buffer))))
327 (defun nnwarchive-init (server)
328 "Initialize buffers and such."
329 (let ((type (intern server)) (defs nnwarchive-type-definition) def)
332 (setq type nnwarchive-default-type))
333 ((assq type nnwarchive-type-definition) t)
336 (while (setq def (pop defs))
337 (when (equal (cdr (assq 'address (cdr def))) server)
339 (setq type (car def))))
341 (error "Undefined server %s" server))))
342 (setq nnwarchive-type type))
343 (unless (file-exists-p nnwarchive-directory)
344 (gnus-make-directory nnwarchive-directory))
345 (unless (gnus-buffer-live-p nnwarchive-buffer)
346 (setq nnwarchive-buffer
348 (nnheader-set-temp-buffer
349 (format " *nnwarchive %s %s*" nnwarchive-type server)))))
350 (nnwarchive-set-default nnwarchive-type))
352 (defun nnwarchive-eval (expr)
355 (cons (nnwarchive-eval (car expr)) (nnwarchive-eval (cdr expr))))
361 (defun nnwarchive-url (xurl)
362 (mm-with-unibyte-current-buffer
363 (let ((url-confirmation-func 'identity) ;; Some hacks.
364 (url-cookie-multiple-line nil))
366 ((eq (car xurl) 'post)
368 (mm-url-fetch-form (car xurl) (nnwarchive-eval (cdr xurl))))
370 (mm-url-insert (apply 'format (nnwarchive-eval xurl))))))))
372 (defun nnwarchive-generate-active ()
374 (set-buffer nntp-server-buffer)
376 (dolist (elem nnwarchive-groups)
377 (insert (prin1-to-string (car elem))
378 " " (number-to-string (or (cadr elem) 0)) " 1 y\n"))))
380 (defun nnwarchive-paged (articles)
381 (let (art narts next)
382 (while (setq art (pop articles))
383 (when (and (>= art (or next 0))
384 (not (assq art nnwarchive-headers)))
386 (setq next (+ art nnwarchive-xover-page-size))))
391 (defun nnwarchive-egroups-list-groups (groups)
394 (set-buffer nnwarchive-buffer)
395 (dolist (group groups)
397 (nnwarchive-url nnwarchive-xover-last-url)
398 (goto-char (point-min))
399 (when (re-search-forward "of \\([0-9]+\\)[ \t\n\r]*</title>" nil t)
400 (setq articles (string-to-number (match-string 1))))
401 (let ((elem (assoc group nnwarchive-groups)))
403 (setcar (cdr elem) articles)
404 (push (list group articles "") nnwarchive-groups)))
405 (setq nnwarchive-headers (cdr (assoc group nnwarchive-headers-cache)))
406 (nnwarchive-egroups-xover group)
407 (let ((elem (assoc group nnwarchive-headers-cache)))
409 (setcdr elem nnwarchive-headers)
410 (push (cons group nnwarchive-headers) nnwarchive-headers-cache)))))))
412 (defun nnwarchive-egroups-list ()
413 (let ((case-fold-search t)
414 group description elem articles)
415 (goto-char (point-min))
417 (re-search-forward "href=\"/group/\\([^/\"\> ]+\\)" nil t)
418 (setq group (match-string 1)
419 description (match-string 2))
420 (if (setq elem (assoc group nnwarchive-groups))
421 (setcar (cdr elem) 0)
422 (push (list group articles description) nnwarchive-groups))))
425 (defun nnwarchive-egroups-xover (group)
426 (let (article subject from date)
427 (goto-char (point-min))
428 (while (re-search-forward
429 "<a href=\"/group/\\([^/]+\\)/\\([0-9]+\\)[^>]+>\\([^<]+\\)<"
431 (setq group (match-string 1)
432 article (string-to-number (match-string 2))
433 subject (match-string 3))
435 (unless (assq article nnwarchive-headers)
436 (if (looking-at "<td[^>]+><font[^>]+>\\([^<]+\\)</font>")
437 (setq from (match-string 1)))
439 (if (looking-at "<td[^>]+><font[^>]+>\\([^<]+\\)</font>")
440 (setq date (identity (match-string 1))))
443 (make-full-mail-header
445 (mm-url-decode-entities-string subject)
446 (mm-url-decode-entities-string from)
448 (concat "<" group "%"
449 (number-to-string article)
452 0 0 "")) nnwarchive-headers))))
455 (defun nnwarchive-egroups-article (group articles)
456 (goto-char (point-min))
457 (if (search-forward "<pre>" nil t)
458 (delete-region (point-min) (point)))
459 (goto-char (point-max))
460 (if (search-backward "</pre>" nil t)
461 (delete-region (point) (point-max)))
462 (goto-char (point-min))
463 (while (re-search-forward "<a[^>]+>\\([^<]+\\)</a>" nil t)
464 (replace-match "\\1"))
465 (mm-url-decode-entities)
468 (defun nnwarchive-egroups-xover-files (group articles)
470 (setq auxs (nnwarchive-paged (sort articles '<)))
471 (while (setq aux (pop auxs))
472 (goto-char (point-max))
473 (nnwarchive-url nnwarchive-xover-url))
474 (if nnwarchive-xover-dissect
475 (nnwarchive-egroups-xover group))))
479 (defun nnwarchive-mail-archive-list-groups (groups)
482 (set-buffer nnwarchive-buffer)
483 (dolist (group groups)
485 (nnwarchive-url nnwarchive-xover-last-url)
486 (goto-char (point-min))
487 (when (re-search-forward "msg\\([0-9]+\\)\\.html" nil t)
488 (setq articles (1+ (string-to-number (match-string 1)))))
489 (let ((elem (assoc group nnwarchive-groups)))
491 (setcar (cdr elem) articles)
492 (push (list group articles "") nnwarchive-groups)))
493 (setq nnwarchive-headers (cdr (assoc group nnwarchive-headers-cache)))
494 (nnwarchive-mail-archive-xover group)
495 (let ((elem (assoc group nnwarchive-headers-cache)))
497 (setcdr elem nnwarchive-headers)
498 (push (cons group nnwarchive-headers)
499 nnwarchive-headers-cache)))))))
501 (defun nnwarchive-mail-archive-list ()
502 (let ((case-fold-search t)
503 group description elem articles)
504 (goto-char (point-min))
505 (while (re-search-forward "<a href=\"\\([^/]+\\)/\">\\([^>]+\\)<" nil t)
506 (setq group (match-string 1)
507 description (match-string 2))
510 (if (setq elem (assoc group nnwarchive-groups))
511 (setcar (cdr elem) articles)
512 (push (list group articles description) nnwarchive-groups))))
515 (defun nnwarchive-mail-archive-xover (group)
516 (let (article subject from date)
517 (goto-char (point-min))
518 (while (re-search-forward
519 "<A[^>]*HREF=\"msg\\([0-9]+\\)\\.html[^>]+>\\([^<]+\\)<"
521 (setq article (1+ (string-to-number (match-string 1)))
522 subject (match-string 2))
524 (unless (assq article nnwarchive-headers)
525 (if (looking-at "<UL><LI><EM>From</EM>: *\\([^<]*[^< ]\\) *<\\([^&]+\\)>")
527 (setq from (match-string 1)
528 date (identity (match-string 2))))
529 (setq from "" date ""))
532 (make-full-mail-header
534 (mm-url-decode-entities-string subject)
535 (mm-url-decode-entities-string from)
537 (format "<%05d%%%s>\n" (1- article) group)
539 0 0 "")) nnwarchive-headers))))
542 (defun nnwarchive-mail-archive-xover-files (group articles)
543 (unless nnwarchive-headers
545 (nnwarchive-url nnwarchive-xover-last-url)
546 (goto-char (point-min))
547 (nnwarchive-mail-archive-xover group))
548 (let ((minart (apply 'min articles))
549 (min (apply 'min (mapcar 'car nnwarchive-headers)))
551 (while (> min minart)
553 (nnwarchive-url nnwarchive-xover-url)
554 (nnwarchive-mail-archive-xover group)
555 (setq min (apply 'min (mapcar 'car nnwarchive-headers))))))
557 (defvar nnwarchive-caesar-translation-table nil
558 "Modified rot13 table. tr/@A-Z[a-z/N-Z[@A-Mn-za-m/.")
560 (defun nnwarchive-make-caesar-translation-table ()
561 "Create modified rot13 table. tr/@A-Z[a-z/N-Z[@A-Mn-za-m/."
563 (table (make-string 256 0))
565 (A (mm-char-int ?A)))
566 (while (< (incf i) 256)
569 (substring table 0 (1- A))
570 (substring table (+ A 13) (+ A 27))
571 (substring table (1- A) (+ A 13))
572 (substring table (+ A 27) a)
573 (substring table (+ a 13) (+ a 26))
574 (substring table a (+ a 13))
575 (substring table (+ a 26) 255))))
577 (defun nnwarchive-from-r13 (from-r13)
581 (let ((message-caesar-translation-table
582 (or nnwarchive-caesar-translation-table
583 (setq nnwarchive-caesar-translation-table
584 (nnwarchive-make-caesar-translation-table)))))
585 (message-caesar-region (point-min) (point-max))
588 (defun nnwarchive-mail-archive-article (group article)
589 (let (p refs url mime e
592 (case-fold-search t))
594 (goto-char (point-min))
595 (when (search-forward "X-Head-End" nil t)
597 (narrow-to-region (point-min) (point))
598 (mm-url-decode-entities)
599 (goto-char (point-min))
600 (while (search-forward "<!--X-" nil t)
602 (goto-char (point-min))
603 (while (search-forward " -->" nil t)
606 (or (mail-fetch-field "from")
608 (mail-fetch-field "from-r13"))))
609 (setq date (mail-fetch-field "date"))
610 (setq id (mail-fetch-field "message-id"))
611 (setq subject (mail-fetch-field "subject"))
612 (goto-char (point-max))
614 (when (search-forward "<ul>" nil t)
616 (delete-region (point-min) (point))
617 (search-forward "</ul>" nil t)
619 (narrow-to-region (point-min) (point))
620 (mm-url-remove-markup)
621 (mm-url-decode-entities)
622 (goto-char (point-min))
625 (message-remove-header "from")
626 (goto-char (point-max))
627 (insert "From: " from "\n"))
629 (message-remove-header "subject")
630 (goto-char (point-max))
631 (insert "Subject: " subject "\n"))
633 (goto-char (point-max))
634 (insert "X-Message-ID: <" id ">\n"))
636 (message-remove-header "date")
637 (goto-char (point-max))
638 (insert "Date: " date "\n"))
639 (goto-char (point-max))
643 (when (search-forward "X-Body-of-Message" nil t)
645 (delete-region p (point))
646 (search-forward "X-Body-of-Message-End" nil t)
649 (narrow-to-region p (point))
650 (goto-char (point-min))
651 (if (> (skip-chars-forward "\040\n\r\t") 0)
652 (delete-region (point-min) (point)))
655 ((looking-at "<PRE>\r?\n?")
656 (delete-region (match-beginning 0) (match-end 0))
658 (when (search-forward "</PRE>" nil t)
659 (delete-region (match-beginning 0) (match-end 0))
661 (narrow-to-region p (point))
662 (mm-url-remove-markup)
663 (mm-url-decode-entities)
664 (goto-char (point-max)))))
665 ((looking-at "<P><A HREF=\"\\([^\"]+\\)")
666 (setq url (match-string 1))
667 (delete-region (match-beginning 0)
668 (progn (forward-line) (point)))
669 ;; I hate to download the url encode it, then immediately
674 (string-match "\\.[^\\.]+$" url)
675 (mailcap-extension-to-mime
676 (match-string 0 url)))
677 "application/octet-stream")
678 (format " url=\"http://www.mail-archive.com/%s/%s\""
685 (insert "<#part type=\"text/html\" disposition=inline>")
687 (if (re-search-forward
688 "[\040\n\r\t]*<PRE>\\|[\040\n\r\t]*<P><A HREF=\""
695 (if (> (skip-chars-forward "\040\n\r\t") 0)
696 (delete-region p (point))))
697 (goto-char (point-max))))
699 (when (search-forward "X-References-End" nil t)
702 (search-backward "X-References" p t)
703 (while (re-search-forward "msg\\([0-9]+\\)\\.html" e t)
704 (push (concat "<" (match-string 1) "%" group ">") refs)))
705 (delete-region p (point-max))
706 (goto-char (point-min))
707 (insert (format "Message-ID: <%05d%%%s>\n" (1- article) group))
709 (insert "References:")
711 (insert " " (pop refs)))
714 (unless (looking-at "$")
715 (search-forward "\n\n" nil t)
717 (narrow-to-region (point) (point-max))
718 (insert "MIME-Version: 1.0\n"
721 (delete-region (point-min) (point-max))))
725 (provide 'nnwarchive)
727 ;;; nnwarchive.el ends here