1 ;;; nnweb.el --- retrieving articles via web search engines
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001
3 ;; Free Software Foundation, Inc.
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
8 ;; This file is part of GNU Emacs.
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)
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.
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.
27 ;; Note: You need to have `url' and `w3' installed for this
32 (eval-when-compile (require 'cl))
46 ;; Report failure to find w3 at load time if appropriate.
47 (unless noninteractive
51 (require 'w3-forms))))
55 (defvoo nnweb-directory (nnheader-concat gnus-directory "nnweb/")
56 "Where nnweb will save its files.")
58 (defvoo nnweb-type 'dejanews
59 "What search engine type is being used.
60 Valid types include `dejanews', `dejanewsold', `reference',
63 (defvar nnweb-type-definition
65 (dejanews ;; bought by google.com
66 (article . nnweb-google-wash-article)
67 (id . "http://groups.google.com/groups?as_umsgid=%s")
68 (reference . nnweb-google-reference)
69 (map . nnweb-google-create-mapping)
70 (search . nnweb-google-search)
71 (address . "http://groups.google.com/groups")
72 (identifier . nnweb-google-identity))
74 ;;; (article . ignore)
75 ;;; (id . "http://search.dejanews.com/msgid.xp?MID=%s&fmt=text")
76 ;;; (map . nnweb-dejanews-create-mapping)
77 ;;; (search . nnweb-dejanews-search)
78 ;;; (address . "http://www.deja.com/=dnc/qs.xp")
79 ;;; (identifier . nnweb-dejanews-identity))
81 ;;; (article . ignore)
82 ;;; (map . nnweb-dejanews-create-mapping)
83 ;;; (search . nnweb-dejanewsold-search)
84 ;;; (address . "http://www.deja.com/dnquery.xp")
85 ;;; (identifier . nnweb-dejanews-identity))
87 (article . nnweb-reference-wash-article)
88 (map . nnweb-reference-create-mapping)
89 (search . nnweb-reference-search)
90 (address . "http://www.reference.com/cgi-bin/pn/go")
91 (identifier . identity))
93 (article . nnweb-altavista-wash-article)
94 (map . nnweb-altavista-create-mapping)
95 (search . nnweb-altavista-search)
96 (address . "http://www.altavista.digital.com/cgi-bin/query")
97 (id . "/cgi-bin/news?id@%s")
98 (identifier . identity)))
99 "Type-definition alist.")
101 (defvoo nnweb-search nil
102 "Search string to feed to DejaNews.")
104 (defvoo nnweb-max-hits 999
105 "Maximum number of hits to display.")
107 (defvoo nnweb-ephemeral-p nil
108 "Whether this nnweb server is ephemeral.")
110 ;;; Internal variables
112 (defvoo nnweb-articles nil)
113 (defvoo nnweb-buffer nil)
114 (defvoo nnweb-group-alist nil)
115 (defvoo nnweb-group nil)
116 (defvoo nnweb-hashtb nil)
118 ;;; Interface functions
120 (nnoo-define-basics nnweb)
122 (deffoo nnweb-retrieve-headers (articles &optional group server fetch-old)
123 (nnweb-possibly-change-server group server)
125 (set-buffer nntp-server-buffer)
127 (let (article header)
128 (mm-with-unibyte-current-buffer
129 (while (setq article (pop articles))
130 (when (setq header (cadr (assq article nnweb-articles)))
131 (nnheader-insert-nov header))))
134 (deffoo nnweb-request-scan (&optional group server)
135 (nnweb-possibly-change-server group server)
136 (funcall (nnweb-definition 'map))
137 (unless nnweb-ephemeral-p
139 (nnweb-write-overview group)))
141 (deffoo nnweb-request-group (group &optional server dont-check)
142 (nnweb-possibly-change-server nil server)
144 (not (equal group nnweb-group))
145 (not nnweb-ephemeral-p))
146 (setq nnweb-group group
148 (let ((info (assoc group nnweb-group-alist)))
150 (setq nnweb-type (nth 2 info))
151 (setq nnweb-search (nth 3 info))
153 (nnweb-read-overview group)))))
155 ((not nnweb-articles)
156 (nnheader-report 'nnweb "No matching articles"))
158 (let ((active (if nnweb-ephemeral-p
159 (cons (caar nnweb-articles)
160 (caar (last nnweb-articles)))
161 (cadr (assoc group nnweb-group-alist)))))
162 (nnheader-report 'nnweb "Opened group %s" group)
164 "211 %d %d %d %s\n" (length nnweb-articles)
165 (car active) (cdr active) group)))))
167 (deffoo nnweb-close-group (group &optional server)
168 (nnweb-possibly-change-server group server)
169 (when (gnus-buffer-live-p nnweb-buffer)
171 (set-buffer nnweb-buffer)
172 (set-buffer-modified-p nil)
173 (kill-buffer nnweb-buffer)))
176 (deffoo nnweb-request-article (article &optional group server buffer)
177 (nnweb-possibly-change-server group server)
179 (set-buffer (or buffer nntp-server-buffer))
180 (let* ((header (cadr (assq article nnweb-articles)))
181 (url (and header (mail-header-xref header))))
183 (mm-with-unibyte-current-buffer
184 (nnweb-fetch-url url)))
185 (and (stringp article)
186 (nnweb-definition 'id t)
187 (let ((fetch (nnweb-definition 'id))
189 (when (string-match "^<\\(.*\\)>$" article)
190 (setq art (match-string 1 article)))
191 (when (and fetch art)
192 (setq url (format fetch article))
193 (mm-with-unibyte-current-buffer
194 (nnweb-fetch-url url))
195 (if (nnweb-definition 'reference t)
197 (funcall (nnweb-definition
198 'reference) article)))))))
199 (unless nnheader-callback-function
200 (funcall (nnweb-definition 'article))
201 (nnweb-decode-entities))
202 (nnheader-report 'nnweb "Fetched article %s" article)
203 (cons group (and (numberp article) article))))))
205 (deffoo nnweb-close-server (&optional server)
206 (when (and (nnweb-server-opened server)
207 (gnus-buffer-live-p nnweb-buffer))
209 (set-buffer nnweb-buffer)
210 (set-buffer-modified-p nil)
211 (kill-buffer nnweb-buffer)))
212 (nnoo-close-server 'nnweb server))
214 (deffoo nnweb-request-list (&optional server)
215 (nnweb-possibly-change-server nil server)
217 (set-buffer nntp-server-buffer)
218 (nnmail-generate-active nnweb-group-alist)
221 (deffoo nnweb-request-update-info (group info &optional server)
222 (nnweb-possibly-change-server group server))
224 (deffoo nnweb-asynchronous-p ()
227 (deffoo nnweb-request-create-group (group &optional server args)
228 (nnweb-possibly-change-server nil server)
229 (nnweb-request-delete-group group)
230 (push `(,group ,(cons 1 0) ,@args) nnweb-group-alist)
234 (deffoo nnweb-request-delete-group (group &optional force server)
235 (nnweb-possibly-change-server group server)
236 (gnus-pull group nnweb-group-alist t)
238 (gnus-delete-file (nnweb-overview-file group))
241 (nnoo-define-skeleton nnweb)
243 ;;; Internal functions
245 (defun nnweb-read-overview (group)
246 "Read the overview of GROUP and build the map."
247 (when (file-exists-p (nnweb-overview-file group))
248 (mm-with-unibyte-buffer
249 (nnheader-insert-file-contents (nnweb-overview-file group))
250 (goto-char (point-min))
253 (setq header (nnheader-parse-nov))
255 (push (list (mail-header-number header)
256 header (mail-header-xref header))
258 (nnweb-set-hashtb header (car nnweb-articles)))))))
260 (defun nnweb-write-overview (group)
261 "Write the overview file for GROUP."
262 (with-temp-file (nnweb-overview-file group)
263 (let ((articles nnweb-articles))
265 (nnheader-insert-nov (cadr (pop articles)))))))
267 (defun nnweb-set-hashtb (header data)
268 (gnus-sethash (nnweb-identifier (mail-header-xref header))
271 (defun nnweb-get-hashtb (url)
272 (gnus-gethash (nnweb-identifier url) nnweb-hashtb))
274 (defun nnweb-identifier (ident)
275 (funcall (nnweb-definition 'identifier) ident))
277 (defun nnweb-overview-file (group)
278 "Return the name of the overview file of GROUP."
279 (nnheader-concat nnweb-directory group ".overview"))
281 (defun nnweb-write-active ()
282 "Save the active file."
283 (gnus-make-directory nnweb-directory)
284 (with-temp-file (nnheader-concat nnweb-directory "active")
285 (prin1 `(setq nnweb-group-alist ',nnweb-group-alist) (current-buffer))))
287 (defun nnweb-read-active ()
288 "Read the active file."
289 (load (nnheader-concat nnweb-directory "active") t t t))
291 (defun nnweb-definition (type &optional noerror)
292 "Return the definition of TYPE."
293 (let ((def (cdr (assq type (assq nnweb-type nnweb-type-definition)))))
296 (error "Undefined definition %s" type))
299 (defun nnweb-possibly-change-server (&optional group server)
302 (unless (nnweb-server-opened server)
303 (nnweb-open-server server)))
304 (unless nnweb-group-alist
307 (when (and (not nnweb-ephemeral-p)
308 (not (equal group nnweb-group)))
309 (setq nnweb-hashtb (gnus-make-hashtable 4095))
310 (nnweb-request-group group nil t))))
312 (defun nnweb-init (server)
313 "Initialize buffers and such."
314 (unless (gnus-buffer-live-p nnweb-buffer)
318 (nnheader-set-temp-buffer
319 (format " *nnweb %s %s %s*"
320 nnweb-type nnweb-search server))
321 (current-buffer))))))
323 (defun nnweb-fetch-url (url)
326 (if (not nnheader-callback-function)
329 (mm-enable-multibyte)
330 (let ((coding-system-for-read 'binary)
331 (coding-system-for-write 'binary)
332 (default-process-coding-system 'binary))
334 (setq buf (buffer-string)))
338 (nnweb-url-retrieve-asynch
339 url 'nnweb-callback (current-buffer) nnheader-callback-function)
342 (defun nnweb-callback (buffer callback)
343 (when (gnus-buffer-live-p url-working-buffer)
345 (set-buffer url-working-buffer)
346 (funcall (nnweb-definition 'article))
347 (nnweb-decode-entities)
349 (goto-char (point-max))
350 (insert-buffer-substring url-working-buffer))
352 (gnus-kill-buffer url-working-buffer)))
354 (defun nnweb-url-retrieve-asynch (url callback &rest data)
355 (let ((url-request-method "GET")
356 (old-asynch url-be-asynchronous)
357 (url-request-data nil)
358 (url-request-extra-headers nil)
359 (url-working-buffer (generate-new-buffer-name " *nnweb*")))
360 (setq-default url-be-asynchronous t)
362 (set-buffer (get-buffer-create url-working-buffer))
363 (setq url-current-callback-data data
364 url-be-asynchronous t
365 url-current-callback-func callback)
366 (url-retrieve url nil))
367 (setq-default url-be-asynchronous old-asynch)))
369 (if (fboundp 'url-retrieve-synchronously)
370 (defun nnweb-url-retrieve-asynch (url callback &rest data)
371 (url-retrieve url callback data)))
374 ;;; DejaNews functions.
377 (defun nnweb-dejanews-create-mapping ()
378 "Perform the search and create an number-to-url alist."
380 (set-buffer nnweb-buffer)
382 (when (funcall (nnweb-definition 'search) nnweb-search)
386 (active (or (cadr (assoc nnweb-group nnweb-group-alist))
389 map url parse a table group text)
391 ;; Go through all the article hits on this page.
392 (goto-char (point-min))
393 (setq parse (w3-parse-buffer (current-buffer))
394 table (nth 1 (nnweb-parse-find-all 'table parse)))
395 (dolist (row (nth 2 (car (nth 2 table))))
396 (setq a (nnweb-parse-find 'a row)
397 url (cdr (assq 'href (nth 1 a)))
398 text (nreverse (nnweb-text row)))
400 (setq subject (nth 4 text)
404 (if (string-match "\\([0-9]+\\)/\\([0-9]+\\)/\\([0-9]+\\)" date)
405 (setq date (format "%s %s 00:00:00 %s"
406 (car (rassq (string-to-number
407 (match-string 2 date))
409 (match-string 3 date)
410 (match-string 1 date)))
411 (setq date "Jan 1 00:00:00 0000"))
413 (setq url (concat url "&fmt=text"))
414 (when (string-match "&context=[^&]+" url)
415 (setq url (replace-match "" t t url)))
416 (unless (nnweb-get-hashtb url)
420 (make-full-mail-header
421 (cdr active) (concat subject " (" group ")") from date
422 (concat "<" (nnweb-identifier url) "@dejanews>")
425 (nnweb-set-hashtb (cadar map) (car map)))))
426 ;; See whether there is a "Get next 20 hits" button here.
427 (goto-char (point-min))
428 (if (or (not (re-search-forward
429 "HREF=\"\\([^\"]+\\)\"[<>b]+Next result" nil t))
430 (>= i nnweb-max-hits))
433 (setq more (match-string 1))
435 (url-insert-file-contents more)))
436 ;; Return the articles in the right order.
438 (sort (nconc nnweb-articles map) 'car-less-than-car))))))
440 (defun nnweb-dejanews-search (search)
443 (nnweb-definition 'address)
445 (nnweb-encode-www-form-urlencoded
447 ("svcclass" . "dnyr")
449 ("defaultOp" . "AND")
451 ("OP" . "dnquery.xp")
455 ("format" . "verbose2")
456 ("showsort" . "date")
458 ("ageweight" . "1")))))
461 (defun nnweb-dejanewsold-search (search)
463 (nnweb-definition 'address)
464 `(("query" . ,search)
465 ("defaultOp" . "AND")
466 ("svcclass" . "dnold")
468 ("format" . "verbose2")
470 ("showsort" . "date")
472 ("ageweight" . "1")))
475 (defun nnweb-dejanews-identity (url)
476 "Return an unique identifier based on URL."
477 (if (string-match "AN=\\([0-9]+\\)" url)
485 (defun nnweb-reference-create-mapping ()
486 "Perform the search and create an number-to-url alist."
488 (set-buffer nnweb-buffer)
490 (when (funcall (nnweb-definition 'search) nnweb-search)
494 (active (or (cadr (assoc nnweb-group nnweb-group-alist))
496 Subject Score Date Newsgroups From Message-ID
499 ;; Go through all the article hits on this page.
500 (goto-char (point-min))
501 (search-forward "</pre><hr>" nil t)
502 (delete-region (point-min) (point))
503 (goto-char (point-min))
504 (while (re-search-forward "^ +[0-9]+\\." nil t)
507 (if (re-search-forward "^$" nil t)
510 (goto-char (point-min))
511 (when (looking-at ".*href=\"\\([^\"]+\\)\"")
512 (setq url (match-string 1)))
513 (nnweb-remove-markup)
514 (goto-char (point-min))
515 (while (search-forward "\t" nil t)
517 (goto-char (point-min))
518 (while (re-search-forward "^\\([^:]+\\): \\(.*\\)$" nil t)
519 (set (intern (match-string 1)) (match-string 2)))
521 (search-forward "</pre>" nil t)
523 (unless (nnweb-get-hashtb url)
527 (make-full-mail-header
528 (cdr active) (concat "(" Newsgroups ") " Subject) From Date
530 nil 0 (string-to-int Score) url))
532 (nnweb-set-hashtb (cadar map) (car map))))
534 ;; Return the articles in the right order.
536 (sort (nconc nnweb-articles map) 'car-less-than-car))))))
538 (defun nnweb-reference-wash-article ()
539 (let ((case-fold-search t))
540 (goto-char (point-min))
541 (re-search-forward "^</center><hr>" nil t)
542 (delete-region (point-min) (point))
543 (search-forward "<pre>" nil t)
545 (let ((body (point-marker)))
546 (search-forward "</pre>" nil t)
547 (delete-region (point) (point-max))
548 (nnweb-remove-markup)
549 (goto-char (point-min))
550 (while (looking-at " *$")
552 (narrow-to-region (point-min) body)
553 (while (and (re-search-forward "^$" nil t)
556 (goto-char (point-min))
557 (while (looking-at "\\(^[^ ]+:\\) *")
558 (replace-match "\\1 " t)
560 (goto-char (point-min))
561 (when (re-search-forward "^References:" nil t)
563 (point) (if (re-search-forward "^$\\|^[^:]+:" nil t)
566 (goto-char (point-min))
568 (unless (looking-at "References")
571 (goto-char (point-min))
572 (while (search-forward "," nil t)
573 (replace-match " " t t)))
575 (set-marker body nil))))
577 (defun nnweb-reference-search (search)
578 (url-insert-file-contents
580 (nnweb-definition 'address)
582 (nnweb-encode-www-form-urlencoded
583 `(("search" . "advanced")
584 ("querytext" . ,search)
589 ("organization" . "")
592 ("choice" . "Search")
593 ("startmonth" . "Jul")
595 ("startyear" . "1996")
600 ("verbosity" . "Verbose")
601 ("ranking" . "Relevance")
605 (setq buffer-file-name nil)
612 (defun nnweb-altavista-create-mapping ()
613 "Perform the search and create an number-to-url alist."
615 (set-buffer nnweb-buffer)
618 (when (funcall (nnweb-definition 'search) nnweb-search part)
622 (active (or (cadr (assoc nnweb-group nnweb-group-alist))
624 subject date from id group
627 ;; Go through all the article hits on this page.
628 (goto-char (point-min))
629 (search-forward "<dt>" nil t)
630 (delete-region (point-min) (match-beginning 0))
631 (goto-char (point-min))
632 (while (search-forward "<dt>" nil t)
633 (replace-match "\n<blubb>"))
634 (nnweb-decode-entities)
635 (goto-char (point-min))
636 (while (re-search-forward "<blubb>.*href=\"\\([^\"]+\\)\"><strong>\\([^>]*\\)</strong></a><dd>\\([^-]+\\)- <b>\\([^<]+\\)<.*href=\"news:\\([^\"]+\\)\">.*\">\\(.+\\)</a><P>"
638 (setq url (match-string 1)
639 subject (match-string 2)
640 date (match-string 3)
641 group (match-string 4)
642 id (concat "<" (match-string 5) ">")
643 from (match-string 6))
645 (unless (nnweb-get-hashtb url)
649 (make-full-mail-header
650 (cdr active) (concat "(" group ") " subject) from date
653 (nnweb-set-hashtb (cadar map) (car map))))
654 ;; See if we want more.
655 (when (or (not nnweb-articles)
656 (>= i nnweb-max-hits)
657 (not (funcall (nnweb-definition 'search)
658 nnweb-search (incf part))))
660 ;; Return the articles in the right order.
662 (sort (nconc nnweb-articles map) 'car-less-than-car)))))))
664 (defun nnweb-altavista-wash-article ()
665 (goto-char (point-min))
666 (let ((case-fold-search t))
667 (when (re-search-forward "^<strong>" nil t)
668 (delete-region (point-min) (match-beginning 0)))
669 (goto-char (point-min))
670 (while (looking-at "<strong>\\([^ ]+\\) +</strong> +\\(.*\\)$")
671 (replace-match "\\1: \\2" t)
673 (when (re-search-backward "^References:" nil t)
674 (narrow-to-region (point) (progn (forward-line 1) (point)))
675 (goto-char (point-min))
676 (while (re-search-forward "<A.*\\?id@\\([^\"]+\\)\">[0-9]+</A>" nil t)
677 (replace-match "<\\1> " t)))
679 (nnweb-remove-markup)))
681 (defun nnweb-altavista-search (search &optional part)
682 (url-insert-file-contents
684 (nnweb-definition 'address)
686 (nnweb-encode-www-form-urlencoded
689 ,@(when part `(("stq" . ,(int-to-string (* part 30)))))
695 (setq buffer-file-name nil)
699 ;;; Deja bought by google.com
702 (defun nnweb-google-wash-article ()
703 (let ((case-fold-search t) url)
704 (goto-char (point-min))
705 (re-search-forward "^<pre>" nil t)
706 (narrow-to-region (point-min) (point))
707 (search-backward "<table " nil t 2)
708 (delete-region (point-min) (point))
709 (if (re-search-forward "Search Result [0-9]+" nil t)
711 (if (re-search-forward "View complete thread ([0-9]+ articles?)" nil t)
713 (goto-char (point-min))
714 (while (search-forward "<br>" nil t)
715 (replace-match "\n"))
716 (nnweb-remove-markup)
717 (goto-char (point-min))
718 (while (re-search-forward "^[ \t]*\n" nil t)
720 (goto-char (point-max))
723 (narrow-to-region (point) (point-max))
724 (search-forward "</pre>" nil t)
725 (delete-region (point) (point-max))
726 (nnweb-remove-markup)
729 (defun nnweb-google-parse-1 (&optional Message-ID)
732 (active (cadr (assoc nnweb-group nnweb-group-alist)))
733 Subject Score Date Newsgroups From
736 (push (list nnweb-group (setq active (cons 1 0))
737 nnweb-type nnweb-search)
739 ;; Go through all the article hits on this page.
740 (goto-char (point-min))
741 (while (re-search-forward
742 "a href=/groups\\(\\?[^ \">]*selm=[^ \">]*\\)" nil t)
744 (concat (nnweb-definition 'address)
746 (narrow-to-region (search-forward ">" nil t)
747 (search-forward "</a>" nil t))
748 (nnweb-remove-markup)
749 (nnweb-decode-entities)
750 (setq Subject (buffer-string))
751 (goto-char (point-max))
754 (when (looking-at "<br><font[^>]+>")
755 (goto-char (match-end 0)))
756 (if (not (looking-at "<a[^>]+>"))
757 (skip-chars-forward " \t")
758 (narrow-to-region (point)
759 (search-forward "</a>" nil t))
760 (nnweb-remove-markup)
761 (nnweb-decode-entities)
762 (setq Newsgroups (buffer-string))
763 (goto-char (point-max))
765 (skip-chars-forward "- \t"))
767 "\\([0-9]+[/ ][A-Za-z]+[/ ][0-9]+\\)[ \t]*by[ \t]*\\([^<]*\\) - <a")
768 (setq From (match-string 2)
769 Date (match-string 1)))
772 (unless (nnweb-get-hashtb url)
776 (make-full-mail-header
777 (cdr active) (if Newsgroups
778 (concat "(" Newsgroups ") " Subject)
783 (nnweb-set-hashtb (cadar map) (car map))))
786 (defun nnweb-google-reference (id)
787 (let ((map (nnweb-google-parse-1 id)) header)
789 (nconc nnweb-articles map))
790 (when (setq header (cadar map))
791 (mm-with-unibyte-current-buffer
792 (nnweb-fetch-url (mail-header-xref header)))
795 (defun nnweb-google-create-mapping ()
796 "Perform the search and create an number-to-url alist."
798 (set-buffer nnweb-buffer)
800 (when (funcall (nnweb-definition 'search) nnweb-search)
804 (nconc nnweb-articles (nnweb-google-parse-1)))
805 ;; FIXME: There is more.
807 ;; Return the articles in the right order.
809 (sort nnweb-articles 'car-less-than-car))))))
811 (defun nnweb-google-search (search)
814 (nnweb-definition 'address)
816 (nnweb-encode-www-form-urlencoded
823 ("sites" . "groups")))))
826 (defun nnweb-google-identity (url)
827 "Return an unique identifier based on URL."
828 (if (string-match "seld=\\([0-9]+\\)" url)
833 ;;; General web/w3 interface utility functions
836 (defun nnweb-insert-html (parse)
837 "Insert HTML based on a w3 parse tree."
839 (insert (nnheader-string-as-multibyte parse))
840 (insert "<" (symbol-name (car parse)) " ")
843 (concat (symbol-name (car param)) "="
845 (if (consp (cdr param))
851 (mapcar 'nnweb-insert-html (nth 2 parse))
852 (insert "</" (symbol-name (car parse)) ">\n")))
854 (defun nnweb-encode-www-form-urlencoded (pairs)
855 "Return PAIRS encoded for forms."
859 (concat (w3-form-encode-xwfu (car data)) "="
860 (w3-form-encode-xwfu (cdr data)))))
863 (defun nnweb-fetch-form (url pairs)
864 "Fetch a form from URL with PAIRS as the data using the POST method."
865 (let ((url-request-data (nnweb-encode-www-form-urlencoded pairs))
866 (url-request-method "POST")
867 (url-request-extra-headers
868 '(("Content-type" . "application/x-www-form-urlencoded"))))
869 (url-insert-file-contents url)
870 (setq buffer-file-name nil))
873 (defun nnweb-decode-entities ()
874 "Decode all HTML entities."
875 (goto-char (point-min))
876 (while (re-search-forward "&\\(#[0-9]+\\|[a-z]+\\);" nil t)
877 (let ((elem (if (eq (aref (match-string 1) 0) ?\#)
879 (string-to-number (substring
880 (match-string 1) 1))))
881 (if (mm-char-or-char-int-p c) c 32))
882 (or (cdr (assq (intern (match-string 1))
885 (unless (stringp elem)
886 (setq elem (char-to-string elem)))
887 (replace-match elem t t))))
889 (defun nnweb-decode-entities-string (string)
892 (nnweb-decode-entities)
893 (buffer-substring (point-min) (point-max))))
895 (defun nnweb-remove-markup ()
896 "Remove all HTML markup, leaving just plain text."
897 (goto-char (point-min))
898 (while (search-forward "<!--" nil t)
899 (delete-region (match-beginning 0)
900 (or (search-forward "-->" nil t)
902 (goto-char (point-min))
903 (while (re-search-forward "<[^>]+>" nil t)
904 (replace-match "" t t)))
906 (defun nnweb-insert (url &optional follow-refresh)
907 "Insert the contents from an URL in the current buffer.
908 If FOLLOW-REFRESH is non-nil, redirect refresh url in META."
909 (let ((name buffer-file-name))
912 (narrow-to-region (point) (point))
913 (url-insert-file-contents url)
914 (goto-char (point-min))
915 (when (re-search-forward
916 "<meta[ \t\r\n]*http-equiv=\"Refresh\"[^>]*URL=\\([^\"]+\\)\"" nil t)
917 (let ((url (match-string 1)))
918 (delete-region (point-min) (point-max))
919 (nnweb-insert url t))))
920 (url-insert-file-contents url))
921 (setq buffer-file-name name)))
923 (defun nnweb-parse-find (type parse &optional maxdepth)
924 "Find the element of TYPE in PARSE."
926 (nnweb-parse-find-1 type parse maxdepth)))
928 (defun nnweb-parse-find-1 (type contents maxdepth)
929 (when (or (null maxdepth)
930 (not (zerop maxdepth)))
931 (when (consp contents)
932 (when (eq (car contents) type)
933 (throw 'found contents))
934 (when (listp (cdr contents))
935 (dolist (element contents)
936 (when (consp element)
937 (nnweb-parse-find-1 type element
938 (and maxdepth (1- maxdepth)))))))))
940 (defun nnweb-parse-find-all (type parse)
941 "Find all elements of TYPE in PARSE."
943 (nnweb-parse-find-all-1 type parse)))
945 (defun nnweb-parse-find-all-1 (type contents)
947 (when (consp contents)
948 (if (eq (car contents) type)
949 (push contents result)
950 (when (listp (cdr contents))
951 (dolist (element contents)
952 (when (consp element)
954 (nconc result (nnweb-parse-find-all-1 type element))))))))
958 (defun nnweb-text (parse)
959 "Return a list of text contents in PARSE."
960 (let ((nnweb-text nil))
962 (nreverse nnweb-text)))
964 (defun nnweb-text-1 (contents)
965 (dolist (element contents)
966 (if (stringp element)
967 (push element nnweb-text)
968 (when (and (consp element)
969 (listp (cdr element)))
970 (nnweb-text-1 element)))))
972 (defun nnweb-replace-in-string (string match newtext)
973 (while (string-match match string)
974 (setq string (replace-match newtext t t string)))
979 ;;; nnweb.el ends here