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