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-type 'dejanews
40 "What search engine type is being used.")
42 (defvar nnweb-type-definition
44 (article . nnweb-dejanews-wash-article)
45 (map . nnweb-dejanews-create-mapping)
46 (search . nnweb-dejanews-search)
47 (address . "http://search.dejanews.com/dnquery.xp"))
49 (article . nnweb-reference-wash-article)
50 (map . nnweb-reference-create-mapping)
51 (search . nnweb-reference-search)
52 (address . "http://www.reference.com/cgi-bin/pn/go"))
54 (article . nnweb-altavista-wash-article)
55 (map . nnweb-altavista-create-mapping)
56 (search . nnweb-altavista-search)
57 (address . "http://www.altavista.digital.com/cgi-bin/query")
58 (id . "/cgi-bin/news?id@%s")))
59 "Type-definition alist.")
61 (defvoo nnweb-search nil
62 "Search string to feed to DejaNews.")
64 (defvoo nnweb-max-hits 100
65 "Maximum number of hits to display.")
67 ;;; Internal variables
69 (defvoo nnweb-articles nil)
70 (defvoo nnweb-buffer nil)
72 ;;; Interface functions
74 (nnoo-define-basics nnweb)
76 (deffoo nnweb-retrieve-headers (articles &optional group server fetch-old)
77 (nnweb-possibly-change-server server)
79 (set-buffer nntp-server-buffer)
82 (while (setq article (pop articles))
83 (when (setq header (cadr (assq article nnweb-articles)))
84 (nnheader-insert-nov header)))
87 (deffoo nnweb-request-group (group &optional server dont-check)
88 (nnweb-possibly-change-server server)
89 (when (or (not dont-check)
91 (funcall (nnweb-definition 'map)))
94 (nnheader-report 'nnweb "No matching articles"))
96 (nnheader-report 'nnweb "Opened group %s" group)
98 "211 %d %d %d %s\n" (length nnweb-articles)
99 (caar nnweb-articles) (caar (last nnweb-articles))
102 (deffoo nnweb-close-group (group &optional server)
103 (nnweb-possibly-change-server server)
104 (when (gnus-buffer-live-p nnweb-buffer)
106 (set-buffer nnweb-buffer)
107 (set-buffer-modified-p nil)
108 (kill-buffer nnweb-buffer)))
111 (deffoo nnweb-request-article (article &optional group server buffer)
112 (nnweb-possibly-change-server server)
114 (set-buffer (or buffer nntp-server-buffer))
115 (let ((url (caddr (assq article nnweb-articles))))
117 (nnweb-fetch-url url))
118 (and (stringp article)
119 (let ((fetch (nnweb-definition 'id))
121 (when (string-match "^<\\(.*\\)>$" article)
122 (setq art (match-string 1 article)))
126 (format fetch article))))))
127 (unless nnheader-callback-function
128 (funcall (nnweb-definition 'article))
129 (nnweb-decode-entities))
130 (nnheader-report 'nnweb "Fetched article %s" article)
133 (deffoo nnweb-close-server (&optional server)
134 (when (and (nnweb-server-opened server)
135 (gnus-buffer-live-p nnweb-buffer))
137 (set-buffer nnweb-buffer)
138 (set-buffer-modified-p nil)
139 (kill-buffer nnweb-buffer)))
140 (nnoo-close-server 'nnweb server))
142 (deffoo nnweb-request-update-info (group info &optional server)
143 (nnweb-possibly-change-server server)
144 (setcar (cddr info) nil))
146 (deffoo nnweb-asynchronous-p ()
149 (nnoo-define-skeleton nnweb)
151 ;;; Internal functions
153 (defun nnweb-definition (type)
154 "Return the definition of TYPE."
155 (let ((def (cdr (assq type (assq nnweb-type nnweb-type-definition)))))
157 (error "Undefined definition %s" type))
160 (defun nnweb-possibly-change-server (&optional server)
163 (unless (nnweb-server-opened server)
164 (nnweb-open-server server))))
166 (defun nnweb-init (server)
167 "Initialize buffers and such."
168 (unless (gnus-buffer-live-p nnweb-buffer)
171 (nnheader-set-temp-buffer
172 (format " *nnweb %s %s %s*" nnweb-type nnweb-search server))))))
174 (defun nnweb-fetch-url (url)
176 (if (not nnheader-callback-function)
177 (let ((buf (current-buffer)))
179 (set-buffer nnweb-buffer)
182 (url-insert-file-contents url)
183 (copy-to-buffer buf (point-min) (point-max)))))
184 (nnweb-url-retrieve-asynch
185 url 'nnweb-callback (current-buffer) nnheader-callback-function)
188 (defun nnweb-callback (buffer callback)
189 (when (gnus-buffer-live-p url-working-buffer)
191 (set-buffer url-working-buffer)
192 (funcall (nnweb-definition 'article))
193 (nnweb-decode-entities)
195 (goto-char (point-max))
196 (insert-buffer-substring url-working-buffer))
198 (gnus-kill-buffer url-working-buffer)))
200 (defun nnweb-url-retrieve-asynch (url callback &rest data)
201 (let ((url-request-method "GET")
202 (old-asynch url-be-asynchronous)
203 (url-request-data nil)
204 (url-request-extra-headers nil)
205 (url-working-buffer (generate-new-buffer-name " *nnweb*")))
206 (setq-default url-be-asynchronous t)
208 (set-buffer (get-buffer-create url-working-buffer))
209 (setq url-current-callback-data data
210 url-be-asynchronous t
211 url-current-callback-func callback)
213 (setq-default url-be-asynchronous old-asynch)))
215 (defun nnweb-encode-www-form-urlencoded (pairs)
216 "Return PAIRS encoded for forms."
220 (concat (w3-form-encode-xwfu (car data)) "="
221 (w3-form-encode-xwfu (cdr data))))) pairs "&"))
223 (defun nnweb-fetch-form (url pairs)
224 (let ((url-request-data (nnweb-encode-www-form-urlencoded pairs))
225 (url-request-method 'POST)
226 (url-request-extra-headers
227 '(("Content-type" . "application/x-www-form-urlencoded"))))
229 (url-insert-file-contents url)
230 (setq buffer-file-name nil))))
232 (defun nnweb-decode-entities ()
233 (goto-char (point-min))
234 (while (re-search-forward "&\\([a-z]+\\);" nil t)
235 (replace-match (char-to-string (or (cdr (assq (intern (match-string 1))
240 (defun nnweb-remove-markup ()
241 (goto-char (point-min))
242 (while (search-forward "<!--" nil t)
243 (delete-region (match-beginning 0)
244 (or (search-forward "-->" nil t)
246 (goto-char (point-min))
247 (while (re-search-forward "<[^>]+>" nil t)
248 (replace-match "" t t)))
251 ;;; DejaNews functions.
254 (defun nnweb-dejanews-create-mapping ()
255 "Perform the search and create an number-to-url alist."
257 (set-buffer nnweb-buffer)
259 (when (funcall (nnweb-definition 'search) nnweb-search)
263 Subject Score Date Newsgroup Author
266 ;; Go through all the article hits on this page.
267 (goto-char (point-min))
268 (nnweb-decode-entities)
269 (goto-char (point-min))
270 (while (re-search-forward "^ +[0-9]+\\." nil t)
273 (cond ((re-search-forward "^ +[0-9]+\\." nil t)
275 ((search-forward "\n\n" nil t)
279 (goto-char (point-min))
280 (when (looking-at ".*HREF=\"\\([^\"]+\\)\"")
281 (setq url (match-string 1)))
282 (nnweb-remove-markup)
283 (goto-char (point-min))
284 (while (search-forward "\t" nil t)
286 (goto-char (point-min))
287 (while (re-search-forward "^ +\\([^:]+\\): +\\(.*\\)$" nil t)
288 (set (intern (match-string 1)) (match-string 2)))
290 (when (string-match "#[0-9]+/[0-9]+ *$" Subject)
291 (setq Subject (substring Subject 0 (match-beginning 0))))
295 (make-full-mail-header
296 i (concat "(" Newsgroup ") " Subject) Author Date
297 (concat "<" (message-unique-id) "-" (int-to-string i)
299 nil 0 (string-to-int Score) nil)
302 ;; See whether there is a "Get next 20 hits" button here.
303 (if (or (not (re-search-forward
304 "HREF=\"\\([^\"]+\\)\">Get next" nil t))
305 (>= i nnweb-max-hits))
308 (setq more (match-string 1))
310 (url-insert-file-contents more)))
311 ;; Return the articles in the right order.
312 (setq nnweb-articles (nreverse map))))))
314 (defun nnweb-dejanews-wash-article ()
315 (let ((case-fold-search t))
316 (goto-char (point-min))
317 (re-search-forward "<PRE>" nil t)
318 (delete-region (point-min) (point))
319 (re-search-forward "</PRE>" nil t)
320 (delete-region (point) (point-max))
321 (nnweb-remove-markup)
322 (goto-char (point-min))
323 (while (and (looking-at " *$")
326 (while (looking-at "\\(^[^ ]+:\\) *")
327 (replace-match "\\1 " t)
329 (when (re-search-forward "\n\n+" nil t)
330 (replace-match "\n" t t))))
332 (defun nnweb-dejanews-search (search)
334 (nnweb-definition 'address)
335 `(("query" . ,search)
336 ("defaultOp" . "AND")
337 ("svcclass" . "dncurrent")
339 ("format" . "verbose")
341 ("showsort" . "score")
343 ("ageweight" . "1"))))
349 (defun nnweb-reference-create-mapping ()
350 "Perform the search and create an number-to-url alist."
352 (set-buffer nnweb-buffer)
354 (when (funcall (nnweb-definition 'search) nnweb-search)
358 Subject Score Date Newsgroups From Message-ID
361 ;; Go through all the article hits on this page.
362 (goto-char (point-min))
363 (search-forward "</pre><hr>" nil t)
364 (delete-region (point-min) (point))
365 ;(nnweb-decode-entities)
366 (goto-char (point-min))
367 (while (re-search-forward "^ +[0-9]+\\." nil t)
370 (if (re-search-forward "^$" nil t)
373 (goto-char (point-min))
374 (when (looking-at ".*href=\"\\([^\"]+\\)\"")
375 (setq url (match-string 1)))
376 (nnweb-remove-markup)
377 (goto-char (point-min))
378 (while (search-forward "\t" nil t)
380 (goto-char (point-min))
381 (while (re-search-forward "^\\([^:]+\\): \\(.*\\)$" nil t)
382 (set (intern (match-string 1)) (match-string 2)))
384 (search-forward "</pre>" nil t)
388 (make-full-mail-header
389 i (concat "(" Newsgroups ") " Subject) From Date
391 nil 0 (string-to-int Score) nil)
395 ;; Return the articles in the right order.
396 (setq nnweb-articles (nreverse map))))))
398 (defun nnweb-reference-wash-article ()
399 (let ((case-fold-search t))
400 (goto-char (point-min))
401 (re-search-forward "^</center><hr>" nil t)
402 (delete-region (point-min) (point))
403 (search-forward "<pre>" nil t)
405 (let ((body (point-marker)))
406 (search-forward "</pre>" nil t)
407 (delete-region (point) (point-max))
408 (nnweb-remove-markup)
409 (goto-char (point-min))
410 (while (looking-at " *$")
412 (narrow-to-region (point-min) body)
413 (while (and (re-search-forward "^$" nil t)
416 (goto-char (point-min))
417 (while (looking-at "\\(^[^ ]+:\\) *")
418 (replace-match "\\1 " t)
420 (goto-char (point-min))
421 (when (re-search-forward "^References:" nil t)
423 (point) (if (re-search-forward "^$\\|^[^:]+:" nil t)
426 (goto-char (point-min))
428 (unless (looking-at "References")
431 (goto-char (point-min))
432 (while (search-forward "," nil t)
433 (replace-match " " t t)))
435 (set-marker body nil))))
437 (defun nnweb-reference-search (search)
438 (url-insert-file-contents
440 (nnweb-definition 'address)
442 (nnweb-encode-www-form-urlencoded
443 `(("search" . "advanced")
444 ("querytext" . ,search)
449 ("organization" . "")
452 ("choice" . "Search")
453 ("startmonth" . "Jul")
455 ("startyear" . "1996")
460 ("verbosity" . "Verbose")
461 ("ranking" . "Relevance")
464 ("score" . "50"))))))
470 (defun nnweb-altavista-create-mapping ()
471 "Perform the search and create an number-to-url alist."
473 (set-buffer nnweb-buffer)
476 (when (funcall (nnweb-definition 'search) nnweb-search part)
480 subject date from id group
483 ;; Go through all the article hits on this page.
484 (goto-char (point-min))
485 (search-forward "<dt>" nil t)
486 (delete-region (point-min) (match-beginning 0))
487 (goto-char (point-min))
488 (while (search-forward "<dt>" nil t)
489 (replace-match "\n<blubb>"))
490 (nnweb-decode-entities)
491 (goto-char (point-min))
492 (while (re-search-forward "<blubb>.*href=\"\\([^\"]+\\)\"><strong>\\([^>]*\\)</strong></a><dd>\\([^-]+\\)- <b>\\([^<]+\\)<.*href=\"news:\\([^\"]+\\)\">.*\">\\(.+\\)</a><P>"
494 (setq url (match-string 1)
495 subject (match-string 2)
496 date (match-string 3)
497 group (match-string 4)
498 id (concat "<" (match-string 5) ">")
499 from (match-string 6))
503 (make-full-mail-header
504 i (concat "(" group ") " subject) from date
508 ;; See if we want more.
509 (when (or (>= i nnweb-max-hits)
510 (not (funcall (nnweb-definition 'search)
511 nnweb-search (incf part))))
513 ;; Return the articles in the right order.
514 (setq nnweb-articles (nreverse map)))))))
516 (defun nnweb-altavista-wash-article ()
517 (goto-char (point-min))
518 (let ((case-fold-search t))
519 (when (re-search-forward "<H1>\\(.*\\)</H1>" nil t)
520 (setq subject (match-string 1)))
521 (re-search-forward "^<strong>" nil t)
522 (delete-region (point-min) (match-beginning 0))
523 (goto-char (point-min))
524 (while (looking-at "<strong>\\([^ ]+\\) +</strong> +\\(.*\\)$")
525 (replace-match "\\1: \\2" t)
527 (when (re-search-backward "^References:" nil t)
528 (narrow-to-region (point) (progn (forward-line 1) (point)))
529 (goto-char (point-min))
530 (while (re-search-forward "<A.*\\?id@\\([^\"]+\\)\">[0-9]+</A>" nil t)
531 (replace-match "<\\1> " t))
533 (nnweb-remove-markup))))
535 (defun nnweb-altavista-search (search &optional part)
536 (url-insert-file-contents
538 (nnweb-definition 'address)
540 (nnweb-encode-www-form-urlencoded
543 ,@(if part `(("stq" . ,(int-to-string (* part 30)))))
552 ;;; nnweb.el ends here