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'.
41 (eval-when-compile (require 'cl
))
45 ;; Avoid compile warnings
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 FIELD-NAME can be a list of field names, return true if any of the fields
71 satisfy the condition.
73 2. Functions which accept an argument SIZE, SIZE is some number.
75 `<' ... True if the size of the message is less than SIZE.
76 `>' ... True if the size of the message is greater than SIZE.
78 3. Functions which accept any number of arguments.
80 `or' ... True if one of the argument returns true.
81 `and' ... True if all of the arguments return true.
83 `spam-p' ... True if contents of the message is guessed as spam.
84 Rest arguments are property list which consists
87 `:register' ... If this value is non-nil,
93 When a symbol is specified, it is evaluated.
95 The 2nd element ACTION is the name of the destination folder or some symbol.
96 If CONDITION is satisfied, the message is splitted according to this value.
98 If ACTION is a string, it will be considered as the name of destination folder.
99 Symbol `delete' means that the substance of the message will be removed. On the
100 other hand, symbol `noop' is used to do nothing and keep the substance of the
101 message as it is. Or, if some function is specified, it will be called.
103 When the 3rd element `continue' is specified as symbol, evaluating rules is
104 not stopped even when the condition is satisfied.
108 \(setq elmo-split-rule
109 ;; Messages from spammers are stored in `+junk'
110 '(((or (address-equal from \"i.am@spammer\")
111 (address-equal from \"dull-work@dull-boy\")
112 (address-equal from \"death-march@software\")
113 (address-equal from \"ares@aon.at\")
114 (address-equal from \"get-money@richman\"))
116 ;; Messages from mule mailing list are stored in `%mule'
117 ((equal x-ml-name \"mule\") \"%mule\")
118 ;; Messages from wanderlust mailing list are stored in `%wanderlust'
119 ;; and continue evaluating following rules.
120 ((equal x-ml-name \"wanderlust\") \"%wanderlust\" continue)
121 ;; Messages from DoCoMo user are stored in `+docomo-{username}'.
122 ((match from \"\\\\(.*\\\\)@docomo\\\\.ne\\\\.jp\")
124 ;; Unmatched mails go to `+inbox'.
129 (defcustom elmo-split-folder
"%inbox"
130 "Target folder or list of folders for splitting."
131 :type
'(choice (string :tag
"folder name")
132 (repeat (string :tag
"folder name")))
135 (defcustom elmo-split-default-action
'noop
136 "Default action for messages which pass all rules.
137 It can be some ACTION as in `elmo-split-rule'."
138 :type
'(choice (const :tag
"do not touch" noop
)
139 (const :tag
"delete" delete
)
140 (string :tag
"folder name")
141 (function :tag
"function"))
144 (defcustom elmo-split-log-coding-system
'x-ctext
145 "A coding-system for writing log file."
149 (defcustom elmo-split-log-file
"~/.elmo/split-log"
150 "The file name of the split log."
155 (defvar elmo-split-match-string-internal nil
156 "Internal variable for string matching. Don't touch this variable by hand.")
158 (defvar elmo-split-message-entity nil
159 "Buffer local variable to store mime-entity.")
160 (make-variable-buffer-local 'elmo-split-message-entity
)
163 (defun elmo-split-or (buffer &rest args
)
166 (if (elmo-split-eval buffer arg
)
170 (defun elmo-split-and (buffer &rest args
)
173 (unless (elmo-split-eval buffer arg
)
177 (defun elmo-split-> (buffer size
)
178 (> (buffer-size buffer
) size
))
180 (defun elmo-split-< (buffer size
)
181 (< (buffer-size buffer
) size
))
183 (defun elmo-split-address-equal (buffer field-or-fields value
)
184 (with-current-buffer buffer
186 (dolist (field (if (listp field-or-fields
)
188 (list field-or-fields
)))
190 'std11-address-string
191 (std11-parse-addresses-string
192 (std11-field-body (symbol-name field
)))))
193 (case-fold-search t
))
195 (when (string-match (concat "^"
200 (setq addrs
(cdr addrs
)))))
203 (defun elmo-split-address-match (buffer field-or-fields value
)
204 (with-current-buffer buffer
206 (dolist (field (if (listp field-or-fields
)
208 (list field-or-fields
)))
210 'std11-address-string
211 (std11-parse-addresses-string
212 (std11-field-body (symbol-name field
))))))
214 (when (string-match value
(car addrs
))
215 (setq elmo-split-match-string-internal
(car addrs
)
218 (setq addrs
(cdr addrs
)))))
221 (defun elmo-split-fetch-decoded-field (entity field-name
)
222 (let ((sym (intern (capitalize field-name
)))
223 (field-body (mime-entity-fetch-field entity field-name
)))
225 (mime-decode-field-body field-body sym
'plain
))))
227 (defun elmo-split-equal (buffer field-or-fields value
)
228 (with-current-buffer buffer
230 (dolist (field (if (listp field-or-fields
)
232 (list field-or-fields
)))
233 (let ((field-value (and
234 elmo-split-message-entity
235 (elmo-split-fetch-decoded-field
236 elmo-split-message-entity
237 (symbol-name field
)))))
238 (setq result
(or result
239 (equal field-value value
)))))
242 (defun elmo-split-spam-p (buffer &rest plist
)
244 (elmo-spam-buffer-spam-p (elmo-spam-processor)
246 (plist-get plist
:register
)))
248 (defun elmo-split-match (buffer field-or-fields value
)
249 (with-current-buffer buffer
251 (dolist (field (if (listp field-or-fields
)
253 (list field-or-fields
)))
254 (let ((field-value (and elmo-split-message-entity
255 (elmo-split-fetch-decoded-field
256 elmo-split-message-entity
257 (symbol-name field
)))))
259 (when (string-match value field-value
)
261 (setq elmo-split-match-string-internal field-value
)))))
264 (defun elmo-split-eval (buffer sexp
)
267 (apply (intern (concat "elmo-split-" (symbol-name (car sexp
))))
271 (std11-field-body sexp
))
274 (defun elmo-split-log (message reharsal
)
275 (with-current-buffer (get-buffer-create "*elmo-split*")
276 (goto-char (point-max))
277 (let ((start (point))
278 (coding-system-for-write elmo-split-log-coding-system
))
282 (pop-to-buffer (current-buffer))
284 (write-region start
(point) elmo-split-log-file t
'no-msg
)))))
287 (defun elmo-split (&optional arg
)
288 "Split messages in the `elmo-split-folder' according to `elmo-split-rule'.
289 If prefix argument ARG is specified, do a reharsal (no harm)."
291 (unless elmo-split-rule
292 (error "Split rule does not exist. Set `elmo-split-rule' first"))
293 (let ((folders (if (listp elmo-split-folder
)
295 (list elmo-split-folder
)))
299 (dolist (folder folders
)
300 (setq ret
(elmo-split-subr (elmo-get-folder folder
) arg
)
301 count
(+ count
(car ret
))
302 fcount
(+ fcount
(cdr ret
))))
303 (run-hooks 'elmo-split-hook
)
308 "No message is splitted")
310 "1 message is splitted")
312 (format "%d messages are splitted" count
)))
315 (format " (%d failure)." fcount
))))
318 (defun elmo-split-subr (folder &optional reharsal
)
321 (default-rule `((t ,elmo-split-default-action
)))
322 msgs action target-folder failure delete-substance
323 record-log log-string flags
)
324 (message "Splitting...")
325 (elmo-folder-open-internal folder
)
326 (setq msgs
(elmo-folder-list-messages folder
))
327 (elmo-with-progress-display (elmo-split (length msgs
)) "Splitting messages"
330 (set-buffer-multibyte nil
)
334 (elmo-message-fetch folder msg
335 (elmo-make-fetch-strategy 'entire
)
337 (run-hooks 'elmo-split-fetch-hook
)
338 (setq elmo-split-message-entity
(mime-parse-buffer))
339 (setq flags
(elmo-message-flags-for-append folder msg
))
341 (dolist (rule (append elmo-split-rule default-rule
))
342 (setq elmo-split-match-string-internal nil
)
343 (when (elmo-split-eval (current-buffer) (car rule
))
344 (if (and (stringp (nth 1 rule
))
345 elmo-split-match-string-internal
)
346 (setq action
(elmo-expand-newtext
348 elmo-split-match-string-internal
))
349 (setq action
(nth 1 rule
)))
350 ;; 1. ACTION & DELETION
360 (setq target-folder
(elmo-get-folder action
))
361 (unless (elmo-folder-exists-p target-folder
)
364 (elmo-folder-creatable-p target-folder
)
367 "Folder %s does not exist, Create it? "
369 (elmo-folder-create target-folder
)))
370 (elmo-folder-open-internal target-folder
)
372 (elmo-folder-append-buffer
375 (elmo-folder-close-internal target-folder
))
376 (error (setq failure t
)
381 (eq (nth 2 rule
) 'continue
))))
392 (error "Wrong action specified in elmo-split-rule")))
393 (when delete-substance
395 (elmo-folder-delete-messages folder
(list msg
)))))
401 (nth 1 (std11-extract-address-components
402 (or (std11-field-body "from") "")))
403 " " (or (std11-field-body "date") "") "\n"
405 (eword-decode-string (or (std11-field-body
411 (concat " Test: " action
"\n"))
413 " Test: /dev/null\n")
415 " Test: do nothing\n")
417 (format " Test: function:%s\n"
418 (prin1-to-string action
)))
420 " ERROR: wrong action specified\n"))
423 (concat " FAILED: " action
"\n"))
425 (concat " Folder: " action
"\n"))
433 ;; 3. CONTINUATION CHECK
434 (unless (eq (nth 2 rule
) 'continue
)
435 (throw 'terminate nil
))))))
436 (elmo-progress-notify 'elmo-split
)))
437 (elmo-folder-close-internal folder
)))
438 (cons count fcount
)))
441 (product-provide (provide 'elmo-split
) (require 'elmo-version
))
443 ;;; elmo-split.el ends here