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'.
44 ;; Avoid compile warnings
45 (defun-maybe elmo-spam-processor ())
46 (defun-maybe elmo-spam-buffer-spam-p (processor buffer)))
48 (defcustom elmo-split-rule nil
49 "Split rule for the command `elmo-split'.
50 The format of this variable is a list of RULEs which has form like:
51 \(CONDITION ACTION [continue]\)
53 The 1st element CONDITION is a sexp which consists of following.
55 1. Functions which accept arguments FIELD-NAME and VALUE.
56 FIELD-NAME is a symbol of the field name.
58 `equal' ... True if the field value equals to VALUE.
59 Case of the letters are ignored.
60 `match' ... True if the field value matches to VALUE.
61 VALUE can contain \\& and \\N which will substitute
62 from matching \\(\\) patterns in the previous VALUE.
63 `address-equal' ... True if one of the addresses in the field equals to
64 VALUE. Case of the letters are ignored.
65 `address-match' ... True if one of the addresses in the field matches to
67 VALUE can contain \\& and \\N which will substitute
68 from matching \\(\\) patterns in the previous VALUE.
70 2. Functions which accept an argument SIZE, SIZE is some number.
72 `<' ... True if the size of the message is less than SIZE.
73 `>' ... True if the size of the message is greater than SIZE.
75 3. Functions which accept any number of arguments.
77 `or' ... True if one of the argument returns true.
78 `and' ... True if all of the arguments return true.
80 4. Functions which accept not argument.
82 `spam-p' ... True if contents of the message is guessed as spam.
86 When a symbol is specified, it is evaluated.
88 The 2nd element ACTION is the name of the destination folder or some symbol.
89 If CONDITION is satisfied, the message is splitted according to this value.
91 If ACTION is a string, it will be considered as the name of destination folder.
92 Symbol `delete' means that the substance of the message will be removed. On the
93 other hand, symbol `noop' is used to do nothing and keep the substance of the
94 message as it is. Or, if some function is specified, it will be called.
96 When the 3rd element `continue' is specified as symbol, evaluating rules is
97 not stopped even when the condition is satisfied.
101 \(setq elmo-split-rule
102 ;; Messages from spammers are stored in `+junk'
103 '(((or (address-equal from \"i.am@spammer\")
104 (address-equal from \"dull-work@dull-boy\")
105 (address-equal from \"death-march@software\")
106 (address-equal from \"ares@aon.at\")
107 (address-equal from \"get-money@richman\"))
109 ;; Messages from mule mailing list are stored in `%mule'
110 ((equal x-ml-name \"mule\") \"%mule\")
111 ;; Messages from wanderlust mailing list are stored in `%wanderlust'
112 ;; and continue evaluating following rules.
113 ((equal x-ml-name \"wanderlust\") \"%wanderlust\" continue)
114 ;; Messages from DoCoMo user are stored in `+docomo-{username}'.
115 ((match from \"\\\\(.*\\\\)@docomo\\\\.ne\\\\.jp\")
117 ;; Unmatched mails go to `+inbox'.
122 (defcustom elmo-split-folder "%inbox"
123 "Target folder or list of folders for splitting."
124 :type '(choice (string :tag "folder name")
125 (repeat (string :tag "folder name")))
128 (defcustom elmo-split-default-action 'noop
129 "Default action for messages which pass all rules.
130 It can be some ACTION as in `elmo-split-rule'."
131 :type '(choice (const :tag "do not touch" noop)
132 (const :tag "delete" delete)
133 (string :tag "folder name")
134 (function :tag "function"))
137 (defcustom elmo-split-log-coding-system 'x-ctext
138 "A coding-system for writing log file."
142 (defcustom elmo-split-log-file "~/.elmo/split-log"
143 "The file name of the split log."
148 (defvar elmo-split-match-string-internal nil
149 "Internal variable for string matching. Don't touch this variable by hand.")
151 (defvar elmo-split-message-entity nil
152 "Buffer local variable to store mime-entity.")
153 (make-variable-buffer-local 'elmo-split-message-entity)
156 (defun elmo-split-or (buffer &rest args)
159 (if (elmo-split-eval buffer arg)
163 (defun elmo-split-and (buffer &rest args)
166 (unless (elmo-split-eval buffer arg)
170 (defun elmo-split-> (buffer size)
171 (> (buffer-size buffer) size))
173 (defun elmo-split-< (buffer size)
174 (< (buffer-size buffer) size))
176 (defun elmo-split-address-equal (buffer field value)
177 (with-current-buffer buffer
179 'std11-address-string
180 (std11-parse-addresses-string
181 (std11-field-body (symbol-name field)))))
185 (when (string-match (concat "^"
190 (setq addrs (cdr addrs)))
193 (defun elmo-split-address-match (buffer field value)
194 (with-current-buffer buffer
196 'std11-address-string
197 (std11-parse-addresses-string
198 (std11-field-body (symbol-name field)))))
201 (when (string-match value (car addrs))
202 (setq elmo-split-match-string-internal (car addrs)
205 (setq addrs (cdr addrs)))
208 (defun elmo-split-fetch-decoded-field (entity field-name)
209 (let ((sym (intern (capitalize field-name)))
210 (field-body (mime-entity-fetch-field entity field-name)))
212 (mime-decode-field-body field-body sym 'plain))))
214 (defun elmo-split-equal (buffer field value)
215 (with-current-buffer buffer
216 (let ((field-value (and
217 elmo-split-message-entity
218 (elmo-split-fetch-decoded-field
219 elmo-split-message-entity
220 (symbol-name field)))))
221 (equal field-value value))))
223 (defun elmo-split-spam-p (buffer)
225 (elmo-spam-buffer-spam-p (elmo-spam-processor) buffer))
227 (defun elmo-split-match (buffer field value)
228 (with-current-buffer buffer
229 (let ((field-value (and elmo-split-message-entity
230 (elmo-split-fetch-decoded-field
231 elmo-split-message-entity
232 (symbol-name field)))))
234 (when (string-match value field-value)
235 (setq elmo-split-match-string-internal field-value))))))
237 (defun elmo-split-eval (buffer sexp)
240 (apply (intern (concat "elmo-split-" (symbol-name (car sexp))))
244 (std11-field-body sexp))
247 (defun elmo-split-log (message reharsal)
248 (with-current-buffer (get-buffer-create "*elmo-split*")
249 (goto-char (point-max))
250 (let ((start (point))
251 (coding-system-for-write elmo-split-log-coding-system))
255 (pop-to-buffer (current-buffer))
257 (write-region start (point) elmo-split-log-file t 'no-msg)))))
260 (defun elmo-split (&optional arg)
261 "Split messages in the `elmo-split-folder' according to `elmo-split-rule'.
262 If prefix argument ARG is specified, do a reharsal (no harm)."
264 (unless elmo-split-rule
265 (error "Split rule does not exist. Set `elmo-split-rule' first"))
266 (let ((folders (if (listp elmo-split-folder)
268 (list elmo-split-folder)))
272 (dolist (folder folders)
273 (setq ret (elmo-split-subr (elmo-make-folder folder) arg)
274 count (+ count (car ret))
275 fcount (+ fcount (cdr ret))))
276 (run-hooks 'elmo-split-hook)
281 "No message is splitted")
283 "1 message is splitted")
285 (format "%d messages are splitted" count)))
288 (format " (%d failure)." fcount))))))
290 (defun elmo-split-subr (folder &optional reharsal)
291 (let ((elmo-inhibit-display-retrieval-progress t)
294 (default-rule `((t ,elmo-split-default-action)))
295 msgs action target-folder failure delete-substance
296 record-log log-string)
297 (message "Splitting...")
298 (elmo-folder-open-internal folder)
299 (setq msgs (elmo-folder-list-messages folder))
300 (elmo-progress-set 'elmo-split (length msgs) "Splitting...")
307 (elmo-message-fetch folder msg
308 (elmo-make-fetch-strategy 'entire)
309 nil (current-buffer) 'unread))
310 (run-hooks 'elmo-split-fetch-hook)
311 (setq elmo-split-message-entity (mime-parse-buffer))
313 (dolist (rule (append elmo-split-rule default-rule))
314 (setq elmo-split-match-string-internal nil)
315 (when (elmo-split-eval (current-buffer) (car rule))
316 (if (and (stringp (nth 1 rule))
317 elmo-split-match-string-internal)
318 (setq action (elmo-expand-newtext
320 elmo-split-match-string-internal))
321 (setq action (nth 1 rule)))
322 ;; 1. ACTION & DELETION
332 (setq target-folder (elmo-make-folder action))
333 (unless (elmo-folder-exists-p target-folder)
336 (elmo-folder-creatable-p target-folder)
339 "Folder %s does not exist, Create it? "
341 (elmo-folder-create target-folder)))
342 (elmo-folder-open-internal target-folder)
343 (elmo-folder-append-buffer target-folder)
344 (elmo-folder-close-internal target-folder))
345 (error (setq failure t)
350 (eq (nth 2 rule) 'continue))))
361 (error "Wrong action specified in elmo-split-rule")))
362 (when delete-substance
364 (elmo-folder-delete-messages folder (list msg)))))
370 (nth 1 (std11-extract-address-components
371 (or (std11-field-body "from") "")))
372 " " (or (std11-field-body "date") "") "\n"
374 (eword-decode-string (or (std11-field-body
380 (concat " Test: " action "\n"))
382 " Test: /dev/null\n")
384 " Test: do nothing\n")
386 (format " Test: function:%s\n"
387 (prin1-to-string action)))
389 " ERROR: wrong action specified\n"))
392 (concat " FAILED: " action "\n"))
394 (concat " Folder: " action "\n"))
402 ;; 3. CONTINUATION CHECK
403 (unless (eq (nth 2 rule) 'continue)
404 (throw 'terminate nil))))))
405 (elmo-progress-notify 'elmo-split)))
406 (elmo-folder-close-internal folder))
407 (elmo-progress-clear 'elmo-split))
408 (cons count fcount)))
411 (product-provide (provide 'elmo-split) (require 'elmo-version))
413 ;;; elmo-split.el ends here