1 ;;; gnus-sync.el --- synchronization facility for Gnus
3 ;; Copyright (C) 2010-2016 Free Software Foundation, Inc.
5 ;; Author: Ted Zlatanov <tzz@lifelogs.com>
6 ;; Keywords: news synchronization nntp nnrss
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/>.
25 ;; This is the gnus-sync.el package.
27 ;; Put this in your startup file (~/.gnus.el for instance)
29 ;; possibilities for gnus-sync-backend:
30 ;; Tramp over SSH: /ssh:user@host:/path/to/filename
31 ;; ...or any other file Tramp and Emacs can handle...
33 ;; (setq gnus-sync-backend "/remote:/path.gpg" ; will use Tramp+EPA if loaded
34 ;; gnus-sync-global-vars '(gnus-newsrc-last-checked-date)
35 ;; gnus-sync-newsrc-groups '("nntp" "nnrss"))
36 ;; gnus-sync-newsrc-offsets '(2 3))
37 ;; against a LeSync server (beware the vampire LeSync, who knows your newsrc)
39 ;; (setq gnus-sync-backend '(lesync "http://lesync.info:5984/tzz")
40 ;; gnus-sync-newsrc-groups '("nntp" "nnrss"))
42 ;; What's a LeSync server?
44 ;; 1. install CouchDB, set up a real server admin user, and create a
45 ;; database, e.g. "tzz" and save the URL,
46 ;; e.g. http://lesync.info:5984/tzz
48 ;; 2. run `M-: (gnus-sync-lesync-setup "http://lesync.info:5984/tzz" "tzzadmin" "mypassword" "mysalt" t t)'
50 ;; (If you run it more than once, you have to remove the entry from
51 ;; _users yourself. This is intentional. This sets up a database
52 ;; admin for the "tzz" database, distinct from the server admin
53 ;; user in (1) above.)
55 ;; That's it, you can start using http://lesync.info:5984/tzz in your
56 ;; gnus-sync-backend as a LeSync backend. Fan fiction about the
57 ;; vampire LeSync is welcome.
59 ;; You may not want to expose a CouchDB install to the Big Bad
60 ;; Internet, especially if your love of all things furry would be thus
61 ;; revealed. Make sure it's not accessible by unauthorized users and
64 ;; If you want to try it out, I will create a test DB for you under
65 ;; http://lesync.info:5984/yourfavoritedbname
69 ;; - after gnus-sync-read, the message counts look wrong until you do
70 ;; `g'. So it's not run automatically, you have to call it with M-x
73 ;; - use gnus-after-set-mark-hook and gnus-before-update-mark-hook to
74 ;; catch the mark updates
76 ;; - repositioning of groups within topic after a LeSync sync is a
77 ;; weird sort of bubble sort ("buttle" sort: the old entry ends up
78 ;; at the rear of the list); you will eventually end up with the
79 ;; right order after calling `gnus-sync-read' a bunch of times.
81 ;; - installing topics and groups is inefficient and annoying, lots of
82 ;; prompts could be avoided
86 (eval-when-compile (require 'cl
))
92 (defvar gnus-topic-alist
) ;; gnus-group.el
93 (autoload 'gnus-group-topic
"gnus-topic")
95 (defgroup gnus-sync nil
96 "The Gnus synchronization facility."
100 (defcustom gnus-sync-newsrc-groups
'("nntp" "nnrss")
101 "List of groups to be synchronized in the gnus-newsrc-alist.
102 The group names are matched, they don't have to be fully
103 qualified. Typically you would choose all of these. That's the
104 default because there is no active sync backend by default, so
105 this setting is harmless until the user chooses a sync backend."
107 :type
'(repeat regexp
))
109 (defcustom gnus-sync-newsrc-offsets
'(2 3)
110 "List of per-group data to be synchronized."
113 :type
'(set (const :tag
"Read ranges" 2)
114 (const :tag
"Marks" 3)))
116 (defcustom gnus-sync-global-vars nil
117 "List of global variables to be synchronized.
118 You may want to sync `gnus-newsrc-last-checked-date' but pretty
119 much any symbol is fair game. You could additionally sync
120 `gnus-newsrc-alist', `gnus-server-alist', `gnus-topic-topology',
121 and `gnus-topic-alist'. Also see `gnus-variable-list'."
123 :type
'(repeat (choice (variable :tag
"A known variable")
124 (symbol :tag
"Any symbol"))))
126 (defcustom gnus-sync-backend nil
127 "The synchronization backend."
129 :type
'(radio (const :format
"None" nil
)
130 (list :tag
"Sync server"
131 (const :format
"LeSync Server API" lesync
)
132 (string :tag
"URL of a CouchDB database for API access"))
133 (string :tag
"Sync to a file")))
135 (defvar gnus-sync-newsrc-loader nil
136 "Carrier for newsrc data")
138 (defcustom gnus-sync-file-encrypt-to nil
139 "If non-nil, set `epa-file-encrypt-to' from this for encrypting the Sync file."
141 :type
'(choice string
(repeat string
))
144 (defcustom gnus-sync-lesync-name
(system-name)
145 "The LeSync name for this machine."
150 (defcustom gnus-sync-lesync-install-topics
'ask
151 "Should LeSync install the recorded topics?"
154 :type
'(choice (const :tag
"Never Install" nil
)
155 (const :tag
"Always Install" t
)
156 (const :tag
"Ask Me Once" ask
)))
158 (defvar gnus-sync-lesync-props-hash
(make-hash-table :test
'equal
)
159 "LeSync props, keyed by group name")
161 (defvar gnus-sync-lesync-design-prefix
"/_design/lesync"
162 "The LeSync design prefix for CouchDB")
164 (defvar gnus-sync-lesync-security-object
"/_security"
165 "The LeSync security object for CouchDB")
167 (defun gnus-sync-lesync-parse ()
168 "Parse the result of a LeSync request."
169 (goto-char (point-min))
171 (when (search-forward-regexp "^$" nil t
)
176 "gnus-sync-lesync-parse: Could not read the LeSync response!")
179 (defun gnus-sync-lesync-call (url method headers
&optional kvdata
)
180 "Make an access request to URL using KVDATA and METHOD.
181 KVDATA must be an alist."
182 (let ((url-request-method method
)
183 (url-request-extra-headers headers
)
184 (url-request-data (if kvdata
(json-encode kvdata
) nil
)))
185 (with-current-buffer (url-retrieve-synchronously url
)
186 (let ((data (gnus-sync-lesync-parse)))
187 (gnus-message 12 "gnus-sync-lesync-call: %s URL %s sent %S got %S"
188 method url
`((headers .
,headers
) (data ,kvdata
)) data
)
189 (kill-buffer (current-buffer))
192 (defun gnus-sync-lesync-PUT (url headers
&optional data
)
193 (gnus-sync-lesync-call url
"PUT" headers data
))
195 (defun gnus-sync-lesync-POST (url headers
&optional data
)
196 (gnus-sync-lesync-call url
"POST" headers data
))
198 (defun gnus-sync-lesync-GET (url headers
&optional data
)
199 (gnus-sync-lesync-call url
"GET" headers data
))
201 (defun gnus-sync-lesync-DELETE (url headers
&optional data
)
202 (gnus-sync-lesync-call url
"DELETE" headers data
))
204 ;; this is not necessary with newer versions of json.el but 1.2 or older
205 ;; (which are in Emacs 24.1 and earlier) need it
206 (defun gnus-sync-json-alist-p (list)
207 "Non-null if and only if LIST is an alist."
209 (setq list
(if (consp (car list
))
214 ;; this is not necessary with newer versions of json.el but 1.2 or older
215 ;; (which are in Emacs 24.1 and earlier) need it
216 (defun gnus-sync-json-plist-p (list)
217 "Non-null if and only if LIST is a plist."
219 (setq list
(if (and (keywordp (car list
))
225 ; (gnus-sync-lesync-setup "http://lesync.info:5984/tzz" "tzzadmin" "mypassword" "mysalt" t t)
226 ; (gnus-sync-lesync-setup "http://lesync.info:5984/tzz")
228 (defun gnus-sync-lesync-setup (url &optional user password salt reader admin
)
229 (interactive "sEnter URL to set up: ")
230 "Set up the LeSync database at URL.
231 Install USER as a READER and/or an ADMIN in the security object
232 under \"_security\", and in the CouchDB \"_users\" table using
233 PASSWORD and SALT. Only one USER is thus supported for now.
234 When SALT is nil, a random one will be generated using `random'."
235 (let* ((design-url (concat url gnus-sync-lesync-design-prefix
))
236 (security-object (concat url
"/_security"))
237 (user-record `((names .
[,user
]) (roles .
[])))
238 (couch-user-name (format "org.couchdb.user:%s" user
))
239 (salt (or salt
(sha1 (format "%s" (random)))))
241 `((_id .
,couch-user-name
)
243 (name .
,(format "%s" user
))
246 (password_sha .
,(when password
248 (format "%s%s" password salt
))))))
250 (gnus-sync-lesync-find-prop 'rev design-url design-url
)
251 (gnus-sync-lesync-get-prop 'rev design-url
)))
252 (latest-func "function(head,req)
256 var ftime = (req.query['ftime'] || 0);
257 while (row = getRow())
259 if (row.value['float-time'] > ftime)
261 var s = row.value['_id'];
262 if (s) tosend.push('\"'+s.replace('\"', '\\\"')+'\"');
265 send('['+tosend.join(',') + ']');
269 ;; <key>de.alt.fan.ipod</key>
271 ;; <integer>1</integer>
272 ;; <integer>2</integer>
275 ;; <integer>100</integer>
277 ;; <integer>100</integer>
281 (xmlplistread-func "function(head, req) {
283 start({ 'headers': { 'Content-Type': 'text/xml' } });
286 send('<key>read</key>');
288 while(row = getRow())
290 var read = row.value.read;
291 if (read && read[0] && read[0] == 'invlist')
293 send('<key>'+row.key+'</key>');
294 //send('<invlist>'+read+'</invlist>');
300 for (var i = 1; i < read.length && read[i]; i++)
307 send('<integer>'+read[i]+'</integer>');
312 send('<key>start</key>');
313 send('<integer>'+from+'</integer>');
314 send('<key>end</key>');
315 send('<integer>'+(cur-1)+'</integer>');
331 (subs-func "function(doc){emit([doc._id, doc.source], doc._rev);}")
332 (revs-func "function(doc){emit(doc._id, doc._rev);}")
333 (bytimesubs-func "function(doc)
334 {emit([(doc['float-time']||0), doc._id], doc._rev);}")
335 (bytime-func "function(doc)
336 {emit([(doc['float-time']||0), doc._id], doc);}")
337 (groups-func "function(doc){emit(doc._id, doc);}"))
339 (and (assq 'ok
(gnus-sync-lesync-PUT
343 (list `(readers .
,user-record
)))
345 (list `(admins .
,user-record
))))))
346 (assq 'ok
(gnus-sync-lesync-PUT
347 (concat (file-name-directory url
)
353 (assq 'ok
(gnus-sync-lesync-PUT
356 `(,@(when rev
(list (cons '_rev rev
)))
357 (lists .
((latest .
,latest-func
)
358 (xmlplistread .
,xmlplistread-func
)))
359 (views .
((subs .
((map .
,subs-func
)))
360 (revs .
((map .
,revs-func
)))
361 (bytimesubs .
((map .
,bytimesubs-func
)))
362 (bytime .
((map .
,bytime-func
)))
363 (groups .
((map .
,groups-func
)))))))))))
365 (defun gnus-sync-lesync-find-prop (prop url key
)
366 "Retrieve a PROPerty of a document KEY at URL.
367 Calls `gnus-sync-lesync-set-prop'.
368 For the 'rev PROP, uses '_rev against the document."
369 (gnus-sync-lesync-set-prop
370 prop key
(cdr (assq (if (eq prop
'rev
) '_rev prop
)
371 (gnus-sync-lesync-GET url nil
)))))
373 (defun gnus-sync-lesync-set-prop (prop key val
)
374 "Update the PROPerty of document KEY at URL to VAL.
375 Updates `gnus-sync-lesync-props-hash'."
376 (puthash (format "%s.%s" key prop
) val gnus-sync-lesync-props-hash
))
378 (defun gnus-sync-lesync-get-prop (prop key
)
379 "Get the PROPerty of KEY from `gnus-sync-lesync-props-hash'."
380 (gethash (format "%s.%s" key prop
) gnus-sync-lesync-props-hash
))
382 (defun gnus-sync-deep-print (data)
383 (let* ((print-quoted t
)
385 (print-escape-multibyte nil
)
386 (print-escape-nonascii t
)
390 (print-escape-newlines t
))
393 (defun gnus-sync-newsrc-loader-builder (&optional only-modified
)
394 (let* ((entries (cdr gnus-newsrc-alist
))
397 (setq entry
(pop entries
)
399 (when (gnus-grep-in-list name gnus-sync-newsrc-groups
)
401 (when (not (equal (gnus-sync-deep-print entry
)
402 (gnus-sync-lesync-get-prop 'checksum name
)))
403 (gnus-message 9 "%s: add %s, it's modified"
404 "gnus-sync-newsrc-loader-builder" name
)
409 ; (json-encode (gnus-sync-range2invlist '((1 . 47137) (47139 . 47714) 48129 48211 49231 49281 49342 49473 49475 49502)))
410 (defun gnus-sync-range2invlist (ranges)
412 (let ((ranges (delq nil ranges
))
415 (setq range
(pop ranges
))
419 (setq from
(car range
)
425 ; (let* ((d '((1 . 47137) (47139 . 47714) 48129 48211 49231 49281 49342 49473 49475 49502)) (j (format "%S" (gnus-sync-invlist2range (gnus-sync-range2invlist d))))) (or (equal (format "%S" d) j) j))
426 (defun gnus-sync-invlist2range (inv)
427 (setq inv
(append inv nil
))
428 (if (equal (format "%s" (car inv
)) "invlist")
438 (push (cons start top
) ret
)))
439 (setq flip
(not flip
))
444 (defun gnus-sync-position (search list
&optional test
)
445 "Find the position of SEARCH in LIST using TEST, defaulting to `eq'."
447 (test (or test
'eq
)))
448 (while (and list
(not (funcall test
(car list
) search
)))
451 (if (funcall test
(car list
) search
) pos nil
)))
453 (defun gnus-sync-topic-group-position (group topic-name
)
455 group
(cdr (assoc topic-name gnus-topic-alist
)) 'equal
))
457 (defun gnus-sync-fix-topic-group-position (group topic-name position
)
458 (unless (equal position
(gnus-sync-topic-group-position group topic-name
))
459 (let* ((loc "gnus-sync-fix-topic-group-position")
460 (groups (delete group
(cdr (assoc topic-name gnus-topic-alist
))))
461 (position (min position
(1- (length groups
))))
462 (old (nth position groups
)))
463 (when (and old
(not (equal old group
)))
464 (setf (nth position groups
) group
)
465 (setcdr (assoc topic-name gnus-topic-alist
)
466 (append groups
(list old
)))
467 (gnus-message 9 "%s: %s moved to %d, swap with %s"
468 loc group position old
)))))
470 (defun gnus-sync-lesync-pre-save-group-entry (url nentry
&rest passed-props
)
471 (let* ((loc "gnus-sync-lesync-save-group-entry")
473 (revision (gnus-sync-lesync-get-prop 'rev k
))
474 (sname gnus-sync-lesync-name
)
475 (topic (gnus-group-topic k
))
476 (topic-offset (gnus-sync-topic-group-position k topic
))
477 (sources (gnus-sync-lesync-get-prop 'source k
)))
478 ;; set the revision so we don't have a conflict
480 (list (cons '_rev revision
)))
484 ;; add our name to the sources list for this key
485 (source ,@(if (member gnus-sync-lesync-name sources
)
487 (cons gnus-sync-lesync-name sources
)))
488 ,(cons 'level
(nth 1 nentry
))
489 ,@(if topic
(list (cons 'topic topic
)) nil
)
490 ,@(if topic-offset
(list (cons 'topic-offset topic-offset
)) nil
)
492 ,(cons 'read
(gnus-sync-range2invlist (nth 2 nentry
)))
494 ,@(delq nil
(mapcar (lambda (mark-entry)
495 (gnus-message 12 "%s: prep param %s in %s"
499 (if (listp (cdr mark-entry
))
500 (cons (car mark-entry
)
501 (gnus-sync-range2invlist
503 (progn ; else this is not a list
504 (gnus-message 9 "%s: non-list param %s in %s"
511 (defun gnus-sync-lesync-post-save-group-entry (url entry
)
512 (let* ((loc "gnus-sync-lesync-post-save-group-entry")
513 (k (cdr (assq 'id entry
))))
516 ((and (assq 'rev entry
) (assq 'id entry
))
518 (gnus-sync-lesync-set-prop 'rev k
(cdr (assq 'rev entry
)))
519 (gnus-sync-lesync-set-prop 'checksum
521 (gnus-sync-deep-print
522 (assoc k gnus-newsrc-alist
)))
523 (gnus-message 9 "%s: successfully synced %s to %s"
525 ;; specifically check for document conflicts
526 ((equal "conflict" (format "%s" (cdr-safe (assq 'error entry
))))
529 "%s: use `%s' to resolve the conflict synchronizing %s to %s: %s"
530 loc
"gnus-sync-read" k url
(cdr (assq 'reason entry
))))
533 (gnus-error 1 "%s: got error while synchronizing %s to %s: %s"
534 loc k url
(cdr (assq 'reason entry
))))
537 (gnus-message 2 "%s: unknown sync status after %s to %s: %S"
539 (assoc 'error entry
)))
541 (defun gnus-sync-lesync-groups-builder (url)
542 (let ((u (concat url gnus-sync-lesync-design-prefix
"/_view/groups")))
543 (cdr (assq 'rows
(gnus-sync-lesync-GET u nil
)))))
545 (defun gnus-sync-subscribe-group (name)
546 "Subscribe to group NAME. Returns NAME on success, nil otherwise."
547 (gnus-subscribe-newsgroup name
))
549 (defun gnus-sync-lesync-read-group-entry (url name entry
&rest passed-props
)
550 "Read ENTRY information for NAME. Returns NAME if successful.
551 Skips entries whose sources don't contain
552 `gnus-sync-lesync-name'. When the alist PASSED-PROPS has a
553 `subscribe-all' element that evaluates to true, we attempt to
554 subscribe to unknown groups. The user is also allowed to delete
555 unwanted groups via the LeSync URL."
556 (let* ((loc "gnus-sync-lesync-read-group-entry")
557 (entry (gnus-sync-lesync-normalize-group-entry entry passed-props
))
558 (subscribe-all (cdr (assq 'subscribe-all passed-props
)))
559 (sources (cdr (assq 'source entry
)))
560 (rev (cdr (assq 'rev entry
)))
561 (in-sources (member gnus-sync-lesync-name sources
))
562 (known (assoc name gnus-newsrc-alist
))
565 (if (and subscribe-all
566 (y-or-n-p (format "Subscribe to group %s?" name
)))
567 (setq known
(gnus-sync-subscribe-group name
)
570 (when (y-or-n-p (format "Delete group %s from server?" name
))
571 (if (equal name
(gnus-sync-lesync-delete-group url name
))
572 (gnus-message 1 "%s: removed group %s from server %s"
574 (gnus-error 1 "%s: could not remove group %s from server %s"
580 (format "Read group %s even though %s is not in sources %S?"
581 name gnus-sync-lesync-name
(or sources
""))))))
583 (gnus-sync-lesync-set-prop 'rev name rev
))
585 ;; if the source matches AND we have this group
586 (if (and known in-sources
)
588 (gnus-message 10 "%s: reading LeSync entry %s, sources %S"
591 (setq cell
(pop entry
))
594 (gnus-sync-lesync-set-prop k name val
)))
598 (gnus-message 5 "%s: ignoring entry %s, it wasn't subscribed. %s"
599 loc name
"Call `gnus-sync-read' with C-u to force it."))
601 (gnus-message 5 "%s: ignoring entry %s, %s not in sources %S"
602 loc name gnus-sync-lesync-name
(or sources
"")))
605 (declare-function gnus-topic-create-topic
"gnus-topic"
606 (topic parent
&optional previous full-topic
))
607 (declare-function gnus-topic-enter-dribble
"gnus-topic" ())
609 (defun gnus-sync-lesync-install-group-entry (name)
610 (let* ((master (assoc name gnus-newsrc-alist
))
611 (old-topic-name (gnus-group-topic name
))
612 (old-topic (assoc old-topic-name gnus-topic-alist
))
613 (target-topic-name (gnus-sync-lesync-get-prop 'topic name
))
614 (target-topic-offset (gnus-sync-lesync-get-prop 'topic-offset name
))
615 (target-topic (assoc target-topic-name gnus-topic-alist
))
616 (loc "gnus-sync-lesync-install-group-entry"))
619 (when (eq 'ask gnus-sync-lesync-install-topics
)
620 (setq gnus-sync-lesync-install-topics
621 (y-or-n-p "Install topics from LeSync?")))
622 (when (and (eq t gnus-sync-lesync-install-topics
)
624 (if (equal old-topic-name target-topic-name
)
625 (gnus-message 12 "%s: %s is already in topic %s"
626 loc name target-topic-name
)
627 ;; see `gnus-topic-move-group'
628 (when (and old-topic target-topic
)
629 (setcdr old-topic
(gnus-delete-first name
(cdr old-topic
)))
630 (gnus-message 5 "%s: removing %s from topic %s"
631 loc name old-topic-name
))
633 (when (y-or-n-p (format "Create missing topic %s?"
635 (gnus-topic-create-topic target-topic-name nil
)
636 (setq target-topic
(assoc target-topic-name
640 (nconc target-topic
(list name
))
641 (gnus-message 5 "%s: adding %s to topic %s"
642 loc name
(car target-topic
))
643 (gnus-topic-enter-dribble))
644 (gnus-error 2 "%s: LeSync group %s can't go in missing topic %s"
645 loc name target-topic-name
)))
646 (when (and target-topic-offset target-topic
)
647 (gnus-sync-fix-topic-group-position
648 name target-topic-name target-topic-offset
)))
649 ;; install the subscription level
650 (when (gnus-sync-lesync-get-prop 'level name
)
651 (setf (nth 1 master
) (gnus-sync-lesync-get-prop 'level name
)))
652 ;; install the read and other marks
653 (setf (nth 2 master
) (gnus-sync-lesync-get-prop 'read name
))
654 (setf (nth 3 master
) (gnus-sync-lesync-get-prop 'marks name
))
655 (gnus-sync-lesync-set-prop 'checksum
657 (gnus-sync-deep-print master
))
659 (gnus-error 1 "%s: invalid LeSync group %s" loc name
)
662 ; (gnus-sync-lesync-delete-group (cdr gnus-sync-backend) "nntp+Gmane:gwene.org.slashdot")
664 (defun gnus-sync-lesync-delete-group (url name
)
665 "Returns NAME if successful deleting it from URL, an error otherwise."
666 (interactive "sEnter URL to set up: \rsEnter group name: ")
667 (let* ((u (concat (cadr gnus-sync-backend
) "/" (url-hexify-string name
)))
668 (del (gnus-sync-lesync-DELETE
670 `(,@(when (gnus-sync-lesync-get-prop 'rev name
)
671 (list (cons "If-Match"
672 (gnus-sync-lesync-get-prop 'rev name
))))))))
673 (or (cdr (assq 'id del
)) del
)))
675 ;;; (gnus-sync-lesync-normalize-group-entry '((subscribe . ["invlist"]) (read . ["invlist"]) (topic-offset . 20) (topic . "news") (level . 6) (source . ["a" "b"]) (float-time . 1319671237.099285) (_rev . "10-edf5107f41e5e6f7f6629d1c0ee172f7") (_id . "nntp+news.net:alt.movies")) '((read-time 1319672156.486414) (subscribe-all nil)))
677 (defun gnus-sync-lesync-normalize-group-entry (entry &optional passed-props
)
681 (setq entry
(append passed-props entry
))
682 (while (setq cell
(pop entry
))
687 (push (cons k
(gnus-sync-invlist2range val
)) ret
))
688 ;; we ignore these parameters
689 ((member k
'(_id subscribe-all _deleted_conflicts
))
692 (push (cons 'rev val
) ret
))
694 (push (cons 'source
(append val nil
)) ret
))
695 ((or (eq k
'float-time
)
700 (push (cons k val
) ret
))
701 ;;; "How often have I said to you that when you have eliminated the
702 ;;; impossible, whatever remains, however improbable, must be the
703 ;;; truth?" --Sherlock Holmes
704 ;; everything remaining must be a mark
705 (t (push (cons k
(gnus-sync-invlist2range val
)) marks
)))))
706 (cons (cons 'marks marks
) ret
)))
708 (defun gnus-sync-save (&optional force
)
709 "Save the Gnus sync data to the backend.
710 With a prefix, FORCE is set and all groups will be saved."
713 ((and (listp gnus-sync-backend
)
714 (eq (nth 0 gnus-sync-backend
) 'lesync
)
715 (stringp (nth 1 gnus-sync-backend
)))
717 ;; refresh the revisions if we're forcing the save
719 (mapc (lambda (entry)
720 (when (and (assq 'key entry
)
722 (gnus-sync-lesync-set-prop
724 (cdr (assq 'key entry
))
725 (cdr (assq 'value entry
)))))
726 ;; the revs view is key = name, value = rev
727 (cdr (assq 'rows
(gnus-sync-lesync-GET
728 (concat (nth 1 gnus-sync-backend
)
729 gnus-sync-lesync-design-prefix
733 (let* ((ftime (float-time))
734 (url (nth 1 gnus-sync-backend
))
736 (mapcar (lambda (entry)
737 (gnus-sync-lesync-pre-save-group-entry
738 (cadr gnus-sync-backend
)
740 (cons 'float-time ftime
)))
741 (gnus-sync-newsrc-loader-builder (not force
))))
742 ;; when there are no entries, there's nothing to save
744 (gnus-sync-lesync-POST
745 (concat url
"/_bulk_docs")
746 '(("Content-Type" .
"application/json"))
747 `((docs .
,(vconcat entries nil
))))
749 2 "gnus-sync-save: nothing to save to the LeSync backend")
751 (mapcar (lambda (e) (gnus-sync-lesync-post-save-group-entry url e
))
753 ((stringp gnus-sync-backend
)
754 (gnus-message 7 "gnus-sync-save: saving to backend %s" gnus-sync-backend
)
755 ;; populate gnus-sync-newsrc-loader from all but the first dummy
756 ;; entry in gnus-newsrc-alist whose group matches any of the
757 ;; gnus-sync-newsrc-groups
758 ;; TODO: keep the old contents for groups we don't have!
759 (let ((gnus-sync-newsrc-loader
760 (loop for entry in
(cdr gnus-newsrc-alist
)
761 when
(gnus-grep-in-list
762 (car entry
) ;the group name
763 gnus-sync-newsrc-groups
)
764 collect
(cons (car entry
)
765 (mapcar (lambda (offset)
766 (cons offset
(nth offset entry
)))
767 gnus-sync-newsrc-offsets
)))))
768 (with-temp-file gnus-sync-backend
770 (let ((coding-system-for-write gnus-ding-file-coding-system
)
771 (standard-output (current-buffer)))
772 (when gnus-sync-file-encrypt-to
773 (set (make-local-variable 'epa-file-encrypt-to
)
774 gnus-sync-file-encrypt-to
))
775 (princ (format ";; -*- mode:emacs-lisp; coding: %s; -*-\n"
776 gnus-ding-file-coding-system
))
777 (princ ";; Gnus sync data v. 0.0.1\n")
778 ;; TODO: replace with `gnus-sync-deep-print'
779 (let* ((print-quoted t
)
781 (print-escape-multibyte nil
)
782 (print-escape-nonascii t
)
786 (print-escape-newlines t
)
787 (variables (cons 'gnus-sync-newsrc-loader
788 gnus-sync-global-vars
))
791 (if (and (boundp (setq variable
(pop variables
)))
792 (symbol-value variable
))
795 (princ (symbol-name variable
))
797 (prin1 (symbol-value variable
))
799 (princ "\n;;; skipping empty variable ")
800 (princ (symbol-name variable
)))))
803 "gnus-sync-save: stored variables %s and %d groups in %s"
804 gnus-sync-global-vars
805 (length gnus-sync-newsrc-loader
)
808 ;; Idea from Dan Christensen <jdc@chow.mat.jhu.edu>
809 ;; Save the .eld file with extra line breaks.
810 (gnus-message 8 "gnus-sync-save: adding whitespace to %s"
813 (goto-char (point-min))
814 (while (re-search-forward "^(\\|(\\\"" nil t
)
815 (replace-match "\n\\&" t
))
816 (goto-char (point-min))
817 (while (re-search-forward " $" nil t
)
818 (replace-match "" t t
))))))))
819 ;; the pass-through case: gnus-sync-backend is not a known choice
822 (defun gnus-sync-read (&optional subscribe-all
)
823 "Load the Gnus sync data from the backend.
824 With a prefix, SUBSCRIBE-ALL is set and unknown groups will be subscribed."
826 (when gnus-sync-backend
827 (gnus-message 7 "gnus-sync-read: loading from backend %s" gnus-sync-backend
)
829 ((and (listp gnus-sync-backend
)
830 (eq (nth 0 gnus-sync-backend
) 'lesync
)
831 (stringp (nth 1 gnus-sync-backend
)))
834 (mapc (lambda (entry)
835 (setq name
(cdr (assq 'id entry
)))
836 ;; set ftime the FIRST time through this loop, that
837 ;; way it reflects the time we FINISHED reading
838 (unless ftime
(setq ftime
(float-time)))
843 (gnus-sync-lesync-read-group-entry
844 (nth 1 gnus-sync-backend
)
846 (cdr (assq 'value entry
))
848 `(subscribe-all ,subscribe-all
)))
849 (gnus-sync-lesync-install-group-entry
850 (cdr (assq 'id entry
)))))))
851 (gnus-sync-lesync-groups-builder (nth 1 gnus-sync-backend
)))))
853 ((stringp gnus-sync-backend
)
855 (if (or debug-on-error debug-on-quit
)
856 (load gnus-sync-backend nil t
)
858 (load gnus-sync-backend nil t
)
860 (error "Error in %s: %s" gnus-sync-backend
(cadr var
)))))
861 (let ((valid-count 0)
863 (dolist (node gnus-sync-newsrc-loader
)
864 (if (gnus-gethash (car node
) gnus-newsrc-hashtb
)
867 (loop for store in
(cdr node
)
868 do
(setf (nth (car store
)
869 (assoc (car node
) gnus-newsrc-alist
))
871 (push (car node
) invalid-groups
)))
874 "gnus-sync-read: loaded %d groups (out of %d) from %s"
875 valid-count
(length gnus-sync-newsrc-loader
)
880 "gnus-sync-read: skipped %d groups (out of %d) from %s"
881 (length invalid-groups
)
882 (length gnus-sync-newsrc-loader
)
884 (gnus-message 9 "gnus-sync-read: skipped groups: %s"
885 (mapconcat 'identity invalid-groups
", ")))))
888 (gnus-message 9 "gnus-sync-read: remaking the newsrc hashtable")
889 (gnus-make-hashtable-from-newsrc-alist)))
892 (defun gnus-sync-initialize ()
893 "Initialize the Gnus sync facility."
895 (gnus-message 5 "Initializing the sync facility")
896 (gnus-sync-install-hooks))
899 (defun gnus-sync-install-hooks ()
900 "Install the sync hooks."
902 ;; (add-hook 'gnus-get-new-news-hook 'gnus-sync-read)
903 ;; (add-hook 'gnus-read-newsrc-el-hook 'gnus-sync-read)
904 (add-hook 'gnus-save-newsrc-hook
'gnus-sync-save
))
906 (defun gnus-sync-unload-hook ()
907 "Uninstall the sync hooks."
909 (remove-hook 'gnus-save-newsrc-hook
'gnus-sync-save
))
911 (add-hook 'gnus-sync-unload-hook
'gnus-sync-unload-hook
)
913 (when gnus-sync-backend
(gnus-sync-initialize))
917 ;;; gnus-sync.el ends here