1 ;;; elmo-mime.el --- MIME module for ELMO.
3 ;; Copyright (C) 1998,1999,2000 Yuuichi Teranishi <teranisi@gohome.org>
5 ;; Author: Yuuichi Teranishi <teranisi@gohome.org>
6 ;; Keywords: mail, net news
8 ;; This file is part of ELMO (Elisp Library for Message Orchestration).
10 ;; This program is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; This program is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
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.
38 (luna-define-class elmo-mime-entity))
40 (luna-define-generic elmo-mime-entity-display-p (entity mime-mode)
41 "Return non-nil if ENTITY is able to display with MIME-MODE.
43 MIME-MODE is a symbol which is one of the following:
44 `mime' (Can display each MIME part)
45 `as-is' (Can display raw message)")
47 (luna-define-generic elmo-mime-entity-reassembled-p (entity)
48 "Return non-nil if ENTITY is reassembled message/partial pieces.")
50 (luna-define-generic elmo-mime-entity-display (entity preview-buffer
54 "Display MIME message ENTITY.
55 PREVIEW-BUFFER is a view buffer.
56 Optional argument ORIGINAL-MAJOR-MODE is major-mode of representation
57 buffer of ENTITY. If it is nil, current `major-mode' is used.
58 If optional argument KEYMAP is specified,
59 use for keymap of representation buffer.")
61 (luna-define-generic elmo-mime-entity-display-as-is (entity
66 "Display MIME message ENTITY as is.
67 PREVIEW-BUFFER is a view buffer.
68 Optional argument ORIGINAL-MAJOR-MODE is major-mode of representation
69 buffer of ENTITY. If it is nil, current `major-mode' is used.
70 If optional argument KEYMAP is specified,
71 use for keymap of representation buffer.")
73 (luna-define-method elmo-mime-entity-display ((entity elmo-mime-entity)
78 (let ((elmo-message-displaying t))
79 (mime-display-message entity
83 original-major-mode)))
85 (defun elmo-mime-entity-fragment-p (entity)
86 (and (not (elmo-mime-entity-reassembled-p entity))
87 (eq (mime-entity-media-type entity) 'message)
88 (eq (mime-entity-media-subtype entity) 'partial)))
91 (luna-define-class mime-elmo-buffer-entity (mime-buffer-entity
94 (luna-define-internal-accessors 'mime-elmo-buffer-entity)
95 (luna-define-class mime-elmo-imap-entity (mime-imap-entity
99 (provide 'mmelmo-imap)
100 (provide 'mmelmo-buffer)
102 (defvar elmo-message-ignored-field-list mime-view-ignored-field-list)
103 (defvar elmo-message-visible-field-list mime-view-visible-field-list)
104 (defvar elmo-message-sorted-field-list nil)
105 (defvar elmo-mime-display-header-analysis t)
107 (defcustom elmo-mime-header-max-column 'fill-column
108 "*Header max column number. Default is `fill-colmn'.
109 If a symbol of variable is specified, use its value in message buffer.
110 If a symbol of function is specified, the function is called and its return
112 :type '(choice (integer :tag "Column Number")
113 (variable :tag "Variable")
114 (function :tag "Function"))
117 (luna-define-method initialize-instance :after ((entity mime-elmo-buffer-entity)
121 (luna-define-method initialize-instance :around ((entity mime-elmo-imap-entity)
123 (luna-call-next-method))
125 ;;; Insert sorted header.
126 (defsubst elmo-mime-insert-header-from-buffer (buffer
128 &optional invisible-fields
131 (let ((the-buf (current-buffer))
132 (max-column (cond ((functionp elmo-mime-header-max-column)
133 (funcall elmo-mime-header-max-column))
134 ((and (symbolp elmo-mime-header-max-column)
135 (boundp elmo-mime-header-max-column))
136 (symbol-value elmo-mime-header-max-column))
138 elmo-mime-header-max-column)))
143 (narrow-to-region start end)
145 (while (re-search-forward std11-field-head-regexp nil t)
146 (let* ((field-start (match-beginning 0))
147 (name-end (match-end 0))
148 (field-name (buffer-substring field-start name-end)))
149 (when (mime-visible-field-p field-name
150 visible-fields invisible-fields)
151 (let* ((field (intern
153 (buffer-substring field-start (1- name-end)))))
154 (field-body (buffer-substring name-end (std11-field-end)))
156 (and elmo-mime-display-header-analysis
157 (inline (mime-find-field-decoder field 'wide)))))
158 (setq vf-alist (cons (list field-name field-body field-decoder)
167 (dolist (re sort-fields)
168 (when (string-match re sf)
170 (when (string-match re df)
175 (let* ((vf (car vf-alist))
176 (field-name (nth 0 vf))
177 (field-body (nth 1 vf))
178 (field-decoder (nth 2 vf)))
180 (insert (or (and field-decoder
182 (funcall field-decoder field-body
183 (string-width field-name)
188 (setq vf-alist (cdr vf-alist)))
189 (run-hooks 'mmelmo-header-inserted-hook))))
191 (luna-define-generic elmo-mime-insert-sorted-header (entity
192 &optional invisible-fields
195 "Insert sorted header fields of the ENTITY.")
197 (luna-define-method elmo-mime-insert-sorted-header ((entity
198 mime-elmo-buffer-entity)
199 &optional invisible-fields
202 (elmo-mime-insert-header-from-buffer
203 (mime-buffer-entity-buffer-internal entity)
204 (mime-buffer-entity-header-start-internal entity)
205 (mime-buffer-entity-header-end-internal entity)
206 invisible-fields visible-fields sorted-fields))
208 (luna-define-method elmo-mime-insert-sorted-header ((entity
209 mime-elmo-imap-entity)
210 &optional invisible-fields
213 (let ((the-buf (current-buffer))
216 (insert (mime-imap-entity-header-string entity))
217 (setq buf (current-buffer)
221 (elmo-mime-insert-header-from-buffer buf p-min p-max
226 (luna-define-method mime-insert-text-content :around
227 ((entity mime-elmo-buffer-entity))
228 (luna-call-next-method)
229 (run-hooks 'elmo-message-text-content-inserted-hook))
231 (luna-define-method mime-insert-text-content :around
232 ((entity mime-elmo-imap-entity))
233 (luna-call-next-method)
234 (run-hooks 'elmo-message-text-content-inserted-hook))
236 (defun elmo-mime-insert-header (entity situation)
237 (elmo-mime-insert-sorted-header
239 elmo-message-ignored-field-list
240 elmo-message-visible-field-list
241 elmo-message-sorted-field-list)
242 (run-hooks 'elmo-message-header-inserted-hook))
244 ;; mime-elmo-buffer-entity
245 (luna-define-method elmo-mime-entity-display-p
246 ((entity mime-elmo-buffer-entity) mime-mode)
250 (luna-define-method elmo-mime-entity-reassembled-p ((entity
251 mime-elmo-buffer-entity))
252 (mime-elmo-buffer-entity-reassembled-internal entity))
254 (luna-define-method elmo-mime-entity-display-as-is ((entity
255 mime-elmo-buffer-entity)
260 (elmo-mime-display-as-is-internal entity
264 original-major-mode))
266 ;; mime-elmo-imap-entity
267 (luna-define-method elmo-mime-entity-display-p
268 ((entity mime-elmo-imap-entity) mime-mode)
269 (not (eq mime-mode 'as-is)))
271 (luna-define-method elmo-mime-entity-display-as-is ((entity
272 mime-elmo-imap-entity)
277 (error "Does not support this method"))
280 (defun elmo-message-mime-entity (folder number rawbuf reassemble
282 ignore-cache unread entire)
283 "Return the mime-entity structure of the message in the FOLDER with NUMBER.
284 RAWBUF is the output buffer for original message.
285 If REASSEMBLE is non-nil and MIME media type of the message is message/partial,
286 the mime-entity is reassembled partial message.
287 If optional argument IGNORE-CACHE is non-nil, existing cache is ignored.
288 If second optional argument UNREAD is non-nil,
289 keep status of the message as unread.
290 If third optional argument ENTIRE is non-nil, fetch entire message at once."
291 (let (id message entity content-type)
293 (setq entity (elmo-message-entity folder number))
294 (setq id (if (setq content-type (elmo-message-entity-field
295 entity 'content-type))
296 (and (string-match "message/partial" content-type)
297 (mime-content-type-parameter
298 (mime-parse-Content-Type content-type) "id"))
299 (and (setq message (elmo-message-mime-entity-internal
301 ignore-cache unread entire))
302 (eq (mime-entity-media-type message) 'message)
303 (eq (mime-entity-media-subtype message) 'partial)
304 (mime-content-type-parameter
305 (mime-entity-content-type message) "id"))))
306 (elmo-message-reassembled-mime-entity
308 (elmo-message-entity-field entity 'subject)
312 (elmo-message-mime-entity-internal
313 folder number rawbuf ignore-cache unread entire))))
316 (defun elmo-message-mime-entity-internal (folder number rawbuf
318 ignore-cache unread entire)
319 (let ((strategy (elmo-find-fetch-strategy folder number
322 (cond ((null strategy) nil)
323 ((eq (elmo-fetch-strategy-entireness strategy) 'section)
326 (luna-make-entity 'mime-elmo-imap-location
330 :strategy strategy)))
332 (with-current-buffer rawbuf
333 (let (buffer-read-only)
335 (elmo-message-fetch folder number strategy unread)))
336 (mime-open-entity 'elmo-buffer rawbuf)))))
339 (defconst elmo-mime-inherit-field-list-from-enclosed
340 '("^Content-.*:" "^Message-Id:" "^Subject:"
341 "^Encrypted.*:" "^MIME-Version:"))
343 (defsubst elmo-mime-make-reassembled-mime-entity (buffer)
344 (let ((entity (mime-open-entity 'elmo-buffer buffer)))
345 (mime-elmo-buffer-entity-set-reassembled-internal entity t)
348 (defun elmo-message-reassembled-mime-entity (folder id rawbuf subject
352 (let ((cache (elmo-file-cache-get (concat "<" id ">")))
354 (if (and (not ignore-cache)
355 (eq (elmo-file-cache-status cache) 'entire))
357 (with-current-buffer rawbuf
358 (let (buffer-read-only)
360 (elmo-file-cache-load (elmo-file-cache-path cache) nil))
361 (elmo-mime-make-reassembled-mime-entity rawbuf))
362 ;; reassemble fragment of the entity
363 (when (setq pieces (elmo-mime-collect-message/partial-pieces
366 (if (string-match "[0-9\n]+" subject)
367 (substring subject 0 (match-beginning 0))
369 ignore-cache unread))
370 (with-current-buffer rawbuf
371 (let (buffer-read-only
372 (outer-header (car pieces))
373 (pieces (sort (cdr pieces) (lambda (l r) (< (car l) (car r)))))
377 (insert (cdr (car pieces)))
378 (setq pieces (cdr pieces)))
379 (let ((case-fold-search t))
381 (std11-narrow-to-header)
382 (goto-char (point-min))
383 (while (re-search-forward std11-field-head-regexp nil t)
384 (let ((field-start (match-beginning 0)))
385 (unless (mime-visible-field-p
386 (buffer-substring field-start (match-end 0))
387 elmo-mime-inherit-field-list-from-enclosed
389 (delete-region field-start (1+ (std11-field-end))))))))
390 (goto-char (point-min))
391 (insert outer-header)
393 (elmo-file-cache-save (elmo-file-cache-path cache) nil)
394 (elmo-mime-make-reassembled-mime-entity rawbuf)))))))
396 (defun elmo-mime-collect-message/partial-pieces (folder id subject-regexp
402 (set-buffer-multibyte nil)
403 (let (total header pieces)
404 (elmo-folder-do-each-message-entity (entity folder)
407 (elmo-message-entity-field entity 'subject))
409 (let* ((message (elmo-message-mime-entity-internal
411 (elmo-message-entity-number entity)
415 (ct (mime-entity-content-type message))
416 (the-id (or (mime-content-type-parameter ct "id") ""))
418 (when (string= (downcase the-id)
420 (setq number (string-to-number
421 (mime-content-type-parameter ct "number")))
422 (setq pieces (cons (cons number (mime-entity-body message))
425 (let ((case-fold-search t))
427 (std11-narrow-to-header)
428 (goto-char (point-min))
429 (while (re-search-forward std11-field-head-regexp nil t)
430 (let ((field-start (match-beginning 0)))
431 (when (mime-visible-field-p
432 (buffer-substring field-start (match-end 0))
434 elmo-mime-inherit-field-list-from-enclosed)
438 field-start (std11-field-end))
441 (setq total (ignore-errors
443 (mime-content-type-parameter ct "total")))))
446 (>= (length pieces) total))
447 (throw 'complete (cons header pieces)))))))))
452 ;; Replacement of mime-display-message.
453 (defun elmo-mime-display-as-is-internal (message
454 &optional preview-buffer
455 mother default-keymap-or-function
456 original-major-mode keymap)
457 (mime-maybe-hide-echo-buffer)
458 (let ((win-conf (current-window-configuration)))
461 (concat "*Preview-" (mime-entity-name message) "*")))
462 (or original-major-mode
463 (setq original-major-mode major-mode))
464 (let ((inhibit-read-only t))
465 (set-buffer (get-buffer-create preview-buffer))
469 (setq mime-mother-buffer mother))
470 (setq mime-preview-original-window-configuration win-conf)
471 (setq major-mode 'mime-view-mode)
472 (setq mode-name "MIME-View")
475 (set-buffer-multibyte nil)
476 (insert (mime-entity-body message))
477 (set-buffer-multibyte t)
478 (decode-coding-region (point-min) (point-max)
479 elmo-mime-display-as-is-coding-system)
480 (goto-char (point-min))
482 (goto-char (point-min))
484 (let ((method (cdr (assq original-major-mode
485 mime-header-presentation-method-alist))))
486 (if (functionp method)
487 (funcall method message nil)))
489 ;; set original major mode for mime-preview-quit
490 (put-text-property (point-min) (point-max)
492 `((major-mode . ,original-major-mode)))
493 (put-text-property (point-min) (point-max)
494 'elmo-as-is-entity message)
497 (if default-keymap-or-function
498 (mime-view-define-keymap default-keymap-or-function)
499 mime-view-mode-default-map)))
500 (goto-char (point-min))
501 (search-forward "\n\n" nil t)
502 (run-hooks 'mime-view-mode-hook)
503 (set-buffer-modified-p nil)
504 (setq buffer-read-only t)
508 (product-provide (provide 'elmo-mime) (require 'elmo-version))
510 ;; elmo-mime.el ends here