39bf7765c57a1d227e64cf336f7fc3fb919431e8
[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 (require 'mm-util)
33
34 (eval-when-compile (require 'cl))
35
36 (eval-and-compile
37   (autoload 'url-insert-file-contents "url-handlers"))
38
39 (defgroup mm-url nil
40   "A wrapper of url package and external url command for Gnus."
41   :group 'gnus)
42
43 (defcustom mm-url-use-external (not
44                                 (condition-case nil
45                                     (require 'url-handlers)
46                                   (error nil)))
47   "*If not-nil, use external grab program `mm-url-program'."
48   :type 'boolean
49   :group 'mm-url)
50
51 (defvar mm-url-predefined-programs
52   '((wget "wget" "-q" "-O" "-")
53     (lynx "lynx" "-source")
54     (curl "curl")))
55
56 (defcustom mm-url-program 
57   (cond
58    ((executable-find "wget") 'wget)
59    ((executable-find "lynx") 'lynx)
60    ((executable-find "curl") 'curl)
61    (t "GET"))
62   "The url grab program."
63   :type '(choice 
64           (symbol :tag "wget" wget)
65           (symbol :tag "lynx" lynx)
66           (symbol :tag "curl" curl)
67           (string :tag "other"))
68   :group 'mm-url)
69
70 (defcustom mm-url-arguments nil
71   "The arguments for `mm-url-program'."
72   :type '(repeat string)
73   :group 'mm-url)
74
75 ;; Stolen from w3.
76 (defvar mm-url-html-entities
77   '(
78     ;;(excl        .  33)
79     (quot        .  34)
80     ;;(num         .  35)
81     ;;(dollar      .  36)
82     ;;(percent     .  37)
83     (amp         .  38)
84     (rsquo       .  39)                 ; should be U+8217
85     ;;(apos        .  39)
86     ;;(lpar        .  40)
87     ;;(rpar        .  41)
88     ;;(ast         .  42)
89     ;;(plus        .  43)
90     ;;(comma       .  44)
91     ;;(period      .  46)
92     ;;(colon       .  58)
93     ;;(semi        .  59)
94     (lt          .  60)
95     ;;(equals      .  61)
96     (gt          .  62)
97     ;;(quest       .  63)
98     ;;(commat      .  64)
99     ;;(lsqb        .  91)
100     ;;(rsqb        .  93)
101     (uarr        .  94)                 ; should be U+8593
102     ;;(lowbar      .  95)
103     (lsquo       .  96)                 ; should be U+8216
104     (lcub        . 123)
105     ;;(verbar      . 124)
106     (rcub        . 125)
107     (tilde       . 126)
108     (nbsp        . 160)
109     (iexcl       . 161)
110     (cent        . 162)
111     (pound       . 163)
112     (curren      . 164)
113     (yen         . 165)
114     (brvbar      . 166)
115     (sect        . 167)
116     (uml         . 168)
117     (copy        . 169)
118     (ordf        . 170)
119     (laquo       . 171)
120     (not         . 172)
121     (shy         . 173)
122     (reg         . 174)
123     (macr        . 175)
124     (deg         . 176)
125     (plusmn      . 177)
126     (sup2        . 178)
127     (sup3        . 179)
128     (acute       . 180)
129     (micro       . 181)
130     (para        . 182)
131     (middot      . 183)
132     (cedil       . 184)
133     (sup1        . 185)
134     (ordm        . 186)
135     (raquo       . 187)
136     (frac14      . 188)
137     (frac12      . 189)
138     (frac34      . 190)
139     (iquest      . 191)
140     (Agrave      . 192)
141     (Aacute      . 193)
142     (Acirc       . 194)
143     (Atilde      . 195)
144     (Auml        . 196)
145     (Aring       . 197)
146     (AElig       . 198)
147     (Ccedil      . 199)
148     (Egrave      . 200)
149     (Eacute      . 201)
150     (Ecirc       . 202)
151     (Euml        . 203)
152     (Igrave      . 204)
153     (Iacute      . 205)
154     (Icirc       . 206)
155     (Iuml        . 207)
156     (ETH         . 208)
157     (Ntilde      . 209)
158     (Ograve      . 210)
159     (Oacute      . 211)
160     (Ocirc       . 212)
161     (Otilde      . 213)
162     (Ouml        . 214)
163     (times       . 215)
164     (Oslash      . 216)
165     (Ugrave      . 217)
166     (Uacute      . 218)
167     (Ucirc       . 219)
168     (Uuml        . 220)
169     (Yacute      . 221)
170     (THORN       . 222)
171     (szlig       . 223)
172     (agrave      . 224)
173     (aacute      . 225)
174     (acirc       . 226)
175     (atilde      . 227)
176     (auml        . 228)
177     (aring       . 229)
178     (aelig       . 230)
179     (ccedil      . 231)
180     (egrave      . 232)
181     (eacute      . 233)
182     (ecirc       . 234)
183     (euml        . 235)
184     (igrave      . 236)
185     (iacute      . 237)
186     (icirc       . 238)
187     (iuml        . 239)
188     (eth         . 240)
189     (ntilde      . 241)
190     (ograve      . 242)
191     (oacute      . 243)
192     (ocirc       . 244)
193     (otilde      . 245)
194     (ouml        . 246)
195     (divide      . 247)
196     (oslash      . 248)
197     (ugrave      . 249)
198     (uacute      . 250)
199     (ucirc       . 251)
200     (uuml        . 252)
201     (yacute      . 253)
202     (thorn       . 254)
203     (yuml        . 255)
204
205     ;; Special handling of these
206     (frac56      . "5/6")
207     (frac16      . "1/6")
208     (frac45      . "4/5")
209     (frac35      . "3/5")
210     (frac25      . "2/5")
211     (frac15      . "1/5")
212     (frac23      . "2/3")
213     (frac13      . "1/3")
214     (frac78      . "7/8")
215     (frac58      . "5/8")
216     (frac38      . "3/8")
217     (frac18      . "1/8")
218
219     ;; The following 5 entities are not mentioned in the HTML 2.0
220     ;; standard, nor in any other HTML proposed standard of which I
221     ;; am aware.  I am not even sure they are ISO entity names.  ***
222     ;; Hence, some arrangement should be made to give a bad HTML
223     ;; message when they are seen.
224     (ndash       .  45)
225     (mdash       .  45)
226     (emsp        .  32)
227     (ensp        .  32)
228     (sim         . 126)
229     (le          . "<=")
230     (agr         . "alpha")
231     (rdquo       . "''")
232     (ldquo       . "``")
233     (trade       . "(TM)")
234     ;; To be done
235     ;; (shy      . ????) ; soft hyphen
236     )
237   "*An assoc list of entity names and how to actually display them.")
238
239 (defconst mm-url-unreserved-chars
240   '(
241     ?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
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     ?0 ?1 ?2 ?3 ?4 ?5 ?6 ?7 ?8 ?9
244     ?- ?_ ?. ?! ?~ ?* ?' ?\( ?\))
245   "A list of characters that are _NOT_ reserved in the URL spec.
246 This is taken from RFC 2396.")
247
248 (defun mm-url-insert-file-contents (url)
249   (if mm-url-use-external
250       (mm-url-insert-file-contents-external url)
251     (url-insert-file-contents url)))
252
253 (defun mm-url-insert-file-contents-external (url)
254   (let (program args)
255     (if (symbolp mm-url-program)
256         (let ((item (cdr (assq mm-url-program mm-url-predefined-programs))))
257           (setq program (car item)
258                 args (append (cdr item) (list url))))
259       (setq program mm-url-program
260             args (append mm-url-arguments (list url))))
261     (apply 'call-process program nil t nil args)))
262
263 (defun mm-url-insert (url &optional follow-refresh)
264   "Insert the contents from an URL in the current buffer.
265 If FOLLOW-REFRESH is non-nil, redirect refresh url in META."
266   (let ((name buffer-file-name))
267     (if follow-refresh
268         (save-restriction
269           (narrow-to-region (point) (point))
270           (mm-url-insert-file-contents url)
271           (goto-char (point-min))
272           (when (re-search-forward
273                  "<meta[ \t\r\n]*http-equiv=\"Refresh\"[^>]*URL=\\([^\"]+\\)\"" nil t)
274             (let ((url (match-string 1)))
275               (delete-region (point-min) (point-max))
276               (mm-url-insert url t))))
277       (mm-url-insert-file-contents url))
278     (setq buffer-file-name name)))
279
280 (defun mm-url-decode-entities ()
281   "Decode all HTML entities."
282   (goto-char (point-min))
283   (while (re-search-forward "&\\(#[0-9]+\\|[a-z]+\\);" nil t)
284     (let ((elem (if (eq (aref (match-string 1) 0) ?\#)
285                         (let ((c
286                                (string-to-number (substring
287                                                   (match-string 1) 1))))
288                           (if (mm-char-or-char-int-p c) c 32))
289                       (or (cdr (assq (intern (match-string 1))
290                                      mm-url-html-entities))
291                           ?#))))
292       (unless (stringp elem)
293         (setq elem (char-to-string elem)))
294       (replace-match elem t t))))
295
296 (defun mm-url-decode-entities-string (string)
297   (with-temp-buffer
298     (insert string)
299     (mm-url-decode-entities)
300     (buffer-substring (point-min) (point-max))))
301
302 (defun mm-url-form-encode-xwfu (chunk)
303   "Escape characters in a string for application/x-www-form-urlencoded.
304 Blasphemous crap because someone didn't think %20 was good enough for encoding
305 spaces.  Die Die Die."
306   ;; This will get rid of the 'attributes' specified by the file type,
307   ;; which are useless for an application/x-www-form-urlencoded form.
308   (if (consp chunk)
309       (setq chunk (cdr chunk)))
310
311   (mapconcat
312    (lambda (char)
313      (cond
314       ((= char ?  ) "+")
315       ((memq char mm-url-unreserved-chars) (char-to-string char))
316       (t (upcase (format "%%%02x" char)))))
317    ;; Fixme: Should this actually be accepting multibyte?  Is there a
318    ;; better way in XEmacs?
319    (if (featurep 'mule)
320        (encode-coding-string chunk
321                              (if (fboundp 'find-coding-systems-string)
322                                  (car (find-coding-systems-string chunk))
323                                  buffer-file-coding-system))
324      chunk)
325    ""))
326
327 (provide 'mm-url)
328
329 ;;; mm-url.el ends here