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 (gnus-kill-buffer nnweb-buffer)
107 (deffoo nnweb-request-article (article &optional group server buffer)
108 (nnweb-possibly-change-server server)
110 (set-buffer (or buffer nntp-server-buffer))
111 (let ((url (caddr (assq article nnweb-articles))))
113 (nnweb-fetch-url url))
114 (and (stringp article)
115 (let ((fetch (nnweb-definition 'id))
117 (when (string-match "^<\\(.*\\)>$" article)
118 (setq art (match-string 1 article)))
122 (format fetch article))))))
123 (unless nnheader-callback-function
124 (funcall (nnweb-definition 'article))
125 (nnweb-decode-entities))
126 (nnheader-report 'nnweb "Fetched article %s" article)
129 (deffoo nnweb-close-server (&optional server)
130 (when (nnweb-server-opened server)
131 (gnus-kill-buffer nnweb-buffer))
132 (nnoo-close-server 'nnweb server))
134 (deffoo nnweb-request-update-info (group info &optional server)
135 (nnweb-possibly-change-server server)
136 (setcar (cddr info) nil))
138 (deffoo nnweb-asynchronous-p ()
141 (nnoo-define-skeleton nnweb)
143 ;;; Internal functions
145 (defun nnweb-definition (type)
146 "Return the definition of TYPE."
147 (let ((def (cdr (assq type (assq nnweb-type nnweb-type-definition)))))
149 (error "Undefined definition %s" type))
152 (defun nnweb-possibly-change-server (&optional server)
155 (unless (nnweb-server-opened server)
156 (nnweb-open-server server))))
158 (defun nnweb-init (server)
159 "Initialize buffers and such."
160 (unless (gnus-buffer-live-p nnweb-buffer)
163 (nnheader-set-temp-buffer
164 (format " *nnweb %s %s %s*" nnweb-type nnweb-search server))))))
166 (defun nnweb-fetch-url (url)
168 (if (not nnheader-callback-function)
169 (let ((buf (current-buffer)))
171 (set-buffer nnweb-buffer)
174 (url-insert-file-contents url)
175 (copy-to-buffer buf (point-min) (point-max)))))
176 (nnweb-url-retrieve-asynch
177 url 'nnweb-callback (current-buffer) nnheader-callback-function)
180 (defun nnweb-callback (buffer callback)
181 (when (gnus-buffer-live-p url-working-buffer)
183 (set-buffer url-working-buffer)
184 (funcall (nnweb-definition 'article))
185 (nnweb-decode-entities)
187 (goto-char (point-max))
188 (insert-buffer-substring url-working-buffer))
190 (gnus-kill-buffer url-working-buffer)))
192 (defun nnweb-url-retrieve-asynch (url callback &rest data)
193 (let ((url-request-method "GET")
194 (old-asynch url-be-asynchronous)
195 (url-request-data nil)
196 (url-request-extra-headers nil)
197 (url-working-buffer (generate-new-buffer-name " *nnweb*")))
198 (setq-default url-be-asynchronous t)
200 (set-buffer (get-buffer-create url-working-buffer))
201 (setq url-current-callback-data data
202 url-be-asynchronous t
203 url-current-callback-func callback)
205 (setq-default url-be-asynchronous old-asynch)))
207 (defun nnweb-encode-www-form-urlencoded (pairs)
208 "Return PAIRS encoded for forms."
212 (concat (w3-form-encode-xwfu (car data)) "="
213 (w3-form-encode-xwfu (cdr data))))) pairs "&"))
215 (defun nnweb-fetch-form (url pairs)
216 (let ((url-request-data (nnweb-encode-www-form-urlencoded pairs))
217 (url-request-method 'POST)
218 (url-request-extra-headers
219 '(("Content-type" . "application/x-www-form-urlencoded"))))
221 (url-insert-file-contents url)
222 (setq buffer-file-name nil))))
224 (defun nnweb-decode-entities ()
225 (goto-char (point-min))
226 (while (re-search-forward "&\\([a-z]+\\);" nil t)
227 (replace-match (char-to-string (or (cdr (assq (intern (match-string 1))
232 (defun nnweb-remove-markup ()
233 (goto-char (point-min))
234 (while (search-forward "<!--" nil t)
235 (delete-region (match-beginning 0)
236 (or (search-forward "-->" nil t)
238 (goto-char (point-min))
239 (while (re-search-forward "<[^>]+>" nil t)
240 (replace-match "" t t)))
243 ;;; DejaNews functions.
246 (defun nnweb-dejanews-create-mapping ()
247 "Perform the search and create an number-to-url alist."
249 ;(set-buffer nnweb-buffer)
251 (when (or t (funcall (nnweb-definition 'search) nnweb-search))
255 Subject Score Date Newsgroup Author
258 ;; Go through all the article hits on this page.
259 (goto-char (point-min))
260 (nnweb-decode-entities)
261 (goto-char (point-min))
262 (while (re-search-forward "^ +[0-9]+\\." nil t)
265 (if (re-search-forward "^ +[0-9]+\\." nil t)
268 (goto-char (point-min))
269 (when (looking-at ".*HREF=\"\\([^\"]+\\)\"")
270 (setq url (match-string 1)))
271 (nnweb-remove-markup)
272 (goto-char (point-min))
273 (while (search-forward "\t" nil t)
275 (goto-char (point-min))
276 (while (re-search-forward "^ +\\([^:]+\\): +\\(.*\\)$" nil t)
277 (set (intern (match-string 1)) (match-string 2)))
279 (when (string-match "#[0-9]+/[0-9]+ *$" Subject)
280 (setq Subject (substring Subject 0 (match-beginning 0))))
284 (make-full-mail-header
285 i (concat "(" Newsgroup ") " Subject) Author Date
286 (concat "<" (message-unique-id) "-" (int-to-string i)
288 nil 0 (string-to-int Score) nil)
291 ;; See whether there is a "Get next 20 hits" button here.
292 (if (or (not (re-search-forward
293 "HREF=\"\\([^\"]+\\)\">Get next" nil t))
294 (> i nnweb-max-hits))
297 (setq more (match-string 1))
299 (url-insert-file-contents more)))
300 ;; Return the articles in the right order.
301 (setq nnweb-articles (nreverse map))))))
303 (defun nnweb-dejanews-wash-article ()
304 (let ((case-fold-search t))
305 (goto-char (point-min))
306 (re-search-forward "<PRE>" nil t)
307 (delete-region (point-min) (point))
308 (re-search-forward "</PRE>" nil t)
309 (delete-region (point) (point-max))
310 (nnweb-remove-markup)
311 (goto-char (point-min))
312 (while (and (looking-at " *$")
315 (while (looking-at "\\(^[^ ]+:\\) *")
316 (replace-match "\\1 " t)
318 (when (re-search-forward "\n\n+" nil t)
319 (replace-match "\n" t t))))
321 (defun nnweb-dejanews-search (search)
323 (nnweb-definition 'address)
324 `(("query" . ,search)
325 ("defaultOp" . "AND")
326 ("svcclass" . "dncurrent")
328 ("format" . "verbose")
330 ("showsort" . "score")
332 ("ageweight" . "1"))))
338 (defun nnweb-reference-create-mapping ()
339 "Perform the search and create an number-to-url alist."
341 (set-buffer nnweb-buffer)
343 (when (funcall (nnweb-definition 'search) nnweb-search)
347 Subject Score Date Newsgroups From Message-ID
350 ;; Go through all the article hits on this page.
351 (goto-char (point-min))
352 (search-forward "</pre><hr>" nil t)
353 (delete-region (point-min) (point))
354 ;(nnweb-decode-entities)
355 (goto-char (point-min))
356 (while (re-search-forward "^ +[0-9]+\\." nil t)
359 (if (re-search-forward "^$" nil t)
362 (goto-char (point-min))
363 (when (looking-at ".*href=\"\\([^\"]+\\)\"")
364 (setq url (match-string 1)))
365 (nnweb-remove-markup)
366 (goto-char (point-min))
367 (while (search-forward "\t" nil t)
369 (goto-char (point-min))
370 (while (re-search-forward "^\\([^:]+\\): \\(.*\\)$" nil t)
371 (set (intern (match-string 1)) (match-string 2)))
373 (search-forward "</pre>" nil t)
377 (make-full-mail-header
378 i (concat "(" Newsgroups ") " Subject) From Date
380 nil 0 (string-to-int Score) nil)
384 ;; Return the articles in the right order.
385 (setq nnweb-articles (nreverse map))))))
387 (defun nnweb-reference-wash-article ()
388 (let ((case-fold-search t))
389 (goto-char (point-min))
390 (re-search-forward "^</center><hr>" nil t)
391 (delete-region (point-min) (point))
392 (search-forward "<pre>" nil t)
394 (let ((body (point-marker)))
395 (search-forward "</pre>" nil t)
396 (delete-region (point) (point-max))
397 (nnweb-remove-markup)
398 (goto-char (point-min))
399 (while (looking-at " *$")
401 (narrow-to-region (point-min) body)
402 (while (and (re-search-forward "^$" nil t)
405 (goto-char (point-min))
406 (while (looking-at "\\(^[^ ]+:\\) *")
407 (replace-match "\\1 " t)
409 (goto-char (point-min))
410 (when (re-search-forward "^References:" nil t)
412 (point) (if (re-search-forward "^$\\|^[^:]+:" nil t)
415 (goto-char (point-min))
417 (unless (looking-at "References")
420 (goto-char (point-min))
421 (while (search-forward "," nil t)
422 (replace-match " " t t)))
424 (set-marker body nil))))
426 (defun nnweb-reference-search (search)
427 (url-insert-file-contents
429 (nnweb-definition 'address)
431 (nnweb-encode-www-form-urlencoded
432 `(("search" . "advanced")
433 ("querytext" . ,search)
438 ("organization" . "")
441 ("choice" . "Search")
442 ("startmonth" . "Jul")
444 ("startyear" . "1996")
449 ("verbosity" . "Verbose")
450 ("ranking" . "Relevance")
453 ("score" . "50"))))))
459 (defun nnweb-altavista-create-mapping ()
460 "Perform the search and create an number-to-url alist."
462 (set-buffer nnweb-buffer)
464 (when (funcall (nnweb-definition 'search) nnweb-search)
468 subject score date newsgroups from id
471 ;; Go through all the article hits on this page.
472 (goto-char (point-min))
473 (search-forward "<dt>" nil t)
474 (delete-region (point-min) (match-beginning 0))
475 (goto-char (point-min))
476 (while (search-forward "<dt>" nil t)
477 (replace-match "\n<blubb>"))
478 (nnweb-decode-entities)
479 (goto-char (point-min))
480 (while (re-search-forward "<blubb>.*href=\"\\([^\"]+\\)\"><strong>\\([^>]*\\)</strong></a><dd>\\([^-]+\\)- <b>\\([^<]+\\)<.*href=\"news:\\([^\"]+\\)\">.*\">\\(.+\\)</a><P>"
482 (setq url (match-string 1)
483 subject (match-string 2)
484 date (match-string 3)
485 group (match-string 4)
486 id (concat "<" (match-string 5) ">")
487 from (match-string 6))
491 (make-full-mail-header
492 i (concat "(" group ") " subject) from date
497 ;; Return the articles in the right order.
498 (setq nnweb-articles (nreverse map))))))
500 (defun nnweb-altavista-wash-article ()
501 (goto-char (point-min))
502 (let ((case-fold-search t)
504 (when (re-search-forward "<H1>\\(.*\\)</H1>" nil t)
505 (setq subject (match-string 1)))
506 (re-search-forward "^<strong>" nil t)
507 (delete-region (point-min) (match-beginning 0))
508 (goto-char (point-min))
509 (while (looking-at "<strong>\\([^ ]+\\) +</strong> +\\(.*\\)$")
510 (replace-match "\\1: \\2" t)
512 (when (re-search-backward "^References:" nil t)
513 (narrow-to-region (point) (progn (forward-line 1) (point)))
514 (goto-char (point-min))
515 (while (re-search-forward "<A.*\\?id@\\([^\"]+\\)\">[0-9]+</A>" nil t)
516 (replace-match "<\\1> " t))
518 (nnweb-remove-markup))))
520 (defun nnweb-altavista-search (search)
521 (url-insert-file-contents
523 (nnweb-definition 'address)
525 (nnweb-encode-www-form-urlencoded
536 ;;; nnweb.el ends here