1 ;;; nnweb.el --- retrieving articles via web search engines
2 ;; Copyright (C) 1996,97,98,99 Free Software Foundation, Inc.
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
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 by
11 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU 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' installed for this
31 (eval-when-compile (require 'cl))
44 ;; Report failure to find w3 at load time if appropriate.
52 (defvoo nnweb-directory (nnheader-concat gnus-directory "nnweb/")
53 "Where nnweb will save its files.")
55 (defvoo nnweb-type 'dejanews
56 "What search engine type is being used.
57 Valid types include `dejanews', `dejanewsold', `reference',
60 (defvoo nnweb-type-definition
62 (article . nnweb-dejanews-wash-article)
63 (map . nnweb-dejanews-create-mapping)
64 (search . nnweb-dejanews-search)
65 (address . "http://x8.dejanews.com/dnquery.xp")
66 (identifier . nnweb-dejanews-identity))
68 (article . nnweb-dejanews-wash-article)
69 (map . nnweb-dejanews-create-mapping)
70 (search . nnweb-dejanewsold-search)
71 (address . "http://x8.dejanews.com/dnquery.xp")
72 (identifier . nnweb-dejanews-identity))
74 (article . nnweb-reference-wash-article)
75 (map . nnweb-reference-create-mapping)
76 (search . nnweb-reference-search)
77 (address . "http://www.reference.com/cgi-bin/pn/go")
78 (identifier . identity))
80 (article . nnweb-altavista-wash-article)
81 (map . nnweb-altavista-create-mapping)
82 (search . nnweb-altavista-search)
83 (address . "http://www.altavista.digital.com/cgi-bin/query")
84 (id . "/cgi-bin/news?id@%s")
85 (identifier . identity)))
86 "Type-definition alist.")
88 (defvoo nnweb-search nil
89 "Search string to feed to DejaNews.")
91 (defvoo nnweb-max-hits 999
92 "Maximum number of hits to display.")
94 (defvoo nnweb-ephemeral-p nil
95 "Whether this nnweb server is ephemeral.")
97 ;;; Internal variables
99 (defvoo nnweb-articles nil)
100 (defvoo nnweb-buffer nil)
101 (defvoo nnweb-group-alist nil)
102 (defvoo nnweb-group nil)
103 (defvoo nnweb-hashtb nil)
105 ;;; Interface functions
107 (nnoo-define-basics nnweb)
109 (deffoo nnweb-retrieve-headers (articles &optional group server fetch-old)
110 (nnweb-possibly-change-server group server)
112 (set-buffer nntp-server-buffer)
114 (let (article header)
115 (while (setq article (pop articles))
116 (when (setq header (cadr (assq article nnweb-articles)))
117 (nnheader-insert-nov header)))
120 (deffoo nnweb-request-scan (&optional group server)
121 (nnweb-possibly-change-server group server)
122 (setq nnweb-hashtb (gnus-make-hashtable 4095))
123 (funcall (nnweb-definition 'map))
124 (unless nnweb-ephemeral-p
126 (nnweb-write-overview group)))
128 (deffoo nnweb-request-group (group &optional server dont-check)
129 (nnweb-possibly-change-server nil server)
131 (not (equal group nnweb-group))
132 (not nnweb-ephemeral-p))
133 (let ((info (assoc group nnweb-group-alist)))
134 (setq nnweb-group group)
135 (setq nnweb-type (nth 2 info))
136 (setq nnweb-search (nth 3 info))
138 (nnweb-read-overview group))))
140 ((not nnweb-articles)
141 (nnheader-report 'nnweb "No matching articles"))
143 (let ((active (if nnweb-ephemeral-p
144 (cons (caar nnweb-articles)
145 (caar (last nnweb-articles)))
146 (cadr (assoc group nnweb-group-alist)))))
147 (nnheader-report 'nnweb "Opened group %s" group)
149 "211 %d %d %d %s\n" (length nnweb-articles)
150 (car active) (cdr active) group)))))
152 (deffoo nnweb-close-group (group &optional server)
153 (nnweb-possibly-change-server group server)
154 (when (gnus-buffer-live-p nnweb-buffer)
156 (set-buffer nnweb-buffer)
157 (set-buffer-modified-p nil)
158 (kill-buffer nnweb-buffer)))
161 (deffoo nnweb-request-article (article &optional group server buffer)
162 (nnweb-possibly-change-server group server)
164 (set-buffer (or buffer nntp-server-buffer))
165 (let* ((header (cadr (assq article nnweb-articles)))
166 (url (and header (mail-header-xref header))))
168 (nnweb-fetch-url url))
169 (and (stringp article)
170 (nnweb-definition 'id t)
171 (let ((fetch (nnweb-definition 'id))
173 (when (string-match "^<\\(.*\\)>$" article)
174 (setq art (match-string 1 article)))
178 (format fetch article))))))
179 (unless nnheader-callback-function
180 (funcall (nnweb-definition 'article))
181 (nnweb-decode-entities))
182 (nnheader-report 'nnweb "Fetched article %s" article)
185 (deffoo nnweb-close-server (&optional server)
186 (when (and (nnweb-server-opened server)
187 (gnus-buffer-live-p nnweb-buffer))
189 (set-buffer nnweb-buffer)
190 (set-buffer-modified-p nil)
191 (kill-buffer nnweb-buffer)))
192 (nnoo-close-server 'nnweb server))
194 (deffoo nnweb-request-list (&optional server)
195 (nnweb-possibly-change-server nil server)
197 (set-buffer nntp-server-buffer)
198 (nnmail-generate-active nnweb-group-alist)
201 (deffoo nnweb-request-update-info (group info &optional server)
202 (nnweb-possibly-change-server group server)
203 ;;(setcar (cddr info) nil)
206 (deffoo nnweb-asynchronous-p ()
209 (deffoo nnweb-request-create-group (group &optional server args)
210 (nnweb-possibly-change-server nil server)
211 (nnweb-request-delete-group group)
212 (push `(,group ,(cons 1 0) ,@args) nnweb-group-alist)
216 (deffoo nnweb-request-delete-group (group &optional force server)
217 (nnweb-possibly-change-server group server)
218 (gnus-pull group nnweb-group-alist t)
220 (gnus-delete-file (nnweb-overview-file group))
223 (nnoo-define-skeleton nnweb)
225 ;;; Internal functions
227 (defun nnweb-read-overview (group)
228 "Read the overview of GROUP and build the map."
229 (when (file-exists-p (nnweb-overview-file group))
231 (nnheader-insert-file-contents (nnweb-overview-file group))
232 (goto-char (point-min))
235 (setq header (nnheader-parse-nov))
237 (push (list (mail-header-number header)
238 header (mail-header-xref header))
240 (nnweb-set-hashtb header (car nnweb-articles)))))))
242 (defun nnweb-write-overview (group)
243 "Write the overview file for GROUP."
244 (with-temp-file (nnweb-overview-file group)
245 (let ((articles nnweb-articles))
247 (nnheader-insert-nov (cadr (pop articles)))))))
249 (defun nnweb-set-hashtb (header data)
250 (gnus-sethash (nnweb-identifier (mail-header-xref header))
253 (defun nnweb-get-hashtb (url)
254 (gnus-gethash (nnweb-identifier url) nnweb-hashtb))
256 (defun nnweb-identifier (ident)
257 (funcall (nnweb-definition 'identifier) ident))
259 (defun nnweb-overview-file (group)
260 "Return the name of the overview file of GROUP."
261 (nnheader-concat nnweb-directory group ".overview"))
263 (defun nnweb-write-active ()
264 "Save the active file."
265 (with-temp-file (nnheader-concat nnweb-directory "active")
266 (prin1 `(setq nnweb-group-alist ',nnweb-group-alist) (current-buffer))))
268 (defun nnweb-read-active ()
269 "Read the active file."
270 (load (nnheader-concat nnweb-directory "active") t t t))
272 (defun nnweb-definition (type &optional noerror)
273 "Return the definition of TYPE."
274 (let ((def (cdr (assq type (assq nnweb-type nnweb-type-definition)))))
277 (error "Undefined definition %s" type))
280 (defun nnweb-possibly-change-server (&optional group server)
283 (unless (nnweb-server-opened server)
284 (nnweb-open-server server)))
285 (unless nnweb-group-alist
288 (when (and (not nnweb-ephemeral-p)
289 (not (equal group nnweb-group)))
290 (nnweb-request-group group nil t))))
292 (defun nnweb-init (server)
293 "Initialize buffers and such."
294 (unless (gnus-buffer-live-p nnweb-buffer)
297 (nnheader-set-temp-buffer
298 (format " *nnweb %s %s %s*" nnweb-type nnweb-search server))))))
300 (defun nnweb-fetch-url (url)
302 (if (not nnheader-callback-function)
303 (let ((buf (current-buffer)))
305 (set-buffer nnweb-buffer)
307 (url-insert-file-contents url)
308 (copy-to-buffer buf (point-min) (point-max))
310 (nnweb-url-retrieve-asynch
311 url 'nnweb-callback (current-buffer) nnheader-callback-function)
314 (defun nnweb-callback (buffer callback)
315 (when (gnus-buffer-live-p url-working-buffer)
317 (set-buffer url-working-buffer)
318 (funcall (nnweb-definition 'article))
319 (nnweb-decode-entities)
321 (goto-char (point-max))
322 (insert-buffer-substring url-working-buffer))
324 (gnus-kill-buffer url-working-buffer)))
326 (defun nnweb-url-retrieve-asynch (url callback &rest data)
327 (let ((url-request-method "GET")
328 (old-asynch url-be-asynchronous)
329 (url-request-data nil)
330 (url-request-extra-headers nil)
331 (url-working-buffer (generate-new-buffer-name " *nnweb*")))
332 (setq-default url-be-asynchronous t)
334 (set-buffer (get-buffer-create url-working-buffer))
335 (setq url-current-callback-data data
336 url-be-asynchronous t
337 url-current-callback-func callback)
339 (setq-default url-be-asynchronous old-asynch)))
342 ;;; DejaNews functions.
345 (defun nnweb-dejanews-create-mapping ()
346 "Perform the search and create an number-to-url alist."
348 (set-buffer nnweb-buffer)
350 (when (funcall (nnweb-definition 'search) nnweb-search)
354 (active (or (cadr (assoc nnweb-group nnweb-group-alist))
356 Subject (Score "0") Date Newsgroup Author
359 ;; Go through all the article hits on this page.
360 (goto-char (point-min))
361 (nnweb-decode-entities)
362 (goto-char (point-min))
363 (while (re-search-forward "^ <P>\n" nil t)
366 (cond ((re-search-forward "^ <P>\n" nil t)
368 ((search-forward "\n\n" nil t)
372 (goto-char (point-min))
373 (looking-at ".*HREF=\"\\([^\"]+\\)\"\\(.*\\)")
374 (setq url (match-string 1))
375 (let ((begin (point)))
376 (nnweb-remove-markup)
378 (while (search-forward "\t" nil t)
382 (setq Subject (buffer-substring begin (point)))
383 (if (re-search-forward
384 "^ Newsgroup: \\(.*\\)\n Posted on \\([0-9/]+\\) by \\(.*\\)$" nil t)
385 (setq Newsgroup (match-string 1)
386 Date (match-string 2)
387 Author (match-string 3))))
390 (unless (nnweb-get-hashtb url)
394 (make-full-mail-header
395 (cdr active) Subject Author Date
396 (concat "<" (nnweb-identifier url) "@dejanews>")
397 nil 0 (string-to-int Score) url))
399 (nnweb-set-hashtb (cadar map) (car map))))
400 ;; See whether there is a "Get next 20 hits" button here.
401 (if (or (not (re-search-forward
402 "HREF=\"\\([^\"]+\\)\"[<>b]+Next result" nil t))
403 (>= i nnweb-max-hits))
406 (setq more (match-string 1))
408 (url-insert-file-contents more)))
409 ;; Return the articles in the right order.
411 (sort (nconc nnweb-articles map) 'car-less-than-car))))))
413 (defun nnweb-dejanews-wash-article ()
414 (let ((case-fold-search t))
415 (goto-char (point-min))
416 (re-search-forward "<PRE>" nil t)
417 (delete-region (point-min) (point))
418 (re-search-forward "</PRE>" nil t)
419 (delete-region (point) (point-max))
420 (nnweb-remove-markup)
421 (goto-char (point-min))
422 (while (and (looking-at " *$")
425 (while (looking-at "\\(^[^ ]+:\\) *")
426 (replace-match "\\1 " t)
428 (when (re-search-forward "\n\n+" nil t)
429 (replace-match "\n" t t))
430 (goto-char (point-min))
431 (when (search-forward "[More Headers]" nil t)
432 (replace-match "" t t))))
434 (defun nnweb-dejanews-search (search)
436 (nnweb-definition 'address)
437 `(("query" . ,search)
438 ("defaultOp" . "AND")
439 ("svcclass" . "dncurrent")
441 ("format" . "verbose2")
443 ("showsort" . "date")
445 ("ageweight" . "1")))
448 (defun nnweb-dejanewsold-search (search)
450 (nnweb-definition 'address)
451 `(("query" . ,search)
452 ("defaultOp" . "AND")
453 ("svcclass" . "dnold")
455 ("format" . "verbose2")
457 ("showsort" . "date")
459 ("ageweight" . "1")))
462 (defun nnweb-dejanews-identity (url)
463 "Return an unique identifier based on URL."
464 (if (string-match "recnum=\\([0-9]+\\)" url)
472 (defun nnweb-reference-create-mapping ()
473 "Perform the search and create an number-to-url alist."
475 (set-buffer nnweb-buffer)
477 (when (funcall (nnweb-definition 'search) nnweb-search)
481 (active (or (cadr (assoc nnweb-group nnweb-group-alist))
483 Subject Score Date Newsgroups From Message-ID
486 ;; Go through all the article hits on this page.
487 (goto-char (point-min))
488 (search-forward "</pre><hr>" nil t)
489 (delete-region (point-min) (point))
490 ;(nnweb-decode-entities)
491 (goto-char (point-min))
492 (while (re-search-forward "^ +[0-9]+\\." nil t)
495 (if (re-search-forward "^$" nil t)
498 (goto-char (point-min))
499 (when (looking-at ".*href=\"\\([^\"]+\\)\"")
500 (setq url (match-string 1)))
501 (nnweb-remove-markup)
502 (goto-char (point-min))
503 (while (search-forward "\t" nil t)
505 (goto-char (point-min))
506 (while (re-search-forward "^\\([^:]+\\): \\(.*\\)$" nil t)
507 (set (intern (match-string 1)) (match-string 2)))
509 (search-forward "</pre>" nil t)
511 (unless (nnweb-get-hashtb url)
515 (make-full-mail-header
516 (cdr active) (concat "(" Newsgroups ") " Subject) From Date
518 nil 0 (string-to-int Score) url))
520 (nnweb-set-hashtb (cadar map) (car map))))
522 ;; Return the articles in the right order.
524 (sort (nconc nnweb-articles map) 'car-less-than-car))))))
526 (defun nnweb-reference-wash-article ()
527 (let ((case-fold-search t))
528 (goto-char (point-min))
529 (re-search-forward "^</center><hr>" nil t)
530 (delete-region (point-min) (point))
531 (search-forward "<pre>" nil t)
533 (let ((body (point-marker)))
534 (search-forward "</pre>" nil t)
535 (delete-region (point) (point-max))
536 (nnweb-remove-markup)
537 (goto-char (point-min))
538 (while (looking-at " *$")
540 (narrow-to-region (point-min) body)
541 (while (and (re-search-forward "^$" nil t)
544 (goto-char (point-min))
545 (while (looking-at "\\(^[^ ]+:\\) *")
546 (replace-match "\\1 " t)
548 (goto-char (point-min))
549 (when (re-search-forward "^References:" nil t)
551 (point) (if (re-search-forward "^$\\|^[^:]+:" nil t)
554 (goto-char (point-min))
556 (unless (looking-at "References")
559 (goto-char (point-min))
560 (while (search-forward "," nil t)
561 (replace-match " " t t)))
563 (set-marker body nil))))
565 (defun nnweb-reference-search (search)
566 (url-insert-file-contents
568 (nnweb-definition 'address)
570 (nnweb-encode-www-form-urlencoded
571 `(("search" . "advanced")
572 ("querytext" . ,search)
577 ("organization" . "")
580 ("choice" . "Search")
581 ("startmonth" . "Jul")
583 ("startyear" . "1996")
588 ("verbosity" . "Verbose")
589 ("ranking" . "Relevance")
593 (setq buffer-file-name nil)
600 (defun nnweb-altavista-create-mapping ()
601 "Perform the search and create an number-to-url alist."
603 (set-buffer nnweb-buffer)
606 (when (funcall (nnweb-definition 'search) nnweb-search part)
610 (active (or (cadr (assoc nnweb-group nnweb-group-alist))
612 subject date from id group
615 ;; Go through all the article hits on this page.
616 (goto-char (point-min))
617 (search-forward "<dt>" nil t)
618 (delete-region (point-min) (match-beginning 0))
619 (goto-char (point-min))
620 (while (search-forward "<dt>" nil t)
621 (replace-match "\n<blubb>"))
622 (nnweb-decode-entities)
623 (goto-char (point-min))
624 (while (re-search-forward "<blubb>.*href=\"\\([^\"]+\\)\"><strong>\\([^>]*\\)</strong></a><dd>\\([^-]+\\)- <b>\\([^<]+\\)<.*href=\"news:\\([^\"]+\\)\">.*\">\\(.+\\)</a><P>"
626 (setq url (match-string 1)
627 subject (match-string 2)
628 date (match-string 3)
629 group (match-string 4)
630 id (concat "<" (match-string 5) ">")
631 from (match-string 6))
633 (unless (nnweb-get-hashtb url)
637 (make-full-mail-header
638 (cdr active) (concat "(" group ") " subject) from date
641 (nnweb-set-hashtb (cadar map) (car map))))
642 ;; See if we want more.
643 (when (or (not nnweb-articles)
644 (>= i nnweb-max-hits)
645 (not (funcall (nnweb-definition 'search)
646 nnweb-search (incf part))))
648 ;; Return the articles in the right order.
650 (sort (nconc nnweb-articles map) 'car-less-than-car)))))))
652 (defun nnweb-altavista-wash-article ()
653 (goto-char (point-min))
654 (let ((case-fold-search t))
655 (when (re-search-forward "^<strong>" nil t)
656 (delete-region (point-min) (match-beginning 0)))
657 (goto-char (point-min))
658 (while (looking-at "<strong>\\([^ ]+\\) +</strong> +\\(.*\\)$")
659 (replace-match "\\1: \\2" t)
661 (when (re-search-backward "^References:" nil t)
662 (narrow-to-region (point) (progn (forward-line 1) (point)))
663 (goto-char (point-min))
664 (while (re-search-forward "<A.*\\?id@\\([^\"]+\\)\">[0-9]+</A>" nil t)
665 (replace-match "<\\1> " t)))
667 (nnweb-remove-markup)))
669 (defun nnweb-altavista-search (search &optional part)
670 (url-insert-file-contents
672 (nnweb-definition 'address)
674 (nnweb-encode-www-form-urlencoded
677 ,@(when part `(("stq" . ,(int-to-string (* part 30)))))
683 (setq buffer-file-name nil)
687 ;;; General web/w3 interface utility functions
690 (defun nnweb-insert-html (parse)
691 "Insert HTML based on a w3 parse tree."
694 (insert "<" (symbol-name (car parse)) " ")
697 (concat (symbol-name (car param)) "="
699 (if (consp (cdr param))
705 (mapcar 'nnweb-insert-html (nth 2 parse))
706 (insert "</" (symbol-name (car parse)) ">\n")))
708 (defun nnweb-encode-www-form-urlencoded (pairs)
709 "Return PAIRS encoded for forms."
713 (concat (w3-form-encode-xwfu (car data)) "="
714 (w3-form-encode-xwfu (cdr data)))))
717 (defun nnweb-fetch-form (url pairs)
718 "Fetch a form from URL with PAIRS as the data."
719 (let ((url-request-data (nnweb-encode-www-form-urlencoded pairs))
720 (url-request-method "POST")
721 (url-request-extra-headers
722 '(("Content-type" . "application/x-www-form-urlencoded"))))
723 (url-insert-file-contents url)
724 (setq buffer-file-name nil))
727 (defun nnweb-decode-entities ()
728 "Decode all HTML entities."
729 (goto-char (point-min))
730 (while (re-search-forward "&\\([a-z]+\\);" nil t)
731 (replace-match (char-to-string (or (cdr (assq (intern (match-string 1))
736 (defun nnweb-remove-markup ()
737 "Remove all HTML markup, leaving just plain text."
738 (goto-char (point-min))
739 (while (search-forward "<!--" nil t)
740 (delete-region (match-beginning 0)
741 (or (search-forward "-->" nil t)
743 (goto-char (point-min))
744 (while (re-search-forward "<[^>]+>" nil t)
745 (replace-match "" t t)))
747 (defun nnweb-insert (url)
748 "Insert the contents from an URL in the current buffer."
749 (let ((name buffer-file-name))
750 (url-insert-file-contents url)
751 (setq buffer-file-name name)))
753 (defun nnweb-parse-find (type parse &optional maxdepth)
754 "Find the element of TYPE in PARSE."
756 (nnweb-parse-find-1 type parse maxdepth)))
758 (defun nnweb-parse-find-1 (type contents maxdepth)
759 (when (or (null maxdepth)
760 (not (zerop maxdepth)))
761 (when (consp contents)
762 (when (eq (car contents) type)
763 (throw 'found contents))
764 (when (listp (cdr contents))
765 (dolist (element contents)
766 (when (consp element)
767 (nnweb-parse-find-1 type element
768 (and maxdepth (1- maxdepth)))))))))
770 (defun nnweb-parse-find-all (type parse)
771 "Find all elements of TYPE in PARSE."
773 (nnweb-parse-find-all-1 type parse)))
775 (defun nnweb-parse-find-all-1 (type contents)
777 (when (consp contents)
778 (if (eq (car contents) type)
779 (push contents result)
780 (when (listp (cdr contents))
781 (dolist (element contents)
782 (when (consp element)
784 (nconc result (nnweb-parse-find-all-1 type element))))))))
788 (defun nnweb-text (parse)
789 "Return a list of text contents in PARSE."
790 (let ((nnweb-text nil))
792 (nreverse nnweb-text)))
794 (defun nnweb-text-1 (contents)
795 (dolist (element contents)
796 (if (stringp element)
797 (push element nnweb-text)
798 (when (and (consp element)
799 (listp (cdr element)))
800 (nnweb-text-1 element)))))
804 ;;; nnweb.el ends here