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))
43 ;; Report failure to find w3 at load time if appropriate.
51 (defvoo nnweb-directory (nnheader-concat gnus-directory "nnweb/")
52 "Where nnweb will save its files.")
54 (defvoo nnweb-type 'dejanews
55 "What search engine type is being used.
56 Valid types include `dejanews', `dejanewsold', `reference',
59 (defvoo nnweb-type-definition
61 (article . nnweb-dejanews-wash-article)
62 (map . nnweb-dejanews-create-mapping)
63 (search . nnweb-dejanews-search)
64 (address . "http://x8.dejanews.com/dnquery.xp")
65 (identifier . nnweb-dejanews-identity))
67 (article . nnweb-dejanews-wash-article)
68 (map . nnweb-dejanews-create-mapping)
69 (search . nnweb-dejanewsold-search)
70 (address . "http://x8.dejanews.com/dnquery.xp")
71 (identifier . nnweb-dejanews-identity))
73 (article . nnweb-reference-wash-article)
74 (map . nnweb-reference-create-mapping)
75 (search . nnweb-reference-search)
76 (address . "http://www.reference.com/cgi-bin/pn/go")
77 (identifier . identity))
79 (article . nnweb-altavista-wash-article)
80 (map . nnweb-altavista-create-mapping)
81 (search . nnweb-altavista-search)
82 (address . "http://www.altavista.digital.com/cgi-bin/query")
83 (id . "/cgi-bin/news?id@%s")
84 (identifier . identity)))
85 "Type-definition alist.")
87 (defvoo nnweb-search nil
88 "Search string to feed to DejaNews.")
90 (defvoo nnweb-max-hits 999
91 "Maximum number of hits to display.")
93 (defvoo nnweb-ephemeral-p nil
94 "Whether this nnweb server is ephemeral.")
96 ;;; Internal variables
98 (defvoo nnweb-articles nil)
99 (defvoo nnweb-buffer nil)
100 (defvoo nnweb-group-alist nil)
101 (defvoo nnweb-group nil)
102 (defvoo nnweb-hashtb nil)
104 ;;; Interface functions
106 (nnoo-define-basics nnweb)
108 (deffoo nnweb-retrieve-headers (articles &optional group server fetch-old)
109 (nnweb-possibly-change-server group server)
111 (set-buffer nntp-server-buffer)
113 (let (article header)
114 (while (setq article (pop articles))
115 (when (setq header (cadr (assq article nnweb-articles)))
116 (nnheader-insert-nov header)))
119 (deffoo nnweb-request-scan (&optional group server)
120 (nnweb-possibly-change-server group server)
121 (setq nnweb-hashtb (gnus-make-hashtable 4095))
122 (funcall (nnweb-definition 'map))
123 (unless nnweb-ephemeral-p
125 (nnweb-write-overview group)))
127 (deffoo nnweb-request-group (group &optional server dont-check)
128 (nnweb-possibly-change-server nil server)
130 (not (equal group nnweb-group))
131 (not nnweb-ephemeral-p))
132 (let ((info (assoc group nnweb-group-alist)))
133 (setq nnweb-group group)
134 (setq nnweb-type (nth 2 info))
135 (setq nnweb-search (nth 3 info))
137 (nnweb-read-overview group))))
139 ((not nnweb-articles)
140 (nnheader-report 'nnweb "No matching articles"))
142 (let ((active (if nnweb-ephemeral-p
143 (cons (caar nnweb-articles)
144 (caar (last nnweb-articles)))
145 (cadr (assoc group nnweb-group-alist)))))
146 (nnheader-report 'nnweb "Opened group %s" group)
148 "211 %d %d %d %s\n" (length nnweb-articles)
149 (car active) (cdr active) group)))))
151 (deffoo nnweb-close-group (group &optional server)
152 (nnweb-possibly-change-server group server)
153 (when (gnus-buffer-live-p nnweb-buffer)
155 (set-buffer nnweb-buffer)
156 (set-buffer-modified-p nil)
157 (kill-buffer nnweb-buffer)))
160 (deffoo nnweb-request-article (article &optional group server buffer)
161 (nnweb-possibly-change-server group server)
163 (set-buffer (or buffer nntp-server-buffer))
164 (let* ((header (cadr (assq article nnweb-articles)))
165 (url (and header (mail-header-xref header))))
167 (nnweb-fetch-url url))
168 (and (stringp article)
169 (nnweb-definition 'id t)
170 (let ((fetch (nnweb-definition 'id))
172 (when (string-match "^<\\(.*\\)>$" article)
173 (setq art (match-string 1 article)))
177 (format fetch article))))))
178 (unless nnheader-callback-function
179 (funcall (nnweb-definition 'article))
180 (nnweb-decode-entities))
181 (nnheader-report 'nnweb "Fetched article %s" article)
184 (deffoo nnweb-close-server (&optional server)
185 (when (and (nnweb-server-opened server)
186 (gnus-buffer-live-p nnweb-buffer))
188 (set-buffer nnweb-buffer)
189 (set-buffer-modified-p nil)
190 (kill-buffer nnweb-buffer)))
191 (nnoo-close-server 'nnweb server))
193 (deffoo nnweb-request-list (&optional server)
194 (nnweb-possibly-change-server nil server)
196 (set-buffer nntp-server-buffer)
197 (nnmail-generate-active nnweb-group-alist)
200 (deffoo nnweb-request-update-info (group info &optional server)
201 (nnweb-possibly-change-server group server)
202 ;;(setcar (cddr info) nil)
205 (deffoo nnweb-asynchronous-p ()
208 (deffoo nnweb-request-create-group (group &optional server args)
209 (nnweb-possibly-change-server nil server)
210 (nnweb-request-delete-group group)
211 (push `(,group ,(cons 1 0) ,@args) nnweb-group-alist)
215 (deffoo nnweb-request-delete-group (group &optional force server)
216 (nnweb-possibly-change-server group server)
217 (gnus-pull group nnweb-group-alist t)
219 (gnus-delete-file (nnweb-overview-file group))
222 (nnoo-define-skeleton nnweb)
224 ;;; Internal functions
226 (defun nnweb-read-overview (group)
227 "Read the overview of GROUP and build the map."
228 (when (file-exists-p (nnweb-overview-file group))
230 (nnheader-insert-file-contents (nnweb-overview-file group))
231 (goto-char (point-min))
234 (setq header (nnheader-parse-nov))
236 (push (list (mail-header-number header)
237 header (mail-header-xref header))
239 (nnweb-set-hashtb header (car nnweb-articles)))))))
241 (defun nnweb-write-overview (group)
242 "Write the overview file for GROUP."
243 (with-temp-file (nnweb-overview-file group)
244 (let ((articles nnweb-articles))
246 (nnheader-insert-nov (cadr (pop articles)))))))
248 (defun nnweb-set-hashtb (header data)
249 (gnus-sethash (nnweb-identifier (mail-header-xref header))
252 (defun nnweb-get-hashtb (url)
253 (gnus-gethash (nnweb-identifier url) nnweb-hashtb))
255 (defun nnweb-identifier (ident)
256 (funcall (nnweb-definition 'identifier) ident))
258 (defun nnweb-overview-file (group)
259 "Return the name of the overview file of GROUP."
260 (nnheader-concat nnweb-directory group ".overview"))
262 (defun nnweb-write-active ()
263 "Save the active file."
264 (with-temp-file (nnheader-concat nnweb-directory "active")
265 (prin1 `(setq nnweb-group-alist ',nnweb-group-alist) (current-buffer))))
267 (defun nnweb-read-active ()
268 "Read the active file."
269 (load (nnheader-concat nnweb-directory "active") t t t))
271 (defun nnweb-definition (type &optional noerror)
272 "Return the definition of TYPE."
273 (let ((def (cdr (assq type (assq nnweb-type nnweb-type-definition)))))
276 (error "Undefined definition %s" type))
279 (defun nnweb-possibly-change-server (&optional group server)
282 (unless (nnweb-server-opened server)
283 (nnweb-open-server server)))
284 (unless nnweb-group-alist
287 (when (and (not nnweb-ephemeral-p)
288 (not (equal group nnweb-group)))
289 (nnweb-request-group group nil t))))
291 (defun nnweb-init (server)
292 "Initialize buffers and such."
293 (unless (gnus-buffer-live-p nnweb-buffer)
296 (nnheader-set-temp-buffer
297 (format " *nnweb %s %s %s*" nnweb-type nnweb-search server))))))
299 (defun nnweb-fetch-url (url)
301 (if (not nnheader-callback-function)
302 (let ((buf (current-buffer)))
304 (set-buffer nnweb-buffer)
306 (url-insert-file-contents url)
307 (copy-to-buffer buf (point-min) (point-max))
309 (nnweb-url-retrieve-asynch
310 url 'nnweb-callback (current-buffer) nnheader-callback-function)
313 (defun nnweb-callback (buffer callback)
314 (when (gnus-buffer-live-p url-working-buffer)
316 (set-buffer url-working-buffer)
317 (funcall (nnweb-definition 'article))
318 (nnweb-decode-entities)
320 (goto-char (point-max))
321 (insert-buffer-substring url-working-buffer))
323 (gnus-kill-buffer url-working-buffer)))
325 (defun nnweb-url-retrieve-asynch (url callback &rest data)
326 (let ((url-request-method "GET")
327 (old-asynch url-be-asynchronous)
328 (url-request-data nil)
329 (url-request-extra-headers nil)
330 (url-working-buffer (generate-new-buffer-name " *nnweb*")))
331 (setq-default url-be-asynchronous t)
333 (set-buffer (get-buffer-create url-working-buffer))
334 (setq url-current-callback-data data
335 url-be-asynchronous t
336 url-current-callback-func callback)
338 (setq-default url-be-asynchronous old-asynch)))
340 (defun nnweb-encode-www-form-urlencoded (pairs)
341 "Return PAIRS encoded for forms."
345 (concat (w3-form-encode-xwfu (car data)) "="
346 (w3-form-encode-xwfu (cdr data)))))
349 (defun nnweb-fetch-form (url pairs)
350 (let ((url-request-data (nnweb-encode-www-form-urlencoded pairs))
351 (url-request-method "POST")
352 (url-request-extra-headers
353 '(("Content-type" . "application/x-www-form-urlencoded"))))
354 (url-insert-file-contents url)
355 (setq buffer-file-name nil))
358 (defun nnweb-decode-entities ()
359 (goto-char (point-min))
360 (while (re-search-forward "&\\([a-z]+\\);" nil t)
361 (replace-match (char-to-string (or (cdr (assq (intern (match-string 1))
366 (defun nnweb-remove-markup ()
367 (goto-char (point-min))
368 (while (search-forward "<!--" nil t)
369 (delete-region (match-beginning 0)
370 (or (search-forward "-->" nil t)
372 (goto-char (point-min))
373 (while (re-search-forward "<[^>]+>" nil t)
374 (replace-match "" t t)))
377 ;;; DejaNews functions.
380 (defun nnweb-dejanews-create-mapping ()
381 "Perform the search and create an number-to-url alist."
383 (set-buffer nnweb-buffer)
385 (when (funcall (nnweb-definition 'search) nnweb-search)
389 (active (or (cadr (assoc nnweb-group nnweb-group-alist))
391 Subject (Score "0") Date Newsgroup Author
394 ;; Go through all the article hits on this page.
395 (goto-char (point-min))
396 (nnweb-decode-entities)
397 (goto-char (point-min))
398 (while (re-search-forward "^ <P>\n" nil t)
401 (cond ((re-search-forward "^ <P>\n" nil t)
403 ((search-forward "\n\n" nil t)
407 (goto-char (point-min))
408 (looking-at ".*HREF=\"\\([^\"]+\\)\"\\(.*\\)")
409 (setq url (match-string 1))
410 (let ((begin (point)))
411 (nnweb-remove-markup)
413 (while (search-forward "\t" nil t)
417 (setq Subject (buffer-substring begin (point)))
418 (if (re-search-forward
419 "^ Newsgroup: \\(.*\\)\n Posted on \\([0-9/]+\\) by \\(.*\\)$" nil t)
420 (setq Newsgroup (match-string 1)
421 Date (match-string 2)
422 Author (match-string 3))))
425 (unless (nnweb-get-hashtb url)
429 (make-full-mail-header
430 (cdr active) Subject Author Date
431 (concat "<" (nnweb-identifier url) "@dejanews>")
432 nil 0 (string-to-int Score) url))
434 (nnweb-set-hashtb (cadar map) (car map))))
435 ;; See whether there is a "Get next 20 hits" button here.
436 (if (or (not (re-search-forward
437 "HREF=\"\\([^\"]+\\)\"[<>b]+Next result" nil t))
438 (>= i nnweb-max-hits))
441 (setq more (match-string 1))
443 (url-insert-file-contents more)))
444 ;; Return the articles in the right order.
446 (sort (nconc nnweb-articles map) 'car-less-than-car))))))
448 (defun nnweb-dejanews-wash-article ()
449 (let ((case-fold-search t))
450 (goto-char (point-min))
451 (re-search-forward "<PRE>" nil t)
452 (delete-region (point-min) (point))
453 (re-search-forward "</PRE>" nil t)
454 (delete-region (point) (point-max))
455 (nnweb-remove-markup)
456 (goto-char (point-min))
457 (while (and (looking-at " *$")
460 (while (looking-at "\\(^[^ ]+:\\) *")
461 (replace-match "\\1 " t)
463 (when (re-search-forward "\n\n+" nil t)
464 (replace-match "\n" t t))
465 (goto-char (point-min))
466 (when (search-forward "[More Headers]" nil t)
467 (replace-match "" t t))))
469 (defun nnweb-dejanews-search (search)
471 (nnweb-definition 'address)
472 `(("query" . ,search)
473 ("defaultOp" . "AND")
474 ("svcclass" . "dncurrent")
476 ("format" . "verbose2")
478 ("showsort" . "date")
480 ("ageweight" . "1")))
483 (defun nnweb-dejanewsold-search (search)
485 (nnweb-definition 'address)
486 `(("query" . ,search)
487 ("defaultOp" . "AND")
488 ("svcclass" . "dnold")
490 ("format" . "verbose2")
492 ("showsort" . "date")
494 ("ageweight" . "1")))
497 (defun nnweb-dejanews-identity (url)
498 "Return an unique identifier based on URL."
499 (if (string-match "recnum=\\([0-9]+\\)" url)
507 (defun nnweb-reference-create-mapping ()
508 "Perform the search and create an number-to-url alist."
510 (set-buffer nnweb-buffer)
512 (when (funcall (nnweb-definition 'search) nnweb-search)
516 (active (or (cadr (assoc nnweb-group nnweb-group-alist))
518 Subject Score Date Newsgroups From Message-ID
521 ;; Go through all the article hits on this page.
522 (goto-char (point-min))
523 (search-forward "</pre><hr>" nil t)
524 (delete-region (point-min) (point))
525 ;(nnweb-decode-entities)
526 (goto-char (point-min))
527 (while (re-search-forward "^ +[0-9]+\\." nil t)
530 (if (re-search-forward "^$" nil t)
533 (goto-char (point-min))
534 (when (looking-at ".*href=\"\\([^\"]+\\)\"")
535 (setq url (match-string 1)))
536 (nnweb-remove-markup)
537 (goto-char (point-min))
538 (while (search-forward "\t" nil t)
540 (goto-char (point-min))
541 (while (re-search-forward "^\\([^:]+\\): \\(.*\\)$" nil t)
542 (set (intern (match-string 1)) (match-string 2)))
544 (search-forward "</pre>" nil t)
546 (unless (nnweb-get-hashtb url)
550 (make-full-mail-header
551 (cdr active) (concat "(" Newsgroups ") " Subject) From Date
553 nil 0 (string-to-int Score) url))
555 (nnweb-set-hashtb (cadar map) (car map))))
557 ;; Return the articles in the right order.
559 (sort (nconc nnweb-articles map) 'car-less-than-car))))))
561 (defun nnweb-reference-wash-article ()
562 (let ((case-fold-search t))
563 (goto-char (point-min))
564 (re-search-forward "^</center><hr>" nil t)
565 (delete-region (point-min) (point))
566 (search-forward "<pre>" nil t)
568 (let ((body (point-marker)))
569 (search-forward "</pre>" nil t)
570 (delete-region (point) (point-max))
571 (nnweb-remove-markup)
572 (goto-char (point-min))
573 (while (looking-at " *$")
575 (narrow-to-region (point-min) body)
576 (while (and (re-search-forward "^$" nil t)
579 (goto-char (point-min))
580 (while (looking-at "\\(^[^ ]+:\\) *")
581 (replace-match "\\1 " t)
583 (goto-char (point-min))
584 (when (re-search-forward "^References:" nil t)
586 (point) (if (re-search-forward "^$\\|^[^:]+:" nil t)
589 (goto-char (point-min))
591 (unless (looking-at "References")
594 (goto-char (point-min))
595 (while (search-forward "," nil t)
596 (replace-match " " t t)))
598 (set-marker body nil))))
600 (defun nnweb-reference-search (search)
601 (url-insert-file-contents
603 (nnweb-definition 'address)
605 (nnweb-encode-www-form-urlencoded
606 `(("search" . "advanced")
607 ("querytext" . ,search)
612 ("organization" . "")
615 ("choice" . "Search")
616 ("startmonth" . "Jul")
618 ("startyear" . "1996")
623 ("verbosity" . "Verbose")
624 ("ranking" . "Relevance")
628 (setq buffer-file-name nil)
635 (defun nnweb-altavista-create-mapping ()
636 "Perform the search and create an number-to-url alist."
638 (set-buffer nnweb-buffer)
641 (when (funcall (nnweb-definition 'search) nnweb-search part)
645 (active (or (cadr (assoc nnweb-group nnweb-group-alist))
647 subject date from id group
650 ;; Go through all the article hits on this page.
651 (goto-char (point-min))
652 (search-forward "<dt>" nil t)
653 (delete-region (point-min) (match-beginning 0))
654 (goto-char (point-min))
655 (while (search-forward "<dt>" nil t)
656 (replace-match "\n<blubb>"))
657 (nnweb-decode-entities)
658 (goto-char (point-min))
659 (while (re-search-forward "<blubb>.*href=\"\\([^\"]+\\)\"><strong>\\([^>]*\\)</strong></a><dd>\\([^-]+\\)- <b>\\([^<]+\\)<.*href=\"news:\\([^\"]+\\)\">.*\">\\(.+\\)</a><P>"
661 (setq url (match-string 1)
662 subject (match-string 2)
663 date (match-string 3)
664 group (match-string 4)
665 id (concat "<" (match-string 5) ">")
666 from (match-string 6))
668 (unless (nnweb-get-hashtb url)
672 (make-full-mail-header
673 (cdr active) (concat "(" group ") " subject) from date
676 (nnweb-set-hashtb (cadar map) (car map))))
677 ;; See if we want more.
678 (when (or (not nnweb-articles)
679 (>= i nnweb-max-hits)
680 (not (funcall (nnweb-definition 'search)
681 nnweb-search (incf part))))
683 ;; Return the articles in the right order.
685 (sort (nconc nnweb-articles map) 'car-less-than-car)))))))
687 (defun nnweb-altavista-wash-article ()
688 (goto-char (point-min))
689 (let ((case-fold-search t))
690 (when (re-search-forward "^<strong>" nil t)
691 (delete-region (point-min) (match-beginning 0)))
692 (goto-char (point-min))
693 (while (looking-at "<strong>\\([^ ]+\\) +</strong> +\\(.*\\)$")
694 (replace-match "\\1: \\2" t)
696 (when (re-search-backward "^References:" nil t)
697 (narrow-to-region (point) (progn (forward-line 1) (point)))
698 (goto-char (point-min))
699 (while (re-search-forward "<A.*\\?id@\\([^\"]+\\)\">[0-9]+</A>" nil t)
700 (replace-match "<\\1> " t)))
702 (nnweb-remove-markup)))
704 (defun nnweb-altavista-search (search &optional part)
705 (url-insert-file-contents
707 (nnweb-definition 'address)
709 (nnweb-encode-www-form-urlencoded
712 ,@(when part `(("stq" . ,(int-to-string (* part 30)))))
718 (setq buffer-file-name nil)
723 ;;; nnweb.el ends here