(TopLevel): Require `mime' and `eword-deocode' instead of `mmbuffer'.
[elisp/gnus.git-] / lisp / mmgnus.el
1 ;;; mmgnus.el --- MIME entity implementation for gnus-article
2
3 ;; Copyright (C) 1998 Free Software Foundation, Inc.
4
5 ;; Author: MORIOKA Tomohiko <tomo@m17n.org>
6 ;;         Keiichi Suzuki <keiichi@nanp.org>
7 ;; Keywords: MIME, multimedia, mail, news
8
9 ;; This file is part of Nana-gnus.
10
11 ;; This program is free software; you can redistribute it and/or
12 ;; modify it under the terms of the GNU General Public License as
13 ;; published by the Free Software Foundation; either version 2, or (at
14 ;; your option) any later version.
15
16 ;; This program is distributed in the hope that it will be useful, but
17 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19 ;; General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
25
26 ;;; Code:
27
28 (require 'mime)
29 (require 'eword-decode)
30
31 (luna-define-class mmgnus-entity (mime-entity)
32                    (body
33                     header undisplayer content-description cache content-id))
34
35 (luna-define-internal-accessors 'mmgnus-entity)
36
37 (luna-define-method initialize-instance ((entity mmgnus-entity)
38                                          &rest init-args)
39   (apply (car (luna-class-find-functions
40                (luna-find-class 'standard-object)
41                'initialize-instance))
42          entity init-args))
43
44 (defun mmgnus-visible-field-p (field-name visible-fields invisible-fields)
45   (or (catch 'found
46         (while visible-fields
47           (let ((regexp (car visible-fields)))
48             (if (string-match regexp field-name)
49                 (throw 'found t)
50               ))
51           (setq visible-fields (cdr visible-fields))
52           ))
53       (catch 'found
54         (while invisible-fields
55           (let ((regexp (car invisible-fields)))
56             (if (string-match regexp field-name)
57                 (throw 'found nil)
58               ))
59           (setq invisible-fields (cdr invisible-fields))
60           )
61         t)))
62
63 (defun mmgnus-insert-header-from-string (string
64                                          &optional invisible-fields
65                                          visible-fields)
66   (let ((the-buf (current-buffer))
67         (mode-obj (mime-find-field-presentation-method 'wide))
68         field-decoder
69         f-b p f-e field-name len field field-body buffer)
70     (with-temp-buffer
71       (setq buffer (current-buffer))
72       (insert string)
73       (goto-char (point-min))
74       (while (re-search-forward std11-field-head-regexp nil t)
75         (setq f-b (match-beginning 0)
76               p (match-end 0)
77               field-name (buffer-substring f-b p)
78               len (string-width field-name)
79               f-e (std11-field-end))
80         (when (mmgnus-visible-field-p field-name
81                                       visible-fields invisible-fields)
82           (setq field (intern
83                        (capitalize (buffer-substring f-b (1- p))))
84                 field-body (buffer-substring p f-e)
85                 field-decoder (inline (mime-find-field-decoder-internal
86                                        field mode-obj)))
87           (with-current-buffer the-buf
88             (insert field-name)
89             (insert (if field-decoder
90                         (funcall field-decoder field-body len)
91                       ;; Don't decode
92                       field-body))
93             (insert "\n")
94             ))))))
95
96 (luna-define-method mime-insert-header ((entity mmgnus-entity)
97                                         &optional invisible-fields
98                                         visible-fields)
99   (mmgnus-insert-header-from-string
100    (mmgnus-entity-header-internal entity)
101    invisible-fields visible-fields))
102
103 (luna-define-method mime-entity-content ((entity mmgnus-entity))
104   (save-excursion
105     (cond
106      ((bufferp (mmgnus-entity-body-internal entity))
107       (set-buffer (mmgnus-entity-body-internal entity))
108       (mime-decode-string (buffer-string) (mime-entity-encoding entity)))
109      (t
110       (error "Invalid body object. %s"
111              (mmgnus-entity-body-internal entity))))))
112
113 (luna-define-class mime-gnus-entity (mmgnus-entity)
114                    (number
115                     subject from date id references chars lines xref extra))
116
117 (luna-define-internal-accessors 'mime-gnus-entity)
118
119 (luna-define-method initialize-instance ((entity mime-gnus-entity)
120                                          &rest init-args)
121   (apply (car (luna-class-find-functions
122                (luna-find-class 'standard-object)
123                'initialize-instance))
124          entity init-args))
125
126 (luna-define-method mime-insert-header :around ((entity mime-gnus-entity)
127                                                 &optional invisible-fields
128                                                 visible-fields)
129   (luna-call-next-method))
130
131 (luna-define-method mime-entity-content :around ((entity mime-gnus-entity))
132   (luna-call-next-method))
133
134 ;;; @ end
135 ;;;
136
137 (provide 'mmgnus)
138
139 ;;; mmgnus.el ends here