1 ;;; sieve-manage.el --- Implementation of the managesieve protocol in elisp
3 ;; Copyright (C) 2001-2012 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 library provides an elisp API for the managesieve network
27 ;; It uses the SASL library for authentication, which means it
28 ;; supports DIGEST-MD5, CRAM-MD5, SCRAM-MD5, NTLM, PLAIN and LOGIN
29 ;; methods. STARTTLS is not well tested, but should be easy to get to
30 ;; work if someone wants.
32 ;; The API should be fairly obvious for anyone familiar with the
33 ;; managesieve protocol, interface functions include:
35 ;; `sieve-manage-open'
36 ;; open connection to managesieve server, returning a buffer to be
37 ;; used by all other API functions.
39 ;; `sieve-manage-opened'
40 ;; check if a server is open or not
42 ;; `sieve-manage-close'
43 ;; close a server connection.
45 ;; `sieve-manage-listscripts'
46 ;; `sieve-manage-deletescript'
47 ;; `sieve-manage-getscript'
48 ;; performs managesieve protocol actions
50 ;; and that's it. Example of a managesieve session in *scratch*:
52 ;; (with-current-buffer (sieve-manage-open "mail.example.com")
53 ;; (sieve-manage-authenticate)
54 ;; (sieve-manage-listscripts))
56 ;; => ((active . "main") "vacation")
60 ;; draft-martin-managesieve-02.txt,
61 ;; "A Protocol for Remotely Managing Sieve Scripts",
66 ;; 2001-10-31 Committed to Oort Gnus.
67 ;; 2002-07-27 Added DELETESCRIPT. Suggested by Ned Ludd.
68 ;; 2002-08-03 Use SASL library.
72 ;; For Emacs <22.2 and XEmacs.
74 (unless (fboundp 'declare-function
) (defmacro declare-function
(&rest r
))))
76 (if (locate-library "password-cache")
77 (require 'password-cache
)
84 (autoload 'sasl-find-mechanism
"sasl")
85 (autoload 'starttls-open-stream
"starttls")
86 (autoload 'auth-source-search
"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'."
162 :group
'sieve-manage
)
164 ;; Internal variables:
166 (defconst sieve-manage-local-variables
'(sieve-manage-server
171 sieve-manage-client-eol
172 sieve-manage-server-eol
173 sieve-manage-capability
))
174 (defconst sieve-manage-coding-system-for-read
'binary
)
175 (defconst sieve-manage-coding-system-for-write
'binary
)
176 (defvar sieve-manage-stream nil
)
177 (defvar sieve-manage-auth nil
)
178 (defvar sieve-manage-server nil
)
179 (defvar sieve-manage-port nil
)
180 (defvar sieve-manage-state
'closed
182 Valid states are `closed', `initial', `nonauth', and `auth'.")
183 (defvar sieve-manage-process nil
)
184 (defvar sieve-manage-capability nil
)
186 ;; Internal utility functions
188 (defmacro sieve-manage-disable-multibyte
()
189 "Enable multibyte in the current buffer."
190 (unless (featurep 'xemacs
)
191 '(set-buffer-multibyte nil
)))
193 (defun sieve-manage-erase (&optional p buffer
)
194 (let ((buffer (or buffer
(current-buffer))))
195 (and sieve-manage-log
196 (with-current-buffer (get-buffer-create sieve-manage-log
)
197 (sieve-manage-disable-multibyte)
198 (buffer-disable-undo)
199 (goto-char (point-max))
200 (insert-buffer-substring buffer
(with-current-buffer buffer
202 (or p
(with-current-buffer buffer
204 (delete-region (point-min) (or p
(point-max))))
206 (defun sieve-manage-open-1 (buffer)
207 (with-current-buffer buffer
209 (setq sieve-manage-state
'initial
212 (funcall (nth 2 (assq sieve-manage-stream
213 sieve-manage-stream-alist
))
214 "sieve" buffer sieve-manage-server sieve-manage-port
)
216 (when sieve-manage-process
217 (while (and (eq sieve-manage-state
'initial
)
218 (memq (process-status sieve-manage-process
) '(open run
)))
219 (message "Waiting for response from %s..." sieve-manage-server
)
220 (accept-process-output sieve-manage-process
1))
221 (message "Waiting for response from %s...done" sieve-manage-server
)
222 (and (memq (process-status sieve-manage-process
) '(open run
))
223 sieve-manage-process
))))
227 (defun sieve-manage-network-p (buffer)
230 (defun sieve-manage-network-open (name buffer server port
)
231 (let* ((port (or port sieve-manage-default-port
))
232 (coding-system-for-read sieve-manage-coding-system-for-read
)
233 (coding-system-for-write sieve-manage-coding-system-for-write
)
234 (process (open-network-stream name buffer server port
)))
236 (while (and (memq (process-status process
) '(open run
))
237 (set-buffer buffer
) ;; XXX "blue moon" nntp.el bug
238 (goto-char (point-min))
239 (not (sieve-manage-parse-greeting-1)))
240 (accept-process-output process
1)
242 (sieve-manage-erase nil buffer
)
243 (when (memq (process-status process
) '(open run
))
246 (defun sieve-manage-starttls-p (buffer)
250 (call-process "starttls"))
253 (defun sieve-manage-starttls-open (name buffer server port
)
254 (let* ((port (or port sieve-manage-default-port
))
255 (coding-system-for-read sieve-manage-coding-system-for-read
)
256 (coding-system-for-write sieve-manage-coding-system-for-write
)
257 (process (starttls-open-stream name buffer server port
))
260 (while (and (memq (process-status process
) '(open run
))
261 (set-buffer buffer
) ;; XXX "blue moon" nntp.el bug
262 (goto-char (point-min))
263 (not (sieve-manage-parse-greeting-1)))
264 (accept-process-output process
1)
266 (sieve-manage-erase nil buffer
)
267 (sieve-manage-send "STARTTLS")
268 (starttls-negotiate process
))
269 (when (memq (process-status process
) '(open run
))
273 (defun sieve-sasl-auth (buffer mech
)
274 "Login to server using the SASL MECH method."
275 (message "sieve: Authenticating using %s..." mech
)
276 (with-current-buffer buffer
277 (let* ((auth-info (auth-source-search :host sieve-manage-server
281 (user-name (or (plist-get (nth 0 auth-info
) :user
) ""))
282 (user-password (or (plist-get (nth 0 auth-info
) :secret
) ""))
283 (user-password (if (functionp user-password
)
284 (funcall user-password
)
286 (client (sasl-make-client (sasl-find-mechanism (list mech
))
287 user-name
"sieve" sieve-manage-server
))
288 (sasl-read-passphrase
289 ;; We *need* to copy the password, because sasl will modify it
291 `(lambda (prompt) ,(copy-sequence user-password
)))
292 (step (sasl-next-step client nil
))
293 (tag (sieve-manage-send
298 (and (sasl-step-data step
)
301 (base64-encode-string
302 (sasl-step-data step
)
309 (goto-char (point-min))
310 (while (null (or (progn
311 (setq rsp
(sieve-manage-is-string))
312 (if (not (and rsp
(looking-at
313 sieve-manage-server-eol
)))
315 (goto-char (match-end 0))
317 (setq rsp
(sieve-manage-is-okno))))
318 (accept-process-output sieve-manage-process
1)
319 (goto-char (point-min)))
321 (when (sieve-manage-ok-p rsp
)
322 (when (and (cadr rsp
)
323 (string-match "^SASL \"\\([^\"]+\\)\"" (cadr rsp
)))
325 step
(base64-decode-string (match-string 1 (cadr rsp
)))))
326 (if (and (setq step
(sasl-next-step client step
))
327 (setq data
(sasl-step-data step
)))
328 ;; We got data for server but it's finished
329 (error "Server not ready for SASL data: %s" data
)
330 ;; The authentication process is finished.
332 (unless (stringp rsp
)
333 (error "Server aborted SASL authentication: %s" (caddr rsp
)))
334 (sasl-step-set-data step
(base64-decode-string rsp
))
335 (setq step
(sasl-next-step client step
))
337 (if (sasl-step-data step
)
339 (base64-encode-string (sasl-step-data step
)
343 (message "sieve: Login using %s...done" mech
))))
345 (defun sieve-manage-cram-md5-p (buffer)
346 (sieve-manage-capability "SASL" "CRAM-MD5" buffer
))
348 (defun sieve-manage-cram-md5-auth (buffer)
349 "Login to managesieve server using the CRAM-MD5 SASL method."
350 (sieve-sasl-auth buffer
"CRAM-MD5"))
352 (defun sieve-manage-digest-md5-p (buffer)
353 (sieve-manage-capability "SASL" "DIGEST-MD5" buffer
))
355 (defun sieve-manage-digest-md5-auth (buffer)
356 "Login to managesieve server using the DIGEST-MD5 SASL method."
357 (sieve-sasl-auth buffer
"DIGEST-MD5"))
359 (defun sieve-manage-scram-md5-p (buffer)
360 (sieve-manage-capability "SASL" "SCRAM-MD5" buffer
))
362 (defun sieve-manage-scram-md5-auth (buffer)
363 "Login to managesieve server using the SCRAM-MD5 SASL method."
364 (sieve-sasl-auth buffer
"SCRAM-MD5"))
366 (defun sieve-manage-ntlm-p (buffer)
367 (sieve-manage-capability "SASL" "NTLM" buffer
))
369 (defun sieve-manage-ntlm-auth (buffer)
370 "Login to managesieve server using the NTLM SASL method."
371 (sieve-sasl-auth buffer
"NTLM"))
373 (defun sieve-manage-plain-p (buffer)
374 (sieve-manage-capability "SASL" "PLAIN" buffer
))
376 (defun sieve-manage-plain-auth (buffer)
377 "Login to managesieve server using the PLAIN SASL method."
378 (sieve-sasl-auth buffer
"PLAIN"))
380 (defun sieve-manage-login-p (buffer)
381 (sieve-manage-capability "SASL" "LOGIN" buffer
))
383 (defun sieve-manage-login-auth (buffer)
384 "Login to managesieve server using the LOGIN SASL method."
385 (sieve-sasl-auth buffer
"LOGIN"))
389 (defun sieve-manage-open (server &optional port stream auth buffer
)
390 "Open a network connection to a managesieve SERVER (string).
391 Optional argument PORT is port number (integer) on remote server.
392 Optional argument STREAM is any of `sieve-manage-streams' (a symbol).
393 Optional argument AUTH indicates authenticator to use, see
394 `sieve-manage-authenticators' for available authenticators.
395 If nil, chooses the best stream the server is capable of.
396 Optional argument BUFFER is buffer (buffer, or string naming buffer)
398 (or port
(setq port sieve-manage-default-port
))
399 (setq buffer
(or buffer
(format " *sieve* %s:%s" server port
)))
400 (with-current-buffer (get-buffer-create buffer
)
401 (mapc 'make-local-variable sieve-manage-local-variables
)
402 (sieve-manage-disable-multibyte)
403 (buffer-disable-undo)
404 (setq sieve-manage-server
(or server sieve-manage-server
))
405 (setq sieve-manage-port port
)
406 (setq sieve-manage-stream
(or stream sieve-manage-stream
))
407 (message "sieve: Connecting to %s..." sieve-manage-server
)
408 (if (let ((sieve-manage-stream
409 (or sieve-manage-stream sieve-manage-default-stream
)))
410 (sieve-manage-open-1 buffer
))
412 (let (stream-changed)
413 (message "sieve: Connecting to %s...done" sieve-manage-server
)
414 (when (null sieve-manage-stream
)
415 (let ((streams sieve-manage-streams
))
416 (while (setq stream
(pop streams
))
417 (if (funcall (nth 1 (assq stream
418 sieve-manage-stream-alist
)) buffer
)
420 (not (eq (or sieve-manage-stream
421 sieve-manage-default-stream
)
423 sieve-manage-stream stream
425 (unless sieve-manage-stream
426 (error "Couldn't figure out a stream for server"))))
428 (message "sieve: Reconnecting with stream `%s'..."
430 (sieve-manage-close buffer
)
431 (if (sieve-manage-open-1 buffer
)
432 (message "sieve: Reconnecting with stream `%s'...done"
434 (message "sieve: Reconnecting with stream `%s'...failed"
435 sieve-manage-stream
))
436 (setq sieve-manage-capability nil
))
437 (if (sieve-manage-opened buffer
)
438 ;; Choose authenticator
439 (when (and (null sieve-manage-auth
)
440 (not (eq sieve-manage-state
'auth
)))
441 (let ((auths sieve-manage-authenticators
))
442 (while (setq auth
(pop auths
))
443 (if (funcall (nth 1 (assq
445 sieve-manage-authenticator-alist
))
447 (setq sieve-manage-auth auth
449 (unless sieve-manage-auth
450 (error "Couldn't figure out authenticator for server"))))))
451 (message "sieve: Connecting to %s...failed" sieve-manage-server
))
452 (when (sieve-manage-opened buffer
)
456 (defun sieve-manage-authenticate (&optional buffer
)
457 "Authenticate on server in BUFFER.
458 Return `sieve-manage-state' value."
459 (with-current-buffer (or buffer
(current-buffer))
460 (if (eq sieve-manage-state
'nonauth
)
461 (when (funcall (nth 2 (assq sieve-manage-auth
462 sieve-manage-authenticator-alist
))
464 (setq sieve-manage-state
'auth
))
465 sieve-manage-state
)))
467 (defun sieve-manage-opened (&optional buffer
)
468 "Return non-nil if connection to managesieve server in BUFFER is open.
469 If BUFFER is nil then the current buffer is used."
470 (and (setq buffer
(get-buffer (or buffer
(current-buffer))))
471 (buffer-live-p buffer
)
472 (with-current-buffer buffer
473 (and sieve-manage-process
474 (memq (process-status sieve-manage-process
) '(open run
))))))
476 (defun sieve-manage-close (&optional buffer
)
477 "Close connection to managesieve server in BUFFER.
478 If BUFFER is nil, the current buffer is used."
479 (with-current-buffer (or buffer
(current-buffer))
480 (when (sieve-manage-opened)
481 (sieve-manage-send "LOGOUT")
483 (when (and sieve-manage-process
484 (memq (process-status sieve-manage-process
) '(open run
)))
485 (delete-process sieve-manage-process
))
486 (setq sieve-manage-process nil
)
490 (defun sieve-manage-capability (&optional name value buffer
)
491 "Check if capability NAME of server BUFFER match VALUE.
492 If it does, return the server value of NAME. If not returns nil.
493 If VALUE is nil, do not check VALUE and return server value.
494 If NAME is nil, return the full server list of capabilities."
495 (with-current-buffer (or buffer
(current-buffer))
497 sieve-manage-capability
498 (let ((server-value (cadr (assoc name sieve-manage-capability
))))
499 (when (or (null value
)
501 (string-match value server-value
)))
504 (defun sieve-manage-listscripts (&optional buffer
)
505 (with-current-buffer (or buffer
(current-buffer))
506 (sieve-manage-send "LISTSCRIPTS")
507 (sieve-manage-parse-listscripts)))
509 (defun sieve-manage-havespace (name size
&optional buffer
)
510 (with-current-buffer (or buffer
(current-buffer))
511 (sieve-manage-send (format "HAVESPACE \"%s\" %s" name size
))
512 (sieve-manage-parse-okno)))
514 (defun sieve-manage-putscript (name content
&optional buffer
)
515 (with-current-buffer (or buffer
(current-buffer))
516 (sieve-manage-send (format "PUTSCRIPT \"%s\" {%d+}%s%s" name
517 ;; Here we assume that the coding-system will
518 ;; replace each char with a single byte.
519 ;; This is always the case if `content' is
522 sieve-manage-client-eol content
))
523 (sieve-manage-parse-okno)))
525 (defun sieve-manage-deletescript (name &optional buffer
)
526 (with-current-buffer (or buffer
(current-buffer))
527 (sieve-manage-send (format "DELETESCRIPT \"%s\"" name
))
528 (sieve-manage-parse-okno)))
530 (defun sieve-manage-getscript (name output-buffer
&optional buffer
)
531 (with-current-buffer (or buffer
(current-buffer))
532 (sieve-manage-send (format "GETSCRIPT \"%s\"" name
))
533 (let ((script (sieve-manage-parse-string)))
534 (sieve-manage-parse-crlf)
535 (with-current-buffer output-buffer
537 (sieve-manage-parse-okno))))
539 (defun sieve-manage-setactive (name &optional buffer
)
540 (with-current-buffer (or buffer
(current-buffer))
541 (sieve-manage-send (format "SETACTIVE \"%s\"" name
))
542 (sieve-manage-parse-okno)))
544 ;; Protocol parsing routines
546 (defun sieve-manage-ok-p (rsp)
547 (string= (downcase (or (car-safe rsp
) "")) "ok"))
549 (defsubst sieve-manage-forward
()
550 (or (eobp) (forward-char)))
552 (defun sieve-manage-is-okno ()
553 (when (looking-at (concat
554 "^\\(OK\\|NO\\)\\( (\\([^)]+\\))\\)?\\( \\(.*\\)\\)?"
555 sieve-manage-server-eol
))
556 (let ((status (match-string 1))
557 (resp-code (match-string 3))
558 (response (match-string 5)))
560 (goto-char (match-beginning 5))
561 (setq response
(sieve-manage-is-string)))
562 (list status resp-code response
))))
564 (defun sieve-manage-parse-okno ()
567 (accept-process-output (get-buffer-process (current-buffer)) 1)
568 (goto-char (point-min))
569 (setq rsp
(sieve-manage-is-okno)))
573 (defun sieve-manage-parse-capability-1 ()
574 "Accept a managesieve greeting."
576 (while (setq str
(sieve-manage-is-string))
577 (if (eq (char-after) ?
)
579 (sieve-manage-forward)
580 (push (list str
(sieve-manage-is-string))
581 sieve-manage-capability
))
582 (push (list str
) sieve-manage-capability
))
584 (when (re-search-forward (concat "^OK.*" sieve-manage-server-eol
) nil t
)
585 (setq sieve-manage-state
'nonauth
)))
587 (defalias 'sieve-manage-parse-greeting-1
'sieve-manage-parse-capability-1
)
589 (defun sieve-manage-is-string ()
590 (cond ((looking-at "\"\\([^\"]+\\)\"")
593 (goto-char (match-end 0))))
594 ((looking-at (concat "{\\([0-9]+\\+?\\)}" sieve-manage-server-eol
))
595 (let ((pos (match-end 0))
596 (len (string-to-number (match-string 1))))
597 (if (< (point-max) (+ pos len
))
599 (goto-char (+ pos len
))
600 (buffer-substring pos
(+ pos len
)))))))
602 (defun sieve-manage-parse-string ()
605 (accept-process-output (get-buffer-process (current-buffer)) 1)
606 (goto-char (point-min))
607 (setq rsp
(sieve-manage-is-string)))
608 (sieve-manage-erase (point))
611 (defun sieve-manage-parse-crlf ()
612 (when (looking-at sieve-manage-server-eol
)
613 (sieve-manage-erase (match-end 0))))
615 (defun sieve-manage-parse-listscripts ()
618 (while (null (or (setq rsp
(sieve-manage-is-okno))
619 (setq tmp
(sieve-manage-is-string))))
620 (accept-process-output (get-buffer-process (current-buffer)) 1)
621 (goto-char (point-min)))
623 (while (not (looking-at (concat "\\( ACTIVE\\)?"
624 sieve-manage-server-eol
)))
625 (accept-process-output (get-buffer-process (current-buffer)) 1)
626 (goto-char (point-min)))
628 (push (cons 'active tmp
) data
)
630 (goto-char (match-end 0))
633 (if (sieve-manage-ok-p rsp
)
637 (defun sieve-manage-send (cmdstr)
638 (setq cmdstr
(concat cmdstr sieve-manage-client-eol
))
639 (and sieve-manage-log
640 (with-current-buffer (get-buffer-create sieve-manage-log
)
641 (sieve-manage-disable-multibyte)
642 (buffer-disable-undo)
643 (goto-char (point-max))
645 (process-send-string sieve-manage-process cmdstr
))
647 (provide 'sieve-manage
)
649 ;; sieve-manage.el ends here