Debian Codegraph @

mew-mime.el

raw
1 ;;; mew-mime.el --- MIME launcher for Mew
2
3 ;; Author: Kazu Yamamoto <[email protected]>
4 ;; Created: Mar 23, 1997
5
6 ;;; Code:
7
8 (require 'mew)
9 (eval-when-compile
10 (require 'mew-env0))
11
12 (eval-when-compile
13 (cond
14 ((memq system-type '(windows-nt cygwin))
15 (require 'mew-win32))
16 ((eq system-type 'darwin)
17 (require 'mew-darwin))
18 (t
19 (require 'mew-unix)))
20 (mew-no-warning-defun w3m-region)
21 (mew-no-warning-defun w3m-expand-file-name-as-url))
22
23 (defvar mew-process-file-alist nil)
24
25 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
26 ;;;
27 ;;; Configuration subroutines
28 ;;;
29
30 (defun mew-progspec-get-prog (def)
31 (cond
32 ((stringp def) def)
33 ((and (listp def) (stringp (car def))) (car def))))
34
35 (defun mew-progspec-get-args (def)
36 (if (listp def) (nth 1 def)))
37
38 (defun mew-progspec-get-async (def)
39 (cond
40 ((stringp def) t)
41 ((listp def) (nth 2 def))))
42
43 (defun mew-progsec-args-convert (args arg)
44 (let (converted ret)
45 (setq ret (mapcar (lambda (x)
46 (if (not (string-match "%s" x))
47 x
48 (setq converted t)
49 (format x arg)))
50 args))
51 (unless converted (setq ret (append ret (list arg))))
52 ret))
53
54 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
55 ;;;
56 ;;; Start and call process
57 ;;;
58
59 (defun mew-mime-start-process (program options file)
60 (let ((process-connection-type mew-connection-type1) pro)
61 (message "Starting %s..." program)
62 (setq pro (apply 'mew-start-process-disp
63 (format "*mew %s*" program)
64 nil
65 program
66 (append options (list file))))
67 (set-process-sentinel pro 'mew-mime-start-process-sentinel)
68 (message "Starting %s...done" program)
69 (setq mew-process-file-alist (cons (cons pro file) mew-process-file-alist))
70 t)) ;; to next part
71
72 (defun mew-mime-start-process-sentinel (process _event)
73 (let* ((al (assoc process mew-process-file-alist))
74 (file (cdr al)))
75 (save-excursion
76 ;; A launcher program may be executed.
77 ;; The launcher program executes an application according to
78 ;; file's suffix or something.
79 ;; The time when the launcher is finished is not the time when
80 ;; the application is finished. So, we can't delete the temporary
81 ;; file here. Hoping that the file will be deleted when Mew is
82 ;; finished.
83 (if mew-delete-temp-file (mew-delete-file file))
84 (setq mew-process-file-alist (delq al mew-process-file-alist)))))
85
86 (defun mew-mime-call-process (program options file)
87 (message "Calling %s..." program)
88 (apply 'call-process program file nil nil options)
89 (mew-delete-file file)
90 (message "Calling %s...done" program)
91 t) ;; to next part
92
93 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
94 ;;;
95 ;;; Part handler
96 ;;;
97
98 ;; see also mew-summary-execute-external().
99
100 (defun mew-mime-part (cache fullpart nums)
101 ;; called in Message buffer
102 ;; if nums is nil, it means singlepart.
103 (let* ((syntax (mew-syntax-get-entry fullpart nums))
104 (begin (mew-syntax-get-begin syntax))
105 (end (mew-syntax-get-end syntax))
106 (ctl (mew-syntax-get-ct syntax))
107 (cte (mew-syntax-get-cte syntax))
108 (ct (mew-syntax-get-value ctl 'cap))
109 (cdpl (mew-syntax-get-cdp syntax))
110 (fname (mew-syntax-get-filename cdpl ctl))
111 (cd (mew-syntax-get-cd syntax))
112 (params (mew-syntax-get-params ctl))
113 (program (mew-ctdb-prog (mew-ctdb-by-ct ct)))
114 func2)
115 (when (listp program)
116 (setq func2 (nth 1 program))
117 (setq program (nth 0 program)))
118 (cond
119 ((null program)
120 (mew-mime-function func2 cache begin end ct cte cd fname))
121 ((symbolp program)
122 (when (fboundp program)
123 (cond
124 ((eq program 'mew-mime-message/rfc822)
125 (funcall program cache syntax)) ;; for recursive MIME
126 ((eq program 'mew-mime-application/octet-stream)
127 (funcall program cache begin end params ct cte cd fname))
128 ((mew-ct-imagep ct)
129 (funcall program cache begin end params fname ct cte))
130 (t
131 (funcall program cache begin end params)))))
132 (t ;; string
133 (insert " ###### ###### ####### ##### ###### # # #\n"
134 " # # # # # # # # # # # # ## ##\n"
135 " # # # # # # # # # # # # # # #\n"
136 " ###### ###### # # # #### ###### # # # # #\n"
137 " # # # # # # # # # ####### # #\n"
138 " # # # # # # # # # # # # #\n"
139 " # # # ####### ##### # # # # # #\n"
140 "\n\n")
141 (mew-insert "Content-Type:\t%s\n" ct)
142 (mew-insert "Encoding:\t%s\n" cte)
143 (mew-insert "Size:\t\t%d bytes\n" (mew-region-bytes begin end cache))
144 (mew-insert "Filename:\t%s\n" fname)
145 (mew-insert "Description:\t%s\n" cd)
146 (mew-insert "Program:\t%s\n" program)
147 (insert "\n")
148 (mew-mime-part-messages t)))))
149
150 (defun mew-mime-function (func cache begin end &optional ct cte cd fname)
151 (insert " ####### # # # # ##### ####### ### ####### # #\n"
152 " # # # ## # # # # # # # ## #\n"
153 " # # # # # # # # # # # # # #\n"
154 " ##### # # # # # # # # # # # # #\n"
155 " # # # # # # # # # # # # # #\n"
156 " # # # # ## # # # # # # # ##\n"
157 " # ##### # # ##### # ### ####### # #\n"
158 "\n\n")
159 (mew-insert "Content-Type:\t%s\n" ct)
160 (mew-insert "Encoding:\t%s\n" cte)
161 (mew-insert "Size:\t\t%d bytes\n" (mew-region-bytes begin end cache))
162 (mew-insert "Filename:\t%s\n" fname)
163 (mew-insert "Description:\t%s\n" cd)
164 (mew-insert "Function:\t%s\n" func)
165 (insert "\n")
166 (mew-mime-part-messages func))
167
168 (defun mew-mime-part-messages (prog)
169 (cond
170 ((and prog (fboundp prog))
171 (mew-insert-manual
172 "To execute an external function, type "
173 "'\\<mew-summary-mode-map>\\[mew-summary-execute-external]'.\n"
174 "\n"))
175 (prog
176 (mew-insert-manual
177 "To execute an external command, type "
178 "'\\<mew-summary-mode-map>\\[mew-summary-execute-external]'.\n"
179 "\n")))
180 (mew-insert-manual
181 "To save this part, type "
182 "'\\<mew-summary-mode-map>\\[mew-summary-save]'.\n"
183 "To insert this part in Message mode, type "
184 "'\\<mew-summary-mode-map>\\[mew-summary-display-asis]'.\n"))
185
186 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
187 ;;;
188 ;;; Text/Plain
189 ;;;
190
191 (defvar mew-flowed-auto-wrap t)
192
193 (defun mew-mime-text/plain (cache begin end &optional params)
194 ;; called in Message buffer
195 (when (> end begin)
196 (save-excursion
197 ;; We need to keep properties (e.g. citation color)
198 ;; in a message cache.
199 ;; This must be "insert-buffer-substring".
200 (insert-buffer-substring cache begin end)
201 (when mew-insert-final-newline
202 (save-excursion
203 (goto-char (point-max))
204 (if (not (bolp)) (insert "\n"))))
205 (if (and mew-flowed-auto-wrap
206 (mew-syntax-get-param params "format")
207 (string= (downcase (mew-syntax-get-param params "format"))
208 "flowed"))
209 (mew-message-line))
210 ;; Page breaks
211 (when mew-break-pages
212 (goto-char (point-min))
213 (mew-message-narrow-to-page)))))
214
215 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
216 ;;;
217 ;;; Text/Enriched
218 ;;;
219
220 (defun mew-mime-text/enriched (cache begin end &optional _params)
221 ;; called in Message buffer
222 (when (> end begin)
223 (save-excursion
224 (let ((start (point)))
225 ;; We need to keep composite properties of charset.
226 ;; This must be "insert-buffer-substring".
227 (insert-buffer-substring cache begin end)
228 ;; Highlight
229 (when mew-use-text/enriched
230 (condition-case nil
231 ;; format.el is buggy.
232 (format-decode-region start (point-max) 'text/enriched)
233 (args-out-of-range (message nil))) ;; just clear
234 (enriched-mode 0)))
235 ;; Page breaks
236 (when mew-break-pages
237 (goto-char (point-min))
238 (mew-message-narrow-to-page)))))
239
240 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
241 ;;;
242 ;;; Text/Html
243 ;;;
244
245 (defun mew-mime-text/html (cache begin end &optional _params)
246 ;; called in Message buffer
247 (if (and (symbolp mew-prog-text/html) (fboundp mew-prog-text/html))
248 (save-excursion
249 (let ((start (point))
250 folder)
251 ;; We need to keep composite properties of charset.
252 ;; This must be "insert-buffer-substring".
253 (insert-buffer-substring cache begin end)
254 (with-current-buffer cache
255 (setq folder (mew-cinfo-get-fld)))
256 (if (or mew-use-text/html
257 (and mew-use-text/html-list
258 (mew-folder-spec folder
259 mew-use-text/html-list
260 mew-use-text/html-string-type
261 mew-use-text/html-list-type)))
262 (progn
263 (funcall mew-prog-text/html start (point-max))
264 (delete-trailing-whitespace start (point-max))
265 (mew-highlight-body-region start (point-max)))
266 (mew-message-for-summary "To parse HTML, type '\\[mew-summary-analyze-again]'"))))
267 (insert " # # ####### # # #\n"
268 " # # # ## ## #\n"
269 " # # # # # # # #\n"
270 " ####### # # # # #\n"
271 " # # # # # #\n"
272 " # # # # # #\n"
273 " # # # # # #######\n"
274 "\n\n")
275 (mew-insert "Size:\t\t%d bytes\n" (mew-region-bytes begin end cache))
276 (mew-insert "Browser:\t%s\n" (or (mew-progspec-get-prog mew-prog-text/html-ext) "none"))
277 (insert "\n")
278 (mew-mime-part-messages t)))
279
280 (defun mew-text/html-detect-cs (begin end)
281 (let ((case-fold-search t))
282 (save-excursion
283 (goto-char begin)
284 (when (or (re-search-forward
285 (concat "<meta[ \t]+http-equiv=\"?content-type\"?[ \t]"
286 "+content=\"[^;]+"
287 ";[ \t]*charset=\"?\\([^\"]+\\)\"?[ \t]*/?>")
288 (min end (+ begin 1024)) t)
289 (re-search-forward
290 (concat "<meta[ \t]+content=\"[^;]+"
291 ";[ \t]*charset=\"?\\([^\"]+\\)\"?"
292 "[ \t]+http-equiv=\"?content-type\"?[ \t]*/?>")
293 (min end (+ begin 1024)) t)
294 (re-search-forward
295 "<\\?xml.*encoding=['\"]\\([^'\"]+\\)['\"] *\\?>"
296 (min end (+ begin 1024)) t))
297 (mew-charset-to-cs (mew-match-string 1))))))
298
299 (defun mew-mime-text/html-ext (cache begin end &optional params)
300 (mew-mime-markup-language-ext
301 mew-prog-text/html-ext cache begin end params "HTML" mew-format-html))
302
303 (defun mew-mime-markup-language-ext (program cache begin end params tag form)
304 ;; called in Message buffer
305 (when (> end begin)
306 (let* ((file (format form (mew-make-temp-name)))
307 (prog (mew-progspec-get-prog program))
308 (args (mew-progsec-args-convert (mew-progspec-get-args program) file))
309 wcs)
310 (with-current-buffer cache
311 (message "Displaying %s..." tag)
312 ;; charset check
313 (setq wcs (mew-text/html-detect-cs begin end))
314 ;; note that application/xml may have the charset parameter
315 (unless (mew-coding-system-p wcs)
316 (setq wcs (mew-charset-to-cs
317 (mew-syntax-get-param params "charset"))))
318 (unless (mew-coding-system-p wcs)
319 (setq wcs mew-cs-text-for-write))
320 (mew-frwlet mew-cs-dummy wcs
321 (write-region begin end file nil 'no-msg)
322 (apply 'mew-start-process-disp prog nil prog args))
323 (message "Displaying %s...done" tag)))))
324
325 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
326 ;;;
327 ;;; Text/XML
328 ;;;
329
330 (defun mew-mime-text/xml (cache begin end &optional _params)
331 ;; called in Message buffer
332 (if (and (symbolp mew-prog-text/xml) (fboundp mew-prog-text/xml))
333 (save-excursion
334 (let ((start (point)))
335 ;; We need to keep composite properties of charset.
336 ;; This must be "insert-buffer-substring".
337 (insert-buffer-substring cache begin end)
338 (if mew-use-text/xml
339 (funcall mew-prog-text/xml start (point-max))
340 (mew-message-for-summary "To parse XML, type '\\[mew-summary-analyze-again]'"))))
341 (insert " # # # # #\n"
342 " # # ## ## #\n"
343 " # # # # # # #\n"
344 " # # # # #\n"
345 " # # # # #\n"
346 " # # # # #\n"
347 " # # # # #######\n"
348 "\n\n")
349 (mew-insert "Size:\t\t%d bytes\n" (mew-region-bytes begin end cache))
350 (mew-insert "Browser:\t%s\n" (or (mew-progspec-get-prog mew-prog-text/xml-ext) "none"))
351 (insert "\n")
352 (mew-mime-part-messages t)))
353
354 (defun mew-mime-text/xml-ext (cache begin end &optional params)
355 (mew-mime-markup-language-ext
356 mew-prog-text/xml-ext cache begin end params "XML" mew-format-xml))
357
358 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
359 ;;;
360 ;;; Application/Xml
361 ;;;
362
363 (defun mew-mime-application/xml (cache begin end &optional _params)
364 ;; called in Message buffer
365 (if (and (symbolp mew-prog-application/xml)
366 (fboundp mew-prog-application/xml))
367 (save-excursion
368 (let ((start (point)))
369 ;; We need to keep composite properties of charset.
370 ;; This must be "insert-buffer-substring".
371 (insert-buffer-substring cache begin end)
372 (funcall mew-prog-application/xml start (point-max))))
373 (insert " # # # # #\n"
374 " # # ## ## #\n"
375 " # # # # # # #\n"
376 " # # # # #\n"
377 " # # # # #\n"
378 " # # # # #\n"
379 " # # # # #######\n"
380 "\n\n")
381 (mew-insert "Size:\t\t%d bytes\n" (mew-region-bytes begin end cache))
382 (mew-insert "Browser:\t%s\n" (or (mew-progspec-get-prog mew-prog-application/xml-ext) "none"))
383 (insert "\n")
384 (mew-mime-part-messages t)))
385
386 (defun mew-mime-application/xml-ext (cache begin end &optional params)
387 (mew-mime-markup-language-ext
388 mew-prog-application/xml-ext cache begin end params "XML" mew-format-xml))
389
390 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
391 ;;;
392 ;;; Text/X-Patch
393 ;;;
394
395 (defun mew-mime-text/patch-ext (cache begin end &optional _params)
396 (when (> end begin)
397 (save-excursion
398 (switch-to-buffer-other-frame "*Mew Patch*")
399 (delete-other-windows)
400 (erase-buffer)
401 (insert-buffer-substring cache begin end)
402 (diff-mode))))
403
404 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
405 ;;;
406 ;;; Image/*
407 ;;;
408
409 (defun mew-mime-image/* (cache begin end &optional _params fname ct cte)
410 (let* ((format (mew-mime-image-format-name ct)))
411 (if (mew-image-inline-p format)
412 (mew-mime-image cache begin end format)
413 (insert " ##### # # # ##### #######\n"
414 " # ## ## # # # # # \n"
415 " # # # # # # # # # \n"
416 " # # # # # # # #### #######\n"
417 " # # # ####### # # # \n"
418 " # # # # # # # # \n"
419 " ##### # # # # ##### #######\n"
420 "\n\n")
421 (mew-insert "Content-Type:\t%s\n" ct)
422 (mew-insert "Encoding:\t%s\n" cte)
423 (mew-insert "Size:\t\t%d bytes\n" (mew-region-bytes begin end cache))
424 (mew-insert "Filename:\t%s\n" fname)
425 (mew-insert "Program:\t%s\n" mew-prog-image/*-ext)
426 (insert "\n")
427 (mew-mime-part-messages t))))
428
429 (defun mew-mime-image/*-ext (cache begin end &optional _params fname _ct _cte)
430 (let ((file (mew-make-temp-name fname)))
431 (with-current-buffer cache
432 (mew-flet
433 (write-region begin end file nil 'no-msg)))
434 (mew-mime-image-ext file)))
435
436 (defun mew-mime-image-ext (file)
437 (let* ((spec mew-prog-image/*-ext)
438 (prog (mew-progspec-get-prog spec))
439 (args (mew-progspec-get-args spec)))
440 (mew-mime-start-process prog args file)))
441
442 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
443 ;;;
444 ;;; Message/Rfc822
445 ;;;
446
447 (defun mew-mime-text/rfc822-headers (cache begin end &optional _params)
448 ;; called in Message buffer
449 (when (> end begin)
450 (save-excursion
451 ;; We need to keep properties of a header.
452 ;; This must be "insert-buffer-substring".
453 (insert-buffer-substring cache begin end)
454 (mew-header-arrange (point-min) (point-max)))))
455
456 (defun mew-mime-message/rfc822 (cache part)
457 ;; called in Message buffer
458 (let* ((hbeg (mew-syntax-get-begin part))
459 (hend (mew-syntax-get-end part))
460 (body (mew-syntax-get-part part)))
461 ;; We need to keep properties of a header.
462 ;; This must be "insert-buffer-substring".
463 (insert-buffer-substring cache hbeg hend)
464 (mew-header-arrange (point-min) (point-max))
465 (cond
466 ;; Displaying the text/plain body or the first part of
467 ;; top level multipart if it is text/plain.
468 ;; see also mew-syntax-singlepart
469 ((mew-syntax-singlepart-p body)
470 (mew-mime-part cache body nil)) ;; nil is single
471 ((mew-syntax-multipart-p body)
472 (let* ((first (mew-syntax-get-part body))
473 (ct (mew-syntax-get-value (mew-syntax-get-ct first) 'cap)))
474 (when (and (mew-xinfo-get-text-body) (mew-ct-textp ct))
475 (mew-mime-part cache first nil))))))) ;; nil is single
476
477 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
478 ;;;
479 ;;; Application/Octet-Stream
480 ;;;
481
482 (defun mew-mime-application/octet-stream (cache begin end &optional params ct cte cd fl)
483 (insert " ###### ### # # # ###### # #\n"
484 " # # # ## # # # # # # #\n"
485 " # # # # # # # # # # # #\n"
486 " ###### # # # # # # ###### #\n"
487 " # # # # # # ####### # # #\n"
488 " # # # # ## # # # # #\n"
489 " ###### ### # # # # # # #\n"
490 "\n\n")
491 (mew-insert "Content-Type:\t%s\n" ct)
492 (mew-insert "Encoding:\t%s\n" cte)
493 (when params
494 (mew-insert
495 "Parameters:\t%s\n"
496 (mapconcat (lambda (x) (concat (nth 0 x) "=" (nth 1 x))) params ", ")))
497 (mew-insert "Size:\t\t%d bytes\n" (mew-region-bytes begin end cache))
498 (mew-insert "Filename:\t%s\n" fl)
499 (mew-insert "Description:\t%s\n" cd)
500 (mew-insert-manual
501 "\nTo specify appropriate Content-Type: \n"
502 "and execute an internal/external function/command, "
503 "type \\<mew-summary-mode-map>\\[mew-summary-execute-external]'.\n")
504 (insert "\n")
505 (mew-mime-part-messages nil))
506
507 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
508 ;;;
509 ;;; Application/Msoffice
510 ;;;
511
512 (defun mew-mime-application/msword (cache begin end &optional parameter)
513 (mew-mime-application/msoffice
514 mew-prog-application/msword cache begin end parameter))
515
516 (defun mew-mime-application/msexcel (cache begin end &optional parameter)
517 (mew-mime-application/msoffice
518 mew-prog-application/msexcel cache begin end parameter))
519
520 (defun mew-mime-application/mspowerpoint (cache begin end &optional parameter)
521 (mew-mime-application/msoffice
522 mew-prog-application/mspowerpoint cache begin end parameter))
523
524 (defun mew-mime-application/rtf (cache begin end &optional parameter)
525 (mew-mime-application/msoffice
526 mew-prog-application/rtf cache begin end parameter))
527
528 (defun mew-mime-application/rtf-ext (cache begin end &optional parameter)
529 (save-excursion
530 (mew-mime-application/rtf cache begin end parameter)))
531
532 (defun mew-mime-application/msoffice (prog cache begin end &optional _parameter)
533 (let ((doit t) file1 file2)
534 (unless mew-internal-utf-8p
535 (condition-case nil
536 (require 'un-define)
537 (file-error
538 (setq doit nil)
539 (insert "To display this, install Mule-UCS for UTF-8.\n"))))
540 (unless (mew-which-exec prog)
541 (setq doit nil)
542 (insert "To display this, install \"" prog "\".\n"))
543 (condition-case nil
544 (require 'mew-w3m)
545 (file-error
546 (setq doit nil)
547 (insert "To display this, install \"w3m.el\".\n")))
548 (if (not doit)
549 (progn
550 (insert "\n")
551 (mew-mime-part-messages nil))
552 (message "Displaying an MS document...")
553 (mew-erase-buffer)
554 (setq file1 (mew-make-temp-name))
555 (with-current-buffer cache
556 (mew-flet
557 (write-region begin end file1 nil 'no-msg)))
558 (setq file2 (mew-make-temp-name))
559 (if (eq prog mew-prog-application/msword)
560 (mew-frwlet 'utf-8 mew-cs-dummy
561 (if mew-use-old-wvhtml
562 (call-process prog nil nil nil file1 file2)
563 (call-process prog nil nil nil
564 "--charset=utf-8"
565 (concat "--targetdir=" (file-name-directory file2))
566 file1
567 (file-name-nondirectory file2)))
568 (let ((buffer-file-coding-system)) ;; to prevent the side effect
569 (mew-insert-file-contents file2)))
570 (if (eq prog mew-prog-application/rtf)
571 (mew-frwlet 'shift_jis mew-cs-dummy
572 (call-process prog nil (current-buffer) nil file1))
573 (mew-frwlet 'utf-8 mew-cs-dummy
574 (call-process prog nil (current-buffer) nil file1))))
575 (mew-delete-file file1)
576 (mew-delete-file file2)
577 (save-excursion
578 (w3m-region (point-min) (point-max)
579 (w3m-expand-file-name-as-url (file-name-directory file2))))
580 (message "Displaying an MS document...done"))))
581
582
583 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
584 ;;;
585 ;;; PDF
586 ;;;
587
588 (defun mew-mime-application/pdf (cache begin end &optional _parameter)
589 (message "Displaying a PDF document...")
590 (mew-erase-buffer)
591 (let ((doit t) (prog mew-prog-application/pdf)
592 file1 file2)
593 (unless (mew-which-exec prog)
594 (setq doit nil)
595 (mew-elet (insert "To display this, install \"" prog "\".\n")))
596 (if (not doit)
597 (progn
598 (mew-elet (insert "\n"))
599 (mew-mime-part-messages t)
600 (message "Displaying a PDF document...failed"))
601 (setq file1 (mew-make-temp-name))
602 (with-current-buffer cache
603 (mew-flet
604 (write-region begin end file1 nil 'no-msg)))
605 (setq file2 (mew-make-temp-name))
606 (call-process prog nil nil nil "-layout" "-enc" "UTF-8" file1 file2)
607 (when (file-exists-p file2)
608 (mew-frwlet 'utf-8 mew-cs-dummy
609 (insert-file-contents file2)))
610 (if (file-exists-p file1) (delete-file file1))
611 (if (file-exists-p file2) (delete-file file2))
612 (message "Displaying a PDF document...done"))))
613
614 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
615 ;;;
616 ;;; Application/Ms-Tnef
617 ;;;
618
619 (defun mew-mime-application-ms-tnef (_cache _begin _end &optional _parameter)
620 (insert " ####### # # ####### ########\n"
621 " # ## # # #\n"
622 " # # # # # #\n"
623 " # # # # ####### ######\n"
624 " # # # # # #\n"
625 " # # ## # #\n"
626 " # # # ####### #\n"
627 "\n\n")
628 (mew-insert-manual
629 "To extract files, type "
630 "'\\<mew-summary-mode-map>\\[mew-summary-execute-external]'.\n"))
631
632 (defvar mew-prog-tnef "tnef")
633
634 (defun mew-mime-application-ms-tnef-ext (cache begin end &optional _parameter)
635 ;; called in Message buffer
636 (let ((file (mew-make-temp-name))
637 (dir (mew-input-directory-name mew-home)))
638 (if (not (mew-which-exec mew-prog-tnef))
639 (message "'%s' not found" mew-prog-tnef)
640 (with-current-buffer cache
641 (mew-plet
642 (write-region begin end file nil 'no-msg)))
643 (mew-erase-buffer)
644 (insert "Extracted: \n\n")
645 (call-process mew-prog-tnef file t nil "--verbose" "-C" dir)
646 (mew-delete-file file))))
647
648 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
649 ;;;
650 ;;; Executing external commands
651 ;;;
652
653 (defun mew-summary-ask-ct (ct fname)
654 (let (pt fl)
655 (cond
656 (fname
657 (setq ct (or (mew-ctdb-ct (mew-ctdb-by-file fname))
658 (mew-content-type (mew-sinfo-get-case))))
659 (setq pt "Type for %s (%s): ")
660 (setq fl fname))
661 (t
662 (setq pt "Type %s(%s): ")
663 (setq fl "")))
664 (mew-input-type pt fl ct mew-mime-content-type-list)))
665
666 (defun mew-summary-execute-external (&optional ask-command)
667 "Execute an external command according to Content-Type:.
668 If this command is executed on the entire message, the first part
669 is chosen as a target.
670
671 If Content-Type of the target part is Application/Octet-Stream,
672 Content-Type is automatically asked. And if it has both
673 internal and external visualization mechanisms, you are asked
674 which you want to use.
675
676 If executed with '\\[universal-argument]', you can specify a
677 command to be executed.
678
679 See 'mew-mime-content-type' to know how actions can be defined."
680 (interactive "P")
681 (mew-summary-msg-or-part
682 (let* ((fld (mew-summary-folder-name))
683 (msg (mew-summary-message-number2))
684 (nums (mew-syntax-nums))
685 (cache (mew-cache-hit fld msg 'must-hit))
686 (syntax (mew-cache-decode-syntax cache))
687 (stx (mew-syntax-get-entry syntax nums))
688 (ctl (mew-syntax-get-ct stx))
689 (ct (mew-syntax-get-value ctl 'cap))
690 (win (selected-window))
691 begin end params cdpl fname program options async was-apo erase-p
692 pro-opt ent1 ent2)
693 (when (and (string= ct mew-ct-msg)
694 (or (not ask-command)
695 (not (y-or-n-p "Save the entire message (y) or the first part (n)? "))))
696 (setq stx (mew-syntax-get-part stx))
697 (if (mew-syntax-multipart-p stx)
698 (setq stx (mew-syntax-get-part stx)))
699 (setq ctl (mew-syntax-get-ct stx))
700 (setq ct (mew-syntax-get-value ctl 'cap)))
701 (setq begin (mew-syntax-get-begin stx))
702 (setq end (mew-syntax-get-end stx))
703 (setq params (mew-syntax-get-params ctl))
704 (setq cdpl (mew-syntax-get-cdp stx))
705 (setq fname (mew-syntax-get-filename cdpl ctl))
706 (when (or (string= ct mew-ct-apo)
707 (eq (mew-ctdb-prog (mew-ctdb-by-ct ct))
708 'mew-mime-application/octet-stream))
709 (setq ct (mew-summary-ask-ct ct fname))
710 (setq was-apo t))
711 (if (not ask-command)
712 (setq program (mew-ctdb-prog (mew-ctdb-by-ct ct)))
713 (setq pro-opt (mew-input-command mew-default-external-program))
714 (setq program (car pro-opt))
715 (setq options (cdr pro-opt)))
716 ;; prog
717 ;; func => called for both
718 ;; (prog ...)
719 ;; (nil prog) == prog
720 ;; (nil (prog...)) == (prog...)
721 ;; (nil func) != func
722 ;; (func func) => need to select
723 ;; (func prog) => need to select
724 ;; (func (prog ...)) => need to select
725 (cond
726 ((stringp program)
727 (setq async t))
728 ((symbolp program)
729 (setq erase-p t))
730 ((listp program)
731 (setq ent1 (nth 0 program))
732 (setq ent2 (nth 1 program))
733 (cond
734 ((stringp ent1)
735 (mew-set '(program options async) program))
736 ((null ent1)
737 (cond
738 ((stringp ent2)
739 (setq program ent2)
740 (setq async t))
741 ((listp ent2)
742 (mew-set '(program options async) ent2))
743 (t ;; symbol
744 (setq program ent2))))
745 (t ;; symbol
746 (if (and was-apo
747 (y-or-n-p "Internal (y) or External (n)? "))
748 (progn
749 (setq erase-p t)
750 (setq program ent1))
751 (setq program ent2))
752 (cond
753 ((stringp program)
754 (setq async t))
755 ((listp program)
756 (mew-set '(program options async) program)))))))
757 (mew-summary-toggle-disp-msg 'on)
758 (mew-window-configure 'message)
759 (if erase-p (mew-erase-buffer))
760 ;; message buffer
761 (unwind-protect
762 (mew-elet
763 (cond
764 ((stringp program)
765 (mew-summary-execute-program
766 program ct ctl cache begin end params fname options async))
767 ((symbolp program)
768 (mew-summary-execute-symbol
769 program ct ctl cache begin end params fname was-apo)))
770 (mew-summary-display-postscript 'no-hook))
771 (select-window win)))))
772
773 (defun mew-summary-execute-symbol (program ct _ctl cache begin end params fname was-apo)
774 (cond
775 ((not (fboundp program))
776 (message "%s is not implemented" (symbol-name program)))
777 ((eq program mew-prog-rfc822)
778 (message "%s cannot be executed" mew-prog-rfc822))
779 ((or (mew-ct-imagep ct) (mew-ct-modelp ct))
780 (funcall program cache begin end params fname ct))
781 (t
782 (let ((mew-use-text/html t)
783 (mbuf (current-buffer))
784 tbuf)
785 (if (and was-apo (mew-ct-textp ct)) ;; decode-broken?
786 (with-temp-buffer
787 (insert-buffer-substring cache begin end)
788 (mew-cs-decode-region (point-min) (point-max) mew-cs-autoconv)
789 (setq tbuf (current-buffer))
790 (setq begin (point-min))
791 (setq end (point-max))
792 (set-buffer mbuf)
793 (funcall program tbuf begin end params))
794 (funcall program cache begin end params))))))
795
796 ;;; external
797 (defun mew-summary-execute-program (program ct ctl cache begin end _params fname options async)
798 (if (not (mew-which-exec program))
799 (message "%s does not exist" program)
800 (let ((file (mew-make-temp-name fname))
801 wcs)
802 (with-current-buffer cache
803 ;; NEVER use call-process-region for privacy reasons
804 (cond
805 ((not (mew-ct-linebasep ct))
806 (setq wcs mew-cs-binary))
807 ((not (mew-ct-textp ct))
808 (setq wcs mew-cs-text-for-write))
809 (t
810 (cond
811 ((or (string= mew-ct-htm ct) (string= mew-ct-xml ct))
812 (setq wcs (mew-text/html-detect-cs begin end))
813 (unless (mew-coding-system-p wcs)
814 (setq wcs (mew-charset-to-cs
815 (mew-syntax-get-param ctl "charset")))))
816 (t
817 (setq wcs (mew-charset-to-cs
818 (mew-syntax-get-param ctl "charset")))))
819 (unless (mew-coding-system-p wcs)
820 (setq wcs (if mew-decode-broken
821 (mew-charset-to-cs
822 (mew-charset-guess-region
823 begin end))
824 mew-cs-text-for-write)))))
825 (mew-frwlet mew-cs-dummy wcs
826 (write-region begin end file nil 'no-msg))
827 (if async
828 (mew-mime-start-process program options file)
829 (mew-mime-call-process program options file))))))
830
831 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
832 ;;;
833 ;;; unzip
834 ;;;
835
836 (defvar mew-ct-zip-list '("Application/Zip"
837 "Application/X-Zip-Compressed"
838 "Application/Octet-Stream"))
839
840 (defun mew-ct-zip-p (ct)
841 (member ct mew-ct-zip-list))
842
843 (defun mew-summary-unzip ()
844 (interactive)
845 (let* ((fld (mew-summary-folder-name))
846 (msg (mew-summary-message-number2))
847 (nums (mew-syntax-nums))
848 (cache (mew-cache-hit fld msg 'must-hit))
849 (syntax (mew-cache-decode-syntax cache))
850 (stx (mew-syntax-get-entry syntax nums))
851 (ct (mew-syntax-get-value (mew-syntax-get-ct stx) 'cap))
852 (file0 (mew-syntax-get-param (mew-syntax-get-cdp stx) "filename"))
853 (beg (mew-syntax-get-begin stx))
854 (end (mew-syntax-get-end stx))
855 (size (- end beg))
856 (dir mew-temp-dir)
857 file size1 end1)
858 (if (not (mew-ct-zip-p ct))
859 (message "Cannot unzip"))
860 (setq file (mew-unzip-file cache beg end dir file0))
861 (if (not file)
862 (message "unzip failed")
863 (let ((ct (mew-ctdb-ct (mew-ctdb-by-file file)))
864 cs)
865 (unless ct
866 (let ((txtp (mew-ct-textp (mew-content-type "default")))
867 (file- (file-name-nondirectory file)))
868 (setq ct (if txtp
869 (if (y-or-n-p (format "\"%s\" as text? " file-))
870 mew-ct-txt mew-ct-apo)
871 (if (y-or-n-p (format "\"%s\" as binary? " file-))
872 mew-ct-apo mew-ct-txt)))))
873 (setq cs (if (mew-ct-textp ct) mew-cs-autoconv mew-cs-binary))
874 (with-current-buffer cache
875 (delete-region beg end)
876 (goto-char beg)
877 (save-restriction
878 (narrow-to-region beg beg)
879 (mew-frwlet cs mew-cs-dummy
880 (insert-file-contents (expand-file-name file dir)))
881 (mew-syntax-set-ct stx (list ct))
882 (setq end1 (point-max))
883 (mew-syntax-set-end stx end1)
884 (setq size1 (- end1 beg))
885 (mew-syntax-set-cdp stx (mew-syntax-cdp-format ct file)))
886 (mew-decode-syntax-adjust-message mew-decode-syntax beg (- size1 size))
887 (mew-xinfo-set-multi-form nil)
888 (mew-decode-syntax-set))
889 (let ((current (point))
890 (start (mew-decode-syntax-begin)))
891 (mew-decode-syntax-delete)
892 (mew-decode-syntax-copy cache)
893 (goto-char start)
894 (forward-line -1)
895 (mew-decode-syntax-print (current-buffer)
896 mew-decode-syntax
897 (mew-xinfo-get-multi-form)
898 (mew-xinfo-get-icon-spec))
899 (goto-char current)
900 (mew-summary-display 'redisplay))))))
901
902 (defun mew-decode-syntax-adjust (val threshold inc)
903 (if (> val threshold) (+ val inc) val))
904
905 (defun mew-decode-syntax-adjust-single (syntax threshold inc)
906 (unless (= (mew-syntax-get-begin syntax) threshold)
907 (mew-syntax-set-begin
908 syntax (mew-decode-syntax-adjust
909 (mew-syntax-get-begin syntax) threshold inc))
910 (mew-syntax-set-end
911 syntax (mew-decode-syntax-adjust
912 (mew-syntax-get-end syntax) threshold inc))))
913
914 (defun mew-decode-syntax-adjust-message (syntax threshold inc)
915 (mew-decode-syntax-adjust-single syntax threshold inc)
916 (let ((body (mew-syntax-get-part syntax)))
917 (if (mew-syntax-multipart-p body)
918 (mew-decode-syntax-adjust-multi body threshold inc)
919 (mew-decode-syntax-adjust-single body threshold inc))))
920
921 (defun mew-decode-syntax-adjust-multi (syntax threshold inc)
922 (mew-decode-syntax-adjust-single syntax threshold inc)
923 (let ((i mew-syntax-magic)
924 (len (length syntax))
925 part)
926 (while (< i len)
927 (setq part (aref syntax i))
928 (cond
929 ((mew-syntax-singlepart-p part)
930 (mew-decode-syntax-adjust-single part threshold inc))
931 ((mew-syntax-multipart-p part)
932 (mew-decode-syntax-adjust-multi part threshold inc))
933 ((mew-syntax-message-p part)
934 (mew-decode-syntax-adjust-message part threshold inc)))
935 (setq i (1+ i)))))
936
937 (defun mew-unzip-file (buf beg end dir file)
938 (let* ((zipfile (expand-file-name file dir))
939 (encrypted (mew-zip-encrypted-p buf beg))
940 (password (if encrypted (read-passwd "Zip password: ")))
941 (args0 (list "-o" "-d" dir zipfile))
942 (args (if password (cons "-P" (cons password args0)) args0)))
943 (with-current-buffer buf
944 (mew-frwlet mew-cs-dummy mew-cs-binary
945 (write-region beg end zipfile nil 'no-msg)))
946 (with-temp-buffer
947 (apply 'call-process "unzip" nil t nil args)
948 (goto-char (point-max))
949 (forward-line -1)
950 (beginning-of-line)
951 (when (looking-at "^ *[a-z]+: \\([^ ]+\\)")
952 (mew-match-string 1)))))
953
954 (defun mew-zip-encrypted-p (buf beg)
955 (with-current-buffer buf
956 (goto-char beg)
957 (forward-char 6)
958 (= (% (char-after) 2) 1)))
959
960 (provide 'mew-mime)
961
962 ;;; Copyright Notice:
963
964 ;; Copyright (C) 1997-2015 Mew developing team.
965 ;; All rights reserved.
966
967 ;; Redistribution and use in source and binary forms, with or without
968 ;; modification, are permitted provided that the following conditions
969 ;; are met:
970 ;;
971 ;; 1. Redistributions of source code must retain the above copyright
972 ;; notice, this list of conditions and the following disclaimer.
973 ;; 2. Redistributions in binary form must reproduce the above copyright
974 ;; notice, this list of conditions and the following disclaimer in the
975 ;; documentation and/or other materials provided with the distribution.
976 ;; 3. Neither the name of the team nor the names of its contributors
977 ;; may be used to endorse or promote products derived from this software
978 ;; without specific prior written permission.
979 ;;
980 ;; THIS SOFTWARE IS PROVIDED BY THE TEAM AND CONTRIBUTORS ``AS IS'' AND
981 ;; ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
982 ;; IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
983 ;; PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE TEAM OR CONTRIBUTORS BE
984 ;; LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
985 ;; CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
986 ;; SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
987 ;; BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
988 ;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
989 ;; OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
990 ;; IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
991
992 ;;; mew-mime.el ends here