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
47 (defcustom elmo-split-rule nil
48 "Split rule for the command `elmo-split'.
49 The format of this variable is a list of RULEs which has form like:
50 \(CONDITION ACTION [continue]\)
52 The 1st element CONDITION is a sexp which consists of following.
54 1. Functions which accept arguments FIELD-NAME and VALUE.
55 FIELD-NAME is a symbol of the field name.
57 `equal' ... True if the field value equals to VALUE.
58 Case of the letters are ignored.
59 `match' ... True if the field value matches to VALUE.
60 VALUE can contain \\& and \\N which will substitute
61 from matching \\(\\) patterns in the previous VALUE.
62 `address-equal' ... True if one of the addresses in the field equals to
63 VALUE. Case of the letters are ignored.
64 `address-match' ... True if one of the addresses in the field matches to
66 VALUE can contain \\& and \\N which will substitute
67 from matching \\(\\) patterns in the previous VALUE.
69 2. Functions which accept an argument SIZE, SIZE is some number.
71 `<' ... True if the size of the message is less than SIZE.
72 `>' ... True if the size of the message is greater than SIZE.
74 3. Functions which accept any number of arguments.
76 `or' ... True if one of the argument returns true.
77 `and' ... True if all of the arguments return true.
79 `spam-p' ... True if contents of the message is guessed as spam.
80 Rest arguments are property list which consists
83 `:register' ... If this value is non-nil,
89 When a symbol is specified, it is evaluated.
91 The 2nd element ACTION is the name of the destination folder or some symbol.
92 If CONDITION is satisfied, the message is splitted according to this value.
94 If ACTION is a string, it will be considered as the name of destination folder.
95 Symbol `delete' means that the substance of the message will be removed. On the
96 other hand, symbol `noop' is used to do nothing and keep the substance of the
97 message as it is. Or, if some function is specified, it will be called.
99 When the 3rd element `continue' is specified as symbol, evaluating rules is
100 not stopped even when the condition is satisfied.
104 \(setq elmo-split-rule
105 ;; Messages from spammers are stored in `+junk'
106 '(((or (address-equal from \"i.am@spammer\")
107 (address-equal from \"dull-work@dull-boy\")
108 (address-equal from \"death-march@software\")
109 (address-equal from \"ares@aon.at\")
110 (address-equal from \"get-money@richman\"))
112 ;; Messages from mule mailing list are stored in `%mule'
113 ((equal x-ml-name \"mule\") \"%mule\")
114 ;; Messages from wanderlust mailing list are stored in `%wanderlust'
115 ;; and continue evaluating following rules.
116 ((equal x-ml-name \"wanderlust\") \"%wanderlust\" continue)
117 ;; Messages from DoCoMo user are stored in `+docomo-{username}'.
118 ((match from \"\\\\(.*\\\\)@docomo\\\\.ne\\\\.jp\")
120 ;; Unmatched mails go to `+inbox'.
125 (defcustom elmo-split-folder "%inbox"
126 "Target folder or list of folders for splitting."
127 :type '(choice (string :tag "folder name")
128 (repeat (string :tag "folder name")))
131 (defcustom elmo-split-default-action 'noop
132 "Default action for messages which pass all rules.
133 It can be some ACTION as in `elmo-split-rule'."
134 :type '(choice (const :tag "do not touch" noop)
135 (const :tag "delete" delete)
136 (string :tag "folder name")
137 (function :tag "function"))
140 (defcustom elmo-split-log-coding-system 'x-ctext
141 "A coding-system for writing log file."
145 (defcustom elmo-split-log-file "~/.elmo/split-log"
146 "The file name of the split log."
151 (defvar elmo-split-match-string-internal nil
152 "Internal variable for string matching. Don't touch this variable by hand.")
154 (defvar elmo-split-message-entity nil
155 "Buffer local variable to store mime-entity.")
156 (make-variable-buffer-local 'elmo-split-message-entity)
159 (defun elmo-split-or (buffer &rest args)
162 (if (elmo-split-eval buffer arg)
166 (defun elmo-split-and (buffer &rest args)
169 (unless (elmo-split-eval buffer arg)
173 (defun elmo-split-> (buffer size)
174 (> (buffer-size buffer) size))
176 (defun elmo-split-< (buffer size)
177 (< (buffer-size buffer) size))
179 (defun elmo-split-address-equal (buffer field value)
180 (with-current-buffer buffer
182 'std11-address-string
183 (std11-parse-addresses-string
184 (std11-field-body (symbol-name field)))))
188 (when (string-match (concat "^"
193 (setq addrs (cdr addrs)))
196 (defun elmo-split-address-match (buffer field value)
197 (with-current-buffer buffer
199 'std11-address-string
200 (std11-parse-addresses-string
201 (std11-field-body (symbol-name field)))))
204 (when (string-match value (car addrs))
205 (setq elmo-split-match-string-internal (car addrs)
208 (setq addrs (cdr addrs)))
211 (defun elmo-split-fetch-decoded-field (entity field-name)
212 (let ((sym (intern (capitalize field-name)))
213 (field-body (mime-entity-fetch-field entity field-name)))
215 (mime-decode-field-body field-body sym 'plain))))
217 (defun elmo-split-equal (buffer field value)
218 (with-current-buffer buffer
219 (let ((field-value (and
220 elmo-split-message-entity
221 (elmo-split-fetch-decoded-field
222 elmo-split-message-entity
223 (symbol-name field)))))
224 (equal field-value value))))
226 (defun elmo-split-spam-p (buffer &rest plist)
228 (elmo-spam-buffer-spam-p (elmo-spam-processor)
230 (plist-get plist :register)))
232 (defun elmo-split-match (buffer field value)
233 (with-current-buffer buffer
234 (let ((field-value (and elmo-split-message-entity
235 (elmo-split-fetch-decoded-field
236 elmo-split-message-entity
237 (symbol-name field)))))
239 (when (string-match value field-value)
240 (setq elmo-split-match-string-internal field-value))))))
242 (defun elmo-split-eval (buffer sexp)
245 (apply (intern (concat "elmo-split-" (symbol-name (car sexp))))
249 (std11-field-body sexp))
252 (defun elmo-split-log (message reharsal)
253 (with-current-buffer (get-buffer-create "*elmo-split*")
254 (goto-char (point-max))
255 (let ((start (point))
256 (coding-system-for-write elmo-split-log-coding-system))
260 (pop-to-buffer (current-buffer))
262 (write-region start (point) elmo-split-log-file t 'no-msg)))))
265 (defun elmo-split (&optional arg)
266 "Split messages in the `elmo-split-folder' according to `elmo-split-rule'.
267 If prefix argument ARG is specified, do a reharsal (no harm)."
269 (unless elmo-split-rule
270 (error "Split rule does not exist. Set `elmo-split-rule' first"))
271 (let ((folders (if (listp elmo-split-folder)
273 (list elmo-split-folder)))
277 (dolist (folder folders)
278 (setq ret (elmo-split-subr (elmo-make-folder folder) arg)
279 count (+ count (car ret))
280 fcount (+ fcount (cdr ret))))
281 (run-hooks 'elmo-split-hook)
286 "No message is splitted")
288 "1 message is splitted")
290 (format "%d messages are splitted" count)))
293 (format " (%d failure)." fcount))))
296 (defun elmo-split-subr (folder &optional reharsal)
297 (let ((elmo-inhibit-display-retrieval-progress t)
300 (default-rule `((t ,elmo-split-default-action)))
301 msgs action target-folder failure delete-substance
302 record-log log-string)
303 (message "Splitting...")
304 (elmo-folder-open-internal folder)
305 (setq msgs (elmo-folder-list-messages folder))
306 (elmo-progress-set 'elmo-split (length msgs) "Splitting...")
313 (elmo-message-fetch folder msg
314 (elmo-make-fetch-strategy 'entire)
315 nil (current-buffer) 'unread))
316 (run-hooks 'elmo-split-fetch-hook)
317 (setq elmo-split-message-entity (mime-parse-buffer))
319 (dolist (rule (append elmo-split-rule default-rule))
320 (setq elmo-split-match-string-internal nil)
321 (when (elmo-split-eval (current-buffer) (car rule))
322 (if (and (stringp (nth 1 rule))
323 elmo-split-match-string-internal)
324 (setq action (elmo-expand-newtext
326 elmo-split-match-string-internal))
327 (setq action (nth 1 rule)))
328 ;; 1. ACTION & DELETION
338 (setq target-folder (elmo-make-folder action))
339 (unless (elmo-folder-exists-p target-folder)
342 (elmo-folder-creatable-p target-folder)
345 "Folder %s does not exist, Create it? "
347 (elmo-folder-create target-folder)))
348 (elmo-folder-open-internal target-folder)
349 (elmo-folder-append-buffer target-folder)
350 (elmo-folder-close-internal target-folder))
351 (error (setq failure t)
356 (eq (nth 2 rule) 'continue))))
367 (error "Wrong action specified in elmo-split-rule")))
368 (when delete-substance
370 (elmo-folder-delete-messages folder (list msg)))))
376 (nth 1 (std11-extract-address-components
377 (or (std11-field-body "from") "")))
378 " " (or (std11-field-body "date") "") "\n"
380 (eword-decode-string (or (std11-field-body
386 (concat " Test: " action "\n"))
388 " Test: /dev/null\n")
390 " Test: do nothing\n")
392 (format " Test: function:%s\n"
393 (prin1-to-string action)))
395 " ERROR: wrong action specified\n"))
398 (concat " FAILED: " action "\n"))
400 (concat " Folder: " action "\n"))
408 ;; 3. CONTINUATION CHECK
409 (unless (eq (nth 2 rule) 'continue)
410 (throw 'terminate nil))))))
411 (elmo-progress-notify 'elmo-split)))
412 (elmo-folder-close-internal folder))
413 (elmo-progress-clear 'elmo-split))
414 (cons count fcount)))
417 (product-provide (provide 'elmo-split) (require 'elmo-version))
419 ;;; elmo-split.el ends here