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