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 4. Functions which accept not argument.
81 `spam-p' ... True if contents of the message is guessed as spam.
85 When a symbol is specified, it is evaluated.
87 The 2nd element ACTION is the name of the destination folder or some symbol.
88 If CONDITION is satisfied, the message is splitted according to this value.
90 If ACTION is a string, it will be considered as the name of destination folder.
91 Symbol `delete' means that the substance of the message will be removed. On the
92 other hand, symbol `noop' is used to do nothing and keep the substance of the
93 message as it is. Or, if some function is specified, it will be called.
95 When the 3rd element `continue' is specified as symbol, evaluating rules is
96 not stopped even when the condition is satisfied.
100 \(setq elmo-split-rule
101 ;; Messages from spammers are stored in `+junk'
102 '(((or (address-equal from \"i.am@spammer\")
103 (address-equal from \"dull-work@dull-boy\")
104 (address-equal from \"death-march@software\")
105 (address-equal from \"ares@aon.at\")
106 (address-equal from \"get-money@richman\"))
108 ;; Messages from mule mailing list are stored in `%mule'
109 ((equal x-ml-name \"mule\") \"%mule\")
110 ;; Messages from wanderlust mailing list are stored in `%wanderlust'
111 ;; and continue evaluating following rules.
112 ((equal x-ml-name \"wanderlust\") \"%wanderlust\" continue)
113 ;; Messages from DoCoMo user are stored in `+docomo-{username}'.
114 ((match from \"\\\\(.*\\\\)@docomo\\\\.ne\\\\.jp\")
116 ;; Unmatched mails go to `+inbox'.
121 (defcustom elmo-split-folder "%inbox"
122 "Target folder or list of folders for splitting."
123 :type '(choice (string :tag "folder name")
124 (repeat (string :tag "folder name")))
127 (defcustom elmo-split-default-action 'noop
128 "Default action for messages which pass all rules.
129 It can be some ACTION as in `elmo-split-rule'."
130 :type '(choice (const :tag "do not touch" noop)
131 (const :tag "delete" delete)
132 (string :tag "folder name")
133 (function :tag "function"))
136 (defcustom elmo-split-log-coding-system 'x-ctext
137 "A coding-system for writing log file."
141 (defcustom elmo-split-log-file "~/.elmo/split-log"
142 "The file name of the split log."
147 (defvar elmo-split-match-string-internal nil
148 "Internal variable for string matching. Don't touch this variable by hand.")
150 (defvar elmo-split-message-entity nil
151 "Buffer local variable to store mime-entity.")
152 (make-variable-buffer-local 'elmo-split-message-entity)
155 (defun elmo-split-or (buffer &rest args)
158 (if (elmo-split-eval buffer arg)
162 (defun elmo-split-and (buffer &rest args)
165 (unless (elmo-split-eval buffer arg)
169 (defun elmo-split-> (buffer size)
170 (> (buffer-size buffer) size))
172 (defun elmo-split-< (buffer size)
173 (< (buffer-size buffer) size))
175 (defun elmo-split-address-equal (buffer field value)
176 (with-current-buffer buffer
178 'std11-address-string
179 (std11-parse-addresses-string
180 (std11-field-body (symbol-name field)))))
184 (when (string-match (concat "^"
189 (setq addrs (cdr addrs)))
192 (defun elmo-split-address-match (buffer field value)
193 (with-current-buffer buffer
195 'std11-address-string
196 (std11-parse-addresses-string
197 (std11-field-body (symbol-name field)))))
200 (when (string-match value (car addrs))
201 (setq elmo-split-match-string-internal (car addrs)
204 (setq addrs (cdr addrs)))
207 (defun elmo-split-fetch-decoded-field (entity field-name)
208 (let ((sym (intern (capitalize field-name)))
209 (field-body (mime-entity-fetch-field entity field-name)))
211 (mime-decode-field-body field-body sym 'plain))))
213 (defun elmo-split-equal (buffer field value)
214 (with-current-buffer buffer
215 (let ((field-value (and
216 elmo-split-message-entity
217 (elmo-split-fetch-decoded-field
218 elmo-split-message-entity
219 (symbol-name field)))))
220 (equal field-value value))))
222 (defun elmo-split-spam-p (buffer &rest plist)
224 (elmo-spam-buffer-spam-p (elmo-spam-processor)
226 (plist-get plist :register)))
228 (defun elmo-split-match (buffer field value)
229 (with-current-buffer buffer
230 (let ((field-value (and elmo-split-message-entity
231 (elmo-split-fetch-decoded-field
232 elmo-split-message-entity
233 (symbol-name field)))))
235 (when (string-match value field-value)
236 (setq elmo-split-match-string-internal field-value))))))
238 (defun elmo-split-eval (buffer sexp)
241 (apply (intern (concat "elmo-split-" (symbol-name (car sexp))))
245 (std11-field-body sexp))
248 (defun elmo-split-log (message reharsal)
249 (with-current-buffer (get-buffer-create "*elmo-split*")
250 (goto-char (point-max))
251 (let ((start (point))
252 (coding-system-for-write elmo-split-log-coding-system))
256 (pop-to-buffer (current-buffer))
258 (write-region start (point) elmo-split-log-file t 'no-msg)))))
261 (defun elmo-split (&optional arg)
262 "Split messages in the `elmo-split-folder' according to `elmo-split-rule'.
263 If prefix argument ARG is specified, do a reharsal (no harm)."
265 (unless elmo-split-rule
266 (error "Split rule does not exist. Set `elmo-split-rule' first"))
267 (let ((folders (if (listp elmo-split-folder)
269 (list elmo-split-folder)))
273 (dolist (folder folders)
274 (setq ret (elmo-split-subr (elmo-make-folder folder) arg)
275 count (+ count (car ret))
276 fcount (+ fcount (cdr ret))))
277 (run-hooks 'elmo-split-hook)
282 "No message is splitted")
284 "1 message is splitted")
286 (format "%d messages are splitted" count)))
289 (format " (%d failure)." fcount))))))
291 (defun elmo-split-subr (folder &optional reharsal)
292 (let ((elmo-inhibit-display-retrieval-progress t)
295 (default-rule `((t ,elmo-split-default-action)))
296 msgs action target-folder failure delete-substance
297 record-log log-string)
298 (message "Splitting...")
299 (elmo-folder-open-internal folder)
300 (setq msgs (elmo-folder-list-messages folder))
301 (elmo-progress-set 'elmo-split (length msgs) "Splitting...")
308 (elmo-message-fetch folder msg
309 (elmo-make-fetch-strategy 'entire)
310 nil (current-buffer) 'unread))
311 (run-hooks 'elmo-split-fetch-hook)
312 (setq elmo-split-message-entity (mime-parse-buffer))
314 (dolist (rule (append elmo-split-rule default-rule))
315 (setq elmo-split-match-string-internal nil)
316 (when (elmo-split-eval (current-buffer) (car rule))
317 (if (and (stringp (nth 1 rule))
318 elmo-split-match-string-internal)
319 (setq action (elmo-expand-newtext
321 elmo-split-match-string-internal))
322 (setq action (nth 1 rule)))
323 ;; 1. ACTION & DELETION
333 (setq target-folder (elmo-make-folder action))
334 (unless (elmo-folder-exists-p target-folder)
337 (elmo-folder-creatable-p target-folder)
340 "Folder %s does not exist, Create it? "
342 (elmo-folder-create target-folder)))
343 (elmo-folder-open-internal target-folder)
344 (elmo-folder-append-buffer target-folder)
345 (elmo-folder-close-internal target-folder))
346 (error (setq failure t)
351 (eq (nth 2 rule) 'continue))))
362 (error "Wrong action specified in elmo-split-rule")))
363 (when delete-substance
365 (elmo-folder-delete-messages folder (list msg)))))
371 (nth 1 (std11-extract-address-components
372 (or (std11-field-body "from") "")))
373 " " (or (std11-field-body "date") "") "\n"
375 (eword-decode-string (or (std11-field-body
381 (concat " Test: " action "\n"))
383 " Test: /dev/null\n")
385 " Test: do nothing\n")
387 (format " Test: function:%s\n"
388 (prin1-to-string action)))
390 " ERROR: wrong action specified\n"))
393 (concat " FAILED: " action "\n"))
395 (concat " Folder: " action "\n"))
403 ;; 3. CONTINUATION CHECK
404 (unless (eq (nth 2 rule) 'continue)
405 (throw 'terminate nil))))))
406 (elmo-progress-notify 'elmo-split)))
407 (elmo-folder-close-internal folder))
408 (elmo-progress-clear 'elmo-split))
409 (cons count fcount)))
412 (product-provide (provide 'elmo-split) (require 'elmo-version))
414 ;;; elmo-split.el ends here