1 ;;; spamassassin-mode.el --- spamassassin rules editing commands for Emacs
3 ;;; Copyright (C) 2004 Eugene Morozov <eugene@renice.org>
5 ;; Author: Eugene Morozov <eugene@renice.org>
6 ;; Maintainer: Eugene Morozov <eugene@renice.org>
7 ;; Keywords: spamassassin
9 ;; This program is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2 of the License, or
12 ;; (at your option) any later version.
14 ;; This program is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with this program; if not, write to the Free Software
21 ;; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 ;; This mode is largerly based on m4-mode. It is useable in its
26 ;; current state but font-lock code is far from perfect.
30 ;; * Improve font-lock code (highlite rule names in meta rules,
31 ;; deal with quotes somehow
32 ;; * Make last argument for spamassassin-assassin-file be the default
33 ;; argument for the next invocation
34 ;; * Strip existing SpamAssassin markup in spamassassin-assassin-file
35 ;; before invoking SpamAssassin
37 (defvar spamassassin-mode-syntax-table nil
38 "Syntax table in use in spamassassin-mode buffers.")
40 (define-skeleton spamassassin-body-rule-skeleton
41 "Inserts a SpamAssassin body rule skeleton."
45 "describe " str " Description\n")
47 (define-skeleton spamassassin-header-rule-skeleton
48 "Inserts a SpamAssassin header rule skeleton."
50 "header " str " Header =~ //i\n"
52 "describe " str " Description\n")
54 (define-skeleton spamassassin-meta-rule-skeleton
55 "Inserts a SpamAssassin meta rule skeleton."
59 "describe " str " Description\n")
61 (defvar spamassassin-mode-abbrev-table
62 (let ((ac abbrevs-changed))
63 (define-abbrev-table 'spamassassin-mode-abbrev-table ())
64 (define-abbrev spamassassin-mode-abbrev-table "bdy" ""
65 'spamassassin-body-rule-skeleton)
66 (define-abbrev spamassassin-mode-abbrev-table "hdr" ""
67 'spamassassin-header-rule-skeleton)
68 (define-abbrev spamassassin-mode-abbrev-table "mta" ""
69 'spamassassin-meta-rule-skeleton)
70 (setq abbrevs-changed ac)
71 spamassassin-mode-abbrev-table)
72 "Abbrev table for use in spamassassin-mode buffers.")
74 (if spamassassin-mode-syntax-table
76 (setq spamassassin-mode-syntax-table (make-syntax-table))
77 (modify-syntax-entry ?# "<" spamassassin-mode-syntax-table)
78 (modify-syntax-entry ?\n ">" spamassassin-mode-syntax-table)
79 (modify-syntax-entry ?\f ">" spamassassin-mode-syntax-table)
80 ;; Quotes doesn't mean something special in spamassassin configuration
81 ;; and treating them as punctuation simplifies my font-lock efforts
82 (modify-syntax-entry ?\" "." spamassassin-mode-syntax-table)
83 (modify-syntax-entry ?_ "_" spamassassin-mode-syntax-table))
85 (defconst spamassassin-font-lock-keywords
88 '("^[ \t]*\\(rawbody\\|body\\|uri\\|full\\)\\>[ \t]*\\([A-Za-z0-9_]+\\)?[ \t]*\\(/\\(\\(?:\\\\.\\|[^/]\\)*\\)/\\)?\\(\\sw+\\)?"
89 (1 font-lock-keyword-face) (2 font-lock-function-name-face nil t) (3 font-lock-constant-face nil t) (4 font-lock-string-face t t) (5 font-lock-type-face nil t))
90 '("^[ \t]*\\(header\\)\\>[ \t]*\\([A-Za-z0-9_]+\\)?[ \t]*\\([-A-Za-z0-9_]+\\)?[ \t]*\\(?:=~\\)?[ \t]*\\(/\\(\\(?:\\\\.\\|[^/]\\)*\\)/\\)?\\(\\sw+\\)?"
91 (1 font-lock-keyword-face) (2 font-lock-function-name-face nil t) (3 font-lock-type-face nil t) (4 font-lock-constant-face nil t) (5 font-lock-string-face t t) (6 font-lock-type-face nil t))
92 '("^[ \t]*\\(uridnsbl\\|describe\\|score\\|meta\\)\\>[ \t]*\\([A-Za-z0-9_]+\\)?"
93 (1 font-lock-keyword-face) (2 font-lock-function-name-face nil t))
94 '("^[ \t]*\\(tflags\\)[ \t]*\\([A-Za-z0-9_]+\\)?[ \t]*\\(\\(\\(net\\|nice\\|userconf\\|learn\\|noautolearn\\)[ \t]*\\)*\\)"
95 (1 font-lock-keyword-face) (2 font-lock-function-name-face nil t) (3 font-lock-builtin-face nil t))
96 '("^[ \t]*\\(test\\)[ \t]*\\([A-Za-z0-9_]+\\)?[ \t]*\\(ok\\|fail\\)?"
97 (1 font-lock-keyword-face) (2 font-lock-function-name-face) (3 font-lock-constant-face))
98 (cons (regexp-opt '("require_version" "version_tag" "def_whitelist_from_rcvd" "whitelist_from_rcvd" "unwhitelist_from" "unwhitelist_from_rcvd" "whitelist_from" "blacklist_from" "unblacklist_from" "whitelist_to" "more_spam_to" "all_spam_to" "blacklist_to" "required_score" "required_hits" "rewrite_header" "fold_headers" "add_header" "remove_header" "clear_headers" "report_safe_copy_headers" "report_safe" "report_charset" "report" "clear_report_template" "report_contact" "report_hostname" "unsafe_report" "clear_unsafe_report_template" "spamtrap" "clear_spamtrap_template" "ok_languages" "ok_locales" "use_dcc" "dcc_timeout" "dcc_body_max" "dcc_fuz1_max" "dcc_fuz2_max" "use_pyzor" "pyzor_timeout" "pyzor_max" "clear_trusted_networks" "clear_internal_networks" "trusted_networks" "internal_networks" "use_razor2" "razor_timeout" "use_bayes" "skip_rbl_checks" "rbl_timeout" "check_mx_attempts" "check_mx_delay" "bayes_ignore_from" "bayes_ignore_to" "dns_available" "use_hashcash" "hashcash_accept" "hashcash_doublespend_path" "hashcash_doublespend_file_mode" "auto_whitelist_factor" "auto_whitelist_db_modules" "bayes_auto_learn" "bayes_auto_threshold_nonspam" "bayes_auto_threshold_spam" "bayes_ignore_header" "bayes_min_ham_num" "bayes_min_spam_num" "bayes_learn_during_report" "bayes_sql_override_username" "pyzor_options" "allow_user_rules" "razor_config" "pyzor_path" "dcc_home" "dcc_dccifd_path" "dcc_path" "dcc_options" "use_auto_whitelist" "auto_whitelist_factory" "auto_whitelist_path" "auto_whitelist_file_mode" "bayes_path" "bayes_file_mode" "bayes_use_hapaxes" "bayes_use_chi2_combining" "bayes_journal_max_size" "bayes_expiry_max_db_size" "bayes_auto_expire" "bayes_learn_to_journal" "bayes_store_module" "bayes_sql_dsn" "bayes_sql_username" "bayes_sql_password" "user_awl_dsn" "user_awl_sql_username" "user_awl_sql_password" "user_awl_sql_table" "user_scores_dsn" "user_scores_ldap_username" "user_scores_ldap_password" "user_scores_sql_username" "user_scores_sql_password" "loadplugin" "ifplugin" "endif" "lang" "uridnsbl_timeout") 'words)
99 'font-lock-builtin-face)
101 "Default font-lock-keyword-face for `spamassassin mode'.")
103 (defcustom spamassassin-mode-hook nil
104 "*Hook called by `spamassassin-mode'."
106 :group 'spamassassin)
108 (defvar spamassassin-mode-map
109 (let ((map (make-sparse-keymap)))
110 (define-key map "\C-c\C-t" 'spamassassin-assassin-file)
111 (define-key map "\C-c\C-l" 'spamassassin-lint-rules)
112 (define-key map "\C-c\C-c" 'comment-region)
116 (easy-menu-define spamassassin-menu spamassassin-mode-map "Menu for the SpamAssassin mode"
118 ["Assassin file" spamassassin-assassin-file]
119 ["Lint rules" spamassassin-lint-rules]))
122 (defun spamassassin-mode ()
123 "A major mode to edit SpamAssassin rules.
124 \\{spamassassin-mode-map}
127 (kill-all-local-variables)
128 (use-local-map spamassassin-mode-map)
129 (setq local-abbrev-table spamassassin-mode-abbrev-table)
131 (make-local-variable 'comment-start)
132 (setq comment-start "#")
134 (make-local-variable 'font-lock-defaults)
135 (setq major-mode 'spamassassin-mode
136 mode-name "SpamAssassin"
137 font-lock-defaults '(spamassassin-font-lock-keywords nil))
138 (set-syntax-table spamassassin-mode-syntax-table)
139 (if (featurep 'easymenu)
140 (easy-menu-add spamassassin-menu))
142 (run-hooks 'spamassassin-mode-hook))
144 (defun spamassassin-lint-rules (&optional debug)
145 "Runs `spamassassin --lint' to lint the rules. Currently
146 spamassassin cannot check only one file, so it will lint your rules
147 only if they're located in one of the places where spamassassin looks
148 for them. Prefix arg runs spamassassin in debug mode."
150 (let ((spamassassin-command "spamassassin --lint"))
152 (setq spamassassin-command (concat spamassassin-command " -D")))
153 (shell-command spamassassin-command)))
155 ;; Regex-opt program can be found here:
156 ;; http://bisqwit.iki.fi/source/regexopt.html
157 (defun spamassassin-optimize-regexp (start end)
158 "Filter selection through regex-opt program."
160 (shell-command-on-region start end
162 (shell-quote-argument (buffer-substring start end)))
165 (defun spamassassin-assassin-file (filename &optional debug)
166 "Filters specified file through the spamassassin. Prefix arg runs
167 spamassassin in debug mode."
169 (let ((spamassassin-command "spamassassin -t"))
171 (setq spamassassin-command (concat spamassassin-command " -D")))
172 (setq spamassassin-command
173 (concat spamassassin-command "< " filename))
174 (shell-command spamassassin-command)))
176 (provide 'spamassassin-mode)