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@ifi.uio.no>
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.")
51 (defvoo nnweb-type-definition
53 (article . nnweb-dejanews-wash-article)
54 (map . nnweb-dejanews-create-mapping)
55 (search . nnweb-dejanews-search)
56 (address . "http://x5.dejanews.com/dnquery.xp")
57 (identifier . nnweb-dejanews-identity))
59 (article . nnweb-dejanews-wash-article)
60 (map . nnweb-dejanews-create-mapping)
61 (search . nnweb-dejanewsold-search)
62 (address . "http://x5.dejanews.com/dnquery.xp")
63 (identifier . nnweb-dejanews-identity))
65 (article . nnweb-reference-wash-article)
66 (map . nnweb-reference-create-mapping)
67 (search . nnweb-reference-search)
68 (address . "http://www.reference.com/cgi-bin/pn/go")
69 (identifier . identity))
71 (article . nnweb-altavista-wash-article)
72 (map . nnweb-altavista-create-mapping)
73 (search . nnweb-altavista-search)
74 (address . "http://www.altavista.digital.com/cgi-bin/query")
75 (id . "/cgi-bin/news?id@%s")
76 (identifier . identity)))
77 "Type-definition alist.")
79 (defvoo nnweb-search nil
80 "Search string to feed to DejaNews.")
82 (defvoo nnweb-max-hits 100
83 "Maximum number of hits to display.")
85 (defvoo nnweb-ephemeral-p nil
86 "Whether this nnweb server is ephemeral.")
88 ;;; Internal variables
90 (defvoo nnweb-articles nil)
91 (defvoo nnweb-buffer nil)
92 (defvoo nnweb-group-alist nil)
93 (defvoo nnweb-group nil)
94 (defvoo nnweb-hashtb nil)
96 ;;; Interface functions
98 (nnoo-define-basics nnweb)
100 (deffoo nnweb-retrieve-headers (articles &optional group server fetch-old)
101 (nnweb-possibly-change-server group server)
103 (set-buffer nntp-server-buffer)
105 (let (article header)
106 (while (setq article (pop articles))
107 (when (setq header (cadr (assq article nnweb-articles)))
108 (nnheader-insert-nov header)))
111 (deffoo nnweb-request-scan (&optional group server)
112 (nnweb-possibly-change-server group server)
113 (setq nnweb-hashtb (gnus-make-hashtable 4095))
114 (funcall (nnweb-definition 'map))
115 (unless nnweb-ephemeral-p
117 (nnweb-write-overview group)))
119 (deffoo nnweb-request-group (group &optional server dont-check)
120 (nnweb-possibly-change-server nil server)
122 (not (equal group nnweb-group))
123 (not nnweb-ephemeral-p))
124 (let ((info (assoc group nnweb-group-alist)))
125 (setq nnweb-group group)
126 (setq nnweb-type (nth 2 info))
127 (setq nnweb-search (nth 3 info))
129 (nnweb-read-overview group))))
131 ((not nnweb-articles)
132 (nnheader-report 'nnweb "No matching articles"))
134 (let ((active (if nnweb-ephemeral-p
135 (cons (caar nnweb-articles)
136 (caar (last nnweb-articles)))
137 (cadr (assoc group nnweb-group-alist)))))
138 (nnheader-report 'nnweb "Opened group %s" group)
140 "211 %d %d %d %s\n" (length nnweb-articles)
141 (car active) (cdr active) group)))))
143 (deffoo nnweb-close-group (group &optional server)
144 (nnweb-possibly-change-server group server)
145 (when (gnus-buffer-live-p nnweb-buffer)
147 (set-buffer nnweb-buffer)
148 (set-buffer-modified-p nil)
149 (kill-buffer nnweb-buffer)))
152 (deffoo nnweb-request-article (article &optional group server buffer)
153 (nnweb-possibly-change-server group server)
155 (set-buffer (or buffer nntp-server-buffer))
156 (let* ((header (cadr (assq article nnweb-articles)))
157 (url (and header (mail-header-xref header))))
159 (nnweb-fetch-url url))
160 (and (stringp article)
161 (nnweb-definition 'id t)
162 (let ((fetch (nnweb-definition 'id))
164 (when (string-match "^<\\(.*\\)>$" article)
165 (setq art (match-string 1 article)))
169 (format fetch article))))))
170 (unless nnheader-callback-function
171 (funcall (nnweb-definition 'article))
172 (nnweb-decode-entities))
173 (nnheader-report 'nnweb "Fetched article %s" article)
176 (deffoo nnweb-close-server (&optional server)
177 (when (and (nnweb-server-opened server)
178 (gnus-buffer-live-p nnweb-buffer))
180 (set-buffer nnweb-buffer)
181 (set-buffer-modified-p nil)
182 (kill-buffer nnweb-buffer)))
183 (nnoo-close-server 'nnweb server))
185 (deffoo nnweb-request-list (&optional server)
186 (nnweb-possibly-change-server nil server)
188 (set-buffer nntp-server-buffer)
189 (nnmail-generate-active nnweb-group-alist)
192 (deffoo nnweb-request-update-info (group info &optional server)
193 (nnweb-possibly-change-server group server)
194 ;;(setcar (cddr info) nil)
197 (deffoo nnweb-asynchronous-p ()
200 (deffoo nnweb-request-create-group (group &optional server args)
201 (nnweb-possibly-change-server nil server)
202 (nnweb-request-delete-group group)
203 (push `(,group ,(cons 1 0) ,@args) nnweb-group-alist)
207 (deffoo nnweb-request-delete-group (group &optional force server)
208 (nnweb-possibly-change-server group server)
209 (gnus-delete-assoc group nnweb-group-alist)
210 (gnus-delete-file (nnweb-overview-file group))
213 (nnoo-define-skeleton nnweb)
215 ;;; Internal functions
217 (defun nnweb-read-overview (group)
218 "Read the overview of GROUP and build the map."
219 (when (file-exists-p (nnweb-overview-file group))
220 (nnheader-temp-write nil
221 (nnheader-insert-file-contents (nnweb-overview-file group))
222 (goto-char (point-min))
225 (setq header (nnheader-parse-nov))
227 (push (list (mail-header-number header)
228 header (mail-header-xref header))
230 (nnweb-set-hashtb header (car nnweb-articles)))))))
232 (defun nnweb-write-overview (group)
233 "Write the overview file for GROUP."
234 (nnheader-temp-write (nnweb-overview-file group)
235 (let ((articles nnweb-articles))
237 (nnheader-insert-nov (cadr (pop articles)))))))
239 (defun nnweb-set-hashtb (header data)
240 (gnus-sethash (nnweb-identifier (mail-header-xref header))
243 (defun nnweb-get-hashtb (url)
244 (gnus-gethash (nnweb-identifier url) nnweb-hashtb))
246 (defun nnweb-identifier (ident)
247 (funcall (nnweb-definition 'identifier) ident))
249 (defun nnweb-overview-file (group)
250 "Return the name of the overview file of GROUP."
251 (nnheader-concat nnweb-directory group ".overview"))
253 (defun nnweb-write-active ()
254 "Save the active file."
255 (nnheader-temp-write (nnheader-concat nnweb-directory "active")
256 (prin1 `(setq nnweb-group-alist ',nnweb-group-alist) (current-buffer))))
258 (defun nnweb-read-active ()
259 "Read the active file."
260 (load (nnheader-concat nnweb-directory "active") t t t))
262 (defun nnweb-definition (type &optional noerror)
263 "Return the definition of TYPE."
264 (let ((def (cdr (assq type (assq nnweb-type nnweb-type-definition)))))
267 (error "Undefined definition %s" type))
270 (defun nnweb-possibly-change-server (&optional group server)
273 (unless (nnweb-server-opened server)
274 (nnweb-open-server server)))
275 (unless nnweb-group-alist
278 (when (and (not nnweb-ephemeral-p)
279 (not (equal group nnweb-group)))
280 (nnweb-request-group group nil t))))
282 (defun nnweb-init (server)
283 "Initialize buffers and such."
284 (unless (gnus-buffer-live-p nnweb-buffer)
287 (nnheader-set-temp-buffer
288 (format " *nnweb %s %s %s*" nnweb-type nnweb-search server))))))
290 (defun nnweb-fetch-url (url)
292 (if (not nnheader-callback-function)
293 (let ((buf (current-buffer)))
295 (set-buffer nnweb-buffer)
297 (url-insert-file-contents url)
298 (copy-to-buffer buf (point-min) (point-max))
300 (nnweb-url-retrieve-asynch
301 url 'nnweb-callback (current-buffer) nnheader-callback-function)
304 (defun nnweb-callback (buffer callback)
305 (when (gnus-buffer-live-p url-working-buffer)
307 (set-buffer url-working-buffer)
308 (funcall (nnweb-definition 'article))
309 (nnweb-decode-entities)
311 (goto-char (point-max))
312 (insert-buffer-substring url-working-buffer))
314 (gnus-kill-buffer url-working-buffer)))
316 (defun nnweb-url-retrieve-asynch (url callback &rest data)
317 (let ((url-request-method "GET")
318 (old-asynch url-be-asynchronous)
319 (url-request-data nil)
320 (url-request-extra-headers nil)
321 (url-working-buffer (generate-new-buffer-name " *nnweb*")))
322 (setq-default url-be-asynchronous t)
324 (set-buffer (get-buffer-create url-working-buffer))
325 (setq url-current-callback-data data
326 url-be-asynchronous t
327 url-current-callback-func callback)
329 (setq-default url-be-asynchronous old-asynch)))
331 (defun nnweb-encode-www-form-urlencoded (pairs)
332 "Return PAIRS encoded for forms."
336 (concat (w3-form-encode-xwfu (car data)) "="
337 (w3-form-encode-xwfu (cdr data)))))
340 (defun nnweb-fetch-form (url pairs)
341 (let ((url-request-data (nnweb-encode-www-form-urlencoded pairs))
342 (url-request-method "POST")
343 (url-request-extra-headers
344 '(("Content-type" . "application/x-www-form-urlencoded"))))
345 (url-insert-file-contents url)
346 (setq buffer-file-name nil))
349 (defun nnweb-decode-entities ()
350 (goto-char (point-min))
351 (while (re-search-forward "&\\([a-z]+\\);" nil t)
352 (replace-match (char-to-string (or (cdr (assq (intern (match-string 1))
357 (defun nnweb-remove-markup ()
358 (goto-char (point-min))
359 (while (search-forward "<!--" nil t)
360 (delete-region (match-beginning 0)
361 (or (search-forward "-->" nil t)
363 (goto-char (point-min))
364 (while (re-search-forward "<[^>]+>" nil t)
365 (replace-match "" t t)))
368 ;;; DejaNews functions.
371 (defun nnweb-dejanews-create-mapping ()
372 "Perform the search and create an number-to-url alist."
374 (set-buffer nnweb-buffer)
376 (when (funcall (nnweb-definition 'search) nnweb-search)
380 (active (or (cadr (assoc nnweb-group nnweb-group-alist))
382 Subject Score Date Newsgroup Author
385 ;; Go through all the article hits on this page.
386 (goto-char (point-min))
387 (nnweb-decode-entities)
388 (goto-char (point-min))
389 (while (re-search-forward "^ +[0-9]+\\." nil t)
392 (cond ((re-search-forward "^ +[0-9]+\\." nil t)
394 ((search-forward "\n\n" nil t)
398 (goto-char (point-min))
399 (when (looking-at ".*HREF=\"\\([^\"]+\\)\"")
400 (setq url (match-string 1)))
401 (nnweb-remove-markup)
402 (goto-char (point-min))
403 (while (search-forward "\t" nil t)
405 (goto-char (point-min))
406 (while (re-search-forward "^ +\\([^:]+\\): +\\(.*\\)$" nil t)
407 (set (intern (match-string 1)) (match-string 2)))
409 (when (string-match "#[0-9]+/[0-9]+ *$" Subject)
410 (setq Subject (substring Subject 0 (match-beginning 0))))
412 (unless (nnweb-get-hashtb url)
416 (make-full-mail-header
417 (cdr active) (concat "(" Newsgroup ") " Subject) Author Date
418 (concat "<" (nnweb-identifier url) "@dejanews>")
419 nil 0 (string-to-int Score) url))
421 (nnweb-set-hashtb (cadar map) (car map))))
422 ;; See whether there is a "Get next 20 hits" button here.
423 (if (or (not (re-search-forward
424 "HREF=\"\\([^\"]+\\)\">Get next" nil t))
425 (>= i nnweb-max-hits))
428 (setq more (match-string 1))
430 (url-insert-file-contents more)))
431 ;; Return the articles in the right order.
433 (sort (nconc nnweb-articles map) 'car-less-than-car))))))
435 (defun nnweb-dejanews-wash-article ()
436 (let ((case-fold-search t))
437 (goto-char (point-min))
438 (re-search-forward "<PRE>" nil t)
439 (delete-region (point-min) (point))
440 (re-search-forward "</PRE>" nil t)
441 (delete-region (point) (point-max))
442 (nnweb-remove-markup)
443 (goto-char (point-min))
444 (while (and (looking-at " *$")
447 (while (looking-at "\\(^[^ ]+:\\) *")
448 (replace-match "\\1 " t)
450 (when (re-search-forward "\n\n+" nil t)
451 (replace-match "\n" t t))
452 (goto-char (point-min))
453 (when (search-forward "[More Headers]" nil t)
454 (replace-match "" t t))))
456 (defun nnweb-dejanews-search (search)
458 (nnweb-definition 'address)
459 `(("query" . ,search)
460 ("defaultOp" . "AND")
461 ("svcclass" . "dncurrent")
463 ("format" . "verbose")
465 ("showsort" . "score")
467 ("ageweight" . "1")))
470 (defun nnweb-dejanewsold-search (search)
472 (nnweb-definition 'address)
473 `(("query" . ,search)
474 ("defaultOp" . "AND")
475 ("svcclass" . "dnold")
477 ("format" . "verbose")
479 ("showsort" . "score")
481 ("ageweight" . "1")))
484 (defun nnweb-dejanews-identity (url)
485 "Return an unique identifier based on URL."
486 (if (string-match "recnum=\\([0-9]+\\)" url)
494 (defun nnweb-reference-create-mapping ()
495 "Perform the search and create an number-to-url alist."
497 (set-buffer nnweb-buffer)
499 (when (funcall (nnweb-definition 'search) nnweb-search)
503 (active (or (cadr (assoc nnweb-group nnweb-group-alist))
505 Subject Score Date Newsgroups From Message-ID
508 ;; Go through all the article hits on this page.
509 (goto-char (point-min))
510 (search-forward "</pre><hr>" nil t)
511 (delete-region (point-min) (point))
512 ;(nnweb-decode-entities)
513 (goto-char (point-min))
514 (while (re-search-forward "^ +[0-9]+\\." nil t)
517 (if (re-search-forward "^$" nil t)
520 (goto-char (point-min))
521 (when (looking-at ".*href=\"\\([^\"]+\\)\"")
522 (setq url (match-string 1)))
523 (nnweb-remove-markup)
524 (goto-char (point-min))
525 (while (search-forward "\t" nil t)
527 (goto-char (point-min))
528 (while (re-search-forward "^\\([^:]+\\): \\(.*\\)$" nil t)
529 (set (intern (match-string 1)) (match-string 2)))
531 (search-forward "</pre>" nil t)
533 (unless (nnweb-get-hashtb url)
537 (make-full-mail-header
538 (cdr active) (concat "(" Newsgroups ") " Subject) From Date
540 nil 0 (string-to-int Score) url))
542 (nnweb-set-hashtb (cadar map) (car map))))
544 ;; Return the articles in the right order.
546 (sort (nconc nnweb-articles map) 'car-less-than-car))))))
548 (defun nnweb-reference-wash-article ()
549 (let ((case-fold-search t))
550 (goto-char (point-min))
551 (re-search-forward "^</center><hr>" nil t)
552 (delete-region (point-min) (point))
553 (search-forward "<pre>" nil t)
555 (let ((body (point-marker)))
556 (search-forward "</pre>" nil t)
557 (delete-region (point) (point-max))
558 (nnweb-remove-markup)
559 (goto-char (point-min))
560 (while (looking-at " *$")
562 (narrow-to-region (point-min) body)
563 (while (and (re-search-forward "^$" nil t)
566 (goto-char (point-min))
567 (while (looking-at "\\(^[^ ]+:\\) *")
568 (replace-match "\\1 " t)
570 (goto-char (point-min))
571 (when (re-search-forward "^References:" nil t)
573 (point) (if (re-search-forward "^$\\|^[^:]+:" nil t)
576 (goto-char (point-min))
578 (unless (looking-at "References")
581 (goto-char (point-min))
582 (while (search-forward "," nil t)
583 (replace-match " " t t)))
585 (set-marker body nil))))
587 (defun nnweb-reference-search (search)
588 (url-insert-file-contents
590 (nnweb-definition 'address)
592 (nnweb-encode-www-form-urlencoded
593 `(("search" . "advanced")
594 ("querytext" . ,search)
599 ("organization" . "")
602 ("choice" . "Search")
603 ("startmonth" . "Jul")
605 ("startyear" . "1996")
610 ("verbosity" . "Verbose")
611 ("ranking" . "Relevance")
615 (setq buffer-file-name nil)
622 (defun nnweb-altavista-create-mapping ()
623 "Perform the search and create an number-to-url alist."
625 (set-buffer nnweb-buffer)
628 (when (funcall (nnweb-definition 'search) nnweb-search part)
632 (active (or (cadr (assoc nnweb-group nnweb-group-alist))
634 subject date from id group
637 ;; Go through all the article hits on this page.
638 (goto-char (point-min))
639 (search-forward "<dt>" nil t)
640 (delete-region (point-min) (match-beginning 0))
641 (goto-char (point-min))
642 (while (search-forward "<dt>" nil t)
643 (replace-match "\n<blubb>"))
644 (nnweb-decode-entities)
645 (goto-char (point-min))
646 (while (re-search-forward "<blubb>.*href=\"\\([^\"]+\\)\"><strong>\\([^>]*\\)</strong></a><dd>\\([^-]+\\)- <b>\\([^<]+\\)<.*href=\"news:\\([^\"]+\\)\">.*\">\\(.+\\)</a><P>"
648 (setq url (match-string 1)
649 subject (match-string 2)
650 date (match-string 3)
651 group (match-string 4)
652 id (concat "<" (match-string 5) ">")
653 from (match-string 6))
655 (unless (nnweb-get-hashtb url)
659 (make-full-mail-header
660 (cdr active) (concat "(" group ") " subject) from date
663 (nnweb-set-hashtb (cadar map) (car map))))
664 ;; See if we want more.
665 (when (or (not nnweb-articles)
666 (>= i nnweb-max-hits)
667 (not (funcall (nnweb-definition 'search)
668 nnweb-search (incf part))))
670 ;; Return the articles in the right order.
672 (sort (nconc nnweb-articles map) 'car-less-than-car)))))))
674 (defun nnweb-altavista-wash-article ()
675 (goto-char (point-min))
676 (let ((case-fold-search t))
677 (when (re-search-forward "^<strong>" nil t)
678 (delete-region (point-min) (match-beginning 0)))
679 (goto-char (point-min))
680 (while (looking-at "<strong>\\([^ ]+\\) +</strong> +\\(.*\\)$")
681 (replace-match "\\1: \\2" t)
683 (when (re-search-backward "^References:" nil t)
684 (narrow-to-region (point) (progn (forward-line 1) (point)))
685 (goto-char (point-min))
686 (while (re-search-forward "<A.*\\?id@\\([^\"]+\\)\">[0-9]+</A>" nil t)
687 (replace-match "<\\1> " t)))
689 (nnweb-remove-markup)))
691 (defun nnweb-altavista-search (search &optional part)
692 (url-insert-file-contents
694 (nnweb-definition 'address)
696 (nnweb-encode-www-form-urlencoded
699 ,@(when part `(("stq" . ,(int-to-string (* part 30)))))
705 (setq buffer-file-name nil)
710 ;;; nnweb.el ends here