*** 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     (url-insert-file-contents url)
229     (setq buffer-file-name nil))
230   t)
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   t)
345
346 ;;;
347 ;;; InReference
348 ;;;
349
350 (defun nnweb-reference-create-mapping ()
351   "Perform the search and create an number-to-url alist."
352   (save-excursion
353     (set-buffer nnweb-buffer)
354     (erase-buffer)
355     (when (funcall (nnweb-definition 'search) nnweb-search)
356       (let ((i 0)
357             (more t)
358             (case-fold-search t)
359             Subject Score Date Newsgroups From Message-ID
360             map url)
361         (while more
362           ;; Go through all the article hits on this page.
363           (goto-char (point-min))
364           (search-forward "</pre><hr>" nil t)
365           (delete-region (point-min) (point))
366                                         ;(nnweb-decode-entities)
367           (goto-char (point-min))
368           (while (re-search-forward "^ +[0-9]+\\." nil t)
369             (narrow-to-region 
370              (point) 
371              (if (re-search-forward "^$" nil t)
372                  (match-beginning 0)
373                (point-max)))
374             (goto-char (point-min))
375             (when (looking-at ".*href=\"\\([^\"]+\\)\"")
376               (setq url (match-string 1)))
377             (nnweb-remove-markup)
378             (goto-char (point-min))
379             (while (search-forward "\t" nil t)
380               (replace-match " "))
381             (goto-char (point-min))
382             (while (re-search-forward "^\\([^:]+\\): \\(.*\\)$" nil t)
383               (set (intern (match-string 1)) (match-string 2)))
384             (widen)
385             (search-forward "</pre>" nil t)
386             (push
387              (list
388               (incf i)
389               (make-full-mail-header
390                i (concat  "(" Newsgroups ") " Subject) From Date
391                Message-ID
392                nil 0 (string-to-int Score) nil)
393               url)
394              map))
395           (setq more nil))
396         ;; Return the articles in the right order.
397         (setq nnweb-articles (nreverse map))))))
398
399 (defun nnweb-reference-wash-article ()
400   (let ((case-fold-search t))
401     (goto-char (point-min))
402     (re-search-forward "^</center><hr>" nil t)
403     (delete-region (point-min) (point))
404     (search-forward "<pre>" nil t)
405     (forward-line -1)
406     (let ((body (point-marker)))
407       (search-forward "</pre>" nil t)
408       (delete-region (point) (point-max))
409       (nnweb-remove-markup)
410       (goto-char (point-min))
411       (while (looking-at " *$")
412         (gnus-delete-line))
413       (narrow-to-region (point-min) body)
414       (while (and (re-search-forward "^$" nil t)
415                   (not (eobp)))
416         (gnus-delete-line))
417       (goto-char (point-min))
418       (while (looking-at "\\(^[^ ]+:\\) *")
419         (replace-match "\\1 " t)
420         (forward-line 1))
421       (goto-char (point-min))
422       (when (re-search-forward "^References:" nil t)
423         (narrow-to-region
424          (point) (if (re-search-forward "^$\\|^[^:]+:" nil t)
425                      (match-beginning 0)
426                    (point-max)))
427         (goto-char (point-min))
428         (while (not (eobp))
429           (unless (looking-at "References")
430             (insert "\t")
431             (forward-line 1)))
432         (goto-char (point-min))
433         (while (search-forward "," nil t)
434           (replace-match " " t t)))
435       (widen)
436       (set-marker body nil))))
437
438 (defun nnweb-reference-search (search)
439   (prog1
440       (url-insert-file-contents
441        (concat 
442         (nnweb-definition 'address)
443         "?"
444         (nnweb-encode-www-form-urlencoded 
445          `(("search" . "advanced")
446            ("querytext" . ,search)
447            ("subj" . "")
448            ("name" . "")
449            ("login" . "")
450            ("host" . "")
451            ("organization" . "")
452            ("groups" . "")
453            ("keywords" . "")
454            ("choice" . "Search")
455            ("startmonth" . "Jul")
456            ("startday" . "25")
457            ("startyear" . "1996")
458            ("endmonth" . "Aug")
459            ("endday" . "24")
460            ("endyear" . "1996")
461            ("mode" . "Quick")
462            ("verbosity" . "Verbose")
463            ("ranking" . "Relevance")
464            ("first" . "1")
465            ("last" . "25")
466            ("score" . "50")))))
467     (setq buffer-file-name nil))
468   t)
469
470 ;;;
471 ;;; Alta Vista
472 ;;;
473
474 (defun nnweb-altavista-create-mapping ()
475   "Perform the search and create an number-to-url alist."
476   (save-excursion
477     (set-buffer nnweb-buffer)
478     (erase-buffer)
479     (let ((part 0))
480       (when (funcall (nnweb-definition 'search) nnweb-search part)
481         (let ((i 0)
482               (more t)
483               (case-fold-search t)
484               subject date from id group
485               map url)
486           (while more
487             ;; Go through all the article hits on this page.
488             (goto-char (point-min))
489             (search-forward "<dt>" nil t)
490             (delete-region (point-min) (match-beginning 0))
491             (goto-char (point-min))
492             (while (search-forward "<dt>" nil t)
493               (replace-match "\n<blubb>"))
494             (nnweb-decode-entities)
495             (goto-char (point-min))
496             (while (re-search-forward "<blubb>.*href=\"\\([^\"]+\\)\"><strong>\\([^>]*\\)</strong></a><dd>\\([^-]+\\)- <b>\\([^<]+\\)<.*href=\"news:\\([^\"]+\\)\">.*\">\\(.+\\)</a><P>"
497                                       nil t)
498               (setq url (match-string 1)
499                     subject (match-string 2)
500                     date (match-string 3)
501                     group (match-string 4)
502                     id (concat "<" (match-string 5) ">")
503                     from (match-string 6))
504               (push
505                (list
506                 (incf i)
507                 (make-full-mail-header
508                  i (concat  "(" group ") " subject) from date
509                  id nil 0 0 nil)
510                 url)
511                map))
512             ;; See if we want more.
513             (when (or (not nnweb-articles)
514                       (>= i nnweb-max-hits)
515                       (not (funcall (nnweb-definition 'search)
516                                     nnweb-search (incf part))))
517               (setq more nil)))
518           ;; Return the articles in the right order.
519           (setq nnweb-articles (nreverse map)))))))
520
521 (defun nnweb-altavista-wash-article ()
522   (goto-char (point-min))
523   (let ((case-fold-search t))
524     (when (re-search-forward "<H1>\\(.*\\)</H1>" nil t)
525       (setq subject (match-string 1)))
526     (re-search-forward "^<strong>" nil t)
527     (delete-region (point-min) (match-beginning 0))
528     (goto-char (point-min))
529     (while (looking-at "<strong>\\([^ ]+\\) +</strong> +\\(.*\\)$")
530       (replace-match "\\1: \\2" t)
531       (forward-line 1))
532     (when (re-search-backward "^References:" nil t)
533       (narrow-to-region (point) (progn (forward-line 1) (point)))
534       (goto-char (point-min))
535       (while (re-search-forward "<A.*\\?id@\\([^\"]+\\)\">[0-9]+</A>" nil t)
536         (replace-match "&lt;\\1&gt; " t))
537       (widen)
538       (nnweb-remove-markup))))
539
540 (defun nnweb-altavista-search (search &optional part)
541   (prog1
542       (url-insert-file-contents
543        (concat 
544         (nnweb-definition 'address)
545         "?"
546         (nnweb-encode-www-form-urlencoded 
547          `(("pg" . "aq")
548            ("what" . "news")
549            ,@(if part `(("stq" . ,(int-to-string (* part 30)))))
550            ("fmt" . "d")
551            ("q" . ,search)
552            ("r" . "")
553            ("d0" . "")
554            ("d1" . "")))))
555     (setq buffer-file-name nil))
556   )t
557
558 (provide 'nnweb)
559
560 ;;; nnweb.el ends here