5a673cd4b4ac88c9214a6b64ddfd951209ebb202
[gnus] / lisp / nnweb.el
1 ;;; nnweb.el --- retrieving articles via web search engines
2 ;; Copyright (C) 1996,97,98 Free Software Foundation, Inc.
3
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
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 (eval-when-compile (require 'cl))
32
33 (require 'nnoo)
34 (require 'message)
35 (require 'gnus-util)
36 (require 'gnus)
37 (require 'w3)
38 (require 'url)
39 (require 'nnmail)
40 (ignore-errors
41   (require 'w3-forms))
42
43 (nnoo-declare nnweb)
44
45 (defvoo nnweb-directory (nnheader-concat gnus-directory "nnweb/")
46   "Where nnweb will save its files.")
47
48 (defvoo nnweb-type 'dejanews
49   "What search engine type is being used.
50 Valid types include `dejanews', `dejanewsold', `reference',
51 and `altavista'.")
52
53 (defvoo nnweb-type-definition
54   '((dejanews
55      (article . nnweb-dejanews-wash-article)
56      (map . nnweb-dejanews-create-mapping)
57      (search . nnweb-dejanews-search)
58      (address . "http://x8.dejanews.com/dnquery.xp")
59      (identifier . nnweb-dejanews-identity))
60     (dejanewsold
61      (article . nnweb-dejanews-wash-article)
62      (map . nnweb-dejanews-create-mapping)
63      (search . nnweb-dejanewsold-search)
64      (address . "http://x8.dejanews.com/dnquery.xp")
65      (identifier . nnweb-dejanews-identity))
66     (reference
67      (article . nnweb-reference-wash-article)
68      (map . nnweb-reference-create-mapping)
69      (search . nnweb-reference-search)
70      (address . "http://www.reference.com/cgi-bin/pn/go")
71      (identifier . identity))
72     (altavista
73      (article . nnweb-altavista-wash-article)
74      (map . nnweb-altavista-create-mapping)
75      (search . nnweb-altavista-search)
76      (address . "http://www.altavista.digital.com/cgi-bin/query")
77      (id . "/cgi-bin/news?id@%s")
78      (identifier . identity)))
79   "Type-definition alist.")
80
81 (defvoo nnweb-search nil
82   "Search string to feed to DejaNews.")
83
84 (defvoo nnweb-max-hits 999
85   "Maximum number of hits to display.")
86
87 (defvoo nnweb-ephemeral-p nil
88   "Whether this nnweb server is ephemeral.")
89
90 ;;; Internal variables
91
92 (defvoo nnweb-articles nil)
93 (defvoo nnweb-buffer nil)
94 (defvoo nnweb-group-alist nil)
95 (defvoo nnweb-group nil)
96 (defvoo nnweb-hashtb nil)
97
98 ;;; Interface functions
99
100 (nnoo-define-basics nnweb)
101
102 (deffoo nnweb-retrieve-headers (articles &optional group server fetch-old)
103   (nnweb-possibly-change-server group server)
104   (save-excursion
105     (set-buffer nntp-server-buffer)
106     (erase-buffer)
107     (let (article header)
108       (while (setq article (pop articles))
109         (when (setq header (cadr (assq article nnweb-articles)))
110           (nnheader-insert-nov header)))
111       'nov)))
112
113 (deffoo nnweb-request-scan (&optional group server)
114   (nnweb-possibly-change-server group server)
115   (setq nnweb-hashtb (gnus-make-hashtable 4095))
116   (funcall (nnweb-definition 'map))
117   (unless nnweb-ephemeral-p
118     (nnweb-write-active)
119     (nnweb-write-overview group)))
120
121 (deffoo nnweb-request-group (group &optional server dont-check)
122   (nnweb-possibly-change-server nil server)
123   (when (and group
124              (not (equal group nnweb-group))
125              (not nnweb-ephemeral-p))
126     (let ((info (assoc group nnweb-group-alist)))
127       (setq nnweb-group group)
128       (setq nnweb-type (nth 2 info))
129       (setq nnweb-search (nth 3 info))
130       (unless dont-check
131         (nnweb-read-overview group))))
132   (cond
133    ((not nnweb-articles)
134     (nnheader-report 'nnweb "No matching articles"))
135    (t
136     (let ((active (if nnweb-ephemeral-p
137                       (cons (caar nnweb-articles)
138                             (caar (last nnweb-articles)))
139                     (cadr (assoc group nnweb-group-alist)))))
140       (nnheader-report 'nnweb "Opened group %s" group)
141       (nnheader-insert
142        "211 %d %d %d %s\n" (length nnweb-articles)
143        (car active) (cdr active) group)))))
144
145 (deffoo nnweb-close-group (group &optional server)
146   (nnweb-possibly-change-server group server)
147   (when (gnus-buffer-live-p nnweb-buffer)
148     (save-excursion
149       (set-buffer nnweb-buffer)
150       (set-buffer-modified-p nil)
151       (kill-buffer nnweb-buffer)))
152   t)
153
154 (deffoo nnweb-request-article (article &optional group server buffer)
155   (nnweb-possibly-change-server group server)
156   (save-excursion
157     (set-buffer (or buffer nntp-server-buffer))
158     (let* ((header (cadr (assq article nnweb-articles)))
159            (url (and header (mail-header-xref header))))
160       (when (or (and url
161                      (nnweb-fetch-url url))
162                 (and (stringp article)
163                      (nnweb-definition 'id t)
164                      (let ((fetch (nnweb-definition 'id))
165                            art)
166                        (when (string-match "^<\\(.*\\)>$" article)
167                          (setq art (match-string 1 article)))
168                        (and fetch
169                             art
170                             (nnweb-fetch-url
171                              (format fetch article))))))
172         (unless nnheader-callback-function
173           (funcall (nnweb-definition 'article))
174           (nnweb-decode-entities))
175         (nnheader-report 'nnweb "Fetched article %s" article)
176         t))))
177
178 (deffoo nnweb-close-server (&optional server)
179   (when (and (nnweb-server-opened server)
180              (gnus-buffer-live-p nnweb-buffer))
181     (save-excursion
182       (set-buffer nnweb-buffer)
183       (set-buffer-modified-p nil)
184       (kill-buffer nnweb-buffer)))
185   (nnoo-close-server 'nnweb server))
186
187 (deffoo nnweb-request-list (&optional server)
188   (nnweb-possibly-change-server nil server)
189   (save-excursion
190     (set-buffer nntp-server-buffer)
191     (nnmail-generate-active nnweb-group-alist)
192     t))
193
194 (deffoo nnweb-request-update-info (group info &optional server)
195   (nnweb-possibly-change-server group server)
196   ;;(setcar (cddr info) nil)
197   )
198
199 (deffoo nnweb-asynchronous-p ()
200   t)
201
202 (deffoo nnweb-request-create-group (group &optional server args)
203   (nnweb-possibly-change-server nil server)
204   (nnweb-request-delete-group group)
205   (push `(,group ,(cons 1 0) ,@args) nnweb-group-alist)
206   (nnweb-write-active)
207   t)
208
209 (deffoo nnweb-request-delete-group (group &optional force server)
210   (nnweb-possibly-change-server group server)
211   (gnus-pull group nnweb-group-alist)
212   (gnus-delete-file (nnweb-overview-file group))
213   t)
214
215 (nnoo-define-skeleton nnweb)
216
217 ;;; Internal functions
218
219 (defun nnweb-read-overview (group)
220   "Read the overview of GROUP and build the map."
221   (when (file-exists-p (nnweb-overview-file group))
222     (nnheader-temp-write nil
223       (nnheader-insert-file-contents (nnweb-overview-file group))
224       (goto-char (point-min))
225       (let (header)
226         (while (not (eobp))
227           (setq header (nnheader-parse-nov))
228           (forward-line 1)
229           (push (list (mail-header-number header)
230                       header (mail-header-xref header))
231                 nnweb-articles)
232           (nnweb-set-hashtb header (car nnweb-articles)))))))
233
234 (defun nnweb-write-overview (group)
235   "Write the overview file for GROUP."
236   (nnheader-temp-write (nnweb-overview-file group)
237     (let ((articles nnweb-articles))
238       (while articles
239         (nnheader-insert-nov (cadr (pop articles)))))))
240
241 (defun nnweb-set-hashtb (header data)
242   (gnus-sethash (nnweb-identifier (mail-header-xref header))
243                 data nnweb-hashtb))
244
245 (defun nnweb-get-hashtb (url)
246   (gnus-gethash (nnweb-identifier url) nnweb-hashtb))
247
248 (defun nnweb-identifier (ident)
249   (funcall (nnweb-definition 'identifier) ident))
250
251 (defun nnweb-overview-file (group)
252   "Return the name of the overview file of GROUP."
253   (nnheader-concat nnweb-directory group ".overview"))
254
255 (defun nnweb-write-active ()
256   "Save the active file."
257   (nnheader-temp-write (nnheader-concat nnweb-directory "active")
258     (prin1 `(setq nnweb-group-alist ',nnweb-group-alist) (current-buffer))))
259
260 (defun nnweb-read-active ()
261   "Read the active file."
262   (load (nnheader-concat nnweb-directory "active") t t t))
263
264 (defun nnweb-definition (type &optional noerror)
265   "Return the definition of TYPE."
266   (let ((def (cdr (assq type (assq nnweb-type nnweb-type-definition)))))
267     (when (and (not def)
268                (not noerror))
269       (error "Undefined definition %s" type))
270     def))
271
272 (defun nnweb-possibly-change-server (&optional group server)
273   (nnweb-init server)
274   (when server
275     (unless (nnweb-server-opened server)
276       (nnweb-open-server server)))
277   (unless nnweb-group-alist
278     (nnweb-read-active))
279   (when group
280     (when (and (not nnweb-ephemeral-p)
281                (not (equal group nnweb-group)))
282       (nnweb-request-group group nil t))))
283
284 (defun nnweb-init (server)
285   "Initialize buffers and such."
286   (unless (gnus-buffer-live-p nnweb-buffer)
287     (setq nnweb-buffer
288           (save-excursion
289             (nnheader-set-temp-buffer
290              (format " *nnweb %s %s %s*" nnweb-type nnweb-search server))))))
291
292 (defun nnweb-fetch-url (url)
293   (save-excursion
294     (if (not nnheader-callback-function)
295         (let ((buf (current-buffer)))
296           (save-excursion
297             (set-buffer nnweb-buffer)
298             (erase-buffer)
299             (url-insert-file-contents url)
300             (copy-to-buffer buf (point-min) (point-max))
301             t))
302       (nnweb-url-retrieve-asynch
303        url 'nnweb-callback (current-buffer) nnheader-callback-function)
304       t)))
305
306 (defun nnweb-callback (buffer callback)
307   (when (gnus-buffer-live-p url-working-buffer)
308     (save-excursion
309       (set-buffer url-working-buffer)
310       (funcall (nnweb-definition 'article))
311       (nnweb-decode-entities)
312       (set-buffer buffer)
313       (goto-char (point-max))
314       (insert-buffer-substring url-working-buffer))
315     (funcall callback t)
316     (gnus-kill-buffer url-working-buffer)))
317
318 (defun nnweb-url-retrieve-asynch (url callback &rest data)
319   (let ((url-request-method "GET")
320         (old-asynch url-be-asynchronous)
321         (url-request-data nil)
322         (url-request-extra-headers nil)
323         (url-working-buffer (generate-new-buffer-name " *nnweb*")))
324     (setq-default url-be-asynchronous t)
325     (save-excursion
326       (set-buffer (get-buffer-create url-working-buffer))
327       (setq url-current-callback-data data
328             url-be-asynchronous t
329             url-current-callback-func callback)
330       (url-retrieve url))
331     (setq-default url-be-asynchronous old-asynch)))
332
333 (defun nnweb-encode-www-form-urlencoded (pairs)
334   "Return PAIRS encoded for forms."
335   (mapconcat
336    (function
337     (lambda (data)
338       (concat (w3-form-encode-xwfu (car data)) "="
339               (w3-form-encode-xwfu (cdr data)))))
340    pairs "&"))
341
342 (defun nnweb-fetch-form (url pairs)
343   (let ((url-request-data (nnweb-encode-www-form-urlencoded pairs))
344         (url-request-method "POST")
345         (url-request-extra-headers
346          '(("Content-type" . "application/x-www-form-urlencoded"))))
347     (url-insert-file-contents url)
348     (setq buffer-file-name nil))
349   t)
350
351 (defun nnweb-decode-entities ()
352   (goto-char (point-min))
353   (while (re-search-forward "&\\([a-z]+\\);" nil t)
354     (replace-match (char-to-string (or (cdr (assq (intern (match-string 1))
355                                                   w3-html-entities))
356                                        ?#))
357                    t t)))
358
359 (defun nnweb-remove-markup ()
360   (goto-char (point-min))
361   (while (search-forward "<!--" nil t)
362     (delete-region (match-beginning 0)
363                    (or (search-forward "-->" nil t)
364                        (point-max))))
365   (goto-char (point-min))
366   (while (re-search-forward "<[^>]+>" nil t)
367     (replace-match "" t t)))
368
369 ;;;
370 ;;; DejaNews functions.
371 ;;;
372
373 (defun nnweb-dejanews-create-mapping ()
374   "Perform the search and create an number-to-url alist."
375   (save-excursion
376     (set-buffer nnweb-buffer)
377     (erase-buffer)
378     (when (funcall (nnweb-definition 'search) nnweb-search)
379       (let ((i 0)
380             (more t)
381             (case-fold-search t)
382             (active (or (cadr (assoc nnweb-group nnweb-group-alist))
383                         (cons 1 0)))
384             Subject (Score "0") Date Newsgroup Author
385             map url)
386         (while more
387           ;; Go through all the article hits on this page.
388           (goto-char (point-min))
389           (nnweb-decode-entities)
390           (goto-char (point-min))
391           (while (re-search-forward "^ <P>\n" nil t)
392             (narrow-to-region
393              (point)
394              (cond ((re-search-forward "^ <P>\n" nil t)
395                     (match-beginning 0))
396                    ((search-forward "\n\n" nil t)
397                     (point))
398                    (t
399                     (point-max))))
400             (goto-char (point-min))
401             (looking-at ".*HREF=\"\\([^\"]+\\)\"\\(.*\\)")
402             (setq url (match-string 1))
403             (let ((begin (point)))
404               (nnweb-remove-markup)
405               (goto-char begin)
406               (while (search-forward "\t" nil t)
407                 (replace-match " "))
408               (goto-char begin)
409               (end-of-line)
410               (setq Subject (buffer-substring begin (point)))
411               (if (re-search-forward
412                    "^ Newsgroup: \\(.*\\)\n Posted on \\([0-9/]+\\) by \\(.*\\)$" nil t)
413                   (setq Newsgroup (match-string 1)
414                         Date (match-string 2)
415                         Author (match-string 3))))
416             (widen)
417             (incf i)
418             (unless (nnweb-get-hashtb url)
419               (push
420                (list
421                 (incf (cdr active))
422                 (make-full-mail-header
423                  (cdr active) Subject Author Date
424                  (concat "<" (nnweb-identifier url) "@dejanews>")
425                  nil 0 (string-to-int Score) url))
426                map)
427               (nnweb-set-hashtb (cadar map) (car map))))
428           ;; See whether there is a "Get next 20 hits" button here.
429           (if (or (not (re-search-forward
430                         "HREF=\"\\([^\"]+\\)\"[<>b]+Next result" nil t))
431                   (>= i nnweb-max-hits))
432               (setq more nil)
433             ;; Yup -- fetch it.
434             (setq more (match-string 1))
435             (erase-buffer)
436             (url-insert-file-contents more)))
437         ;; Return the articles in the right order.
438         (setq nnweb-articles
439               (sort (nconc nnweb-articles map) 'car-less-than-car))))))
440
441 (defun nnweb-dejanews-wash-article ()
442   (let ((case-fold-search t))
443     (goto-char (point-min))
444     (re-search-forward "<PRE>" nil t)
445     (delete-region (point-min) (point))
446     (re-search-forward "</PRE>" nil t)
447     (delete-region (point) (point-max))
448     (nnweb-remove-markup)
449     (goto-char (point-min))
450     (while (and (looking-at " *$")
451                 (not (eobp)))
452       (gnus-delete-line))
453     (while (looking-at "\\(^[^ ]+:\\) *")
454       (replace-match "\\1 " t)
455       (forward-line 1))
456     (when (re-search-forward "\n\n+" nil t)
457       (replace-match "\n" t t))
458     (goto-char (point-min))
459     (when (search-forward "[More Headers]" nil t)
460       (replace-match "" t t))))
461
462 (defun nnweb-dejanews-search (search)
463   (nnweb-fetch-form
464    (nnweb-definition 'address)
465    `(("query" . ,search)
466      ("defaultOp" . "AND")
467      ("svcclass" . "dncurrent")
468      ("maxhits" . "100")
469      ("format" . "verbose2")
470      ("threaded" . "0")
471      ("showsort" . "date")
472      ("agesign" . "1")
473      ("ageweight" . "1")))
474   t)
475
476 (defun nnweb-dejanewsold-search (search)
477   (nnweb-fetch-form
478    (nnweb-definition 'address)
479    `(("query" . ,search)
480      ("defaultOp" . "AND")
481      ("svcclass" . "dnold")
482      ("maxhits" . "100")
483      ("format" . "verbose2")
484      ("threaded" . "0")
485      ("showsort" . "date")
486      ("agesign" . "1")
487      ("ageweight" . "1")))
488   t)
489
490 (defun nnweb-dejanews-identity (url)
491   "Return an unique identifier based on URL."
492   (if (string-match "recnum=\\([0-9]+\\)" url)
493       (match-string 1 url)
494     url))
495
496 ;;;
497 ;;; InReference
498 ;;;
499
500 (defun nnweb-reference-create-mapping ()
501   "Perform the search and create an number-to-url alist."
502   (save-excursion
503     (set-buffer nnweb-buffer)
504     (erase-buffer)
505     (when (funcall (nnweb-definition 'search) nnweb-search)
506       (let ((i 0)
507             (more t)
508             (case-fold-search t)
509             (active (or (cadr (assoc nnweb-group nnweb-group-alist))
510                         (cons 1 0)))
511             Subject Score Date Newsgroups From Message-ID
512             map url)
513         (while more
514           ;; Go through all the article hits on this page.
515           (goto-char (point-min))
516           (search-forward "</pre><hr>" nil t)
517           (delete-region (point-min) (point))
518                                         ;(nnweb-decode-entities)
519           (goto-char (point-min))
520           (while (re-search-forward "^ +[0-9]+\\." nil t)
521             (narrow-to-region
522              (point)
523              (if (re-search-forward "^$" nil t)
524                  (match-beginning 0)
525                (point-max)))
526             (goto-char (point-min))
527             (when (looking-at ".*href=\"\\([^\"]+\\)\"")
528               (setq url (match-string 1)))
529             (nnweb-remove-markup)
530             (goto-char (point-min))
531             (while (search-forward "\t" nil t)
532               (replace-match " "))
533             (goto-char (point-min))
534             (while (re-search-forward "^\\([^:]+\\): \\(.*\\)$" nil t)
535               (set (intern (match-string 1)) (match-string 2)))
536             (widen)
537             (search-forward "</pre>" nil t)
538             (incf i)
539             (unless (nnweb-get-hashtb url)
540               (push
541                (list
542                 (incf (cdr active))
543                 (make-full-mail-header
544                  (cdr active) (concat  "(" Newsgroups ") " Subject) From Date
545                  Message-ID
546                  nil 0 (string-to-int Score) url))
547                map)
548               (nnweb-set-hashtb (cadar map) (car map))))
549           (setq more nil))
550         ;; Return the articles in the right order.
551         (setq nnweb-articles
552               (sort (nconc nnweb-articles map) 'car-less-than-car))))))
553
554 (defun nnweb-reference-wash-article ()
555   (let ((case-fold-search t))
556     (goto-char (point-min))
557     (re-search-forward "^</center><hr>" nil t)
558     (delete-region (point-min) (point))
559     (search-forward "<pre>" nil t)
560     (forward-line -1)
561     (let ((body (point-marker)))
562       (search-forward "</pre>" nil t)
563       (delete-region (point) (point-max))
564       (nnweb-remove-markup)
565       (goto-char (point-min))
566       (while (looking-at " *$")
567         (gnus-delete-line))
568       (narrow-to-region (point-min) body)
569       (while (and (re-search-forward "^$" nil t)
570                   (not (eobp)))
571         (gnus-delete-line))
572       (goto-char (point-min))
573       (while (looking-at "\\(^[^ ]+:\\) *")
574         (replace-match "\\1 " t)
575         (forward-line 1))
576       (goto-char (point-min))
577       (when (re-search-forward "^References:" nil t)
578         (narrow-to-region
579          (point) (if (re-search-forward "^$\\|^[^:]+:" nil t)
580                      (match-beginning 0)
581                    (point-max)))
582         (goto-char (point-min))
583         (while (not (eobp))
584           (unless (looking-at "References")
585             (insert "\t")
586             (forward-line 1)))
587         (goto-char (point-min))
588         (while (search-forward "," nil t)
589           (replace-match " " t t)))
590       (widen)
591       (set-marker body nil))))
592
593 (defun nnweb-reference-search (search)
594   (url-insert-file-contents
595    (concat
596     (nnweb-definition 'address)
597     "?"
598     (nnweb-encode-www-form-urlencoded
599      `(("search" . "advanced")
600        ("querytext" . ,search)
601        ("subj" . "")
602        ("name" . "")
603        ("login" . "")
604        ("host" . "")
605        ("organization" . "")
606        ("groups" . "")
607        ("keywords" . "")
608        ("choice" . "Search")
609        ("startmonth" . "Jul")
610        ("startday" . "25")
611        ("startyear" . "1996")
612        ("endmonth" . "Aug")
613        ("endday" . "24")
614        ("endyear" . "1996")
615        ("mode" . "Quick")
616        ("verbosity" . "Verbose")
617        ("ranking" . "Relevance")
618        ("first" . "1")
619        ("last" . "25")
620        ("score" . "50")))))
621   (setq buffer-file-name nil)
622   t)
623
624 ;;;
625 ;;; Alta Vista
626 ;;;
627
628 (defun nnweb-altavista-create-mapping ()
629   "Perform the search and create an number-to-url alist."
630   (save-excursion
631     (set-buffer nnweb-buffer)
632     (erase-buffer)
633     (let ((part 0))
634       (when (funcall (nnweb-definition 'search) nnweb-search part)
635         (let ((i 0)
636               (more t)
637               (case-fold-search t)
638               (active (or (cadr (assoc nnweb-group nnweb-group-alist))
639                           (cons 1 0)))
640               subject date from id group
641               map url)
642           (while more
643             ;; Go through all the article hits on this page.
644             (goto-char (point-min))
645             (search-forward "<dt>" nil t)
646             (delete-region (point-min) (match-beginning 0))
647             (goto-char (point-min))
648             (while (search-forward "<dt>" nil t)
649               (replace-match "\n<blubb>"))
650             (nnweb-decode-entities)
651             (goto-char (point-min))
652             (while (re-search-forward "<blubb>.*href=\"\\([^\"]+\\)\"><strong>\\([^>]*\\)</strong></a><dd>\\([^-]+\\)- <b>\\([^<]+\\)<.*href=\"news:\\([^\"]+\\)\">.*\">\\(.+\\)</a><P>"
653                                       nil t)
654               (setq url (match-string 1)
655                     subject (match-string 2)
656                     date (match-string 3)
657                     group (match-string 4)
658                     id (concat "<" (match-string 5) ">")
659                     from (match-string 6))
660               (incf i)
661               (unless (nnweb-get-hashtb url)
662                 (push
663                  (list
664                   (incf (cdr active))
665                   (make-full-mail-header
666                    (cdr active) (concat  "(" group ") " subject) from date
667                    id nil 0 0 url))
668                  map)
669                 (nnweb-set-hashtb (cadar map) (car map))))
670             ;; See if we want more.
671             (when (or (not nnweb-articles)
672                       (>= i nnweb-max-hits)
673                       (not (funcall (nnweb-definition 'search)
674                                     nnweb-search (incf part))))
675               (setq more nil)))
676           ;; Return the articles in the right order.
677           (setq nnweb-articles
678                 (sort (nconc nnweb-articles map) 'car-less-than-car)))))))
679
680 (defun nnweb-altavista-wash-article ()
681   (goto-char (point-min))
682   (let ((case-fold-search t))
683     (when (re-search-forward "^<strong>" nil t)
684       (delete-region (point-min) (match-beginning 0)))
685     (goto-char (point-min))
686     (while (looking-at "<strong>\\([^ ]+\\) +</strong> +\\(.*\\)$")
687       (replace-match "\\1: \\2" t)
688       (forward-line 1))
689     (when (re-search-backward "^References:" nil t)
690       (narrow-to-region (point) (progn (forward-line 1) (point)))
691       (goto-char (point-min))
692       (while (re-search-forward "<A.*\\?id@\\([^\"]+\\)\">[0-9]+</A>" nil t)
693         (replace-match "&lt;\\1&gt; " t)))
694     (widen)
695     (nnweb-remove-markup)))
696
697 (defun nnweb-altavista-search (search &optional part)
698   (url-insert-file-contents
699    (concat
700     (nnweb-definition 'address)
701     "?"
702     (nnweb-encode-www-form-urlencoded
703      `(("pg" . "aq")
704        ("what" . "news")
705        ,@(when part `(("stq" . ,(int-to-string (* part 30)))))
706        ("fmt" . "d")
707        ("q" . ,search)
708        ("r" . "")
709        ("d0" . "")
710        ("d1" . "")))))
711   (setq buffer-file-name nil)
712   t)
713
714 (provide 'nnweb)
715
716 ;;; nnweb.el ends here