*** 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   (when (gnus-buffer-live-p nnweb-buffer)
105     (save-excursion
106       (set-buffer nnweb-buffer)
107       (set-buffer-modified-p nil)
108       (kill-buffer nnweb-buffer)))
109   t)
110
111 (deffoo nnweb-request-article (article &optional group server buffer)
112   (nnweb-possibly-change-server server)
113   (save-excursion
114     (set-buffer (or buffer nntp-server-buffer))
115     (let ((url (caddr (assq article nnweb-articles))))
116       (when (or (and url
117                      (nnweb-fetch-url url))
118                 (and (stringp article)
119                      (let ((fetch (nnweb-definition 'id))
120                            art)
121                        (when (string-match "^<\\(.*\\)>$" article)
122                          (setq art (match-string 1 article)))
123                        (and fetch
124                             art
125                             (nnweb-fetch-url
126                              (format fetch article))))))
127         (unless nnheader-callback-function
128           (funcall (nnweb-definition 'article))
129           (nnweb-decode-entities))
130         (nnheader-report 'nnweb "Fetched article %s" article)
131         t))))
132
133 (deffoo nnweb-close-server (&optional server)
134   (when (and (nnweb-server-opened server)
135              (gnus-buffer-live-p nnweb-buffer))
136     (save-excursion
137       (set-buffer nnweb-buffer)
138       (set-buffer-modified-p nil)
139       (kill-buffer nnweb-buffer)))
140   (nnoo-close-server 'nnweb server))
141
142 (deffoo nnweb-request-update-info (group info &optional server)
143   (nnweb-possibly-change-server server)
144   (setcar (cddr info) nil))
145
146 (deffoo nnweb-asynchronous-p ()
147   t)
148
149 (nnoo-define-skeleton nnweb)
150
151 ;;; Internal functions
152
153 (defun nnweb-definition (type)
154   "Return the definition of TYPE."
155   (let ((def (cdr (assq type (assq nnweb-type nnweb-type-definition)))))
156     (unless def
157       (error "Undefined definition %s" type))
158     def))
159
160 (defun nnweb-possibly-change-server (&optional server)
161   (nnweb-init server)
162   (when server
163     (unless (nnweb-server-opened server)
164       (nnweb-open-server server))))
165
166 (defun nnweb-init (server)
167   "Initialize buffers and such."
168   (unless (gnus-buffer-live-p nnweb-buffer)
169     (setq nnweb-buffer
170           (save-excursion
171             (nnheader-set-temp-buffer
172              (format " *nnweb %s %s %s*" nnweb-type nnweb-search server))))))
173
174 (defun nnweb-fetch-url (url)
175   (save-excursion
176     (if (not nnheader-callback-function)
177         (let ((buf (current-buffer)))
178           (save-excursion
179             (set-buffer nnweb-buffer)
180             (erase-buffer)
181             (prog1
182                 (url-insert-file-contents url)
183               (copy-to-buffer buf (point-min) (point-max)))))
184       (nnweb-url-retrieve-asynch
185        url 'nnweb-callback (current-buffer) nnheader-callback-function)
186       t)))
187
188 (defun nnweb-callback (buffer callback)
189   (when (gnus-buffer-live-p url-working-buffer)
190     (save-excursion
191       (set-buffer url-working-buffer)
192       (funcall (nnweb-definition 'article))
193       (nnweb-decode-entities)
194       (set-buffer buffer)
195       (goto-char (point-max))
196       (insert-buffer-substring url-working-buffer))
197     (funcall callback t)
198     (gnus-kill-buffer url-working-buffer)))
199
200 (defun nnweb-url-retrieve-asynch (url callback &rest data)
201   (let ((url-request-method "GET")
202         (old-asynch url-be-asynchronous)
203         (url-request-data nil)
204         (url-request-extra-headers nil)
205         (url-working-buffer (generate-new-buffer-name " *nnweb*")))
206     (setq-default url-be-asynchronous t)
207     (save-excursion
208       (set-buffer (get-buffer-create url-working-buffer))
209       (setq url-current-callback-data data
210             url-be-asynchronous t
211             url-current-callback-func callback)
212       (url-retrieve url))
213     (setq-default url-be-asynchronous old-asynch)))
214
215 (defun nnweb-encode-www-form-urlencoded (pairs)
216   "Return PAIRS encoded for forms."
217   (mapconcat 
218     (function
219       (lambda (data)
220         (concat (w3-form-encode-xwfu (car data)) "="
221                 (w3-form-encode-xwfu (cdr data))))) pairs "&"))
222
223 (defun nnweb-fetch-form (url pairs)
224   (let ((url-request-data (nnweb-encode-www-form-urlencoded pairs))
225         (url-request-method 'POST)
226         (url-request-extra-headers 
227          '(("Content-type" . "application/x-www-form-urlencoded"))))
228     (prog1
229         (url-insert-file-contents url)
230       (setq buffer-file-name nil))))
231
232 (defun nnweb-decode-entities ()
233   (goto-char (point-min))
234   (while (re-search-forward "&\\([a-z]+\\);" nil t)
235     (replace-match (char-to-string (or (cdr (assq (intern (match-string 1))
236                                                   w3-html-entities ))
237                                        ?#))
238                    t t)))
239
240 (defun nnweb-remove-markup ()
241   (goto-char (point-min))
242   (while (search-forward "<!--" nil t)
243     (delete-region (match-beginning 0)
244                    (or (search-forward "-->" nil t)
245                        (point-max))))
246   (goto-char (point-min))
247   (while (re-search-forward "<[^>]+>" nil t)
248     (replace-match "" t t)))
249
250 ;;;
251 ;;; DejaNews functions.
252 ;;;
253
254 (defun nnweb-dejanews-create-mapping ()
255   "Perform the search and create an number-to-url alist."
256   (save-excursion
257     (set-buffer nnweb-buffer)
258     (erase-buffer)
259     (when (funcall (nnweb-definition 'search) nnweb-search)
260       (let ((i 0)
261             (more t)
262             (case-fold-search t)
263             Subject Score Date Newsgroup Author
264             map url)
265         (while more
266           ;; Go through all the article hits on this page.
267           (goto-char (point-min))
268           (nnweb-decode-entities)
269           (goto-char (point-min))
270           (while (re-search-forward "^ +[0-9]+\\." nil t)
271             (narrow-to-region 
272              (point) 
273              (cond ((re-search-forward "^ +[0-9]+\\." nil t)
274                     (match-beginning 0))
275                    ((search-forward "\n\n" nil t)
276                     (point))
277                    (t
278                     (point-max))))
279             (goto-char (point-min))
280             (when (looking-at ".*HREF=\"\\([^\"]+\\)\"")
281               (setq url (match-string 1)))
282             (nnweb-remove-markup)
283             (goto-char (point-min))
284             (while (search-forward "\t" nil t)
285               (replace-match " "))
286             (goto-char (point-min))
287             (while (re-search-forward "^ +\\([^:]+\\): +\\(.*\\)$" nil t)
288               (set (intern (match-string 1)) (match-string 2)))
289             (widen)
290             (when (string-match "#[0-9]+/[0-9]+ *$" Subject)
291               (setq Subject (substring Subject 0 (match-beginning 0))))
292             (push
293              (list
294               (incf i)
295               (make-full-mail-header
296                i (concat  "(" Newsgroup ") " Subject) Author Date
297                (concat "<" (message-unique-id) "-" (int-to-string i)
298                        "@dejanews>")
299                nil 0 (string-to-int Score) nil)
300               url)
301              map))
302           ;; See whether there is a "Get next 20 hits" button here.
303           (if (or (not (re-search-forward
304                         "HREF=\"\\([^\"]+\\)\">Get next" nil t))
305                   (>= i nnweb-max-hits))
306               (setq more nil)
307             ;; Yup -- fetch it.
308             (setq more (match-string 1))
309             (erase-buffer)
310             (url-insert-file-contents more)))
311         ;; Return the articles in the right order.
312         (setq nnweb-articles (nreverse map))))))
313
314 (defun nnweb-dejanews-wash-article ()
315   (let ((case-fold-search t))
316     (goto-char (point-min))
317     (re-search-forward "<PRE>" nil t)
318     (delete-region (point-min) (point))
319     (re-search-forward "</PRE>" nil t)
320     (delete-region (point) (point-max))
321     (nnweb-remove-markup)
322     (goto-char (point-min))
323     (while (and (looking-at " *$")
324                 (not (eobp)))
325       (gnus-delete-line))
326     (while (looking-at "\\(^[^ ]+:\\) *")
327       (replace-match "\\1 " t)
328       (forward-line 1))
329     (when (re-search-forward "\n\n+" nil t)
330       (replace-match "\n" t t))))
331
332 (defun nnweb-dejanews-search (search)
333   (nnweb-fetch-form 
334    (nnweb-definition 'address)
335    `(("query" . ,search)
336      ("defaultOp" . "AND")
337      ("svcclass" . "dncurrent")
338      ("maxhits" . "100")
339      ("format" . "verbose")
340      ("threaded" . "0")
341      ("showsort" . "score")
342      ("agesign" . "1")
343      ("ageweight" . "1"))))
344
345 ;;;
346 ;;; InReference
347 ;;;
348
349 (defun nnweb-reference-create-mapping ()
350   "Perform the search and create an number-to-url alist."
351   (save-excursion
352     (set-buffer nnweb-buffer)
353     (erase-buffer)
354     (when (funcall (nnweb-definition 'search) nnweb-search)
355       (let ((i 0)
356             (more t)
357             (case-fold-search t)
358             Subject Score Date Newsgroups From Message-ID
359             map url)
360         (while more
361           ;; Go through all the article hits on this page.
362           (goto-char (point-min))
363           (search-forward "</pre><hr>" nil t)
364           (delete-region (point-min) (point))
365           ;(nnweb-decode-entities)
366           (goto-char (point-min))
367           (while (re-search-forward "^ +[0-9]+\\." nil t)
368             (narrow-to-region 
369              (point) 
370              (if (re-search-forward "^$" nil t)
371                  (match-beginning 0)
372                (point-max)))
373             (goto-char (point-min))
374             (when (looking-at ".*href=\"\\([^\"]+\\)\"")
375               (setq url (match-string 1)))
376             (nnweb-remove-markup)
377             (goto-char (point-min))
378             (while (search-forward "\t" nil t)
379               (replace-match " "))
380             (goto-char (point-min))
381             (while (re-search-forward "^\\([^:]+\\): \\(.*\\)$" nil t)
382               (set (intern (match-string 1)) (match-string 2)))
383             (widen)
384             (search-forward "</pre>" nil t)
385             (push
386              (list
387               (incf i)
388               (make-full-mail-header
389                i (concat  "(" Newsgroups ") " Subject) From Date
390                Message-ID
391                nil 0 (string-to-int Score) nil)
392               url)
393              map))
394           (setq more nil))
395         ;; Return the articles in the right order.
396         (setq nnweb-articles (nreverse map))))))
397
398 (defun nnweb-reference-wash-article ()
399   (let ((case-fold-search t))
400     (goto-char (point-min))
401     (re-search-forward "^</center><hr>" nil t)
402     (delete-region (point-min) (point))
403     (search-forward "<pre>" nil t)
404     (forward-line -1)
405     (let ((body (point-marker)))
406       (search-forward "</pre>" nil t)
407       (delete-region (point) (point-max))
408       (nnweb-remove-markup)
409       (goto-char (point-min))
410       (while (looking-at " *$")
411         (gnus-delete-line))
412       (narrow-to-region (point-min) body)
413       (while (and (re-search-forward "^$" nil t)
414                   (not (eobp)))
415         (gnus-delete-line))
416       (goto-char (point-min))
417       (while (looking-at "\\(^[^ ]+:\\) *")
418         (replace-match "\\1 " t)
419         (forward-line 1))
420       (goto-char (point-min))
421       (when (re-search-forward "^References:" nil t)
422         (narrow-to-region
423          (point) (if (re-search-forward "^$\\|^[^:]+:" nil t)
424                      (match-beginning 0)
425                    (point-max)))
426         (goto-char (point-min))
427         (while (not (eobp))
428           (unless (looking-at "References")
429             (insert "\t")
430             (forward-line 1)))
431         (goto-char (point-min))
432         (while (search-forward "," nil t)
433           (replace-match " " t t)))
434       (widen)
435       (set-marker body nil))))
436
437 (defun nnweb-reference-search (search)
438   (url-insert-file-contents
439    (concat 
440     (nnweb-definition 'address)
441     "?"
442     (nnweb-encode-www-form-urlencoded 
443      `(("search" . "advanced")
444        ("querytext" . ,search)
445        ("subj" . "")
446        ("name" . "")
447        ("login" . "")
448        ("host" . "")
449        ("organization" . "")
450        ("groups" . "")
451        ("keywords" . "")
452        ("choice" . "Search")
453        ("startmonth" . "Jul")
454        ("startday" . "25")
455        ("startyear" . "1996")
456        ("endmonth" . "Aug")
457        ("endday" . "24")
458        ("endyear" . "1996")
459        ("mode" . "Quick")
460        ("verbosity" . "Verbose")
461        ("ranking" . "Relevance")
462        ("first" . "1")
463        ("last" . "25")
464        ("score" . "50"))))))
465
466 ;;;
467 ;;; Alta Vista
468 ;;;
469
470 (defun nnweb-altavista-create-mapping ()
471   "Perform the search and create an number-to-url alist."
472   (save-excursion
473     (set-buffer nnweb-buffer)
474     (erase-buffer)
475     (let ((part 0))
476       (when (funcall (nnweb-definition 'search) nnweb-search part)
477         (let ((i 0)
478               (more t)
479               (case-fold-search t)
480               subject date from id group
481               map url)
482           (while more
483             ;; Go through all the article hits on this page.
484             (goto-char (point-min))
485             (search-forward "<dt>" nil t)
486             (delete-region (point-min) (match-beginning 0))
487             (goto-char (point-min))
488             (while (search-forward "<dt>" nil t)
489               (replace-match "\n<blubb>"))
490             (nnweb-decode-entities)
491             (goto-char (point-min))
492             (while (re-search-forward "<blubb>.*href=\"\\([^\"]+\\)\"><strong>\\([^>]*\\)</strong></a><dd>\\([^-]+\\)- <b>\\([^<]+\\)<.*href=\"news:\\([^\"]+\\)\">.*\">\\(.+\\)</a><P>"
493                                       nil t)
494               (setq url (match-string 1)
495                     subject (match-string 2)
496                     date (match-string 3)
497                     group (match-string 4)
498                     id (concat "<" (match-string 5) ">")
499                     from (match-string 6))
500               (push
501                (list
502                 (incf i)
503                 (make-full-mail-header
504                  i (concat  "(" group ") " subject) from date
505                  id nil 0 0 nil)
506                 url)
507                map))
508             ;; See if we want more.
509             (when (or (>= i nnweb-max-hits)
510                       (not (funcall (nnweb-definition 'search)
511                                     nnweb-search (incf part))))
512               (setq more nil)))
513           ;; Return the articles in the right order.
514           (setq nnweb-articles (nreverse map)))))))
515
516 (defun nnweb-altavista-wash-article ()
517   (goto-char (point-min))
518   (let ((case-fold-search t))
519     (when (re-search-forward "<H1>\\(.*\\)</H1>" nil t)
520       (setq subject (match-string 1)))
521     (re-search-forward "^<strong>" nil t)
522     (delete-region (point-min) (match-beginning 0))
523     (goto-char (point-min))
524     (while (looking-at "<strong>\\([^ ]+\\) +</strong> +\\(.*\\)$")
525       (replace-match "\\1: \\2" t)
526       (forward-line 1))
527     (when (re-search-backward "^References:" nil t)
528       (narrow-to-region (point) (progn (forward-line 1) (point)))
529       (goto-char (point-min))
530       (while (re-search-forward "<A.*\\?id@\\([^\"]+\\)\">[0-9]+</A>" nil t)
531         (replace-match "&lt;\\1&gt; " t))
532       (widen)
533       (nnweb-remove-markup))))
534
535 (defun nnweb-altavista-search (search &optional part)
536   (url-insert-file-contents
537    (concat 
538     (nnweb-definition 'address)
539     "?"
540     (nnweb-encode-www-form-urlencoded 
541      `(("pg" . "aq")
542        ("what" . "news")
543        ,@(if part `(("stq" . ,(int-to-string (* part 30)))))
544        ("fmt" . "d")
545        ("q" . ,search)
546        ("r" . "")
547        ("d0" . "")
548        ("d1" . ""))))))
549
550 (provide 'nnweb)
551
552 ;;; nnweb.el ends here