1 ;;; nnweb.el --- retrieving articles via web search engines
2 ;; Copyright (C) 1996 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
39 (defvoo nnweb-directory (nnheader-concat gnus-directory "nnweb/")
40 "Where nnweb will save its files.")
42 (defvoo nnweb-type 'dejanews
43 "What search engine type is being used.")
45 (defvar nnweb-type-definition
47 (article . nnweb-dejanews-wash-article)
48 (map . nnweb-dejanews-create-mapping)
49 (search . nnweb-dejanews-search)
50 (address . "http://search.dejanews.com/dnquery.xp"))
52 (article . nnweb-reference-wash-article)
53 (map . nnweb-reference-create-mapping)
54 (search . nnweb-reference-search)
55 (address . "http://www.reference.com/cgi-bin/pn/go"))
57 (article . nnweb-altavista-wash-article)
58 (map . nnweb-altavista-create-mapping)
59 (search . nnweb-altavista-search)
60 (address . "http://www.altavista.digital.com/cgi-bin/query")
61 (id . "/cgi-bin/news?id@%s")))
62 "Type-definition alist.")
64 (defvoo nnweb-search nil
65 "Search string to feed to DejaNews.")
67 (defvoo nnweb-max-hits 100
68 "Maximum number of hits to display.")
70 (defvoo nnweb-ephemeral-p nil
71 "Whether this nnweb server is ephemeral.")
73 ;;; Internal variables
75 (defvoo nnweb-articles nil)
76 (defvoo nnweb-buffer nil)
77 (defvoo nnweb-group-alist nil)
78 (defvoo nnweb-group nil)
79 (defvoo nnweb-hashtb nil)
81 ;;; Interface functions
83 (nnoo-define-basics nnweb)
85 (deffoo nnweb-retrieve-headers (articles &optional group server fetch-old)
86 (nnweb-possibly-change-server group server)
88 (set-buffer nntp-server-buffer)
91 (while (setq article (pop articles))
92 (when (setq header (cadr (assq article nnweb-articles)))
93 (nnheader-insert-nov header)))
96 (deffoo nnweb-request-scan (&optional group server)
97 (nnweb-possibly-change-server group server)
98 (funcall (nnweb-definition 'map))
99 (unless nnweb-ephemeral-p
101 (nnweb-write-overview group)))
103 (deffoo nnweb-request-group (group &optional server dont-check)
104 (nnweb-possibly-change-server nil server)
106 (not (equal group nnweb-group))
107 (not nnweb-ephemeral-p))
108 (let ((info (assoc group nnweb-group-alist)))
109 (setq nnweb-group group)
110 (setq nnweb-type (nth 2 info))
111 (setq nnweb-search (nth 3 info))
113 (nnweb-read-overview group))))
115 ((not nnweb-articles)
116 (nnheader-report 'nnweb "No matching articles"))
118 (let ((active (if nnweb-ephemeral-p
119 (cons (caar nnweb-articles)
120 (caar (last nnweb-articles)))
121 (cadr (assoc group nnweb-group-alist)))))
122 (nnheader-report 'nnweb "Opened group %s" group)
124 "211 %d %d %d %s\n" (length nnweb-articles)
125 (car active) (cdr active) group)))))
127 (deffoo nnweb-close-group (group &optional server)
128 (nnweb-possibly-change-server group server)
129 (when (gnus-buffer-live-p nnweb-buffer)
131 (set-buffer nnweb-buffer)
132 (set-buffer-modified-p nil)
133 (kill-buffer nnweb-buffer)))
136 (deffoo nnweb-request-article (article &optional group server buffer)
137 (nnweb-possibly-change-server group server)
139 (set-buffer (or buffer nntp-server-buffer))
140 (let ((url (caddr (assq article nnweb-articles))))
142 (nnweb-fetch-url url))
143 (and (stringp article)
144 (let ((fetch (nnweb-definition 'id))
146 (when (string-match "^<\\(.*\\)>$" article)
147 (setq art (match-string 1 article)))
151 (format fetch article))))))
152 (unless nnheader-callback-function
153 (funcall (nnweb-definition 'article))
154 (nnweb-decode-entities))
155 (nnheader-report 'nnweb "Fetched article %s" article)
158 (deffoo nnweb-close-server (&optional server)
159 (when (and (nnweb-server-opened server)
160 (gnus-buffer-live-p nnweb-buffer))
162 (set-buffer nnweb-buffer)
163 (set-buffer-modified-p nil)
164 (kill-buffer nnweb-buffer)))
165 (nnoo-close-server 'nnweb server))
167 (deffoo nnweb-request-list (&optional server)
168 (nnweb-possibly-change-server nil server)
170 (set-buffer nntp-server-buffer)
171 (nnmail-generate-active nnweb-group-alist)
174 (deffoo nnweb-request-update-info (group info &optional server)
175 (nnweb-possibly-change-server group server)
176 ;;(setcar (cddr info) nil)
179 (deffoo nnweb-asynchronous-p ()
182 (deffoo nnweb-request-create-group (group &optional server args)
183 (nnweb-possibly-change-server nil server)
184 (nnweb-request-delete-group group)
185 (push `(,group ,(cons 1 0) ,@args) nnweb-group-alist)
189 (deffoo nnweb-request-delete-group (group &optional force server)
190 (nnweb-possibly-change-server group server)
191 (gnus-delete-assoc group nnweb-group-alist)
192 (gnus-delete-file (nnweb-overview-file group))
195 (nnoo-define-skeleton nnweb)
197 ;;; Internal functions
199 (defun nnweb-read-overview (group)
200 "Read the overview of GROUP and build the map."
201 (when (file-exists-p (nnweb-overview-file group))
202 (nnheader-temp-write nil
203 (insert-file-contents (nnweb-overview-file group))
204 (goto-char (point-min))
205 (setq nnweb-hashtb (gnus-make-hashtable
206 (count-lines (point-min) (point-max))))
209 (setq header (nnheader-parse-nov))
211 (push (list (mail-header-number header)
212 header (mail-header-xrefs header))
214 (nnweb-set-hashtb header (car nnweb-articles)))))))
216 (defun nnweb-write-overview (group)
217 "Write the overview file for GROUP."
218 (nnheader-temp-write (nnweb-overview-file group)
219 (let ((articles nnweb-articles))
221 (nnheader-insert-nov (cadr (pop articles)))))))
223 (defun nnweb-set-hashtb (header data)
224 (gnus-sethasb (nnweb-identifier (mail-header-xrefs header))
227 (defun nnweb-get-hashtb (url)
228 (gnus-gethash (nnweb-identifier url) nnweb-hashtb))
230 (defun nnweb-identifier (ident)
233 (defun nnweb-overview-file (group)
234 "Return the name of the overview file of GROUP."
235 (nnheader-concat nnweb-directory group ".overview"))
237 (defun nnweb-write-active ()
238 "Save the active file."
239 (nnheader-temp-write (nnheader-concat nnweb-directory "active")
240 (prin1 `(setq nnweb-group-alist ',nnweb-group-alist) (current-buffer))))
242 (defun nnweb-read-active ()
243 "Read the active file."
244 (load (nnheader-concat nnweb-directory "active") t t t))
246 (defun nnweb-definition (type)
247 "Return the definition of TYPE."
248 (let ((def (cdr (assq type (assq nnweb-type nnweb-type-definition)))))
250 (error "Undefined definition %s" type))
253 (defun nnweb-possibly-change-server (&optional group server)
256 (unless (nnweb-server-opened server)
257 (nnweb-open-server server)))
259 (when (and (not nnweb-ephemeral-p)
260 (not (equal group nnweb-group)))
261 (nnweb-request-group group nil t))))
263 (defun nnweb-init (server)
264 "Initialize buffers and such."
265 (unless (gnus-buffer-live-p nnweb-buffer)
268 (nnheader-set-temp-buffer
269 (format " *nnweb %s %s %s*" nnweb-type nnweb-search server))))))
271 (defun nnweb-fetch-url (url)
273 (if (not nnheader-callback-function)
274 (let ((buf (current-buffer)))
276 (set-buffer nnweb-buffer)
279 (url-insert-file-contents url)
280 (copy-to-buffer buf (point-min) (point-max)))))
281 (nnweb-url-retrieve-asynch
282 url 'nnweb-callback (current-buffer) nnheader-callback-function)
285 (defun nnweb-callback (buffer callback)
286 (when (gnus-buffer-live-p url-working-buffer)
288 (set-buffer url-working-buffer)
289 (funcall (nnweb-definition 'article))
290 (nnweb-decode-entities)
292 (goto-char (point-max))
293 (insert-buffer-substring url-working-buffer))
295 (gnus-kill-buffer url-working-buffer)))
297 (defun nnweb-url-retrieve-asynch (url callback &rest data)
298 (let ((url-request-method "GET")
299 (old-asynch url-be-asynchronous)
300 (url-request-data nil)
301 (url-request-extra-headers nil)
302 (url-working-buffer (generate-new-buffer-name " *nnweb*")))
303 (setq-default url-be-asynchronous t)
305 (set-buffer (get-buffer-create url-working-buffer))
306 (setq url-current-callback-data data
307 url-be-asynchronous t
308 url-current-callback-func callback)
310 (setq-default url-be-asynchronous old-asynch)))
312 (defun nnweb-encode-www-form-urlencoded (pairs)
313 "Return PAIRS encoded for forms."
317 (concat (w3-form-encode-xwfu (car data)) "="
318 (w3-form-encode-xwfu (cdr data)))))
321 (defun nnweb-fetch-form (url pairs)
322 (let ((url-request-data (nnweb-encode-www-form-urlencoded pairs))
323 (url-request-method 'POST)
324 (url-request-extra-headers
325 '(("Content-type" . "application/x-www-form-urlencoded"))))
326 (url-insert-file-contents url)
327 (setq buffer-file-name nil))
330 (defun nnweb-decode-entities ()
331 (goto-char (point-min))
332 (while (re-search-forward "&\\([a-z]+\\);" nil t)
333 (replace-match (char-to-string (or (cdr (assq (intern (match-string 1))
338 (defun nnweb-remove-markup ()
339 (goto-char (point-min))
340 (while (search-forward "<!--" nil t)
341 (delete-region (match-beginning 0)
342 (or (search-forward "-->" nil t)
344 (goto-char (point-min))
345 (while (re-search-forward "<[^>]+>" nil t)
346 (replace-match "" t t)))
349 ;;; DejaNews functions.
352 (defun nnweb-dejanews-create-mapping ()
353 "Perform the search and create an number-to-url alist."
355 (set-buffer nnweb-buffer)
357 (when (funcall (nnweb-definition 'search) nnweb-search)
361 (active (or (cadr (assoc nnweb-group nnweb-group-alist))
363 Subject Score Date Newsgroup Author
366 ;; Go through all the article hits on this page.
367 (goto-char (point-min))
368 (nnweb-decode-entities)
369 (goto-char (point-min))
370 (while (re-search-forward "^ +[0-9]+\\." nil t)
373 (cond ((re-search-forward "^ +[0-9]+\\." nil t)
375 ((search-forward "\n\n" nil t)
379 (goto-char (point-min))
380 (when (looking-at ".*HREF=\"\\([^\"]+\\)\"")
381 (setq url (match-string 1)))
382 (nnweb-remove-markup)
383 (goto-char (point-min))
384 (while (search-forward "\t" nil t)
386 (goto-char (point-min))
387 (while (re-search-forward "^ +\\([^:]+\\): +\\(.*\\)$" nil t)
388 (set (intern (match-string 1)) (match-string 2)))
390 (when (string-match "#[0-9]+/[0-9]+ *$" Subject)
391 (setq Subject (substring Subject 0 (match-beginning 0))))
392 (unless (nnweb-get-hashtb url)
397 (make-full-mail-header
398 (cdr active) (concat "(" Newsgroup ") " Subject) Author Date
399 (concat "<" (message-unique-id) "-" (int-to-string i)
401 nil 0 (string-to-int Score) nil)
404 ;; See whether there is a "Get next 20 hits" button here.
405 (if (or (not (re-search-forward
406 "HREF=\"\\([^\"]+\\)\">Get next" nil t))
407 (>= i nnweb-max-hits))
410 (setq more (match-string 1))
412 (url-insert-file-contents more)))
413 ;; Return the articles in the right order.
415 (sort map (lambda (s1 s2) (< (car s1) (car s2)))))))))
417 (defun nnweb-dejanews-wash-article ()
418 (let ((case-fold-search t))
419 (goto-char (point-min))
420 (re-search-forward "<PRE>" nil t)
421 (delete-region (point-min) (point))
422 (re-search-forward "</PRE>" nil t)
423 (delete-region (point) (point-max))
424 (nnweb-remove-markup)
425 (goto-char (point-min))
426 (while (and (looking-at " *$")
429 (while (looking-at "\\(^[^ ]+:\\) *")
430 (replace-match "\\1 " t)
432 (when (re-search-forward "\n\n+" nil t)
433 (replace-match "\n" t t))))
435 (defun nnweb-dejanews-search (search)
437 (nnweb-definition 'address)
438 `(("query" . ,search)
439 ("defaultOp" . "AND")
440 ("svcclass" . "dncurrent")
442 ("format" . "verbose")
444 ("showsort" . "score")
446 ("ageweight" . "1")))
453 (defun nnweb-reference-create-mapping ()
454 "Perform the search and create an number-to-url alist."
456 (set-buffer nnweb-buffer)
458 (when (funcall (nnweb-definition 'search) nnweb-search)
462 (active (or (cadr (assoc nnweb-group nnweb-group-alist))
464 Subject Score Date Newsgroups From Message-ID
467 ;; Go through all the article hits on this page.
468 (goto-char (point-min))
469 (search-forward "</pre><hr>" nil t)
470 (delete-region (point-min) (point))
471 ;(nnweb-decode-entities)
472 (goto-char (point-min))
473 (while (re-search-forward "^ +[0-9]+\\." nil t)
476 (if (re-search-forward "^$" nil t)
479 (goto-char (point-min))
480 (when (looking-at ".*href=\"\\([^\"]+\\)\"")
481 (setq url (match-string 1)))
482 (nnweb-remove-markup)
483 (goto-char (point-min))
484 (while (search-forward "\t" nil t)
486 (goto-char (point-min))
487 (while (re-search-forward "^\\([^:]+\\): \\(.*\\)$" nil t)
488 (set (intern (match-string 1)) (match-string 2)))
490 (search-forward "</pre>" nil t)
491 (unless (nnweb-get-hashtb url)
496 (make-full-mail-header
497 (cdr active) (concat "(" Newsgroups ") " Subject) From Date
499 nil 0 (string-to-int Score) nil)
503 ;; Return the articles in the right order.
505 (sort map (lambda (s1 s2) (< (car s1) (car s2)))))))))
507 (defun nnweb-reference-wash-article ()
508 (let ((case-fold-search t))
509 (goto-char (point-min))
510 (re-search-forward "^</center><hr>" nil t)
511 (delete-region (point-min) (point))
512 (search-forward "<pre>" nil t)
514 (let ((body (point-marker)))
515 (search-forward "</pre>" nil t)
516 (delete-region (point) (point-max))
517 (nnweb-remove-markup)
518 (goto-char (point-min))
519 (while (looking-at " *$")
521 (narrow-to-region (point-min) body)
522 (while (and (re-search-forward "^$" nil t)
525 (goto-char (point-min))
526 (while (looking-at "\\(^[^ ]+:\\) *")
527 (replace-match "\\1 " t)
529 (goto-char (point-min))
530 (when (re-search-forward "^References:" nil t)
532 (point) (if (re-search-forward "^$\\|^[^:]+:" nil t)
535 (goto-char (point-min))
537 (unless (looking-at "References")
540 (goto-char (point-min))
541 (while (search-forward "," nil t)
542 (replace-match " " t t)))
544 (set-marker body nil))))
546 (defun nnweb-reference-search (search)
548 (url-insert-file-contents
550 (nnweb-definition 'address)
552 (nnweb-encode-www-form-urlencoded
553 `(("search" . "advanced")
554 ("querytext" . ,search)
559 ("organization" . "")
562 ("choice" . "Search")
563 ("startmonth" . "Jul")
565 ("startyear" . "1996")
570 ("verbosity" . "Verbose")
571 ("ranking" . "Relevance")
575 (setq buffer-file-name nil))
582 (defun nnweb-altavista-create-mapping ()
583 "Perform the search and create an number-to-url alist."
585 (set-buffer nnweb-buffer)
588 (when (funcall (nnweb-definition 'search) nnweb-search part)
592 (active (or (cadr (assoc nnweb-group nnweb-group-alist))
594 subject date from id group
597 ;; Go through all the article hits on this page.
598 (goto-char (point-min))
599 (search-forward "<dt>" nil t)
600 (delete-region (point-min) (match-beginning 0))
601 (goto-char (point-min))
602 (while (search-forward "<dt>" nil t)
603 (replace-match "\n<blubb>"))
604 (nnweb-decode-entities)
605 (goto-char (point-min))
606 (while (re-search-forward "<blubb>.*href=\"\\([^\"]+\\)\"><strong>\\([^>]*\\)</strong></a><dd>\\([^-]+\\)- <b>\\([^<]+\\)<.*href=\"news:\\([^\"]+\\)\">.*\">\\(.+\\)</a><P>"
608 (setq url (match-string 1)
609 subject (match-string 2)
610 date (match-string 3)
611 group (match-string 4)
612 id (concat "<" (match-string 5) ">")
613 from (match-string 6))
614 (unless (nnweb-get-hashtb url)
619 (make-full-mail-header
620 (cdr active) (concat "(" group ") " subject) from date
624 ;; See if we want more.
625 (when (or (not nnweb-articles)
626 (>= i nnweb-max-hits)
627 (not (funcall (nnweb-definition 'search)
628 nnweb-search (incf part))))
630 ;; Return the articles in the right order.
632 (sort map (lambda (s1 s2) (< (car s1) (car s2))))))))))
634 (defun nnweb-altavista-wash-article ()
635 (goto-char (point-min))
636 (let ((case-fold-search t))
637 (when (re-search-forward "^<strong>" nil t)
638 (delete-region (point-min) (match-beginning 0)))
639 (goto-char (point-min))
640 (while (looking-at "<strong>\\([^ ]+\\) +</strong> +\\(.*\\)$")
641 (replace-match "\\1: \\2" t)
643 (when (re-search-backward "^References:" nil t)
644 (narrow-to-region (point) (progn (forward-line 1) (point)))
645 (goto-char (point-min))
646 (while (re-search-forward "<A.*\\?id@\\([^\"]+\\)\">[0-9]+</A>" nil t)
647 (replace-match "<\\1> " t)))
649 (nnweb-remove-markup)))
651 (defun nnweb-altavista-search (search &optional part)
653 (url-insert-file-contents
655 (nnweb-definition 'address)
657 (nnweb-encode-www-form-urlencoded
660 ,@(when part `(("stq" . ,(int-to-string (* part 30)))))
666 (setq buffer-file-name nil)))
670 ;;; nnweb.el ends here