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
40 (defvoo nnweb-type 'dejanews
41 "What search engine type is being used.")
43 (defvar nnweb-type-definition
45 (article . nnweb-dejanews-wash-article)
46 (map . nnweb-dejanews-create-mapping)
47 (search . nnweb-dejanews-search)
48 (address . "http://search.dejanews.com/dnquery.xp"))
50 (article . nnweb-reference-wash-article)
51 (map . nnweb-reference-create-mapping)
52 (search . nnweb-reference-search)
53 (address . "http://www.reference.com/cgi-bin/pn/go"))
55 (article . nnweb-altavista-wash-article)
56 (map . nnweb-altavista-create-mapping)
57 (search . nnweb-altavista-search)
58 (address . "http://www.altavista.digital.com/cgi-bin/query")
59 (id . "/cgi-bin/news?id@%s")))
60 "Type-definition alist.")
62 (defvoo nnweb-search nil
63 "Search string to feed to DejaNews.")
65 (defvoo nnweb-max-hits 100
66 "Maximum number of hits to display.")
68 ;;; Internal variables
70 (defvoo nnweb-articles nil)
71 (defvoo nnweb-buffer nil)
73 ;;; Interface functions
75 (nnoo-define-basics nnweb)
77 (deffoo nnweb-retrieve-headers (articles &optional group server fetch-old)
78 (nnweb-possibly-change-server server)
80 (set-buffer nntp-server-buffer)
83 (while (setq article (pop articles))
84 (when (setq header (cadr (assq article nnweb-articles)))
85 (nnheader-insert-nov header)))
88 (deffoo nnweb-request-group (group &optional server dont-check)
89 (nnweb-possibly-change-server server)
90 (when (or (not dont-check)
92 (funcall (nnweb-definition 'map)))
95 (nnheader-report 'nnweb "No matching articles"))
97 (nnheader-report 'nnweb "Opened group %s" group)
99 "211 %d %d %d %s\n" (length nnweb-articles)
100 (caar nnweb-articles) (caar (last nnweb-articles))
103 (deffoo nnweb-close-group (group &optional server)
104 (nnweb-possibly-change-server server)
105 (when (gnus-buffer-live-p nnweb-buffer)
107 (set-buffer nnweb-buffer)
108 (set-buffer-modified-p nil)
109 (kill-buffer nnweb-buffer)))
112 (deffoo nnweb-request-article (article &optional group server buffer)
113 (nnweb-possibly-change-server server)
115 (set-buffer (or buffer nntp-server-buffer))
116 (let ((url (caddr (assq article nnweb-articles))))
118 (nnweb-fetch-url url))
119 (and (stringp article)
120 (let ((fetch (nnweb-definition 'id))
122 (when (string-match "^<\\(.*\\)>$" article)
123 (setq art (match-string 1 article)))
127 (format fetch article))))))
128 (unless nnheader-callback-function
129 (funcall (nnweb-definition 'article))
130 (nnweb-decode-entities))
131 (nnheader-report 'nnweb "Fetched article %s" article)
134 (deffoo nnweb-close-server (&optional server)
135 (when (and (nnweb-server-opened server)
136 (gnus-buffer-live-p nnweb-buffer))
138 (set-buffer nnweb-buffer)
139 (set-buffer-modified-p nil)
140 (kill-buffer nnweb-buffer)))
141 (nnoo-close-server 'nnweb server))
143 (deffoo nnweb-request-update-info (group info &optional server)
144 (nnweb-possibly-change-server server)
145 (setcar (cddr info) nil))
147 (deffoo nnweb-asynchronous-p ()
150 (nnoo-define-skeleton nnweb)
152 ;;; Internal functions
154 (defun nnweb-definition (type)
155 "Return the definition of TYPE."
156 (let ((def (cdr (assq type (assq nnweb-type nnweb-type-definition)))))
158 (error "Undefined definition %s" type))
161 (defun nnweb-possibly-change-server (&optional server)
164 (unless (nnweb-server-opened server)
165 (nnweb-open-server server))))
167 (defun nnweb-init (server)
168 "Initialize buffers and such."
169 (unless (gnus-buffer-live-p nnweb-buffer)
172 (nnheader-set-temp-buffer
173 (format " *nnweb %s %s %s*" nnweb-type nnweb-search server))))))
175 (defun nnweb-fetch-url (url)
177 (if (not nnheader-callback-function)
178 (let ((buf (current-buffer)))
180 (set-buffer nnweb-buffer)
183 (url-insert-file-contents url)
184 (copy-to-buffer buf (point-min) (point-max)))))
185 (nnweb-url-retrieve-asynch
186 url 'nnweb-callback (current-buffer) nnheader-callback-function)
189 (defun nnweb-callback (buffer callback)
190 (when (gnus-buffer-live-p url-working-buffer)
192 (set-buffer url-working-buffer)
193 (funcall (nnweb-definition 'article))
194 (nnweb-decode-entities)
196 (goto-char (point-max))
197 (insert-buffer-substring url-working-buffer))
199 (gnus-kill-buffer url-working-buffer)))
201 (defun nnweb-url-retrieve-asynch (url callback &rest data)
202 (let ((url-request-method "GET")
203 (old-asynch url-be-asynchronous)
204 (url-request-data nil)
205 (url-request-extra-headers nil)
206 (url-working-buffer (generate-new-buffer-name " *nnweb*")))
207 (setq-default url-be-asynchronous t)
209 (set-buffer (get-buffer-create url-working-buffer))
210 (setq url-current-callback-data data
211 url-be-asynchronous t
212 url-current-callback-func callback)
214 (setq-default url-be-asynchronous old-asynch)))
216 (defun nnweb-encode-www-form-urlencoded (pairs)
217 "Return PAIRS encoded for forms."
221 (concat (w3-form-encode-xwfu (car data)) "="
222 (w3-form-encode-xwfu (cdr data))))) pairs "&"))
224 (defun nnweb-fetch-form (url pairs)
225 (let ((url-request-data (nnweb-encode-www-form-urlencoded pairs))
226 (url-request-method 'POST)
227 (url-request-extra-headers
228 '(("Content-type" . "application/x-www-form-urlencoded"))))
229 (url-insert-file-contents url)
230 (setq buffer-file-name nil))
233 (defun nnweb-decode-entities ()
234 (goto-char (point-min))
235 (while (re-search-forward "&\\([a-z]+\\);" nil t)
236 (replace-match (char-to-string (or (cdr (assq (intern (match-string 1))
241 (defun nnweb-remove-markup ()
242 (goto-char (point-min))
243 (while (search-forward "<!--" nil t)
244 (delete-region (match-beginning 0)
245 (or (search-forward "-->" nil t)
247 (goto-char (point-min))
248 (while (re-search-forward "<[^>]+>" nil t)
249 (replace-match "" t t)))
252 ;;; DejaNews functions.
255 (defun nnweb-dejanews-create-mapping ()
256 "Perform the search and create an number-to-url alist."
258 (set-buffer nnweb-buffer)
260 (when (funcall (nnweb-definition 'search) nnweb-search)
264 Subject Score Date Newsgroup Author
267 ;; Go through all the article hits on this page.
268 (goto-char (point-min))
269 (nnweb-decode-entities)
270 (goto-char (point-min))
271 (while (re-search-forward "^ +[0-9]+\\." nil t)
274 (cond ((re-search-forward "^ +[0-9]+\\." nil t)
276 ((search-forward "\n\n" nil t)
280 (goto-char (point-min))
281 (when (looking-at ".*HREF=\"\\([^\"]+\\)\"")
282 (setq url (match-string 1)))
283 (nnweb-remove-markup)
284 (goto-char (point-min))
285 (while (search-forward "\t" nil t)
287 (goto-char (point-min))
288 (while (re-search-forward "^ +\\([^:]+\\): +\\(.*\\)$" nil t)
289 (set (intern (match-string 1)) (match-string 2)))
291 (when (string-match "#[0-9]+/[0-9]+ *$" Subject)
292 (setq Subject (substring Subject 0 (match-beginning 0))))
296 (make-full-mail-header
297 i (concat "(" Newsgroup ") " Subject) Author Date
298 (concat "<" (message-unique-id) "-" (int-to-string i)
300 nil 0 (string-to-int Score) nil)
303 ;; See whether there is a "Get next 20 hits" button here.
304 (if (or (not (re-search-forward
305 "HREF=\"\\([^\"]+\\)\">Get next" nil t))
306 (>= i nnweb-max-hits))
309 (setq more (match-string 1))
311 (url-insert-file-contents more)))
312 ;; Return the articles in the right order.
313 (setq nnweb-articles (nreverse map))))))
315 (defun nnweb-dejanews-wash-article ()
316 (let ((case-fold-search t))
317 (goto-char (point-min))
318 (re-search-forward "<PRE>" nil t)
319 (delete-region (point-min) (point))
320 (re-search-forward "</PRE>" nil t)
321 (delete-region (point) (point-max))
322 (nnweb-remove-markup)
323 (goto-char (point-min))
324 (while (and (looking-at " *$")
327 (while (looking-at "\\(^[^ ]+:\\) *")
328 (replace-match "\\1 " t)
330 (when (re-search-forward "\n\n+" nil t)
331 (replace-match "\n" t t))))
333 (defun nnweb-dejanews-search (search)
335 (nnweb-definition 'address)
336 `(("query" . ,search)
337 ("defaultOp" . "AND")
338 ("svcclass" . "dncurrent")
340 ("format" . "verbose")
342 ("showsort" . "score")
344 ("ageweight" . "1")))
351 (defun nnweb-reference-create-mapping ()
352 "Perform the search and create an number-to-url alist."
354 (set-buffer nnweb-buffer)
356 (when (funcall (nnweb-definition 'search) nnweb-search)
360 Subject Score Date Newsgroups From Message-ID
363 ;; Go through all the article hits on this page.
364 (goto-char (point-min))
365 (search-forward "</pre><hr>" nil t)
366 (delete-region (point-min) (point))
367 ;(nnweb-decode-entities)
368 (goto-char (point-min))
369 (while (re-search-forward "^ +[0-9]+\\." nil t)
372 (if (re-search-forward "^$" nil t)
375 (goto-char (point-min))
376 (when (looking-at ".*href=\"\\([^\"]+\\)\"")
377 (setq url (match-string 1)))
378 (nnweb-remove-markup)
379 (goto-char (point-min))
380 (while (search-forward "\t" nil t)
382 (goto-char (point-min))
383 (while (re-search-forward "^\\([^:]+\\): \\(.*\\)$" nil t)
384 (set (intern (match-string 1)) (match-string 2)))
386 (search-forward "</pre>" nil t)
390 (make-full-mail-header
391 i (concat "(" Newsgroups ") " Subject) From Date
393 nil 0 (string-to-int Score) nil)
397 ;; Return the articles in the right order.
398 (setq nnweb-articles (nreverse map))))))
400 (defun nnweb-reference-wash-article ()
401 (let ((case-fold-search t))
402 (goto-char (point-min))
403 (re-search-forward "^</center><hr>" nil t)
404 (delete-region (point-min) (point))
405 (search-forward "<pre>" nil t)
407 (let ((body (point-marker)))
408 (search-forward "</pre>" nil t)
409 (delete-region (point) (point-max))
410 (nnweb-remove-markup)
411 (goto-char (point-min))
412 (while (looking-at " *$")
414 (narrow-to-region (point-min) body)
415 (while (and (re-search-forward "^$" nil t)
418 (goto-char (point-min))
419 (while (looking-at "\\(^[^ ]+:\\) *")
420 (replace-match "\\1 " t)
422 (goto-char (point-min))
423 (when (re-search-forward "^References:" nil t)
425 (point) (if (re-search-forward "^$\\|^[^:]+:" nil t)
428 (goto-char (point-min))
430 (unless (looking-at "References")
433 (goto-char (point-min))
434 (while (search-forward "," nil t)
435 (replace-match " " t t)))
437 (set-marker body nil))))
439 (defun nnweb-reference-search (search)
441 (url-insert-file-contents
443 (nnweb-definition 'address)
445 (nnweb-encode-www-form-urlencoded
446 `(("search" . "advanced")
447 ("querytext" . ,search)
452 ("organization" . "")
455 ("choice" . "Search")
456 ("startmonth" . "Jul")
458 ("startyear" . "1996")
463 ("verbosity" . "Verbose")
464 ("ranking" . "Relevance")
468 (setq buffer-file-name nil))
475 (defun nnweb-altavista-create-mapping ()
476 "Perform the search and create an number-to-url alist."
478 (set-buffer nnweb-buffer)
481 (when (funcall (nnweb-definition 'search) nnweb-search part)
485 subject date from id group
488 ;; Go through all the article hits on this page.
489 (goto-char (point-min))
490 (search-forward "<dt>" nil t)
491 (delete-region (point-min) (match-beginning 0))
492 (goto-char (point-min))
493 (while (search-forward "<dt>" nil t)
494 (replace-match "\n<blubb>"))
495 (nnweb-decode-entities)
496 (goto-char (point-min))
497 (while (re-search-forward "<blubb>.*href=\"\\([^\"]+\\)\"><strong>\\([^>]*\\)</strong></a><dd>\\([^-]+\\)- <b>\\([^<]+\\)<.*href=\"news:\\([^\"]+\\)\">.*\">\\(.+\\)</a><P>"
499 (setq url (match-string 1)
500 subject (match-string 2)
501 date (match-string 3)
502 group (match-string 4)
503 id (concat "<" (match-string 5) ">")
504 from (match-string 6))
508 (make-full-mail-header
509 i (concat "(" group ") " subject) from date
513 ;; See if we want more.
514 (when (or (not nnweb-articles)
515 (>= i nnweb-max-hits)
516 (not (funcall (nnweb-definition 'search)
517 nnweb-search (incf part))))
519 ;; Return the articles in the right order.
520 (setq nnweb-articles (nreverse map)))))))
522 (defun nnweb-altavista-wash-article ()
523 (goto-char (point-min))
524 (let ((case-fold-search t))
525 (when (re-search-forward "<H1>\\(.*\\)</H1>" nil t)
526 (setq subject (match-string 1)))
527 (re-search-forward "^<strong>" nil t)
528 (delete-region (point-min) (match-beginning 0))
529 (goto-char (point-min))
530 (while (looking-at "<strong>\\([^ ]+\\) +</strong> +\\(.*\\)$")
531 (replace-match "\\1: \\2" t)
533 (when (re-search-backward "^References:" nil t)
534 (narrow-to-region (point) (progn (forward-line 1) (point)))
535 (goto-char (point-min))
536 (while (re-search-forward "<A.*\\?id@\\([^\"]+\\)\">[0-9]+</A>" nil t)
537 (replace-match "<\\1> " t))
539 (nnweb-remove-markup))))
541 (defun nnweb-altavista-search (search &optional part)
543 (url-insert-file-contents
545 (nnweb-definition 'address)
547 (nnweb-encode-www-form-urlencoded
550 ,@(if part `(("stq" . ,(int-to-string (* part 30)))))
556 (setq buffer-file-name nil))
561 ;;; nnweb.el ends here