scmail: Scheme によるメールフィルタ

最終更新日: 2003-03-21 (公開日: 2002-10-24)


scmail とは?

scmail は Scheme で書かれたメールフィルタです。メールが届い た瞬間の自動振り分けと、フォルダの中のメールの自動振り分けを 行えます。

新着情報

必要なもの

特長

インストール方法

scmail のインストールは次のように実行して行います。

% gzip -dc scmail-0.2.tar.gz | tar xvf - 
% cd scmail-0.2
Password: (rootのパスワードを入力)
# make
# make install

構成

scmail の設定

scmailrc.sample を ~/.scmailrc にコピーして設定ファイルを準 備します。

  ;; -*- scheme -*-
  (
   :log-file  "~/.scmail-log"
   :smtp-host "mailhost"      ; for mail forwarding
  
   ;; for MH
   :mailbox   "~/Mail"
   :inbox     "inbox"
   :mailbox-type  mh
  
   ;; for Maildir
   ;; :mailbox   "~/Maildir"
   ;; :inbox     ""
   ;; :mailbox-type  maildir
   )

scmail-deliver の使い方

scmail-deliver はメールを届いた瞬間 (受信時) に自動振り分けするツールです。 る。scmail-deliver 用の振り分け規則は ~/.scmailrc-deliver ファイルに定義します。 scmailrc-deliver.sample をコピーして準備してください。

SendmailPostfix などのメールサーバで は ~/.forward ファイルに次のような設定を加えると、メール受信 時に scmail-deliver による自動振り分けが行えるようになります。

| /usr/local/bin/scmail-deliver

POPサーバからのメールの受信に fetchmail を使っ ている場合は ~/.fetchmailrc の mda の設定に /usr/local/bin/scmail-deliver を指定します。

poll pop3.example.org
     protocol apop
     user satoru
     mda "/usr/local/bin/scmail-deliver"
     no mimedecode

scmail-refile の使い方

scmail-refile はフォルダの中のメールを自動振り分けするツール です。scmail-refile 用の振り分け規則は ~/.scmailrc-refile ファ イルに定義します。scmailrc-refile.sample をコピーして準備し てください。

scmail-refile はコマンドラインから次のように実行します。

% scmail-refile
refile: inbox/93 -> ml/enkai@coboler/57
refile: inbox/94 -> ml/linux-zaurus/218
refile: inbox/96 -> ml/komatsu-project/26
refile: inbox/98 -> ml/linux-zaurus/219
refile: inbox/99 -> ml/ming/42           

実行結果の最初の行は inbox の 93 番のメールが ml/enkai@coboler というフォルダに 57 番のメールとして振り分 けられた、という意味を示しています。~/.scmail-log には同様のレ ポートが時刻付きで記録されます。

% tail -5 ~/.scmail-log
2002-09-26T12:49:31: refile: inbox/93 -> ml/enkai@coboler/57  
2002-09-26T12:49:31: refile: inbox/94 -> ml/linux-zaurus/218  
2002-09-26T12:49:31: refile: inbox/98 -> ml/linux-zaurus/219
2002-09-26T12:49:31: refile: inbox/99 -> ml/ming/42

規則の書き方

サンプルを参考にして修正してください。

scmailrc-deliver.sample

  ;; -*- scheme -*-
  ;;
  ;; This sample file cointains Japanese EUC-JP characters.
  ;; Please remove unreadable rules if you don't want Japanese rules.
  ;;
  
  ;;
  ;; Copy all mails to "inbox-all" folder for backup.
  ;;
  (add-filter-rule!
   (lambda (config mail)
     (command-copy config mail "inbox-all")))
  
  (add-filter-rule!
   ;;
   ;; Forward a mail from "partner@example.net" to "mobile@example.com".
   ;;
   '(from
     ("partner@example.net" (forward "mobile@example.com")))
  
   ;;
   ;; Drop a mail writtein in unreadable charcodes to "junk".
   ;; For people who cannot read a mail written in Chinese and Korean.
   ;;
   '(content-type
     (#/gb2312|euc-kr|big5|gbk|ks_c_5601/i
               "junk"))
  
   ;;
   ;; Drop a Japanese junk mail to "junk" folder.
   ;;
   '(subject
     ((#/[未末]承[認諾]広告[**※]/ #/[!!]広告[!!]/)
      "junk")))
  
  ;;
  ;; Drop a mail containing junk words in Subject: and "text/html" in the body
  ;; to "junk" folder.
  ;;
  (define junk-words
    '("money" "adult" "sex" "viagra"))
  
  (use srfi-13) ; string-contains-ci
  (use srfi-1)  ; find
  (add-filter-rule!
   (lambda (config mail)
     (if (and (find (lambda (word)
                      (string-contains-ci (mail-query mail 'subject)
                                          word))
                    junk-words)
              (string-contains-ci (mail-query mail 'body)
                                  "text/html"))
         (command-refile config mail "junk")
         :next)))
  

scmailrc-refile.sample

  ;; -*- scheme -*-
  
  (add-filter-rule!
   ;;
   ;; Refile a mail from "foo@example.jp" to "from/foo" folder.
   ;;
   '(from
     ("foo@example.jp" "from/foo"))
  
   ;;
   ;; Refile a mail to "admin@example.com" to "admin" folder.
   ;;
   '(to/cc
     ("admin@example.com" "admin"))
  
   ;;
   ;; Refile a mail from a mailing list according to List-Id header.
   ;; e.g. "List-Id: <ml.example.jp>" => "ml/ml.example.jp"
   ;;
   '(list-id
     (#/<([-.\w]+)>/  "ml/\\1"))
  
   ;;
   ;; Refile a mail from a mailing list according to X-ML-Name header.
   ;; e.g. "X-ML-Name: foo-ml" => "ml/foo-ml"
   ;;
   '(x-ml-name
     (#/([-.\w]+)/  "ml/\\1"))
  
   ;;
   ;; Refile a mail from satoru@example.jp or satoru@example.org
   ;; to "from/satoru" folder.
   ;;
   '(from
     (("satoru@example.jp" "satoru@example.org") "from/satoru")))
  
  ;; Replace unsafe characters with `-' in match data.
  (set-match-data-replace-rule! '(#/[^a-zA-Z0-9_-]/ "-"))

scmailrc-kill.sample

  spammer@example.com
  nasty-guy@example.net
  virus-sender@example.org

FAQ

規則ファイルに文法エラーがあった場合は?

規則ファイルに括弧の閉じ忘れなどの文法エラーがあった場合、 空の規則ファイルとして扱われます。文法エラーのメッセージは ~/.scmail-log ファイルに記録されます。

日本語の処理がうまくいきません

サンプルの規則ファイルは、EUC-JP で書かれています。 Gauche の内部コードが EUC-JP の場合は問題 ありませんが、内部コードが UTF-8 の場合は、規則ファイルを UTF-8 に変換する必要があると思います。(試していませんが)

ダウンロード

GNU Lesser General Public License に従ったフリーソフトウェアとして公開します。 完全に無保証です。

参考文献

関連リンク集

メーリングリスト

QuickML でメーリングリストを作りました。 参加するには次のようなメールを送ってください。

Subject: 参加します
To: scmail@quickml.com
Cc: satoru@namazu.org

(参加メッセージ)

高林哲 (satoru@namazu.org)