681376d71de3cfec1ec299b303441e94a7c49336
[elisp/flim.git] / mime.el
1 ;;; mime.el --- MIME library module
2
3 ;; Copyright (C) 1998 Free Software Foundation, Inc.
4
5 ;; Author: MORIOKA Tomohiko <morioka@jaist.ac.jp>
6 ;; Keywords: MIME, multimedia, mail, news
7
8 ;; This file is part of FLIM (Faithful Library about Internet Message).
9
10 ;; This program is free software; you can redistribute it and/or
11 ;; modify it under the terms of the GNU General Public License as
12 ;; published by the Free Software Foundation; either version 2, or (at
13 ;; your option) any later version.
14
15 ;; This program is distributed in the hope that it will be useful, but
16 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18 ;; General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Code:
26
27 (require 'alist)
28 (require 'std11)
29 (require 'mime-def)
30 (require 'eword-decode)
31
32 (autoload 'eword-encode-field "eword-encode"
33   "Encode header field STRING, and return the result.")
34 (autoload 'eword-encode-header "eword-encode"
35   "Encode header fields to network representation, such as MIME encoded-word.")
36
37
38 (autoload 'mime-parse-Content-Type "mime-parse"
39   "Parse STRING as field-body of Content-Type field.")
40 (autoload 'mime-read-Content-Type "mime-parse"
41   "Read field-body of Content-Type field from current-buffer,
42 and return parsed it.")
43
44 (autoload 'mime-parse-Content-Disposition "mime-parse"
45   "Parse STRING as field-body of Content-Disposition field.")
46 (autoload 'mime-read-Content-Disposition "mime-parse"
47   "Read field-body of Content-Disposition field from current-buffer,
48 and return parsed it.")
49
50 (autoload 'mime-parse-Content-Transfer-Encoding "mime-parse"
51   "Parse STRING as field-body of Content-Transfer-Encoding field.")
52 (autoload 'mime-read-Content-Transfer-Encoding "mime-parse"
53   "Read field-body of Content-Transfer-Encoding field from
54 current-buffer, and return it.")
55
56 (autoload 'mime-parse-message "mime-parse"
57   "Parse current-buffer as a MIME message.")
58
59 (autoload 'mime-parse-buffer "mime-parse"
60   "Parse BUFFER as a MIME message.")
61
62
63 ;;; @ Entity Representation and Implementation
64 ;;;
65
66 (defvar mime-entity-implementation-alist nil)
67
68 (defsubst mime-find-function (service type)
69   (let ((imps (cdr (assq type mime-entity-implementation-alist))))
70     (if imps
71         (let ((func (cdr (assq service imps))))
72           (unless func
73             (setq func (intern (format "mm%s-%s" type service)))
74             (set-alist 'mime-entity-implementation-alist
75                        type (put-alist service func imps))
76             )
77           func)
78       (let ((prefix (format "mm%s" type)))
79         (require (intern prefix))
80         (let ((func (intern (format "%s-%s" prefix service))))
81           (set-alist 'mime-entity-implementation-alist
82                      type
83                      (list (cons service func)))
84           func)))))
85
86 (defun mime-open-entity (type location)
87   "Open an entity and return it.
88 TYPE is representation-type.
89 LOCATION is location of entity.  Specification of it is depended on
90 representation-type."
91   (funcall (mime-find-function 'open-entity type) location)
92   )
93
94 (defsubst mime-entity-function (entity service)
95   (mime-find-function service
96                       (mime-entity-representation-type-internal entity)))
97
98 (defun mime-entity-cooked-p (entity)
99   "Return non-nil if contents of ENTITY has been already code-converted."
100   (funcall (mime-entity-function entity 'cooked-p))
101   )
102
103
104 ;;; @ Entity as node of message
105 ;;;
106
107 (defalias 'mime-entity-children 'mime-entity-children-internal)
108
109 (defalias 'mime-entity-node-id 'mime-entity-node-id-internal)
110
111 (defun mime-entity-number (entity)
112   "Return entity-number of ENTITY."
113   (reverse (mime-entity-node-id-internal entity)))
114
115 (defun mime-find-entity-from-number (entity-number &optional message)
116   "Return entity from ENTITY-NUMBER in MESSAGE.
117 If MESSAGE is not specified, `mime-message-structure' is used."
118   (or message
119       (setq message mime-message-structure))
120   (let ((sn (car entity-number)))
121     (if (null sn)
122         message
123       (let ((rc (nth sn (mime-entity-children message))))
124         (if rc
125             (mime-find-entity-from-number (cdr entity-number) rc)
126           ))
127       )))
128
129 (defun mime-find-entity-from-node-id (entity-node-id &optional message)
130   "Return entity from ENTITY-NODE-ID in MESSAGE.
131 If MESSAGE is not specified, `mime-message-structure' is used."
132   (mime-find-entity-from-number (reverse entity-node-id) message))
133
134 (defun mime-entity-parent (entity &optional message)
135   "Return mother entity of ENTITY.
136 If MESSAGE is not specified, `mime-message-structure' in the buffer of
137 ENTITY is used."
138   (mime-find-entity-from-node-id
139    (cdr (mime-entity-node-id entity))
140    (or message
141        (save-excursion
142          (set-buffer (mime-entity-buffer entity))
143          mime-message-structure))))
144
145 (defun mime-root-entity-p (entity)
146   "Return t if ENTITY is root-entity (message)."
147   (null (mime-entity-node-id entity)))
148
149
150 ;;; @ Entity Buffer
151 ;;;
152
153 (defun mime-entity-buffer (entity)
154   (or (mime-entity-buffer-internal entity)
155       (funcall (mime-entity-function entity 'entity-buffer) entity)
156       ))
157
158 (defun mime-entity-point-min (entity)
159   (funcall (mime-entity-function entity 'entity-point-min) entity)
160   )
161 (defun mime-entity-point-max (entity)
162   (funcall (mime-entity-function entity 'entity-point-max) entity)
163   )
164
165 (defun mime-entity-header-start (entity)
166   (or (mime-entity-header-start-internal entity)
167       (funcall (mime-entity-function entity 'entity-header-start) entity)
168       ))
169 (defsubst mime-entity-header-end (entity)
170   (or (mime-entity-header-end-internal entity)
171       (funcall (mime-entity-function entity 'entity-header-end) entity)
172       ))
173
174 (defsubst mime-entity-body-start (entity)
175   (or (mime-entity-body-start-internal entity)
176       (funcall (mime-entity-function entity 'entity-body-start) entity)
177       ))
178 (defsubst mime-entity-body-end (entity)
179   (or (mime-entity-body-end-internal entity)
180       (funcall (mime-entity-function entity 'entity-body-end) entity)
181       ))
182
183
184 ;;; @ Entity Header
185 ;;;
186
187 (defun mime-fetch-field (field-name &optional entity)
188   (or (symbolp field-name)
189       (setq field-name (intern (capitalize (capitalize field-name)))))
190   (or entity
191       (setq entity mime-message-structure))
192   (let* ((header (mime-entity-original-header-internal entity))
193          (field-body (cdr (assq field-name header))))
194     (or field-body
195         (progn
196           (if (setq field-body
197                     (funcall (mime-entity-function entity 'fetch-field)
198                              entity (symbol-name field-name)))
199               (mime-entity-set-original-header-internal
200                entity (put-alist field-name field-body header))
201             )
202           field-body))))
203
204 (defalias 'mime-entity-content-type 'mime-entity-content-type-internal)
205
206 (defun mime-entity-content-disposition (entity)
207   (or (mime-entity-content-disposition-internal entity)
208       (let ((ret (mime-fetch-field 'Content-Disposition entity)))
209         (if ret
210             (let ((disposition (mime-parse-Content-Disposition ret)))
211               (when disposition
212                 (mime-entity-set-content-disposition-internal
213                  entity disposition)
214                 disposition))))))
215
216 (defun mime-entity-encoding (entity)
217   (or (mime-entity-encoding-internal entity)
218       (let ((ret (mime-fetch-field 'Content-Transfer-Encoding entity)))
219         (if ret
220             (let ((encoding (mime-parse-Content-Transfer-Encoding ret)))
221               (when encoding
222                 (mime-entity-set-encoding-internal entity encoding)
223                 encoding))))))
224
225 (defun mime-read-field (field-name &optional entity)
226   (or (symbolp field-name)
227       (setq field-name (capitalize (capitalize field-name))))
228   (or entity
229       (setq entity mime-message-structure))
230   (cond ((eq field-name 'Content-Type)
231          (mime-entity-content-type entity)
232          )
233         ((eq field-name 'Content-Disposition)
234          (mime-entity-content-disposition entity)
235          )
236         ((eq field-name 'Content-Transfer-Encoding)
237          (mime-entity-encoding entity)
238          )
239         (t
240          (let* ((header (mime-entity-parsed-header-internal entity))
241                 (field (cdr (assq field-name header))))
242            (or field
243                (let ((field-body (mime-fetch-field field-name entity)))
244                  (when field-body
245                    (cond ((memq field-name '(From Resent-From
246                                              To Resent-To
247                                              Cc Resent-Cc
248                                              Bcc Resent-Bcc
249                                              Reply-To Resent-Reply-To))
250                           (setq field (std11-parse-addresses
251                                        (eword-lexical-analyze field-body)))
252                           )
253                          ((memq field-name '(Sender Resent-Sender))
254                           (setq field (std11-parse-address
255                                        (eword-lexical-analyze field-body)))
256                           )
257                          ((memq field-name eword-decode-ignored-field-list)
258                           (setq field field-body))
259                          ((memq field-name eword-decode-structured-field-list)
260                           (setq field (eword-decode-structured-field-body
261                                        field-body)))
262                          (t
263                           (setq field (eword-decode-unstructured-field-body
264                                        field-body))
265                           ))
266                    (mime-entity-set-parsed-header-internal
267                     entity (put-alist field-name field header))
268                    field)))))))
269
270 (defun eword-visible-field-p (field-name visible-fields invisible-fields)
271   (or (catch 'found
272         (while visible-fields
273           (let ((regexp (car visible-fields)))
274             (if (string-match regexp field-name)
275                 (throw 'found t)
276               ))
277           (setq visible-fields (cdr visible-fields))
278           ))
279       (catch 'found
280         (while invisible-fields
281           (let ((regexp (car invisible-fields)))
282             (if (string-match regexp field-name)
283                 (throw 'found nil)
284               ))
285           (setq invisible-fields (cdr invisible-fields))
286           )
287         t)))
288                 
289 (defun mime-insert-decoded-header (entity
290                                    &optional invisible-fields visible-fields
291                                    code-conversion)
292   "Insert before point a decoded header of ENTITY."
293   (let ((default-charset
294           (if code-conversion
295               (if (mime-charset-to-coding-system code-conversion)
296                   code-conversion
297                 default-mime-charset))))
298     (save-restriction
299       (narrow-to-region (point)(point))
300       (let ((the-buf (current-buffer))
301             (src-buf (mime-entity-buffer entity))
302             (h-end (mime-entity-header-end entity))
303             beg p end field-name len field)
304         (save-excursion
305           (set-buffer src-buf)
306           (goto-char (mime-entity-header-start entity))
307           (save-restriction
308             (narrow-to-region (point) h-end)
309             (while (re-search-forward std11-field-head-regexp nil t)
310               (setq beg (match-beginning 0)
311                     p (match-end 0)
312                     field-name (buffer-substring beg (1- p))
313                     len (string-width field-name)
314                     end (std11-field-end))
315               (when (eword-visible-field-p field-name
316                                            visible-fields invisible-fields)
317                 (setq field (intern (capitalize field-name)))
318                 (save-excursion
319                   (set-buffer the-buf)
320                   (insert field-name)
321                   (insert ":")
322                   (cond ((memq field eword-decode-ignored-field-list)
323                          ;; Don't decode
324                          (insert-buffer-substring src-buf p end)
325                          )
326                         ((memq field eword-decode-structured-field-list)
327                          ;; Decode as structured field
328                          (let ((body (save-excursion
329                                        (set-buffer src-buf)
330                                        (buffer-substring p end)))
331                                (default-mime-charset default-charset))
332                            (insert (eword-decode-and-fold-structured-field
333                                     body (1+ len)))
334                            ))
335                         (t
336                          ;; Decode as unstructured field
337                          (let ((body (save-excursion
338                                        (set-buffer src-buf)
339                                        (buffer-substring p end)))
340                                (default-mime-charset default-charset))
341                            (insert (eword-decode-unstructured-field-body
342                                     body (1+ len)))
343                            )))
344                   (insert "\n")
345                   )))))))))
346
347
348 ;;; @ Entity Attributes
349 ;;;
350
351 (defun mime-entity-uu-filename (entity)
352   (if (member (mime-entity-encoding entity) mime-uuencode-encoding-name-list)
353       (save-excursion
354         (set-buffer (mime-entity-buffer entity))
355         (goto-char (mime-entity-body-start entity))
356         (if (re-search-forward "^begin [0-9]+ "
357                                (mime-entity-body-end entity) t)
358             (if (looking-at ".+$")
359                 (buffer-substring (match-beginning 0)(match-end 0))
360               )))))
361
362 (defun mime-entity-filename (entity)
363   "Return filename of ENTITY."
364   (or (mime-entity-uu-filename entity)
365       (mime-content-disposition-filename
366        (mime-entity-content-disposition entity))
367       (cdr (let ((param (mime-content-type-parameters
368                          (mime-entity-content-type entity))))
369              (or (assoc "name" param)
370                  (assoc "x-name" param))
371              ))))
372
373
374 (defsubst mime-entity-media-type (entity)
375   (mime-content-type-primary-type (mime-entity-content-type entity)))
376 (defsubst mime-entity-media-subtype (entity)
377   (mime-content-type-subtype (mime-entity-content-type entity)))
378 (defsubst mime-entity-parameters (entity)
379   (mime-content-type-parameters (mime-entity-content-type entity)))
380 (defsubst mime-entity-type/subtype (entity-info)
381   (mime-type/subtype-string (mime-entity-media-type entity-info)
382                             (mime-entity-media-subtype entity-info)))
383
384
385 ;;; @ Entity Content
386 ;;;
387
388 (defun mime-entity-content (entity)
389   (save-excursion
390     (set-buffer (mime-entity-buffer entity))
391     (mime-decode-string (buffer-substring (mime-entity-body-start entity)
392                                           (mime-entity-body-end entity))
393                         (mime-entity-encoding entity))))
394
395 (defun mime-write-entity-content (entity filename)
396   "Write content of ENTITY into FILENAME."
397   (save-excursion
398     (set-buffer (mime-entity-buffer entity))
399     (let ((encoding (or (mime-entity-encoding entity) "7bit")))
400       (if (and (mime-entity-cooked-p entity)
401                (member encoding '("7bit" "8bit" "binary")))
402           (write-region (mime-entity-body-start entity)
403                         (mime-entity-body-end entity) filename)
404         (mime-write-decoded-region (mime-entity-body-start entity)
405                                    (mime-entity-body-end entity)
406                                    filename encoding)
407         ))))
408
409 (defun mime-write-entity (entity filename)
410   "Write ENTITY into FILENAME."
411   (save-excursion
412     (set-buffer (mime-entity-buffer entity))
413     (if (mime-entity-cooked-p entity)
414         (write-region (mime-entity-point-min entity)
415                       (mime-entity-point-max entity) filename)
416       (write-region-as-binary (mime-entity-point-min entity)
417                               (mime-entity-point-max entity) filename)
418       )))
419
420 (defun mime-write-entity-body (entity filename)
421   "Write body of ENTITY into FILENAME."
422   (save-excursion
423     (set-buffer (mime-entity-buffer entity))
424     (if (mime-entity-cooked-p entity)
425         (write-region (mime-entity-body-start entity)
426                       (mime-entity-body-end entity) filename)
427       (write-region-as-binary (mime-entity-body-start entity)
428                               (mime-entity-body-end entity) filename)
429       )))
430
431
432 ;;; @ end
433 ;;;
434
435 (provide 'mime)
436
437 ;;; mime.el ends here