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
42 ;; Report failure to find w3 at load time if appropriate.
50 (defvoo nnweb-directory (nnheader-concat gnus-directory "nnweb/")
51 "Where nnweb will save its files.")
53 (defvoo nnweb-type 'dejanews
54 "What search engine type is being used.
55 Valid types include `dejanews', `dejanewsold', `reference',
58 (defvoo nnweb-type-definition
60 (article . nnweb-dejanews-wash-article)
61 (map . nnweb-dejanews-create-mapping)
62 (search . nnweb-dejanews-search)
63 (address . "http://x8.dejanews.com/dnquery.xp")
64 (identifier . nnweb-dejanews-identity))
66 (article . nnweb-dejanews-wash-article)
67 (map . nnweb-dejanews-create-mapping)
68 (search . nnweb-dejanewsold-search)
69 (address . "http://x8.dejanews.com/dnquery.xp")
70 (identifier . nnweb-dejanews-identity))
72 (article . nnweb-reference-wash-article)
73 (map . nnweb-reference-create-mapping)
74 (search . nnweb-reference-search)
75 (address . "http://www.reference.com/cgi-bin/pn/go")
76 (identifier . identity))
78 (article . nnweb-altavista-wash-article)
79 (map . nnweb-altavista-create-mapping)
80 (search . nnweb-altavista-search)
81 (address . "http://www.altavista.digital.com/cgi-bin/query")
82 (id . "/cgi-bin/news?id@%s")
83 (identifier . identity)))
84 "Type-definition alist.")
86 (defvoo nnweb-search nil
87 "Search string to feed to DejaNews.")
89 (defvoo nnweb-max-hits 999
90 "Maximum number of hits to display.")
92 (defvoo nnweb-ephemeral-p nil
93 "Whether this nnweb server is ephemeral.")
95 ;;; Internal variables
97 (defvoo nnweb-articles nil)
98 (defvoo nnweb-buffer nil)
99 (defvoo nnweb-group-alist nil)
100 (defvoo nnweb-group nil)
101 (defvoo nnweb-hashtb nil)
103 ;;; Interface functions
105 (nnoo-define-basics nnweb)
107 (deffoo nnweb-retrieve-headers (articles &optional group server fetch-old)
108 (nnweb-possibly-change-server group server)
110 (set-buffer nntp-server-buffer)
112 (let (article header)
113 (while (setq article (pop articles))
114 (when (setq header (cadr (assq article nnweb-articles)))
115 (nnheader-insert-nov header)))
118 (deffoo nnweb-request-scan (&optional group server)
119 (nnweb-possibly-change-server group server)
120 (setq nnweb-hashtb (gnus-make-hashtable 4095))
121 (funcall (nnweb-definition 'map))
122 (unless nnweb-ephemeral-p
124 (nnweb-write-overview group)))
126 (deffoo nnweb-request-group (group &optional server dont-check)
127 (nnweb-possibly-change-server nil server)
129 (not (equal group nnweb-group))
130 (not nnweb-ephemeral-p))
131 (let ((info (assoc group nnweb-group-alist)))
132 (setq nnweb-group group)
133 (setq nnweb-type (nth 2 info))
134 (setq nnweb-search (nth 3 info))
136 (nnweb-read-overview group))))
138 ((not nnweb-articles)
139 (nnheader-report 'nnweb "No matching articles"))
141 (let ((active (if nnweb-ephemeral-p
142 (cons (caar nnweb-articles)
143 (caar (last nnweb-articles)))
144 (cadr (assoc group nnweb-group-alist)))))
145 (nnheader-report 'nnweb "Opened group %s" group)
147 "211 %d %d %d %s\n" (length nnweb-articles)
148 (car active) (cdr active) group)))))
150 (deffoo nnweb-close-group (group &optional server)
151 (nnweb-possibly-change-server group server)
152 (when (gnus-buffer-live-p nnweb-buffer)
154 (set-buffer nnweb-buffer)
155 (set-buffer-modified-p nil)
156 (kill-buffer nnweb-buffer)))
159 (deffoo nnweb-request-article (article &optional group server buffer)
160 (nnweb-possibly-change-server group server)
162 (set-buffer (or buffer nntp-server-buffer))
163 (let* ((header (cadr (assq article nnweb-articles)))
164 (url (and header (mail-header-xref header))))
166 (nnweb-fetch-url url))
167 (and (stringp article)
168 (nnweb-definition 'id t)
169 (let ((fetch (nnweb-definition 'id))
171 (when (string-match "^<\\(.*\\)>$" article)
172 (setq art (match-string 1 article)))
176 (format fetch article))))))
177 (unless nnheader-callback-function
178 (funcall (nnweb-definition 'article))
179 (nnweb-decode-entities))
180 (nnheader-report 'nnweb "Fetched article %s" article)
183 (deffoo nnweb-close-server (&optional server)
184 (when (and (nnweb-server-opened server)
185 (gnus-buffer-live-p nnweb-buffer))
187 (set-buffer nnweb-buffer)
188 (set-buffer-modified-p nil)
189 (kill-buffer nnweb-buffer)))
190 (nnoo-close-server 'nnweb server))
192 (deffoo nnweb-request-list (&optional server)
193 (nnweb-possibly-change-server nil server)
195 (set-buffer nntp-server-buffer)
196 (nnmail-generate-active nnweb-group-alist)
199 (deffoo nnweb-request-update-info (group info &optional server)
200 (nnweb-possibly-change-server group server)
201 ;;(setcar (cddr info) nil)
204 (deffoo nnweb-asynchronous-p ()
207 (deffoo nnweb-request-create-group (group &optional server args)
208 (nnweb-possibly-change-server nil server)
209 (nnweb-request-delete-group group)
210 (push `(,group ,(cons 1 0) ,@args) nnweb-group-alist)
214 (deffoo nnweb-request-delete-group (group &optional force server)
215 (nnweb-possibly-change-server group server)
216 (gnus-pull group nnweb-group-alist t)
218 (gnus-delete-file (nnweb-overview-file group))
221 (nnoo-define-skeleton nnweb)
223 ;;; Internal functions
225 (defun nnweb-read-overview (group)
226 "Read the overview of GROUP and build the map."
227 (when (file-exists-p (nnweb-overview-file group))
229 (nnheader-insert-file-contents (nnweb-overview-file group))
230 (goto-char (point-min))
233 (setq header (nnheader-parse-nov))
235 (push (list (mail-header-number header)
236 header (mail-header-xref header))
238 (nnweb-set-hashtb header (car nnweb-articles)))))))
240 (defun nnweb-write-overview (group)
241 "Write the overview file for GROUP."
242 (with-temp-file (nnweb-overview-file group)
243 (let ((articles nnweb-articles))
245 (nnheader-insert-nov (cadr (pop articles)))))))
247 (defun nnweb-set-hashtb (header data)
248 (gnus-sethash (nnweb-identifier (mail-header-xref header))
251 (defun nnweb-get-hashtb (url)
252 (gnus-gethash (nnweb-identifier url) nnweb-hashtb))
254 (defun nnweb-identifier (ident)
255 (funcall (nnweb-definition 'identifier) ident))
257 (defun nnweb-overview-file (group)
258 "Return the name of the overview file of GROUP."
259 (nnheader-concat nnweb-directory group ".overview"))
261 (defun nnweb-write-active ()
262 "Save the active file."
263 (with-temp-file (nnheader-concat nnweb-directory "active")
264 (prin1 `(setq nnweb-group-alist ',nnweb-group-alist) (current-buffer))))
266 (defun nnweb-read-active ()
267 "Read the active file."
268 (load (nnheader-concat nnweb-directory "active") t t t))
270 (defun nnweb-definition (type &optional noerror)
271 "Return the definition of TYPE."
272 (let ((def (cdr (assq type (assq nnweb-type nnweb-type-definition)))))
275 (error "Undefined definition %s" type))
278 (defun nnweb-possibly-change-server (&optional group server)
281 (unless (nnweb-server-opened server)
282 (nnweb-open-server server)))
283 (unless nnweb-group-alist
286 (when (and (not nnweb-ephemeral-p)
287 (not (equal group nnweb-group)))
288 (nnweb-request-group group nil t))))
290 (defun nnweb-init (server)
291 "Initialize buffers and such."
292 (unless (gnus-buffer-live-p nnweb-buffer)
295 (nnheader-set-temp-buffer
296 (format " *nnweb %s %s %s*" nnweb-type nnweb-search server))))))
298 (defun nnweb-fetch-url (url)
300 (if (not nnheader-callback-function)
301 (let ((buf (current-buffer)))
303 (set-buffer nnweb-buffer)
305 (url-insert-file-contents url)
306 (copy-to-buffer buf (point-min) (point-max))
308 (nnweb-url-retrieve-asynch
309 url 'nnweb-callback (current-buffer) nnheader-callback-function)
312 (defun nnweb-callback (buffer callback)
313 (when (gnus-buffer-live-p url-working-buffer)
315 (set-buffer url-working-buffer)
316 (funcall (nnweb-definition 'article))
317 (nnweb-decode-entities)
319 (goto-char (point-max))
320 (insert-buffer-substring url-working-buffer))
322 (gnus-kill-buffer url-working-buffer)))
324 (defun nnweb-url-retrieve-asynch (url callback &rest data)
325 (let ((url-request-method "GET")
326 (old-asynch url-be-asynchronous)
327 (url-request-data nil)
328 (url-request-extra-headers nil)
329 (url-working-buffer (generate-new-buffer-name " *nnweb*")))
330 (setq-default url-be-asynchronous t)
332 (set-buffer (get-buffer-create url-working-buffer))
333 (setq url-current-callback-data data
334 url-be-asynchronous t
335 url-current-callback-func callback)
337 (setq-default url-be-asynchronous old-asynch)))
339 (defun nnweb-encode-www-form-urlencoded (pairs)
340 "Return PAIRS encoded for forms."
344 (concat (w3-form-encode-xwfu (car data)) "="
345 (w3-form-encode-xwfu (cdr data)))))
348 (defun nnweb-fetch-form (url pairs)
349 (let ((url-request-data (nnweb-encode-www-form-urlencoded pairs))
350 (url-request-method "POST")
351 (url-request-extra-headers
352 '(("Content-type" . "application/x-www-form-urlencoded"))))
353 (url-insert-file-contents url)
354 (setq buffer-file-name nil))
357 (defun nnweb-decode-entities ()
358 (goto-char (point-min))
359 (while (re-search-forward "&\\([a-z]+\\);" nil t)
360 (replace-match (char-to-string (or (cdr (assq (intern (match-string 1))
365 (defun nnweb-remove-markup ()
366 (goto-char (point-min))
367 (while (search-forward "<!--" nil t)
368 (delete-region (match-beginning 0)
369 (or (search-forward "-->" nil t)
371 (goto-char (point-min))
372 (while (re-search-forward "<[^>]+>" nil t)
373 (replace-match "" t t)))
376 ;;; DejaNews functions.
379 (defun nnweb-dejanews-create-mapping ()
380 "Perform the search and create an number-to-url alist."
382 (set-buffer nnweb-buffer)
384 (when (funcall (nnweb-definition 'search) nnweb-search)
388 (active (or (cadr (assoc nnweb-group nnweb-group-alist))
390 Subject (Score "0") Date Newsgroup Author
393 ;; Go through all the article hits on this page.
394 (goto-char (point-min))
395 (nnweb-decode-entities)
396 (goto-char (point-min))
397 (while (re-search-forward "^ <P>\n" nil t)
400 (cond ((re-search-forward "^ <P>\n" nil t)
402 ((search-forward "\n\n" nil t)
406 (goto-char (point-min))
407 (looking-at ".*HREF=\"\\([^\"]+\\)\"\\(.*\\)")
408 (setq url (match-string 1))
409 (let ((begin (point)))
410 (nnweb-remove-markup)
412 (while (search-forward "\t" nil t)
416 (setq Subject (buffer-substring begin (point)))
417 (if (re-search-forward
418 "^ Newsgroup: \\(.*\\)\n Posted on \\([0-9/]+\\) by \\(.*\\)$" nil t)
419 (setq Newsgroup (match-string 1)
420 Date (match-string 2)
421 Author (match-string 3))))
424 (unless (nnweb-get-hashtb url)
428 (make-full-mail-header
429 (cdr active) Subject Author Date
430 (concat "<" (nnweb-identifier url) "@dejanews>")
431 nil 0 (string-to-int Score) url))
433 (nnweb-set-hashtb (cadar map) (car map))))
434 ;; See whether there is a "Get next 20 hits" button here.
435 (if (or (not (re-search-forward
436 "HREF=\"\\([^\"]+\\)\"[<>b]+Next result" nil t))
437 (>= i nnweb-max-hits))
440 (setq more (match-string 1))
442 (url-insert-file-contents more)))
443 ;; Return the articles in the right order.
445 (sort (nconc nnweb-articles map) 'car-less-than-car))))))
447 (defun nnweb-dejanews-wash-article ()
448 (let ((case-fold-search t))
449 (goto-char (point-min))
450 (re-search-forward "<PRE>" nil t)
451 (delete-region (point-min) (point))
452 (re-search-forward "</PRE>" nil t)
453 (delete-region (point) (point-max))
454 (nnweb-remove-markup)
455 (goto-char (point-min))
456 (while (and (looking-at " *$")
459 (while (looking-at "\\(^[^ ]+:\\) *")
460 (replace-match "\\1 " t)
462 (when (re-search-forward "\n\n+" nil t)
463 (replace-match "\n" t t))
464 (goto-char (point-min))
465 (when (search-forward "[More Headers]" nil t)
466 (replace-match "" t t))))
468 (defun nnweb-dejanews-search (search)
470 (nnweb-definition 'address)
471 `(("query" . ,search)
472 ("defaultOp" . "AND")
473 ("svcclass" . "dncurrent")
475 ("format" . "verbose2")
477 ("showsort" . "date")
479 ("ageweight" . "1")))
482 (defun nnweb-dejanewsold-search (search)
484 (nnweb-definition 'address)
485 `(("query" . ,search)
486 ("defaultOp" . "AND")
487 ("svcclass" . "dnold")
489 ("format" . "verbose2")
491 ("showsort" . "date")
493 ("ageweight" . "1")))
496 (defun nnweb-dejanews-identity (url)
497 "Return an unique identifier based on URL."
498 (if (string-match "recnum=\\([0-9]+\\)" url)
506 (defun nnweb-reference-create-mapping ()
507 "Perform the search and create an number-to-url alist."
509 (set-buffer nnweb-buffer)
511 (when (funcall (nnweb-definition 'search) nnweb-search)
515 (active (or (cadr (assoc nnweb-group nnweb-group-alist))
517 Subject Score Date Newsgroups From Message-ID
520 ;; Go through all the article hits on this page.
521 (goto-char (point-min))
522 (search-forward "</pre><hr>" nil t)
523 (delete-region (point-min) (point))
524 ;(nnweb-decode-entities)
525 (goto-char (point-min))
526 (while (re-search-forward "^ +[0-9]+\\." nil t)
529 (if (re-search-forward "^$" nil t)
532 (goto-char (point-min))
533 (when (looking-at ".*href=\"\\([^\"]+\\)\"")
534 (setq url (match-string 1)))
535 (nnweb-remove-markup)
536 (goto-char (point-min))
537 (while (search-forward "\t" nil t)
539 (goto-char (point-min))
540 (while (re-search-forward "^\\([^:]+\\): \\(.*\\)$" nil t)
541 (set (intern (match-string 1)) (match-string 2)))
543 (search-forward "</pre>" nil t)
545 (unless (nnweb-get-hashtb url)
549 (make-full-mail-header
550 (cdr active) (concat "(" Newsgroups ") " Subject) From Date
552 nil 0 (string-to-int Score) url))
554 (nnweb-set-hashtb (cadar map) (car map))))
556 ;; Return the articles in the right order.
558 (sort (nconc nnweb-articles map) 'car-less-than-car))))))
560 (defun nnweb-reference-wash-article ()
561 (let ((case-fold-search t))
562 (goto-char (point-min))
563 (re-search-forward "^</center><hr>" nil t)
564 (delete-region (point-min) (point))
565 (search-forward "<pre>" nil t)
567 (let ((body (point-marker)))
568 (search-forward "</pre>" nil t)
569 (delete-region (point) (point-max))
570 (nnweb-remove-markup)
571 (goto-char (point-min))
572 (while (looking-at " *$")
574 (narrow-to-region (point-min) body)
575 (while (and (re-search-forward "^$" nil t)
578 (goto-char (point-min))
579 (while (looking-at "\\(^[^ ]+:\\) *")
580 (replace-match "\\1 " t)
582 (goto-char (point-min))
583 (when (re-search-forward "^References:" nil t)
585 (point) (if (re-search-forward "^$\\|^[^:]+:" nil t)
588 (goto-char (point-min))
590 (unless (looking-at "References")
593 (goto-char (point-min))
594 (while (search-forward "," nil t)
595 (replace-match " " t t)))
597 (set-marker body nil))))
599 (defun nnweb-reference-search (search)
600 (url-insert-file-contents
602 (nnweb-definition 'address)
604 (nnweb-encode-www-form-urlencoded
605 `(("search" . "advanced")
606 ("querytext" . ,search)
611 ("organization" . "")
614 ("choice" . "Search")
615 ("startmonth" . "Jul")
617 ("startyear" . "1996")
622 ("verbosity" . "Verbose")
623 ("ranking" . "Relevance")
627 (setq buffer-file-name nil)
634 (defun nnweb-altavista-create-mapping ()
635 "Perform the search and create an number-to-url alist."
637 (set-buffer nnweb-buffer)
640 (when (funcall (nnweb-definition 'search) nnweb-search part)
644 (active (or (cadr (assoc nnweb-group nnweb-group-alist))
646 subject date from id group
649 ;; Go through all the article hits on this page.
650 (goto-char (point-min))
651 (search-forward "<dt>" nil t)
652 (delete-region (point-min) (match-beginning 0))
653 (goto-char (point-min))
654 (while (search-forward "<dt>" nil t)
655 (replace-match "\n<blubb>"))
656 (nnweb-decode-entities)
657 (goto-char (point-min))
658 (while (re-search-forward "<blubb>.*href=\"\\([^\"]+\\)\"><strong>\\([^>]*\\)</strong></a><dd>\\([^-]+\\)- <b>\\([^<]+\\)<.*href=\"news:\\([^\"]+\\)\">.*\">\\(.+\\)</a><P>"
660 (setq url (match-string 1)
661 subject (match-string 2)
662 date (match-string 3)
663 group (match-string 4)
664 id (concat "<" (match-string 5) ">")
665 from (match-string 6))
667 (unless (nnweb-get-hashtb url)
671 (make-full-mail-header
672 (cdr active) (concat "(" group ") " subject) from date
675 (nnweb-set-hashtb (cadar map) (car map))))
676 ;; See if we want more.
677 (when (or (not nnweb-articles)
678 (>= i nnweb-max-hits)
679 (not (funcall (nnweb-definition 'search)
680 nnweb-search (incf part))))
682 ;; Return the articles in the right order.
684 (sort (nconc nnweb-articles map) 'car-less-than-car)))))))
686 (defun nnweb-altavista-wash-article ()
687 (goto-char (point-min))
688 (let ((case-fold-search t))
689 (when (re-search-forward "^<strong>" nil t)
690 (delete-region (point-min) (match-beginning 0)))
691 (goto-char (point-min))
692 (while (looking-at "<strong>\\([^ ]+\\) +</strong> +\\(.*\\)$")
693 (replace-match "\\1: \\2" t)
695 (when (re-search-backward "^References:" nil t)
696 (narrow-to-region (point) (progn (forward-line 1) (point)))
697 (goto-char (point-min))
698 (while (re-search-forward "<A.*\\?id@\\([^\"]+\\)\">[0-9]+</A>" nil t)
699 (replace-match "<\\1> " t)))
701 (nnweb-remove-markup)))
703 (defun nnweb-altavista-search (search &optional part)
704 (url-insert-file-contents
706 (nnweb-definition 'address)
708 (nnweb-encode-www-form-urlencoded
711 ,@(when part `(("stq" . ,(int-to-string (* part 30)))))
717 (setq buffer-file-name nil)
722 ;;; nnweb.el ends here