*** 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 "Couldn't request search"))
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 " *dejanews*")))
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     (url-insert-file-contents url)))
215
216 (defun nnweb-decode-entities ()
217   (goto-char (point-min))
218   (while (re-search-forward "&\\([a-z]+\\);" nil t)
219     (replace-match (char-to-string (or (cdr (assq (intern (match-string 1))
220                                                   w3-html-entities ))
221                                        ?#))
222                    t t)))
223
224 (defun nnweb-remove-markup ()
225   (goto-char (point-min))
226   (while (search-forward "<!--" nil t)
227     (delete-region (match-beginning 0)
228                    (or (search-forward "-->" nil t)
229                        (point-max))))
230   (goto-char (point-min))
231   (while (re-search-forward "<[^>]+>" nil t)
232     (replace-match "" t t)))
233
234 ;;;
235 ;;; DejaNews functions.
236 ;;;
237
238 (defun nnweb-dejanews-create-mapping ()
239   "Perform the search and create an number-to-url alist."
240   (save-excursion
241     (set-buffer nnweb-buffer)
242     (erase-buffer)
243     (when (funcall (nnweb-definition 'search) nnweb-search)
244       (let ((i 0)
245             (more t)
246             Subject Score Date Newsgroup Author
247             map url)
248         (while more
249           ;; Go through all the article hits on this page.
250           (goto-char (point-min))
251           (nnweb-decode-entities)
252           (goto-char (point-min))
253           (while (re-search-forward "^ +[0-9]+\\." nil t)
254             (narrow-to-region 
255              (point) 
256              (if (re-search-forward "^ +[0-9]+\\." nil t)
257                  (match-beginning 0)
258                (point-max)))
259             (goto-char (point-min))
260             (when (looking-at ".*HREF=\"\\([^\"]+\\)\"")
261               (setq url (match-string 1)))
262             (nnweb-remove-markup)
263             (goto-char (point-min))
264             (while (search-forward "\t" nil t)
265               (replace-match " "))
266             (goto-char (point-min))
267             (while (re-search-forward "^ +\\([^:]+\\): +\\(.*\\)$" nil t)
268               (set (intern (match-string 1)) (match-string 2)))
269             (widen)
270             (when (string-match "#[0-9]+/[0-9]+ *$" Subject)
271               (setq Subject (substring Subject 0 (match-beginning 0))))
272             (push
273              (list
274               (incf i)
275               (make-full-mail-header
276                i (concat  "(" Newsgroup ") " Subject) Author Date
277                (concat "<" (message-unique-id) "-" (int-to-string i)
278                        "@dejanews>")
279                nil 0 (string-to-int Score) nil)
280               url)
281              map))
282           ;; See whether there is a "Get next 20 hits" button here.
283           (if (or (not (re-search-forward
284                         "HREF=\"\\([^\"]+\\)\">Get next" nil t))
285                   (> i nnweb-max-hits))
286               (setq more nil)
287             ;; Yup -- fetch it.
288             (setq more (match-string 1))
289             (erase-buffer)
290             (url-insert-file-contents more)))
291         ;; Return the articles in the right order.
292         (setq nnweb-articles (nreverse map))))))
293
294 (defun nnweb-dejanews-wash-article ()
295   (goto-char (point-min))
296   (re-search-forward "<PRE>" nil t)
297   (delete-region (point-min) (point))
298   (re-search-forward "</PRE>" nil t)
299   (delete-region (point) (point-max))
300   (nnweb-remove-markup)
301   (goto-char (point-min))
302   (while (and (looking-at " *$")
303               (not (eobp)))
304     (gnus-delete-line))
305   (while (looking-at "\\(^[^ ]+:\\) *")
306     (replace-match "\\1 " t)
307     (forward-line 1))
308   (when (re-search-forward "\n\n+" nil t)
309     (replace-match "\n" t t)))
310
311 (defun nnweb-dejanews-search (search)
312   (nnweb-fetch-form 
313    (nnweb-definition 'address)
314    `(("query" . ,search)
315      ("defaultOp" . "AND")
316      ("svcclass" . "dncurrent")
317      ("maxhits" . "100")
318      ("format" . "verbose")
319      ("threaded" . "0")
320      ("showsort" . "score")
321      ("agesign" . "1")
322      ("ageweight" . "1"))))
323
324 ;;;
325 ;;; InReference
326 ;;;
327
328 (defun nnweb-reference-create-mapping ()
329   "Perform the search and create an number-to-url alist."
330   (save-excursion
331     (set-buffer nnweb-buffer)
332     (erase-buffer)
333     (when (funcall (nnweb-definition 'search) nnweb-search)
334       (let ((i 0)
335             (more t)
336             Subject Score Date Newsgroups From Message-ID
337             map url)
338         (while more
339           ;; Go through all the article hits on this page.
340           (goto-char (point-min))
341           (search-forward "</pre><hr>" nil t)
342           (delete-region (point-min) (point))
343           ;(nnweb-decode-entities)
344           (goto-char (point-min))
345           (while (re-search-forward "^ +[0-9]+\\." nil t)
346             (narrow-to-region 
347              (point) 
348              (if (re-search-forward "^$" nil t)
349                  (match-beginning 0)
350                (point-max)))
351             (goto-char (point-min))
352             (when (looking-at ".*href=\"\\([^\"]+\\)\"")
353               (setq url (match-string 1)))
354             (nnweb-remove-markup)
355             (goto-char (point-min))
356             (while (search-forward "\t" nil t)
357               (replace-match " "))
358             (goto-char (point-min))
359             (while (re-search-forward "^\\([^:]+\\): \\(.*\\)$" nil t)
360               (set (intern (match-string 1)) (match-string 2)))
361             (widen)
362             (search-forward "</pre>" nil t)
363             (push
364              (list
365               (incf i)
366               (make-full-mail-header
367                i (concat  "(" Newsgroups ") " Subject) From Date
368                Message-ID
369                nil 0 (string-to-int Score) nil)
370               url)
371              map))
372           (setq more nil))
373         ;; Return the articles in the right order.
374         (setq nnweb-articles (nreverse map))))))
375
376 (defun nnweb-reference-wash-article ()
377   (goto-char (point-min))
378   (re-search-forward "^</center><hr>" nil t)
379   (delete-region (point-min) (point))
380   (search-forward "<pre>" nil t)
381   (forward-line -1)
382   (let ((body (point-marker)))
383     (search-forward "</pre>" nil t)
384     (delete-region (point) (point-max))
385     (nnweb-remove-markup)
386     (goto-char (point-min))
387     (while (looking-at " *$")
388       (gnus-delete-line))
389     (narrow-to-region (point-min) body)
390     (while (and (re-search-forward "^$" nil t)
391                 (not (eobp)))
392       (gnus-delete-line))
393     (goto-char (point-min))
394     (while (looking-at "\\(^[^ ]+:\\) *")
395       (replace-match "\\1 " t)
396       (forward-line 1))
397     (goto-char (point-min))
398     (when (re-search-forward "^References:" nil t)
399       (narrow-to-region
400        (point) (if (re-search-forward "^$\\|^[^:]+:" nil t)
401                    (match-beginning 0)
402                  (point-max)))
403       (goto-char (point-min))
404       (while (not (eobp))
405         (unless (looking-at "References")
406           (insert "\t")
407           (forward-line 1)))
408       (goto-char (point-min))
409       (while (search-forward "," nil t)
410         (replace-match " " t t)))
411     (widen)
412     (set-marker body nil)))
413
414 (defun nnweb-reference-search (search)
415   (url-insert-file-contents
416    (concat 
417     (nnweb-definition 'address)
418     "?"
419     (nnweb-encode-www-form-urlencoded 
420      `(("search" . "advanced")
421        ("querytext" . ,search)
422        ("subj" . "")
423        ("name" . "")
424        ("login" . "")
425        ("host" . "")
426        ("organization" . "")
427        ("groups" . "")
428        ("keywords" . "")
429        ("choice" . "Search")
430        ("startmonth" . "Jul")
431        ("startday" . "25")
432        ("startyear" . "1996")
433        ("endmonth" . "Aug")
434        ("endday" . "24")
435        ("endyear" . "1996")
436        ("mode" . "Quick")
437        ("verbosity" . "Verbose")
438        ("ranking" . "Relevance")
439        ("first" . "1")
440        ("last" . "25")
441        ("score" . "50"))))))
442
443 ;;;
444 ;;; Alta Vista
445 ;;;
446
447 (defun nnweb-altavista-create-mapping ()
448   "Perform the search and create an number-to-url alist."
449   (save-excursion
450     (set-buffer nnweb-buffer)
451     (erase-buffer)
452     (when (funcall (nnweb-definition 'search) nnweb-search)
453       (let ((i 0)
454             (more t)
455             Subject Score Date Newsgroups From Message-ID
456             map url)
457         (while more
458           ;; Go through all the article hits on this page.
459           (goto-char (point-min))
460           (search-forward "<dt>" nil t)
461           (delete-region (point-min) (match-beginning 0))
462           (goto-char (point-min))
463           (while (search-forward "<dt>" nil t)
464             (replace-match "\n<blubb>"))
465           (nnweb-decode-entities)
466           (goto-char (point-min))
467           (while (re-search-forward "<blubb>.*href=\"\\([^\"]+\\)\"><strong>\\([^>]*\\)</strong></a><dd>\\([^-]+\\)- <b>\\([^<]+\\)<.*href=\"news:\\([^\"]+\\)\">.*\">\\(.+\\)</a><P>"
468                                     nil t)
469             (setq url (match-string 1)
470                   subject (match-string 2)
471                   date (match-string 3)
472                   group (match-string 4)
473                   id (concat "<" (match-string 5) ">")
474                   from (match-string 6))
475             (push
476              (list
477               (incf i)
478               (make-full-mail-header
479                i (concat  "(" group ") " subject) from date
480                id nil 0 0 nil)
481               url)
482              map))
483           (setq more nil))
484         ;; Return the articles in the right order.
485         (setq nnweb-articles (nreverse map))))))
486
487 (defun nnweb-altavista-wash-article ()
488   (goto-char (point-min))
489   (let (subject)
490     (when (re-search-forward "<H1>\\(.*\\)</H1>" nil t)
491       (setq subject (match-string 1)))
492     (re-search-forward "^<strong>" nil t)
493     (delete-region (point-min) (match-beginning 0))
494     (goto-char (point-min))
495     (while (looking-at "<strong>\\([^ ]+\\) +</strong> +\\(.*\\)$")
496       (replace-match "\\1: \\2" t)
497       (forward-line 1))
498     (when (re-search-backward "^References:" nil t)
499       (narrow-to-region (point) (progn (forward-line 1) (point)))
500       (goto-char (point-min))
501       (while (re-search-forward "<A.*\\?id@\\([^\"]+\\)\">[0-9]+</A>" nil t)
502         (replace-match "&lt;\\1&gt; " t))
503       (widen)
504       (nnweb-remove-markup))))
505
506 (defun nnweb-altavista-search (search)
507   (url-insert-file-contents
508    (concat 
509     (nnweb-definition 'address)
510     "?"
511     (nnweb-encode-www-form-urlencoded 
512      `(("pg" . "aq")
513        ("what" . "news")
514        ("fmt" . "d")
515        ("q" . ,search)
516        ("r" . "")
517        ("d0" . "")
518        ("d1" . ""))))))
519
520 (provide 'nnweb)
521
522 ;;; nnweb.el ends here