1 ;;; elmo-split.el --- Split messages according to the user defined rules.
3 ;; Copyright (C) 2002 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.
28 ;; Put following lines on your .emacs.
30 ;; (autoload 'elmo-split "elmo-split" "Split messages on the folder." t)
32 ;; A command elmo-split is provided. If you enter:
36 ;; Messages in the `elmo-split-folder' are splitted to the folders
37 ;; according to the definition of `elmo-split-rule'.
43 (defcustom elmo-split-rule nil
44 "Split rule for the command `elmo-split'.
45 The format of this variable is a list of RULEs which has form like:
46 \(CONDITION ACTION [continue]\)
48 The 1st element CONDITION is a sexp which consists of following.
50 1. Functions which accept argument FIELD-NAME and VALUE.
51 FIELD-NAME is a symbol of the field name.
53 `equal' ... True if the field value equals to VALUE.
54 Case of the letters are ignored.
55 `match' ... True if the field value matches to VALUE.
56 VALUE can contain \\& and \\N which will substitute
57 from matching \\(\\) patterns in the previous VALUE.
58 `address-equal' ... True if one of the addresses in the field equals to
59 VALUE. Case of the letters are ignored.
60 `address-match' ... True if one of the addresses in the field matches to
62 VALUE can contain \\& and \\N which will substitute
63 from matching \\(\\) patterns in the previous VALUE.
65 2. Functions which accept any number of arguments.
67 `or' ... True if one of the argument returns true.
68 `and' ... True if all of the arguments return true.
72 When a symbol is specified, it is evaluated.
74 The 2nd element ACTION is the name of the destination folder or some symbol.
75 If CONDITION is satisfied, the message is splitted according to this value.
77 If ACTION is a string, it will be considered as the name of destination folder.
78 Symbol `delete' means that the substance of the message will be removed. On the
79 other hand, symbol `noop' is used to do nothing and keep the substance of the
80 message as it is. Or, if some function is specified, it will be called.
82 When the 3rd element `continue' is specified as symbol, evaluating rules is
83 not stopped even when the condition is satisfied.
87 \(setq elmo-split-rule
88 ;; Messages from spammers are stored in `+junk'
89 '(((or (address-equal from \"i.am@spammer\")
90 (address-equal from \"dull-work@dull-boy\")
91 (address-equal from \"death-march@software\")
92 (address-equal from \"ares@aon.at\")
93 (address-equal from \"get-money@richman\"))
95 ;; Messages from mule mailing list are stored in `%mule'
96 ((equal x-ml-name \"mule\") \"%mule\")
97 ;; Messages from wanderlust mailing list are stored in `%wanderlust'
98 ;; and continue evaluating following rules.
99 ((equal x-ml-name \"wanderlust\") \"%wanderlust\" continue)
100 ;; Messages from DoCoMo user are stored in `+docomo-{username}'.
101 ((match from \"\\\\(.*\\\\)@docomo\\\\.ne\\\\.jp\")
103 ;; Unmatched mails go to `+inbox'.
108 (defcustom elmo-split-folder "%inbox"
109 "Target folder or list of folders for splitting."
110 :type '(choice (string :tag "folder name")
111 (repeat (string :tag "folder name")))
114 (defcustom elmo-split-default-action 'noop
115 "Default action for messages which pass all rules."
116 :type '(choice (const :tag "do not touch" noop)
117 (const :tag "delete" delete)
118 (string :tag "folder name")
119 (function :tag "function"))
122 (defcustom elmo-split-log-coding-system 'x-ctext
123 "A coding-system for writing log file."
127 (defcustom elmo-split-log-file "~/.elmo/split-log"
128 "The file name of the split log."
133 (defvar elmo-split-match-string-internal nil
134 "Internal variable for string matching. Don't touch this variable by hand.")
136 (defvar elmo-split-message-entity nil
137 "Buffer local variable to store mime-entity.")
138 (make-variable-buffer-local 'elmo-split-message-entity)
141 (defun elmo-split-or (buffer &rest args)
144 (if (elmo-split-eval buffer arg)
148 (defun elmo-split-and (buffer &rest args)
151 (unless (elmo-split-eval buffer arg)
155 (defun elmo-split-> (buffer size)
156 (> (buffer-size buffer) size))
158 (defun elmo-split-< (buffer size)
159 (< (buffer-size buffer) size))
161 (defun elmo-split-address-equal (buffer field value)
162 (with-current-buffer buffer
164 'std11-address-string
165 (std11-parse-addresses-string
166 (std11-field-body (symbol-name field)))))
170 (when (string-match (concat "^"
175 (setq addrs (cdr addrs)))
178 (defun elmo-split-address-match (buffer field value)
179 (with-current-buffer buffer
181 'std11-address-string
182 (std11-parse-addresses-string
183 (std11-field-body (symbol-name field)))))
186 (when (string-match value (car addrs))
187 (setq elmo-split-match-string-internal (car addrs)
190 (setq addrs (cdr addrs)))
193 (defun elmo-split-fetch-decoded-field (entity field-name)
194 (let ((sym (intern (capitalize field-name)))
195 (field-body (mime-entity-fetch-field entity field-name)))
197 (mime-decode-field-body field-body sym 'plain))))
199 (defun elmo-split-equal (buffer field value)
200 (with-current-buffer buffer
201 (let ((field-value (and
202 elmo-split-message-entity
203 (elmo-split-fetch-decoded-field
204 elmo-split-message-entity
205 (symbol-name field)))))
206 (equal field-value value))))
208 (defun elmo-split-match (buffer field value)
209 (with-current-buffer buffer
210 (let ((field-value (and elmo-split-message-entity
211 (elmo-split-fetch-decoded-field
212 elmo-split-message-entity
213 (symbol-name field)))))
215 (when (string-match value field-value)
216 (setq elmo-split-match-string-internal field-value))))))
218 (defun elmo-split-eval (buffer sexp)
221 (apply (intern (concat "elmo-split-" (symbol-name (car sexp))))
225 (std11-field-body sexp))
228 (defun elmo-split-log (message reharsal)
229 (with-current-buffer (get-buffer-create "*elmo-split*")
230 (goto-char (point-max))
231 (let ((start (point))
232 (coding-system-for-write elmo-split-log-coding-system))
236 (pop-to-buffer (current-buffer))
238 (write-region start (point) elmo-split-log-file t 'no-msg)))))
241 (defun elmo-split (&optional arg)
242 "Split messages in the `elmo-split-folder' according to `elmo-split-rule'.
243 If prefix argument ARG is specified, do a reharsal (no harm)."
245 (unless elmo-split-rule
246 (error "Split rule does not exist. Set `elmo-split-rule' first"))
247 (let ((folders (if (listp elmo-split-folder)
249 (list elmo-split-folder)))
253 (dolist (folder folders)
254 (setq ret (elmo-split-subr (elmo-make-folder folder) arg)
255 count (+ count (car ret))
256 fcount (+ fcount (cdr ret))))
257 (run-hooks 'elmo-split-hook)
262 "No message is splitted")
264 "1 message is splitted")
266 (format "%d messages are splitted" count)))
269 (format " (%d failure)." fcount))))))
271 (defun elmo-split-subr (folder &optional reharsal)
272 (let ((elmo-inhibit-display-retrieval-progress t)
275 (default-rule `((t ,elmo-split-default-action)))
276 msgs action target-folder failure delete-substance
277 record-log log-string)
278 (message "Splitting...")
279 (elmo-folder-open-internal folder)
280 (setq msgs (elmo-folder-list-messages folder))
281 (elmo-progress-set 'elmo-split (length msgs) "Splitting...")
288 (elmo-message-fetch folder msg
289 (elmo-make-fetch-strategy 'entire)
290 nil (current-buffer) 'unread))
291 (setq elmo-split-message-entity (mime-parse-buffer))
293 (dolist (rule (append elmo-split-rule default-rule))
294 (setq elmo-split-match-string-internal nil)
295 (when (elmo-split-eval (current-buffer) (car rule))
296 (if (and (stringp (nth 1 rule))
297 elmo-split-match-string-internal)
298 (setq action (elmo-expand-newtext
300 elmo-split-match-string-internal))
301 (setq action (nth 1 rule)))
302 ;; 1. ACTION & DELETION
312 (setq target-folder (elmo-make-folder action))
313 (unless (elmo-folder-exists-p target-folder)
316 (elmo-folder-creatable-p target-folder)
319 "Folder %s does not exist, Create it? "
321 (elmo-folder-create target-folder)))
322 (elmo-folder-open-internal target-folder)
323 (elmo-folder-append-buffer target-folder 'unread)
324 (elmo-folder-close-internal target-folder))
325 (error (setq failure t)
330 (eq (nth 2 rule) 'continue))))
341 (error "Wrong action specified in elmo-split-rule")))
342 (when delete-substance
344 (elmo-folder-delete-messages folder (list msg)))))
350 (nth 1 (std11-extract-address-components
351 (or (std11-field-body "from") "")))
352 " " (or (std11-field-body "date") "") "\n"
354 (eword-decode-string (or (std11-field-body
360 (concat " Test: " action "\n"))
362 " Test: /dev/null\n")
364 " Test: do nothing\n")
366 (format " Test: function:%s\n"
367 (symbol-name action)))
369 " ERROR: wrong action specified\n"))
372 (concat " FAILED: " action "\n"))
374 (concat " Folder: " action "\n"))
382 ;; 3. CONTINUATION CHECK
383 (unless (eq (nth 2 rule) 'continue)
384 (throw 'terminate nil))))))
385 (elmo-progress-notify 'elmo-split)))
386 (elmo-folder-close-internal folder))
387 (elmo-progress-clear 'elmo-split))
388 (cons count fcount)))
391 (product-provide (provide 'elmo-split) (require 'elmo-version))
393 ;;; elmo-split.el ends here