Fixes for NetBSD (Bug#4629, Bug#2072).
[emacs.git] / lisp / gnus / sieve-manage.el
blob4b78490bbe314ffc657cf77d2b3172e160a07473
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/>.
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-authenticate'
47 ;; `sieve-manage-listscripts'
48 ;; `sieve-manage-deletescript'
49 ;; `sieve-manage-getscript'
50 ;; performs managesieve protocol actions
52 ;; and that's it. Example of a managesieve session in *scratch*:
54 ;; (setq my-buf (sieve-manage-open "my.server.com"))
55 ;; " *sieve* my.server.com:2000*"
57 ;; (sieve-manage-authenticate "myusername" "mypassword" my-buf)
58 ;; 'auth
60 ;; (sieve-manage-listscripts my-buf)
61 ;; ("vacation" "testscript" ("splitmail") "badscript")
63 ;; References:
65 ;; draft-martin-managesieve-02.txt,
66 ;; "A Protocol for Remotely Managing Sieve Scripts",
67 ;; by Tim Martin.
69 ;; Release history:
71 ;; 2001-10-31 Committed to Oort Gnus.
72 ;; 2002-07-27 Added DELETESCRIPT. Suggested by Ned Ludd.
73 ;; 2002-08-03 Use SASL library.
75 ;;; Code:
77 ;; For Emacs < 22.2.
78 (eval-and-compile
79 (unless (fboundp 'declare-function) (defmacro declare-function (&rest r))))
81 (if (locate-library "password-cache")
82 (require 'password-cache)
83 (require 'password))
85 (eval-when-compile
86 (require 'sasl)
87 (require 'starttls))
88 (autoload 'sasl-find-mechanism "sasl")
89 (autoload 'starttls-open-stream "starttls")
91 ;; User customizable variables:
93 (defgroup sieve-manage nil
94 "Low-level Managesieve protocol issues."
95 :group 'mail
96 :prefix "sieve-")
98 (defcustom sieve-manage-log "*sieve-manage-log*"
99 "Name of buffer for managesieve session trace."
100 :type 'string
101 :group 'sieve-manage)
103 (defcustom sieve-manage-default-user (user-login-name)
104 "Default username to use."
105 :type 'string
106 :group 'sieve-manage)
108 (defcustom sieve-manage-server-eol "\r\n"
109 "The EOL string sent from the server."
110 :type 'string
111 :group 'sieve-manage)
113 (defcustom sieve-manage-client-eol "\r\n"
114 "The EOL string we send to the server."
115 :type 'string
116 :group 'sieve-manage)
118 (defcustom sieve-manage-streams '(network starttls shell)
119 "Priority of streams to consider when opening connection to server."
120 :group 'sieve-manage)
122 (defcustom sieve-manage-stream-alist
123 '((network sieve-manage-network-p sieve-manage-network-open)
124 (shell sieve-manage-shell-p sieve-manage-shell-open)
125 (starttls sieve-manage-starttls-p sieve-manage-starttls-open))
126 "Definition of network streams.
128 \(NAME CHECK OPEN)
130 NAME names the stream, CHECK is a function returning non-nil if the
131 server support the stream and OPEN is a function for opening the
132 stream."
133 :group 'sieve-manage)
135 (defcustom sieve-manage-authenticators '(digest-md5
136 cram-md5
137 scram-md5
138 ntlm
139 plain
140 login)
141 "Priority of authenticators to consider when authenticating to server."
142 :group 'sieve-manage)
144 (defcustom sieve-manage-authenticator-alist
145 '((cram-md5 sieve-manage-cram-md5-p sieve-manage-cram-md5-auth)
146 (digest-md5 sieve-manage-digest-md5-p sieve-manage-digest-md5-auth)
147 (scram-md5 sieve-manage-scram-md5-p sieve-manage-scram-md5-auth)
148 (ntlm sieve-manage-ntlm-p sieve-manage-ntlm-auth)
149 (plain sieve-manage-plain-p sieve-manage-plain-auth)
150 (login sieve-manage-login-p sieve-manage-login-auth))
151 "Definition of authenticators.
153 \(NAME CHECK AUTHENTICATE)
155 NAME names the authenticator. CHECK is a function returning non-nil if
156 the server support the authenticator and AUTHENTICATE is a function
157 for doing the actual authentication."
158 :group 'sieve-manage)
160 (defcustom sieve-manage-default-port 2000
161 "Default port number for managesieve protocol."
162 :type 'integer
163 :group 'sieve-manage)
165 ;; Internal variables:
167 (defconst sieve-manage-local-variables '(sieve-manage-server
168 sieve-manage-port
169 sieve-manage-auth
170 sieve-manage-stream
171 sieve-manage-username
172 sieve-manage-password
173 sieve-manage-process
174 sieve-manage-client-eol
175 sieve-manage-server-eol
176 sieve-manage-capability))
177 (defconst sieve-manage-default-stream 'network)
178 (defconst sieve-manage-coding-system-for-read 'binary)
179 (defconst sieve-manage-coding-system-for-write 'binary)
180 (defvar sieve-manage-stream nil)
181 (defvar sieve-manage-auth nil)
182 (defvar sieve-manage-server nil)
183 (defvar sieve-manage-port nil)
184 (defvar sieve-manage-username nil)
185 (defvar sieve-manage-password nil)
186 (defvar sieve-manage-state 'closed
187 "Managesieve state.
188 Valid states are `closed', `initial', `nonauth', and `auth'.")
189 (defvar sieve-manage-process nil)
190 (defvar sieve-manage-capability nil)
192 ;; Internal utility functions
194 (defsubst sieve-manage-disable-multibyte ()
195 "Enable multibyte in the current buffer."
196 (when (fboundp 'set-buffer-multibyte)
197 (set-buffer-multibyte nil)))
199 (declare-function password-read "password-cache" (prompt &optional key))
200 (declare-function password-cache-add "password-cache" (key password))
201 (declare-function password-cache-remove "password-cache" (key))
203 ;; Uses the dynamically bound `reason' variable.
204 (defvar reason)
205 (defun sieve-manage-interactive-login (buffer loginfunc)
206 "Login to server in BUFFER.
207 LOGINFUNC is passed a username and a password, it should return t if
208 it was successful authenticating itself to the server, nil otherwise.
209 Returns t if login was successful, nil otherwise."
210 (with-current-buffer buffer
211 (make-local-variable 'sieve-manage-username)
212 (make-local-variable 'sieve-manage-password)
213 (let (user passwd ret reason passwd-key)
214 (condition-case ()
215 (while (or (not user) (not passwd))
216 (setq user (or sieve-manage-username
217 (read-from-minibuffer
218 (concat "Managesieve username for "
219 sieve-manage-server ": ")
220 (or user sieve-manage-default-user)))
221 passwd-key (concat "managesieve:" user "@" sieve-manage-server
222 ":" sieve-manage-port)
223 passwd (or sieve-manage-password
224 (password-read (concat "Managesieve password for "
225 user "@" sieve-manage-server
226 ": ")
227 passwd-key)))
228 (when (y-or-n-p "Store password for this session? ")
229 (password-cache-add passwd-key (copy-sequence passwd)))
230 (when (and user passwd)
231 (if (funcall loginfunc user passwd)
232 (setq ret t
233 sieve-manage-username user)
234 (if reason
235 (message "Login failed (reason given: %s)..." reason)
236 (message "Login failed..."))
237 (password-cache-remove passwd-key)
238 (setq sieve-manage-password nil)
239 (setq passwd nil)
240 (setq reason nil)
241 (sit-for 1))))
242 (quit (with-current-buffer buffer
243 (password-cache-remove passwd-key)
244 (setq user nil
245 passwd nil
246 sieve-manage-password nil)))
247 (error (with-current-buffer buffer
248 (password-cache-remove passwd-key)
249 (setq user nil
250 passwd nil
251 sieve-manage-password nil))))
252 ret)))
254 (defun sieve-manage-erase (&optional p buffer)
255 (let ((buffer (or buffer (current-buffer))))
256 (and sieve-manage-log
257 (with-current-buffer (get-buffer-create sieve-manage-log)
258 (sieve-manage-disable-multibyte)
259 (buffer-disable-undo)
260 (goto-char (point-max))
261 (insert-buffer-substring buffer (with-current-buffer buffer
262 (point-min))
263 (or p (with-current-buffer buffer
264 (point-max)))))))
265 (delete-region (point-min) (or p (point-max))))
267 (defun sieve-manage-open-1 (buffer)
268 (with-current-buffer buffer
269 (sieve-manage-erase)
270 (setq sieve-manage-state 'initial
271 sieve-manage-process
272 (condition-case ()
273 (funcall (nth 2 (assq sieve-manage-stream
274 sieve-manage-stream-alist))
275 "sieve" buffer sieve-manage-server sieve-manage-port)
276 ((error quit) nil)))
277 (when sieve-manage-process
278 (while (and (eq sieve-manage-state 'initial)
279 (memq (process-status sieve-manage-process) '(open run)))
280 (message "Waiting for response from %s..." sieve-manage-server)
281 (accept-process-output sieve-manage-process 1))
282 (message "Waiting for response from %s...done" sieve-manage-server)
283 (and (memq (process-status sieve-manage-process) '(open run))
284 sieve-manage-process))))
286 ;; Streams
288 (defun sieve-manage-network-p (buffer)
291 (defun sieve-manage-network-open (name buffer server port)
292 (let* ((port (or port sieve-manage-default-port))
293 (coding-system-for-read sieve-manage-coding-system-for-read)
294 (coding-system-for-write sieve-manage-coding-system-for-write)
295 (process (open-network-stream name buffer server port)))
296 (when process
297 (while (and (memq (process-status process) '(open run))
298 (set-buffer buffer) ;; XXX "blue moon" nntp.el bug
299 (goto-char (point-min))
300 (not (sieve-manage-parse-greeting-1)))
301 (accept-process-output process 1)
302 (sit-for 1))
303 (sieve-manage-erase nil buffer)
304 (when (memq (process-status process) '(open run))
305 process))))
307 (defun sieve-manage-starttls-p (buffer)
308 (condition-case ()
309 (progn
310 (require 'starttls)
311 (call-process "starttls"))
312 (error nil)))
314 (defun sieve-manage-starttls-open (name buffer server port)
315 (let* ((port (or port sieve-manage-default-port))
316 (coding-system-for-read sieve-manage-coding-system-for-read)
317 (coding-system-for-write sieve-manage-coding-system-for-write)
318 (process (starttls-open-stream name buffer server port))
319 done)
320 (when process
321 (while (and (memq (process-status process) '(open run))
322 (set-buffer buffer) ;; XXX "blue moon" nntp.el bug
323 (goto-char (point-min))
324 (not (sieve-manage-parse-greeting-1)))
325 (accept-process-output process 1)
326 (sit-for 1))
327 (sieve-manage-erase nil buffer)
328 (sieve-manage-send "STARTTLS")
329 (starttls-negotiate process))
330 (when (memq (process-status process) '(open run))
331 process)))
333 ;; Authenticators
335 (defun sieve-sasl-auth (buffer mech)
336 "Login to server using the SASL MECH method."
337 (message "sieve: Authenticating using %s..." mech)
338 (if (sieve-manage-interactive-login
339 buffer
340 (lambda (user passwd)
341 (let (client step tag data rsp)
342 (setq client (sasl-make-client (sasl-find-mechanism (list mech))
343 user "sieve" sieve-manage-server))
344 (setq sasl-read-passphrase (function (lambda (prompt) passwd)))
345 (setq step (sasl-next-step client nil))
346 (setq tag
347 (sieve-manage-send
348 (concat
349 "AUTHENTICATE \""
350 mech
351 "\""
352 (and (sasl-step-data step)
353 (concat
354 " \""
355 (base64-encode-string
356 (sasl-step-data step)
357 'no-line-break)
358 "\"")))))
359 (catch 'done
360 (while t
361 (setq rsp nil)
362 (goto-char (point-min))
363 (while (null (or (progn
364 (setq rsp (sieve-manage-is-string))
365 (if (not (and rsp (looking-at
366 sieve-manage-server-eol)))
367 (setq rsp nil)
368 (goto-char (match-end 0))
369 rsp))
370 (setq rsp (sieve-manage-is-okno))))
371 (accept-process-output sieve-manage-process 1)
372 (goto-char (point-min)))
373 (sieve-manage-erase)
374 (when (sieve-manage-ok-p rsp)
375 (when (string-match "^SASL \"\\([^\"]+\\)\"" (cadr rsp))
376 (sasl-step-set-data
377 step (base64-decode-string (match-string 1 (cadr rsp)))))
378 (if (and (setq step (sasl-next-step client step))
379 (setq data (sasl-step-data step)))
380 ;; We got data for server but it's finished
381 (error "Server not ready for SASL data: %s" data)
382 ;; The authentication process is finished.
383 (throw 'done t)))
384 (unless (stringp rsp)
385 (apply 'error "Server aborted SASL authentication: %s %s %s"
386 rsp))
387 (sasl-step-set-data step (base64-decode-string rsp))
388 (setq step (sasl-next-step client step))
389 (sieve-manage-send
390 (if (sasl-step-data step)
391 (concat "\""
392 (base64-encode-string (sasl-step-data step)
393 'no-line-break)
394 "\"")
395 "")))))))
396 (message "sieve: Authenticating using %s...done" mech)
397 (message "sieve: Authenticating using %s...failed" mech)))
399 (defun sieve-manage-cram-md5-p (buffer)
400 (sieve-manage-capability "SASL" "CRAM-MD5" buffer))
402 (defun sieve-manage-cram-md5-auth (buffer)
403 "Login to managesieve server using the CRAM-MD5 SASL method."
404 (sieve-sasl-auth buffer "CRAM-MD5"))
406 (defun sieve-manage-digest-md5-p (buffer)
407 (sieve-manage-capability "SASL" "DIGEST-MD5" buffer))
409 (defun sieve-manage-digest-md5-auth (buffer)
410 "Login to managesieve server using the DIGEST-MD5 SASL method."
411 (sieve-sasl-auth buffer "DIGEST-MD5"))
413 (defun sieve-manage-scram-md5-p (buffer)
414 (sieve-manage-capability "SASL" "SCRAM-MD5" buffer))
416 (defun sieve-manage-scram-md5-auth (buffer)
417 "Login to managesieve server using the SCRAM-MD5 SASL method."
418 (sieve-sasl-auth buffer "SCRAM-MD5"))
420 (defun sieve-manage-ntlm-p (buffer)
421 (sieve-manage-capability "SASL" "NTLM" buffer))
423 (defun sieve-manage-ntlm-auth (buffer)
424 "Login to managesieve server using the NTLM SASL method."
425 (sieve-sasl-auth buffer "NTLM"))
427 (defun sieve-manage-plain-p (buffer)
428 (sieve-manage-capability "SASL" "PLAIN" buffer))
430 (defun sieve-manage-plain-auth (buffer)
431 "Login to managesieve server using the PLAIN SASL method."
432 (sieve-sasl-auth buffer "PLAIN"))
434 (defun sieve-manage-login-p (buffer)
435 (sieve-manage-capability "SASL" "LOGIN" buffer))
437 (defun sieve-manage-login-auth (buffer)
438 "Login to managesieve server using the LOGIN SASL method."
439 (sieve-sasl-auth buffer "LOGIN"))
441 ;; Managesieve API
443 (defun sieve-manage-open (server &optional port stream auth buffer)
444 "Open a network connection to a managesieve SERVER (string).
445 Optional argument PORT is port number (integer) on remote server.
446 Optional argument STREAM is any of `sieve-manage-streams' (a symbol).
447 Optional argument AUTH indicates authenticator to use, see
448 `sieve-manage-authenticators' for available authenticators.
449 If nil, chooses the best stream the server is capable of.
450 Optional argument BUFFER is buffer (buffer, or string naming buffer)
451 to work in."
452 (setq buffer (or buffer (format " *sieve* %s:%d" server (or port 2000))))
453 (with-current-buffer (get-buffer-create buffer)
454 (mapc 'make-local-variable sieve-manage-local-variables)
455 (sieve-manage-disable-multibyte)
456 (buffer-disable-undo)
457 (setq sieve-manage-server (or server sieve-manage-server))
458 (setq sieve-manage-port (or port sieve-manage-port))
459 (setq sieve-manage-stream (or stream sieve-manage-stream))
460 (message "sieve: Connecting to %s..." sieve-manage-server)
461 (if (let ((sieve-manage-stream
462 (or sieve-manage-stream sieve-manage-default-stream)))
463 (sieve-manage-open-1 buffer))
464 ;; Choose stream.
465 (let (stream-changed)
466 (message "sieve: Connecting to %s...done" sieve-manage-server)
467 (when (null sieve-manage-stream)
468 (let ((streams sieve-manage-streams))
469 (while (setq stream (pop streams))
470 (if (funcall (nth 1 (assq stream
471 sieve-manage-stream-alist)) buffer)
472 (setq stream-changed
473 (not (eq (or sieve-manage-stream
474 sieve-manage-default-stream)
475 stream))
476 sieve-manage-stream stream
477 streams nil)))
478 (unless sieve-manage-stream
479 (error "Couldn't figure out a stream for server"))))
480 (when stream-changed
481 (message "sieve: Reconnecting with stream `%s'..."
482 sieve-manage-stream)
483 (sieve-manage-close buffer)
484 (if (sieve-manage-open-1 buffer)
485 (message "sieve: Reconnecting with stream `%s'...done"
486 sieve-manage-stream)
487 (message "sieve: Reconnecting with stream `%s'...failed"
488 sieve-manage-stream))
489 (setq sieve-manage-capability nil))
490 (if (sieve-manage-opened buffer)
491 ;; Choose authenticator
492 (when (and (null sieve-manage-auth)
493 (not (eq sieve-manage-state 'auth)))
494 (let ((auths sieve-manage-authenticators))
495 (while (setq auth (pop auths))
496 (if (funcall (nth 1 (assq
497 auth
498 sieve-manage-authenticator-alist))
499 buffer)
500 (setq sieve-manage-auth auth
501 auths nil)))
502 (unless sieve-manage-auth
503 (error "Couldn't figure out authenticator for server"))))))
504 (message "sieve: Connecting to %s...failed" sieve-manage-server))
505 (when (sieve-manage-opened buffer)
506 (sieve-manage-erase)
507 buffer)))
509 (defun sieve-manage-opened (&optional buffer)
510 "Return non-nil if connection to managesieve server in BUFFER is open.
511 If BUFFER is nil then the current buffer is used."
512 (and (setq buffer (get-buffer (or buffer (current-buffer))))
513 (buffer-live-p buffer)
514 (with-current-buffer buffer
515 (and sieve-manage-process
516 (memq (process-status sieve-manage-process) '(open run))))))
518 (defun sieve-manage-close (&optional buffer)
519 "Close connection to managesieve server in BUFFER.
520 If BUFFER is nil, the current buffer is used."
521 (with-current-buffer (or buffer (current-buffer))
522 (when (sieve-manage-opened)
523 (sieve-manage-send "LOGOUT")
524 (sit-for 1))
525 (when (and sieve-manage-process
526 (memq (process-status sieve-manage-process) '(open run)))
527 (delete-process sieve-manage-process))
528 (setq sieve-manage-process nil)
529 (sieve-manage-erase)
532 (defun sieve-manage-authenticate (&optional user passwd buffer)
533 "Authenticate to server in BUFFER, using current buffer if nil.
534 It uses the authenticator specified when opening the server. If the
535 authenticator requires username/passwords, they are queried from the
536 user and optionally stored in the buffer. If USER and/or PASSWD is
537 specified, the user will not be questioned and the username and/or
538 password is remembered in the buffer."
539 (with-current-buffer (or buffer (current-buffer))
540 (if (not (eq sieve-manage-state 'nonauth))
541 (eq sieve-manage-state 'auth)
542 (make-local-variable 'sieve-manage-username)
543 (make-local-variable 'sieve-manage-password)
544 (if user (setq sieve-manage-username user))
545 (if passwd (setq sieve-manage-password passwd))
546 (if (funcall (nth 2 (assq sieve-manage-auth
547 sieve-manage-authenticator-alist)) buffer)
548 (setq sieve-manage-state 'auth)))))
550 (defun sieve-manage-capability (&optional name value buffer)
551 (with-current-buffer (or buffer (current-buffer))
552 (if (null name)
553 sieve-manage-capability
554 (if (null value)
555 (nth 1 (assoc name sieve-manage-capability))
556 (when (string-match value (nth 1 (assoc name sieve-manage-capability)))
557 (nth 1 (assoc name sieve-manage-capability)))))))
559 (defun sieve-manage-listscripts (&optional buffer)
560 (with-current-buffer (or buffer (current-buffer))
561 (sieve-manage-send "LISTSCRIPTS")
562 (sieve-manage-parse-listscripts)))
564 (defun sieve-manage-havespace (name size &optional buffer)
565 (with-current-buffer (or buffer (current-buffer))
566 (sieve-manage-send (format "HAVESPACE \"%s\" %s" name size))
567 (sieve-manage-parse-okno)))
569 (defun sieve-manage-putscript (name content &optional buffer)
570 (with-current-buffer (or buffer (current-buffer))
571 (sieve-manage-send (format "PUTSCRIPT \"%s\" {%d+}%s%s" name
572 ;; Here we assume that the coding-system will
573 ;; replace each char with a single byte.
574 ;; This is always the case if `content' is
575 ;; a unibyte string.
576 (length content)
577 sieve-manage-client-eol content))
578 (sieve-manage-parse-okno)))
580 (defun sieve-manage-deletescript (name &optional buffer)
581 (with-current-buffer (or buffer (current-buffer))
582 (sieve-manage-send (format "DELETESCRIPT \"%s\"" name))
583 (sieve-manage-parse-okno)))
585 (defun sieve-manage-getscript (name output-buffer &optional buffer)
586 (with-current-buffer (or buffer (current-buffer))
587 (sieve-manage-send (format "GETSCRIPT \"%s\"" name))
588 (let ((script (sieve-manage-parse-string)))
589 (sieve-manage-parse-crlf)
590 (with-current-buffer output-buffer
591 (insert script))
592 (sieve-manage-parse-okno))))
594 (defun sieve-manage-setactive (name &optional buffer)
595 (with-current-buffer (or buffer (current-buffer))
596 (sieve-manage-send (format "SETACTIVE \"%s\"" name))
597 (sieve-manage-parse-okno)))
599 ;; Protocol parsing routines
601 (defun sieve-manage-ok-p (rsp)
602 (string= (downcase (or (car-safe rsp) "")) "ok"))
604 (defsubst sieve-manage-forward ()
605 (or (eobp) (forward-char)))
607 (defun sieve-manage-is-okno ()
608 (when (looking-at (concat
609 "^\\(OK\\|NO\\)\\( (\\([^)]+\\))\\)?\\( \\(.*\\)\\)?"
610 sieve-manage-server-eol))
611 (let ((status (match-string 1))
612 (resp-code (match-string 3))
613 (response (match-string 5)))
614 (when response
615 (goto-char (match-beginning 5))
616 (setq response (sieve-manage-is-string)))
617 (list status resp-code response))))
619 (defun sieve-manage-parse-okno ()
620 (let (rsp)
621 (while (null rsp)
622 (accept-process-output (get-buffer-process (current-buffer)) 1)
623 (goto-char (point-min))
624 (setq rsp (sieve-manage-is-okno)))
625 (sieve-manage-erase)
626 rsp))
628 (defun sieve-manage-parse-capability-1 ()
629 "Accept a managesieve greeting."
630 (let (str)
631 (while (setq str (sieve-manage-is-string))
632 (if (eq (char-after) ? )
633 (progn
634 (sieve-manage-forward)
635 (push (list str (sieve-manage-is-string))
636 sieve-manage-capability))
637 (push (list str) sieve-manage-capability))
638 (forward-line)))
639 (when (re-search-forward (concat "^OK.*" sieve-manage-server-eol) nil t)
640 (setq sieve-manage-state 'nonauth)))
642 (defalias 'sieve-manage-parse-greeting-1 'sieve-manage-parse-capability-1)
644 (defun sieve-manage-is-string ()
645 (cond ((looking-at "\"\\([^\"]+\\)\"")
646 (prog1
647 (match-string 1)
648 (goto-char (match-end 0))))
649 ((looking-at (concat "{\\([0-9]+\\+?\\)}" sieve-manage-server-eol))
650 (let ((pos (match-end 0))
651 (len (string-to-number (match-string 1))))
652 (if (< (point-max) (+ pos len))
654 (goto-char (+ pos len))
655 (buffer-substring pos (+ pos len)))))))
657 (defun sieve-manage-parse-string ()
658 (let (rsp)
659 (while (null rsp)
660 (accept-process-output (get-buffer-process (current-buffer)) 1)
661 (goto-char (point-min))
662 (setq rsp (sieve-manage-is-string)))
663 (sieve-manage-erase (point))
664 rsp))
666 (defun sieve-manage-parse-crlf ()
667 (when (looking-at sieve-manage-server-eol)
668 (sieve-manage-erase (match-end 0))))
670 (defun sieve-manage-parse-listscripts ()
671 (let (tmp rsp data)
672 (while (null rsp)
673 (while (null (or (setq rsp (sieve-manage-is-okno))
674 (setq tmp (sieve-manage-is-string))))
675 (accept-process-output (get-buffer-process (current-buffer)) 1)
676 (goto-char (point-min)))
677 (when tmp
678 (while (not (looking-at (concat "\\( ACTIVE\\)?"
679 sieve-manage-server-eol)))
680 (accept-process-output (get-buffer-process (current-buffer)) 1)
681 (goto-char (point-min)))
682 (if (match-string 1)
683 (push (cons 'active tmp) data)
684 (push tmp data))
685 (goto-char (match-end 0))
686 (setq tmp nil)))
687 (sieve-manage-erase)
688 (if (sieve-manage-ok-p rsp)
689 data
690 rsp)))
692 (defun sieve-manage-send (cmdstr)
693 (setq cmdstr (concat cmdstr sieve-manage-client-eol))
694 (and sieve-manage-log
695 (with-current-buffer (get-buffer-create sieve-manage-log)
696 (sieve-manage-disable-multibyte)
697 (buffer-disable-undo)
698 (goto-char (point-max))
699 (insert cmdstr)))
700 (process-send-string sieve-manage-process cmdstr))
702 (provide 'sieve-manage)
704 ;; arch-tag: 321c4640-1371-4495-9baf-8ccb71dd5bd1
705 ;; sieve-manage.el ends here