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
41 (defvoo nnweb-directory (nnheader-concat gnus-directory "nnweb/")
42 "Where nnweb will save its files.")
44 (defvoo nnweb-type 'dejanews
45 "What search engine type is being used.")
47 (defvar nnweb-type-definition
49 (article . nnweb-dejanews-wash-article)
50 (map . nnweb-dejanews-create-mapping)
51 (search . nnweb-dejanews-search)
52 (address . "http://search.dejanews.com/dnquery.xp"))
54 (article . nnweb-reference-wash-article)
55 (map . nnweb-reference-create-mapping)
56 (search . nnweb-reference-search)
57 (address . "http://www.reference.com/cgi-bin/pn/go"))
59 (article . nnweb-altavista-wash-article)
60 (map . nnweb-altavista-create-mapping)
61 (search . nnweb-altavista-search)
62 (address . "http://www.altavista.digital.com/cgi-bin/query")
63 (id . "/cgi-bin/news?id@%s")))
64 "Type-definition alist.")
66 (defvoo nnweb-search nil
67 "Search string to feed to DejaNews.")
69 (defvoo nnweb-max-hits 100
70 "Maximum number of hits to display.")
72 (defvoo nnweb-ephemeral-p nil
73 "Whether this nnweb server is ephemeral.")
75 ;;; Internal variables
77 (defvoo nnweb-articles nil)
78 (defvoo nnweb-buffer nil)
79 (defvoo nnweb-group-alist nil)
80 (defvoo nnweb-group nil)
81 (defvoo nnweb-hashtb nil)
83 ;;; Interface functions
85 (nnoo-define-basics nnweb)
87 (deffoo nnweb-retrieve-headers (articles &optional group server fetch-old)
88 (nnweb-possibly-change-server group server)
90 (set-buffer nntp-server-buffer)
93 (while (setq article (pop articles))
94 (when (setq header (cadr (assq article nnweb-articles)))
95 (nnheader-insert-nov header)))
98 (deffoo nnweb-request-scan (&optional group server)
99 (nnweb-possibly-change-server group server)
100 (funcall (nnweb-definition 'map))
101 (unless nnweb-ephemeral-p
103 (nnweb-write-overview group)))
105 (deffoo nnweb-request-group (group &optional server dont-check)
106 (nnweb-possibly-change-server nil server)
108 (not (equal group nnweb-group))
109 (not nnweb-ephemeral-p))
110 (let ((info (assoc group nnweb-group-alist)))
111 (setq nnweb-group group)
112 (setq nnweb-type (nth 2 info))
113 (setq nnweb-search (nth 3 info))
115 (nnweb-read-overview group))))
117 ((not nnweb-articles)
118 (nnheader-report 'nnweb "No matching articles"))
120 (let ((active (if nnweb-ephemeral-p
121 (cons (caar nnweb-articles)
122 (caar (last nnweb-articles)))
123 (cadr (assoc group nnweb-group-alist)))))
124 (nnheader-report 'nnweb "Opened group %s" group)
126 "211 %d %d %d %s\n" (length nnweb-articles)
127 (car active) (cdr active) group)))))
129 (deffoo nnweb-close-group (group &optional server)
130 (nnweb-possibly-change-server group server)
131 (when (gnus-buffer-live-p nnweb-buffer)
133 (set-buffer nnweb-buffer)
134 (set-buffer-modified-p nil)
135 (kill-buffer nnweb-buffer)))
138 (deffoo nnweb-request-article (article &optional group server buffer)
139 (nnweb-possibly-change-server group server)
141 (set-buffer (or buffer nntp-server-buffer))
142 (let ((url (caddr (assq article nnweb-articles))))
144 (nnweb-fetch-url url))
145 (and (stringp article)
146 (let ((fetch (nnweb-definition 'id))
148 (when (string-match "^<\\(.*\\)>$" article)
149 (setq art (match-string 1 article)))
153 (format fetch article))))))
154 (unless nnheader-callback-function
155 (funcall (nnweb-definition 'article))
156 (nnweb-decode-entities))
157 (nnheader-report 'nnweb "Fetched article %s" article)
160 (deffoo nnweb-close-server (&optional server)
161 (when (and (nnweb-server-opened server)
162 (gnus-buffer-live-p nnweb-buffer))
164 (set-buffer nnweb-buffer)
165 (set-buffer-modified-p nil)
166 (kill-buffer nnweb-buffer)))
167 (nnoo-close-server 'nnweb server))
169 (deffoo nnweb-request-list (&optional server)
170 (nnweb-possibly-change-server nil server)
172 (set-buffer nntp-server-buffer)
173 (nnmail-generate-active nnweb-group-alist)
176 (deffoo nnweb-request-update-info (group info &optional server)
177 (nnweb-possibly-change-server group server)
178 ;;(setcar (cddr info) nil)
181 (deffoo nnweb-asynchronous-p ()
184 (deffoo nnweb-request-create-group (group &optional server args)
185 (nnweb-possibly-change-server nil server)
186 (nnweb-request-delete-group group)
187 (push `(,group ,(cons 1 0) ,@args) nnweb-group-alist)
191 (deffoo nnweb-request-delete-group (group &optional force server)
192 (nnweb-possibly-change-server group server)
193 (gnus-delete-assoc group nnweb-group-alist)
194 (gnus-delete-file (nnweb-overview-file group))
197 (nnoo-define-skeleton nnweb)
199 ;;; Internal functions
201 (defun nnweb-read-overview (group)
202 "Read the overview of GROUP and build the map."
203 (when (file-exists-p (nnweb-overview-file group))
204 (nnheader-temp-write nil
205 (insert-file-contents (nnweb-overview-file group))
206 (goto-char (point-min))
207 (setq nnweb-hashtb (gnus-make-hashtable
208 (count-lines (point-min) (point-max))))
211 (setq header (nnheader-parse-nov))
213 (push (list (mail-header-number header)
214 header (mail-header-xref header))
216 (nnweb-set-hashtb header (car nnweb-articles)))))))
218 (defun nnweb-write-overview (group)
219 "Write the overview file for GROUP."
220 (nnheader-temp-write (nnweb-overview-file group)
221 (let ((articles nnweb-articles))
223 (nnheader-insert-nov (cadr (pop articles)))))))
225 (defun nnweb-set-hashtb (header data)
226 (gnus-sethash (nnweb-identifier (mail-header-xref header))
229 (defun nnweb-get-hashtb (url)
230 (gnus-gethash (nnweb-identifier url) nnweb-hashtb))
232 (defun nnweb-identifier (ident)
235 (defun nnweb-overview-file (group)
236 "Return the name of the overview file of GROUP."
237 (nnheader-concat nnweb-directory group ".overview"))
239 (defun nnweb-write-active ()
240 "Save the active file."
241 (nnheader-temp-write (nnheader-concat nnweb-directory "active")
242 (prin1 `(setq nnweb-group-alist ',nnweb-group-alist) (current-buffer))))
244 (defun nnweb-read-active ()
245 "Read the active file."
246 (load (nnheader-concat nnweb-directory "active") t t t))
248 (defun nnweb-definition (type)
249 "Return the definition of TYPE."
250 (let ((def (cdr (assq type (assq nnweb-type nnweb-type-definition)))))
252 (error "Undefined definition %s" type))
255 (defun nnweb-possibly-change-server (&optional group server)
258 (unless (nnweb-server-opened server)
259 (nnweb-open-server server)))
261 (when (and (not nnweb-ephemeral-p)
262 (not (equal group nnweb-group)))
263 (nnweb-request-group group nil t))))
265 (defun nnweb-init (server)
266 "Initialize buffers and such."
267 (unless (gnus-buffer-live-p nnweb-buffer)
270 (nnheader-set-temp-buffer
271 (format " *nnweb %s %s %s*" nnweb-type nnweb-search server))))))
273 (defun nnweb-fetch-url (url)
275 (if (not nnheader-callback-function)
276 (let ((buf (current-buffer)))
278 (set-buffer nnweb-buffer)
281 (url-insert-file-contents url)
282 (copy-to-buffer buf (point-min) (point-max)))))
283 (nnweb-url-retrieve-asynch
284 url 'nnweb-callback (current-buffer) nnheader-callback-function)
287 (defun nnweb-callback (buffer callback)
288 (when (gnus-buffer-live-p url-working-buffer)
290 (set-buffer url-working-buffer)
291 (funcall (nnweb-definition 'article))
292 (nnweb-decode-entities)
294 (goto-char (point-max))
295 (insert-buffer-substring url-working-buffer))
297 (gnus-kill-buffer url-working-buffer)))
299 (defun nnweb-url-retrieve-asynch (url callback &rest data)
300 (let ((url-request-method "GET")
301 (old-asynch url-be-asynchronous)
302 (url-request-data nil)
303 (url-request-extra-headers nil)
304 (url-working-buffer (generate-new-buffer-name " *nnweb*")))
305 (setq-default url-be-asynchronous t)
307 (set-buffer (get-buffer-create url-working-buffer))
308 (setq url-current-callback-data data
309 url-be-asynchronous t
310 url-current-callback-func callback)
312 (setq-default url-be-asynchronous old-asynch)))
314 (defun nnweb-encode-www-form-urlencoded (pairs)
315 "Return PAIRS encoded for forms."
319 (concat (w3-form-encode-xwfu (car data)) "="
320 (w3-form-encode-xwfu (cdr data)))))
323 (defun nnweb-fetch-form (url pairs)
324 (let ((url-request-data (nnweb-encode-www-form-urlencoded pairs))
325 (url-request-method 'POST)
326 (url-request-extra-headers
327 '(("Content-type" . "application/x-www-form-urlencoded"))))
328 (url-insert-file-contents url)
329 (setq buffer-file-name nil))
332 (defun nnweb-decode-entities ()
333 (goto-char (point-min))
334 (while (re-search-forward "&\\([a-z]+\\);" nil t)
335 (replace-match (char-to-string (or (cdr (assq (intern (match-string 1))
340 (defun nnweb-remove-markup ()
341 (goto-char (point-min))
342 (while (search-forward "<!--" nil t)
343 (delete-region (match-beginning 0)
344 (or (search-forward "-->" nil t)
346 (goto-char (point-min))
347 (while (re-search-forward "<[^>]+>" nil t)
348 (replace-match "" t t)))
351 ;;; DejaNews functions.
354 (defun nnweb-dejanews-create-mapping ()
355 "Perform the search and create an number-to-url alist."
357 (set-buffer nnweb-buffer)
359 (when (funcall (nnweb-definition 'search) nnweb-search)
363 (active (or (cadr (assoc nnweb-group nnweb-group-alist))
365 Subject Score Date Newsgroup Author
368 ;; Go through all the article hits on this page.
369 (goto-char (point-min))
370 (nnweb-decode-entities)
371 (goto-char (point-min))
372 (while (re-search-forward "^ +[0-9]+\\." nil t)
375 (cond ((re-search-forward "^ +[0-9]+\\." nil t)
377 ((search-forward "\n\n" nil t)
381 (goto-char (point-min))
382 (when (looking-at ".*HREF=\"\\([^\"]+\\)\"")
383 (setq url (match-string 1)))
384 (nnweb-remove-markup)
385 (goto-char (point-min))
386 (while (search-forward "\t" nil t)
388 (goto-char (point-min))
389 (while (re-search-forward "^ +\\([^:]+\\): +\\(.*\\)$" nil t)
390 (set (intern (match-string 1)) (match-string 2)))
392 (when (string-match "#[0-9]+/[0-9]+ *$" Subject)
393 (setq Subject (substring Subject 0 (match-beginning 0))))
394 (unless (nnweb-get-hashtb url)
399 (make-full-mail-header
400 (cdr active) (concat "(" Newsgroup ") " Subject) Author Date
401 (concat "<" (message-unique-id) "-" (int-to-string i)
403 nil 0 (string-to-int Score) nil)
406 ;; See whether there is a "Get next 20 hits" button here.
407 (if (or (not (re-search-forward
408 "HREF=\"\\([^\"]+\\)\">Get next" nil t))
409 (>= i nnweb-max-hits))
412 (setq more (match-string 1))
414 (url-insert-file-contents more)))
415 ;; Return the articles in the right order.
417 (sort map (lambda (s1 s2) (< (car s1) (car s2)))))))))
419 (defun nnweb-dejanews-wash-article ()
420 (let ((case-fold-search t))
421 (goto-char (point-min))
422 (re-search-forward "<PRE>" nil t)
423 (delete-region (point-min) (point))
424 (re-search-forward "</PRE>" nil t)
425 (delete-region (point) (point-max))
426 (nnweb-remove-markup)
427 (goto-char (point-min))
428 (while (and (looking-at " *$")
431 (while (looking-at "\\(^[^ ]+:\\) *")
432 (replace-match "\\1 " t)
434 (when (re-search-forward "\n\n+" nil t)
435 (replace-match "\n" t t))))
437 (defun nnweb-dejanews-search (search)
439 (nnweb-definition 'address)
440 `(("query" . ,search)
441 ("defaultOp" . "AND")
442 ("svcclass" . "dncurrent")
444 ("format" . "verbose")
446 ("showsort" . "score")
448 ("ageweight" . "1")))
455 (defun nnweb-reference-create-mapping ()
456 "Perform the search and create an number-to-url alist."
458 (set-buffer nnweb-buffer)
460 (when (funcall (nnweb-definition 'search) nnweb-search)
464 (active (or (cadr (assoc nnweb-group nnweb-group-alist))
466 Subject Score Date Newsgroups From Message-ID
469 ;; Go through all the article hits on this page.
470 (goto-char (point-min))
471 (search-forward "</pre><hr>" nil t)
472 (delete-region (point-min) (point))
473 ;(nnweb-decode-entities)
474 (goto-char (point-min))
475 (while (re-search-forward "^ +[0-9]+\\." nil t)
478 (if (re-search-forward "^$" nil t)
481 (goto-char (point-min))
482 (when (looking-at ".*href=\"\\([^\"]+\\)\"")
483 (setq url (match-string 1)))
484 (nnweb-remove-markup)
485 (goto-char (point-min))
486 (while (search-forward "\t" nil t)
488 (goto-char (point-min))
489 (while (re-search-forward "^\\([^:]+\\): \\(.*\\)$" nil t)
490 (set (intern (match-string 1)) (match-string 2)))
492 (search-forward "</pre>" nil t)
493 (unless (nnweb-get-hashtb url)
498 (make-full-mail-header
499 (cdr active) (concat "(" Newsgroups ") " Subject) From Date
501 nil 0 (string-to-int Score) nil)
505 ;; Return the articles in the right order.
507 (sort map (lambda (s1 s2) (< (car s1) (car s2)))))))))
509 (defun nnweb-reference-wash-article ()
510 (let ((case-fold-search t))
511 (goto-char (point-min))
512 (re-search-forward "^</center><hr>" nil t)
513 (delete-region (point-min) (point))
514 (search-forward "<pre>" nil t)
516 (let ((body (point-marker)))
517 (search-forward "</pre>" nil t)
518 (delete-region (point) (point-max))
519 (nnweb-remove-markup)
520 (goto-char (point-min))
521 (while (looking-at " *$")
523 (narrow-to-region (point-min) body)
524 (while (and (re-search-forward "^$" nil t)
527 (goto-char (point-min))
528 (while (looking-at "\\(^[^ ]+:\\) *")
529 (replace-match "\\1 " t)
531 (goto-char (point-min))
532 (when (re-search-forward "^References:" nil t)
534 (point) (if (re-search-forward "^$\\|^[^:]+:" nil t)
537 (goto-char (point-min))
539 (unless (looking-at "References")
542 (goto-char (point-min))
543 (while (search-forward "," nil t)
544 (replace-match " " t t)))
546 (set-marker body nil))))
548 (defun nnweb-reference-search (search)
550 (url-insert-file-contents
552 (nnweb-definition 'address)
554 (nnweb-encode-www-form-urlencoded
555 `(("search" . "advanced")
556 ("querytext" . ,search)
561 ("organization" . "")
564 ("choice" . "Search")
565 ("startmonth" . "Jul")
567 ("startyear" . "1996")
572 ("verbosity" . "Verbose")
573 ("ranking" . "Relevance")
577 (setq buffer-file-name nil))
584 (defun nnweb-altavista-create-mapping ()
585 "Perform the search and create an number-to-url alist."
587 (set-buffer nnweb-buffer)
590 (when (funcall (nnweb-definition 'search) nnweb-search part)
594 (active (or (cadr (assoc nnweb-group nnweb-group-alist))
596 subject date from id group
599 ;; Go through all the article hits on this page.
600 (goto-char (point-min))
601 (search-forward "<dt>" nil t)
602 (delete-region (point-min) (match-beginning 0))
603 (goto-char (point-min))
604 (while (search-forward "<dt>" nil t)
605 (replace-match "\n<blubb>"))
606 (nnweb-decode-entities)
607 (goto-char (point-min))
608 (while (re-search-forward "<blubb>.*href=\"\\([^\"]+\\)\"><strong>\\([^>]*\\)</strong></a><dd>\\([^-]+\\)- <b>\\([^<]+\\)<.*href=\"news:\\([^\"]+\\)\">.*\">\\(.+\\)</a><P>"
610 (setq url (match-string 1)
611 subject (match-string 2)
612 date (match-string 3)
613 group (match-string 4)
614 id (concat "<" (match-string 5) ">")
615 from (match-string 6))
616 (unless (nnweb-get-hashtb url)
621 (make-full-mail-header
622 (cdr active) (concat "(" group ") " subject) from date
626 ;; See if we want more.
627 (when (or (not nnweb-articles)
628 (>= i nnweb-max-hits)
629 (not (funcall (nnweb-definition 'search)
630 nnweb-search (incf part))))
632 ;; Return the articles in the right order.
634 (sort map (lambda (s1 s2) (< (car s1) (car s2))))))))))
636 (defun nnweb-altavista-wash-article ()
637 (goto-char (point-min))
638 (let ((case-fold-search t))
639 (when (re-search-forward "^<strong>" nil t)
640 (delete-region (point-min) (match-beginning 0)))
641 (goto-char (point-min))
642 (while (looking-at "<strong>\\([^ ]+\\) +</strong> +\\(.*\\)$")
643 (replace-match "\\1: \\2" t)
645 (when (re-search-backward "^References:" nil t)
646 (narrow-to-region (point) (progn (forward-line 1) (point)))
647 (goto-char (point-min))
648 (while (re-search-forward "<A.*\\?id@\\([^\"]+\\)\">[0-9]+</A>" nil t)
649 (replace-match "<\\1> " t)))
651 (nnweb-remove-markup)))
653 (defun nnweb-altavista-search (search &optional part)
655 (url-insert-file-contents
657 (nnweb-definition 'address)
659 (nnweb-encode-www-form-urlencoded
662 ,@(when part `(("stq" . ,(int-to-string (* part 30)))))
668 (setq buffer-file-name nil)))
672 ;;; nnweb.el ends here