copy-file now truncates output after writing
[emacs.git] / lisp / gnus / sieve-manage.el
blob72f22e76b8f864906156569b6ce15ebea0a787ae
1 ;;; sieve-manage.el --- Implementation of the managesieve protocol in elisp
3 ;; Copyright (C) 2001-2015 Free Software Foundation, Inc.
5 ;; Author: Simon Josefsson <simon@josefsson.org>
6 ;; Albert Krewinkel <tarleb@moltkeplatz.de>
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/>.
23 ;;; Commentary:
25 ;; This library provides an elisp API for the managesieve network
26 ;; protocol.
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")
59 ;; References:
61 ;; draft-martin-managesieve-02.txt,
62 ;; "A Protocol for Remotely Managing Sieve Scripts",
63 ;; by Tim Martin.
65 ;; Release history:
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.
70 ;; 2013-06-05 Enabled STARTTLS support, fixed bit rot.
72 ;;; Code:
74 (if (locate-library "password-cache")
75 (require 'password-cache)
76 (require 'password))
78 (eval-when-compile
79 (require 'cl) ; caddr
80 (require 'sasl)
81 (require 'starttls))
82 (autoload 'sasl-find-mechanism "sasl")
83 (autoload 'auth-source-search "auth-source")
85 ;; User customizable variables:
87 (defgroup sieve-manage nil
88 "Low-level Managesieve protocol issues."
89 :group 'mail
90 :prefix "sieve-")
92 (defcustom sieve-manage-log "*sieve-manage-log*"
93 "Name of buffer for managesieve session trace."
94 :type 'string
95 :group 'sieve-manage)
97 (defcustom sieve-manage-server-eol "\r\n"
98 "The EOL string sent from the server."
99 :type 'string
100 :group 'sieve-manage)
102 (defcustom sieve-manage-client-eol "\r\n"
103 "The EOL string we send to the server."
104 :type 'string
105 :group 'sieve-manage)
107 (defcustom sieve-manage-authenticators '(digest-md5
108 cram-md5
109 scram-md5
110 ntlm
111 plain
112 login)
113 "Priority of authenticators to consider when authenticating to server."
114 ;; FIXME Improve this. It's not `set'.
115 ;; It's like (repeat (choice (const ...))), where each choice can
116 ;; only appear once.
117 :type '(repeat symbol)
118 :group 'sieve-manage)
120 (defcustom sieve-manage-authenticator-alist
121 '((cram-md5 sieve-manage-cram-md5-p sieve-manage-cram-md5-auth)
122 (digest-md5 sieve-manage-digest-md5-p sieve-manage-digest-md5-auth)
123 (scram-md5 sieve-manage-scram-md5-p sieve-manage-scram-md5-auth)
124 (ntlm sieve-manage-ntlm-p sieve-manage-ntlm-auth)
125 (plain sieve-manage-plain-p sieve-manage-plain-auth)
126 (login sieve-manage-login-p sieve-manage-login-auth))
127 "Definition of authenticators.
129 \(NAME CHECK AUTHENTICATE)
131 NAME names the authenticator. CHECK is a function returning non-nil if
132 the server support the authenticator and AUTHENTICATE is a function
133 for doing the actual authentication."
134 :type '(repeat (list (symbol :tag "Name") (function :tag "Check function")
135 (function :tag "Authentication function")))
136 :group 'sieve-manage)
138 (defcustom sieve-manage-default-port "sieve"
139 "Default port number or service name for managesieve protocol."
140 :type '(choice integer string)
141 :version "24.4"
142 :group 'sieve-manage)
144 (defcustom sieve-manage-default-stream 'network
145 "Default stream type to use for `sieve-manage'."
146 :version "24.1"
147 :type 'symbol
148 :group 'sieve-manage)
150 ;; Internal variables:
152 (defconst sieve-manage-local-variables '(sieve-manage-server
153 sieve-manage-port
154 sieve-manage-auth
155 sieve-manage-stream
156 sieve-manage-process
157 sieve-manage-client-eol
158 sieve-manage-server-eol
159 sieve-manage-capability))
160 (defconst sieve-manage-coding-system-for-read 'binary)
161 (defconst sieve-manage-coding-system-for-write 'binary)
162 (defvar sieve-manage-stream nil)
163 (defvar sieve-manage-auth nil)
164 (defvar sieve-manage-server nil)
165 (defvar sieve-manage-port nil)
166 (defvar sieve-manage-state 'closed
167 "Managesieve state.
168 Valid states are `closed', `initial', `nonauth', and `auth'.")
169 (defvar sieve-manage-process nil)
170 (defvar sieve-manage-capability nil)
172 ;; Internal utility functions
173 (autoload 'mm-enable-multibyte "mm-util")
175 (defun sieve-manage-make-process-buffer ()
176 (with-current-buffer
177 (generate-new-buffer (format " *sieve %s:%s*"
178 sieve-manage-server
179 sieve-manage-port))
180 (mapc 'make-local-variable sieve-manage-local-variables)
181 (mm-enable-multibyte)
182 (buffer-disable-undo)
183 (current-buffer)))
185 (defun sieve-manage-erase (&optional p buffer)
186 (let ((buffer (or buffer (current-buffer))))
187 (and sieve-manage-log
188 (with-current-buffer (get-buffer-create sieve-manage-log)
189 (mm-enable-multibyte)
190 (buffer-disable-undo)
191 (goto-char (point-max))
192 (insert-buffer-substring buffer (with-current-buffer buffer
193 (point-min))
194 (or p (with-current-buffer buffer
195 (point-max)))))))
196 (delete-region (point-min) (or p (point-max))))
198 (defun sieve-manage-open-server (server port &optional stream buffer)
199 "Open network connection to SERVER on PORT.
200 Return the buffer associated with the connection."
201 (with-current-buffer buffer
202 (sieve-manage-erase)
203 (setq sieve-manage-state 'initial)
204 (destructuring-bind (proc . props)
205 (open-protocol-stream
206 "SIEVE" buffer server port
207 :type stream
208 :capability-command "CAPABILITY\r\n"
209 :end-of-command "^\\(OK\\|NO\\).*\n"
210 :success "^OK.*\n"
211 :return-list t
212 :starttls-function
213 (lambda (capabilities)
214 (when (string-match "\\bSTARTTLS\\b" capabilities)
215 "STARTTLS\r\n")))
216 (setq sieve-manage-process proc)
217 (setq sieve-manage-capability
218 (sieve-manage-parse-capability (plist-get props :capabilities)))
219 ;; Ignore new capabilities issues after successful STARTTLS
220 (when (and (memq stream '(nil network starttls))
221 (eq (plist-get props :type) 'tls))
222 (sieve-manage-drop-next-answer))
223 (current-buffer))))
225 ;; Authenticators
226 (defun sieve-sasl-auth (buffer mech)
227 "Login to server using the SASL MECH method."
228 (message "sieve: Authenticating using %s..." mech)
229 (with-current-buffer buffer
230 (let* ((auth-info (auth-source-search :host sieve-manage-server
231 :port "sieve"
232 :max 1
233 :create t))
234 (user-name (or (plist-get (nth 0 auth-info) :user) ""))
235 (user-password (or (plist-get (nth 0 auth-info) :secret) ""))
236 (user-password (if (functionp user-password)
237 (funcall user-password)
238 user-password))
239 (client (sasl-make-client (sasl-find-mechanism (list mech))
240 user-name "sieve" sieve-manage-server))
241 (sasl-read-passphrase
242 ;; We *need* to copy the password, because sasl will modify it
243 ;; somehow.
244 `(lambda (prompt) ,(copy-sequence user-password)))
245 (step (sasl-next-step client nil))
246 (tag (sieve-manage-send
247 (concat
248 "AUTHENTICATE \""
249 mech
250 "\""
251 (and (sasl-step-data step)
252 (concat
253 " \""
254 (base64-encode-string
255 (sasl-step-data step)
256 'no-line-break)
257 "\"")))))
258 data rsp)
259 (catch 'done
260 (while t
261 (setq rsp nil)
262 (goto-char (point-min))
263 (while (null (or (progn
264 (setq rsp (sieve-manage-is-string))
265 (if (not (and rsp (looking-at
266 sieve-manage-server-eol)))
267 (setq rsp nil)
268 (goto-char (match-end 0))
269 rsp))
270 (setq rsp (sieve-manage-is-okno))))
271 (accept-process-output sieve-manage-process 1)
272 (goto-char (point-min)))
273 (sieve-manage-erase)
274 (when (sieve-manage-ok-p rsp)
275 (when (and (cadr rsp)
276 (string-match "^SASL \"\\([^\"]+\\)\"" (cadr rsp)))
277 (sasl-step-set-data
278 step (base64-decode-string (match-string 1 (cadr rsp)))))
279 (if (and (setq step (sasl-next-step client step))
280 (setq data (sasl-step-data step)))
281 ;; We got data for server but it's finished
282 (error "Server not ready for SASL data: %s" data)
283 ;; The authentication process is finished.
284 (throw 'done t)))
285 (unless (stringp rsp)
286 (error "Server aborted SASL authentication: %s" (caddr rsp)))
287 (sasl-step-set-data step (base64-decode-string rsp))
288 (setq step (sasl-next-step client step))
289 (sieve-manage-send
290 (if (sasl-step-data step)
291 (concat "\""
292 (base64-encode-string (sasl-step-data step)
293 'no-line-break)
294 "\"")
295 ""))))
296 (message "sieve: Login using %s...done" mech))))
298 (defun sieve-manage-cram-md5-p (buffer)
299 (sieve-manage-capability "SASL" "CRAM-MD5" buffer))
301 (defun sieve-manage-cram-md5-auth (buffer)
302 "Login to managesieve server using the CRAM-MD5 SASL method."
303 (sieve-sasl-auth buffer "CRAM-MD5"))
305 (defun sieve-manage-digest-md5-p (buffer)
306 (sieve-manage-capability "SASL" "DIGEST-MD5" buffer))
308 (defun sieve-manage-digest-md5-auth (buffer)
309 "Login to managesieve server using the DIGEST-MD5 SASL method."
310 (sieve-sasl-auth buffer "DIGEST-MD5"))
312 (defun sieve-manage-scram-md5-p (buffer)
313 (sieve-manage-capability "SASL" "SCRAM-MD5" buffer))
315 (defun sieve-manage-scram-md5-auth (buffer)
316 "Login to managesieve server using the SCRAM-MD5 SASL method."
317 (sieve-sasl-auth buffer "SCRAM-MD5"))
319 (defun sieve-manage-ntlm-p (buffer)
320 (sieve-manage-capability "SASL" "NTLM" buffer))
322 (defun sieve-manage-ntlm-auth (buffer)
323 "Login to managesieve server using the NTLM SASL method."
324 (sieve-sasl-auth buffer "NTLM"))
326 (defun sieve-manage-plain-p (buffer)
327 (sieve-manage-capability "SASL" "PLAIN" buffer))
329 (defun sieve-manage-plain-auth (buffer)
330 "Login to managesieve server using the PLAIN SASL method."
331 (sieve-sasl-auth buffer "PLAIN"))
333 (defun sieve-manage-login-p (buffer)
334 (sieve-manage-capability "SASL" "LOGIN" buffer))
336 (defun sieve-manage-login-auth (buffer)
337 "Login to managesieve server using the LOGIN SASL method."
338 (sieve-sasl-auth buffer "LOGIN"))
340 ;; Managesieve API
342 (defun sieve-manage-open (server &optional port stream auth buffer)
343 "Open a network connection to a managesieve SERVER (string).
344 Optional argument PORT is port number (integer) on remote server.
345 Optional argument STREAM is any of `sieve-manage-streams' (a symbol).
346 Optional argument AUTH indicates authenticator to use, see
347 `sieve-manage-authenticators' for available authenticators.
348 If nil, chooses the best stream the server is capable of.
349 Optional argument BUFFER is buffer (buffer, or string naming buffer)
350 to work in."
351 (setq sieve-manage-port (or port sieve-manage-default-port))
352 (with-current-buffer (or buffer (sieve-manage-make-process-buffer))
353 (setq sieve-manage-server (or server
354 sieve-manage-server)
355 sieve-manage-stream (or stream
356 sieve-manage-stream
357 sieve-manage-default-stream)
358 sieve-manage-auth (or auth
359 sieve-manage-auth))
360 (message "sieve: Connecting to %s..." sieve-manage-server)
361 (sieve-manage-open-server sieve-manage-server
362 sieve-manage-port
363 sieve-manage-stream
364 (current-buffer))
365 (when (sieve-manage-opened (current-buffer))
366 ;; Choose authenticator
367 (when (and (null sieve-manage-auth)
368 (not (eq sieve-manage-state 'auth)))
369 (dolist (auth sieve-manage-authenticators)
370 (when (funcall (nth 1 (assq auth sieve-manage-authenticator-alist))
371 buffer)
372 (setq sieve-manage-auth auth)
373 (return)))
374 (unless sieve-manage-auth
375 (error "Couldn't figure out authenticator for server")))
376 (sieve-manage-erase)
377 (current-buffer))))
379 (defun sieve-manage-authenticate (&optional buffer)
380 "Authenticate on server in BUFFER.
381 Return `sieve-manage-state' value."
382 (with-current-buffer (or buffer (current-buffer))
383 (if (eq sieve-manage-state 'nonauth)
384 (when (funcall (nth 2 (assq sieve-manage-auth
385 sieve-manage-authenticator-alist))
386 (current-buffer))
387 (setq sieve-manage-state 'auth))
388 sieve-manage-state)))
390 (defun sieve-manage-opened (&optional buffer)
391 "Return non-nil if connection to managesieve server in BUFFER is open.
392 If BUFFER is nil then the current buffer is used."
393 (and (setq buffer (get-buffer (or buffer (current-buffer))))
394 (buffer-live-p buffer)
395 (with-current-buffer buffer
396 (and sieve-manage-process
397 (memq (process-status sieve-manage-process) '(open run))))))
399 (defun sieve-manage-close (&optional buffer)
400 "Close connection to managesieve server in BUFFER.
401 If BUFFER is nil, the current buffer is used."
402 (with-current-buffer (or buffer (current-buffer))
403 (when (sieve-manage-opened)
404 (sieve-manage-send "LOGOUT")
405 (sit-for 1))
406 (when (and sieve-manage-process
407 (memq (process-status sieve-manage-process) '(open run)))
408 (delete-process sieve-manage-process))
409 (setq sieve-manage-process nil)
410 (sieve-manage-erase)
413 (defun sieve-manage-capability (&optional name value buffer)
414 "Check if capability NAME of server BUFFER match VALUE.
415 If it does, return the server value of NAME. If not returns nil.
416 If VALUE is nil, do not check VALUE and return server value.
417 If NAME is nil, return the full server list of capabilities."
418 (with-current-buffer (or buffer (current-buffer))
419 (if (null name)
420 sieve-manage-capability
421 (let ((server-value (cadr (assoc name sieve-manage-capability))))
422 (when (or (null value)
423 (and server-value
424 (string-match value server-value)))
425 server-value)))))
427 (defun sieve-manage-listscripts (&optional buffer)
428 (with-current-buffer (or buffer (current-buffer))
429 (sieve-manage-send "LISTSCRIPTS")
430 (sieve-manage-parse-listscripts)))
432 (defun sieve-manage-havespace (name size &optional buffer)
433 (with-current-buffer (or buffer (current-buffer))
434 (sieve-manage-send (format "HAVESPACE \"%s\" %s" name size))
435 (sieve-manage-parse-okno)))
437 (defun sieve-manage-putscript (name content &optional buffer)
438 (with-current-buffer (or buffer (current-buffer))
439 (sieve-manage-send (format "PUTSCRIPT \"%s\" {%d+}%s%s" name
440 ;; Here we assume that the coding-system will
441 ;; replace each char with a single byte.
442 ;; This is always the case if `content' is
443 ;; a unibyte string.
444 (length content)
445 sieve-manage-client-eol content))
446 (sieve-manage-parse-okno)))
448 (defun sieve-manage-deletescript (name &optional buffer)
449 (with-current-buffer (or buffer (current-buffer))
450 (sieve-manage-send (format "DELETESCRIPT \"%s\"" name))
451 (sieve-manage-parse-okno)))
453 (defun sieve-manage-getscript (name output-buffer &optional buffer)
454 (with-current-buffer (or buffer (current-buffer))
455 (sieve-manage-send (format "GETSCRIPT \"%s\"" name))
456 (let ((script (sieve-manage-parse-string)))
457 (sieve-manage-parse-crlf)
458 (with-current-buffer output-buffer
459 (insert script))
460 (sieve-manage-parse-okno))))
462 (defun sieve-manage-setactive (name &optional buffer)
463 (with-current-buffer (or buffer (current-buffer))
464 (sieve-manage-send (format "SETACTIVE \"%s\"" name))
465 (sieve-manage-parse-okno)))
467 ;; Protocol parsing routines
469 (defun sieve-manage-wait-for-answer ()
470 (let ((pattern "^\\(OK\\|NO\\).*\n")
471 pos)
472 (while (not pos)
473 (setq pos (search-forward-regexp pattern nil t))
474 (goto-char (point-min))
475 (sleep-for 0 50))
476 pos))
478 (defun sieve-manage-drop-next-answer ()
479 (sieve-manage-wait-for-answer)
480 (sieve-manage-erase))
482 (defun sieve-manage-ok-p (rsp)
483 (string= (downcase (or (car-safe rsp) "")) "ok"))
485 (defun sieve-manage-is-okno ()
486 (when (looking-at (concat
487 "^\\(OK\\|NO\\)\\( (\\([^)]+\\))\\)?\\( \\(.*\\)\\)?"
488 sieve-manage-server-eol))
489 (let ((status (match-string 1))
490 (resp-code (match-string 3))
491 (response (match-string 5)))
492 (when response
493 (goto-char (match-beginning 5))
494 (setq response (sieve-manage-is-string)))
495 (list status resp-code response))))
497 (defun sieve-manage-parse-okno ()
498 (let (rsp)
499 (while (null rsp)
500 (accept-process-output (get-buffer-process (current-buffer)) 1)
501 (goto-char (point-min))
502 (setq rsp (sieve-manage-is-okno)))
503 (sieve-manage-erase)
504 rsp))
506 (defun sieve-manage-parse-capability (str)
507 "Parse managesieve capability string `STR'.
508 Set variable `sieve-manage-capability' to "
509 (let ((capas (delq nil
510 (mapcar #'split-string-and-unquote
511 (split-string str "\n")))))
512 (when (string= "OK" (caar (last capas)))
513 (setq sieve-manage-state 'nonauth))
514 capas))
516 (defun sieve-manage-is-string ()
517 (cond ((looking-at "\"\\([^\"]+\\)\"")
518 (prog1
519 (match-string 1)
520 (goto-char (match-end 0))))
521 ((looking-at (concat "{\\([0-9]+\\+?\\)}" sieve-manage-server-eol))
522 (let ((pos (match-end 0))
523 (len (string-to-number (match-string 1))))
524 (if (< (point-max) (+ pos len))
526 (goto-char (+ pos len))
527 (buffer-substring pos (+ pos len)))))))
529 (defun sieve-manage-parse-string ()
530 (let (rsp)
531 (while (null rsp)
532 (accept-process-output (get-buffer-process (current-buffer)) 1)
533 (goto-char (point-min))
534 (setq rsp (sieve-manage-is-string)))
535 (sieve-manage-erase (point))
536 rsp))
538 (defun sieve-manage-parse-crlf ()
539 (when (looking-at sieve-manage-server-eol)
540 (sieve-manage-erase (match-end 0))))
542 (defun sieve-manage-parse-listscripts ()
543 (let (tmp rsp data)
544 (while (null rsp)
545 (while (null (or (setq rsp (sieve-manage-is-okno))
546 (setq tmp (sieve-manage-is-string))))
547 (accept-process-output (get-buffer-process (current-buffer)) 1)
548 (goto-char (point-min)))
549 (when tmp
550 (while (not (looking-at (concat "\\( ACTIVE\\)?"
551 sieve-manage-server-eol)))
552 (accept-process-output (get-buffer-process (current-buffer)) 1)
553 (goto-char (point-min)))
554 (if (match-string 1)
555 (push (cons 'active tmp) data)
556 (push tmp data))
557 (goto-char (match-end 0))
558 (setq tmp nil)))
559 (sieve-manage-erase)
560 (if (sieve-manage-ok-p rsp)
561 data
562 rsp)))
564 (defun sieve-manage-send (cmdstr)
565 (setq cmdstr (concat cmdstr sieve-manage-client-eol))
566 (and sieve-manage-log
567 (with-current-buffer (get-buffer-create sieve-manage-log)
568 (mm-enable-multibyte)
569 (buffer-disable-undo)
570 (goto-char (point-max))
571 (insert cmdstr)))
572 (process-send-string sieve-manage-process cmdstr))
574 (provide 'sieve-manage)
576 ;; sieve-manage.el ends here