*** empty log message ***
[gnus] / lisp / nnweb.el
1 ;;; nnweb.el --- retrieving articles via web search engines
2 ;; Copyright (C) 1996 Free Software Foundation, Inc.
3
4 ;; Author: Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
5 ;; Keywords: news
6
7 ;; This file is part of GNU Emacs.
8
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)
12 ;; any later version.
13
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.
18
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.
23
24 ;;; Commentary:
25
26 ;; Note: You need to have `url' and `w3' installed for this
27 ;; backend to work.
28
29 ;;; Code:
30
31 (require 'nnoo)
32 (require 'message)
33 (require 'gnus-util)
34 (require 'w3-forms)
35 (require 'url)
36
37 (nnoo-declare nnweb)
38
39 (defvoo nnweb-type 'dejanews
40   "What search engine type is being used.")
41
42 (defvar nnweb-type-definition
43   '((dejanews
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"))
48     (reference
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"))
53     (altavista
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.")
60
61 (defvoo nnweb-search nil
62   "Search string to feed to DejaNews.")
63
64 (defvoo nnweb-max-hits 100
65   "Maximum number of hits to display.")
66
67 ;;; Internal variables
68
69 (defvoo nnweb-articles nil)
70 (defvoo nnweb-buffer nil)
71
72 ;;; Interface functions
73
74 (nnoo-define-basics nnweb)
75
76 (deffoo nnweb-retrieve-headers (articles &optional group server fetch-old)
77   (nnweb-possibly-change-server server)
78   (save-excursion
79     (set-buffer nntp-server-buffer)
80     (erase-buffer)
81     (let (article header)
82       (while (setq article (pop articles))
83         (when (setq header (cadr (assq article nnweb-articles)))
84           (nnheader-insert-nov header)))
85       'nov)))
86
87 (deffoo nnweb-request-group (group &optional server dont-check)
88   (nnweb-possibly-change-server server)
89   (when (or (not dont-check)
90             (not nnweb-articles))
91     (funcall (nnweb-definition 'map)))
92   (cond
93    ((not nnweb-articles)
94     (nnheader-report 'nnweb "No matching articles"))
95    (t
96     (nnheader-report 'nnweb "Opened group %s" group)
97     (nnheader-insert
98      "211 %d %d %d %s\n" (length nnweb-articles)
99      (caar nnweb-articles) (caar (last nnweb-articles))
100       group))))
101
102 (deffoo nnweb-request-article (article &optional group server buffer)
103   (nnweb-possibly-change-server server)
104   (save-excursion
105     (set-buffer (or buffer nntp-server-buffer))
106     (let ((url (caddr (assq article nnweb-articles))))
107       (when (or (and url
108                      (nnweb-fetch-url url))
109                 (and (stringp article)
110                      (let ((fetch (nnweb-definition 'id))
111                            art)
112                        (when (string-match "^<\\(.*\\)>$" article)
113                          (setq art (match-string 1 article)))
114                        (and fetch
115                             art
116                             (nnweb-fetch-url
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)
122         t))))
123
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))
128
129 (deffoo nnweb-request-update-info (group info &optional server)
130   (nnweb-possibly-change-server server)
131   (setcar (cddr info) nil))
132
133 (deffoo nnweb-asynchronous-p ()
134   t)
135
136 (nnoo-define-skeleton nnweb)
137
138 ;;; Internal functions
139
140 (defun nnweb-definition (type)
141   "Return the definition of TYPE."
142   (let ((def (cdr (assq type (assq nnweb-type nnweb-type-definition)))))
143     (unless def
144       (error "Undefined definition %s" type))
145     def))
146
147 (defun nnweb-possibly-change-server (&optional server)
148   (nnweb-init server)
149   (when server
150     (unless (nnweb-server-opened server)
151       (nnweb-open-server server))))
152
153 (defun nnweb-init (server)
154   "Initialize buffers and such."
155   (unless (gnus-buffer-live-p nnweb-buffer)
156     (setq nnweb-buffer
157           (save-excursion
158             (nnheader-set-temp-buffer
159              (format " *nnweb %s %s %s*" nnweb-type nnweb-search server))))))
160
161 (defun nnweb-fetch-url (url)
162   (save-excursion
163     (if (not nnheader-callback-function)
164         (let ((buf (current-buffer)))
165           (save-excursion
166             (set-buffer nnweb-buffer)
167             (erase-buffer)
168             (prog1
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)
173       t)))
174
175 (defun nnweb-callback (buffer callback)
176   (save-excursion
177     (set-buffer url-working-buffer)
178     (funcall (nnweb-definition 'article))
179     (nnweb-decode-entities)
180     (set-buffer buffer)
181     (goto-char (point-max))
182     (insert-buffer-substring url-working-buffer))
183   (funcall callback t)
184   (gnus-kill-buffer url-working-buffer))
185
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)
193     (save-excursion
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)
198       (url-retrieve url))
199     (setq-default url-be-asynchronous old-asynch)))
200
201 (defun nnweb-encode-www-form-urlencoded (pairs)
202   "Return PAIRS encoded for forms."
203   (mapconcat 
204     (function
205       (lambda (data)
206         (concat (w3-form-encode-xwfu (car data)) "="
207                 (w3-form-encode-xwfu (cdr data))))) pairs "&"))
208
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"))))
214     (prog1
215         (url-insert-file-contents url)
216       (setq buffer-file-name nil))))
217
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))
222                                                   w3-html-entities ))
223                                        ?#))
224                    t t)))
225
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)
231                        (point-max))))
232   (goto-char (point-min))
233   (while (re-search-forward "<[^>]+>" nil t)
234     (replace-match "" t t)))
235
236 ;;;
237 ;;; DejaNews functions.
238 ;;;
239
240 (defun nnweb-dejanews-create-mapping ()
241   "Perform the search and create an number-to-url alist."
242   (save-excursion
243     (set-buffer nnweb-buffer)
244     (erase-buffer)
245     (when (funcall (nnweb-definition 'search) nnweb-search)
246       (let ((i 0)
247             (more t)
248             Subject Score Date Newsgroup Author
249             map url)
250         (while more
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)
256             (narrow-to-region 
257              (point) 
258              (if (re-search-forward "^ +[0-9]+\\." nil t)
259                  (match-beginning 0)
260                (point-max)))
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)
267               (replace-match " "))
268             (goto-char (point-min))
269             (while (re-search-forward "^ +\\([^:]+\\): +\\(.*\\)$" nil t)
270               (set (intern (match-string 1)) (match-string 2)))
271             (widen)
272             (when (string-match "#[0-9]+/[0-9]+ *$" Subject)
273               (setq Subject (substring Subject 0 (match-beginning 0))))
274             (push
275              (list
276               (incf i)
277               (make-full-mail-header
278                i (concat  "(" Newsgroup ") " Subject) Author Date
279                (concat "<" (message-unique-id) "-" (int-to-string i)
280                        "@dejanews>")
281                nil 0 (string-to-int Score) nil)
282               url)
283              map))
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))
288               (setq more nil)
289             ;; Yup -- fetch it.
290             (setq more (match-string 1))
291             (erase-buffer)
292             (url-insert-file-contents more)))
293         ;; Return the articles in the right order.
294         (setq nnweb-articles (nreverse map))))))
295
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 " *$")
305               (not (eobp)))
306     (gnus-delete-line))
307   (while (looking-at "\\(^[^ ]+:\\) *")
308     (replace-match "\\1 " t)
309     (forward-line 1))
310   (when (re-search-forward "\n\n+" nil t)
311     (replace-match "\n" t t)))
312
313 (defun nnweb-dejanews-search (search)
314   (nnweb-fetch-form 
315    (nnweb-definition 'address)
316    `(("query" . ,search)
317      ("defaultOp" . "AND")
318      ("svcclass" . "dncurrent")
319      ("maxhits" . "100")
320      ("format" . "verbose")
321      ("threaded" . "0")
322      ("showsort" . "score")
323      ("agesign" . "1")
324      ("ageweight" . "1"))))
325
326 ;;;
327 ;;; InReference
328 ;;;
329
330 (defun nnweb-reference-create-mapping ()
331   "Perform the search and create an number-to-url alist."
332   (save-excursion
333     (set-buffer nnweb-buffer)
334     (erase-buffer)
335     (when (funcall (nnweb-definition 'search) nnweb-search)
336       (let ((i 0)
337             (more t)
338             Subject Score Date Newsgroups From Message-ID
339             map url)
340         (while more
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)
348             (narrow-to-region 
349              (point) 
350              (if (re-search-forward "^$" nil t)
351                  (match-beginning 0)
352                (point-max)))
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)
359               (replace-match " "))
360             (goto-char (point-min))
361             (while (re-search-forward "^\\([^:]+\\): \\(.*\\)$" nil t)
362               (set (intern (match-string 1)) (match-string 2)))
363             (widen)
364             (search-forward "</pre>" nil t)
365             (push
366              (list
367               (incf i)
368               (make-full-mail-header
369                i (concat  "(" Newsgroups ") " Subject) From Date
370                Message-ID
371                nil 0 (string-to-int Score) nil)
372               url)
373              map))
374           (setq more nil))
375         ;; Return the articles in the right order.
376         (setq nnweb-articles (nreverse map))))))
377
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)
383   (forward-line -1)
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 " *$")
390       (gnus-delete-line))
391     (narrow-to-region (point-min) body)
392     (while (and (re-search-forward "^$" nil t)
393                 (not (eobp)))
394       (gnus-delete-line))
395     (goto-char (point-min))
396     (while (looking-at "\\(^[^ ]+:\\) *")
397       (replace-match "\\1 " t)
398       (forward-line 1))
399     (goto-char (point-min))
400     (when (re-search-forward "^References:" nil t)
401       (narrow-to-region
402        (point) (if (re-search-forward "^$\\|^[^:]+:" nil t)
403                    (match-beginning 0)
404                  (point-max)))
405       (goto-char (point-min))
406       (while (not (eobp))
407         (unless (looking-at "References")
408           (insert "\t")
409           (forward-line 1)))
410       (goto-char (point-min))
411       (while (search-forward "," nil t)
412         (replace-match " " t t)))
413     (widen)
414     (set-marker body nil)))
415
416 (defun nnweb-reference-search (search)
417   (url-insert-file-contents
418    (concat 
419     (nnweb-definition 'address)
420     "?"
421     (nnweb-encode-www-form-urlencoded 
422      `(("search" . "advanced")
423        ("querytext" . ,search)
424        ("subj" . "")
425        ("name" . "")
426        ("login" . "")
427        ("host" . "")
428        ("organization" . "")
429        ("groups" . "")
430        ("keywords" . "")
431        ("choice" . "Search")
432        ("startmonth" . "Jul")
433        ("startday" . "25")
434        ("startyear" . "1996")
435        ("endmonth" . "Aug")
436        ("endday" . "24")
437        ("endyear" . "1996")
438        ("mode" . "Quick")
439        ("verbosity" . "Verbose")
440        ("ranking" . "Relevance")
441        ("first" . "1")
442        ("last" . "25")
443        ("score" . "50"))))))
444
445 ;;;
446 ;;; Alta Vista
447 ;;;
448
449 (defun nnweb-altavista-create-mapping ()
450   "Perform the search and create an number-to-url alist."
451   (save-excursion
452     (set-buffer nnweb-buffer)
453     (erase-buffer)
454     (when (funcall (nnweb-definition 'search) nnweb-search)
455       (let ((i 0)
456             (more t)
457             Subject Score Date Newsgroups From Message-ID
458             map url)
459         (while more
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>"
470                                     nil t)
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))
477             (push
478              (list
479               (incf i)
480               (make-full-mail-header
481                i (concat  "(" group ") " subject) from date
482                id nil 0 0 nil)
483               url)
484              map))
485           (setq more nil))
486         ;; Return the articles in the right order.
487         (setq nnweb-articles (nreverse map))))))
488
489 (defun nnweb-altavista-wash-article ()
490   (goto-char (point-min))
491   (let (subject)
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)
499       (forward-line 1))
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 "&lt;\\1&gt; " t))
505       (widen)
506       (nnweb-remove-markup))))
507
508 (defun nnweb-altavista-search (search)
509   (url-insert-file-contents
510    (concat 
511     (nnweb-definition 'address)
512     "?"
513     (nnweb-encode-www-form-urlencoded 
514      `(("pg" . "aq")
515        ("what" . "news")
516        ("fmt" . "d")
517        ("q" . ,search)
518        ("r" . "")
519        ("d0" . "")
520        ("d1" . ""))))))
521
522 (provide 'nnweb)
523
524 ;;; nnweb.el ends here