lisp/gnus/gnus-start.el: Optionally check the newsrc.eld file's timestamp before...
[emacs.git] / lisp / gnus / gnus-sync.el
blob3aa1eeaba442052bcad437cdcba12e79be3de5d2
1 ;;; gnus-sync.el --- synchronization facility for Gnus
3 ;; Copyright (C) 2010-2014 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/>.
23 ;;; Commentary:
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
62 ;; guests, at least.
64 ;; If you want to try it out, I will create a test DB for you under
65 ;; http://lesync.info:5984/yourfavoritedbname
67 ;; TODO:
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
71 ;; gnus-sync-read
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
84 ;;; Code:
86 (eval-when-compile (require 'cl))
87 (require 'json)
88 (require 'gnus)
89 (require 'gnus-start)
90 (require 'gnus-util)
92 (defvar gnus-topic-alist) ;; gnus-group.el
93 (eval-when-compile
94 (autoload 'gnus-group-topic "gnus-topic")
95 (autoload 'gnus-topic-create-topic "gnus-topic" nil t)
96 (autoload 'gnus-topic-enter-dribble "gnus-topic"))
98 (defgroup gnus-sync nil
99 "The Gnus synchronization facility."
100 :version "24.1"
101 :group 'gnus)
103 (defcustom gnus-sync-newsrc-groups '("nntp" "nnrss")
104 "List of groups to be synchronized in the gnus-newsrc-alist.
105 The group names are matched, they don't have to be fully
106 qualified. Typically you would choose all of these. That's the
107 default because there is no active sync backend by default, so
108 this setting is harmless until the user chooses a sync backend."
109 :group 'gnus-sync
110 :type '(repeat regexp))
112 (defcustom gnus-sync-newsrc-offsets '(2 3)
113 "List of per-group data to be synchronized."
114 :group 'gnus-sync
115 :version "24.4"
116 :type '(set (const :tag "Read ranges" 2)
117 (const :tag "Marks" 3)))
119 (defcustom gnus-sync-global-vars nil
120 "List of global variables to be synchronized.
121 You may want to sync `gnus-newsrc-last-checked-date' but pretty
122 much any symbol is fair game. You could additionally sync
123 `gnus-newsrc-alist', `gnus-server-alist', `gnus-topic-topology',
124 and `gnus-topic-alist'. Also see `gnus-variable-list'."
125 :group 'gnus-sync
126 :type '(repeat (choice (variable :tag "A known variable")
127 (symbol :tag "Any symbol"))))
129 (defcustom gnus-sync-backend nil
130 "The synchronization backend."
131 :group 'gnus-sync
132 :type '(radio (const :format "None" nil)
133 (list :tag "Sync server"
134 (const :format "LeSync Server API" lesync)
135 (string :tag "URL of a CouchDB database for API access"))
136 (string :tag "Sync to a file")))
138 (defvar gnus-sync-newsrc-loader nil
139 "Carrier for newsrc data")
141 (defcustom gnus-sync-file-encrypt-to nil
142 "If non-nil, set `epa-file-encrypt-to' from this for encrypting the Sync file."
143 :version "24.4"
144 :type '(choice string (repeat string))
145 :group 'gnus-sync)
147 (defcustom gnus-sync-lesync-name (system-name)
148 "The LeSync name for this machine."
149 :group 'gnus-sync
150 :version "24.3"
151 :type 'string)
153 (defcustom gnus-sync-lesync-install-topics 'ask
154 "Should LeSync install the recorded topics?"
155 :group 'gnus-sync
156 :version "24.3"
157 :type '(choice (const :tag "Never Install" nil)
158 (const :tag "Always Install" t)
159 (const :tag "Ask Me Once" ask)))
161 (defvar gnus-sync-lesync-props-hash (make-hash-table :test 'equal)
162 "LeSync props, keyed by group name")
164 (defvar gnus-sync-lesync-design-prefix "/_design/lesync"
165 "The LeSync design prefix for CouchDB")
167 (defvar gnus-sync-lesync-security-object "/_security"
168 "The LeSync security object for CouchDB")
170 (defun gnus-sync-lesync-parse ()
171 "Parse the result of a LeSync request."
172 (goto-char (point-min))
173 (condition-case nil
174 (when (search-forward-regexp "^$" nil t)
175 (json-read))
176 (error
177 (gnus-message
179 "gnus-sync-lesync-parse: Could not read the LeSync response!")
180 nil)))
182 (defun gnus-sync-lesync-call (url method headers &optional kvdata)
183 "Make an access request to URL using KVDATA and METHOD.
184 KVDATA must be an alist."
185 (let ((url-request-method method)
186 (url-request-extra-headers headers)
187 (url-request-data (if kvdata (json-encode kvdata) nil)))
188 (with-current-buffer (url-retrieve-synchronously url)
189 (let ((data (gnus-sync-lesync-parse)))
190 (gnus-message 12 "gnus-sync-lesync-call: %s URL %s sent %S got %S"
191 method url `((headers . ,headers) (data ,kvdata)) data)
192 (kill-buffer (current-buffer))
193 data))))
195 (defun gnus-sync-lesync-PUT (url headers &optional data)
196 (gnus-sync-lesync-call url "PUT" headers data))
198 (defun gnus-sync-lesync-POST (url headers &optional data)
199 (gnus-sync-lesync-call url "POST" headers data))
201 (defun gnus-sync-lesync-GET (url headers &optional data)
202 (gnus-sync-lesync-call url "GET" headers data))
204 (defun gnus-sync-lesync-DELETE (url headers &optional data)
205 (gnus-sync-lesync-call url "DELETE" headers data))
207 ;; this is not necessary with newer versions of json.el but 1.2 or older
208 ;; (which are in Emacs 24.1 and earlier) need it
209 (defun gnus-sync-json-alist-p (list)
210 "Non-null if and only if LIST is an alist."
211 (while (consp list)
212 (setq list (if (consp (car list))
213 (cdr list)
214 'not-alist)))
215 (null list))
217 ;; this is not necessary with newer versions of json.el but 1.2 or older
218 ;; (which are in Emacs 24.1 and earlier) need it
219 (defun gnus-sync-json-plist-p (list)
220 "Non-null if and only if LIST is a plist."
221 (while (consp list)
222 (setq list (if (and (keywordp (car list))
223 (consp (cdr list)))
224 (cddr list)
225 'not-plist)))
226 (null list))
228 ; (gnus-sync-lesync-setup "http://lesync.info:5984/tzz" "tzzadmin" "mypassword" "mysalt" t t)
229 ; (gnus-sync-lesync-setup "http://lesync.info:5984/tzz")
231 (defun gnus-sync-lesync-setup (url &optional user password salt reader admin)
232 (interactive "sEnter URL to set up: ")
233 "Set up the LeSync database at URL.
234 Install USER as a READER and/or an ADMIN in the security object
235 under \"_security\", and in the CouchDB \"_users\" table using
236 PASSWORD and SALT. Only one USER is thus supported for now.
237 When SALT is nil, a random one will be generated using `random'."
238 (let* ((design-url (concat url gnus-sync-lesync-design-prefix))
239 (security-object (concat url "/_security"))
240 (user-record `((names . [,user]) (roles . [])))
241 (couch-user-name (format "org.couchdb.user:%s" user))
242 (salt (or salt (sha1 (format "%s" (random)))))
243 (couch-user-record
244 `((_id . ,couch-user-name)
245 (type . user)
246 (name . ,(format "%s" user))
247 (roles . [])
248 (salt . ,salt)
249 (password_sha . ,(when password
250 (sha1
251 (format "%s%s" password salt))))))
252 (rev (progn
253 (gnus-sync-lesync-find-prop 'rev design-url design-url)
254 (gnus-sync-lesync-get-prop 'rev design-url)))
255 (latest-func "function(head,req)
257 var tosend = [];
258 var row;
259 var ftime = (req.query['ftime'] || 0);
260 while (row = getRow())
262 if (row.value['float-time'] > ftime)
264 var s = row.value['_id'];
265 if (s) tosend.push('\"'+s.replace('\"', '\\\"')+'\"');
268 send('['+tosend.join(',') + ']');
270 ;; <key>read</key>
271 ;; <dict>
272 ;; <key>de.alt.fan.ipod</key>
273 ;; <array>
274 ;; <integer>1</integer>
275 ;; <integer>2</integer>
276 ;; <dict>
277 ;; <key>start</key>
278 ;; <integer>100</integer>
279 ;; <key>length</key>
280 ;; <integer>100</integer>
281 ;; </dict>
282 ;; </array>
283 ;; </dict>
284 (xmlplistread-func "function(head, req) {
285 var row;
286 start({ 'headers': { 'Content-Type': 'text/xml' } });
288 send('<dict>');
289 send('<key>read</key>');
290 send('<dict>');
291 while(row = getRow())
293 var read = row.value.read;
294 if (read && read[0] && read[0] == 'invlist')
296 send('<key>'+row.key+'</key>');
297 //send('<invlist>'+read+'</invlist>');
298 send('<array>');
300 var from = 0;
301 var flip = false;
303 for (var i = 1; i < read.length && read[i]; i++)
305 var cur = read[i];
306 if (flip)
308 if (from == cur-1)
310 send('<integer>'+read[i]+'</integer>');
312 else
314 send('<dict>');
315 send('<key>start</key>');
316 send('<integer>'+from+'</integer>');
317 send('<key>end</key>');
318 send('<integer>'+(cur-1)+'</integer>');
319 send('</dict>');
323 flip = ! flip;
324 from = cur;
326 send('</array>');
330 send('</dict>');
331 send('</dict>');
334 (subs-func "function(doc){emit([doc._id, doc.source], doc._rev);}")
335 (revs-func "function(doc){emit(doc._id, doc._rev);}")
336 (bytimesubs-func "function(doc)
337 {emit([(doc['float-time']||0), doc._id], doc._rev);}")
338 (bytime-func "function(doc)
339 {emit([(doc['float-time']||0), doc._id], doc);}")
340 (groups-func "function(doc){emit(doc._id, doc);}"))
341 (and (if user
342 (and (assq 'ok (gnus-sync-lesync-PUT
343 security-object
345 (append (and reader
346 (list `(readers . ,user-record)))
347 (and admin
348 (list `(admins . ,user-record))))))
349 (assq 'ok (gnus-sync-lesync-PUT
350 (concat (file-name-directory url)
351 "_users/"
352 couch-user-name)
354 couch-user-record)))
356 (assq 'ok (gnus-sync-lesync-PUT
357 design-url
359 `(,@(when rev (list (cons '_rev rev)))
360 (lists . ((latest . ,latest-func)
361 (xmlplistread . ,xmlplistread-func)))
362 (views . ((subs . ((map . ,subs-func)))
363 (revs . ((map . ,revs-func)))
364 (bytimesubs . ((map . ,bytimesubs-func)))
365 (bytime . ((map . ,bytime-func)))
366 (groups . ((map . ,groups-func)))))))))))
368 (defun gnus-sync-lesync-find-prop (prop url key)
369 "Retrieve a PROPerty of a document KEY at URL.
370 Calls `gnus-sync-lesync-set-prop'.
371 For the 'rev PROP, uses '_rev against the document."
372 (gnus-sync-lesync-set-prop
373 prop key (cdr (assq (if (eq prop 'rev) '_rev prop)
374 (gnus-sync-lesync-GET url nil)))))
376 (defun gnus-sync-lesync-set-prop (prop key val)
377 "Update the PROPerty of document KEY at URL to VAL.
378 Updates `gnus-sync-lesync-props-hash'."
379 (puthash (format "%s.%s" key prop) val gnus-sync-lesync-props-hash))
381 (defun gnus-sync-lesync-get-prop (prop key)
382 "Get the PROPerty of KEY from `gnus-sync-lesync-props-hash'."
383 (gethash (format "%s.%s" key prop) gnus-sync-lesync-props-hash))
385 (defun gnus-sync-deep-print (data)
386 (let* ((print-quoted t)
387 (print-readably t)
388 (print-escape-multibyte nil)
389 (print-escape-nonascii t)
390 (print-length nil)
391 (print-level nil)
392 (print-circle nil)
393 (print-escape-newlines t))
394 (format "%S" data)))
396 (defun gnus-sync-newsrc-loader-builder (&optional only-modified)
397 (let* ((entries (cdr gnus-newsrc-alist))
398 entry name ret)
399 (while entries
400 (setq entry (pop entries)
401 name (car entry))
402 (when (gnus-grep-in-list name gnus-sync-newsrc-groups)
403 (if only-modified
404 (when (not (equal (gnus-sync-deep-print entry)
405 (gnus-sync-lesync-get-prop 'checksum name)))
406 (gnus-message 9 "%s: add %s, it's modified"
407 "gnus-sync-newsrc-loader-builder" name)
408 (push entry ret))
409 (push entry ret))))
410 ret))
412 ; (json-encode (gnus-sync-range2invlist '((1 . 47137) (47139 . 47714) 48129 48211 49231 49281 49342 49473 49475 49502)))
413 (defun gnus-sync-range2invlist (ranges)
414 (append '(invlist)
415 (let ((ranges (delq nil ranges))
416 ret range from to)
417 (while ranges
418 (setq range (pop ranges))
419 (if (atom range)
420 (setq from range
421 to range)
422 (setq from (car range)
423 to (cdr range)))
424 (push from ret)
425 (push (1+ to) ret))
426 (reverse ret))))
428 ; (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))
429 (defun gnus-sync-invlist2range (inv)
430 (setq inv (append inv nil))
431 (if (equal (format "%s" (car inv)) "invlist")
432 (let ((i (cdr inv))
433 (start 0)
434 ret cur top flip)
435 (while i
436 (setq cur (pop i))
437 (when flip
438 (setq top (1- cur))
439 (if (= start top)
440 (push start ret)
441 (push (cons start top) ret)))
442 (setq flip (not flip))
443 (setq start cur))
444 (reverse ret))
445 inv))
447 (defun gnus-sync-position (search list &optional test)
448 "Find the position of SEARCH in LIST using TEST, defaulting to `eq'."
449 (let ((pos 0)
450 (test (or test 'eq)))
451 (while (and list (not (funcall test (car list) search)))
452 (pop list)
453 (incf pos))
454 (if (funcall test (car list) search) pos nil)))
456 (defun gnus-sync-topic-group-position (group topic-name)
457 (gnus-sync-position
458 group (cdr (assoc topic-name gnus-topic-alist)) 'equal))
460 (defun gnus-sync-fix-topic-group-position (group topic-name position)
461 (unless (equal position (gnus-sync-topic-group-position group topic-name))
462 (let* ((loc "gnus-sync-fix-topic-group-position")
463 (groups (delete group (cdr (assoc topic-name gnus-topic-alist))))
464 (position (min position (1- (length groups))))
465 (old (nth position groups)))
466 (when (and old (not (equal old group)))
467 (setf (nth position groups) group)
468 (setcdr (assoc topic-name gnus-topic-alist)
469 (append groups (list old)))
470 (gnus-message 9 "%s: %s moved to %d, swap with %s"
471 loc group position old)))))
473 (defun gnus-sync-lesync-pre-save-group-entry (url nentry &rest passed-props)
474 (let* ((loc "gnus-sync-lesync-save-group-entry")
475 (k (car nentry))
476 (revision (gnus-sync-lesync-get-prop 'rev k))
477 (sname gnus-sync-lesync-name)
478 (topic (gnus-group-topic k))
479 (topic-offset (gnus-sync-topic-group-position k topic))
480 (sources (gnus-sync-lesync-get-prop 'source k)))
481 ;; set the revision so we don't have a conflict
482 `(,@(when revision
483 (list (cons '_rev revision)))
484 (_id . ,k)
485 ;; the time we saved
486 ,@passed-props
487 ;; add our name to the sources list for this key
488 (source ,@(if (member gnus-sync-lesync-name sources)
489 sources
490 (cons gnus-sync-lesync-name sources)))
491 ,(cons 'level (nth 1 nentry))
492 ,@(if topic (list (cons 'topic topic)) nil)
493 ,@(if topic-offset (list (cons 'topic-offset topic-offset)) nil)
494 ;; the read marks
495 ,(cons 'read (gnus-sync-range2invlist (nth 2 nentry)))
496 ;; the other marks
497 ,@(delq nil (mapcar (lambda (mark-entry)
498 (gnus-message 12 "%s: prep param %s in %s"
500 (car mark-entry)
501 (nth 3 nentry))
502 (if (listp (cdr mark-entry))
503 (cons (car mark-entry)
504 (gnus-sync-range2invlist
505 (cdr mark-entry)))
506 (progn ; else this is not a list
507 (gnus-message 9 "%s: non-list param %s in %s"
509 (car mark-entry)
510 (nth 3 nentry))
511 nil)))
512 (nth 3 nentry))))))
514 (defun gnus-sync-lesync-post-save-group-entry (url entry)
515 (let* ((loc "gnus-sync-lesync-post-save-group-entry")
516 (k (cdr (assq 'id entry))))
517 (cond
518 ;; success!
519 ((and (assq 'rev entry) (assq 'id entry))
520 (progn
521 (gnus-sync-lesync-set-prop 'rev k (cdr (assq 'rev entry)))
522 (gnus-sync-lesync-set-prop 'checksum
524 (gnus-sync-deep-print
525 (assoc k gnus-newsrc-alist)))
526 (gnus-message 9 "%s: successfully synced %s to %s"
527 loc k url)))
528 ;; specifically check for document conflicts
529 ((equal "conflict" (format "%s" (cdr-safe (assq 'error entry))))
530 (gnus-error
532 "%s: use `%s' to resolve the conflict synchronizing %s to %s: %s"
533 loc "gnus-sync-read" k url (cdr (assq 'reason entry))))
534 ;; generic errors
535 ((assq 'error entry)
536 (gnus-error 1 "%s: got error while synchronizing %s to %s: %s"
537 loc k url (cdr (assq 'reason entry))))
540 (gnus-message 2 "%s: unknown sync status after %s to %s: %S"
541 loc k url entry)))
542 (assoc 'error entry)))
544 (defun gnus-sync-lesync-groups-builder (url)
545 (let ((u (concat url gnus-sync-lesync-design-prefix "/_view/groups")))
546 (cdr (assq 'rows (gnus-sync-lesync-GET u nil)))))
548 (defun gnus-sync-subscribe-group (name)
549 "Subscribe to group NAME. Returns NAME on success, nil otherwise."
550 (gnus-subscribe-newsgroup name))
552 (defun gnus-sync-lesync-read-group-entry (url name entry &rest passed-props)
553 "Read ENTRY information for NAME. Returns NAME if successful.
554 Skips entries whose sources don't contain
555 `gnus-sync-lesync-name'. When the alist PASSED-PROPS has a
556 `subscribe-all' element that evaluates to true, we attempt to
557 subscribe to unknown groups. The user is also allowed to delete
558 unwanted groups via the LeSync URL."
559 (let* ((loc "gnus-sync-lesync-read-group-entry")
560 (entry (gnus-sync-lesync-normalize-group-entry entry passed-props))
561 (subscribe-all (cdr (assq 'subscribe-all passed-props)))
562 (sources (cdr (assq 'source entry)))
563 (rev (cdr (assq 'rev entry)))
564 (in-sources (member gnus-sync-lesync-name sources))
565 (known (assoc name gnus-newsrc-alist))
566 cell)
567 (unless known
568 (if (and subscribe-all
569 (y-or-n-p (format "Subscribe to group %s?" name)))
570 (setq known (gnus-sync-subscribe-group name)
571 in-sources t)
572 ;; else...
573 (when (y-or-n-p (format "Delete group %s from server?" name))
574 (if (equal name (gnus-sync-lesync-delete-group url name))
575 (gnus-message 1 "%s: removed group %s from server %s"
576 loc name url)
577 (gnus-error 1 "%s: could not remove group %s from server %s"
578 loc name url)))))
579 (when known
580 (unless in-sources
581 (setq in-sources
582 (y-or-n-p
583 (format "Read group %s even though %s is not in sources %S?"
584 name gnus-sync-lesync-name (or sources ""))))))
585 (when rev
586 (gnus-sync-lesync-set-prop 'rev name rev))
588 ;; if the source matches AND we have this group
589 (if (and known in-sources)
590 (progn
591 (gnus-message 10 "%s: reading LeSync entry %s, sources %S"
592 loc name sources)
593 (while entry
594 (setq cell (pop entry))
595 (let ((k (car cell))
596 (val (cdr cell)))
597 (gnus-sync-lesync-set-prop k name val)))
598 name)
599 ;; else...
600 (unless known
601 (gnus-message 5 "%s: ignoring entry %s, it wasn't subscribed. %s"
602 loc name "Call `gnus-sync-read' with C-u to force it."))
603 (unless in-sources
604 (gnus-message 5 "%s: ignoring entry %s, %s not in sources %S"
605 loc name gnus-sync-lesync-name (or sources "")))
606 nil)))
608 (defun gnus-sync-lesync-install-group-entry (name)
609 (let* ((master (assoc name gnus-newsrc-alist))
610 (old-topic-name (gnus-group-topic name))
611 (old-topic (assoc old-topic-name gnus-topic-alist))
612 (target-topic-name (gnus-sync-lesync-get-prop 'topic name))
613 (target-topic-offset (gnus-sync-lesync-get-prop 'topic-offset name))
614 (target-topic (assoc target-topic-name gnus-topic-alist))
615 (loc "gnus-sync-lesync-install-group-entry"))
616 (if master
617 (progn
618 (when (eq 'ask gnus-sync-lesync-install-topics)
619 (setq gnus-sync-lesync-install-topics
620 (y-or-n-p "Install topics from LeSync?")))
621 (when (and (eq t gnus-sync-lesync-install-topics)
622 target-topic-name)
623 (if (equal old-topic-name target-topic-name)
624 (gnus-message 12 "%s: %s is already in topic %s"
625 loc name target-topic-name)
626 ;; see `gnus-topic-move-group'
627 (when (and old-topic target-topic)
628 (setcdr old-topic (gnus-delete-first name (cdr old-topic)))
629 (gnus-message 5 "%s: removing %s from topic %s"
630 loc name old-topic-name))
631 (unless target-topic
632 (when (y-or-n-p (format "Create missing topic %s?"
633 target-topic-name))
634 (gnus-topic-create-topic target-topic-name nil)
635 (setq target-topic (assoc target-topic-name
636 gnus-topic-alist))))
637 (if target-topic
638 (prog1
639 (nconc target-topic (list name))
640 (gnus-message 5 "%s: adding %s to topic %s"
641 loc name (car target-topic))
642 (gnus-topic-enter-dribble))
643 (gnus-error 2 "%s: LeSync group %s can't go in missing topic %s"
644 loc name target-topic-name)))
645 (when (and target-topic-offset target-topic)
646 (gnus-sync-fix-topic-group-position
647 name target-topic-name target-topic-offset)))
648 ;; install the subscription level
649 (when (gnus-sync-lesync-get-prop 'level name)
650 (setf (nth 1 master) (gnus-sync-lesync-get-prop 'level name)))
651 ;; install the read and other marks
652 (setf (nth 2 master) (gnus-sync-lesync-get-prop 'read name))
653 (setf (nth 3 master) (gnus-sync-lesync-get-prop 'marks name))
654 (gnus-sync-lesync-set-prop 'checksum
655 name
656 (gnus-sync-deep-print master))
657 nil)
658 (gnus-error 1 "%s: invalid LeSync group %s" loc name)
659 'invalid-name)))
661 ; (gnus-sync-lesync-delete-group (cdr gnus-sync-backend) "nntp+Gmane:gwene.org.slashdot")
663 (defun gnus-sync-lesync-delete-group (url name)
664 "Returns NAME if successful deleting it from URL, an error otherwise."
665 (interactive "sEnter URL to set up: \rsEnter group name: ")
666 (let* ((u (concat (cadr gnus-sync-backend) "/" (url-hexify-string name)))
667 (del (gnus-sync-lesync-DELETE
669 `(,@(when (gnus-sync-lesync-get-prop 'rev name)
670 (list (cons "If-Match"
671 (gnus-sync-lesync-get-prop 'rev name))))))))
672 (or (cdr (assq 'id del)) del)))
674 ;;; (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)))
676 (defun gnus-sync-lesync-normalize-group-entry (entry &optional passed-props)
677 (let (ret
678 marks
679 cell)
680 (setq entry (append passed-props entry))
681 (while (setq cell (pop entry))
682 (let ((k (car cell))
683 (val (cdr cell)))
684 (cond
685 ((eq k 'read)
686 (push (cons k (gnus-sync-invlist2range val)) ret))
687 ;; we ignore these parameters
688 ((member k '(_id subscribe-all _deleted_conflicts))
689 nil)
690 ((eq k '_rev)
691 (push (cons 'rev val) ret))
692 ((eq k 'source)
693 (push (cons 'source (append val nil)) ret))
694 ((or (eq k 'float-time)
695 (eq k 'level)
696 (eq k 'topic)
697 (eq k 'topic-offset)
698 (eq k 'read-time))
699 (push (cons k val) ret))
700 ;;; "How often have I said to you that when you have eliminated the
701 ;;; impossible, whatever remains, however improbable, must be the
702 ;;; truth?" --Sherlock Holmes
703 ;; everything remaining must be a mark
704 (t (push (cons k (gnus-sync-invlist2range val)) marks)))))
705 (cons (cons 'marks marks) ret)))
707 (defun gnus-sync-save (&optional force)
708 "Save the Gnus sync data to the backend.
709 With a prefix, FORCE is set and all groups will be saved."
710 (interactive "P")
711 (cond
712 ((and (listp gnus-sync-backend)
713 (eq (nth 0 gnus-sync-backend) 'lesync)
714 (stringp (nth 1 gnus-sync-backend)))
716 ;; refresh the revisions if we're forcing the save
717 (when force
718 (mapc (lambda (entry)
719 (when (and (assq 'key entry)
720 (assq 'value entry))
721 (gnus-sync-lesync-set-prop
722 'rev
723 (cdr (assq 'key entry))
724 (cdr (assq 'value entry)))))
725 ;; the revs view is key = name, value = rev
726 (cdr (assq 'rows (gnus-sync-lesync-GET
727 (concat (nth 1 gnus-sync-backend)
728 gnus-sync-lesync-design-prefix
729 "/_view/revs")
730 nil)))))
732 (let* ((ftime (float-time))
733 (url (nth 1 gnus-sync-backend))
734 (entries
735 (mapcar (lambda (entry)
736 (gnus-sync-lesync-pre-save-group-entry
737 (cadr gnus-sync-backend)
738 entry
739 (cons 'float-time ftime)))
740 (gnus-sync-newsrc-loader-builder (not force))))
741 ;; when there are no entries, there's nothing to save
742 (sync (if entries
743 (gnus-sync-lesync-POST
744 (concat url "/_bulk_docs")
745 '(("Content-Type" . "application/json"))
746 `((docs . ,(vconcat entries nil))))
747 (gnus-message
748 2 "gnus-sync-save: nothing to save to the LeSync backend")
749 nil)))
750 (mapcar (lambda (e) (gnus-sync-lesync-post-save-group-entry url e))
751 sync)))
752 ((stringp gnus-sync-backend)
753 (gnus-message 7 "gnus-sync-save: saving to backend %s" gnus-sync-backend)
754 ;; populate gnus-sync-newsrc-loader from all but the first dummy
755 ;; entry in gnus-newsrc-alist whose group matches any of the
756 ;; gnus-sync-newsrc-groups
757 ;; TODO: keep the old contents for groups we don't have!
758 (let ((gnus-sync-newsrc-loader
759 (loop for entry in (cdr gnus-newsrc-alist)
760 when (gnus-grep-in-list
761 (car entry) ;the group name
762 gnus-sync-newsrc-groups)
763 collect (cons (car entry)
764 (mapcar (lambda (offset)
765 (cons offset (nth offset entry)))
766 gnus-sync-newsrc-offsets)))))
767 (with-temp-file gnus-sync-backend
768 (progn
769 (let ((coding-system-for-write gnus-ding-file-coding-system)
770 (standard-output (current-buffer)))
771 (when gnus-sync-file-encrypt-to
772 (set (make-local-variable 'epa-file-encrypt-to)
773 gnus-sync-file-encrypt-to))
774 (princ (format ";; -*- mode:emacs-lisp; coding: %s; -*-\n"
775 gnus-ding-file-coding-system))
776 (princ ";; Gnus sync data v. 0.0.1\n")
777 ;; TODO: replace with `gnus-sync-deep-print'
778 (let* ((print-quoted t)
779 (print-readably t)
780 (print-escape-multibyte nil)
781 (print-escape-nonascii t)
782 (print-length nil)
783 (print-level nil)
784 (print-circle nil)
785 (print-escape-newlines t)
786 (variables (cons 'gnus-sync-newsrc-loader
787 gnus-sync-global-vars))
788 variable)
789 (while variables
790 (if (and (boundp (setq variable (pop variables)))
791 (symbol-value variable))
792 (progn
793 (princ "\n(setq ")
794 (princ (symbol-name variable))
795 (princ " '")
796 (prin1 (symbol-value variable))
797 (princ ")\n"))
798 (princ "\n;;; skipping empty variable ")
799 (princ (symbol-name variable)))))
800 (gnus-message
802 "gnus-sync-save: stored variables %s and %d groups in %s"
803 gnus-sync-global-vars
804 (length gnus-sync-newsrc-loader)
805 gnus-sync-backend)
807 ;; Idea from Dan Christensen <jdc@chow.mat.jhu.edu>
808 ;; Save the .eld file with extra line breaks.
809 (gnus-message 8 "gnus-sync-save: adding whitespace to %s"
810 gnus-sync-backend)
811 (save-excursion
812 (goto-char (point-min))
813 (while (re-search-forward "^(\\|(\\\"" nil t)
814 (replace-match "\n\\&" t))
815 (goto-char (point-min))
816 (while (re-search-forward " $" nil t)
817 (replace-match "" t t))))))))
818 ;; the pass-through case: gnus-sync-backend is not a known choice
819 (nil)))
821 (defun gnus-sync-read (&optional subscribe-all)
822 "Load the Gnus sync data from the backend.
823 With a prefix, SUBSCRIBE-ALL is set and unknown groups will be subscribed."
824 (interactive "P")
825 (when gnus-sync-backend
826 (gnus-message 7 "gnus-sync-read: loading from backend %s" gnus-sync-backend)
827 (cond
828 ((and (listp gnus-sync-backend)
829 (eq (nth 0 gnus-sync-backend) 'lesync)
830 (stringp (nth 1 gnus-sync-backend)))
831 (let ((errored nil)
832 name ftime)
833 (mapc (lambda (entry)
834 (setq name (cdr (assq 'id entry)))
835 ;; set ftime the FIRST time through this loop, that
836 ;; way it reflects the time we FINISHED reading
837 (unless ftime (setq ftime (float-time)))
839 (unless errored
840 (setq errored
841 (when (equal name
842 (gnus-sync-lesync-read-group-entry
843 (nth 1 gnus-sync-backend)
844 name
845 (cdr (assq 'value entry))
846 `(read-time ,ftime)
847 `(subscribe-all ,subscribe-all)))
848 (gnus-sync-lesync-install-group-entry
849 (cdr (assq 'id entry)))))))
850 (gnus-sync-lesync-groups-builder (nth 1 gnus-sync-backend)))))
852 ((stringp gnus-sync-backend)
853 ;; read data here...
854 (if (or debug-on-error debug-on-quit)
855 (load gnus-sync-backend nil t)
856 (condition-case var
857 (load gnus-sync-backend nil t)
858 (error
859 (error "Error in %s: %s" gnus-sync-backend (cadr var)))))
860 (let ((valid-count 0)
861 invalid-groups)
862 (dolist (node gnus-sync-newsrc-loader)
863 (if (gnus-gethash (car node) gnus-newsrc-hashtb)
864 (progn
865 (incf valid-count)
866 (loop for store in (cdr node)
867 do (setf (nth (car store)
868 (assoc (car node) gnus-newsrc-alist))
869 (cdr store))))
870 (push (car node) invalid-groups)))
871 (gnus-message
873 "gnus-sync-read: loaded %d groups (out of %d) from %s"
874 valid-count (length gnus-sync-newsrc-loader)
875 gnus-sync-backend)
876 (when invalid-groups
877 (gnus-message
879 "gnus-sync-read: skipped %d groups (out of %d) from %s"
880 (length invalid-groups)
881 (length gnus-sync-newsrc-loader)
882 gnus-sync-backend)
883 (gnus-message 9 "gnus-sync-read: skipped groups: %s"
884 (mapconcat 'identity invalid-groups ", ")))))
885 (nil))
887 (gnus-message 9 "gnus-sync-read: remaking the newsrc hashtable")
888 (gnus-make-hashtable-from-newsrc-alist)))
890 ;;;###autoload
891 (defun gnus-sync-initialize ()
892 "Initialize the Gnus sync facility."
893 (interactive)
894 (gnus-message 5 "Initializing the sync facility")
895 (gnus-sync-install-hooks))
897 ;;;###autoload
898 (defun gnus-sync-install-hooks ()
899 "Install the sync hooks."
900 (interactive)
901 ;; (add-hook 'gnus-get-new-news-hook 'gnus-sync-read)
902 ;; (add-hook 'gnus-read-newsrc-el-hook 'gnus-sync-read)
903 (add-hook 'gnus-save-newsrc-hook 'gnus-sync-save))
905 (defun gnus-sync-unload-hook ()
906 "Uninstall the sync hooks."
907 (interactive)
908 (remove-hook 'gnus-save-newsrc-hook 'gnus-sync-save))
910 (add-hook 'gnus-sync-unload-hook 'gnus-sync-unload-hook)
912 (when gnus-sync-backend (gnus-sync-initialize))
914 (provide 'gnus-sync)
916 ;;; gnus-sync.el ends here