Importing Gnus v5.8.3.
[elisp/gnus.git-] / lisp / nnweb.el
1 ;;; nnweb.el --- retrieving articles via web search engines
2 ;; Copyright (C) 1996,97,98,99 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   (setq nnweb-hashtb (gnus-make-hashtable 4095))
125   (funcall (nnweb-definition 'map))
126   (unless nnweb-ephemeral-p
127     (nnweb-write-active)
128     (nnweb-write-overview group)))
129
130 (deffoo nnweb-request-group (group &optional server dont-check)
131   (nnweb-possibly-change-server nil server)
132   (when (and group
133              (not (equal group nnweb-group))
134              (not nnweb-ephemeral-p))
135     (let ((info (assoc group nnweb-group-alist)))
136       (when info
137         (setq nnweb-group group)
138         (setq nnweb-type (nth 2 info))
139         (setq nnweb-search (nth 3 info))
140         (unless dont-check
141           (nnweb-read-overview group)))))
142   (unless dont-check
143     (nnweb-request-scan group))
144   (cond
145    ((not nnweb-articles)
146     (nnheader-report 'nnweb "No matching articles"))
147    (t
148     (let ((active (if nnweb-ephemeral-p
149                       (cons (caar nnweb-articles)
150                             (caar (last nnweb-articles)))
151                     (cadr (assoc group nnweb-group-alist)))))
152       (nnheader-report 'nnweb "Opened group %s" group)
153       (nnheader-insert
154        "211 %d %d %d %s\n" (length nnweb-articles)
155        (car active) (cdr active) group)))))
156
157 (deffoo nnweb-close-group (group &optional server)
158   (nnweb-possibly-change-server group server)
159   (when (gnus-buffer-live-p nnweb-buffer)
160     (save-excursion
161       (set-buffer nnweb-buffer)
162       (set-buffer-modified-p nil)
163       (kill-buffer nnweb-buffer)))
164   t)
165
166 (deffoo nnweb-request-article (article &optional group server buffer)
167   (nnweb-possibly-change-server group server)
168   (save-excursion
169     (set-buffer (or buffer nntp-server-buffer))
170     (let* ((header (cadr (assq article nnweb-articles)))
171            (url (and header (mail-header-xref header))))
172       (when (or (and url
173                      (mm-with-unibyte-current-buffer
174                        (nnweb-fetch-url url)))
175                 (and (stringp article)
176                      (nnweb-definition 'id t)
177                      (let ((fetch (nnweb-definition 'id))
178                            art)
179                        (when (string-match "^<\\(.*\\)>$" article)
180                          (setq art (match-string 1 article)))
181                        (and fetch
182                             art
183                             (mm-with-unibyte-current-buffer
184                               (nnweb-fetch-url
185                                (format fetch article)))))))
186         (unless nnheader-callback-function
187           (funcall (nnweb-definition 'article))
188           (nnweb-decode-entities))
189         (nnheader-report 'nnweb "Fetched article %s" article)
190         (cons group (and (numberp article) article))))))
191
192 (deffoo nnweb-close-server (&optional server)
193   (when (and (nnweb-server-opened server)
194              (gnus-buffer-live-p nnweb-buffer))
195     (save-excursion
196       (set-buffer nnweb-buffer)
197       (set-buffer-modified-p nil)
198       (kill-buffer nnweb-buffer)))
199   (nnoo-close-server 'nnweb server))
200
201 (deffoo nnweb-request-list (&optional server)
202   (nnweb-possibly-change-server nil server)
203   (save-excursion
204     (set-buffer nntp-server-buffer)
205     (nnmail-generate-active nnweb-group-alist)
206     t))
207
208 (deffoo nnweb-request-update-info (group info &optional server)
209   (nnweb-possibly-change-server group server))
210
211 (deffoo nnweb-asynchronous-p ()
212   t)
213
214 (deffoo nnweb-request-create-group (group &optional server args)
215   (nnweb-possibly-change-server nil server)
216   (nnweb-request-delete-group group)
217   (push `(,group ,(cons 1 0) ,@args) nnweb-group-alist)
218   (nnweb-write-active)
219   t)
220
221 (deffoo nnweb-request-delete-group (group &optional force server)
222   (nnweb-possibly-change-server group server)
223   (gnus-pull group nnweb-group-alist t)
224   (nnweb-write-active)
225   (gnus-delete-file (nnweb-overview-file group))
226   t)
227
228 (nnoo-define-skeleton nnweb)
229
230 ;;; Internal functions
231
232 (defun nnweb-read-overview (group)
233   "Read the overview of GROUP and build the map."
234   (when (file-exists-p (nnweb-overview-file group))
235     (mm-with-unibyte-buffer
236       (nnheader-insert-file-contents (nnweb-overview-file group))
237       (goto-char (point-min))
238       (let (header)
239         (while (not (eobp))
240           (setq header (nnheader-parse-nov))
241           (forward-line 1)
242           (push (list (mail-header-number header)
243                       header (mail-header-xref header))
244                 nnweb-articles)
245           (nnweb-set-hashtb header (car nnweb-articles)))))))
246
247 (defun nnweb-write-overview (group)
248   "Write the overview file for GROUP."
249   (with-temp-file (nnweb-overview-file group)
250     (let ((articles nnweb-articles))
251       (while articles
252         (nnheader-insert-nov (cadr (pop articles)))))))
253
254 (defun nnweb-set-hashtb (header data)
255   (gnus-sethash (nnweb-identifier (mail-header-xref header))
256                 data nnweb-hashtb))
257
258 (defun nnweb-get-hashtb (url)
259   (gnus-gethash (nnweb-identifier url) nnweb-hashtb))
260
261 (defun nnweb-identifier (ident)
262   (funcall (nnweb-definition 'identifier) ident))
263
264 (defun nnweb-overview-file (group)
265   "Return the name of the overview file of GROUP."
266   (nnheader-concat nnweb-directory group ".overview"))
267
268 (defun nnweb-write-active ()
269   "Save the active file."
270   (gnus-make-directory nnweb-directory)
271   (with-temp-file (nnheader-concat nnweb-directory "active")
272     (prin1 `(setq nnweb-group-alist ',nnweb-group-alist) (current-buffer))))
273
274 (defun nnweb-read-active ()
275   "Read the active file."
276   (load (nnheader-concat nnweb-directory "active") t t t))
277
278 (defun nnweb-definition (type &optional noerror)
279   "Return the definition of TYPE."
280   (let ((def (cdr (assq type (assq nnweb-type nnweb-type-definition)))))
281     (when (and (not def)
282                (not noerror))
283       (error "Undefined definition %s" type))
284     def))
285
286 (defun nnweb-possibly-change-server (&optional group server)
287   (nnweb-init server)
288   (when server
289     (unless (nnweb-server-opened server)
290       (nnweb-open-server server)))
291   (unless nnweb-group-alist
292     (nnweb-read-active))
293   (when group
294     (when (and (not nnweb-ephemeral-p)
295                (not (equal group nnweb-group)))
296       (nnweb-request-group group nil t))))
297
298 (defun nnweb-init (server)
299   "Initialize buffers and such."
300   (unless (gnus-buffer-live-p nnweb-buffer)
301     (setq nnweb-buffer
302           (save-excursion
303             (mm-with-unibyte
304               (nnheader-set-temp-buffer
305                (format " *nnweb %s %s %s*"
306                        nnweb-type nnweb-search server))
307               (current-buffer))))))
308
309 (defun nnweb-fetch-url (url)
310   (let (buf)
311     (save-excursion
312       (if (not nnheader-callback-function)
313           (progn
314             (with-temp-buffer
315               (mm-enable-multibyte)
316               (let ((coding-system-for-read 'binary)
317                     (coding-system-for-write 'binary)
318                     (default-process-coding-system 'binary))
319                 (nnweb-insert url))
320               (setq buf (buffer-string)))
321             (erase-buffer)
322             (insert buf)
323             t)
324         (nnweb-url-retrieve-asynch
325          url 'nnweb-callback (current-buffer) nnheader-callback-function)
326         t))))
327
328 (defun nnweb-callback (buffer callback)
329   (when (gnus-buffer-live-p url-working-buffer)
330     (save-excursion
331       (set-buffer url-working-buffer)
332       (funcall (nnweb-definition 'article))
333       (nnweb-decode-entities)
334       (set-buffer buffer)
335       (goto-char (point-max))
336       (insert-buffer-substring url-working-buffer))
337     (funcall callback t)
338     (gnus-kill-buffer url-working-buffer)))
339
340 (defun nnweb-url-retrieve-asynch (url callback &rest data)
341   (let ((url-request-method "GET")
342         (old-asynch url-be-asynchronous)
343         (url-request-data nil)
344         (url-request-extra-headers nil)
345         (url-working-buffer (generate-new-buffer-name " *nnweb*")))
346     (setq-default url-be-asynchronous t)
347     (save-excursion
348       (set-buffer (get-buffer-create url-working-buffer))
349       (setq url-current-callback-data data
350             url-be-asynchronous t
351             url-current-callback-func callback)
352       (url-retrieve url))
353     (setq-default url-be-asynchronous old-asynch)))
354
355 ;;;
356 ;;; DejaNews functions.
357 ;;;
358
359 (defun nnweb-dejanews-create-mapping ()
360   "Perform the search and create an number-to-url alist."
361   (save-excursion
362     (set-buffer nnweb-buffer)
363     (erase-buffer)
364     (when (funcall (nnweb-definition 'search) nnweb-search)
365       (let ((i 0)
366             (more t)
367             (case-fold-search t)
368             (active (or (cadr (assoc nnweb-group nnweb-group-alist))
369                         (cons 1 0)))
370             subject date from
371             map url parse a table group text)
372         (while more
373           ;; Go through all the article hits on this page.
374           (goto-char (point-min))
375           (setq parse (w3-parse-buffer (current-buffer))
376                 table (nth 1 (nnweb-parse-find-all 'table parse)))
377           (dolist (row (nth 2 (car (nth 2 table))))
378             (setq a (nnweb-parse-find 'a row)
379                   url (cdr (assq 'href (nth 1 a)))
380                   text (nreverse (nnweb-text row)))
381             (when a
382               (setq subject (nth 4 text)
383                     group (nth 2 text)
384                     date (nth 1 text)
385                     from (nth 0 text))
386               (if (string-match "\\([0-9]+\\)/\\([0-9]+\\)/\\([0-9]+\\)" date)
387                   (setq date (format "%s %s 00:00:00 %s"
388                                      (car (rassq (string-to-number
389                                                   (match-string 2 date))
390                                                  parse-time-months))
391                                      (match-string 3 date) 
392                                      (match-string 1 date)))
393                 (setq date "Jan 1 00:00:00 0000"))
394               (incf i)
395               (setq url (concat url "&fmt=text"))
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                         (string-to-number (substring (match-string 1) 1))
726                       (or (cdr (assq (intern (match-string 1))
727                                      w3-html-entities))
728                           ?#)))
729                    t t)))
730
731 (defun nnweb-decode-entities-string (str)
732   (with-temp-buffer
733     (insert str)
734     (nnweb-decode-entities)
735     (buffer-substring (point-min) (point-max))))
736
737 (defun nnweb-remove-markup ()
738   "Remove all HTML markup, leaving just plain text."
739   (goto-char (point-min))
740   (while (search-forward "<!--" nil t)
741     (delete-region (match-beginning 0)
742                    (or (search-forward "-->" nil t)
743                        (point-max))))
744   (goto-char (point-min))
745   (while (re-search-forward "<[^>]+>" nil t)
746     (replace-match "" t t)))
747
748 (defun nnweb-insert (url &optional follow-refresh)
749   "Insert the contents from an URL in the current buffer.
750 If FOLLOW-REFRESH is non-nil, redirect refresh url in META."
751   (let ((name buffer-file-name))
752     (if follow-refresh
753         (save-restriction
754           (narrow-to-region (point) (point))
755           (url-insert-file-contents url)
756           (goto-char (point-min))
757           (while (re-search-forward 
758                   "HTTP-EQUIV=\"Refresh\"[^>]*URL=\\([^\"]+\\)\""
759                   nil t)
760             (let ((url (match-string 1)))
761               (delete-region (point-min) (point-max))
762               (nnweb-insert url))
763             (goto-char (point-min)))
764           (goto-char (point-max)))
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