1 ;;; nnweb.el --- retrieving articles via web search engines
2 ;; Copyright (C) 1996,97 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
43 (defvoo nnweb-directory (nnheader-concat gnus-directory "nnweb/")
44 "Where nnweb will save its files.")
46 (defvoo nnweb-type 'dejanews
47 "What search engine type is being used.")
49 (defvar nnweb-type-definition
51 (article . nnweb-dejanews-wash-article)
52 (map . nnweb-dejanews-create-mapping)
53 (search . nnweb-dejanews-search)
54 (address . "http://xp9.dejanews.com/dnquery.xp")
55 (identifier . nnweb-dejanews-identity))
57 (article . nnweb-reference-wash-article)
58 (map . nnweb-reference-create-mapping)
59 (search . nnweb-reference-search)
60 (address . "http://www.reference.com/cgi-bin/pn/go")
61 (identifier . identity))
63 (article . nnweb-altavista-wash-article)
64 (map . nnweb-altavista-create-mapping)
65 (search . nnweb-altavista-search)
66 (address . "http://www.altavista.digital.com/cgi-bin/query")
67 (id . "/cgi-bin/news?id@%s")
68 (identifier . identity)))
69 "Type-definition alist.")
71 (defvoo nnweb-search nil
72 "Search string to feed to DejaNews.")
74 (defvoo nnweb-max-hits 100
75 "Maximum number of hits to display.")
77 (defvoo nnweb-ephemeral-p nil
78 "Whether this nnweb server is ephemeral.")
80 ;;; Internal variables
82 (defvoo nnweb-articles nil)
83 (defvoo nnweb-buffer nil)
84 (defvoo nnweb-group-alist nil)
85 (defvoo nnweb-group nil)
86 (defvoo nnweb-hashtb nil)
88 ;;; Interface functions
90 (nnoo-define-basics nnweb)
92 (deffoo nnweb-retrieve-headers (articles &optional group server fetch-old)
93 (nnweb-possibly-change-server group server)
95 (set-buffer nntp-server-buffer)
98 (while (setq article (pop articles))
99 (when (setq header (cadr (assq article nnweb-articles)))
100 (nnheader-insert-nov header)))
103 (deffoo nnweb-request-scan (&optional group server)
104 (nnweb-possibly-change-server group server)
105 (funcall (nnweb-definition 'map))
106 (unless nnweb-ephemeral-p
108 (nnweb-write-overview group)))
110 (deffoo nnweb-request-group (group &optional server dont-check)
111 (nnweb-possibly-change-server nil server)
113 (not (equal group nnweb-group))
114 (not nnweb-ephemeral-p))
115 (let ((info (assoc group nnweb-group-alist)))
116 (setq nnweb-group group)
117 (setq nnweb-type (nth 2 info))
118 (setq nnweb-search (nth 3 info))
120 (nnweb-read-overview group))))
122 ((not nnweb-articles)
123 (nnheader-report 'nnweb "No matching articles"))
125 (let ((active (if nnweb-ephemeral-p
126 (cons (caar nnweb-articles)
127 (caar (last nnweb-articles)))
128 (cadr (assoc group nnweb-group-alist)))))
129 (nnheader-report 'nnweb "Opened group %s" group)
131 "211 %d %d %d %s\n" (length nnweb-articles)
132 (car active) (cdr active) group)))))
134 (deffoo nnweb-close-group (group &optional server)
135 (nnweb-possibly-change-server group server)
136 (when (gnus-buffer-live-p nnweb-buffer)
138 (set-buffer nnweb-buffer)
139 (set-buffer-modified-p nil)
140 (kill-buffer nnweb-buffer)))
143 (deffoo nnweb-request-article (article &optional group server buffer)
144 (nnweb-possibly-change-server group server)
146 (set-buffer (or buffer nntp-server-buffer))
147 (let* ((header (cadr (assq article nnweb-articles)))
148 (url (and header (mail-header-xref header))))
150 (nnweb-fetch-url url))
151 (and (stringp article)
152 (nnweb-definition 'id t)
153 (let ((fetch (nnweb-definition 'id))
155 (when (string-match "^<\\(.*\\)>$" article)
156 (setq art (match-string 1 article)))
160 (format fetch article))))))
161 (unless nnheader-callback-function
162 (funcall (nnweb-definition 'article))
163 (nnweb-decode-entities))
164 (nnheader-report 'nnweb "Fetched article %s" article)
167 (deffoo nnweb-close-server (&optional server)
168 (when (and (nnweb-server-opened server)
169 (gnus-buffer-live-p nnweb-buffer))
171 (set-buffer nnweb-buffer)
172 (set-buffer-modified-p nil)
173 (kill-buffer nnweb-buffer)))
174 (nnoo-close-server 'nnweb server))
176 (deffoo nnweb-request-list (&optional server)
177 (nnweb-possibly-change-server nil server)
179 (set-buffer nntp-server-buffer)
180 (nnmail-generate-active nnweb-group-alist)
183 (deffoo nnweb-request-update-info (group info &optional server)
184 (nnweb-possibly-change-server group server)
185 ;;(setcar (cddr info) nil)
188 (deffoo nnweb-asynchronous-p ()
191 (deffoo nnweb-request-create-group (group &optional server args)
192 (nnweb-possibly-change-server nil server)
193 (nnweb-request-delete-group group)
194 (push `(,group ,(cons 1 0) ,@args) nnweb-group-alist)
198 (deffoo nnweb-request-delete-group (group &optional force server)
199 (nnweb-possibly-change-server group server)
200 (gnus-delete-assoc group nnweb-group-alist)
201 (gnus-delete-file (nnweb-overview-file group))
204 (nnoo-define-skeleton nnweb)
206 ;;; Internal functions
208 (defun nnweb-read-overview (group)
209 "Read the overview of GROUP and build the map."
210 (when (file-exists-p (nnweb-overview-file group))
211 (nnheader-temp-write nil
212 (nnheader-insert-file-contents (nnweb-overview-file group))
213 (goto-char (point-min))
214 (setq nnweb-hashtb (gnus-make-hashtable
215 (count-lines (point-min) (point-max))))
218 (setq header (nnheader-parse-nov))
220 (push (list (mail-header-number header)
221 header (mail-header-xref header))
223 (nnweb-set-hashtb header (car nnweb-articles)))))))
225 (defun nnweb-write-overview (group)
226 "Write the overview file for GROUP."
227 (nnheader-temp-write (nnweb-overview-file group)
228 (let ((articles nnweb-articles))
230 (nnheader-insert-nov (cadr (pop articles)))))))
232 (defun nnweb-set-hashtb (header data)
233 (gnus-sethash (nnweb-identifier (mail-header-xref header))
236 (defun nnweb-get-hashtb (url)
237 (gnus-gethash (nnweb-identifier url) nnweb-hashtb))
239 (defun nnweb-identifier (ident)
240 (funcall (nnweb-definition 'identifier) ident))
242 (defun nnweb-overview-file (group)
243 "Return the name of the overview file of GROUP."
244 (nnheader-concat nnweb-directory group ".overview"))
246 (defun nnweb-write-active ()
247 "Save the active file."
248 (nnheader-temp-write (nnheader-concat nnweb-directory "active")
249 (prin1 `(setq nnweb-group-alist ',nnweb-group-alist) (current-buffer))))
251 (defun nnweb-read-active ()
252 "Read the active file."
253 (load (nnheader-concat nnweb-directory "active") t t t))
255 (defun nnweb-definition (type &optional noerror)
256 "Return the definition of TYPE."
257 (let ((def (cdr (assq type (assq nnweb-type nnweb-type-definition)))))
260 (error "Undefined definition %s" type))
263 (defun nnweb-possibly-change-server (&optional group server)
266 (unless (nnweb-server-opened server)
267 (nnweb-open-server server)))
268 (unless nnweb-group-alist
271 (when (and (not nnweb-ephemeral-p)
272 (not (equal group nnweb-group)))
273 (nnweb-request-group group nil t))))
275 (defun nnweb-init (server)
276 "Initialize buffers and such."
277 (unless (gnus-buffer-live-p nnweb-buffer)
280 (nnheader-set-temp-buffer
281 (format " *nnweb %s %s %s*" nnweb-type nnweb-search server))))))
283 (defun nnweb-fetch-url (url)
285 (if (not nnheader-callback-function)
286 (let ((buf (current-buffer)))
288 (set-buffer nnweb-buffer)
291 (url-insert-file-contents url)
292 (copy-to-buffer buf (point-min) (point-max)))))
293 (nnweb-url-retrieve-asynch
294 url 'nnweb-callback (current-buffer) nnheader-callback-function)
297 (defun nnweb-callback (buffer callback)
298 (when (gnus-buffer-live-p url-working-buffer)
300 (set-buffer url-working-buffer)
301 (funcall (nnweb-definition 'article))
302 (nnweb-decode-entities)
304 (goto-char (point-max))
305 (insert-buffer-substring url-working-buffer))
307 (gnus-kill-buffer url-working-buffer)))
309 (defun nnweb-url-retrieve-asynch (url callback &rest data)
310 (let ((url-request-method "GET")
311 (old-asynch url-be-asynchronous)
312 (url-request-data nil)
313 (url-request-extra-headers nil)
314 (url-working-buffer (generate-new-buffer-name " *nnweb*")))
315 (setq-default url-be-asynchronous t)
317 (set-buffer (get-buffer-create url-working-buffer))
318 (setq url-current-callback-data data
319 url-be-asynchronous t
320 url-current-callback-func callback)
322 (setq-default url-be-asynchronous old-asynch)))
324 (defun nnweb-encode-www-form-urlencoded (pairs)
325 "Return PAIRS encoded for forms."
329 (concat (w3-form-encode-xwfu (car data)) "="
330 (w3-form-encode-xwfu (cdr data)))))
333 (defun nnweb-fetch-form (url pairs)
334 (let ((url-request-data (nnweb-encode-www-form-urlencoded pairs))
335 (url-request-method "POST")
336 (url-request-extra-headers
337 '(("Content-type" . "application/x-www-form-urlencoded"))))
338 (url-insert-file-contents url)
339 (setq buffer-file-name nil))
342 (defun nnweb-decode-entities ()
343 (goto-char (point-min))
344 (while (re-search-forward "&\\([a-z]+\\);" nil t)
345 (replace-match (char-to-string (or (cdr (assq (intern (match-string 1))
350 (defun nnweb-remove-markup ()
351 (goto-char (point-min))
352 (while (search-forward "<!--" nil t)
353 (delete-region (match-beginning 0)
354 (or (search-forward "-->" nil t)
356 (goto-char (point-min))
357 (while (re-search-forward "<[^>]+>" nil t)
358 (replace-match "" t t)))
361 ;;; DejaNews functions.
364 (defun nnweb-dejanews-create-mapping ()
365 "Perform the search and create an number-to-url alist."
367 (set-buffer nnweb-buffer)
369 (when (funcall (nnweb-definition 'search) nnweb-search)
373 (active (or (cadr (assoc nnweb-group nnweb-group-alist))
375 Subject Score Date Newsgroup Author
378 ;; Go through all the article hits on this page.
379 (goto-char (point-min))
380 (nnweb-decode-entities)
381 (goto-char (point-min))
382 (while (re-search-forward "^ +[0-9]+\\." nil t)
385 (cond ((re-search-forward "^ +[0-9]+\\." nil t)
387 ((search-forward "\n\n" nil t)
391 (goto-char (point-min))
392 (when (looking-at ".*HREF=\"\\([^\"]+\\)\"")
393 (setq url (match-string 1)))
394 (nnweb-remove-markup)
395 (goto-char (point-min))
396 (while (search-forward "\t" nil t)
398 (goto-char (point-min))
399 (while (re-search-forward "^ +\\([^:]+\\): +\\(.*\\)$" nil t)
400 (set (intern (match-string 1)) (match-string 2)))
402 (when (string-match "#[0-9]+/[0-9]+ *$" Subject)
403 (setq Subject (substring Subject 0 (match-beginning 0))))
405 (unless (nnweb-get-hashtb url)
409 (make-full-mail-header
410 (cdr active) (concat "(" Newsgroup ") " Subject) Author Date
411 (concat "<" (nnweb-identifier url) "@dejanews>")
412 nil 0 (string-to-int Score) url))
414 (nnweb-set-hashtb (cadar map) (car map))))
415 ;; See whether there is a "Get next 20 hits" button here.
416 (if (or (not (re-search-forward
417 "HREF=\"\\([^\"]+\\)\">Get next" nil t))
418 (>= i nnweb-max-hits))
421 (setq more (match-string 1))
423 (url-insert-file-contents more)))
424 ;; Return the articles in the right order.
426 (sort (nconc nnweb-articles map)
427 (lambda (s1 s2) (< (car s1) (car s2)))))))))
429 (defun nnweb-dejanews-wash-article ()
430 (let ((case-fold-search t))
431 (goto-char (point-min))
432 (re-search-forward "<PRE>" nil t)
433 (delete-region (point-min) (point))
434 (re-search-forward "</PRE>" nil t)
435 (delete-region (point) (point-max))
436 (nnweb-remove-markup)
437 (goto-char (point-min))
438 (while (and (looking-at " *$")
441 (while (looking-at "\\(^[^ ]+:\\) *")
442 (replace-match "\\1 " t)
444 (when (re-search-forward "\n\n+" nil t)
445 (replace-match "\n" t t))))
447 (defun nnweb-dejanews-search (search)
449 (nnweb-definition 'address)
450 `(("query" . ,search)
451 ("defaultOp" . "AND")
452 ("svcclass" . "dncurrent")
454 ("format" . "verbose")
456 ("showsort" . "score")
458 ("ageweight" . "1")))
461 (defun nnweb-dejanews-identity (url)
462 "Return an unique identifier based on URL."
463 (if (string-match "recnum=\\([0-9]+\\)" url)
471 (defun nnweb-reference-create-mapping ()
472 "Perform the search and create an number-to-url alist."
474 (set-buffer nnweb-buffer)
476 (when (funcall (nnweb-definition 'search) nnweb-search)
480 (active (or (cadr (assoc nnweb-group nnweb-group-alist))
482 Subject Score Date Newsgroups From Message-ID
485 ;; Go through all the article hits on this page.
486 (goto-char (point-min))
487 (search-forward "</pre><hr>" nil t)
488 (delete-region (point-min) (point))
489 ;(nnweb-decode-entities)
490 (goto-char (point-min))
491 (while (re-search-forward "^ +[0-9]+\\." nil t)
494 (if (re-search-forward "^$" nil t)
497 (goto-char (point-min))
498 (when (looking-at ".*href=\"\\([^\"]+\\)\"")
499 (setq url (match-string 1)))
500 (nnweb-remove-markup)
501 (goto-char (point-min))
502 (while (search-forward "\t" nil t)
504 (goto-char (point-min))
505 (while (re-search-forward "^\\([^:]+\\): \\(.*\\)$" nil t)
506 (set (intern (match-string 1)) (match-string 2)))
508 (search-forward "</pre>" nil t)
510 (unless (nnweb-get-hashtb url)
514 (make-full-mail-header
515 (cdr active) (concat "(" Newsgroups ") " Subject) From Date
517 nil 0 (string-to-int Score) url))
519 (nnweb-set-hashtb (cadar map) (car map))))
521 ;; Return the articles in the right order.
523 (sort (nconc nnweb-articles map)
524 (lambda (s1 s2) (< (car s1) (car s2)))))))))
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)
567 (url-insert-file-contents
569 (nnweb-definition 'address)
571 (nnweb-encode-www-form-urlencoded
572 `(("search" . "advanced")
573 ("querytext" . ,search)
578 ("organization" . "")
581 ("choice" . "Search")
582 ("startmonth" . "Jul")
584 ("startyear" . "1996")
589 ("verbosity" . "Verbose")
590 ("ranking" . "Relevance")
594 (setq buffer-file-name nil))
601 (defun nnweb-altavista-create-mapping ()
602 "Perform the search and create an number-to-url alist."
604 (set-buffer nnweb-buffer)
607 (when (funcall (nnweb-definition 'search) nnweb-search part)
611 (active (or (cadr (assoc nnweb-group nnweb-group-alist))
613 subject date from id group
616 ;; Go through all the article hits on this page.
617 (goto-char (point-min))
618 (search-forward "<dt>" nil t)
619 (delete-region (point-min) (match-beginning 0))
620 (goto-char (point-min))
621 (while (search-forward "<dt>" nil t)
622 (replace-match "\n<blubb>"))
623 (nnweb-decode-entities)
624 (goto-char (point-min))
625 (while (re-search-forward "<blubb>.*href=\"\\([^\"]+\\)\"><strong>\\([^>]*\\)</strong></a><dd>\\([^-]+\\)- <b>\\([^<]+\\)<.*href=\"news:\\([^\"]+\\)\">.*\">\\(.+\\)</a><P>"
627 (setq url (match-string 1)
628 subject (match-string 2)
629 date (match-string 3)
630 group (match-string 4)
631 id (concat "<" (match-string 5) ">")
632 from (match-string 6))
634 (unless (nnweb-get-hashtb url)
638 (make-full-mail-header
639 (cdr active) (concat "(" group ") " subject) from date
642 (nnweb-set-hashtb (cadar map) (car map))))
643 ;; See if we want more.
644 (when (or (not nnweb-articles)
645 (>= i nnweb-max-hits)
646 (not (funcall (nnweb-definition 'search)
647 nnweb-search (incf part))))
649 ;; Return the articles in the right order.
651 (sort (nconc nnweb-articles map)
652 (lambda (s1 s2) (< (car s1) (car s2))))))))))
654 (defun nnweb-altavista-wash-article ()
655 (goto-char (point-min))
656 (let ((case-fold-search t))
657 (when (re-search-forward "^<strong>" nil t)
658 (delete-region (point-min) (match-beginning 0)))
659 (goto-char (point-min))
660 (while (looking-at "<strong>\\([^ ]+\\) +</strong> +\\(.*\\)$")
661 (replace-match "\\1: \\2" t)
663 (when (re-search-backward "^References:" nil t)
664 (narrow-to-region (point) (progn (forward-line 1) (point)))
665 (goto-char (point-min))
666 (while (re-search-forward "<A.*\\?id@\\([^\"]+\\)\">[0-9]+</A>" nil t)
667 (replace-match "<\\1> " t)))
669 (nnweb-remove-markup)))
671 (defun nnweb-altavista-search (search &optional part)
673 (url-insert-file-contents
675 (nnweb-definition 'address)
677 (nnweb-encode-www-form-urlencoded
680 ,@(when part `(("stq" . ,(int-to-string (* part 30)))))
686 (setq buffer-file-name nil)))
690 ;;; nnweb.el ends here