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-request-article (article &optional group server buffer)
103 (nnweb-possibly-change-server server)
105 (set-buffer (or buffer nntp-server-buffer))
106 (let ((url (caddr (assq article nnweb-articles))))
108 (nnweb-fetch-url url))
109 (and (stringp article)
110 (let ((fetch (nnweb-definition 'id))
112 (when (string-match "^<\\(.*\\)>$" article)
113 (setq art (match-string 1 article)))
117 (format fetch article))))))
118 (unless nnheader-callback-function
119 (funcall (nnweb-definition 'article))
120 (nnweb-decode-entities))
121 (nnheader-report 'nnweb "Fetched article %s" article)
124 (deffoo nnweb-close-server (&optional server)
125 (when (nnweb-server-opened server)
126 (gnus-kill-buffer nnweb-buffer))
127 (nnoo-close-server 'nnweb server))
129 (deffoo nnweb-request-update-info (group info &optional server)
130 (nnweb-possibly-change-server server)
131 (setcar (cddr info) nil))
133 (deffoo nnweb-asynchronous-p ()
136 (nnoo-define-skeleton nnweb)
138 ;;; Internal functions
140 (defun nnweb-definition (type)
141 "Return the definition of TYPE."
142 (let ((def (cdr (assq type (assq nnweb-type nnweb-type-definition)))))
144 (error "Undefined definition %s" type))
147 (defun nnweb-possibly-change-server (&optional server)
150 (unless (nnweb-server-opened server)
151 (nnweb-open-server server))))
153 (defun nnweb-init (server)
154 "Initialize buffers and such."
155 (unless (gnus-buffer-live-p nnweb-buffer)
158 (nnheader-set-temp-buffer
159 (format " *nnweb %s %s %s*" nnweb-type nnweb-search server))))))
161 (defun nnweb-fetch-url (url)
163 (if (not nnheader-callback-function)
164 (let ((buf (current-buffer)))
166 (set-buffer nnweb-buffer)
169 (url-insert-file-contents url)
170 (copy-to-buffer buf (point-min) (point-max)))))
171 (nnweb-url-retrieve-asynch
172 url 'nnweb-callback (current-buffer) nnheader-callback-function)
175 (defun nnweb-callback (buffer callback)
177 (set-buffer url-working-buffer)
178 (funcall (nnweb-definition 'article))
179 (nnweb-decode-entities)
181 (goto-char (point-max))
182 (insert-buffer-substring url-working-buffer))
184 (gnus-kill-buffer url-working-buffer))
186 (defun nnweb-url-retrieve-asynch (url callback &rest data)
187 (let ((url-request-method "GET")
188 (old-asynch url-be-asynchronous)
189 (url-request-data nil)
190 (url-request-extra-headers nil)
191 (url-working-buffer (generate-new-buffer-name " *nnweb*")))
192 (setq-default url-be-asynchronous t)
194 (set-buffer (get-buffer-create url-working-buffer))
195 (setq url-current-callback-data data
196 url-be-asynchronous t
197 url-current-callback-func callback)
199 (setq-default url-be-asynchronous old-asynch)))
201 (defun nnweb-encode-www-form-urlencoded (pairs)
202 "Return PAIRS encoded for forms."
206 (concat (w3-form-encode-xwfu (car data)) "="
207 (w3-form-encode-xwfu (cdr data))))) pairs "&"))
209 (defun nnweb-fetch-form (url pairs)
210 (let ((url-request-data (nnweb-encode-www-form-urlencoded pairs))
211 (url-request-method 'POST)
212 (url-request-extra-headers
213 '(("Content-type" . "application/x-www-form-urlencoded"))))
215 (url-insert-file-contents url)
216 (setq buffer-file-name nil))))
218 (defun nnweb-decode-entities ()
219 (goto-char (point-min))
220 (while (re-search-forward "&\\([a-z]+\\);" nil t)
221 (replace-match (char-to-string (or (cdr (assq (intern (match-string 1))
226 (defun nnweb-remove-markup ()
227 (goto-char (point-min))
228 (while (search-forward "<!--" nil t)
229 (delete-region (match-beginning 0)
230 (or (search-forward "-->" nil t)
232 (goto-char (point-min))
233 (while (re-search-forward "<[^>]+>" nil t)
234 (replace-match "" t t)))
237 ;;; DejaNews functions.
240 (defun nnweb-dejanews-create-mapping ()
241 "Perform the search and create an number-to-url alist."
243 (set-buffer nnweb-buffer)
245 (when (funcall (nnweb-definition 'search) nnweb-search)
248 Subject Score Date Newsgroup Author
251 ;; Go through all the article hits on this page.
252 (goto-char (point-min))
253 (nnweb-decode-entities)
254 (goto-char (point-min))
255 (while (re-search-forward "^ +[0-9]+\\." nil t)
258 (if (re-search-forward "^ +[0-9]+\\." nil t)
261 (goto-char (point-min))
262 (when (looking-at ".*HREF=\"\\([^\"]+\\)\"")
263 (setq url (match-string 1)))
264 (nnweb-remove-markup)
265 (goto-char (point-min))
266 (while (search-forward "\t" nil t)
268 (goto-char (point-min))
269 (while (re-search-forward "^ +\\([^:]+\\): +\\(.*\\)$" nil t)
270 (set (intern (match-string 1)) (match-string 2)))
272 (when (string-match "#[0-9]+/[0-9]+ *$" Subject)
273 (setq Subject (substring Subject 0 (match-beginning 0))))
277 (make-full-mail-header
278 i (concat "(" Newsgroup ") " Subject) Author Date
279 (concat "<" (message-unique-id) "-" (int-to-string i)
281 nil 0 (string-to-int Score) nil)
284 ;; See whether there is a "Get next 20 hits" button here.
285 (if (or (not (re-search-forward
286 "HREF=\"\\([^\"]+\\)\">Get next" nil t))
287 (> i nnweb-max-hits))
290 (setq more (match-string 1))
292 (url-insert-file-contents more)))
293 ;; Return the articles in the right order.
294 (setq nnweb-articles (nreverse map))))))
296 (defun nnweb-dejanews-wash-article ()
297 (goto-char (point-min))
298 (re-search-forward "<PRE>" nil t)
299 (delete-region (point-min) (point))
300 (re-search-forward "</PRE>" nil t)
301 (delete-region (point) (point-max))
302 (nnweb-remove-markup)
303 (goto-char (point-min))
304 (while (and (looking-at " *$")
307 (while (looking-at "\\(^[^ ]+:\\) *")
308 (replace-match "\\1 " t)
310 (when (re-search-forward "\n\n+" nil t)
311 (replace-match "\n" t t)))
313 (defun nnweb-dejanews-search (search)
315 (nnweb-definition 'address)
316 `(("query" . ,search)
317 ("defaultOp" . "AND")
318 ("svcclass" . "dncurrent")
320 ("format" . "verbose")
322 ("showsort" . "score")
324 ("ageweight" . "1"))))
330 (defun nnweb-reference-create-mapping ()
331 "Perform the search and create an number-to-url alist."
333 (set-buffer nnweb-buffer)
335 (when (funcall (nnweb-definition 'search) nnweb-search)
338 Subject Score Date Newsgroups From Message-ID
341 ;; Go through all the article hits on this page.
342 (goto-char (point-min))
343 (search-forward "</pre><hr>" nil t)
344 (delete-region (point-min) (point))
345 ;(nnweb-decode-entities)
346 (goto-char (point-min))
347 (while (re-search-forward "^ +[0-9]+\\." nil t)
350 (if (re-search-forward "^$" nil t)
353 (goto-char (point-min))
354 (when (looking-at ".*href=\"\\([^\"]+\\)\"")
355 (setq url (match-string 1)))
356 (nnweb-remove-markup)
357 (goto-char (point-min))
358 (while (search-forward "\t" nil t)
360 (goto-char (point-min))
361 (while (re-search-forward "^\\([^:]+\\): \\(.*\\)$" nil t)
362 (set (intern (match-string 1)) (match-string 2)))
364 (search-forward "</pre>" nil t)
368 (make-full-mail-header
369 i (concat "(" Newsgroups ") " Subject) From Date
371 nil 0 (string-to-int Score) nil)
375 ;; Return the articles in the right order.
376 (setq nnweb-articles (nreverse map))))))
378 (defun nnweb-reference-wash-article ()
379 (goto-char (point-min))
380 (re-search-forward "^</center><hr>" nil t)
381 (delete-region (point-min) (point))
382 (search-forward "<pre>" nil t)
384 (let ((body (point-marker)))
385 (search-forward "</pre>" nil t)
386 (delete-region (point) (point-max))
387 (nnweb-remove-markup)
388 (goto-char (point-min))
389 (while (looking-at " *$")
391 (narrow-to-region (point-min) body)
392 (while (and (re-search-forward "^$" nil t)
395 (goto-char (point-min))
396 (while (looking-at "\\(^[^ ]+:\\) *")
397 (replace-match "\\1 " t)
399 (goto-char (point-min))
400 (when (re-search-forward "^References:" nil t)
402 (point) (if (re-search-forward "^$\\|^[^:]+:" nil t)
405 (goto-char (point-min))
407 (unless (looking-at "References")
410 (goto-char (point-min))
411 (while (search-forward "," nil t)
412 (replace-match " " t t)))
414 (set-marker body nil)))
416 (defun nnweb-reference-search (search)
417 (url-insert-file-contents
419 (nnweb-definition 'address)
421 (nnweb-encode-www-form-urlencoded
422 `(("search" . "advanced")
423 ("querytext" . ,search)
428 ("organization" . "")
431 ("choice" . "Search")
432 ("startmonth" . "Jul")
434 ("startyear" . "1996")
439 ("verbosity" . "Verbose")
440 ("ranking" . "Relevance")
443 ("score" . "50"))))))
449 (defun nnweb-altavista-create-mapping ()
450 "Perform the search and create an number-to-url alist."
452 (set-buffer nnweb-buffer)
454 (when (funcall (nnweb-definition 'search) nnweb-search)
457 Subject Score Date Newsgroups From Message-ID
460 ;; Go through all the article hits on this page.
461 (goto-char (point-min))
462 (search-forward "<dt>" nil t)
463 (delete-region (point-min) (match-beginning 0))
464 (goto-char (point-min))
465 (while (search-forward "<dt>" nil t)
466 (replace-match "\n<blubb>"))
467 (nnweb-decode-entities)
468 (goto-char (point-min))
469 (while (re-search-forward "<blubb>.*href=\"\\([^\"]+\\)\"><strong>\\([^>]*\\)</strong></a><dd>\\([^-]+\\)- <b>\\([^<]+\\)<.*href=\"news:\\([^\"]+\\)\">.*\">\\(.+\\)</a><P>"
471 (setq url (match-string 1)
472 subject (match-string 2)
473 date (match-string 3)
474 group (match-string 4)
475 id (concat "<" (match-string 5) ">")
476 from (match-string 6))
480 (make-full-mail-header
481 i (concat "(" group ") " subject) from date
486 ;; Return the articles in the right order.
487 (setq nnweb-articles (nreverse map))))))
489 (defun nnweb-altavista-wash-article ()
490 (goto-char (point-min))
492 (when (re-search-forward "<H1>\\(.*\\)</H1>" nil t)
493 (setq subject (match-string 1)))
494 (re-search-forward "^<strong>" nil t)
495 (delete-region (point-min) (match-beginning 0))
496 (goto-char (point-min))
497 (while (looking-at "<strong>\\([^ ]+\\) +</strong> +\\(.*\\)$")
498 (replace-match "\\1: \\2" t)
500 (when (re-search-backward "^References:" nil t)
501 (narrow-to-region (point) (progn (forward-line 1) (point)))
502 (goto-char (point-min))
503 (while (re-search-forward "<A.*\\?id@\\([^\"]+\\)\">[0-9]+</A>" nil t)
504 (replace-match "<\\1> " t))
506 (nnweb-remove-markup))))
508 (defun nnweb-altavista-search (search)
509 (url-insert-file-contents
511 (nnweb-definition 'address)
513 (nnweb-encode-www-form-urlencoded
524 ;;; nnweb.el ends here