1 ;;; sieve-manage.el --- Implementation of the managesive protocol in elisp
3 ;; Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007,
4 ;; 2008, 2009, 2010 Free Software Foundation, Inc.
6 ;; Author: Simon Josefsson <simon@josefsson.org>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs 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 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs 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. If not, see <http://www.gnu.org/licenses/>.
25 ;; This library provides an elisp API for the managesieve network
28 ;; It uses the SASL library for authentication, which means it
29 ;; supports DIGEST-MD5, CRAM-MD5, SCRAM-MD5, NTLM, PLAIN and LOGIN
30 ;; methods. STARTTLS is not well tested, but should be easy to get to
31 ;; work if someone wants.
33 ;; The API should be fairly obvious for anyone familiar with the
34 ;; managesieve protocol, interface functions include:
36 ;; `sieve-manage-open'
37 ;; open connection to managesieve server, returning a buffer to be
38 ;; used by all other API functions.
40 ;; `sieve-manage-opened'
41 ;; check if a server is open or not
43 ;; `sieve-manage-close'
44 ;; close a server connection.
46 ;; `sieve-manage-listscripts'
47 ;; `sieve-manage-deletescript'
48 ;; `sieve-manage-getscript'
49 ;; performs managesieve protocol actions
51 ;; and that's it. Example of a managesieve session in *scratch*:
53 ;; (with-current-buffer (sieve-manage-open "mail.example.com")
54 ;; (sieve-manage-authenticate)
55 ;; (sieve-manage-listscripts))
57 ;; => ((active . "main") "vacation")
61 ;; draft-martin-managesieve-02.txt,
62 ;; "A Protocol for Remotely Managing Sieve Scripts",
67 ;; 2001-10-31 Committed to Oort Gnus.
68 ;; 2002-07-27 Added DELETESCRIPT. Suggested by Ned Ludd.
69 ;; 2002-08-03 Use SASL library.
73 ;; For Emacs <22.2 and XEmacs.
75 (unless (fboundp 'declare-function
) (defmacro declare-function
(&rest r
))))
77 (if (locate-library "password-cache")
78 (require 'password-cache
)
84 (autoload 'sasl-find-mechanism
"sasl")
85 (autoload 'starttls-open-stream
"starttls")
86 (autoload 'auth-source-user-or-password
"auth-source")
88 ;; User customizable variables:
90 (defgroup sieve-manage nil
91 "Low-level Managesieve protocol issues."
95 (defcustom sieve-manage-log
"*sieve-manage-log*"
96 "Name of buffer for managesieve session trace."
100 (defcustom sieve-manage-server-eol
"\r\n"
101 "The EOL string sent from the server."
103 :group
'sieve-manage
)
105 (defcustom sieve-manage-client-eol
"\r\n"
106 "The EOL string we send to the server."
108 :group
'sieve-manage
)
110 (defcustom sieve-manage-streams
'(network starttls shell
)
111 "Priority of streams to consider when opening connection to server."
112 :group
'sieve-manage
)
114 (defcustom sieve-manage-stream-alist
115 '((network sieve-manage-network-p sieve-manage-network-open
)
116 (shell sieve-manage-shell-p sieve-manage-shell-open
)
117 (starttls sieve-manage-starttls-p sieve-manage-starttls-open
))
118 "Definition of network streams.
122 NAME names the stream, CHECK is a function returning non-nil if the
123 server support the stream and OPEN is a function for opening the
125 :group
'sieve-manage
)
127 (defcustom sieve-manage-authenticators
'(digest-md5
133 "Priority of authenticators to consider when authenticating to server."
134 :group
'sieve-manage
)
136 (defcustom sieve-manage-authenticator-alist
137 '((cram-md5 sieve-manage-cram-md5-p sieve-manage-cram-md5-auth
)
138 (digest-md5 sieve-manage-digest-md5-p sieve-manage-digest-md5-auth
)
139 (scram-md5 sieve-manage-scram-md5-p sieve-manage-scram-md5-auth
)
140 (ntlm sieve-manage-ntlm-p sieve-manage-ntlm-auth
)
141 (plain sieve-manage-plain-p sieve-manage-plain-auth
)
142 (login sieve-manage-login-p sieve-manage-login-auth
))
143 "Definition of authenticators.
145 \(NAME CHECK AUTHENTICATE)
147 NAME names the authenticator. CHECK is a function returning non-nil if
148 the server support the authenticator and AUTHENTICATE is a function
149 for doing the actual authentication."
150 :group
'sieve-manage
)
152 (defcustom sieve-manage-default-port
2000
153 "Default port number or service name for managesieve protocol."
155 :group
'sieve-manage
)
157 (defcustom sieve-manage-default-stream
'network
158 "Default stream type to use for `sieve-manage'.
159 Must be a name of a stream in `sieve-manage-stream-alist'."
161 :group
'sieve-manage
)
163 ;; Internal variables:
165 (defconst sieve-manage-local-variables
'(sieve-manage-server
170 sieve-manage-client-eol
171 sieve-manage-server-eol
172 sieve-manage-capability
))
173 (defconst sieve-manage-coding-system-for-read
'binary
)
174 (defconst sieve-manage-coding-system-for-write
'binary
)
175 (defvar sieve-manage-stream nil
)
176 (defvar sieve-manage-auth nil
)
177 (defvar sieve-manage-server nil
)
178 (defvar sieve-manage-port nil
)
179 (defvar sieve-manage-state
'closed
181 Valid states are `closed', `initial', `nonauth', and `auth'.")
182 (defvar sieve-manage-process nil
)
183 (defvar sieve-manage-capability nil
)
185 ;; Internal utility functions
187 (defmacro sieve-manage-disable-multibyte
()
188 "Enable multibyte in the current buffer."
189 (unless (featurep 'xemacs
)
190 '(set-buffer-multibyte nil
)))
192 (defun sieve-manage-erase (&optional p buffer
)
193 (let ((buffer (or buffer
(current-buffer))))
194 (and sieve-manage-log
195 (with-current-buffer (get-buffer-create sieve-manage-log
)
196 (sieve-manage-disable-multibyte)
197 (buffer-disable-undo)
198 (goto-char (point-max))
199 (insert-buffer-substring buffer
(with-current-buffer buffer
201 (or p
(with-current-buffer buffer
203 (delete-region (point-min) (or p
(point-max))))
205 (defun sieve-manage-open-1 (buffer)
206 (with-current-buffer buffer
208 (setq sieve-manage-state
'initial
211 (funcall (nth 2 (assq sieve-manage-stream
212 sieve-manage-stream-alist
))
213 "sieve" buffer sieve-manage-server sieve-manage-port
)
215 (when sieve-manage-process
216 (while (and (eq sieve-manage-state
'initial
)
217 (memq (process-status sieve-manage-process
) '(open run
)))
218 (message "Waiting for response from %s..." sieve-manage-server
)
219 (accept-process-output sieve-manage-process
1))
220 (message "Waiting for response from %s...done" sieve-manage-server
)
221 (and (memq (process-status sieve-manage-process
) '(open run
))
222 sieve-manage-process
))))
226 (defun sieve-manage-network-p (buffer)
229 (defun sieve-manage-network-open (name buffer server port
)
230 (let* ((port (or port sieve-manage-default-port
))
231 (coding-system-for-read sieve-manage-coding-system-for-read
)
232 (coding-system-for-write sieve-manage-coding-system-for-write
)
233 (process (open-network-stream name buffer server port
)))
235 (while (and (memq (process-status process
) '(open run
))
236 (set-buffer buffer
) ;; XXX "blue moon" nntp.el bug
237 (goto-char (point-min))
238 (not (sieve-manage-parse-greeting-1)))
239 (accept-process-output process
1)
241 (sieve-manage-erase nil buffer
)
242 (when (memq (process-status process
) '(open run
))
245 (defun sieve-manage-starttls-p (buffer)
249 (call-process "starttls"))
252 (defun sieve-manage-starttls-open (name buffer server port
)
253 (let* ((port (or port sieve-manage-default-port
))
254 (coding-system-for-read sieve-manage-coding-system-for-read
)
255 (coding-system-for-write sieve-manage-coding-system-for-write
)
256 (process (starttls-open-stream name buffer server port
))
259 (while (and (memq (process-status process
) '(open run
))
260 (set-buffer buffer
) ;; XXX "blue moon" nntp.el bug
261 (goto-char (point-min))
262 (not (sieve-manage-parse-greeting-1)))
263 (accept-process-output process
1)
265 (sieve-manage-erase nil buffer
)
266 (sieve-manage-send "STARTTLS")
267 (starttls-negotiate process
))
268 (when (memq (process-status process
) '(open run
))
272 (defun sieve-sasl-auth (buffer mech
)
273 "Login to server using the SASL MECH method."
274 (message "sieve: Authenticating using %s..." mech
)
275 (with-current-buffer buffer
276 (let* ((user-password (auth-source-user-or-password
277 '("login" "password")
280 (client (sasl-make-client (sasl-find-mechanism (list mech
))
281 (car user-password
) "sieve" sieve-manage-server
))
282 (sasl-read-passphrase
283 ;; We *need* to copy the password, because sasl will modify it
285 `(lambda (prompt) ,(copy-sequence (cadr user-password
))))
286 (step (sasl-next-step client nil
))
287 (tag (sieve-manage-send
292 (and (sasl-step-data step
)
295 (base64-encode-string
296 (sasl-step-data step
)
303 (goto-char (point-min))
304 (while (null (or (progn
305 (setq rsp
(sieve-manage-is-string))
306 (if (not (and rsp
(looking-at
307 sieve-manage-server-eol
)))
309 (goto-char (match-end 0))
311 (setq rsp
(sieve-manage-is-okno))))
312 (accept-process-output sieve-manage-process
1)
313 (goto-char (point-min)))
315 (when (sieve-manage-ok-p rsp
)
316 (when (and (cadr rsp
)
317 (string-match "^SASL \"\\([^\"]+\\)\"" (cadr rsp
)))
319 step
(base64-decode-string (match-string 1 (cadr rsp
)))))
320 (if (and (setq step
(sasl-next-step client step
))
321 (setq data
(sasl-step-data step
)))
322 ;; We got data for server but it's finished
323 (error "Server not ready for SASL data: %s" data
)
324 ;; The authentication process is finished.
326 (unless (stringp rsp
)
327 (error "Server aborted SASL authentication: %s" (caddr rsp
)))
328 (sasl-step-set-data step
(base64-decode-string rsp
))
329 (setq step
(sasl-next-step client step
))
331 (if (sasl-step-data step
)
333 (base64-encode-string (sasl-step-data step
)
337 (message "sieve: Login using %s...done" mech
))))
339 (defun sieve-manage-cram-md5-p (buffer)
340 (sieve-manage-capability "SASL" "CRAM-MD5" buffer
))
342 (defun sieve-manage-cram-md5-auth (buffer)
343 "Login to managesieve server using the CRAM-MD5 SASL method."
344 (sieve-sasl-auth buffer
"CRAM-MD5"))
346 (defun sieve-manage-digest-md5-p (buffer)
347 (sieve-manage-capability "SASL" "DIGEST-MD5" buffer
))
349 (defun sieve-manage-digest-md5-auth (buffer)
350 "Login to managesieve server using the DIGEST-MD5 SASL method."
351 (sieve-sasl-auth buffer
"DIGEST-MD5"))
353 (defun sieve-manage-scram-md5-p (buffer)
354 (sieve-manage-capability "SASL" "SCRAM-MD5" buffer
))
356 (defun sieve-manage-scram-md5-auth (buffer)
357 "Login to managesieve server using the SCRAM-MD5 SASL method."
358 (sieve-sasl-auth buffer
"SCRAM-MD5"))
360 (defun sieve-manage-ntlm-p (buffer)
361 (sieve-manage-capability "SASL" "NTLM" buffer
))
363 (defun sieve-manage-ntlm-auth (buffer)
364 "Login to managesieve server using the NTLM SASL method."
365 (sieve-sasl-auth buffer
"NTLM"))
367 (defun sieve-manage-plain-p (buffer)
368 (sieve-manage-capability "SASL" "PLAIN" buffer
))
370 (defun sieve-manage-plain-auth (buffer)
371 "Login to managesieve server using the PLAIN SASL method."
372 (sieve-sasl-auth buffer
"PLAIN"))
374 (defun sieve-manage-login-p (buffer)
375 (sieve-manage-capability "SASL" "LOGIN" buffer
))
377 (defun sieve-manage-login-auth (buffer)
378 "Login to managesieve server using the LOGIN SASL method."
379 (sieve-sasl-auth buffer
"LOGIN"))
383 (defun sieve-manage-open (server &optional port stream auth buffer
)
384 "Open a network connection to a managesieve SERVER (string).
385 Optional argument PORT is port number (integer) on remote server.
386 Optional argument STREAM is any of `sieve-manage-streams' (a symbol).
387 Optional argument AUTH indicates authenticator to use, see
388 `sieve-manage-authenticators' for available authenticators.
389 If nil, chooses the best stream the server is capable of.
390 Optional argument BUFFER is buffer (buffer, or string naming buffer)
392 (setq buffer
(or buffer
(format " *sieve* %s:%s" server
(or port sieve-manage-default-port
))))
393 (with-current-buffer (get-buffer-create buffer
)
394 (mapc 'make-local-variable sieve-manage-local-variables
)
395 (sieve-manage-disable-multibyte)
396 (buffer-disable-undo)
397 (setq sieve-manage-server
(or server sieve-manage-server
))
398 (setq sieve-manage-port
(or port sieve-manage-port
))
399 (setq sieve-manage-stream
(or stream sieve-manage-stream
))
400 (message "sieve: Connecting to %s..." sieve-manage-server
)
401 (if (let ((sieve-manage-stream
402 (or sieve-manage-stream sieve-manage-default-stream
)))
403 (sieve-manage-open-1 buffer
))
405 (let (stream-changed)
406 (message "sieve: Connecting to %s...done" sieve-manage-server
)
407 (when (null sieve-manage-stream
)
408 (let ((streams sieve-manage-streams
))
409 (while (setq stream
(pop streams
))
410 (if (funcall (nth 1 (assq stream
411 sieve-manage-stream-alist
)) buffer
)
413 (not (eq (or sieve-manage-stream
414 sieve-manage-default-stream
)
416 sieve-manage-stream stream
418 (unless sieve-manage-stream
419 (error "Couldn't figure out a stream for server"))))
421 (message "sieve: Reconnecting with stream `%s'..."
423 (sieve-manage-close buffer
)
424 (if (sieve-manage-open-1 buffer
)
425 (message "sieve: Reconnecting with stream `%s'...done"
427 (message "sieve: Reconnecting with stream `%s'...failed"
428 sieve-manage-stream
))
429 (setq sieve-manage-capability nil
))
430 (if (sieve-manage-opened buffer
)
431 ;; Choose authenticator
432 (when (and (null sieve-manage-auth
)
433 (not (eq sieve-manage-state
'auth
)))
434 (let ((auths sieve-manage-authenticators
))
435 (while (setq auth
(pop auths
))
436 (if (funcall (nth 1 (assq
438 sieve-manage-authenticator-alist
))
440 (setq sieve-manage-auth auth
442 (unless sieve-manage-auth
443 (error "Couldn't figure out authenticator for server"))))))
444 (message "sieve: Connecting to %s...failed" sieve-manage-server
))
445 (when (sieve-manage-opened buffer
)
449 (defun sieve-manage-authenticate (&optional buffer
)
450 "Authenticate on server in BUFFER.
451 Return `sieve-manage-state' value."
452 (with-current-buffer (or buffer
(current-buffer))
453 (if (eq sieve-manage-state
'nonauth
)
454 (when (funcall (nth 2 (assq sieve-manage-auth
455 sieve-manage-authenticator-alist
))
457 (setq sieve-manage-state
'auth
))
458 sieve-manage-state
)))
460 (defun sieve-manage-opened (&optional buffer
)
461 "Return non-nil if connection to managesieve server in BUFFER is open.
462 If BUFFER is nil then the current buffer is used."
463 (and (setq buffer
(get-buffer (or buffer
(current-buffer))))
464 (buffer-live-p buffer
)
465 (with-current-buffer buffer
466 (and sieve-manage-process
467 (memq (process-status sieve-manage-process
) '(open run
))))))
469 (defun sieve-manage-close (&optional buffer
)
470 "Close connection to managesieve server in BUFFER.
471 If BUFFER is nil, the current buffer is used."
472 (with-current-buffer (or buffer
(current-buffer))
473 (when (sieve-manage-opened)
474 (sieve-manage-send "LOGOUT")
476 (when (and sieve-manage-process
477 (memq (process-status sieve-manage-process
) '(open run
)))
478 (delete-process sieve-manage-process
))
479 (setq sieve-manage-process nil
)
483 (defun sieve-manage-capability (&optional name value buffer
)
484 "Check if capability NAME of server BUFFER match VALUE.
485 If it does, return the server value of NAME. If not returns nil.
486 If VALUE is nil, do not check VALUE and return server value.
487 If NAME is nil, return the full server list of capabilities."
488 (with-current-buffer (or buffer
(current-buffer))
490 sieve-manage-capability
491 (let ((server-value (cadr (assoc name sieve-manage-capability
))))
492 (when (or (null value
)
494 (string-match value server-value
)))
497 (defun sieve-manage-listscripts (&optional buffer
)
498 (with-current-buffer (or buffer
(current-buffer))
499 (sieve-manage-send "LISTSCRIPTS")
500 (sieve-manage-parse-listscripts)))
502 (defun sieve-manage-havespace (name size
&optional buffer
)
503 (with-current-buffer (or buffer
(current-buffer))
504 (sieve-manage-send (format "HAVESPACE \"%s\" %s" name size
))
505 (sieve-manage-parse-okno)))
507 (defun sieve-manage-putscript (name content
&optional buffer
)
508 (with-current-buffer (or buffer
(current-buffer))
509 (sieve-manage-send (format "PUTSCRIPT \"%s\" {%d+}%s%s" name
510 ;; Here we assume that the coding-system will
511 ;; replace each char with a single byte.
512 ;; This is always the case if `content' is
515 sieve-manage-client-eol content
))
516 (sieve-manage-parse-okno)))
518 (defun sieve-manage-deletescript (name &optional buffer
)
519 (with-current-buffer (or buffer
(current-buffer))
520 (sieve-manage-send (format "DELETESCRIPT \"%s\"" name
))
521 (sieve-manage-parse-okno)))
523 (defun sieve-manage-getscript (name output-buffer
&optional buffer
)
524 (with-current-buffer (or buffer
(current-buffer))
525 (sieve-manage-send (format "GETSCRIPT \"%s\"" name
))
526 (let ((script (sieve-manage-parse-string)))
527 (sieve-manage-parse-crlf)
528 (with-current-buffer output-buffer
530 (sieve-manage-parse-okno))))
532 (defun sieve-manage-setactive (name &optional buffer
)
533 (with-current-buffer (or buffer
(current-buffer))
534 (sieve-manage-send (format "SETACTIVE \"%s\"" name
))
535 (sieve-manage-parse-okno)))
537 ;; Protocol parsing routines
539 (defun sieve-manage-ok-p (rsp)
540 (string= (downcase (or (car-safe rsp
) "")) "ok"))
542 (defsubst sieve-manage-forward
()
543 (or (eobp) (forward-char)))
545 (defun sieve-manage-is-okno ()
546 (when (looking-at (concat
547 "^\\(OK\\|NO\\)\\( (\\([^)]+\\))\\)?\\( \\(.*\\)\\)?"
548 sieve-manage-server-eol
))
549 (let ((status (match-string 1))
550 (resp-code (match-string 3))
551 (response (match-string 5)))
553 (goto-char (match-beginning 5))
554 (setq response
(sieve-manage-is-string)))
555 (list status resp-code response
))))
557 (defun sieve-manage-parse-okno ()
560 (accept-process-output (get-buffer-process (current-buffer)) 1)
561 (goto-char (point-min))
562 (setq rsp
(sieve-manage-is-okno)))
566 (defun sieve-manage-parse-capability-1 ()
567 "Accept a managesieve greeting."
569 (while (setq str
(sieve-manage-is-string))
570 (if (eq (char-after) ?
)
572 (sieve-manage-forward)
573 (push (list str
(sieve-manage-is-string))
574 sieve-manage-capability
))
575 (push (list str
) sieve-manage-capability
))
577 (when (re-search-forward (concat "^OK.*" sieve-manage-server-eol
) nil t
)
578 (setq sieve-manage-state
'nonauth
)))
580 (defalias 'sieve-manage-parse-greeting-1
'sieve-manage-parse-capability-1
)
582 (defun sieve-manage-is-string ()
583 (cond ((looking-at "\"\\([^\"]+\\)\"")
586 (goto-char (match-end 0))))
587 ((looking-at (concat "{\\([0-9]+\\+?\\)}" sieve-manage-server-eol
))
588 (let ((pos (match-end 0))
589 (len (string-to-number (match-string 1))))
590 (if (< (point-max) (+ pos len
))
592 (goto-char (+ pos len
))
593 (buffer-substring pos
(+ pos len
)))))))
595 (defun sieve-manage-parse-string ()
598 (accept-process-output (get-buffer-process (current-buffer)) 1)
599 (goto-char (point-min))
600 (setq rsp
(sieve-manage-is-string)))
601 (sieve-manage-erase (point))
604 (defun sieve-manage-parse-crlf ()
605 (when (looking-at sieve-manage-server-eol
)
606 (sieve-manage-erase (match-end 0))))
608 (defun sieve-manage-parse-listscripts ()
611 (while (null (or (setq rsp
(sieve-manage-is-okno))
612 (setq tmp
(sieve-manage-is-string))))
613 (accept-process-output (get-buffer-process (current-buffer)) 1)
614 (goto-char (point-min)))
616 (while (not (looking-at (concat "\\( ACTIVE\\)?"
617 sieve-manage-server-eol
)))
618 (accept-process-output (get-buffer-process (current-buffer)) 1)
619 (goto-char (point-min)))
621 (push (cons 'active tmp
) data
)
623 (goto-char (match-end 0))
626 (if (sieve-manage-ok-p rsp
)
630 (defun sieve-manage-send (cmdstr)
631 (setq cmdstr
(concat cmdstr sieve-manage-client-eol
))
632 (and sieve-manage-log
633 (with-current-buffer (get-buffer-create sieve-manage-log
)
634 (sieve-manage-disable-multibyte)
635 (buffer-disable-undo)
636 (goto-char (point-max))
638 (process-send-string sieve-manage-process cmdstr
))
640 (provide 'sieve-manage
)
642 ;; sieve-manage.el ends here