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