164c1577fe076fa7af77e84f2b5710727a3f8d64
[gnus] / 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         t))))
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             (let ((multibyte (default-value 'enable-multibyte-characters)))
304               (unwind-protect
305                   (progn
306                     (setq-default enable-multibyte-characters nil)
307                     (nnheader-set-temp-buffer
308                      (format " *nnweb %s %s %s*"
309                              nnweb-type nnweb-search server)))
310                 (setq-default enable-multibyte-characters multibyte))
311               (current-buffer))))))
312
313 (defun nnweb-fetch-url (url)
314   (let (buf)
315     (save-excursion
316       (if (not nnheader-callback-function)
317           (progn
318             (with-temp-buffer
319               (mm-enable-multibyte)
320               (let ((coding-system-for-read 'binary)
321                     (coding-system-for-write 'binary)
322                     (default-process-coding-system 'binary))
323                 (nnweb-insert url))
324               (setq buf (buffer-string)))
325             (erase-buffer)
326             (insert buf)
327             t)
328         (nnweb-url-retrieve-asynch
329          url 'nnweb-callback (current-buffer) nnheader-callback-function)
330         t))))
331
332 (defun nnweb-callback (buffer callback)
333   (when (gnus-buffer-live-p url-working-buffer)
334     (save-excursion
335       (set-buffer url-working-buffer)
336       (funcall (nnweb-definition 'article))
337       (nnweb-decode-entities)
338       (set-buffer buffer)
339       (goto-char (point-max))
340       (insert-buffer-substring url-working-buffer))
341     (funcall callback t)
342     (gnus-kill-buffer url-working-buffer)))
343
344 (defun nnweb-url-retrieve-asynch (url callback &rest data)
345   (let ((url-request-method "GET")
346         (old-asynch url-be-asynchronous)
347         (url-request-data nil)
348         (url-request-extra-headers nil)
349         (url-working-buffer (generate-new-buffer-name " *nnweb*")))
350     (setq-default url-be-asynchronous t)
351     (save-excursion
352       (set-buffer (get-buffer-create url-working-buffer))
353       (setq url-current-callback-data data
354             url-be-asynchronous t
355             url-current-callback-func callback)
356       (url-retrieve url))
357     (setq-default url-be-asynchronous old-asynch)))
358
359 ;;;
360 ;;; DejaNews functions.
361 ;;;
362
363 (defun nnweb-dejanews-create-mapping ()
364   "Perform the search and create an number-to-url alist."
365   (save-excursion
366     (set-buffer nnweb-buffer)
367     (erase-buffer)
368     (when (funcall (nnweb-definition 'search) nnweb-search)
369       (let ((i 0)
370             (more t)
371             (case-fold-search t)
372             (active (or (cadr (assoc nnweb-group nnweb-group-alist))
373                         (cons 1 0)))
374             subject date from
375             map url parse a table group text)
376         (while more
377           ;; Go through all the article hits on this page.
378           (goto-char (point-min))
379           (setq parse (w3-parse-buffer (current-buffer))
380                 table (nth 1 (nnweb-parse-find-all 'table parse)))
381           (dolist (row (nth 2 (car (nth 2 table))))
382             (setq a (nnweb-parse-find 'a row)
383                   url (cdr (assq 'href (nth 1 a)))
384                   text (nreverse (nnweb-text row)))
385             (when a
386               (setq subject (nth 4 text)
387                     group (nth 2 text)
388                     date (nth 1 text)
389                     from (nth 0 text))
390               (if (string-match "\\([0-9]+\\)/\\([0-9]+\\)/\\([0-9]+\\)" date)
391                   (setq date (format "%s %s 00:00:00 %s"
392                                      (car (rassq (string-to-number
393                                                   (match-string 2 date))
394                                                  parse-time-months))
395                                      (match-string 3 date) 
396                                      (match-string 1 date)))
397                 (setq date "Jan 1 00:00:00 0000"))
398               (incf i)
399               (setq url (concat url "&fmt=text"))
400               (unless (nnweb-get-hashtb url)
401                 (push
402                  (list
403                   (incf (cdr active))
404                   (make-full-mail-header
405                    (cdr active) (concat subject " (" group ")") from date
406                    (concat "<" (nnweb-identifier url) "@dejanews>")
407                    nil 0 0 url))
408                  map)
409                 (nnweb-set-hashtb (cadar map) (car map)))))
410           ;; See whether there is a "Get next 20 hits" button here.
411           (goto-char (point-min))
412           (if (or (not (re-search-forward
413                         "HREF=\"\\([^\"]+\\)\"[<>b]+Next result" nil t))
414                   (>= i nnweb-max-hits))
415               (setq more nil)
416             ;; Yup -- fetch it.
417             (setq more (match-string 1))
418             (erase-buffer)
419             (url-insert-file-contents more)))
420         ;; Return the articles in the right order.
421         (setq nnweb-articles
422               (sort (nconc nnweb-articles map) 'car-less-than-car))))))
423
424 (defun nnweb-dejanews-search (search)
425   (nnweb-insert
426    (concat
427     (nnweb-definition 'address)
428     "?"
429     (nnweb-encode-www-form-urlencoded
430      `(("ST" . "PS")
431        ("svcclass" . "dnyr")
432        ("QRY" . ,search)
433        ("defaultOp" . "AND")
434        ("DBS" . "1")
435        ("OP" . "dnquery.xp")
436        ("LNG" . "ALL")
437        ("maxhits" . "100")
438        ("threaded" . "0")
439        ("format" . "verbose2")
440        ("showsort" . "date")
441        ("agesign" . "1")
442        ("ageweight" . "1")))))
443   t)
444
445 (defun nnweb-dejanewsold-search (search)
446   (nnweb-fetch-form
447    (nnweb-definition 'address)
448    `(("query" . ,search)
449      ("defaultOp" . "AND")
450      ("svcclass" . "dnold")
451      ("maxhits" . "100")
452      ("format" . "verbose2")
453      ("threaded" . "0")
454      ("showsort" . "date")
455      ("agesign" . "1")
456      ("ageweight" . "1")))
457   t)
458
459 (defun nnweb-dejanews-identity (url)
460   "Return an unique identifier based on URL."
461   (if (string-match "AN=\\([0-9]+\\)" url)
462       (match-string 1 url)
463     url))
464
465 ;;;
466 ;;; InReference
467 ;;;
468
469 (defun nnweb-reference-create-mapping ()
470   "Perform the search and create an number-to-url alist."
471   (save-excursion
472     (set-buffer nnweb-buffer)
473     (erase-buffer)
474     (when (funcall (nnweb-definition 'search) nnweb-search)
475       (let ((i 0)
476             (more t)
477             (case-fold-search t)
478             (active (or (cadr (assoc nnweb-group nnweb-group-alist))
479                         (cons 1 0)))
480             Subject Score Date Newsgroups From Message-ID
481             map url)
482         (while more
483           ;; Go through all the article hits on this page.
484           (goto-char (point-min))
485           (search-forward "</pre><hr>" nil t)
486           (delete-region (point-min) (point))
487           (goto-char (point-min))
488           (while (re-search-forward "^ +[0-9]+\\." nil t)
489             (narrow-to-region
490              (point)
491              (if (re-search-forward "^$" nil t)
492                  (match-beginning 0)
493                (point-max)))
494             (goto-char (point-min))
495             (when (looking-at ".*href=\"\\([^\"]+\\)\"")
496               (setq url (match-string 1)))
497             (nnweb-remove-markup)
498             (goto-char (point-min))
499             (while (search-forward "\t" nil t)
500               (replace-match " "))
501             (goto-char (point-min))
502             (while (re-search-forward "^\\([^:]+\\): \\(.*\\)$" nil t)
503               (set (intern (match-string 1)) (match-string 2)))
504             (widen)
505             (search-forward "</pre>" nil t)
506             (incf i)
507             (unless (nnweb-get-hashtb url)
508               (push
509                (list
510                 (incf (cdr active))
511                 (make-full-mail-header
512                  (cdr active) (concat  "(" Newsgroups ") " Subject) From Date
513                  Message-ID
514                  nil 0 (string-to-int Score) url))
515                map)
516               (nnweb-set-hashtb (cadar map) (car map))))
517           (setq more nil))
518         ;; Return the articles in the right order.
519         (setq nnweb-articles
520               (sort (nconc nnweb-articles map) 'car-less-than-car))))))
521
522 (defun nnweb-reference-wash-article ()
523   (let ((case-fold-search t))
524     (goto-char (point-min))
525     (re-search-forward "^</center><hr>" nil t)
526     (delete-region (point-min) (point))
527     (search-forward "<pre>" nil t)
528     (forward-line -1)
529     (let ((body (point-marker)))
530       (search-forward "</pre>" nil t)
531       (delete-region (point) (point-max))
532       (nnweb-remove-markup)
533       (goto-char (point-min))
534       (while (looking-at " *$")
535         (gnus-delete-line))
536       (narrow-to-region (point-min) body)
537       (while (and (re-search-forward "^$" nil t)
538                   (not (eobp)))
539         (gnus-delete-line))
540       (goto-char (point-min))
541       (while (looking-at "\\(^[^ ]+:\\) *")
542         (replace-match "\\1 " t)
543         (forward-line 1))
544       (goto-char (point-min))
545       (when (re-search-forward "^References:" nil t)
546         (narrow-to-region
547          (point) (if (re-search-forward "^$\\|^[^:]+:" nil t)
548                      (match-beginning 0)
549                    (point-max)))
550         (goto-char (point-min))
551         (while (not (eobp))
552           (unless (looking-at "References")
553             (insert "\t")
554             (forward-line 1)))
555         (goto-char (point-min))
556         (while (search-forward "," nil t)
557           (replace-match " " t t)))
558       (widen)
559       (set-marker body nil))))
560
561 (defun nnweb-reference-search (search)
562   (url-insert-file-contents
563    (concat
564     (nnweb-definition 'address)
565     "?"
566     (nnweb-encode-www-form-urlencoded
567      `(("search" . "advanced")
568        ("querytext" . ,search)
569        ("subj" . "")
570        ("name" . "")
571        ("login" . "")
572        ("host" . "")
573        ("organization" . "")
574        ("groups" . "")
575        ("keywords" . "")
576        ("choice" . "Search")
577        ("startmonth" . "Jul")
578        ("startday" . "25")
579        ("startyear" . "1996")
580        ("endmonth" . "Aug")
581        ("endday" . "24")
582        ("endyear" . "1996")
583        ("mode" . "Quick")
584        ("verbosity" . "Verbose")
585        ("ranking" . "Relevance")
586        ("first" . "1")
587        ("last" . "25")
588        ("score" . "50")))))
589   (setq buffer-file-name nil)
590   t)
591
592 ;;;
593 ;;; Alta Vista
594 ;;;
595
596 (defun nnweb-altavista-create-mapping ()
597   "Perform the search and create an number-to-url alist."
598   (save-excursion
599     (set-buffer nnweb-buffer)
600     (erase-buffer)
601     (let ((part 0))
602       (when (funcall (nnweb-definition 'search) nnweb-search part)
603         (let ((i 0)
604               (more t)
605               (case-fold-search t)
606               (active (or (cadr (assoc nnweb-group nnweb-group-alist))
607                           (cons 1 0)))
608               subject date from id group
609               map url)
610           (while more
611             ;; Go through all the article hits on this page.
612             (goto-char (point-min))
613             (search-forward "<dt>" nil t)
614             (delete-region (point-min) (match-beginning 0))
615             (goto-char (point-min))
616             (while (search-forward "<dt>" nil t)
617               (replace-match "\n<blubb>"))
618             (nnweb-decode-entities)
619             (goto-char (point-min))
620             (while (re-search-forward "<blubb>.*href=\"\\([^\"]+\\)\"><strong>\\([^>]*\\)</strong></a><dd>\\([^-]+\\)- <b>\\([^<]+\\)<.*href=\"news:\\([^\"]+\\)\">.*\">\\(.+\\)</a><P>"
621                                       nil t)
622               (setq url (match-string 1)
623                     subject (match-string 2)
624                     date (match-string 3)
625                     group (match-string 4)
626                     id (concat "<" (match-string 5) ">")
627                     from (match-string 6))
628               (incf i)
629               (unless (nnweb-get-hashtb url)
630                 (push
631                  (list
632                   (incf (cdr active))
633                   (make-full-mail-header
634                    (cdr active) (concat  "(" group ") " subject) from date
635                    id nil 0 0 url))
636                  map)
637                 (nnweb-set-hashtb (cadar map) (car map))))
638             ;; See if we want more.
639             (when (or (not nnweb-articles)
640                       (>= i nnweb-max-hits)
641                       (not (funcall (nnweb-definition 'search)
642                                     nnweb-search (incf part))))
643               (setq more nil)))
644           ;; Return the articles in the right order.
645           (setq nnweb-articles
646                 (sort (nconc nnweb-articles map) 'car-less-than-car)))))))
647
648 (defun nnweb-altavista-wash-article ()
649   (goto-char (point-min))
650   (let ((case-fold-search t))
651     (when (re-search-forward "^<strong>" nil t)
652       (delete-region (point-min) (match-beginning 0)))
653     (goto-char (point-min))
654     (while (looking-at "<strong>\\([^ ]+\\) +</strong> +\\(.*\\)$")
655       (replace-match "\\1: \\2" t)
656       (forward-line 1))
657     (when (re-search-backward "^References:" nil t)
658       (narrow-to-region (point) (progn (forward-line 1) (point)))
659       (goto-char (point-min))
660       (while (re-search-forward "<A.*\\?id@\\([^\"]+\\)\">[0-9]+</A>" nil t)
661         (replace-match "&lt;\\1&gt; " t)))
662     (widen)
663     (nnweb-remove-markup)))
664
665 (defun nnweb-altavista-search (search &optional part)
666   (url-insert-file-contents
667    (concat
668     (nnweb-definition 'address)
669     "?"
670     (nnweb-encode-www-form-urlencoded
671      `(("pg" . "aq")
672        ("what" . "news")
673        ,@(when part `(("stq" . ,(int-to-string (* part 30)))))
674        ("fmt" . "d")
675        ("q" . ,search)
676        ("r" . "")
677        ("d0" . "")
678        ("d1" . "")))))
679   (setq buffer-file-name nil)
680   t)
681
682 ;;;
683 ;;; General web/w3 interface utility functions
684 ;;;
685
686 (defun nnweb-insert-html (parse)
687   "Insert HTML based on a w3 parse tree."
688   (if (stringp parse)
689       (insert parse)
690     (insert "<" (symbol-name (car parse)) " ")
691     (insert (mapconcat
692              (lambda (param)
693                (concat (symbol-name (car param)) "="
694                        (prin1-to-string
695                         (if (consp (cdr param))
696                             (cadr param)
697                           (cdr param)))))
698              (nth 1 parse)
699              " "))
700     (insert ">\n")
701     (mapcar 'nnweb-insert-html (nth 2 parse))
702     (insert "</" (symbol-name (car parse)) ">\n")))
703
704 (defun nnweb-encode-www-form-urlencoded (pairs)
705   "Return PAIRS encoded for forms."
706   (mapconcat
707    (function
708     (lambda (data)
709       (concat (w3-form-encode-xwfu (car data)) "="
710               (w3-form-encode-xwfu (cdr data)))))
711    pairs "&"))
712
713 (defun nnweb-fetch-form (url pairs)
714   "Fetch a form from URL with PAIRS as the data using the POST method."
715   (let ((url-request-data (nnweb-encode-www-form-urlencoded pairs))
716         (url-request-method "POST")
717         (url-request-extra-headers
718          '(("Content-type" . "application/x-www-form-urlencoded"))))
719     (url-insert-file-contents url)
720     (setq buffer-file-name nil))
721   t)
722
723 (defun nnweb-decode-entities ()
724   "Decode all HTML entities."
725   (goto-char (point-min))
726   (while (re-search-forward "&\\([a-z]+\\);" nil t)
727     (replace-match (char-to-string (or (cdr (assq (intern (match-string 1))
728                                                   w3-html-entities))
729                                        ?#))
730                    t t)))
731
732 (defun nnweb-remove-markup ()
733   "Remove all HTML markup, leaving just plain text."
734   (goto-char (point-min))
735   (while (search-forward "<!--" nil t)
736     (delete-region (match-beginning 0)
737                    (or (search-forward "-->" nil t)
738                        (point-max))))
739   (goto-char (point-min))
740   (while (re-search-forward "<[^>]+>" nil t)
741     (replace-match "" t t)))
742
743 (defun nnweb-insert (url)
744   "Insert the contents from an URL in the current buffer."
745   (let ((name buffer-file-name))
746     (url-insert-file-contents url)
747     (setq buffer-file-name name)))
748
749 (defun nnweb-parse-find (type parse &optional maxdepth)
750   "Find the element of TYPE in PARSE."
751   (catch 'found
752     (nnweb-parse-find-1 type parse maxdepth)))
753
754 (defun nnweb-parse-find-1 (type contents maxdepth)
755   (when (or (null maxdepth)
756             (not (zerop maxdepth)))
757     (when (consp contents)
758       (when (eq (car contents) type)
759         (throw 'found contents))
760       (when (listp (cdr contents))
761         (dolist (element contents)
762           (when (consp element)
763             (nnweb-parse-find-1 type element
764                                 (and maxdepth (1- maxdepth)))))))))
765
766 (defun nnweb-parse-find-all (type parse)
767   "Find all elements of TYPE in PARSE."
768   (catch 'found
769     (nnweb-parse-find-all-1 type parse)))
770
771 (defun nnweb-parse-find-all-1 (type contents)
772   (let (result)
773     (when (consp contents)
774       (if (eq (car contents) type)
775           (push contents result)
776         (when (listp (cdr contents))
777           (dolist (element contents)
778             (when (consp element)
779               (setq result
780                     (nconc result (nnweb-parse-find-all-1 type element))))))))
781     result))
782
783 (defvar nnweb-text)
784 (defun nnweb-text (parse)
785   "Return a list of text contents in PARSE."
786   (let ((nnweb-text nil))
787     (nnweb-text-1 parse)
788     (nreverse nnweb-text)))
789
790 (defun nnweb-text-1 (contents)
791   (dolist (element contents)
792     (if (stringp element)
793         (push element nnweb-text)
794       (when (and (consp element)
795                  (listp (cdr element)))
796         (nnweb-text-1 element)))))
797
798 (provide 'nnweb)
799
800 ;;; nnweb.el ends here