1 ;;; nnweb.el --- retrieving articles via web search engines
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000
3 ;; Free Software Foundation, Inc.
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
27 ;; Note: You need to have `url' and `w3' installed for this
32 (eval-when-compile (require 'cl))
45 ;; Report failure to find w3 at load time if appropriate.
53 (defvoo nnweb-directory (nnheader-concat gnus-directory "nnweb/")
54 "Where nnweb will save its files.")
56 (defvoo nnweb-type 'dejanews
57 "What search engine type is being used.
58 Valid types include `dejanews', `dejanewsold', `reference',
61 (defvar nnweb-type-definition
64 (id . "http://search.dejanews.com/msgid.xp?MID=%s&fmt=text")
65 (map . nnweb-dejanews-create-mapping)
66 (search . nnweb-dejanews-search)
67 (address . "http://www.deja.com/=dnc/qs.xp")
68 (identifier . nnweb-dejanews-identity))
71 (map . nnweb-dejanews-create-mapping)
72 (search . nnweb-dejanewsold-search)
73 (address . "http://www.deja.com/dnquery.xp")
74 (identifier . nnweb-dejanews-identity))
76 (article . nnweb-reference-wash-article)
77 (map . nnweb-reference-create-mapping)
78 (search . nnweb-reference-search)
79 (address . "http://www.reference.com/cgi-bin/pn/go")
80 (identifier . identity))
82 (article . nnweb-altavista-wash-article)
83 (map . nnweb-altavista-create-mapping)
84 (search . nnweb-altavista-search)
85 (address . "http://www.altavista.digital.com/cgi-bin/query")
86 (id . "/cgi-bin/news?id@%s")
87 (identifier . identity)))
88 "Type-definition alist.")
90 (defvoo nnweb-search nil
91 "Search string to feed to DejaNews.")
93 (defvoo nnweb-max-hits 999
94 "Maximum number of hits to display.")
96 (defvoo nnweb-ephemeral-p nil
97 "Whether this nnweb server is ephemeral.")
99 ;;; Internal variables
101 (defvoo nnweb-articles nil)
102 (defvoo nnweb-buffer nil)
103 (defvoo nnweb-group-alist nil)
104 (defvoo nnweb-group nil)
105 (defvoo nnweb-hashtb nil)
107 ;;; Interface functions
109 (nnoo-define-basics nnweb)
111 (deffoo nnweb-retrieve-headers (articles &optional group server fetch-old)
112 (nnweb-possibly-change-server group server)
114 (set-buffer nntp-server-buffer)
116 (let (article header)
117 (mm-with-unibyte-current-buffer
118 (while (setq article (pop articles))
119 (when (setq header (cadr (assq article nnweb-articles)))
120 (nnheader-insert-nov header))))
123 (deffoo nnweb-request-scan (&optional group server)
124 (nnweb-possibly-change-server group server)
125 (funcall (nnweb-definition 'map))
126 (unless nnweb-ephemeral-p
128 (nnweb-write-overview group)))
130 (deffoo nnweb-request-group (group &optional server dont-check)
131 (nnweb-possibly-change-server nil server)
133 (not (equal group nnweb-group))
134 (not nnweb-ephemeral-p))
135 (let ((info (assoc group nnweb-group-alist)))
137 (setq nnweb-group group)
138 (setq nnweb-type (nth 2 info))
139 (setq nnweb-search (nth 3 info))
141 (nnweb-read-overview group)))))
143 ((not nnweb-articles)
144 (nnheader-report 'nnweb "No matching articles"))
146 (let ((active (if nnweb-ephemeral-p
147 (cons (caar nnweb-articles)
148 (caar (last nnweb-articles)))
149 (cadr (assoc group nnweb-group-alist)))))
150 (nnheader-report 'nnweb "Opened group %s" group)
152 "211 %d %d %d %s\n" (length nnweb-articles)
153 (car active) (cdr active) group)))))
155 (deffoo nnweb-close-group (group &optional server)
156 (nnweb-possibly-change-server group server)
157 (when (gnus-buffer-live-p nnweb-buffer)
159 (set-buffer nnweb-buffer)
160 (set-buffer-modified-p nil)
161 (kill-buffer nnweb-buffer)))
164 (deffoo nnweb-request-article (article &optional group server buffer)
165 (nnweb-possibly-change-server group server)
167 (set-buffer (or buffer nntp-server-buffer))
168 (let* ((header (cadr (assq article nnweb-articles)))
169 (url (and header (mail-header-xref header))))
171 (mm-with-unibyte-current-buffer
172 (nnweb-fetch-url url)))
173 (and (stringp article)
174 (nnweb-definition 'id t)
175 (let ((fetch (nnweb-definition 'id))
177 (when (string-match "^<\\(.*\\)>$" article)
178 (setq art (match-string 1 article)))
181 (mm-with-unibyte-current-buffer
183 (format fetch article)))))))
184 (unless nnheader-callback-function
185 (funcall (nnweb-definition 'article))
186 (nnweb-decode-entities))
187 (nnheader-report 'nnweb "Fetched article %s" article)
188 (cons group (and (numberp article) article))))))
190 (deffoo nnweb-close-server (&optional server)
191 (when (and (nnweb-server-opened server)
192 (gnus-buffer-live-p nnweb-buffer))
194 (set-buffer nnweb-buffer)
195 (set-buffer-modified-p nil)
196 (kill-buffer nnweb-buffer)))
197 (nnoo-close-server 'nnweb server))
199 (deffoo nnweb-request-list (&optional server)
200 (nnweb-possibly-change-server nil server)
202 (set-buffer nntp-server-buffer)
203 (nnmail-generate-active nnweb-group-alist)
206 (deffoo nnweb-request-update-info (group info &optional server)
207 (nnweb-possibly-change-server group server))
209 (deffoo nnweb-asynchronous-p ()
212 (deffoo nnweb-request-create-group (group &optional server args)
213 (nnweb-possibly-change-server nil server)
214 (nnweb-request-delete-group group)
215 (push `(,group ,(cons 1 0) ,@args) nnweb-group-alist)
219 (deffoo nnweb-request-delete-group (group &optional force server)
220 (nnweb-possibly-change-server group server)
221 (gnus-pull group nnweb-group-alist t)
223 (gnus-delete-file (nnweb-overview-file group))
226 (nnoo-define-skeleton nnweb)
228 ;;; Internal functions
230 (defun nnweb-read-overview (group)
231 "Read the overview of GROUP and build the map."
232 (when (file-exists-p (nnweb-overview-file group))
233 (mm-with-unibyte-buffer
234 (nnheader-insert-file-contents (nnweb-overview-file group))
235 (goto-char (point-min))
238 (setq header (nnheader-parse-nov))
240 (push (list (mail-header-number header)
241 header (mail-header-xref header))
243 (nnweb-set-hashtb header (car nnweb-articles)))))))
245 (defun nnweb-write-overview (group)
246 "Write the overview file for GROUP."
247 (with-temp-file (nnweb-overview-file group)
248 (let ((articles nnweb-articles))
250 (nnheader-insert-nov (cadr (pop articles)))))))
252 (defun nnweb-set-hashtb (header data)
253 (gnus-sethash (nnweb-identifier (mail-header-xref header))
256 (defun nnweb-get-hashtb (url)
257 (gnus-gethash (nnweb-identifier url) nnweb-hashtb))
259 (defun nnweb-identifier (ident)
260 (funcall (nnweb-definition 'identifier) ident))
262 (defun nnweb-overview-file (group)
263 "Return the name of the overview file of GROUP."
264 (nnheader-concat nnweb-directory group ".overview"))
266 (defun nnweb-write-active ()
267 "Save the active file."
268 (gnus-make-directory nnweb-directory)
269 (with-temp-file (nnheader-concat nnweb-directory "active")
270 (prin1 `(setq nnweb-group-alist ',nnweb-group-alist) (current-buffer))))
272 (defun nnweb-read-active ()
273 "Read the active file."
274 (load (nnheader-concat nnweb-directory "active") t t t))
276 (defun nnweb-definition (type &optional noerror)
277 "Return the definition of TYPE."
278 (let ((def (cdr (assq type (assq nnweb-type nnweb-type-definition)))))
281 (error "Undefined definition %s" type))
284 (defun nnweb-possibly-change-server (&optional group server)
287 (unless (nnweb-server-opened server)
288 (nnweb-open-server server)))
289 (unless nnweb-group-alist
292 (when (and (not nnweb-ephemeral-p)
293 (not (equal group nnweb-group)))
294 (setq nnweb-hashtb (gnus-make-hashtable 4095))
295 (nnweb-request-group group nil t))))
297 (defun nnweb-init (server)
298 "Initialize buffers and such."
299 (unless (gnus-buffer-live-p nnweb-buffer)
303 (nnheader-set-temp-buffer
304 (format " *nnweb %s %s %s*"
305 nnweb-type nnweb-search server))
306 (current-buffer))))))
308 (defun nnweb-fetch-url (url)
311 (if (not nnheader-callback-function)
314 (mm-enable-multibyte)
315 (let ((coding-system-for-read 'binary)
316 (coding-system-for-write 'binary)
317 (default-process-coding-system 'binary))
319 (setq buf (buffer-string)))
323 (nnweb-url-retrieve-asynch
324 url 'nnweb-callback (current-buffer) nnheader-callback-function)
327 (defun nnweb-callback (buffer callback)
328 (when (gnus-buffer-live-p url-working-buffer)
330 (set-buffer url-working-buffer)
331 (funcall (nnweb-definition 'article))
332 (nnweb-decode-entities)
334 (goto-char (point-max))
335 (insert-buffer-substring url-working-buffer))
337 (gnus-kill-buffer url-working-buffer)))
339 (defun nnweb-url-retrieve-asynch (url callback &rest data)
340 (let ((url-request-method "GET")
341 (old-asynch url-be-asynchronous)
342 (url-request-data nil)
343 (url-request-extra-headers nil)
344 (url-working-buffer (generate-new-buffer-name " *nnweb*")))
345 (setq-default url-be-asynchronous t)
347 (set-buffer (get-buffer-create url-working-buffer))
348 (setq url-current-callback-data data
349 url-be-asynchronous t
350 url-current-callback-func callback)
352 (setq-default url-be-asynchronous old-asynch)))
355 ;;; DejaNews functions.
358 (defun nnweb-dejanews-create-mapping ()
359 "Perform the search and create an number-to-url alist."
361 (set-buffer nnweb-buffer)
363 (when (funcall (nnweb-definition 'search) nnweb-search)
367 (active (or (cadr (assoc nnweb-group nnweb-group-alist))
370 map url parse a table group text)
372 ;; Go through all the article hits on this page.
373 (goto-char (point-min))
374 (setq parse (w3-parse-buffer (current-buffer))
375 table (nth 1 (nnweb-parse-find-all 'table parse)))
376 (dolist (row (nth 2 (car (nth 2 table))))
377 (setq a (nnweb-parse-find 'a row)
378 url (cdr (assq 'href (nth 1 a)))
379 text (nreverse (nnweb-text row)))
381 (setq subject (nth 4 text)
385 (if (string-match "\\([0-9]+\\)/\\([0-9]+\\)/\\([0-9]+\\)" date)
386 (setq date (format "%s %s 00:00:00 %s"
387 (car (rassq (string-to-number
388 (match-string 2 date))
390 (match-string 3 date)
391 (match-string 1 date)))
392 (setq date "Jan 1 00:00:00 0000"))
394 (setq url (concat url "&fmt=text"))
395 (when (string-match "&context=[^&]+" url)
396 (setq url (replace-match "" t t url)))
397 (unless (nnweb-get-hashtb url)
401 (make-full-mail-header
402 (cdr active) (concat subject " (" group ")") from date
403 (concat "<" (nnweb-identifier url) "@dejanews>")
406 (nnweb-set-hashtb (cadar map) (car map)))))
407 ;; See whether there is a "Get next 20 hits" button here.
408 (goto-char (point-min))
409 (if (or (not (re-search-forward
410 "HREF=\"\\([^\"]+\\)\"[<>b]+Next result" nil t))
411 (>= i nnweb-max-hits))
414 (setq more (match-string 1))
416 (url-insert-file-contents more)))
417 ;; Return the articles in the right order.
419 (sort (nconc nnweb-articles map) 'car-less-than-car))))))
421 (defun nnweb-dejanews-search (search)
424 (nnweb-definition 'address)
426 (nnweb-encode-www-form-urlencoded
428 ("svcclass" . "dnyr")
430 ("defaultOp" . "AND")
432 ("OP" . "dnquery.xp")
436 ("format" . "verbose2")
437 ("showsort" . "date")
439 ("ageweight" . "1")))))
442 (defun nnweb-dejanewsold-search (search)
444 (nnweb-definition 'address)
445 `(("query" . ,search)
446 ("defaultOp" . "AND")
447 ("svcclass" . "dnold")
449 ("format" . "verbose2")
451 ("showsort" . "date")
453 ("ageweight" . "1")))
456 (defun nnweb-dejanews-identity (url)
457 "Return an unique identifier based on URL."
458 (if (string-match "AN=\\([0-9]+\\)" url)
466 (defun nnweb-reference-create-mapping ()
467 "Perform the search and create an number-to-url alist."
469 (set-buffer nnweb-buffer)
471 (when (funcall (nnweb-definition 'search) nnweb-search)
475 (active (or (cadr (assoc nnweb-group nnweb-group-alist))
477 Subject Score Date Newsgroups From Message-ID
480 ;; Go through all the article hits on this page.
481 (goto-char (point-min))
482 (search-forward "</pre><hr>" nil t)
483 (delete-region (point-min) (point))
484 (goto-char (point-min))
485 (while (re-search-forward "^ +[0-9]+\\." nil t)
488 (if (re-search-forward "^$" nil t)
491 (goto-char (point-min))
492 (when (looking-at ".*href=\"\\([^\"]+\\)\"")
493 (setq url (match-string 1)))
494 (nnweb-remove-markup)
495 (goto-char (point-min))
496 (while (search-forward "\t" nil t)
498 (goto-char (point-min))
499 (while (re-search-forward "^\\([^:]+\\): \\(.*\\)$" nil t)
500 (set (intern (match-string 1)) (match-string 2)))
502 (search-forward "</pre>" nil t)
504 (unless (nnweb-get-hashtb url)
508 (make-full-mail-header
509 (cdr active) (concat "(" Newsgroups ") " Subject) From Date
511 nil 0 (string-to-int Score) url))
513 (nnweb-set-hashtb (cadar map) (car map))))
515 ;; Return the articles in the right order.
517 (sort (nconc nnweb-articles map) 'car-less-than-car))))))
519 (defun nnweb-reference-wash-article ()
520 (let ((case-fold-search t))
521 (goto-char (point-min))
522 (re-search-forward "^</center><hr>" nil t)
523 (delete-region (point-min) (point))
524 (search-forward "<pre>" nil t)
526 (let ((body (point-marker)))
527 (search-forward "</pre>" nil t)
528 (delete-region (point) (point-max))
529 (nnweb-remove-markup)
530 (goto-char (point-min))
531 (while (looking-at " *$")
533 (narrow-to-region (point-min) body)
534 (while (and (re-search-forward "^$" nil t)
537 (goto-char (point-min))
538 (while (looking-at "\\(^[^ ]+:\\) *")
539 (replace-match "\\1 " t)
541 (goto-char (point-min))
542 (when (re-search-forward "^References:" nil t)
544 (point) (if (re-search-forward "^$\\|^[^:]+:" nil t)
547 (goto-char (point-min))
549 (unless (looking-at "References")
552 (goto-char (point-min))
553 (while (search-forward "," nil t)
554 (replace-match " " t t)))
556 (set-marker body nil))))
558 (defun nnweb-reference-search (search)
559 (url-insert-file-contents
561 (nnweb-definition 'address)
563 (nnweb-encode-www-form-urlencoded
564 `(("search" . "advanced")
565 ("querytext" . ,search)
570 ("organization" . "")
573 ("choice" . "Search")
574 ("startmonth" . "Jul")
576 ("startyear" . "1996")
581 ("verbosity" . "Verbose")
582 ("ranking" . "Relevance")
586 (setq buffer-file-name nil)
593 (defun nnweb-altavista-create-mapping ()
594 "Perform the search and create an number-to-url alist."
596 (set-buffer nnweb-buffer)
599 (when (funcall (nnweb-definition 'search) nnweb-search part)
603 (active (or (cadr (assoc nnweb-group nnweb-group-alist))
605 subject date from id group
608 ;; Go through all the article hits on this page.
609 (goto-char (point-min))
610 (search-forward "<dt>" nil t)
611 (delete-region (point-min) (match-beginning 0))
612 (goto-char (point-min))
613 (while (search-forward "<dt>" nil t)
614 (replace-match "\n<blubb>"))
615 (nnweb-decode-entities)
616 (goto-char (point-min))
617 (while (re-search-forward "<blubb>.*href=\"\\([^\"]+\\)\"><strong>\\([^>]*\\)</strong></a><dd>\\([^-]+\\)- <b>\\([^<]+\\)<.*href=\"news:\\([^\"]+\\)\">.*\">\\(.+\\)</a><P>"
619 (setq url (match-string 1)
620 subject (match-string 2)
621 date (match-string 3)
622 group (match-string 4)
623 id (concat "<" (match-string 5) ">")
624 from (match-string 6))
626 (unless (nnweb-get-hashtb url)
630 (make-full-mail-header
631 (cdr active) (concat "(" group ") " subject) from date
634 (nnweb-set-hashtb (cadar map) (car map))))
635 ;; See if we want more.
636 (when (or (not nnweb-articles)
637 (>= i nnweb-max-hits)
638 (not (funcall (nnweb-definition 'search)
639 nnweb-search (incf part))))
641 ;; Return the articles in the right order.
643 (sort (nconc nnweb-articles map) 'car-less-than-car)))))))
645 (defun nnweb-altavista-wash-article ()
646 (goto-char (point-min))
647 (let ((case-fold-search t))
648 (when (re-search-forward "^<strong>" nil t)
649 (delete-region (point-min) (match-beginning 0)))
650 (goto-char (point-min))
651 (while (looking-at "<strong>\\([^ ]+\\) +</strong> +\\(.*\\)$")
652 (replace-match "\\1: \\2" t)
654 (when (re-search-backward "^References:" nil t)
655 (narrow-to-region (point) (progn (forward-line 1) (point)))
656 (goto-char (point-min))
657 (while (re-search-forward "<A.*\\?id@\\([^\"]+\\)\">[0-9]+</A>" nil t)
658 (replace-match "<\\1> " t)))
660 (nnweb-remove-markup)))
662 (defun nnweb-altavista-search (search &optional part)
663 (url-insert-file-contents
665 (nnweb-definition 'address)
667 (nnweb-encode-www-form-urlencoded
670 ,@(when part `(("stq" . ,(int-to-string (* part 30)))))
676 (setq buffer-file-name nil)
680 ;;; General web/w3 interface utility functions
683 (defun nnweb-insert-html (parse)
684 "Insert HTML based on a w3 parse tree."
687 (insert "<" (symbol-name (car parse)) " ")
690 (concat (symbol-name (car param)) "="
692 (if (consp (cdr param))
698 (mapcar 'nnweb-insert-html (nth 2 parse))
699 (insert "</" (symbol-name (car parse)) ">\n")))
701 (defun nnweb-encode-www-form-urlencoded (pairs)
702 "Return PAIRS encoded for forms."
706 (concat (w3-form-encode-xwfu (car data)) "="
707 (w3-form-encode-xwfu (cdr data)))))
710 (defun nnweb-fetch-form (url pairs)
711 "Fetch a form from URL with PAIRS as the data using the POST method."
712 (let ((url-request-data (nnweb-encode-www-form-urlencoded pairs))
713 (url-request-method "POST")
714 (url-request-extra-headers
715 '(("Content-type" . "application/x-www-form-urlencoded"))))
716 (url-insert-file-contents url)
717 (setq buffer-file-name nil))
720 (defun nnweb-decode-entities ()
721 "Decode all HTML entities."
722 (goto-char (point-min))
723 (while (re-search-forward "&\\(#[0-9]+\\|[a-z]+\\);" nil t)
724 (replace-match (char-to-string
725 (if (eq (aref (match-string 1) 0) ?\#)
727 (string-to-number (substring
728 (match-string 1) 1))))
729 (if (mm-char-or-char-int-p c) c 32))
730 (or (cdr (assq (intern (match-string 1))
735 (defun nnweb-decode-entities-string (str)
738 (nnweb-decode-entities)
739 (buffer-substring (point-min) (point-max))))
741 (defun nnweb-remove-markup ()
742 "Remove all HTML markup, leaving just plain text."
743 (goto-char (point-min))
744 (while (search-forward "<!--" nil t)
745 (delete-region (match-beginning 0)
746 (or (search-forward "-->" nil t)
748 (goto-char (point-min))
749 (while (re-search-forward "<[^>]+>" nil t)
750 (replace-match "" t t)))
752 (defun nnweb-insert (url &optional follow-refresh)
753 "Insert the contents from an URL in the current buffer.
754 If FOLLOW-REFRESH is non-nil, redirect refresh url in META."
755 (let ((name buffer-file-name))
758 (narrow-to-region (point) (point))
759 (url-insert-file-contents url)
760 (goto-char (point-min))
761 (when (re-search-forward
762 "<meta[ \t\r\n]*http-equiv=\"Refresh\"[^>]*URL=\\([^\"]+\\)\"" nil t)
763 (let ((url (match-string 1)))
764 (delete-region (point-min) (point-max))
765 (nnweb-insert url t))))
766 (url-insert-file-contents url))
767 (setq buffer-file-name name)))
769 (defun nnweb-parse-find (type parse &optional maxdepth)
770 "Find the element of TYPE in PARSE."
772 (nnweb-parse-find-1 type parse maxdepth)))
774 (defun nnweb-parse-find-1 (type contents maxdepth)
775 (when (or (null maxdepth)
776 (not (zerop maxdepth)))
777 (when (consp contents)
778 (when (eq (car contents) type)
779 (throw 'found contents))
780 (when (listp (cdr contents))
781 (dolist (element contents)
782 (when (consp element)
783 (nnweb-parse-find-1 type element
784 (and maxdepth (1- maxdepth)))))))))
786 (defun nnweb-parse-find-all (type parse)
787 "Find all elements of TYPE in PARSE."
789 (nnweb-parse-find-all-1 type parse)))
791 (defun nnweb-parse-find-all-1 (type contents)
793 (when (consp contents)
794 (if (eq (car contents) type)
795 (push contents result)
796 (when (listp (cdr contents))
797 (dolist (element contents)
798 (when (consp element)
800 (nconc result (nnweb-parse-find-all-1 type element))))))))
804 (defun nnweb-text (parse)
805 "Return a list of text contents in PARSE."
806 (let ((nnweb-text nil))
808 (nreverse nnweb-text)))
810 (defun nnweb-text-1 (contents)
811 (dolist (element contents)
812 (if (stringp element)
813 (push element nnweb-text)
814 (when (and (consp element)
815 (listp (cdr element)))
816 (nnweb-text-1 element)))))
820 ;;; nnweb.el ends here