Revert experimental changes.
[gnus] / lisp / mm-url.el
1 ;;; mm-url.el --- a wrapper of url functions/commands for Gnus
2 ;; Copyright (C) 2001 Free Software Foundation, Inc.
3
4 ;; Author: Shenghuo Zhu <zsh@cs.rochester.edu>
5
6 ;; This file is part of GNU Emacs.
7
8 ;; GNU Emacs is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published
10 ;; by the Free Software Foundation; either version 2, or (at your
11 ;; option) any later version.
12
13 ;; GNU Emacs is distributed in the hope that it will be useful, but
14 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 ;; General Public License for more details.
17
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
20 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 ;; Boston, MA 02111-1307, USA.
22
23 ;;; Commentary:
24
25 ;; Some codes are stolen from w3 and url packages. Some are moved from
26 ;; nnweb.
27
28 ;; TODO: Support POST, cookie.
29
30 ;;; Code:
31
32 (eval-when-compile (require 'cl))
33
34 (require 'mm-util)
35
36 (eval-and-compile
37   (autoload 'executable-find "executable")
38   (autoload 'url-insert-file-contents "url-handlers"))
39
40 (defgroup mm-url nil
41   "A wrapper of url package and external url command for Gnus."
42   :group 'gnus)
43
44 (defcustom mm-url-use-external (not
45                                 (condition-case nil
46                                     (require 'url-handlers)
47                                   (error nil)))
48   "*If not-nil, use external grab program `mm-url-program'."
49   :type 'boolean
50   :group 'mm-url)
51
52 (defvar mm-url-predefined-programs
53   '((wget "wget" "-q" "-O" "-")
54     (lynx "lynx" "-source")
55     (curl "curl")))
56
57 (defcustom mm-url-program 
58   (cond
59    ((executable-find "wget") 'wget)
60    ((executable-find "lynx") 'lynx)
61    ((executable-find "curl") 'curl)
62    (t "GET"))
63   "The url grab program."
64   :type '(choice 
65           (symbol :tag "wget" wget)
66           (symbol :tag "lynx" lynx)
67           (symbol :tag "curl" curl)
68           (string :tag "other"))
69   :group 'mm-url)
70
71 (defcustom mm-url-arguments nil
72   "The arguments for `mm-url-program'."
73   :type '(repeat string)
74   :group 'mm-url)
75
76 ;; Stolen from w3.
77 (defvar mm-url-html-entities
78   '(
79     ;;(excl        .  33)
80     (quot        .  34)
81     ;;(num         .  35)
82     ;;(dollar      .  36)
83     ;;(percent     .  37)
84     (amp         .  38)
85     (rsquo       .  39)                 ; should be U+8217
86     ;;(apos        .  39)
87     ;;(lpar        .  40)
88     ;;(rpar        .  41)
89     ;;(ast         .  42)
90     ;;(plus        .  43)
91     ;;(comma       .  44)
92     ;;(period      .  46)
93     ;;(colon       .  58)
94     ;;(semi        .  59)
95     (lt          .  60)
96     ;;(equals      .  61)
97     (gt          .  62)
98     ;;(quest       .  63)
99     ;;(commat      .  64)
100     ;;(lsqb        .  91)
101     ;;(rsqb        .  93)
102     (uarr        .  94)                 ; should be U+8593
103     ;;(lowbar      .  95)
104     (lsquo       .  96)                 ; should be U+8216
105     (lcub        . 123)
106     ;;(verbar      . 124)
107     (rcub        . 125)
108     (tilde       . 126)
109     (nbsp        . 160)
110     (iexcl       . 161)
111     (cent        . 162)
112     (pound       . 163)
113     (curren      . 164)
114     (yen         . 165)
115     (brvbar      . 166)
116     (sect        . 167)
117     (uml         . 168)
118     (copy        . 169)
119     (ordf        . 170)
120     (laquo       . 171)
121     (not         . 172)
122     (shy         . 173)
123     (reg         . 174)
124     (macr        . 175)
125     (deg         . 176)
126     (plusmn      . 177)
127     (sup2        . 178)
128     (sup3        . 179)
129     (acute       . 180)
130     (micro       . 181)
131     (para        . 182)
132     (middot      . 183)
133     (cedil       . 184)
134     (sup1        . 185)
135     (ordm        . 186)
136     (raquo       . 187)
137     (frac14      . 188)
138     (frac12      . 189)
139     (frac34      . 190)
140     (iquest      . 191)
141     (Agrave      . 192)
142     (Aacute      . 193)
143     (Acirc       . 194)
144     (Atilde      . 195)
145     (Auml        . 196)
146     (Aring       . 197)
147     (AElig       . 198)
148     (Ccedil      . 199)
149     (Egrave      . 200)
150     (Eacute      . 201)
151     (Ecirc       . 202)
152     (Euml        . 203)
153     (Igrave      . 204)
154     (Iacute      . 205)
155     (Icirc       . 206)
156     (Iuml        . 207)
157     (ETH         . 208)
158     (Ntilde      . 209)
159     (Ograve      . 210)
160     (Oacute      . 211)
161     (Ocirc       . 212)
162     (Otilde      . 213)
163     (Ouml        . 214)
164     (times       . 215)
165     (Oslash      . 216)
166     (Ugrave      . 217)
167     (Uacute      . 218)
168     (Ucirc       . 219)
169     (Uuml        . 220)
170     (Yacute      . 221)
171     (THORN       . 222)
172     (szlig       . 223)
173     (agrave      . 224)
174     (aacute      . 225)
175     (acirc       . 226)
176     (atilde      . 227)
177     (auml        . 228)
178     (aring       . 229)
179     (aelig       . 230)
180     (ccedil      . 231)
181     (egrave      . 232)
182     (eacute      . 233)
183     (ecirc       . 234)
184     (euml        . 235)
185     (igrave      . 236)
186     (iacute      . 237)
187     (icirc       . 238)
188     (iuml        . 239)
189     (eth         . 240)
190     (ntilde      . 241)
191     (ograve      . 242)
192     (oacute      . 243)
193     (ocirc       . 244)
194     (otilde      . 245)
195     (ouml        . 246)
196     (divide      . 247)
197     (oslash      . 248)
198     (ugrave      . 249)
199     (uacute      . 250)
200     (ucirc       . 251)
201     (uuml        . 252)
202     (yacute      . 253)
203     (thorn       . 254)
204     (yuml        . 255)
205
206     ;; Special handling of these
207     (frac56      . "5/6")
208     (frac16      . "1/6")
209     (frac45      . "4/5")
210     (frac35      . "3/5")
211     (frac25      . "2/5")
212     (frac15      . "1/5")
213     (frac23      . "2/3")
214     (frac13      . "1/3")
215     (frac78      . "7/8")
216     (frac58      . "5/8")
217     (frac38      . "3/8")
218     (frac18      . "1/8")
219
220     ;; The following 5 entities are not mentioned in the HTML 2.0
221     ;; standard, nor in any other HTML proposed standard of which I
222     ;; am aware.  I am not even sure they are ISO entity names.  ***
223     ;; Hence, some arrangement should be made to give a bad HTML
224     ;; message when they are seen.
225     (ndash       .  45)
226     (mdash       .  45)
227     (emsp        .  32)
228     (ensp        .  32)
229     (sim         . 126)
230     (le          . "<=")
231     (agr         . "alpha")
232     (rdquo       . "''")
233     (ldquo       . "``")
234     (trade       . "(TM)")
235     ;; To be done
236     ;; (shy      . ????) ; soft hyphen
237     )
238   "*An assoc list of entity names and how to actually display them.")
239
240 (defconst mm-url-unreserved-chars
241   '(
242     ?a ?b ?c ?d ?e ?f ?g ?h ?i ?j ?k ?l ?m ?n ?o ?p ?q ?r ?s ?t ?u ?v ?w ?x ?y ?z
243     ?A ?B ?C ?D ?E ?F ?G ?H ?I ?J ?K ?L ?M ?N ?O ?P ?Q ?R ?S ?T ?U ?V ?W ?X ?Y ?Z
244     ?0 ?1 ?2 ?3 ?4 ?5 ?6 ?7 ?8 ?9
245     ?- ?_ ?. ?! ?~ ?* ?' ?\( ?\))
246   "A list of characters that are _NOT_ reserved in the URL spec.
247 This is taken from RFC 2396.")
248
249 (defun mm-url-insert-file-contents (url)
250   (if mm-url-use-external
251       (if (string-match "^file:/+" url)
252           (insert-file-contents (substring url (1- (match-end 0))))
253         (mm-url-insert-file-contents-external url))
254     (require 'url-handlers)
255     (let ((name buffer-file-name))
256       (prog1
257           (url-insert-file-contents url)
258         (setq buffer-file-name name)))))
259
260 (defun mm-url-insert-file-contents-external (url)
261   (let (program args)
262     (if (symbolp mm-url-program)
263         (let ((item (cdr (assq mm-url-program mm-url-predefined-programs))))
264           (setq program (car item)
265                 args (append (cdr item) (list url))))
266       (setq program mm-url-program
267             args (append mm-url-arguments (list url))))
268     (apply 'call-process program nil t nil args)))
269
270 (defun mm-url-insert (url &optional follow-refresh)
271   "Insert the contents from an URL in the current buffer.
272 If FOLLOW-REFRESH is non-nil, redirect refresh url in META."
273   (if follow-refresh
274       (save-restriction
275         (narrow-to-region (point) (point))
276         (mm-url-insert-file-contents url)
277         (goto-char (point-min))
278         (when (re-search-forward
279                "<meta[ \t\r\n]*http-equiv=\"Refresh\"[^>]*URL=\\([^\"]+\\)\"" nil t)
280           (let ((url (match-string 1)))
281             (delete-region (point-min) (point-max))
282             (mm-url-insert url t))))
283     (mm-url-insert-file-contents url)))
284
285 (defun mm-url-decode-entities ()
286   "Decode all HTML entities."
287   (goto-char (point-min))
288   (while (re-search-forward "&\\(#[0-9]+\\|[a-z]+\\);" nil t)
289     (let ((elem (if (eq (aref (match-string 1) 0) ?\#)
290                         (let ((c
291                                (string-to-number (substring
292                                                   (match-string 1) 1))))
293                           (if (mm-char-or-char-int-p c) c 32))
294                       (or (cdr (assq (intern (match-string 1))
295                                      mm-url-html-entities))
296                           ?#))))
297       (unless (stringp elem)
298         (setq elem (char-to-string elem)))
299       (replace-match elem t t))))
300
301 (defun mm-url-decode-entities-nbsp ()
302   "Decode all HTML entities and &nbsp; to a space."
303   (let ((mm-url-html-entities (cons '(nbsp . 32) mm-url-html-entities)))
304     (mm-url-decode-entities)))
305
306 (defun mm-url-decode-entities-string (string)
307   (with-temp-buffer
308     (insert string)
309     (mm-url-decode-entities)
310     (buffer-substring (point-min) (point-max))))
311
312 (defun mm-url-form-encode-xwfu (chunk)
313   "Escape characters in a string for application/x-www-form-urlencoded.
314 Blasphemous crap because someone didn't think %20 was good enough for encoding
315 spaces.  Die Die Die."
316   ;; This will get rid of the 'attributes' specified by the file type,
317   ;; which are useless for an application/x-www-form-urlencoded form.
318   (if (consp chunk)
319       (setq chunk (cdr chunk)))
320
321   (mapconcat
322    (lambda (char)
323      (cond
324       ((= char ?  ) "+")
325       ((memq char mm-url-unreserved-chars) (char-to-string char))
326       (t (upcase (format "%%%02x" char)))))
327    ;; Fixme: Should this actually be accepting multibyte?  Is there a
328    ;; better way in XEmacs?
329    (if (featurep 'mule)
330        (encode-coding-string chunk
331                              (if (fboundp 'find-coding-systems-string)
332                                  (car (find-coding-systems-string chunk))
333                                  buffer-file-coding-system))
334      chunk)
335    ""))
336
337 (defun mm-url-encode-www-form-urlencoded (pairs)
338   "Return PAIRS encoded for forms."
339   (mapconcat
340    (lambda (data)
341      (concat (mm-url-form-encode-xwfu (car data)) "="
342              (mm-url-form-encode-xwfu (cdr data))))
343    pairs "&"))
344
345 (defun mm-url-fetch-form (url pairs)
346   "Fetch a form from URL with PAIRS as the data using the POST method."
347   (require 'url-handlers)
348   (let ((url-request-data (mm-url-encode-www-form-urlencoded pairs))
349         (url-request-method "POST")
350         (url-request-extra-headers
351          '(("Content-type" . "application/x-www-form-urlencoded"))))
352     (url-insert-file-contents url)
353     (setq buffer-file-name nil))
354   t)
355
356 (defun mm-url-fetch-simple (url content)
357   (require 'url-handlers)
358   (let ((url-request-data content)
359         (url-request-method "POST")
360         (url-request-extra-headers
361          '(("Content-type" . "application/x-www-form-urlencoded"))))
362     (url-insert-file-contents url)
363     (setq buffer-file-name nil))
364   t)
365
366 (defun mm-url-remove-markup ()
367   "Remove all HTML markup, leaving just plain text."
368   (goto-char (point-min))
369   (while (search-forward "<!--" nil t)
370     (delete-region (match-beginning 0)
371                    (or (search-forward "-->" nil t)
372                        (point-max))))
373   (goto-char (point-min))
374   (while (re-search-forward "<[^>]+>" nil t)
375     (replace-match "" t t)))
376
377 (provide 'mm-url)
378
379 ;;; mm-url.el ends here