1 ;;; sieve.el --- Utilities to manage sieve scripts
3 ;; Copyright (C) 2001-2017 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 <https://www.gnu.org/licenses/>.
24 ;; This file contain utilities to facilitate 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
)
101 (defvar sieve-buffer-script-name nil
102 "The real script name of the buffer.")
103 (make-local-variable 'sieve-buffer-script-name
)
105 ;; Sieve-manage mode:
107 (defvar sieve-manage-mode-map
108 (let ((map (make-sparse-keymap)))
110 (define-key map
"?" 'sieve-help
)
111 (define-key map
"h" 'sieve-help
)
113 (define-key map
"m" 'sieve-activate
)
114 (define-key map
"u" 'sieve-deactivate
)
115 (define-key map
"\M-\C-?" 'sieve-deactivate-all
)
117 (define-key map
"\C-p" 'sieve-prev-line
)
118 (define-key map
[up] 'sieve-prev-line)
119 (define-key map "\C-n" 'sieve-next-line)
120 (define-key map [down] 'sieve-next-line)
121 (define-key map " " 'sieve-next-line)
122 (define-key map "n" 'sieve-next-line)
123 (define-key map "p" 'sieve-prev-line)
124 (define-key map "\C-m" 'sieve-edit-script)
125 (define-key map "f" 'sieve-edit-script)
126 (define-key map "o" 'sieve-edit-script-other-window)
127 (define-key map "r" 'sieve-remove)
128 (define-key map "q" 'sieve-bury-buffer)
129 (define-key map "Q" 'sieve-manage-quit)
130 (define-key map [(down-mouse-2)] 'sieve-edit-script)
131 (define-key map [(down-mouse-3)] 'sieve-manage-mode-menu)
133 "Keymap for `sieve-manage-mode'.")
135 (easy-menu-define sieve-manage-mode-menu sieve-manage-mode-map
138 ["Edit script" sieve-edit-script t]
139 ["Activate script" sieve-activate t]
140 ["Deactivate script" sieve-deactivate t]))
142 (define-derived-mode sieve-manage-mode fundamental-mode "Sieve-manage"
143 "Mode used for sieve script management."
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 ;; Commands used in sieve-manage mode:
152 (defun sieve-manage-quit ()
153 "Quit Manage Sieve and close the connection."
155 (sieve-manage-close sieve-manage-buffer)
156 (kill-buffer sieve-manage-buffer)
157 (kill-buffer (current-buffer)))
159 (defun sieve-bury-buffer ()
160 "Bury the Manage Sieve buffer without closing the connection."
164 (defun sieve-activate (&optional pos)
166 (let ((name (sieve-script-at-point)) err)
167 (when (or (null name) (string-equal name sieve-new-script))
168 (error "No sieve script at point"))
169 (message "Activating script %s..." name)
170 (setq err (sieve-manage-setactive name sieve-manage-buffer))
171 (sieve-refresh-scriptlist)
172 (if (sieve-manage-ok-p err)
173 (message "Activating script %s...done" name)
174 (message "Activating script %s...failed: %s" name (nth 2 err)))))
176 (defun sieve-deactivate-all (&optional pos)
178 (let ((name (sieve-script-at-point)) err)
179 (message "Deactivating scripts...")
180 (setq err (sieve-manage-setactive "" sieve-manage-buffer))
181 (sieve-refresh-scriptlist)
182 (if (sieve-manage-ok-p err)
183 (message "Deactivating scripts...done")
184 (message "Deactivating scripts...failed: %s" (nth 2 err)))))
186 (defalias 'sieve-deactivate 'sieve-deactivate-all)
188 (defun sieve-remove (&optional pos)
190 (let ((name (sieve-script-at-point)) err)
191 (when (or (null name) (string-equal name sieve-new-script))
192 (error "No sieve script at point"))
193 (message "Removing sieve script %s..." name)
194 (setq err (sieve-manage-deletescript name sieve-manage-buffer))
195 (unless (sieve-manage-ok-p err)
196 (error "Removing sieve script %s...failed: " err))
197 (sieve-refresh-scriptlist)
198 (message "Removing sieve script %s...done" name)))
200 (defun sieve-edit-script (&optional pos)
202 (let ((name (sieve-script-at-point)))
204 (error "No sieve script at point"))
205 (if (not (string-equal name sieve-new-script))
206 (let ((newbuf (generate-new-buffer name))
208 (setq err (sieve-manage-getscript name newbuf sieve-manage-buffer))
209 (switch-to-buffer newbuf)
210 (if (sieve-manage-ok-p err)
211 (set-buffer-modified-p nil)
212 (error "Sieve download failed: %s" err)))
213 (switch-to-buffer (get-buffer-create "template.siv"))
214 (insert sieve-template))
216 (setq sieve-buffer-script-name name)
217 (goto-char (point-min))
219 (substitute-command-keys
220 "Press \\[sieve-upload] to upload script to server."))))
222 (defmacro sieve-change-region (&rest body)
223 "Turns off sieve-region before executing BODY, then re-enables it after.
224 Used to bracket operations which move point in the sieve-buffer."
226 (sieve-highlight nil)
228 (sieve-highlight t)))
229 (put 'sieve-change-region 'lisp-indent-function 0)
231 (defun sieve-next-line (&optional arg)
237 (sieve-script-at-point))
240 (message "End of list")))
242 (defun sieve-prev-line (&optional arg)
248 (sieve-script-at-point))
251 (message "Beginning of list")))
254 "Display help for various sieve commands."
256 (if (eq last-command 'sieve-help)
257 ;; would need minor-mode for log-edit-mode
258 (describe-function 'sieve-mode)
259 (message "%s" (substitute-command-keys
260 "`\\[sieve-edit-script]':edit `\\[sieve-activate]':activate `\\[sieve-deactivate]':deactivate `\\[sieve-remove]':remove"))))
264 (defun sieve-setup-buffer (server port)
265 (setq buffer-read-only nil)
267 (buffer-disable-undo)
268 (let* ((port (or port sieve-manage-default-port))
269 (header (format "Server : %s:%s\n\n" server port)))
271 (set (make-local-variable 'sieve-buffer-header-end)
274 (defun sieve-script-at-point (&optional pos)
275 "Return name of sieve script at point POS, or nil."
277 (get-char-property (or pos (point)) 'script-name))
279 (defun sieve-highlight (on)
280 "Turn ON or off highlighting on the current language overlay."
281 (overlay-put (car (overlays-at (point))) 'face (if on 'highlight 'default)))
283 (defun sieve-insert-scripts (scripts)
284 "Format and insert LANGUAGE-LIST strings into current buffer at point."
288 (script (pop scripts)))
290 (insert (format " ACTIVE %s" (cdr script)))
291 (insert (format " %s" script)))
292 (setq ext (make-overlay p (point)))
293 (overlay-put ext 'mouse-face 'highlight)
294 (overlay-put ext 'script-name (if (consp script)
299 (defun sieve-open-server (server &optional port)
300 "Open SERVER (on PORT) and authenticate."
303 (set (make-local-variable 'sieve-manage-buffer)
304 (sieve-manage-open server port))
305 (error "Error opening server %s" server))
306 (sieve-manage-authenticate)))
308 (defun sieve-refresh-scriptlist ()
310 (with-current-buffer sieve-buffer
311 (setq buffer-read-only nil)
312 (delete-region (or sieve-buffer-header-end (point-max)) (point-max))
313 (goto-char (point-max))
314 ;; get list of script names and print them
315 (let ((scripts (sieve-manage-listscripts sieve-manage-buffer)))
318 (substitute-command-keys
320 "No scripts on server, press \\[sieve-edit-script] on %s to create a new script.\n"
323 (substitute-command-keys
324 (format (concat "%d script%s on server, press \\[sieve-edit-script] on a script "
325 "name edits it, or\npress \\[sieve-edit-script] on %s to create "
326 "a new script.\n") (length scripts)
327 (if (eq (length scripts) 1) "" "s")
330 (sieve-insert-scripts (list sieve-new-script))
331 (sieve-insert-scripts scripts)))
333 (setq buffer-read-only t)))
336 (defun sieve-manage (server &optional port)
337 (interactive "sServer: ")
338 (switch-to-buffer (get-buffer-create sieve-buffer))
340 (sieve-setup-buffer server port)
341 (if (sieve-open-server server port)
342 (sieve-refresh-scriptlist)
343 (message "Could not open server %s" server)))
346 (defun sieve-upload (&optional name)
348 (when (or (get-buffer sieve-buffer) (call-interactively 'sieve-manage))
349 (let ((script (buffer-string)) err)
350 (with-current-buffer (get-buffer sieve-buffer)
351 (setq err (sieve-manage-putscript
352 (or name sieve-buffer-script-name (buffer-name))
353 script sieve-manage-buffer))
354 (if (sieve-manage-ok-p err)
355 (message (substitute-command-keys
356 "Sieve upload done. Use \\[sieve-manage] to manage scripts."))
357 (message "Sieve upload failed: %s" (nth 2 err)))))))
360 (defun sieve-upload-and-bury (&optional name)
366 (defun sieve-upload-and-kill (&optional name)
373 ;; sieve.el ends here