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"))))
228 (url-insert-file-contents url)
229 (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")))
350 (defun nnweb-reference-create-mapping ()
351 "Perform the search and create an number-to-url alist."
353 (set-buffer nnweb-buffer)
355 (when (funcall (nnweb-definition 'search) nnweb-search)
359 Subject Score Date Newsgroups From Message-ID
362 ;; Go through all the article hits on this page.
363 (goto-char (point-min))
364 (search-forward "</pre><hr>" nil t)
365 (delete-region (point-min) (point))
366 ;(nnweb-decode-entities)
367 (goto-char (point-min))
368 (while (re-search-forward "^ +[0-9]+\\." nil t)
371 (if (re-search-forward "^$" nil t)
374 (goto-char (point-min))
375 (when (looking-at ".*href=\"\\([^\"]+\\)\"")
376 (setq url (match-string 1)))
377 (nnweb-remove-markup)
378 (goto-char (point-min))
379 (while (search-forward "\t" nil t)
381 (goto-char (point-min))
382 (while (re-search-forward "^\\([^:]+\\): \\(.*\\)$" nil t)
383 (set (intern (match-string 1)) (match-string 2)))
385 (search-forward "</pre>" nil t)
389 (make-full-mail-header
390 i (concat "(" Newsgroups ") " Subject) From Date
392 nil 0 (string-to-int Score) nil)
396 ;; Return the articles in the right order.
397 (setq nnweb-articles (nreverse map))))))
399 (defun nnweb-reference-wash-article ()
400 (let ((case-fold-search t))
401 (goto-char (point-min))
402 (re-search-forward "^</center><hr>" nil t)
403 (delete-region (point-min) (point))
404 (search-forward "<pre>" nil t)
406 (let ((body (point-marker)))
407 (search-forward "</pre>" nil t)
408 (delete-region (point) (point-max))
409 (nnweb-remove-markup)
410 (goto-char (point-min))
411 (while (looking-at " *$")
413 (narrow-to-region (point-min) body)
414 (while (and (re-search-forward "^$" nil t)
417 (goto-char (point-min))
418 (while (looking-at "\\(^[^ ]+:\\) *")
419 (replace-match "\\1 " t)
421 (goto-char (point-min))
422 (when (re-search-forward "^References:" nil t)
424 (point) (if (re-search-forward "^$\\|^[^:]+:" nil t)
427 (goto-char (point-min))
429 (unless (looking-at "References")
432 (goto-char (point-min))
433 (while (search-forward "," nil t)
434 (replace-match " " t t)))
436 (set-marker body nil))))
438 (defun nnweb-reference-search (search)
440 (url-insert-file-contents
442 (nnweb-definition 'address)
444 (nnweb-encode-www-form-urlencoded
445 `(("search" . "advanced")
446 ("querytext" . ,search)
451 ("organization" . "")
454 ("choice" . "Search")
455 ("startmonth" . "Jul")
457 ("startyear" . "1996")
462 ("verbosity" . "Verbose")
463 ("ranking" . "Relevance")
467 (setq buffer-file-name nil))
474 (defun nnweb-altavista-create-mapping ()
475 "Perform the search and create an number-to-url alist."
477 (set-buffer nnweb-buffer)
480 (when (funcall (nnweb-definition 'search) nnweb-search part)
484 subject date from id group
487 ;; Go through all the article hits on this page.
488 (goto-char (point-min))
489 (search-forward "<dt>" nil t)
490 (delete-region (point-min) (match-beginning 0))
491 (goto-char (point-min))
492 (while (search-forward "<dt>" nil t)
493 (replace-match "\n<blubb>"))
494 (nnweb-decode-entities)
495 (goto-char (point-min))
496 (while (re-search-forward "<blubb>.*href=\"\\([^\"]+\\)\"><strong>\\([^>]*\\)</strong></a><dd>\\([^-]+\\)- <b>\\([^<]+\\)<.*href=\"news:\\([^\"]+\\)\">.*\">\\(.+\\)</a><P>"
498 (setq url (match-string 1)
499 subject (match-string 2)
500 date (match-string 3)
501 group (match-string 4)
502 id (concat "<" (match-string 5) ">")
503 from (match-string 6))
507 (make-full-mail-header
508 i (concat "(" group ") " subject) from date
512 ;; See if we want more.
513 (when (or (not nnweb-articles)
514 (>= i nnweb-max-hits)
515 (not (funcall (nnweb-definition 'search)
516 nnweb-search (incf part))))
518 ;; Return the articles in the right order.
519 (setq nnweb-articles (nreverse map)))))))
521 (defun nnweb-altavista-wash-article ()
522 (goto-char (point-min))
523 (let ((case-fold-search t))
524 (when (re-search-forward "<H1>\\(.*\\)</H1>" nil t)
525 (setq subject (match-string 1)))
526 (re-search-forward "^<strong>" nil t)
527 (delete-region (point-min) (match-beginning 0))
528 (goto-char (point-min))
529 (while (looking-at "<strong>\\([^ ]+\\) +</strong> +\\(.*\\)$")
530 (replace-match "\\1: \\2" t)
532 (when (re-search-backward "^References:" nil t)
533 (narrow-to-region (point) (progn (forward-line 1) (point)))
534 (goto-char (point-min))
535 (while (re-search-forward "<A.*\\?id@\\([^\"]+\\)\">[0-9]+</A>" nil t)
536 (replace-match "<\\1> " t))
538 (nnweb-remove-markup))))
540 (defun nnweb-altavista-search (search &optional part)
542 (url-insert-file-contents
544 (nnweb-definition 'address)
546 (nnweb-encode-www-form-urlencoded
549 ,@(if part `(("stq" . ,(int-to-string (* part 30)))))
555 (setq buffer-file-name nil))
560 ;;; nnweb.el ends here