88155
|
1 ;;; sieve-manage.el --- Implementation of the managesive protocol in elisp
|
|
2
|
|
3 ;; Copyright (C) 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
|
|
4
|
|
5 ;; Author: Simon Josefsson <simon@josefsson.org>
|
|
6
|
|
7 ;; This file is part of GNU Emacs.
|
|
8
|
|
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 2, or (at your option)
|
|
12 ;; any later version.
|
|
13
|
|
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.
|
|
18
|
|
19 ;; You should have received a copy of the GNU General Public License
|
|
20 ;; along with GNU Emacs; see the file COPYING. If not, write to the
|
|
21 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
|
22 ;; Boston, MA 02110-1301, USA.
|
|
23
|
|
24 ;;; Commentary:
|
|
25
|
|
26 ;; This library provides an elisp API for the managesieve network
|
|
27 ;; protocol.
|
|
28 ;;
|
|
29 ;; Currently only the CRAM-MD5 authentication mechanism is supported.
|
|
30 ;;
|
|
31 ;; The API should be fairly obvious for anyone familiar with the
|
|
32 ;; managesieve protocol, interface functions include:
|
|
33 ;;
|
|
34 ;; `sieve-manage-open'
|
|
35 ;; open connection to managesieve server, returning a buffer to be
|
|
36 ;; used by all other API functions.
|
|
37 ;;
|
|
38 ;; `sieve-manage-opened'
|
|
39 ;; check if a server is open or not
|
|
40 ;;
|
|
41 ;; `sieve-manage-close'
|
|
42 ;; close a server connection.
|
|
43 ;;
|
|
44 ;; `sieve-manage-authenticate'
|
|
45 ;; `sieve-manage-listscripts'
|
|
46 ;; `sieve-manage-deletescript'
|
|
47 ;; `sieve-manage-getscript'
|
|
48 ;; performs managesieve protocol actions
|
|
49 ;;
|
|
50 ;; and that's it. Example of a managesieve session in *scratch*:
|
|
51 ;;
|
|
52 ;; (setq my-buf (sieve-manage-open "my.server.com"))
|
|
53 ;; " *sieve* my.server.com:2000*"
|
|
54 ;;
|
|
55 ;; (sieve-manage-authenticate "myusername" "mypassword" my-buf)
|
|
56 ;; 'auth
|
|
57 ;;
|
|
58 ;; (sieve-manage-listscripts my-buf)
|
|
59 ;; ("vacation" "testscript" ("splitmail") "badscript")
|
|
60 ;;
|
|
61 ;; References:
|
|
62 ;;
|
|
63 ;; draft-martin-managesieve-02.txt,
|
|
64 ;; "A Protocol for Remotely Managing Sieve Scripts",
|
|
65 ;; by Tim Martin.
|
|
66 ;;
|
|
67 ;; Release history:
|
|
68 ;;
|
|
69 ;; 2001-10-31 Committed to Oort Gnus.
|
|
70 ;; 2002-07-27 Added DELETESCRIPT. Suggested by Ned Ludd.
|
|
71
|
|
72 ;;; Code:
|
|
73
|
|
74 (require 'rfc2104)
|
|
75 (or (fboundp 'md5)
|
|
76 (require 'md5))
|
|
77 (eval-and-compile
|
|
78 (autoload 'starttls-open-stream "starttls")
|
|
79 (autoload 'starttls-negotiate "starttls"))
|
|
80
|
|
81 ;; User customizable variables:
|
|
82
|
|
83 (defgroup sieve-manage nil
|
|
84 "Low-level Managesieve protocol issues."
|
|
85 :group 'mail
|
|
86 :prefix "sieve-")
|
|
87
|
|
88 (defcustom sieve-manage-log "*sieve-manage-log*"
|
|
89 "Name of buffer for managesieve session trace."
|
|
90 :type 'string
|
|
91 :group 'sieve-manage)
|
|
92
|
|
93 (defcustom sieve-manage-default-user (user-login-name)
|
|
94 "Default username to use."
|
|
95 :type 'string
|
|
96 :group 'sieve-manage)
|
|
97
|
|
98 (defcustom sieve-manage-server-eol "\r\n"
|
|
99 "The EOL string sent from the server."
|
|
100 :type 'string
|
|
101 :group 'sieve-manage)
|
|
102
|
|
103 (defcustom sieve-manage-client-eol "\r\n"
|
|
104 "The EOL string we send to the server."
|
|
105 :type 'string
|
|
106 :group 'sieve-manage)
|
|
107
|
|
108 (defcustom sieve-manage-streams '(network starttls shell)
|
|
109 "Priority of streams to consider when opening connection to server."
|
|
110 :group 'sieve-manage)
|
|
111
|
|
112 (defcustom sieve-manage-stream-alist
|
|
113 '((network sieve-manage-network-p sieve-manage-network-open)
|
|
114 (shell sieve-manage-shell-p sieve-manage-shell-open)
|
|
115 (starttls sieve-manage-starttls-p sieve-manage-starttls-open))
|
|
116 "Definition of network streams.
|
|
117
|
|
118 \(NAME CHECK OPEN)
|
|
119
|
|
120 NAME names the stream, CHECK is a function returning non-nil if the
|
|
121 server support the stream and OPEN is a function for opening the
|
|
122 stream."
|
|
123 :group 'sieve-manage)
|
|
124
|
|
125 (defcustom sieve-manage-authenticators '(cram-md5 plain)
|
|
126 "Priority of authenticators to consider when authenticating to server."
|
|
127 :group 'sieve-manage)
|
|
128
|
|
129 (defcustom sieve-manage-authenticator-alist
|
|
130 '((cram-md5 sieve-manage-cram-md5-p sieve-manage-cram-md5-auth)
|
|
131 (plain sieve-manage-plain-p sieve-manage-plain-auth))
|
|
132 "Definition of authenticators.
|
|
133
|
|
134 \(NAME CHECK AUTHENTICATE)
|
|
135
|
|
136 NAME names the authenticator. CHECK is a function returning non-nil if
|
|
137 the server support the authenticator and AUTHENTICATE is a function
|
|
138 for doing the actual authentication."
|
|
139 :group 'sieve-manage)
|
|
140
|
|
141 (defcustom sieve-manage-default-port 2000
|
|
142 "Default port number for managesieve protocol."
|
|
143 :type 'integer
|
|
144 :group 'sieve-manage)
|
|
145
|
|
146 ;; Internal variables:
|
|
147
|
|
148 (defconst sieve-manage-local-variables '(sieve-manage-server
|
|
149 sieve-manage-port
|
|
150 sieve-manage-auth
|
|
151 sieve-manage-stream
|
|
152 sieve-manage-username
|
|
153 sieve-manage-password
|
|
154 sieve-manage-process
|
|
155 sieve-manage-client-eol
|
|
156 sieve-manage-server-eol
|
|
157 sieve-manage-capability))
|
|
158 (defconst sieve-manage-default-stream 'network)
|
|
159 (defconst sieve-manage-coding-system-for-read 'binary)
|
|
160 (defconst sieve-manage-coding-system-for-write 'binary)
|
|
161 (defvar sieve-manage-stream nil)
|
|
162 (defvar sieve-manage-auth nil)
|
|
163 (defvar sieve-manage-server nil)
|
|
164 (defvar sieve-manage-port nil)
|
|
165 (defvar sieve-manage-username nil)
|
|
166 (defvar sieve-manage-password nil)
|
|
167 (defvar sieve-manage-state 'closed
|
|
168 "Managesieve state.
|
|
169 Valid states are `closed', `initial', `nonauth', and `auth'.")
|
|
170 (defvar sieve-manage-process nil)
|
|
171 (defvar sieve-manage-capability nil)
|
|
172
|
|
173 ;; Internal utility functions
|
|
174
|
|
175 (defsubst sieve-manage-disable-multibyte ()
|
|
176 "Enable multibyte in the current buffer."
|
|
177 (when (fboundp 'set-buffer-multibyte)
|
|
178 (set-buffer-multibyte nil)))
|
|
179
|
|
180 ;; Uses the dynamically bound `reason' variable.
|
|
181 (defvar reason)
|
|
182 (defun sieve-manage-interactive-login (buffer loginfunc)
|
|
183 "Login to server in BUFFER.
|
|
184 LOGINFUNC is passed a username and a password, it should return t if
|
|
185 it where sucessful authenticating itself to the server, nil otherwise.
|
|
186 Returns t if login was successful, nil otherwise."
|
|
187 (with-current-buffer buffer
|
|
188 (make-local-variable 'sieve-manage-username)
|
|
189 (make-local-variable 'sieve-manage-password)
|
|
190 (let (user passwd ret reason)
|
|
191 ;; (condition-case ()
|
|
192 (while (or (not user) (not passwd))
|
|
193 (setq user (or sieve-manage-username
|
|
194 (read-from-minibuffer
|
|
195 (concat "Managesieve username for "
|
|
196 sieve-manage-server ": ")
|
|
197 (or user sieve-manage-default-user))))
|
|
198 (setq passwd (or sieve-manage-password
|
|
199 (read-passwd
|
|
200 (concat "Managesieve password for " user "@"
|
|
201 sieve-manage-server ": "))))
|
|
202 (when (and user passwd)
|
|
203 (if (funcall loginfunc user passwd)
|
|
204 (progn
|
|
205 (setq ret t
|
|
206 sieve-manage-username user)
|
|
207 (if (and (not sieve-manage-password)
|
|
208 (y-or-n-p "Store password for this session? "))
|
|
209 (setq sieve-manage-password passwd)))
|
|
210 (if reason
|
|
211 (message "Login failed (reason given: %s)..." reason)
|
|
212 (message "Login failed..."))
|
|
213 (setq reason nil)
|
|
214 (setq passwd nil)
|
|
215 (sit-for 1))))
|
|
216 ;; (quit (with-current-buffer buffer
|
|
217 ;; (setq user nil
|
|
218 ;; passwd nil)))
|
|
219 ;; (error (with-current-buffer buffer
|
|
220 ;; (setq user nil
|
|
221 ;; passwd nil))))
|
|
222 ret)))
|
|
223
|
|
224 (defun sieve-manage-erase (&optional p buffer)
|
|
225 (let ((buffer (or buffer (current-buffer))))
|
|
226 (and sieve-manage-log
|
|
227 (with-current-buffer (get-buffer-create sieve-manage-log)
|
|
228 (sieve-manage-disable-multibyte)
|
|
229 (buffer-disable-undo)
|
|
230 (goto-char (point-max))
|
|
231 (insert-buffer-substring buffer (with-current-buffer buffer
|
|
232 (point-min))
|
|
233 (or p (with-current-buffer buffer
|
|
234 (point-max)))))))
|
|
235 (delete-region (point-min) (or p (point-max))))
|
|
236
|
|
237 (defun sieve-manage-open-1 (buffer)
|
|
238 (with-current-buffer buffer
|
|
239 (sieve-manage-erase)
|
|
240 (setq sieve-manage-state 'initial
|
|
241 sieve-manage-process
|
|
242 (condition-case ()
|
|
243 (funcall (nth 2 (assq sieve-manage-stream
|
|
244 sieve-manage-stream-alist))
|
|
245 "sieve" buffer sieve-manage-server sieve-manage-port)
|
|
246 ((error quit) nil)))
|
|
247 (when sieve-manage-process
|
|
248 (while (and (eq sieve-manage-state 'initial)
|
|
249 (memq (process-status sieve-manage-process) '(open run)))
|
|
250 (message "Waiting for response from %s..." sieve-manage-server)
|
|
251 (accept-process-output sieve-manage-process 1))
|
|
252 (message "Waiting for response from %s...done" sieve-manage-server)
|
|
253 (and (memq (process-status sieve-manage-process) '(open run))
|
|
254 sieve-manage-process))))
|
|
255
|
|
256 ;; Streams
|
|
257
|
|
258 (defun sieve-manage-network-p (buffer)
|
|
259 t)
|
|
260
|
|
261 (defun sieve-manage-network-open (name buffer server port)
|
|
262 (let* ((port (or port sieve-manage-default-port))
|
|
263 (coding-system-for-read sieve-manage-coding-system-for-read)
|
|
264 (coding-system-for-write sieve-manage-coding-system-for-write)
|
|
265 (process (open-network-stream name buffer server port)))
|
|
266 (when process
|
|
267 (while (and (memq (process-status process) '(open run))
|
|
268 (set-buffer buffer) ;; XXX "blue moon" nntp.el bug
|
|
269 (goto-char (point-min))
|
|
270 (not (sieve-manage-parse-greeting-1)))
|
|
271 (accept-process-output process 1)
|
|
272 (sit-for 1))
|
|
273 (sieve-manage-erase nil buffer)
|
|
274 (when (memq (process-status process) '(open run))
|
|
275 process))))
|
|
276
|
|
277 (defun imap-starttls-p (buffer)
|
|
278 ;; (and (imap-capability 'STARTTLS buffer)
|
|
279 (condition-case ()
|
|
280 (progn
|
|
281 (require 'starttls)
|
|
282 (call-process "starttls"))
|
|
283 (error nil)))
|
|
284
|
|
285 (defun imap-starttls-open (name buffer server port)
|
|
286 (let* ((port (or port sieve-manage-default-port))
|
|
287 (coding-system-for-read sieve-manage-coding-system-for-read)
|
|
288 (coding-system-for-write sieve-manage-coding-system-for-write)
|
|
289 (process (starttls-open-stream name buffer server port))
|
|
290 done)
|
|
291 (when process
|
|
292 (while (and (memq (process-status process) '(open run))
|
|
293 (set-buffer buffer) ;; XXX "blue moon" nntp.el bug
|
|
294 (goto-char (point-min))
|
|
295 (not (sieve-manage-parse-greeting-1)))
|
|
296 (accept-process-output process 1)
|
|
297 (sit-for 1))
|
|
298 (sieve-manage-erase nil buffer)
|
|
299 (sieve-manage-send "STARTTLS")
|
|
300 (starttls-negotiate process))
|
|
301 (when (memq (process-status process) '(open run))
|
|
302 process)))
|
|
303
|
|
304 ;; Authenticators
|
|
305
|
|
306 (defun sieve-manage-plain-p (buffer)
|
|
307 (sieve-manage-capability "SASL" "PLAIN" buffer))
|
|
308
|
|
309 (defun sieve-manage-plain-auth (buffer)
|
|
310 "Login to managesieve server using the PLAIN SASL method."
|
|
311 (let* ((done (sieve-manage-interactive-login
|
|
312 buffer
|
|
313 (lambda (user passwd)
|
|
314 (sieve-manage-send (concat "AUTHENTICATE \"PLAIN\" \""
|
|
315 (base64-encode-string
|
|
316 (concat (char-to-string 0)
|
|
317 user
|
|
318 (char-to-string 0)
|
|
319 passwd))
|
|
320 "\""))
|
|
321 (let ((rsp (sieve-manage-parse-okno)))
|
|
322 (if (sieve-manage-ok-p rsp)
|
|
323 t
|
|
324 (setq reason (cdr-safe rsp))
|
|
325 nil))))))
|
|
326 (if done
|
|
327 (message "sieve: Authenticating using PLAIN...done")
|
|
328 (message "sieve: Authenticating using PLAIN...failed"))))
|
|
329
|
|
330 (defun sieve-manage-cram-md5-p (buffer)
|
|
331 (sieve-manage-capability "SASL" "CRAM-MD5" buffer))
|
|
332
|
|
333 (defun sieve-manage-cram-md5-auth (buffer)
|
|
334 "Login to managesieve server using the CRAM-MD5 SASL method."
|
|
335 (message "sieve: Authenticating using CRAM-MD5...")
|
|
336 (let* ((done (sieve-manage-interactive-login
|
|
337 buffer
|
|
338 (lambda (user passwd)
|
|
339 (sieve-manage-send "AUTHENTICATE \"CRAM-MD5\"")
|
|
340 (sieve-manage-send
|
|
341 (concat
|
|
342 "\""
|
|
343 (base64-encode-string
|
|
344 (concat
|
|
345 user " "
|
|
346 (rfc2104-hash 'md5 64 16 passwd
|
|
347 (base64-decode-string
|
|
348 (prog1
|
|
349 (sieve-manage-parse-string)
|
|
350 (sieve-manage-erase))))))
|
|
351 "\""))
|
|
352 (let ((rsp (sieve-manage-parse-okno)))
|
|
353 (if (sieve-manage-ok-p rsp)
|
|
354 t
|
|
355 (setq reason (cdr-safe rsp))
|
|
356 nil))))))
|
|
357 (if done
|
|
358 (message "sieve: Authenticating using CRAM-MD5...done")
|
|
359 (message "sieve: Authenticating using CRAM-MD5...failed"))))
|
|
360
|
|
361 ;; Managesieve API
|
|
362
|
|
363 (defun sieve-manage-open (server &optional port stream auth buffer)
|
|
364 "Open a network connection to a managesieve SERVER (string).
|
|
365 Optional variable PORT is port number (integer) on remote server.
|
|
366 Optional variable STREAM is any of `sieve-manage-streams' (a symbol).
|
|
367 Optional variable AUTH indicates authenticator to use, see
|
|
368 `sieve-manage-authenticators' for available authenticators. If nil, chooses
|
|
369 the best stream the server is capable of.
|
|
370 Optional variable BUFFER is buffer (buffer, or string naming buffer)
|
|
371 to work in."
|
|
372 (setq buffer (or buffer (format " *sieve* %s:%d" server (or port 2000))))
|
|
373 (with-current-buffer (get-buffer-create buffer)
|
|
374 (mapcar 'make-local-variable sieve-manage-local-variables)
|
|
375 (sieve-manage-disable-multibyte)
|
|
376 (buffer-disable-undo)
|
|
377 (setq sieve-manage-server (or server sieve-manage-server))
|
|
378 (setq sieve-manage-port (or port sieve-manage-port))
|
|
379 (setq sieve-manage-stream (or stream sieve-manage-stream))
|
|
380 (message "sieve: Connecting to %s..." sieve-manage-server)
|
|
381 (if (let ((sieve-manage-stream
|
|
382 (or sieve-manage-stream sieve-manage-default-stream)))
|
|
383 (sieve-manage-open-1 buffer))
|
|
384 ;; Choose stream.
|
|
385 (let (stream-changed)
|
|
386 (message "sieve: Connecting to %s...done" sieve-manage-server)
|
|
387 (when (null sieve-manage-stream)
|
|
388 (let ((streams sieve-manage-streams))
|
|
389 (while (setq stream (pop streams))
|
|
390 (if (funcall (nth 1 (assq stream
|
|
391 sieve-manage-stream-alist)) buffer)
|
|
392 (setq stream-changed
|
|
393 (not (eq (or sieve-manage-stream
|
|
394 sieve-manage-default-stream)
|
|
395 stream))
|
|
396 sieve-manage-stream stream
|
|
397 streams nil)))
|
|
398 (unless sieve-manage-stream
|
|
399 (error "Couldn't figure out a stream for server"))))
|
|
400 (when stream-changed
|
|
401 (message "sieve: Reconnecting with stream `%s'..."
|
|
402 sieve-manage-stream)
|
|
403 (sieve-manage-close buffer)
|
|
404 (if (sieve-manage-open-1 buffer)
|
|
405 (message "sieve: Reconnecting with stream `%s'...done"
|
|
406 sieve-manage-stream)
|
|
407 (message "sieve: Reconnecting with stream `%s'...failed"
|
|
408 sieve-manage-stream))
|
|
409 (setq sieve-manage-capability nil))
|
|
410 (if (sieve-manage-opened buffer)
|
|
411 ;; Choose authenticator
|
|
412 (when (and (null sieve-manage-auth)
|
|
413 (not (eq sieve-manage-state 'auth)))
|
|
414 (let ((auths sieve-manage-authenticators))
|
|
415 (while (setq auth (pop auths))
|
|
416 (if (funcall (nth 1 (assq
|
|
417 auth
|
|
418 sieve-manage-authenticator-alist))
|
|
419 buffer)
|
|
420 (setq sieve-manage-auth auth
|
|
421 auths nil)))
|
|
422 (unless sieve-manage-auth
|
|
423 (error "Couldn't figure out authenticator for server"))))))
|
|
424 (message "sieve: Connecting to %s...failed" sieve-manage-server))
|
|
425 (when (sieve-manage-opened buffer)
|
|
426 (sieve-manage-erase)
|
|
427 buffer)))
|
|
428
|
|
429 (defun sieve-manage-opened (&optional buffer)
|
|
430 "Return non-nil if connection to managesieve server in BUFFER is open.
|
|
431 If BUFFER is nil then the current buffer is used."
|
|
432 (and (setq buffer (get-buffer (or buffer (current-buffer))))
|
|
433 (buffer-live-p buffer)
|
|
434 (with-current-buffer buffer
|
|
435 (and sieve-manage-process
|
|
436 (memq (process-status sieve-manage-process) '(open run))))))
|
|
437
|
|
438 (defun sieve-manage-close (&optional buffer)
|
|
439 "Close connection to managesieve server in BUFFER.
|
|
440 If BUFFER is nil, the current buffer is used."
|
|
441 (with-current-buffer (or buffer (current-buffer))
|
|
442 (when (sieve-manage-opened)
|
|
443 (sieve-manage-send "LOGOUT")
|
|
444 (sit-for 1))
|
|
445 (when (and sieve-manage-process
|
|
446 (memq (process-status sieve-manage-process) '(open run)))
|
|
447 (delete-process sieve-manage-process))
|
|
448 (setq sieve-manage-process nil)
|
|
449 (sieve-manage-erase)
|
|
450 t))
|
|
451
|
|
452 (defun sieve-manage-authenticate (&optional user passwd buffer)
|
|
453 "Authenticate to server in BUFFER, using current buffer if nil.
|
|
454 It uses the authenticator specified when opening the server. If the
|
|
455 authenticator requires username/passwords, they are queried from the
|
|
456 user and optionally stored in the buffer. If USER and/or PASSWD is
|
|
457 specified, the user will not be questioned and the username and/or
|
|
458 password is remembered in the buffer."
|
|
459 (with-current-buffer (or buffer (current-buffer))
|
|
460 (if (not (eq sieve-manage-state 'nonauth))
|
|
461 (eq sieve-manage-state 'auth)
|
|
462 (make-local-variable 'sieve-manage-username)
|
|
463 (make-local-variable 'sieve-manage-password)
|
|
464 (if user (setq sieve-manage-username user))
|
|
465 (if passwd (setq sieve-manage-password passwd))
|
|
466 (if (funcall (nth 2 (assq sieve-manage-auth
|
|
467 sieve-manage-authenticator-alist)) buffer)
|
|
468 (setq sieve-manage-state 'auth)))))
|
|
469
|
|
470 (defun sieve-manage-capability (&optional name value buffer)
|
|
471 (with-current-buffer (or buffer (current-buffer))
|
|
472 (if (null name)
|
|
473 sieve-manage-capability
|
|
474 (if (null value)
|
|
475 (nth 1 (assoc name sieve-manage-capability))
|
|
476 (when (string-match value (nth 1 (assoc name sieve-manage-capability)))
|
|
477 (nth 1 (assoc name sieve-manage-capability)))))))
|
|
478
|
|
479 (defun sieve-manage-listscripts (&optional buffer)
|
|
480 (with-current-buffer (or buffer (current-buffer))
|
|
481 (sieve-manage-send "LISTSCRIPTS")
|
|
482 (sieve-manage-parse-listscripts)))
|
|
483
|
|
484 (defun sieve-manage-havespace (name size &optional buffer)
|
|
485 (with-current-buffer (or buffer (current-buffer))
|
|
486 (sieve-manage-send (format "HAVESPACE \"%s\" %s" name size))
|
|
487 (sieve-manage-parse-okno)))
|
|
488
|
|
489 (eval-and-compile
|
|
490 (if (fboundp 'string-bytes)
|
|
491 (defalias 'sieve-string-bytes 'string-bytes)
|
|
492 (defalias 'sieve-string-bytes 'length)))
|
|
493
|
|
494 (defun sieve-manage-putscript (name content &optional buffer)
|
|
495 (with-current-buffer (or buffer (current-buffer))
|
|
496 (sieve-manage-send (format "PUTSCRIPT \"%s\" {%d+}%s%s" name
|
|
497 (sieve-string-bytes content)
|
|
498 sieve-manage-client-eol content))
|
|
499 (sieve-manage-parse-okno)))
|
|
500
|
|
501 (defun sieve-manage-deletescript (name &optional buffer)
|
|
502 (with-current-buffer (or buffer (current-buffer))
|
|
503 (sieve-manage-send (format "DELETESCRIPT \"%s\"" name))
|
|
504 (sieve-manage-parse-okno)))
|
|
505
|
|
506 (defun sieve-manage-getscript (name output-buffer &optional buffer)
|
|
507 (with-current-buffer (or buffer (current-buffer))
|
|
508 (sieve-manage-send (format "GETSCRIPT \"%s\"" name))
|
|
509 (let ((script (sieve-manage-parse-string)))
|
|
510 (sieve-manage-parse-crlf)
|
|
511 (with-current-buffer output-buffer
|
|
512 (insert script))
|
|
513 (sieve-manage-parse-okno))))
|
|
514
|
|
515 (defun sieve-manage-setactive (name &optional buffer)
|
|
516 (with-current-buffer (or buffer (current-buffer))
|
|
517 (sieve-manage-send (format "SETACTIVE \"%s\"" name))
|
|
518 (sieve-manage-parse-okno)))
|
|
519
|
|
520 ;; Protocol parsing routines
|
|
521
|
|
522 (defun sieve-manage-ok-p (rsp)
|
|
523 (string= (downcase (or (car-safe rsp) "")) "ok"))
|
|
524
|
|
525 (defsubst sieve-manage-forward ()
|
|
526 (or (eobp) (forward-char)))
|
|
527
|
|
528 (defun sieve-manage-is-okno ()
|
|
529 (when (looking-at (concat
|
|
530 "^\\(OK\\|NO\\)\\( (\\([^)]+\\))\\)?\\( \\(.*\\)\\)?"
|
|
531 sieve-manage-server-eol))
|
|
532 (let ((status (match-string 1))
|
|
533 (resp-code (match-string 3))
|
|
534 (response (match-string 5)))
|
|
535 (when response
|
|
536 (goto-char (match-beginning 5))
|
|
537 (setq response (sieve-manage-is-string)))
|
|
538 (list status resp-code response))))
|
|
539
|
|
540 (defun sieve-manage-parse-okno ()
|
|
541 (let (rsp)
|
|
542 (while (null rsp)
|
|
543 (accept-process-output (get-buffer-process (current-buffer)) 1)
|
|
544 (goto-char (point-min))
|
|
545 (setq rsp (sieve-manage-is-okno)))
|
|
546 (sieve-manage-erase)
|
|
547 rsp))
|
|
548
|
|
549 (defun sieve-manage-parse-capability-1 ()
|
|
550 "Accept a managesieve greeting."
|
|
551 (let (str)
|
|
552 (while (setq str (sieve-manage-is-string))
|
|
553 (if (eq (char-after) ? )
|
|
554 (progn
|
|
555 (sieve-manage-forward)
|
|
556 (push (list str (sieve-manage-is-string))
|
|
557 sieve-manage-capability))
|
|
558 (push (list str) sieve-manage-capability))
|
|
559 (forward-line)))
|
|
560 (when (re-search-forward (concat "^OK" sieve-manage-server-eol) nil t)
|
|
561 (setq sieve-manage-state 'nonauth)))
|
|
562
|
|
563 (defalias 'sieve-manage-parse-greeting-1 'sieve-manage-parse-capability-1)
|
|
564
|
|
565 (defun sieve-manage-is-string ()
|
|
566 (cond ((looking-at "\"\\([^\"]+\\)\"")
|
|
567 (prog1
|
|
568 (match-string 1)
|
|
569 (goto-char (match-end 0))))
|
|
570 ((looking-at (concat "{\\([0-9]+\\)}" sieve-manage-server-eol))
|
|
571 (let ((pos (match-end 0))
|
|
572 (len (string-to-number (match-string 1))))
|
|
573 (if (< (point-max) (+ pos len))
|
|
574 nil
|
|
575 (goto-char (+ pos len))
|
|
576 (buffer-substring pos (+ pos len)))))))
|
|
577
|
|
578 (defun sieve-manage-parse-string ()
|
|
579 (let (rsp)
|
|
580 (while (null rsp)
|
|
581 (accept-process-output (get-buffer-process (current-buffer)) 1)
|
|
582 (goto-char (point-min))
|
|
583 (setq rsp (sieve-manage-is-string)))
|
|
584 (sieve-manage-erase (point))
|
|
585 rsp))
|
|
586
|
|
587 (defun sieve-manage-parse-crlf ()
|
|
588 (when (looking-at sieve-manage-server-eol)
|
|
589 (sieve-manage-erase (match-end 0))))
|
|
590
|
|
591 (defun sieve-manage-parse-listscripts ()
|
|
592 (let (tmp rsp data)
|
|
593 (while (null rsp)
|
|
594 (while (null (or (setq rsp (sieve-manage-is-okno))
|
|
595 (setq tmp (sieve-manage-is-string))))
|
|
596 (accept-process-output (get-buffer-process (current-buffer)) 1)
|
|
597 (goto-char (point-min)))
|
|
598 (when tmp
|
|
599 (while (not (looking-at (concat "\\( ACTIVE\\)?"
|
|
600 sieve-manage-server-eol)))
|
|
601 (accept-process-output (get-buffer-process (current-buffer)) 1)
|
|
602 (goto-char (point-min)))
|
|
603 (if (match-string 1)
|
|
604 (push (cons 'active tmp) data)
|
|
605 (push tmp data))
|
|
606 (goto-char (match-end 0))
|
|
607 (setq tmp nil)))
|
|
608 (sieve-manage-erase)
|
|
609 (if (sieve-manage-ok-p rsp)
|
|
610 data
|
|
611 rsp)))
|
|
612
|
|
613 (defun sieve-manage-send (cmdstr)
|
|
614 (setq cmdstr (concat cmdstr sieve-manage-client-eol))
|
|
615 (and sieve-manage-log
|
|
616 (with-current-buffer (get-buffer-create sieve-manage-log)
|
|
617 (sieve-manage-disable-multibyte)
|
|
618 (buffer-disable-undo)
|
|
619 (goto-char (point-max))
|
|
620 (insert cmdstr)))
|
|
621 (process-send-string sieve-manage-process cmdstr))
|
|
622
|
|
623 (provide 'sieve-manage)
|
|
624
|
|
625 ;;; arch-tag: 321c4640-1371-4495-9baf-8ccb71dd5bd1
|
|
626 ;; sieve-manage.el ends here
|