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 (setq nnweb-hashtb (gnus-make-hashtable 4095))
106 (funcall (nnweb-definition 'map))
107 (unless nnweb-ephemeral-p
109 (nnweb-write-overview group)))
111 (deffoo nnweb-request-group (group &optional server dont-check)
112 (nnweb-possibly-change-server nil server)
114 (not (equal group nnweb-group))
115 (not nnweb-ephemeral-p))
116 (let ((info (assoc group nnweb-group-alist)))
117 (setq nnweb-group group)
118 (setq nnweb-type (nth 2 info))
119 (setq nnweb-search (nth 3 info))
121 (nnweb-read-overview group))))
123 ((not nnweb-articles)
124 (nnheader-report 'nnweb "No matching articles"))
126 (let ((active (if nnweb-ephemeral-p
127 (cons (caar nnweb-articles)
128 (caar (last nnweb-articles)))
129 (cadr (assoc group nnweb-group-alist)))))
130 (nnheader-report 'nnweb "Opened group %s" group)
132 "211 %d %d %d %s\n" (length nnweb-articles)
133 (car active) (cdr active) group)))))
135 (deffoo nnweb-close-group (group &optional server)
136 (nnweb-possibly-change-server group server)
137 (when (gnus-buffer-live-p nnweb-buffer)
139 (set-buffer nnweb-buffer)
140 (set-buffer-modified-p nil)
141 (kill-buffer nnweb-buffer)))
144 (deffoo nnweb-request-article (article &optional group server buffer)
145 (nnweb-possibly-change-server group server)
147 (set-buffer (or buffer nntp-server-buffer))
148 (let* ((header (cadr (assq article nnweb-articles)))
149 (url (and header (mail-header-xref header))))
151 (nnweb-fetch-url url))
152 (and (stringp article)
153 (nnweb-definition 'id t)
154 (let ((fetch (nnweb-definition 'id))
156 (when (string-match "^<\\(.*\\)>$" article)
157 (setq art (match-string 1 article)))
161 (format fetch article))))))
162 (unless nnheader-callback-function
163 (funcall (nnweb-definition 'article))
164 (nnweb-decode-entities))
165 (nnheader-report 'nnweb "Fetched article %s" article)
168 (deffoo nnweb-close-server (&optional server)
169 (when (and (nnweb-server-opened server)
170 (gnus-buffer-live-p nnweb-buffer))
172 (set-buffer nnweb-buffer)
173 (set-buffer-modified-p nil)
174 (kill-buffer nnweb-buffer)))
175 (nnoo-close-server 'nnweb server))
177 (deffoo nnweb-request-list (&optional server)
178 (nnweb-possibly-change-server nil server)
180 (set-buffer nntp-server-buffer)
181 (nnmail-generate-active nnweb-group-alist)
184 (deffoo nnweb-request-update-info (group info &optional server)
185 (nnweb-possibly-change-server group server)
186 ;;(setcar (cddr info) nil)
189 (deffoo nnweb-asynchronous-p ()
192 (deffoo nnweb-request-create-group (group &optional server args)
193 (nnweb-possibly-change-server nil server)
194 (nnweb-request-delete-group group)
195 (push `(,group ,(cons 1 0) ,@args) nnweb-group-alist)
199 (deffoo nnweb-request-delete-group (group &optional force server)
200 (nnweb-possibly-change-server group server)
201 (gnus-delete-assoc group nnweb-group-alist)
202 (gnus-delete-file (nnweb-overview-file group))
205 (nnoo-define-skeleton nnweb)
207 ;;; Internal functions
209 (defun nnweb-read-overview (group)
210 "Read the overview of GROUP and build the map."
211 (when (file-exists-p (nnweb-overview-file group))
212 (nnheader-temp-write nil
213 (nnheader-insert-file-contents (nnweb-overview-file group))
214 (goto-char (point-min))
217 (setq header (nnheader-parse-nov))
219 (push (list (mail-header-number header)
220 header (mail-header-xref header))
222 (nnweb-set-hashtb header (car nnweb-articles)))))))
224 (defun nnweb-write-overview (group)
225 "Write the overview file for GROUP."
226 (nnheader-temp-write (nnweb-overview-file group)
227 (let ((articles nnweb-articles))
229 (nnheader-insert-nov (cadr (pop articles)))))))
231 (defun nnweb-set-hashtb (header data)
232 (gnus-sethash (nnweb-identifier (mail-header-xref header))
235 (defun nnweb-get-hashtb (url)
236 (gnus-gethash (nnweb-identifier url) nnweb-hashtb))
238 (defun nnweb-identifier (ident)
239 (funcall (nnweb-definition 'identifier) ident))
241 (defun nnweb-overview-file (group)
242 "Return the name of the overview file of GROUP."
243 (nnheader-concat nnweb-directory group ".overview"))
245 (defun nnweb-write-active ()
246 "Save the active file."
247 (nnheader-temp-write (nnheader-concat nnweb-directory "active")
248 (prin1 `(setq nnweb-group-alist ',nnweb-group-alist) (current-buffer))))
250 (defun nnweb-read-active ()
251 "Read the active file."
252 (load (nnheader-concat nnweb-directory "active") t t t))
254 (defun nnweb-definition (type &optional noerror)
255 "Return the definition of TYPE."
256 (let ((def (cdr (assq type (assq nnweb-type nnweb-type-definition)))))
259 (error "Undefined definition %s" type))
262 (defun nnweb-possibly-change-server (&optional group server)
265 (unless (nnweb-server-opened server)
266 (nnweb-open-server server)))
267 (unless nnweb-group-alist
270 (when (and (not nnweb-ephemeral-p)
271 (not (equal group nnweb-group)))
272 (nnweb-request-group group nil t))))
274 (defun nnweb-init (server)
275 "Initialize buffers and such."
276 (unless (gnus-buffer-live-p nnweb-buffer)
279 (nnheader-set-temp-buffer
280 (format " *nnweb %s %s %s*" nnweb-type nnweb-search server))))))
282 (defun nnweb-fetch-url (url)
284 (if (not nnheader-callback-function)
285 (let ((buf (current-buffer)))
287 (set-buffer nnweb-buffer)
289 (url-insert-file-contents url)
290 (copy-to-buffer buf (point-min) (point-max))
292 (nnweb-url-retrieve-asynch
293 url 'nnweb-callback (current-buffer) nnheader-callback-function)
296 (defun nnweb-callback (buffer callback)
297 (when (gnus-buffer-live-p url-working-buffer)
299 (set-buffer url-working-buffer)
300 (funcall (nnweb-definition 'article))
301 (nnweb-decode-entities)
303 (goto-char (point-max))
304 (insert-buffer-substring url-working-buffer))
306 (gnus-kill-buffer url-working-buffer)))
308 (defun nnweb-url-retrieve-asynch (url callback &rest data)
309 (let ((url-request-method "GET")
310 (old-asynch url-be-asynchronous)
311 (url-request-data nil)
312 (url-request-extra-headers nil)
313 (url-working-buffer (generate-new-buffer-name " *nnweb*")))
314 (setq-default url-be-asynchronous t)
316 (set-buffer (get-buffer-create url-working-buffer))
317 (setq url-current-callback-data data
318 url-be-asynchronous t
319 url-current-callback-func callback)
321 (setq-default url-be-asynchronous old-asynch)))
323 (defun nnweb-encode-www-form-urlencoded (pairs)
324 "Return PAIRS encoded for forms."
328 (concat (w3-form-encode-xwfu (car data)) "="
329 (w3-form-encode-xwfu (cdr data)))))
332 (defun nnweb-fetch-form (url pairs)
333 (let ((url-request-data (nnweb-encode-www-form-urlencoded pairs))
334 (url-request-method "POST")
335 (url-request-extra-headers
336 '(("Content-type" . "application/x-www-form-urlencoded"))))
337 (url-insert-file-contents url)
338 (setq buffer-file-name nil))
341 (defun nnweb-decode-entities ()
342 (goto-char (point-min))
343 (while (re-search-forward "&\\([a-z]+\\);" nil t)
344 (replace-match (char-to-string (or (cdr (assq (intern (match-string 1))
349 (defun nnweb-remove-markup ()
350 (goto-char (point-min))
351 (while (search-forward "<!--" nil t)
352 (delete-region (match-beginning 0)
353 (or (search-forward "-->" nil t)
355 (goto-char (point-min))
356 (while (re-search-forward "<[^>]+>" nil t)
357 (replace-match "" t t)))
360 ;;; DejaNews functions.
363 (defun nnweb-dejanews-create-mapping ()
364 "Perform the search and create an number-to-url alist."
366 (set-buffer nnweb-buffer)
368 (when (funcall (nnweb-definition 'search) nnweb-search)
372 (active (or (cadr (assoc nnweb-group nnweb-group-alist))
374 Subject Score Date Newsgroup Author
377 ;; Go through all the article hits on this page.
378 (goto-char (point-min))
379 (nnweb-decode-entities)
380 (goto-char (point-min))
381 (while (re-search-forward "^ +[0-9]+\\." nil t)
384 (cond ((re-search-forward "^ +[0-9]+\\." nil t)
386 ((search-forward "\n\n" nil t)
390 (goto-char (point-min))
391 (when (looking-at ".*HREF=\"\\([^\"]+\\)\"")
392 (setq url (match-string 1)))
393 (nnweb-remove-markup)
394 (goto-char (point-min))
395 (while (search-forward "\t" nil t)
397 (goto-char (point-min))
398 (while (re-search-forward "^ +\\([^:]+\\): +\\(.*\\)$" nil t)
399 (set (intern (match-string 1)) (match-string 2)))
401 (when (string-match "#[0-9]+/[0-9]+ *$" Subject)
402 (setq Subject (substring Subject 0 (match-beginning 0))))
404 (unless (nnweb-get-hashtb url)
408 (make-full-mail-header
409 (cdr active) (concat "(" Newsgroup ") " Subject) Author Date
410 (concat "<" (nnweb-identifier url) "@dejanews>")
411 nil 0 (string-to-int Score) url))
413 (nnweb-set-hashtb (cadar map) (car map))))
414 ;; See whether there is a "Get next 20 hits" button here.
415 (if (or (not (re-search-forward
416 "HREF=\"\\([^\"]+\\)\">Get next" nil t))
417 (>= i nnweb-max-hits))
420 (setq more (match-string 1))
422 (url-insert-file-contents more)))
423 ;; Return the articles in the right order.
425 (sort (nconc nnweb-articles map)
426 (lambda (s1 s2) (< (car s1) (car s2)))))))))
428 (defun nnweb-dejanews-wash-article ()
429 (let ((case-fold-search t))
430 (goto-char (point-min))
431 (re-search-forward "<PRE>" nil t)
432 (delete-region (point-min) (point))
433 (re-search-forward "</PRE>" nil t)
434 (delete-region (point) (point-max))
435 (nnweb-remove-markup)
436 (goto-char (point-min))
437 (while (and (looking-at " *$")
440 (while (looking-at "\\(^[^ ]+:\\) *")
441 (replace-match "\\1 " t)
443 (when (re-search-forward "\n\n+" nil t)
444 (replace-match "\n" t t))
445 (goto-char (point-min))
446 (when (search-forward "[More Headers]" nil t)
447 (replace-match "" t t))))
449 (defun nnweb-dejanews-search (search)
451 (nnweb-definition 'address)
452 `(("query" . ,search)
453 ("defaultOp" . "AND")
454 ("svcclass" . "dncurrent")
456 ("format" . "verbose")
458 ("showsort" . "score")
460 ("ageweight" . "1")))
463 (defun nnweb-dejanews-identity (url)
464 "Return an unique identifier based on URL."
465 (if (string-match "recnum=\\([0-9]+\\)" url)
473 (defun nnweb-reference-create-mapping ()
474 "Perform the search and create an number-to-url alist."
476 (set-buffer nnweb-buffer)
478 (when (funcall (nnweb-definition 'search) nnweb-search)
482 (active (or (cadr (assoc nnweb-group nnweb-group-alist))
484 Subject Score Date Newsgroups From Message-ID
487 ;; Go through all the article hits on this page.
488 (goto-char (point-min))
489 (search-forward "</pre><hr>" nil t)
490 (delete-region (point-min) (point))
491 ;(nnweb-decode-entities)
492 (goto-char (point-min))
493 (while (re-search-forward "^ +[0-9]+\\." nil t)
496 (if (re-search-forward "^$" nil t)
499 (goto-char (point-min))
500 (when (looking-at ".*href=\"\\([^\"]+\\)\"")
501 (setq url (match-string 1)))
502 (nnweb-remove-markup)
503 (goto-char (point-min))
504 (while (search-forward "\t" nil t)
506 (goto-char (point-min))
507 (while (re-search-forward "^\\([^:]+\\): \\(.*\\)$" nil t)
508 (set (intern (match-string 1)) (match-string 2)))
510 (search-forward "</pre>" nil t)
512 (unless (nnweb-get-hashtb url)
516 (make-full-mail-header
517 (cdr active) (concat "(" Newsgroups ") " Subject) From Date
519 nil 0 (string-to-int Score) url))
521 (nnweb-set-hashtb (cadar map) (car map))))
523 ;; Return the articles in the right order.
525 (sort (nconc nnweb-articles map)
526 (lambda (s1 s2) (< (car s1) (car s2)))))))))
528 (defun nnweb-reference-wash-article ()
529 (let ((case-fold-search t))
530 (goto-char (point-min))
531 (re-search-forward "^</center><hr>" nil t)
532 (delete-region (point-min) (point))
533 (search-forward "<pre>" nil t)
535 (let ((body (point-marker)))
536 (search-forward "</pre>" nil t)
537 (delete-region (point) (point-max))
538 (nnweb-remove-markup)
539 (goto-char (point-min))
540 (while (looking-at " *$")
542 (narrow-to-region (point-min) body)
543 (while (and (re-search-forward "^$" nil t)
546 (goto-char (point-min))
547 (while (looking-at "\\(^[^ ]+:\\) *")
548 (replace-match "\\1 " t)
550 (goto-char (point-min))
551 (when (re-search-forward "^References:" nil t)
553 (point) (if (re-search-forward "^$\\|^[^:]+:" nil t)
556 (goto-char (point-min))
558 (unless (looking-at "References")
561 (goto-char (point-min))
562 (while (search-forward "," nil t)
563 (replace-match " " t t)))
565 (set-marker body nil))))
567 (defun nnweb-reference-search (search)
568 (url-insert-file-contents
570 (nnweb-definition 'address)
572 (nnweb-encode-www-form-urlencoded
573 `(("search" . "advanced")
574 ("querytext" . ,search)
579 ("organization" . "")
582 ("choice" . "Search")
583 ("startmonth" . "Jul")
585 ("startyear" . "1996")
590 ("verbosity" . "Verbose")
591 ("ranking" . "Relevance")
595 (setq buffer-file-name nil)
602 (defun nnweb-altavista-create-mapping ()
603 "Perform the search and create an number-to-url alist."
605 (set-buffer nnweb-buffer)
608 (when (funcall (nnweb-definition 'search) nnweb-search part)
612 (active (or (cadr (assoc nnweb-group nnweb-group-alist))
614 subject date from id group
617 ;; Go through all the article hits on this page.
618 (goto-char (point-min))
619 (search-forward "<dt>" nil t)
620 (delete-region (point-min) (match-beginning 0))
621 (goto-char (point-min))
622 (while (search-forward "<dt>" nil t)
623 (replace-match "\n<blubb>"))
624 (nnweb-decode-entities)
625 (goto-char (point-min))
626 (while (re-search-forward "<blubb>.*href=\"\\([^\"]+\\)\"><strong>\\([^>]*\\)</strong></a><dd>\\([^-]+\\)- <b>\\([^<]+\\)<.*href=\"news:\\([^\"]+\\)\">.*\">\\(.+\\)</a><P>"
628 (setq url (match-string 1)
629 subject (match-string 2)
630 date (match-string 3)
631 group (match-string 4)
632 id (concat "<" (match-string 5) ">")
633 from (match-string 6))
635 (unless (nnweb-get-hashtb url)
639 (make-full-mail-header
640 (cdr active) (concat "(" group ") " subject) from date
643 (nnweb-set-hashtb (cadar map) (car map))))
644 ;; See if we want more.
645 (when (or (not nnweb-articles)
646 (>= i nnweb-max-hits)
647 (not (funcall (nnweb-definition 'search)
648 nnweb-search (incf part))))
650 ;; Return the articles in the right order.
652 (sort (nconc nnweb-articles map)
653 (lambda (s1 s2) (< (car s1) (car s2))))))))))
655 (defun nnweb-altavista-wash-article ()
656 (goto-char (point-min))
657 (let ((case-fold-search t))
658 (when (re-search-forward "^<strong>" nil t)
659 (delete-region (point-min) (match-beginning 0)))
660 (goto-char (point-min))
661 (while (looking-at "<strong>\\([^ ]+\\) +</strong> +\\(.*\\)$")
662 (replace-match "\\1: \\2" t)
664 (when (re-search-backward "^References:" nil t)
665 (narrow-to-region (point) (progn (forward-line 1) (point)))
666 (goto-char (point-min))
667 (while (re-search-forward "<A.*\\?id@\\([^\"]+\\)\">[0-9]+</A>" nil t)
668 (replace-match "<\\1> " t)))
670 (nnweb-remove-markup)))
672 (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)
691 ;;; nnweb.el ends here