e48d0d48354e34e68158891c7363dc2348761f9f
[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 (require 'executable)
34
35 (eval-when-compile (require 'cl))
36
37 (eval-and-compile
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     (url-insert-file-contents url)))
255
256 (defun mm-url-insert-file-contents-external (url)
257   (let (program args)
258     (if (symbolp mm-url-program)
259         (let ((item (cdr (assq mm-url-program mm-url-predefined-programs))))
260           (setq program (car item)
261                 args (append (cdr item) (list url))))
262       (setq program mm-url-program
263             args (append mm-url-arguments (list url))))
264     (apply 'call-process program nil t nil args)))
265
266 (defun mm-url-insert (url &optional follow-refresh)
267   "Insert the contents from an URL in the current buffer.
268 If FOLLOW-REFRESH is non-nil, redirect refresh url in META."
269   (let ((name buffer-file-name))
270     (if follow-refresh
271         (save-restriction
272           (narrow-to-region (point) (point))
273           (mm-url-insert-file-contents url)
274           (goto-char (point-min))
275           (when (re-search-forward
276                  "<meta[ \t\r\n]*http-equiv=\"Refresh\"[^>]*URL=\\([^\"]+\\)\"" nil t)
277             (let ((url (match-string 1)))
278               (delete-region (point-min) (point-max))
279               (mm-url-insert url t))))
280       (mm-url-insert-file-contents url))
281     (setq buffer-file-name name)))
282
283 (defun mm-url-decode-entities ()
284   "Decode all HTML entities."
285   (goto-char (point-min))
286   (while (re-search-forward "&\\(#[0-9]+\\|[a-z]+\\);" nil t)
287     (let ((elem (if (eq (aref (match-string 1) 0) ?\#)
288                         (let ((c
289                                (string-to-number (substring
290                                                   (match-string 1) 1))))
291                           (if (mm-char-or-char-int-p c) c 32))
292                       (or (cdr (assq (intern (match-string 1))
293                                      mm-url-html-entities))
294                           ?#))))
295       (unless (stringp elem)
296         (setq elem (char-to-string elem)))
297       (replace-match elem t t))))
298
299 (defun mm-url-decode-entities-string (string)
300   (with-temp-buffer
301     (insert string)
302     (mm-url-decode-entities)
303     (buffer-substring (point-min) (point-max))))
304
305 (defun mm-url-form-encode-xwfu (chunk)
306   "Escape characters in a string for application/x-www-form-urlencoded.
307 Blasphemous crap because someone didn't think %20 was good enough for encoding
308 spaces.  Die Die Die."
309   ;; This will get rid of the 'attributes' specified by the file type,
310   ;; which are useless for an application/x-www-form-urlencoded form.
311   (if (consp chunk)
312       (setq chunk (cdr chunk)))
313
314   (mapconcat
315    (lambda (char)
316      (cond
317       ((= char ?  ) "+")
318       ((memq char mm-url-unreserved-chars) (char-to-string char))
319       (t (upcase (format "%%%02x" char)))))
320    ;; Fixme: Should this actually be accepting multibyte?  Is there a
321    ;; better way in XEmacs?
322    (if (featurep 'mule)
323        (encode-coding-string chunk
324                              (if (fboundp 'find-coding-systems-string)
325                                  (car (find-coding-systems-string chunk))
326                                  buffer-file-coding-system))
327      chunk)
328    ""))
329
330 (provide 'mm-url)
331
332 ;;; mm-url.el ends here