unofficial mirror of bug-guix@gnu.org 
 help / color / mirror / code / Atom feed
blob f8c392d3e5e2b91e8a8d2e9fac763bbe5721dd91 15196 bytes (raw)
name: gnu/build/jami-service.scm 	 # note: path name is non-authoritative(*)

  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
 
;;; GNU Guix --- Functional package management for GNU
;;; Copyright © 2021 Maxim Cournoyer <maxim.cournoyer@gmail.com>
;;;
;;; This file is part of GNU Guix.
;;;
;;; GNU Guix 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 3 of the License, or (at
;;; your option) any later version.
;;;
;;; GNU Guix 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 Guix.  If not, see <http://www.gnu.org/licenses/>.

;;; Commentary:
;;;
;;; This module contains helpers used as part of the jami-service-type
;;; definition.
;;;
;;; Code:

(define-module (gnu build jami-service)
  #:use-module (gnu build dbus-service)
  #:use-module (ice-9 format)
  #:use-module (ice-9 match)
  #:use-module (ice-9 rdelim)
  #:use-module (ice-9 regex)
  #:use-module (srfi srfi-1)
  #:use-module (srfi srfi-26)
  #:export (account-fingerprint?
            account-details->recutil
            get-accounts
            get-usernames
            set-account-details
            add-account
            account->username
            username->account
            username->contacts
            enable-account
            disable-account

            add-contact
            remove-contact

            set-all-moderators
            set-moderator
            username->all-moderators?
            username->moderators))

;;;
;;; Utilities.
;;;

(define (alist->list alist)
  "Flatten ALIST into a list."
  (append-map (match-lambda
                (() '())
                ((key . value)
                 (list key value)))
              alist))

(define account-fingerprint-rx (make-regexp "[0-9A-Fa-f]{40}"))

(define (account-fingerprint? val)
  "A Jami account fingerprint is 40 characters long and only contains
hexadecimal characters."
  (and (string? val)
       (regexp-exec account-fingerprint-rx val)))

\f
;;;
;;; Low-level, D-Bus-related procedures.
;;;

(define (parse-account-ids reply)
  "Return the Jami account IDs from REPLY, which is assumed to be the output
of the Jami D-Bus `getAccountList' method."
  (dbus-array->list (parse-dbus-reply reply)))

(define (parse-account-details reply)
  "Parse REPLY, which is assumed to be the output of the Jami D-Bus
`getAccountDetails' method, and return its content as an alist."
  (dbus-dict->alist (parse-dbus-reply reply)))

(define (parse-contacts reply)
  "Parse REPLY, which is assumed to be the output of the Jamid D-Bus
`getContacts' method, and return its content as an alist."
  (match (parse-dbus-reply reply)
    ('array
     '())
    (('array dicts ...)
     (map dbus-dict->alist dicts))))

\f
;;;
;;; Higher-level, D-Bus-related procedures.
;;;

(define (validate-fingerprint fingerprint)
  "Validate that fingerprint is 40 characters long."
  (unless (account-fingerprint? fingerprint)
    (error "Account fingerprint is not valid:" fingerprint)))

(define* (send-dbus/configuration-manager #:key method arguments timeout)
  "Query the Jami D-Bus ConfigurationManager service."
  (send-dbus #:service "cx.ring.Ring"
             #:path "/cx/ring/Ring/ConfigurationManager"
             #:interface "cx.ring.Ring.ConfigurationManager"
             #:method method
             #:arguments arguments
             #:timeout timeout))

;;; The following methods are for internal use; they make use of the account
;;; ID, an implementation detail of Jami the user should not need to be
;;; concerned with.
(define (get-account-ids)
  "Return the available Jami account identifiers (IDs).  Account IDs are an
implementation detail used to identify the accounts in Jami."
  (parse-account-ids
   (send-dbus/configuration-manager #:method "getAccountList")))

(define (id->account-details id)
  "Retrieve the account data associated with the given account ID."
  (parse-account-details
   (send-dbus/configuration-manager
    #:method "getAccountDetails"
    #:arguments (list (string-append "string:" id)))))

(define (id->volatile-account-details id)
  "Retrieve the account data associated with the given account ID."
  (parse-account-details
   (send-dbus/configuration-manager
    #:method "getVolatileAccountDetails"
    #:arguments (list (string-append "string:" id)))))

(define (id->account id)
  "Retrieve the complete account data associated with the given account ID."
  (append (id->volatile-account-details id)
          (id->account-details id)))

(define %username-to-id-cache #f)

(define (invalidate-username-to-id-cache!)
  (set! %username-to-id-cache #f))

(define (username->id username)
  "Return the first account ID corresponding to USERNAME."
  (unless (assoc-ref %username-to-id-cache username)
    (set! %username-to-id-cache
          (append-map
           (lambda (id)
             (let* ((account (id->account id))
                    (username (assoc-ref account "Account.username"))
                    (registered-name (assoc-ref account
                                                "Account.registeredName")))
               `(,@(if username
                       (list (cons username id))
                       '())
                 ,@(if registered-name
                       (list (cons registered-name id))
                       '()))))
           (get-account-ids))))
  (or (assoc-ref %username-to-id-cache username)
      (let ((message (format #f "Could not retrieve a local account ID\
 for ~:[username~;fingerprint~]" (account-fingerprint? username))))
        (error message username))))

(define (account->username account)
  "Return USERNAME, the registered username associated with ACCOUNT, else its
public key fingerprint."
  (or (assoc-ref account "Account.registeredName")
      (assoc-ref account "Account.username")))

(define (id->username id)
  "Return USERNAME, the registered username associated with ID, else its
public key fingerprint, else #f."
  (account->username (id->account id)))

(define (get-accounts)
  "Return the list of all accounts, as a list of alists."
  (map id->account (get-account-ids)))

(define (get-usernames)
  "Return the list of the usernames associated with the present accounts."
  (map account->username (get-accounts)))

(define (username->account username)
  "Return the first account associated with USERNAME, else #f.
USERNAME can be either the account 40 characters public key fingerprint or a
registered username."
  (find (lambda (account)
          (member username
                  (list (assoc-ref account "Account.username")
                        (assoc-ref account "Account.registeredName"))))
        (get-accounts)))

(define (add-account archive)
  "Import the Jami account ARCHIVE and return its account ID.  The archive
should *not* be encrypted with a password.  Return the username associated
with the account."
  (invalidate-username-to-id-cache!)
  (let ((reply (send-dbus/configuration-manager
                #:method "addAccount"
                #:arguments (list (string-append
                                   "dict:string:string:Account.archivePath,"
                                   archive
                                   ",Account.type,RING")))))
    ;; The account information takes some time to be populated.
    (let ((id (deserialize-item (parse-dbus-reply reply))))
      (with-retries 20 1
        (let ((username (id->username id)))
          (if (string-null? username)
              #f
              username))))))

(define (remove-account username)
  "Delete the Jami account associated with USERNAME, the account 40 characters
fingerprint or a registered username."
  (let ((id (username->id username)))
    (send-dbus/configuration-manager
     #:method "removeAccount"
     #:arguments (list (string-append "string:" id))))
  (invalidate-username-to-id-cache!))

(define* (username->contacts username)
  "Return the contacts associated with the account of USERNAME as two values;
the first one being the regular contacts and the second one the banned
contacts.  USERNAME can be either the account 40 characters public key
fingerprint or a registered username.  The contacts returned are represented
using their 40 characters fingerprint."
  (let* ((id (username->id username))
         (reply (send-dbus/configuration-manager
                 #:method "getContacts"
                 #:arguments (list (string-append "string:" id))))
         (all-contacts (parse-contacts reply))
         (banned? (lambda (contact)
                    (and=> (assoc-ref contact "banned")
                           (cut string=? "true" <>))))
         (banned (filter banned? all-contacts))
         (not-banned (filter (negate banned?) all-contacts))
         (fingerprint (cut assoc-ref <> "id")))
    (values (map fingerprint not-banned)
            (map fingerprint banned))))

(define* (remove-contact contact username #:key ban?)
  "Remove CONTACT, the 40 characters public key fingerprint of a contact, from
the account associated with USERNAME (either a fingerprint or a registered
username).  When BAN? is true, also mark the contact as banned."
  (validate-fingerprint contact)
  (let ((id (username->id username)))
    (send-dbus/configuration-manager
     #:method "removeContact"
     #:arguments (list (string-append "string:" id)
                       (string-append "string:" contact)
                       (serialize-boolean ban?)))))

(define (add-contact contact username)
  "Add CONTACT, the 40 characters public key fingerprint of a contact, to the
account of USERNAME (either a fingerprint or a registered username)."
  (validate-fingerprint contact)
  (let ((id (username->id username)))
    (send-dbus/configuration-manager
     #:method "addContact"
     #:arguments (list (string-append "string:" id)
                       (string-append "string:" contact)))))

(define* (set-account-details details username #:key timeout)
  "Set DETAILS, an alist containing the key value pairs to set for the account
of USERNAME, a registered username or account fingerprint.  The value of the
parameters not provided are unchanged.  TIMEOUT is a value in milliseconds to
pass to the `send-dbus/configuration-manager' procedure."
  (let* ((id (username->id username))
         (current-details (id->account-details id))
         (updated-details (map (match-lambda
                                 ((key . value)
                                  (or (and=> (assoc-ref details key)
                                             (cut cons key <>))
                                      (cons key value))))
                               current-details))
         ;; dbus-send does not permit sending null strings (it throws a
         ;; "malformed dictionary" error).  Luckily they seem to have the
         ;; semantic of "default account value" in Jami; so simply drop them.
         (updated-details* (remove (match-lambda
                                     ((_ . value)
                                      (string-null? value)))
                                   updated-details)))
    (send-dbus/configuration-manager
     #:timeout timeout
     #:method "setAccountDetails"
     #:arguments
     (list (string-append "string:" id)
           (string-append "dict:string:string:"
                          (string-join (alist->list updated-details*)
                                       ","))))))

(define (set-all-moderators enabled? username)
  "Set the 'AllModerators' property to enabled? for the account of USERNAME, a
registered username or account fingerprint."
  (let ((id (username->id username)))
    (send-dbus/configuration-manager
     #:method "setAllModerators"
     #:arguments
     (list (string-append "string:" id)
           (serialize-boolean enabled?)))))

(define (username->all-moderators? username)
  "Return the 'AllModerators' property for the account of USERNAME, a
registered username or account fingerprint."
  (let* ((id (username->id username))
         (reply (send-dbus/configuration-manager
                 #:method "isAllModerators"
                 #:arguments
                 (list (string-append "string:" id)))))
    (deserialize-item (parse-dbus-reply reply))))

(define (username->moderators username)
  "Return the moderators for the account of USERNAME, a registered username or
account fingerprint."
  (let* ((id (username->id username))
         (reply (send-dbus/configuration-manager
                 #:method "getDefaultModerators"
                 #:arguments
                 (list (string-append "string:" id)))))
    (dbus-array->list (parse-dbus-reply reply))))

(define (set-moderator contact enabled? username)
  "Set the moderator flag to ENABLED? for CONTACT, the 40 characters public
key fingerprint of a contact for the account of USERNAME, a registered
username or account fingerprint."
  (validate-fingerprint contact)
  (let* ((id (username->id username)))
    (send-dbus/configuration-manager #:method "setDefaultModerator"
                                     #:arguments
                                     (list (string-append "string:" id)
                                           (string-append "string:" contact)
                                           (serialize-boolean enabled?)))))

(define (disable-account username)
  "Disable the account known by USERNAME, a registered username or account
fingerprint."
  (set-account-details '(("Account.enable" . "false")) username
                       ;; Waiting for the reply on this command takes a very
                       ;; long time that trips the default D-Bus timeout value
                       ;; (25 s), for some reason.
                        #:timeout 60000))

(define (enable-account username)
  "Enable the account known by USERNAME, a registered username or account
fingerprint."
  (set-account-details '(("Account.enable" . "true")) username))

\f
;;;
;;; Presentation procedures.
;;;

(define (.->_ text)
  "Map each period character to underscore characters."
  (string-map (match-lambda
                (#\. #\_)
                (c c))
              text))

(define (account-details->recutil account-details)
  "Serialize the account-details alist into a recutil string.  Period
characters in the keys are normalized to underscore to meet Recutils' format
requirements."
  (define (pair->recutil-property pair)
    (match pair
      ((key . value)
       (string-append (.->_ key) ": " value))))

  (define sorted-account-details
    ;; Have the account username, display name and alias appear first, for
    ;; convenience.
    (let ((first-items '("Account.username"
                         "Account.displayName"
                         "Account.alias")))
      (append (map (cut assoc <> account-details) first-items)
              (fold alist-delete account-details first-items))))

  (string-join (map pair->recutil-property sorted-account-details) "\n"))

;; Local Variables:
;; eval: (put 'with-retries 'scheme-indent-function 2)
;; End:

debug log:

solving f8c392d3e5 ...
found f8c392d3e5 in https://yhetil.org/guix-bugs/875yrxyrjr.fsf@gmail.com/
found ddfc8cf937 in https://git.savannah.gnu.org/cgit/guix.git
preparing index
index prepared:
100644 ddfc8cf937254aabbe7baa42482f35ed1535c022	gnu/build/jami-service.scm

applying [1/1] https://yhetil.org/guix-bugs/875yrxyrjr.fsf@gmail.com/
diff --git a/gnu/build/jami-service.scm b/gnu/build/jami-service.scm
index ddfc8cf937..f8c392d3e5 100644

Checking patch gnu/build/jami-service.scm...
Applied patch gnu/build/jami-service.scm cleanly.

index at:
100644 f8c392d3e5e2b91e8a8d2e9fac763bbe5721dd91	gnu/build/jami-service.scm

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/guix.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).