1 ;;; sieve.el --- Utilities to manage sieve scripts
3 ;; Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
5 ;; Author: Simon Josefsson <simon@josefsson.org>
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs 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 3 of the License, or
12 ;; (at your option) any later version.
14 ;; GNU Emacs 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 GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;; This file contain utilities to facilate upload, download and
25 ;; general management of sieve scripts. Currently only the
26 ;; Managesieve protocol is supported (using sieve-manage.el), but when
27 ;; (useful) alternatives become available, they might be supported as
30 ;; The cursor navigation was inspired by biff-mode by Franklin Lee.
34 ;; 2001-10-31 Committed to Oort Gnus.
35 ;; 2002-07-27 Fix down-mouse-2 and down-mouse-3 in manage-mode. Fix menubar
36 ;; in manage-mode. Change some messages. Added sieve-deactivate*,
37 ;; sieve-remove. Fixed help text in manage-mode. Suggested by
42 ;; * Namespace? This file contains `sieve-manage' and
43 ;; `sieve-manage-mode', but there is a sieve-manage.el file as well.
44 ;; Can't think of a good solution though, this file need a *-mode,
45 ;; and naming it `sieve-mode' would collide with sieve-mode.el. One
46 ;; solution would be to come up with some better name that this file
47 ;; can use that doesn't have the managesieve specific "manage" in
48 ;; it. sieve-dired? i dunno. we could copy all off sieve.el into
49 ;; sieve-manage.el too, but I'd like to separate the interface from
50 ;; the protocol implementation since the backends are likely to
53 ;; * Define servers? We could have a customize buffer to create a server,
54 ;; with authentication/stream/etc parameters, much like Gnus, and then
55 ;; only use names of defined servers when interacting with M-x sieve-*.
56 ;; Right now you can't use STARTTLS, which sieve-manage.el provides
60 (require 'sieve-manage
)
63 ;; User customizable variables:
66 "Manage sieve scripts."
70 (defcustom sieve-new-script
"<new script>"
71 "Name of name script indicator."
75 (defcustom sieve-buffer
"*sieve*"
76 "Name of sieve management buffer."
80 (defcustom sieve-template
"\
83 # Example script (remove comment character '#' to make it effective!):
85 # if header :contains \"from\" \"coyote\" {
87 # } elsif header :contains [\"subject\"] [\"$$$\"] {
93 "Template sieve script."
97 ;; Internal variables:
99 (defvar sieve-manage-buffer nil
)
100 (defvar sieve-buffer-header-end nil
)
102 ;; Sieve-manage mode:
104 (defvar sieve-manage-mode-map nil
105 "Keymap for `sieve-manage-mode'.")
107 (if sieve-manage-mode-map
109 (setq sieve-manage-mode-map
(make-sparse-keymap))
110 (suppress-keymap sieve-manage-mode-map
)
112 (define-key sieve-manage-mode-map
"?" 'sieve-help
)
113 (define-key sieve-manage-mode-map
"h" 'sieve-help
)
114 (define-key sieve-manage-mode-map
"q" 'sieve-bury-buffer
)
116 (define-key sieve-manage-mode-map
"m" 'sieve-activate
)
117 (define-key sieve-manage-mode-map
"u" 'sieve-deactivate
)
118 (define-key sieve-manage-mode-map
"\M-\C-?" 'sieve-deactivate-all
)
120 (define-key sieve-manage-mode-map
"\C-p" 'sieve-prev-line
)
121 (define-key sieve-manage-mode-map
[up] 'sieve-prev-line)
122 (define-key sieve-manage-mode-map "\C-n" 'sieve-next-line)
123 (define-key sieve-manage-mode-map [down] 'sieve-next-line)
124 (define-key sieve-manage-mode-map " " 'sieve-next-line)
125 (define-key sieve-manage-mode-map "n" 'sieve-next-line)
126 (define-key sieve-manage-mode-map "p" 'sieve-prev-line)
127 (define-key sieve-manage-mode-map "\C-m" 'sieve-edit-script)
128 (define-key sieve-manage-mode-map "f" 'sieve-edit-script)
129 (define-key sieve-manage-mode-map "o" 'sieve-edit-script-other-window)
130 (define-key sieve-manage-mode-map "r" 'sieve-remove)
131 (define-key sieve-manage-mode-map [(down-mouse-2)] 'sieve-edit-script)
132 (define-key sieve-manage-mode-map [(down-mouse-3)] 'sieve-manage-mode-menu))
134 (easy-menu-define sieve-manage-mode-menu sieve-manage-mode-map
137 ["Edit script" sieve-edit-script t]
138 ["Activate script" sieve-activate t]
139 ["Deactivate script" sieve-deactivate t]))
141 (define-derived-mode sieve-manage-mode fundamental-mode "SIEVE"
142 "Mode used for sieve script management."
143 (setq mode-name "SIEVE")
144 (buffer-disable-undo (current-buffer))
145 (setq truncate-lines t)
146 (easy-menu-add sieve-manage-mode-menu sieve-manage-mode-map))
148 (put 'sieve-manage-mode 'mode-class 'special)
150 ;; This is necessary to allow correct handling of \\[cvs-mode-diff-map]
151 ;; in substitute-command-keys.
152 ;(fset 'sieve-manage-mode-map sieve-manage-mode-map)
154 ;; Commands used in sieve-manage mode:
156 (defun sieve-activate (&optional pos)
158 (let ((name (sieve-script-at-point)) err)
159 (when (or (null name) (string-equal name sieve-new-script))
160 (error "No sieve script at point"))
161 (message "Activating script %s..." name)
162 (setq err (sieve-manage-setactive name sieve-manage-buffer))
163 (sieve-refresh-scriptlist)
164 (if (sieve-manage-ok-p err)
165 (message "Activating script %s...done" name)
166 (message "Activating script %s...failed: %s" name (nth 2 err)))))
168 (defun sieve-deactivate-all (&optional pos)
170 (let ((name (sieve-script-at-point)) err)
171 (message "Deactivating scripts...")
172 (setq err (sieve-manage-setactive "" sieve-manage-buffer))
173 (sieve-refresh-scriptlist)
174 (if (sieve-manage-ok-p err)
175 (message "Deactivating scripts...done")
176 (message "Deactivating scripts...failed: %s" (nth 2 err)))))
178 (defalias 'sieve-deactivate 'sieve-deactivate-all)
180 (defun sieve-remove (&optional pos)
182 (let ((name (sieve-script-at-point)) err)
183 (when (or (null name) (string-equal name sieve-new-script))
184 (error "No sieve script at point"))
185 (message "Removing sieve script %s..." name)
186 (setq err (sieve-manage-deletescript name sieve-manage-buffer))
187 (unless (sieve-manage-ok-p err)
188 (error "Removing sieve script %s...failed: " err))
189 (sieve-refresh-scriptlist)
190 (message "Removing sieve script %s...done" name)))
192 (defun sieve-edit-script (&optional pos)
194 (let ((name (sieve-script-at-point)))
196 (error "No sieve script at point"))
197 (if (not (string-equal name sieve-new-script))
198 (let ((newbuf (generate-new-buffer name))
200 (setq err (sieve-manage-getscript name newbuf sieve-manage-buffer))
201 (switch-to-buffer newbuf)
202 (unless (sieve-manage-ok-p err)
203 (error "Sieve download failed: %s" err)))
204 (switch-to-buffer (get-buffer-create "template.siv"))
205 (insert sieve-template))
207 (message "Press C-c C-l to upload script to server.")))
209 (defmacro sieve-change-region (&rest body)
210 "Turns off sieve-region before executing BODY, then re-enables it after.
211 Used to bracket operations which move point in the sieve-buffer."
213 (sieve-highlight nil)
215 (sieve-highlight t)))
216 (put 'sieve-change-region 'lisp-indent-function 0)
218 (defun sieve-next-line (&optional arg)
224 (sieve-script-at-point))
227 (message "End of list")))
229 (defun sieve-prev-line (&optional arg)
235 (sieve-script-at-point))
238 (message "Beginning of list")))
241 "Display help for various sieve commands."
243 (if (eq last-command 'sieve-help)
244 ;; would need minor-mode for log-edit-mode
245 (describe-function 'sieve-mode)
246 (message "%s" (substitute-command-keys
247 "`\\[sieve-edit-script]':edit `\\[sieve-activate]':activate `\\[sieve-deactivate]':deactivate `\\[sieve-remove]':remove"))))
249 (defun sieve-bury-buffer (buf &optional mainbuf)
250 "Hide the buffer BUF that was temporarily popped up.
251 BUF is assumed to be a temporary buffer used from the buffer MAINBUF."
252 (interactive (list (current-buffer)))
254 (let ((win (if (eq buf (window-buffer (selected-window))) (selected-window)
255 (get-buffer-window buf t))))
257 (if (window-dedicated-p win)
260 (error (iconify-frame (window-frame win))))
261 (if (and mainbuf (get-buffer-window mainbuf))
262 (delete-window win)))))
263 (with-current-buffer buf
264 (bury-buffer (unless (and (eq buf (window-buffer (selected-window)))
265 (not (window-dedicated-p (selected-window))))
268 (let ((mainwin (or (get-buffer-window mainbuf)
269 (get-buffer-window mainbuf 'visible))))
270 (when mainwin (select-window mainwin))))))
274 (defun sieve-setup-buffer (server port)
275 (setq buffer-read-only nil)
277 (buffer-disable-undo)
279 Server : " server ":" (or port "2000") "
282 (set (make-local-variable 'sieve-buffer-header-end)
285 (defun sieve-script-at-point (&optional pos)
286 "Return name of sieve script at point POS, or nil."
288 (get-char-property (or pos (point)) 'script-name))
291 (defalias 'sieve-make-overlay (if (featurep 'xemacs)
294 (defalias 'sieve-overlay-put (if (featurep 'xemacs)
297 (defalias 'sieve-overlays-at (if (featurep 'xemacs)
301 (defun sieve-highlight (on)
302 "Turn ON or off highlighting on the current language overlay."
303 (sieve-overlay-put (car (sieve-overlays-at (point)))
304 'face (if on 'highlight 'default)))
306 (defun sieve-insert-scripts (scripts)
307 "Format and insert LANGUAGE-LIST strings into current buffer at point."
311 (script (pop scripts)))
313 (insert (format " ACTIVE %s" (cdr script)))
314 (insert (format " %s" script)))
315 (setq ext (sieve-make-overlay p (point)))
316 (sieve-overlay-put ext 'mouse-face 'highlight)
317 (sieve-overlay-put ext 'script-name (if (consp script)
322 (defun sieve-open-server (server &optional port)
324 (set (make-local-variable 'sieve-manage-buffer)
325 (sieve-manage-open server))
327 (sieve-manage-authenticate nil nil sieve-manage-buffer))
329 (defun sieve-refresh-scriptlist ()
331 (with-current-buffer sieve-buffer
332 (setq buffer-read-only nil)
333 (delete-region (or sieve-buffer-header-end (point-max)) (point-max))
334 (goto-char (point-max))
335 ;; get list of script names and print them
336 (let ((scripts (sieve-manage-listscripts sieve-manage-buffer)))
338 (insert (format (concat "No scripts on server, press RET on %s to "
339 "create a new script.\n") sieve-new-script))
340 (insert (format (concat "%d script%s on server, press RET on a script "
341 "name edits it, or\npress RET on %s to create "
342 "a new script.\n") (length scripts)
343 (if (eq (length scripts) 1) "" "s")
346 (sieve-insert-scripts (list sieve-new-script))
347 (sieve-insert-scripts scripts)))
349 (setq buffer-read-only t)))
352 (defun sieve-manage (server &optional port)
353 (interactive "sServer: ")
354 (switch-to-buffer (get-buffer-create sieve-buffer))
356 (sieve-setup-buffer server port)
357 (if (sieve-open-server server port)
358 (sieve-refresh-scriptlist)
359 (message "Could not open server %s" server)))
362 (defun sieve-upload (&optional name)
365 (setq name (buffer-name)))
366 (when (or (get-buffer sieve-buffer) (call-interactively 'sieve-manage))
367 (let ((script (buffer-string)) err)
368 (with-current-buffer (get-buffer sieve-buffer)
369 (setq err (sieve-manage-putscript name script sieve-manage-buffer))
370 (if (sieve-manage-ok-p err)
372 "Sieve upload done. Use `C-c RET' to manage scripts."))
373 (message "Sieve upload failed: %s" (nth 2 err)))))))
376 (defun sieve-upload-and-bury (&optional name)
383 ;; arch-tag: 7f6a6d94-94e1-4654-ab9a-aee21b9b8a94
384 ;; sieve.el ends here