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