*** 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-close-group (group &optional server)
103   (nnweb-possibly-change-server server)
104   (gnus-kill-buffer nnweb-buffer)
105   t)
106
107 (deffoo nnweb-request-article (article &optional group server buffer)
108   (nnweb-possibly-change-server server)
109   (save-excursion
110     (set-buffer (or buffer nntp-server-buffer))
111     (let ((url (caddr (assq article nnweb-articles))))
112       (when (or (and url
113                      (nnweb-fetch-url url))
114                 (and (stringp article)
115                      (let ((fetch (nnweb-definition 'id))
116                            art)
117                        (when (string-match "^<\\(.*\\)>$" article)
118                          (setq art (match-string 1 article)))
119                        (and fetch
120                             art
121                             (nnweb-fetch-url
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)
127         t))))
128
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))
133
134 (deffoo nnweb-request-update-info (group info &optional server)
135   (nnweb-possibly-change-server server)
136   (setcar (cddr info) nil))
137
138 (deffoo nnweb-asynchronous-p ()
139   t)
140
141 (nnoo-define-skeleton nnweb)
142
143 ;;; Internal functions
144
145 (defun nnweb-definition (type)
146   "Return the definition of TYPE."
147   (let ((def (cdr (assq type (assq nnweb-type nnweb-type-definition)))))
148     (unless def
149       (error "Undefined definition %s" type))
150     def))
151
152 (defun nnweb-possibly-change-server (&optional server)
153   (nnweb-init server)
154   (when server
155     (unless (nnweb-server-opened server)
156       (nnweb-open-server server))))
157
158 (defun nnweb-init (server)
159   "Initialize buffers and such."
160   (unless (gnus-buffer-live-p nnweb-buffer)
161     (setq nnweb-buffer
162           (save-excursion
163             (nnheader-set-temp-buffer
164              (format " *nnweb %s %s %s*" nnweb-type nnweb-search server))))))
165
166 (defun nnweb-fetch-url (url)
167   (save-excursion
168     (if (not nnheader-callback-function)
169         (let ((buf (current-buffer)))
170           (save-excursion
171             (set-buffer nnweb-buffer)
172             (erase-buffer)
173             (prog1
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)
178       t)))
179
180 (defun nnweb-callback (buffer callback)
181   (when (gnus-buffer-live-p url-working-buffer)
182     (save-excursion
183       (set-buffer url-working-buffer)
184       (funcall (nnweb-definition 'article))
185       (nnweb-decode-entities)
186       (set-buffer buffer)
187       (goto-char (point-max))
188       (insert-buffer-substring url-working-buffer))
189     (funcall callback t)
190     (gnus-kill-buffer url-working-buffer)))
191
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)
199     (save-excursion
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)
204       (url-retrieve url))
205     (setq-default url-be-asynchronous old-asynch)))
206
207 (defun nnweb-encode-www-form-urlencoded (pairs)
208   "Return PAIRS encoded for forms."
209   (mapconcat 
210     (function
211       (lambda (data)
212         (concat (w3-form-encode-xwfu (car data)) "="
213                 (w3-form-encode-xwfu (cdr data))))) pairs "&"))
214
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"))))
220     (prog1
221         (url-insert-file-contents url)
222       (setq buffer-file-name nil))))
223
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))
228                                                   w3-html-entities ))
229                                        ?#))
230                    t t)))
231
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)
237                        (point-max))))
238   (goto-char (point-min))
239   (while (re-search-forward "<[^>]+>" nil t)
240     (replace-match "" t t)))
241
242 ;;;
243 ;;; DejaNews functions.
244 ;;;
245
246 (defun nnweb-dejanews-create-mapping ()
247   "Perform the search and create an number-to-url alist."
248   (save-excursion
249     ;(set-buffer nnweb-buffer)
250     ;(erase-buffer)
251     (when (or t (funcall (nnweb-definition 'search) nnweb-search))
252       (let ((i 0)
253             (more t)
254             (case-fold-search t)
255             Subject Score Date Newsgroup Author
256             map url)
257         (while more
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)
263             (narrow-to-region 
264              (point) 
265              (if (re-search-forward "^ +[0-9]+\\." nil t)
266                  (match-beginning 0)
267                (point-max)))
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)
274               (replace-match " "))
275             (goto-char (point-min))
276             (while (re-search-forward "^ +\\([^:]+\\): +\\(.*\\)$" nil t)
277               (set (intern (match-string 1)) (match-string 2)))
278             (widen)
279             (when (string-match "#[0-9]+/[0-9]+ *$" Subject)
280               (setq Subject (substring Subject 0 (match-beginning 0))))
281             (push
282              (list
283               (incf i)
284               (make-full-mail-header
285                i (concat  "(" Newsgroup ") " Subject) Author Date
286                (concat "<" (message-unique-id) "-" (int-to-string i)
287                        "@dejanews>")
288                nil 0 (string-to-int Score) nil)
289               url)
290              map))
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))
295               (setq more nil)
296             ;; Yup -- fetch it.
297             (setq more (match-string 1))
298             (erase-buffer)
299             (url-insert-file-contents more)))
300         ;; Return the articles in the right order.
301         (setq nnweb-articles (nreverse map))))))
302
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 " *$")
313                 (not (eobp)))
314       (gnus-delete-line))
315     (while (looking-at "\\(^[^ ]+:\\) *")
316       (replace-match "\\1 " t)
317       (forward-line 1))
318     (when (re-search-forward "\n\n+" nil t)
319       (replace-match "\n" t t))))
320
321 (defun nnweb-dejanews-search (search)
322   (nnweb-fetch-form 
323    (nnweb-definition 'address)
324    `(("query" . ,search)
325      ("defaultOp" . "AND")
326      ("svcclass" . "dncurrent")
327      ("maxhits" . "100")
328      ("format" . "verbose")
329      ("threaded" . "0")
330      ("showsort" . "score")
331      ("agesign" . "1")
332      ("ageweight" . "1"))))
333
334 ;;;
335 ;;; InReference
336 ;;;
337
338 (defun nnweb-reference-create-mapping ()
339   "Perform the search and create an number-to-url alist."
340   (save-excursion
341     (set-buffer nnweb-buffer)
342     (erase-buffer)
343     (when (funcall (nnweb-definition 'search) nnweb-search)
344       (let ((i 0)
345             (more t)
346             (case-fold-search t)
347             Subject Score Date Newsgroups From Message-ID
348             map url)
349         (while more
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)
357             (narrow-to-region 
358              (point) 
359              (if (re-search-forward "^$" nil t)
360                  (match-beginning 0)
361                (point-max)))
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)
368               (replace-match " "))
369             (goto-char (point-min))
370             (while (re-search-forward "^\\([^:]+\\): \\(.*\\)$" nil t)
371               (set (intern (match-string 1)) (match-string 2)))
372             (widen)
373             (search-forward "</pre>" nil t)
374             (push
375              (list
376               (incf i)
377               (make-full-mail-header
378                i (concat  "(" Newsgroups ") " Subject) From Date
379                Message-ID
380                nil 0 (string-to-int Score) nil)
381               url)
382              map))
383           (setq more nil))
384         ;; Return the articles in the right order.
385         (setq nnweb-articles (nreverse map))))))
386
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)
393     (forward-line -1)
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 " *$")
400         (gnus-delete-line))
401       (narrow-to-region (point-min) body)
402       (while (and (re-search-forward "^$" nil t)
403                   (not (eobp)))
404         (gnus-delete-line))
405       (goto-char (point-min))
406       (while (looking-at "\\(^[^ ]+:\\) *")
407         (replace-match "\\1 " t)
408         (forward-line 1))
409       (goto-char (point-min))
410       (when (re-search-forward "^References:" nil t)
411         (narrow-to-region
412          (point) (if (re-search-forward "^$\\|^[^:]+:" nil t)
413                      (match-beginning 0)
414                    (point-max)))
415         (goto-char (point-min))
416         (while (not (eobp))
417           (unless (looking-at "References")
418             (insert "\t")
419             (forward-line 1)))
420         (goto-char (point-min))
421         (while (search-forward "," nil t)
422           (replace-match " " t t)))
423       (widen)
424       (set-marker body nil))))
425
426 (defun nnweb-reference-search (search)
427   (url-insert-file-contents
428    (concat 
429     (nnweb-definition 'address)
430     "?"
431     (nnweb-encode-www-form-urlencoded 
432      `(("search" . "advanced")
433        ("querytext" . ,search)
434        ("subj" . "")
435        ("name" . "")
436        ("login" . "")
437        ("host" . "")
438        ("organization" . "")
439        ("groups" . "")
440        ("keywords" . "")
441        ("choice" . "Search")
442        ("startmonth" . "Jul")
443        ("startday" . "25")
444        ("startyear" . "1996")
445        ("endmonth" . "Aug")
446        ("endday" . "24")
447        ("endyear" . "1996")
448        ("mode" . "Quick")
449        ("verbosity" . "Verbose")
450        ("ranking" . "Relevance")
451        ("first" . "1")
452        ("last" . "25")
453        ("score" . "50"))))))
454
455 ;;;
456 ;;; Alta Vista
457 ;;;
458
459 (defun nnweb-altavista-create-mapping ()
460   "Perform the search and create an number-to-url alist."
461   (save-excursion
462     (set-buffer nnweb-buffer)
463     (erase-buffer)
464     (when (funcall (nnweb-definition 'search) nnweb-search)
465       (let ((i 0)
466             (more t)
467             (case-fold-search t)
468             subject score date newsgroups from id
469             map url)
470         (while more
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>"
481                                     nil t)
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))
488             (push
489              (list
490               (incf i)
491               (make-full-mail-header
492                i (concat  "(" group ") " subject) from date
493                id nil 0 0 nil)
494               url)
495              map))
496           (setq more nil))
497         ;; Return the articles in the right order.
498         (setq nnweb-articles (nreverse map))))))
499
500 (defun nnweb-altavista-wash-article ()
501   (goto-char (point-min))
502   (let ((case-fold-search t)
503         subject)
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)
511       (forward-line 1))
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 "&lt;\\1&gt; " t))
517       (widen)
518       (nnweb-remove-markup))))
519
520 (defun nnweb-altavista-search (search)
521   (url-insert-file-contents
522    (concat 
523     (nnweb-definition 'address)
524     "?"
525     (nnweb-encode-www-form-urlencoded 
526      `(("pg" . "aq")
527        ("what" . "news")
528        ("fmt" . "d")
529        ("q" . ,search)
530        ("r" . "")
531        ("d0" . "")
532        ("d1" . ""))))))
533
534 (provide 'nnweb)
535
536 ;;; nnweb.el ends here