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