1 ;;; nnweb.el --- retrieving articles via web search engines
2 ;; Copyright (C) 1996,97,98 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))
45 (defvoo nnweb-directory (nnheader-concat gnus-directory "nnweb/")
46 "Where nnweb will save its files.")
48 (defvoo nnweb-type 'dejanews
49 "What search engine type is being used.
50 Valid types include `dejanews', `dejanewsold', `reference',
53 (defvoo nnweb-type-definition
55 (article . nnweb-dejanews-wash-article)
56 (map . nnweb-dejanews-create-mapping)
57 (search . nnweb-dejanews-search)
58 (address . "http://x8.dejanews.com/dnquery.xp")
59 (identifier . nnweb-dejanews-identity))
61 (article . nnweb-dejanews-wash-article)
62 (map . nnweb-dejanews-create-mapping)
63 (search . nnweb-dejanewsold-search)
64 (address . "http://x8.dejanews.com/dnquery.xp")
65 (identifier . nnweb-dejanews-identity))
67 (article . nnweb-reference-wash-article)
68 (map . nnweb-reference-create-mapping)
69 (search . nnweb-reference-search)
70 (address . "http://www.reference.com/cgi-bin/pn/go")
71 (identifier . identity))
73 (article . nnweb-altavista-wash-article)
74 (map . nnweb-altavista-create-mapping)
75 (search . nnweb-altavista-search)
76 (address . "http://www.altavista.digital.com/cgi-bin/query")
77 (id . "/cgi-bin/news?id@%s")
78 (identifier . identity)))
79 "Type-definition alist.")
81 (defvoo nnweb-search nil
82 "Search string to feed to DejaNews.")
84 (defvoo nnweb-max-hits 999
85 "Maximum number of hits to display.")
87 (defvoo nnweb-ephemeral-p nil
88 "Whether this nnweb server is ephemeral.")
90 ;;; Internal variables
92 (defvoo nnweb-articles nil)
93 (defvoo nnweb-buffer nil)
94 (defvoo nnweb-group-alist nil)
95 (defvoo nnweb-group nil)
96 (defvoo nnweb-hashtb nil)
98 ;;; Interface functions
100 (nnoo-define-basics nnweb)
102 (deffoo nnweb-retrieve-headers (articles &optional group server fetch-old)
103 (nnweb-possibly-change-server group server)
105 (set-buffer nntp-server-buffer)
107 (let (article header)
108 (while (setq article (pop articles))
109 (when (setq header (cadr (assq article nnweb-articles)))
110 (nnheader-insert-nov header)))
113 (deffoo nnweb-request-scan (&optional group server)
114 (nnweb-possibly-change-server group server)
115 (setq nnweb-hashtb (gnus-make-hashtable 4095))
116 (funcall (nnweb-definition 'map))
117 (unless nnweb-ephemeral-p
119 (nnweb-write-overview group)))
121 (deffoo nnweb-request-group (group &optional server dont-check)
122 (nnweb-possibly-change-server nil server)
124 (not (equal group nnweb-group))
125 (not nnweb-ephemeral-p))
126 (let ((info (assoc group nnweb-group-alist)))
127 (setq nnweb-group group)
128 (setq nnweb-type (nth 2 info))
129 (setq nnweb-search (nth 3 info))
131 (nnweb-read-overview group))))
133 ((not nnweb-articles)
134 (nnheader-report 'nnweb "No matching articles"))
136 (let ((active (if nnweb-ephemeral-p
137 (cons (caar nnweb-articles)
138 (caar (last nnweb-articles)))
139 (cadr (assoc group nnweb-group-alist)))))
140 (nnheader-report 'nnweb "Opened group %s" group)
142 "211 %d %d %d %s\n" (length nnweb-articles)
143 (car active) (cdr active) group)))))
145 (deffoo nnweb-close-group (group &optional server)
146 (nnweb-possibly-change-server group server)
147 (when (gnus-buffer-live-p nnweb-buffer)
149 (set-buffer nnweb-buffer)
150 (set-buffer-modified-p nil)
151 (kill-buffer nnweb-buffer)))
154 (deffoo nnweb-request-article (article &optional group server buffer)
155 (nnweb-possibly-change-server group server)
157 (set-buffer (or buffer nntp-server-buffer))
158 (let* ((header (cadr (assq article nnweb-articles)))
159 (url (and header (mail-header-xref header))))
161 (nnweb-fetch-url url))
162 (and (stringp article)
163 (nnweb-definition 'id t)
164 (let ((fetch (nnweb-definition 'id))
166 (when (string-match "^<\\(.*\\)>$" article)
167 (setq art (match-string 1 article)))
171 (format fetch article))))))
172 (unless nnheader-callback-function
173 (funcall (nnweb-definition 'article))
174 (nnweb-decode-entities))
175 (nnheader-report 'nnweb "Fetched article %s" article)
178 (deffoo nnweb-close-server (&optional server)
179 (when (and (nnweb-server-opened server)
180 (gnus-buffer-live-p nnweb-buffer))
182 (set-buffer nnweb-buffer)
183 (set-buffer-modified-p nil)
184 (kill-buffer nnweb-buffer)))
185 (nnoo-close-server 'nnweb server))
187 (deffoo nnweb-request-list (&optional server)
188 (nnweb-possibly-change-server nil server)
190 (set-buffer nntp-server-buffer)
191 (nnmail-generate-active nnweb-group-alist)
194 (deffoo nnweb-request-update-info (group info &optional server)
195 (nnweb-possibly-change-server group server)
196 ;;(setcar (cddr info) nil)
199 (deffoo nnweb-asynchronous-p ()
202 (deffoo nnweb-request-create-group (group &optional server args)
203 (nnweb-possibly-change-server nil server)
204 (nnweb-request-delete-group group)
205 (push `(,group ,(cons 1 0) ,@args) nnweb-group-alist)
209 (deffoo nnweb-request-delete-group (group &optional force server)
210 (nnweb-possibly-change-server group server)
211 (gnus-pull group nnweb-group-alist)
212 (gnus-delete-file (nnweb-overview-file group))
215 (nnoo-define-skeleton nnweb)
217 ;;; Internal functions
219 (defun nnweb-read-overview (group)
220 "Read the overview of GROUP and build the map."
221 (when (file-exists-p (nnweb-overview-file group))
222 (nnheader-temp-write nil
223 (nnheader-insert-file-contents (nnweb-overview-file group))
224 (goto-char (point-min))
227 (setq header (nnheader-parse-nov))
229 (push (list (mail-header-number header)
230 header (mail-header-xref header))
232 (nnweb-set-hashtb header (car nnweb-articles)))))))
234 (defun nnweb-write-overview (group)
235 "Write the overview file for GROUP."
236 (nnheader-temp-write (nnweb-overview-file group)
237 (let ((articles nnweb-articles))
239 (nnheader-insert-nov (cadr (pop articles)))))))
241 (defun nnweb-set-hashtb (header data)
242 (gnus-sethash (nnweb-identifier (mail-header-xref header))
245 (defun nnweb-get-hashtb (url)
246 (gnus-gethash (nnweb-identifier url) nnweb-hashtb))
248 (defun nnweb-identifier (ident)
249 (funcall (nnweb-definition 'identifier) ident))
251 (defun nnweb-overview-file (group)
252 "Return the name of the overview file of GROUP."
253 (nnheader-concat nnweb-directory group ".overview"))
255 (defun nnweb-write-active ()
256 "Save the active file."
257 (nnheader-temp-write (nnheader-concat nnweb-directory "active")
258 (prin1 `(setq nnweb-group-alist ',nnweb-group-alist) (current-buffer))))
260 (defun nnweb-read-active ()
261 "Read the active file."
262 (load (nnheader-concat nnweb-directory "active") t t t))
264 (defun nnweb-definition (type &optional noerror)
265 "Return the definition of TYPE."
266 (let ((def (cdr (assq type (assq nnweb-type nnweb-type-definition)))))
269 (error "Undefined definition %s" type))
272 (defun nnweb-possibly-change-server (&optional group server)
275 (unless (nnweb-server-opened server)
276 (nnweb-open-server server)))
277 (unless nnweb-group-alist
280 (when (and (not nnweb-ephemeral-p)
281 (not (equal group nnweb-group)))
282 (nnweb-request-group group nil t))))
284 (defun nnweb-init (server)
285 "Initialize buffers and such."
286 (unless (gnus-buffer-live-p nnweb-buffer)
289 (nnheader-set-temp-buffer
290 (format " *nnweb %s %s %s*" nnweb-type nnweb-search server))))))
292 (defun nnweb-fetch-url (url)
294 (if (not nnheader-callback-function)
295 (let ((buf (current-buffer)))
297 (set-buffer nnweb-buffer)
299 (url-insert-file-contents url)
300 (copy-to-buffer buf (point-min) (point-max))
302 (nnweb-url-retrieve-asynch
303 url 'nnweb-callback (current-buffer) nnheader-callback-function)
306 (defun nnweb-callback (buffer callback)
307 (when (gnus-buffer-live-p url-working-buffer)
309 (set-buffer url-working-buffer)
310 (funcall (nnweb-definition 'article))
311 (nnweb-decode-entities)
313 (goto-char (point-max))
314 (insert-buffer-substring url-working-buffer))
316 (gnus-kill-buffer url-working-buffer)))
318 (defun nnweb-url-retrieve-asynch (url callback &rest data)
319 (let ((url-request-method "GET")
320 (old-asynch url-be-asynchronous)
321 (url-request-data nil)
322 (url-request-extra-headers nil)
323 (url-working-buffer (generate-new-buffer-name " *nnweb*")))
324 (setq-default url-be-asynchronous t)
326 (set-buffer (get-buffer-create url-working-buffer))
327 (setq url-current-callback-data data
328 url-be-asynchronous t
329 url-current-callback-func callback)
331 (setq-default url-be-asynchronous old-asynch)))
333 (defun nnweb-encode-www-form-urlencoded (pairs)
334 "Return PAIRS encoded for forms."
338 (concat (w3-form-encode-xwfu (car data)) "="
339 (w3-form-encode-xwfu (cdr data)))))
342 (defun nnweb-fetch-form (url pairs)
343 (let ((url-request-data (nnweb-encode-www-form-urlencoded pairs))
344 (url-request-method "POST")
345 (url-request-extra-headers
346 '(("Content-type" . "application/x-www-form-urlencoded"))))
347 (url-insert-file-contents url)
348 (setq buffer-file-name nil))
351 (defun nnweb-decode-entities ()
352 (goto-char (point-min))
353 (while (re-search-forward "&\\([a-z]+\\);" nil t)
354 (replace-match (char-to-string (or (cdr (assq (intern (match-string 1))
359 (defun nnweb-remove-markup ()
360 (goto-char (point-min))
361 (while (search-forward "<!--" nil t)
362 (delete-region (match-beginning 0)
363 (or (search-forward "-->" nil t)
365 (goto-char (point-min))
366 (while (re-search-forward "<[^>]+>" nil t)
367 (replace-match "" t t)))
370 ;;; DejaNews functions.
373 (defun nnweb-dejanews-create-mapping ()
374 "Perform the search and create an number-to-url alist."
376 (set-buffer nnweb-buffer)
378 (when (funcall (nnweb-definition 'search) nnweb-search)
382 (active (or (cadr (assoc nnweb-group nnweb-group-alist))
384 Subject (Score "0") Date Newsgroup Author
387 ;; Go through all the article hits on this page.
388 (goto-char (point-min))
389 (nnweb-decode-entities)
390 (goto-char (point-min))
391 (while (re-search-forward "^ <P>\n" nil t)
394 (cond ((re-search-forward "^ <P>\n" nil t)
396 ((search-forward "\n\n" nil t)
400 (goto-char (point-min))
401 (looking-at ".*HREF=\"\\([^\"]+\\)\"\\(.*\\)")
402 (setq url (match-string 1))
403 (let ((begin (point)))
404 (nnweb-remove-markup)
406 (while (search-forward "\t" nil t)
410 (setq Subject (buffer-substring begin (point)))
411 (if (re-search-forward
412 "^ Newsgroup: \\(.*\\)\n Posted on \\([0-9/]+\\) by \\(.*\\)$" nil t)
413 (setq Newsgroup (match-string 1)
414 Date (match-string 2)
415 Author (match-string 3))))
418 (unless (nnweb-get-hashtb url)
422 (make-full-mail-header
423 (cdr active) Subject Author Date
424 (concat "<" (nnweb-identifier url) "@dejanews>")
425 nil 0 (string-to-int Score) url))
427 (nnweb-set-hashtb (cadar map) (car map))))
428 ;; See whether there is a "Get next 20 hits" button here.
429 (if (or (not (re-search-forward
430 "HREF=\"\\([^\"]+\\)\"[<>b]+Next result" nil t))
431 (>= i nnweb-max-hits))
434 (setq more (match-string 1))
436 (url-insert-file-contents more)))
437 ;; Return the articles in the right order.
439 (sort (nconc nnweb-articles map) 'car-less-than-car))))))
441 (defun nnweb-dejanews-wash-article ()
442 (let ((case-fold-search t))
443 (goto-char (point-min))
444 (re-search-forward "<PRE>" nil t)
445 (delete-region (point-min) (point))
446 (re-search-forward "</PRE>" nil t)
447 (delete-region (point) (point-max))
448 (nnweb-remove-markup)
449 (goto-char (point-min))
450 (while (and (looking-at " *$")
453 (while (looking-at "\\(^[^ ]+:\\) *")
454 (replace-match "\\1 " t)
456 (when (re-search-forward "\n\n+" nil t)
457 (replace-match "\n" t t))
458 (goto-char (point-min))
459 (when (search-forward "[More Headers]" nil t)
460 (replace-match "" t t))))
462 (defun nnweb-dejanews-search (search)
464 (nnweb-definition 'address)
465 `(("query" . ,search)
466 ("defaultOp" . "AND")
467 ("svcclass" . "dncurrent")
469 ("format" . "verbose2")
471 ("showsort" . "date")
473 ("ageweight" . "1")))
476 (defun nnweb-dejanewsold-search (search)
478 (nnweb-definition 'address)
479 `(("query" . ,search)
480 ("defaultOp" . "AND")
481 ("svcclass" . "dnold")
483 ("format" . "verbose2")
485 ("showsort" . "date")
487 ("ageweight" . "1")))
490 (defun nnweb-dejanews-identity (url)
491 "Return an unique identifier based on URL."
492 (if (string-match "recnum=\\([0-9]+\\)" url)
500 (defun nnweb-reference-create-mapping ()
501 "Perform the search and create an number-to-url alist."
503 (set-buffer nnweb-buffer)
505 (when (funcall (nnweb-definition 'search) nnweb-search)
509 (active (or (cadr (assoc nnweb-group nnweb-group-alist))
511 Subject Score Date Newsgroups From Message-ID
514 ;; Go through all the article hits on this page.
515 (goto-char (point-min))
516 (search-forward "</pre><hr>" nil t)
517 (delete-region (point-min) (point))
518 ;(nnweb-decode-entities)
519 (goto-char (point-min))
520 (while (re-search-forward "^ +[0-9]+\\." nil t)
523 (if (re-search-forward "^$" nil t)
526 (goto-char (point-min))
527 (when (looking-at ".*href=\"\\([^\"]+\\)\"")
528 (setq url (match-string 1)))
529 (nnweb-remove-markup)
530 (goto-char (point-min))
531 (while (search-forward "\t" nil t)
533 (goto-char (point-min))
534 (while (re-search-forward "^\\([^:]+\\): \\(.*\\)$" nil t)
535 (set (intern (match-string 1)) (match-string 2)))
537 (search-forward "</pre>" nil t)
539 (unless (nnweb-get-hashtb url)
543 (make-full-mail-header
544 (cdr active) (concat "(" Newsgroups ") " Subject) From Date
546 nil 0 (string-to-int Score) url))
548 (nnweb-set-hashtb (cadar map) (car map))))
550 ;; Return the articles in the right order.
552 (sort (nconc nnweb-articles map) 'car-less-than-car))))))
554 (defun nnweb-reference-wash-article ()
555 (let ((case-fold-search t))
556 (goto-char (point-min))
557 (re-search-forward "^</center><hr>" nil t)
558 (delete-region (point-min) (point))
559 (search-forward "<pre>" nil t)
561 (let ((body (point-marker)))
562 (search-forward "</pre>" nil t)
563 (delete-region (point) (point-max))
564 (nnweb-remove-markup)
565 (goto-char (point-min))
566 (while (looking-at " *$")
568 (narrow-to-region (point-min) body)
569 (while (and (re-search-forward "^$" nil t)
572 (goto-char (point-min))
573 (while (looking-at "\\(^[^ ]+:\\) *")
574 (replace-match "\\1 " t)
576 (goto-char (point-min))
577 (when (re-search-forward "^References:" nil t)
579 (point) (if (re-search-forward "^$\\|^[^:]+:" nil t)
582 (goto-char (point-min))
584 (unless (looking-at "References")
587 (goto-char (point-min))
588 (while (search-forward "," nil t)
589 (replace-match " " t t)))
591 (set-marker body nil))))
593 (defun nnweb-reference-search (search)
594 (url-insert-file-contents
596 (nnweb-definition 'address)
598 (nnweb-encode-www-form-urlencoded
599 `(("search" . "advanced")
600 ("querytext" . ,search)
605 ("organization" . "")
608 ("choice" . "Search")
609 ("startmonth" . "Jul")
611 ("startyear" . "1996")
616 ("verbosity" . "Verbose")
617 ("ranking" . "Relevance")
621 (setq buffer-file-name nil)
628 (defun nnweb-altavista-create-mapping ()
629 "Perform the search and create an number-to-url alist."
631 (set-buffer nnweb-buffer)
634 (when (funcall (nnweb-definition 'search) nnweb-search part)
638 (active (or (cadr (assoc nnweb-group nnweb-group-alist))
640 subject date from id group
643 ;; Go through all the article hits on this page.
644 (goto-char (point-min))
645 (search-forward "<dt>" nil t)
646 (delete-region (point-min) (match-beginning 0))
647 (goto-char (point-min))
648 (while (search-forward "<dt>" nil t)
649 (replace-match "\n<blubb>"))
650 (nnweb-decode-entities)
651 (goto-char (point-min))
652 (while (re-search-forward "<blubb>.*href=\"\\([^\"]+\\)\"><strong>\\([^>]*\\)</strong></a><dd>\\([^-]+\\)- <b>\\([^<]+\\)<.*href=\"news:\\([^\"]+\\)\">.*\">\\(.+\\)</a><P>"
654 (setq url (match-string 1)
655 subject (match-string 2)
656 date (match-string 3)
657 group (match-string 4)
658 id (concat "<" (match-string 5) ">")
659 from (match-string 6))
661 (unless (nnweb-get-hashtb url)
665 (make-full-mail-header
666 (cdr active) (concat "(" group ") " subject) from date
669 (nnweb-set-hashtb (cadar map) (car map))))
670 ;; See if we want more.
671 (when (or (not nnweb-articles)
672 (>= i nnweb-max-hits)
673 (not (funcall (nnweb-definition 'search)
674 nnweb-search (incf part))))
676 ;; Return the articles in the right order.
678 (sort (nconc nnweb-articles map) 'car-less-than-car)))))))
680 (defun nnweb-altavista-wash-article ()
681 (goto-char (point-min))
682 (let ((case-fold-search t))
683 (when (re-search-forward "^<strong>" nil t)
684 (delete-region (point-min) (match-beginning 0)))
685 (goto-char (point-min))
686 (while (looking-at "<strong>\\([^ ]+\\) +</strong> +\\(.*\\)$")
687 (replace-match "\\1: \\2" t)
689 (when (re-search-backward "^References:" nil t)
690 (narrow-to-region (point) (progn (forward-line 1) (point)))
691 (goto-char (point-min))
692 (while (re-search-forward "<A.*\\?id@\\([^\"]+\\)\">[0-9]+</A>" nil t)
693 (replace-match "<\\1> " t)))
695 (nnweb-remove-markup)))
697 (defun nnweb-altavista-search (search &optional part)
698 (url-insert-file-contents
700 (nnweb-definition 'address)
702 (nnweb-encode-www-form-urlencoded
705 ,@(when part `(("stq" . ,(int-to-string (* part 30)))))
711 (setq buffer-file-name nil)
716 ;;; nnweb.el ends here