1 ;;; modb-entity.el --- Message Entity Interface.
3 ;; Copyright (C) 2003 Yuuichi Teranishi <teranisi@gohome.org>
5 ;; Author: Yuuichi Teranishi <teranisi@gohome.org>
6 ;; Hiroya Murata <lapis-lazuli@pop06.odn.ne.jp>
7 ;; Keywords: mail, net news
9 ;; This file is part of ELMO (Elisp Library for Message Orchestration).
11 ;; This program is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; This program is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
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.
29 ;; Message entity handling.
33 (eval-when-compile (require 'cl))
40 (luna-define-class modb-entity-handler () (mime-charset))
41 (luna-define-internal-accessors 'modb-entity-handler))
43 (defcustom modb-entity-default-handler 'modb-legacy-entity-handler
44 "Default entity handler."
48 (defcustom modb-entity-field-extractor-alist
49 '((ml-info . modb-entity-extract-mailing-list-info))
50 "*An alist of field name and function to extract field body from buffer."
51 :type '(repeat (cons (symbol :tag "Field Name")
52 (function :tag "Function")))
55 (defvar modb-entity-default-cache-internal nil)
57 (defun elmo-message-entity-handler (&optional entity)
58 "Get modb entity handler instance which corresponds to the ENTITY."
61 (not (eq (car entity) t))
62 (not (stringp (car entity))))
64 (or modb-entity-default-cache-internal
65 (setq modb-entity-default-cache-internal
66 (luna-make-entity modb-entity-default-handler)))))
68 (luna-define-generic modb-entity-handler-list-parameters (handler)
69 "Return a parameter list of HANDLER.")
71 (luna-define-generic elmo-msgdb-make-message-entity (handler &rest args)
72 "Make a message entity using HANDLER.")
74 (luna-define-generic elmo-msgdb-message-entity-number (handler entity)
75 "Number of the ENTITY.")
77 (luna-define-generic elmo-msgdb-message-entity-set-number (handler
79 "Set number of the ENTITY.")
81 (luna-define-generic elmo-msgdb-message-entity-field (handler entity field
83 "Retrieve field value of the message entity.
84 HANDLER is the message entity handler.
85 ENTITY is the message entity structure.
86 FIELD is a symbol of the field.
87 If optional argument TYPE is specified, return converted value.")
89 (luna-define-generic elmo-msgdb-message-entity-set-field (handler
91 "Set the field value of the message entity.
92 HANDLER is the message entity handler.
93 ENTITY is the message entity structure.
94 FIELD is a symbol of the field.
95 VALUE is the field value to set.")
97 (luna-define-generic elmo-msgdb-message-entity-update-fields (handler
99 "Update message entity by VALUES.
100 HANDLER is the message entity handler.
101 ENTITY is the message entity structure.
102 VALUES is an alist of field-name and field-value.")
104 (luna-define-generic elmo-msgdb-copy-message-entity (handler entity
107 "Copy message entity.
108 HANDLER is the message entity handler.
109 ENTITY is the message entity structure.
110 If optional argument MAKE-HANDLER is specified, use it to make new entity.")
112 (luna-define-generic elmo-msgdb-create-message-entity-from-file (handler
115 "Create message entity from file.
116 HANDLER is the message entity handler.
117 NUMBER is the number of the newly created message entity.
118 FILE is the message file.")
120 (luna-define-generic elmo-msgdb-create-message-entity-from-buffer (handler
123 "Create message entity from current buffer.
124 HANDLER is the message entity handler.
125 NUMBER is the number of the newly created message entity.
126 Rest of the ARGS is a plist of message entity field for initial value.
127 Header region is supposed to be narrowed.")
129 ;; Transitional interface.
130 (luna-define-generic elmo-msgdb-message-match-condition (handler
133 "Return non-nil when the entity matches the condition.")
135 ;; Generic implementation.
136 (luna-define-method initialize-instance :after ((handler modb-entity-handler)
138 (unless (modb-entity-handler-mime-charset-internal handler)
139 (modb-entity-handler-set-mime-charset-internal handler elmo-mime-charset))
142 (luna-define-method modb-entity-handler-list-parameters
143 ((handler modb-entity-handler))
144 (list 'mime-charset))
146 (luna-define-method elmo-msgdb-create-message-entity-from-file
147 ((handler modb-entity-handler) number file)
148 (let (insert-file-contents-pre-hook ; To avoid autoconv-xmas...
149 insert-file-contents-post-hook header-end
150 (attrib (file-attributes file))
153 (if (not (file-exists-p file))
155 (setq size (nth 7 attrib))
156 (setq mtime (timezone-make-date-arpa-standard
157 (current-time-string (nth 5 attrib)) (current-time-zone)))
158 ;; insert header from file.
161 (elmo-msgdb-insert-file-header file)
162 (error (throw 'done nil)))
163 (goto-char (point-min))
165 (if (re-search-forward "\\(^--.*$\\)\\|\\(\n\n\\)" nil t)
168 (narrow-to-region (point-min) header-end)
169 (elmo-msgdb-create-message-entity-from-buffer
170 handler number :size size :date mtime))))))
172 (luna-define-method elmo-msgdb-make-message-entity ((handler
177 (luna-define-method elmo-msgdb-message-entity-field ((handler
181 (plist-get (cdr entity) (intern (concat ":" (symbol-name field)))))
183 (luna-define-method elmo-msgdb-message-entity-number ((handler
186 (plist-get (cdr entity) :number))
188 (luna-define-method elmo-msgdb-message-entity-update-fields
189 ((handler modb-entity-handler) entity values)
191 (dolist (pair values)
194 (elmo-msgdb-message-entity-field handler entity (car pair)))
195 (elmo-msgdb-message-entity-set-field handler entity
196 (car pair) (cdr pair))
201 (defsubst modb-entity-handler-mime-charset (handler)
202 (or (modb-entity-handler-mime-charset-internal handler)
205 (defun modb-entity-handler-equal-p (handler other)
206 "Return non-nil, if OTHER hanlder is equal this HANDLER."
207 (and (eq (luna-class-name handler)
208 (luna-class-name other))
210 (dolist (slot (modb-entity-handler-list-parameters handler))
211 (when (not (equal (luna-slot-value handler slot)
212 (luna-slot-value other slot)))
213 (throw 'mismatch nil)))
216 (defun modb-entity-handler-dump-parameters (handler)
217 "Return parameters for reconstruct HANDLER as plist."
219 (mapcar (lambda (slot)
220 (let ((value (luna-slot-value handler slot)))
222 (list (intern (concat ":" (symbol-name slot)))
224 (modb-entity-handler-list-parameters handler))))
226 ;; field in/out converter
227 (defun modb-set-field-converter (converter type &rest specs)
228 "Set convert function of TYPE into CONVERTER.
229 SPECS must be like `FIELD1 FUNCTION1 FIELD2 FUNCTION2 ...'.
230 If each field is t, function is set as default converter."
232 (let ((alist (symbol-value converter))
235 (let ((field (pop specs))
236 (function (pop specs))
238 (if (setq cell (assq type alist))
239 (setcdr cell (put-alist field function (cdr cell)))
240 (setq cell (cons type (list (cons field function)))
241 alist (cons cell alist)))
242 ;; support colon keyword (syntax sugar).
243 (unless (or (eq field t)
244 (string-match "^:" (symbol-name field)))
245 (setcdr cell (put-alist (intern (concat ":" (symbol-name field)))
248 (set converter alist))))
249 (put 'modb-set-field-converter 'lisp-indent-function 2)
251 (defsubst modb-convert-field-value (converter field value &optional type)
253 (let* ((alist (cdr (assq (or type t) converter)))
254 (function (cdr (or (assq field alist)
257 (funcall function field value)
261 (defvar elmo-msgdb-decoded-cache-hashtb nil)
262 (make-variable-buffer-local 'elmo-msgdb-decoded-cache-hashtb)
264 (defsubst elmo-msgdb-get-decoded-cache (string)
265 (if elmo-use-decoded-cache
266 (let ((hashtb (or elmo-msgdb-decoded-cache-hashtb
267 (setq elmo-msgdb-decoded-cache-hashtb
268 (elmo-make-hash 2048))))
270 (or (elmo-get-hash-val string hashtb)
273 (elmo-with-enable-multibyte
274 (decode-mime-charset-string string elmo-mime-charset)))
275 (elmo-set-hash-val string decoded hashtb))))
276 (elmo-with-enable-multibyte
277 (decode-mime-charset-string string elmo-mime-charset))))
279 (defun modb-entity-string-decoder (field value)
280 (elmo-msgdb-get-decoded-cache value))
282 (defun modb-entity-string-encoder (field value)
283 (elmo-with-enable-multibyte
284 (encode-mime-charset-string value elmo-mime-charset)))
286 (defun modb-entity-parse-date-string (field value)
288 (elmo-time-parse-date-string value)
291 (defun modb-entity-make-date-string (field value)
294 (elmo-time-make-date-string value)))
296 (defun modb-entity-mime-decoder (field value)
297 (mime-decode-field-body value (symbol-name field) 'summary))
299 (defun modb-entity-mime-encoder (field value)
300 (mime-encode-field-body value (symbol-name field)))
302 (defun modb-entity-address-list-decoder (field value)
304 (mapcar (lambda (address)
305 (mime-decode-field-body address (symbol-name field)))
306 (elmo-parse-addresses value))
309 (defun modb-entity-address-list-encoder (field value)
312 (mime-encode-field-body (mapconcat 'identity value ", ")
313 (symbol-name field))))
315 (defun modb-entity-parse-address-string (field value)
316 (modb-entity-encode-string-recursive
319 (elmo-parse-addresses value)
322 (defun modb-entity-make-address-string (field value)
323 (let ((value (modb-entity-decode-string-recursive field value)))
326 (mapconcat 'identity value ", "))))
328 (defun modb-entity-decode-string-recursive (field value)
329 (cond ((stringp value)
330 (elmo-msgdb-get-decoded-cache value))
332 (setcar value (modb-entity-decode-string-recursive field (car value)))
333 (setcdr value (modb-entity-decode-string-recursive field (cdr value)))
338 (defun modb-entity-encode-string-recursive (field value)
339 (cond ((stringp value)
340 (elmo-with-enable-multibyte
341 (encode-mime-charset-string value elmo-mime-charset)))
343 (setcar value (modb-entity-encode-string-recursive field (car value)))
344 (setcdr value (modb-entity-encode-string-recursive field (cdr value)))
350 (defun modb-entity-create-field-indices (slots)
354 (setq indices (cons (cons (car slots) index) indices)
359 (mapcar (lambda (cell)
360 (cons (intern (concat ":" (symbol-name (car cell))))
365 ;; Legacy implementation.
367 (luna-define-class modb-legacy-entity-handler (modb-entity-handler)))
369 (defconst modb-legacy-entity-field-slots
380 (defconst modb-legacy-entity-field-indices
381 (modb-entity-create-field-indices modb-legacy-entity-field-slots))
383 (defvar modb-legacy-entity-normalizer nil)
384 (modb-set-field-converter 'modb-legacy-entity-normalizer nil
388 'from #'modb-entity-string-encoder
389 'subject #'modb-entity-string-encoder
390 'date #'modb-entity-make-date-string
391 'to #'modb-entity-address-list-encoder
392 'cc #'modb-entity-address-list-encoder
394 t #'modb-entity-mime-encoder)
396 (defvar modb-legacy-entity-specializer nil)
398 (modb-set-field-converter 'modb-legacy-entity-specializer nil
402 'from #'modb-entity-string-decoder
403 'subject #'modb-entity-string-decoder
404 'date #'modb-entity-parse-date-string
405 'to #'modb-entity-address-list-decoder
406 'cc #'modb-entity-address-list-decoder
408 t #'modb-entity-mime-decoder)
410 (modb-set-field-converter 'modb-legacy-entity-specializer 'string
412 'number nil ; not supported
414 'from #'modb-entity-string-decoder
415 'subject #'modb-entity-string-decoder
417 'size nil ; not supported
418 t #'modb-entity-mime-decoder)
421 (defmacro modb-legacy-entity-field-index (field)
422 `(cdr (assq ,field modb-legacy-entity-field-indices)))
424 (defsubst modb-legacy-entity-set-field (entity field value &optional as-is)
428 (setq value (modb-convert-field-value
429 modb-legacy-entity-normalizer
431 (cond ((memq field '(message-id :message-id))
432 (setcar entity value))
433 ((setq index (modb-legacy-entity-field-index field))
434 (aset (cdr entity) index value))
436 (setq index (modb-legacy-entity-field-index :extra))
437 (let ((extras (and entity (aref (cdr entity) index)))
439 (if (setq extra (assoc (symbol-name field) extras))
441 (aset (cdr entity) index (cons (cons (symbol-name field)
442 value) extras)))))))))
444 (defsubst modb-legacy-make-message-entity (args)
445 "Make an message entity."
446 (let ((entity (cons nil (make-vector 9 nil)))
449 (setq field (pop args)
452 (modb-legacy-entity-set-field entity field value)))
455 (luna-define-method elmo-msgdb-make-message-entity
456 ((handler modb-legacy-entity-handler) args)
457 (modb-legacy-make-message-entity args))
459 (luna-define-method elmo-msgdb-create-message-entity-from-buffer
460 ((handler modb-legacy-entity-handler) number args)
461 (let ((extras elmo-msgdb-extra-fields)
462 (default-mime-charset default-mime-charset)
463 entity message-id references from subject to cc date
464 extra field-body charset size)
466 (setq entity (modb-legacy-make-message-entity args))
467 (set-buffer-multibyte default-enable-multibyte-characters)
468 (setq message-id (elmo-msgdb-get-message-id-from-buffer))
469 (and (setq charset (cdr (assoc "charset" (mime-read-Content-Type))))
470 (setq charset (intern-soft charset))
471 (setq default-mime-charset charset))
473 (elmo-msgdb-get-references-from-buffer)
474 from (elmo-replace-in-string
475 (elmo-mime-string (or (elmo-field-body "from")
478 subject (elmo-replace-in-string
479 (elmo-mime-string (or (elmo-field-body "subject")
482 date (elmo-decoded-field-body "date")
483 to (mapconcat 'identity (elmo-multiple-field-body "to") ",")
484 cc (mapconcat 'identity (elmo-multiple-field-body "cc") ","))
485 (unless (elmo-msgdb-message-entity-field handler entity 'size)
486 (if (setq size (elmo-field-body "content-length"))
487 (setq size (string-to-int size))
490 (if (setq field-body (elmo-field-body (car extras)))
491 (modb-legacy-entity-set-field
492 entity (intern (downcase (car extras))) field-body 'as-is))
493 (setq extras (cdr extras)))
494 (dolist (field '(message-id number references from subject
496 (when (symbol-value field)
497 (modb-legacy-entity-set-field
498 entity field (symbol-value field) 'as-is)))
501 (luna-define-method elmo-msgdb-message-entity-number
502 ((handler modb-legacy-entity-handler) entity)
503 (and entity (aref (cdr entity) 0)))
505 (luna-define-method elmo-msgdb-message-entity-set-number
506 ((handler modb-legacy-entity-handler) entity number)
507 (and entity (aset (cdr entity) 0 number)))
509 (luna-define-method elmo-msgdb-message-entity-field
510 ((handler modb-legacy-entity-handler) entity field &optional type)
513 (modb-convert-field-value
514 modb-legacy-entity-specializer
516 (cond ((memq field '(message-id :message-id))
518 ((setq index (modb-legacy-entity-field-index field))
519 (aref (cdr entity) index))
521 (setq index (modb-legacy-entity-field-index :extra))
522 (cdr (assoc (symbol-name field)
523 (aref (cdr entity) index)))))
526 (luna-define-method elmo-msgdb-message-entity-set-field
527 ((handler modb-legacy-entity-handler) entity field value)
528 (modb-legacy-entity-set-field entity field value))
530 (luna-define-method elmo-msgdb-copy-message-entity
531 ((handler modb-legacy-entity-handler) entity &optional make-handler)
533 (let ((copy (elmo-msgdb-make-message-entity make-handler)))
534 (dolist (field (append '(message-id number references from subject
536 (mapcar (lambda (extra) (intern (car extra)))
537 (aref (cdr entity) 8))))
538 (elmo-msgdb-message-entity-set-field
539 make-handler copy field
540 (elmo-msgdb-message-entity-field handler entity field)))
543 (copy-sequence (cdr entity)))))
545 (luna-define-method elmo-msgdb-message-match-condition
546 ((handler modb-entity-handler) condition entity)
547 (let ((key (elmo-filter-key condition))
551 ((or (string= key "since")
552 (string= key "before"))
553 (let ((field-date (elmo-msgdb-message-entity-field
554 handler entity 'date))
556 (elmo-datevec-to-time
557 (elmo-date-get-datevec
558 (elmo-filter-value condition)))))
559 (if (string= key "since")
560 (not (elmo-time< field-date specified-date))
561 (elmo-time< field-date specified-date))))
562 ((setq field-value (elmo-msgdb-message-entity-field handler
566 (and (stringp field-value)
567 (string-match (elmo-filter-value condition) field-value)))
572 ;; Standard implementation.
574 (luna-define-class modb-standard-entity-handler (modb-entity-handler)))
576 (defconst modb-standard-entity-field-slots
589 (defconst modb-standard-entity-field-indices
590 (modb-entity-create-field-indices modb-standard-entity-field-slots))
592 (defvar modb-standard-entity-normalizer nil)
593 (modb-set-field-converter 'modb-standard-entity-normalizer nil
596 'date #'modb-entity-parse-date-string
597 'to #'modb-entity-parse-address-string
598 'cc #'modb-entity-parse-address-string
602 t #'modb-entity-encode-string-recursive)
604 (defvar modb-standard-entity-specializer nil)
605 (modb-set-field-converter 'modb-standard-entity-specializer nil
612 t #'modb-entity-decode-string-recursive)
613 (modb-set-field-converter 'modb-standard-entity-specializer 'string
616 'date #'modb-entity-make-date-string
617 'to #'modb-entity-make-address-string
618 'cc #'modb-entity-make-address-string
622 'ml-info #'modb-entity-make-mailing-list-info-string
623 t #'modb-entity-decode-string-recursive)
625 (defmacro modb-standard-entity-field-index (field)
626 `(cdr (assq ,field modb-standard-entity-field-indices)))
628 (defsubst modb-standard-entity-set-field (entity field value &optional as-is)
632 (let ((elmo-mime-charset
633 (modb-entity-handler-mime-charset (car entity))))
634 (setq value (modb-convert-field-value modb-standard-entity-normalizer
636 (cond ((memq field '(message-id :message-id))
637 (setcar (cdr entity) value))
638 ((setq index (modb-standard-entity-field-index field))
639 (aset (cdr (cdr entity)) index value))
641 (setq index (modb-standard-entity-field-index :extra))
642 (let ((extras (aref (cdr (cdr entity)) index))
644 (if (setq cell (assq field extras))
646 (aset (cdr (cdr entity))
648 (cons (cons field value) extras)))))))))
650 (defsubst modb-standard-make-message-entity (handler args)
651 (let ((entity (cons handler
654 (length modb-standard-entity-field-slots)
658 (setq field (pop args)
661 (modb-standard-entity-set-field entity field value)))
664 (luna-define-method elmo-msgdb-make-message-entity
665 ((handler modb-standard-entity-handler) args)
666 (modb-standard-make-message-entity handler args))
668 (luna-define-method elmo-msgdb-message-entity-number
669 ((handler modb-standard-entity-handler) entity)
670 (and entity (aref (cdr (cdr entity)) 0)))
672 (luna-define-method elmo-msgdb-message-entity-set-number
673 ((handler modb-standard-entity-handler) entity number)
674 (and entity (aset (cdr (cdr entity)) 0 number)))
676 (luna-define-method elmo-msgdb-message-entity-field
677 ((handler modb-standard-entity-handler) entity field &optional type)
679 (let ((elmo-mime-charset
680 (modb-entity-handler-mime-charset handler))
682 (modb-convert-field-value
683 modb-standard-entity-specializer
685 (cond ((memq field '(message-id :message-id))
687 ((setq index (modb-standard-entity-field-index field))
688 (aref (cdr (cdr entity)) index))
690 (setq index (modb-standard-entity-field-index :extra))
691 (cdr (assq field (aref (cdr (cdr entity)) index)))))
694 (luna-define-method elmo-msgdb-message-entity-set-field
695 ((handler modb-standard-entity-handler) entity field value)
696 (modb-standard-entity-set-field entity field value))
698 (luna-define-method elmo-msgdb-copy-message-entity
699 ((handler modb-standard-entity-handler) entity &optional make-handler)
701 (let ((copy (elmo-msgdb-make-message-entity make-handler)))
702 (dolist (field (nconc
704 (copy-sequence modb-standard-entity-field-slots))
708 (modb-standard-entity-field-index :extra)))
710 (elmo-msgdb-message-entity-set-field
711 make-handler copy field
712 (elmo-msgdb-message-entity-field handler entity field)))
715 (cons (car (cdr entity))
716 (copy-sequence (cdr (cdr entity)))))))
718 (luna-define-method elmo-msgdb-create-message-entity-from-buffer
719 ((handler modb-standard-entity-handler) number args)
720 (let ((default-mime-charset default-mime-charset)
721 entity content-type charset)
723 (set-buffer-multibyte default-enable-multibyte-characters)
724 (and (setq content-type (elmo-decoded-field-body
725 "content-type" 'summary))
726 (setq charset (mime-content-type-parameter
727 (mime-parse-Content-Type content-type) "charset"))
728 (setq charset (intern-soft charset))
729 (mime-charset-p charset)
730 (setq default-mime-charset charset))
732 (modb-standard-make-message-entity
740 (elmo-msgdb-get-message-id-from-buffer)
742 (elmo-msgdb-get-references-from-buffer)
744 (elmo-replace-in-string
745 (or (elmo-decoded-field-body "from" 'summary)
749 (elmo-replace-in-string
750 (or (elmo-decoded-field-body "subject" 'summary)
754 (elmo-decoded-field-body "date" 'summary)
758 (mime-decode-field-body field-body "to" 'summary))
759 (elmo-multiple-field-body "to") ",")
763 (mime-decode-field-body field-body "cc" 'summary))
764 (elmo-multiple-field-body "cc") ",")
768 (let ((size (elmo-field-body "content-length")))
771 (or (plist-get args :size) 0)))))))
772 (let (field-name field-body extractor)
773 (dolist (extra (cons "newsgroups" elmo-msgdb-extra-fields))
774 (setq field-name (intern (downcase extra))
775 extractor (cdr (assq field-name
776 modb-entity-field-extractor-alist))
777 field-body (if extractor
778 (funcall extractor field-name)
779 (elmo-decoded-field-body extra 'summary)))
781 (modb-standard-entity-set-field entity field-name field-body))))
785 ;; mailing list info handling
786 (defun modb-entity-extract-ml-info-from-x-sequence ()
787 (let ((sequence (elmo-decoded-field-body "x-sequence" 'summary))
790 (elmo-set-list '(name count) (split-string sequence " "))
793 (defun modb-entity-extract-ml-info-from-subject ()
794 (let ((subject (elmo-decoded-field-body "subject" 'summary)))
796 (string-match "^\\s(\\(\\S)+\\)[ :]\\([0-9]+\\)\\s)[ \t]*"
798 (cons (match-string 1 subject) (match-string 2 subject)))))
800 (defun modb-entity-extract-ml-info-from-return-path ()
801 (let ((return-path (elmo-decoded-field-body "return-path" 'summary)))
802 (when (and return-path
803 (string-match "^<\\([^@>]+\\)-return-\\([0-9]+\\)-"
805 (cons (match-string 1 return-path)
806 (match-string 2 return-path)))))
808 (defun modb-entity-extract-ml-info-from-delivered-to ()
809 (let ((delivered-to (elmo-decoded-field-body "delivered-to" 'summary)))
810 (when (and delivered-to
811 (string-match "^mailing list \\([^@]+\\)@" delivered-to))
812 (cons (match-string 1 delivered-to) nil))))
814 (defun modb-entity-extract-ml-info-from-mailing-list ()
815 (let ((mailing-list (elmo-decoded-field-body "mailing-list" 'summary)))
816 ;; *-help@, *-owner@, etc.
817 (when (and mailing-list
818 (string-match "\\(^\\|; \\)contact \\([^@]+\\)-[^-@]+@"
820 (cons (match-string 2 mailing-list) nil))))
822 (defvar modb-entity-extract-mailing-list-info-functions
823 '(modb-entity-extract-ml-info-from-x-sequence
824 modb-entity-extract-ml-info-from-subject
825 modb-entity-extract-ml-info-from-return-path
826 modb-entity-extract-ml-info-from-delivered-to
827 modb-entity-extract-ml-info-from-mailing-list))
829 (defun modb-entity-extract-mailing-list-info (field)
830 (let ((ml-name (elmo-decoded-field-body "x-ml-name" 'summary))
831 (ml-count (or (elmo-decoded-field-body "x-mail-count" 'summary)
832 (elmo-decoded-field-body "x-ml-count" 'summary)))
833 (functions modb-entity-extract-mailing-list-info-functions)
835 (while (and functions
836 (or (null ml-name) (null ml-count)))
837 (when (setq result (funcall (car functions)))
839 (setq ml-name (car result)))
841 (setq ml-count (cdr result))))
842 (setq functions (cdr functions)))
843 (when (or ml-name ml-count)
844 (cons (and ml-name (car (split-string ml-name " ")))
845 (and ml-count (string-to-int ml-count))))))
847 (defun modb-entity-make-mailing-list-info-string (field value)
849 (format (if (cdr value) "(%s %05.0f)" "(%s)")
850 (elmo-msgdb-get-decoded-cache (car value))
853 ;; message buffer handler
855 (luna-define-class modb-buffer-entity-handler (modb-entity-handler)))
857 (defvar modb-buffer-entity-specializer nil)
858 (modb-set-field-converter 'modb-buffer-entity-specializer nil
859 'date #'elmo-time-parse-date-string)
861 (luna-define-method elmo-msgdb-make-message-entity
862 ((handler modb-buffer-entity-handler) args)
863 (cons handler (cons (or (plist-get args :number)
864 (plist-get args 'number))
865 (or (plist-get args :buffer)
866 (plist-get args 'buffer)
869 (luna-define-method elmo-msgdb-message-entity-number
870 ((handler modb-buffer-entity-handler) entity)
873 (luna-define-method elmo-msgdb-message-entity-set-number
874 ((handler modb-buffer-entity-handler) entity number)
875 (and entity (setcar (cdr entity) number)))
877 (luna-define-method elmo-msgdb-message-entity-field
878 ((handler modb-buffer-entity-handler) entity field &optional type)
880 (let ((elmo-mime-charset
881 (modb-entity-handler-mime-charset handler)))
882 (modb-convert-field-value
883 modb-buffer-entity-specializer
885 (if (memq field '(number :number))
887 (with-current-buffer (cdr (cdr entity))
888 (let ((extractor (cdr (assq field
889 modb-entity-field-extractor-alist))))
891 (funcall extractor field)
894 (mime-decode-field-body field-body (symbol-name field)
896 (elmo-multiple-field-body (symbol-name field))
900 (luna-define-method elmo-msgdb-message-match-condition :around
901 ((handler modb-buffer-entity-handler) condition entity)
902 (let ((key (elmo-filter-key condition))
903 (case-fold-search t))
905 ((string= (elmo-filter-key condition) "body")
906 (with-current-buffer (cdr (cdr entity))
907 (goto-char (point-min))
908 (and (re-search-forward "^$" nil t) ; goto body
909 (search-forward (elmo-filter-value condition) nil t))))
911 (luna-call-next-method)))))
914 (product-provide (provide 'modb-entity) (require 'elmo-version))
916 ;;; modb-entity.el ends here