File: wl-acap.el

package info (click to toggle)
wl-beta 2.15.9+0.20190205-2
  • links: PTS, VCS
  • area: main
  • in suites: buster, sid
  • size: 6,260 kB
  • sloc: lisp: 51,446; sh: 191; makefile: 141
file content (406 lines) | stat: -rw-r--r-- 11,981 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
;;; wl-acap.el --- ACAP support for Wanderlust.

;; Copyright (C) 2001 Yuuichi Teranishi <teranisi@gohome.org>

;; Author: Yuuichi Teranishi <teranisi@gohome.org>
;; Keywords: mail, net news

;; This file is part of Wanderlust (Yet Another Message Interface on Emacsen).

;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; see the file COPYING.  If not, write to the
;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.
;;

;;; Commentary:

;;; Code:
;;

;;(cond
;; ((and (not (featurep 'utf-2000))
;;       (module-installed-p 'un-define))
;;  (require 'un-define))
;; ((and (featurep 'xemacs)
;;       (not (featurep 'utf-2000))
;;       (module-installed-p 'xemacs-ucs))
;;  (require 'xemacs-ucs)))
(require 'custom)
(require 'cus-edit)
(require 'wl-vars)
(require 'wl)
(require 'elmo-vars)
(require 'acap)
(require 'slp)

(defconst wl-acap-dataset-class "vendor.wanderlust")
(defconst wl-acap-entry-name "settings")

(defcustom wl-acap-user (or (getenv "USER")
			    (getenv "LOGNAME")
			    (user-login-name))
  "ACAP user."
  :type 'string
  :group 'wl)

(defcustom wl-acap-server nil
  "ACAP server.
If nil, SLP is used to find ACAP server.
If nil and SLP is not available, localhost is assumed."
  :type 'string
  :group 'wl)

(defcustom wl-acap-port nil
  "ACAP server port.
Only valid when `wl-acap-server' is non-nil.
If nil, default acap port is used."
  :type 'string
  :group 'wl)

(defcustom wl-acap-authenticate-type 'cram-md5
  "ACAP authenticate type."
  :type 'symbol
  :group 'wl)

(defcustom wl-acap-stream-type nil
  "ACAP stream type."
  :type 'symbol
  :group 'wl)

(defcustom wl-acap-extra-options nil
  "Extra options to be saved on ACAP server."
  :type '(repeat symbol)
  :group 'wl)

(defcustom wl-acap-cache-filename "acap-cache"
  "ACAP setting cache file."
  :type 'string
  :group 'wl)

;; Encoding string as BASE64 is temporal solution.
;; As far as I know, current implementation of ACAP server
;; (cyrus-smlacapd 0.5) does not accept literal argument for STORE.
(defvar wl-acap-base64-encode-options
  '(wl-template-alist
    wl-draft-config-alist)
  "Options which should be encoded with base64 to store ACAP server.")

(defcustom wl-acap-coding-system 'utf-8
  "Coding system for ACAP."
  :type 'symbol
  :group 'wl)

(defvar wl-acap-original-msgdb-directory nil)

(defun wl-acap-exit ()
  "End ACAP session."
  (when wl-acap-original-msgdb-directory
    (setq elmo-msgdb-directory wl-acap-original-msgdb-directory)))

(defun wl-acap-init ()
  "A candidate for `wl-folder-init-function'."
  (setq wl-acap-original-msgdb-directory nil)
  (condition-case err			; catch error and quit.
      (let ((service (wl-acap-find-acap-service))
	    proc entries settings folder-top type caches msgdb-dir)
	(if (null (car service))
	    (if (setq caches
		      (delq
		       nil
		       (mapcar
			(lambda (dirent)
			  (let ((dir
				 (elmo-localdir-folder-directory-internal
				  (elmo-make-folder dirent))))
			    (if (file-exists-p
				 (setq dir (expand-file-name
					    wl-acap-cache-filename
					    dir)))
				dir)))
			(elmo-folder-list-subfolders
			 (elmo-make-folder (concat "+"
						   (expand-file-name
						    "acap"
						    elmo-msgdb-directory)))))))
		(if (y-or-n-p "No ACAP service found.  Try cache? ")
		    (let (selected rpath alist)
		      (setq alist
			    (mapcar
			     (lambda (dir)
			       (setq rpath (nreverse (split-string dir "/")))
			       (cons (concat (nth 1 rpath) "@" (nth 2 rpath))
				     dir))
			     caches)
			    selected
			    (cdr (assoc
				  (completing-read
				   "Select ACAP cache: " alist nil t)
				  alist))
			    msgdb-dir (file-name-directory selected)
			    entries (elmo-object-load selected)))
		  (error "No ACAP service found"))
	      (error "No ACAP service found"))
	  (setq proc (acap-open (car service)
				wl-acap-user
				(upcase (symbol-name
					 wl-acap-authenticate-type))
				(cdr service)))
	  (setq entries (acap-response-entries
			 (acap-search proc (concat "/"
						   wl-acap-dataset-class
						   "/~/")
				      '((RETURN ("*"))))))
	  (when entries
	    (elmo-object-save
	     (expand-file-name
	      (concat "acap/" (car service) "/" wl-acap-user "/"
		      wl-acap-cache-filename)
	      elmo-msgdb-directory)
	     entries)))
	(while entries
	  (when (string= (acap-response-entry-entry (car entries))
			 wl-acap-entry-name)
	    (setq settings (car (acap-response-entry-return-data-list
				 (car entries)))
		  entries nil))
	  (setq entries (cdr entries)))
	(setq settings
	      (delq
	       'wl-acap-ignored
	       (mapcar (lambda (x)
			 (let ((sym (wl-acap-symbol (car x))))
			   (cond
			    ((and sym (eq sym 'wl-folders))
			     ;; Folders.
			     (setq wl-folder-entity
				   (wl-acap-create-folder-entity (cadr x)))
			     'wl-acap-ignored)
			    ((and sym (boundp sym))
			     (setq type (custom-variable-type sym))
			     (cons
			      sym
			      (when (cadr x)
				(cond
				 ((or (eq (car type) 'string)
				      (and (eq (car type) 'choice)
					   (memq 'string type)))
				  (if (memq sym wl-acap-base64-encode-options)
				      (wl-acap-base64-decode-string (cadr x))
				    (decode-coding-string
				     (cadr x)
				     wl-acap-coding-system)))
				 (t
				  (if (cadr x)
				      (read
				       (if (memq sym
						 wl-acap-base64-encode-options)
					   (wl-acap-base64-decode-string
					    (cadr x))
					 (read (concat
						"\""
						(decode-coding-string
						 (cadr x)
						 wl-acap-coding-system)
						"\""))
					 ))))))))
			    (t 'wl-acap-ignored))))
		       settings)))
	;; Setup options.
	(dolist (setting settings)
	  (set (car setting) (cdr setting)))
	;; Database directory becomes specific to the ACAP server.
	(setq wl-acap-original-msgdb-directory elmo-msgdb-directory)
	(setq elmo-msgdb-directory (or msgdb-dir
				       (expand-file-name
					(concat "acap/" (car service)
						"/" wl-acap-user)
					elmo-msgdb-directory)))
	(when proc (acap-close proc)))
    ((error quit)
     (when wl-acap-original-msgdb-directory
       (setq elmo-msgdb-directory wl-acap-original-msgdb-directory))
     (signal (car err) (cdr err)))))

(defun wl-acap-create-folder-entity (string)
  (with-temp-buffer
    (message "Initializing folder...")
    (let (folders entity)
      (setq string (elmo-base64-decode-string string))
      (setq string (decode-coding-string string wl-acap-coding-system))
      (insert string)
      (goto-char (point-min))
      (while (and (not (eobp))
		  (setq entity (wl-create-folder-entity-from-buffer)))
	(unless (eq entity 'ignore)
	  (wl-append folders (list entity))))
      (message "Initializing folder...done")
      (list wl-folder-desktop-name 'group folders))))

(defun wl-acap-find-acap-service ()
  (or (and wl-acap-server
	   (cons wl-acap-server wl-acap-port))
      (with-temp-buffer
	(message "Searching ACAP server...")
	(prog1 (let ((response (condition-case nil
				   (slp-findsrvs "acap")
				 (error)))
		     selected)
		 (when response
		   (if (> (length (slp-response-body response)) 1)
		       (progn
			 (setq selected
			       (completing-read
				"Select ACAP server: "
				(mapcar (lambda (body)
					  (list
					   (concat
					    (slp-response-srv-url-host
					     body)
					    (when (slp-response-srv-url-port
						   body)
					      (concat
					       ":"
					       (slp-response-srv-url-port
						body))))))
					(slp-response-body response)))
			       response
			       (catch 'done
				 (dolist (entry (slp-response-body response))
				   (when (string=
					  (concat
					   (slp-response-srv-url-host
					    entry)
					   (when
					       (slp-response-srv-url-port
						entry)
					     (concat
					      ":"
					      (slp-response-srv-url-port
					       entry))))
					  selected)
				     (throw 'done entry))))))
		     (setq response (car (slp-response-body response))))
		   (cons (slp-response-srv-url-host response)
			 (slp-response-srv-url-port response))))
	  (message "Searching ACAP server...done")))
      (cons "localhost" nil)))

(defun wl-acap-name (option)
  (let ((name (symbol-name option))
	prefix)
    (cond ((string-match "^wl-" name)
	   (setq name (substring name (match-end 0))
		 prefix "wl"))
	  ((string-match "^elmo-" name)
	   (setq name (substring name (match-end 0))
		 prefix "elmo")))
    (concat
     wl-acap-dataset-class "." prefix "."
     (mapconcat 'capitalize (split-string name "-") ""))))

(defun wl-acap-symbol (name)
  (let (case-fold-search li)
    (when (string-match (concat "^" (regexp-quote wl-acap-dataset-class)
				"\\.\\([^\\.]+\\)\\.") name)
      (setq li (list (match-string 1 name))
	    name (substring name (match-end 0)))
      (while (string-match "^[A-Z][a-z0-9]*" name)
	(setq li (cons (match-string 0 name) li))
	(setq name (substring name (match-end 0))))
      (intern (mapconcat 'downcase (nreverse li) "-")))))

(defun wl-acap-list-options ()
  (nconc (mapcar 'car (append (custom-group-members 'wl-setting nil)
			      (custom-group-members 'elmo-setting nil)))
	 wl-acap-extra-options))

(defun wl-acap-store-folders (proc)
  (with-temp-buffer
    (insert-file-contents wl-folders-file)
    (acap-store
     proc
     (list (concat "/" wl-acap-dataset-class "/~/"
		   wl-acap-entry-name)
	   (concat wl-acap-dataset-class ".wl.Folders")
	   (wl-acap-base64-encode-string (buffer-string))))))

(defun wl-acap-base64-encode-string (string)
  (elmo-base64-encode-string
   (encode-coding-string string wl-acap-coding-system)
   'no-line-break))

(defun wl-acap-base64-decode-string (string)
  (decode-coding-string
   (elmo-base64-decode-string string )
   wl-acap-coding-system))

(defun wl-acap-store ()
  "Store Wanderlust configuration to the ACAP server."
  (interactive)
  (wl-load-profile)
  (elmo-init)
  (let ((service (wl-acap-find-acap-service))
	proc settings type)
    (setq proc (acap-open (car service)
			  wl-acap-user
			  (upcase (symbol-name wl-acap-authenticate-type))
			  (cdr service)))
    (dolist (option (wl-acap-list-options))
      (setq settings
	    (cons (wl-acap-name option) settings)
	    settings
	    (cons (when (symbol-value option)
		    (setq type (custom-variable-type option))
		    (cond
		     ((or (eq (car type) 'string)
			  (and (eq (car type) 'choice)
			       (memq 'string type)))
		      (if (memq option wl-acap-base64-encode-options)
			  (wl-acap-base64-encode-string
			   (symbol-value option))
			(encode-coding-string
			 (symbol-value option)
			 wl-acap-coding-system)))
		     (t (if (memq option wl-acap-base64-encode-options)
			    (wl-acap-base64-encode-string
			     (prin1-to-string (symbol-value option)))
			  (encode-coding-string
			   (prin1-to-string (symbol-value option))
			   wl-acap-coding-system)))))
		  settings)))
    (unwind-protect
	(progn
	  (message "Storing settings...")
	  (acap-store proc
		      (nconc
		       (list
			(concat
			 "/" wl-acap-dataset-class "/~/" wl-acap-entry-name))
		       (nreverse settings)))
	  (message "Storing folders...")
	  (wl-acap-store-folders proc)
;;; Does not work correctly??
;;;	  (acap-setacl proc (list
;;;			     (concat
;;;			      "/" wl-acap-dataset-class "/~/"))
;;;		       "anyone" "") ; protect.
	  )
      (acap-close proc))
    (if (interactive-p)
	(message "Store completed."))))

(require 'product)
(product-provide (provide 'wl-acap) (require 'wl-version))

;;; wl-acap.el ends here