From fbe376b6db019adbcf32a7b869cba600c5503f8e Mon Sep 17 00:00:00 2001
From: Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
Date: Thu, 28 Jun 2018 15:09:02 +0200
Subject: Reintroduce the concept of resource, but named device_id and
 generated.

---
 data/default_config.cfg |  4 ++++
 poezio/config.py        |  1 +
 poezio/connection.py    | 10 ++++++++++
 3 files changed, 15 insertions(+)

diff --git a/data/default_config.cfg b/data/default_config.cfg
index a71fa42c..3b8d17f0 100644
--- a/data/default_config.cfg
+++ b/data/default_config.cfg
@@ -22,6 +22,10 @@ password =
 # manager like secret-tool on gnome, or anything you want
 eval_password =
 
+# This identifies this client over time with the server, to let it optimise
+# offline storage and various other features.
+device_id =
+
 # Path to a PEM certificate file to use for certificate authentication
 # through SASL External. If set, keyfile MUST be provided as well in
 # order to login.
diff --git a/poezio/config.py b/poezio/config.py
index e90cac8b..ed295c92 100644
--- a/poezio/config.py
+++ b/poezio/config.py
@@ -47,6 +47,7 @@ DEFAULT_CONFIG = {
         'data_dir': '',
         'default_nick': '',
         'deterministic_nick_colors': True,
+        'device_id': '',
         'nick_color_aliases': True,
         'display_activity_notifications': False,
         'display_gaming_notifications': False,
diff --git a/poezio/connection.py b/poezio/connection.py
index 8a74635d..c97365f8 100644
--- a/poezio/connection.py
+++ b/poezio/connection.py
@@ -14,6 +14,8 @@ log = logging.getLogger(__name__)
 import getpass
 import subprocess
 import sys
+import base64
+import random
 
 import slixmpp
 from slixmpp.xmlstream import ET
@@ -38,6 +40,13 @@ class Connection(slixmpp.ClientXMPP):
         keyfile = config.get('keyfile')
         certfile = config.get('certfile')
 
+        device_id = config.get('device_id')
+        if not device_id:
+            rng = random.SystemRandom()
+            device_id = base64.urlsafe_b64encode(
+                rng.getrandbits(24).to_bytes(3, 'little')).decode('ascii')
+            config.set_and_save('device_id', device_id)
+
         if config.get('jid'):
             # Field used to know if we are anonymous or not.
             # many features will be handled differently
@@ -72,6 +81,7 @@ class Connection(slixmpp.ClientXMPP):
             jid = config.get('server')
             password = None
         jid = safeJID(jid)
+        jid.resource = '%s-%s' % (jid.resource, device_id) if jid.resource else 'poezio-%s' % device_id
         # TODO: use the system language
         slixmpp.ClientXMPP.__init__(
             self, jid, password, lang=config.get('lang'))
-- 
cgit v1.2.3