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