Пример #1
0
        private async Task updateDevicesIfNeededAsync(OmemoXmlDevices devicesRemote)
        {
            if (devicesRemote is null)
            {
                devicesRemote = new OmemoXmlDevices();
            }

            bool updateDeviceList = false;

            /**
             * Device id hasn't been set or we have an invalid one. Pick a random, unique one.
             * Versions of UWPX < v.0.41.0.0 had an invalid one.
             * OMEMO allows only device IDs between 1 and (2^31 - 1)
             * https://xmpp.org/extensions/xep-0384.html#usecases-setup
             **/
            if (CONNECTION.account.omemoDeviceId == 0 || CONNECTION.account.omemoDeviceId > 0x7FFFFFFF)
            {
                tmpDeviceId = CryptoUtils.GenerateUniqueDeviceId(devicesRemote.DEVICES.Select(d => d.ID).ToList());
                devicesRemote.DEVICES.Add(new OmemoXmlDevice(tmpDeviceId, CONNECTION.account.omemoDeviceLabel));
                updateDeviceList = true;
            }
            else
            {
                tmpDeviceId = CONNECTION.account.omemoDeviceId;
                OmemoXmlDevice remoteDevice = devicesRemote.DEVICES.Where(d => d.ID == tmpDeviceId).FirstOrDefault();
                // Device does not exist:
                if (remoteDevice is null)
                {
                    devicesRemote.DEVICES.Add(new OmemoXmlDevice(tmpDeviceId, CONNECTION.account.omemoDeviceLabel));
                    updateDeviceList = true;
                }
                // Device label changed:
                else if (!string.Equals(remoteDevice.label, CONNECTION.account.omemoDeviceLabel))
                {
                    remoteDevice.label = CONNECTION.account.omemoDeviceLabel;
                    updateDeviceList   = true;
                }
            }

            if (updateDeviceList)
            {
                await updateDeviceListAsync(devicesRemote);
            }
            else if (!CONNECTION.account.omemoBundleInfoAnnounced || updateDeviceList)
            {
                await announceBundleInfoAsync();
            }
            else
            {
                setState(OmemoHelperState.ENABLED);
            }
            DEVICES = devicesRemote;
            // Store the new list in the DB:
            OMEMO_STORAGE.StoreDevices(DEVICES.toOmemoProtocolAddress(CONNECTION.account.getBareJid()), CONNECTION.account.getBareJid());
        }
Пример #2
0
        private async Task updateDevicesIfNeededAsync(OmemoXmlDevices devicesRemote)
        {
            if (devicesRemote is null)
            {
                devicesRemote = new OmemoXmlDevices();
            }

            bool updateDeviceList = false;

            // Device id hasn't been set. Pick a random, unique one:
            if (CONNECTION.account.omemoDeviceId == 0)
            {
                tmpDeviceId = CryptoUtils.GenerateUniqueDeviceId(devicesRemote.DEVICES.Select(d => d.ID).ToList());
                devicesRemote.DEVICES.Add(new OmemoXmlDevice(tmpDeviceId, CONNECTION.account.omemoDeviceLabel));
                updateDeviceList = true;
            }
            else
            {
                tmpDeviceId = CONNECTION.account.omemoDeviceId;
                OmemoXmlDevice remoteDevice = devicesRemote.DEVICES.Where(d => d.ID == tmpDeviceId).FirstOrDefault();
                // Device does not exist:
                if (remoteDevice is null)
                {
                    devicesRemote.DEVICES.Add(new OmemoXmlDevice(tmpDeviceId, CONNECTION.account.omemoDeviceLabel));
                    updateDeviceList = true;
                }
                // Device label changed:
                else if (!string.Equals(remoteDevice.label, CONNECTION.account.omemoDeviceLabel))
                {
                    remoteDevice.label = CONNECTION.account.omemoDeviceLabel;
                    updateDeviceList   = true;
                }
            }

            if (updateDeviceList)
            {
                await updateDeviceListAsync(devicesRemote);
            }
            else if (!CONNECTION.account.omemoBundleInfoAnnounced || true)
            {
                await announceBundleInfoAsync();
            }
            else
            {
                setState(OmemoHelperState.ENABLED);
            }
            DEVICES = devicesRemote;
        }
        public void SaveDeviceLabel(string deviceLabel, Client client)
        {
            OmemoAccountInformationModel omemoInfo = client.dbAccount.omemoInfo;

            deviceLabel = deviceLabel.Trim();

            string newLabel;

            if (string.IsNullOrEmpty(deviceLabel) || string.Equals(deviceLabel, omemoInfo.deviceId.ToString()))
            {
                newLabel = null;
            }
            else
            {
                newLabel = deviceLabel;
            }

            // Prevent unnecessary updates in case for example the control loaded:
            if (string.Equals(newLabel, omemoInfo.deviceLabel))
            {
                return;
            }

            MODEL.Saving = true;
            Task.Run(async() =>
            {
                try
                {
                    if (client.xmppClient.isConnected())
                    {
                        MessageResponseHelperResult <IQMessage> result = await client.xmppClient.OMEMO_COMMAND_HELPER.requestDeviceListAsync(client.dbAccount.bareJid);
                        if (result.STATE == MessageResponseHelperResultState.SUCCESS)
                        {
                            if (result.RESULT is OmemoDeviceListResultMessage deviceListResultMessage)
                            {
                                OmemoXmlDevice device = deviceListResultMessage.DEVICES.DEVICES.Where(d => d.ID == omemoInfo.deviceId).FirstOrDefault();
                                if (device is null)
                                {
                                    device = new OmemoXmlDevice(omemoInfo.deviceId, deviceLabel);
                                    deviceListResultMessage.DEVICES.DEVICES.Add(device);
                                }
                                else if (string.Equals(device.label, deviceLabel))
                                {
                                    MODEL.ErrorSaving = false;
                                    MODEL.Saving      = false;
                                    Logger.Info("No need to update devices. Label already the same.");
                                    return;
                                }
                                else
                                {
                                    device.label = newLabel;
                                    result       = await client.xmppClient.OMEMO_COMMAND_HELPER.setDeviceListAsync(deviceListResultMessage.DEVICES);
                                    if (result.STATE != MessageResponseHelperResultState.SUCCESS)
                                    {
                                        Logger.Error("Failed to set device list (" + result.STATE + ").");
                                    }
                                    else if (result.RESULT is IQErrorMessage errorMessage)
                                    {
                                        Logger.Error("Failed to set device list (" + errorMessage.ERROR_OBJ.ToString() + ").");
                                    }
                                    else
                                    {
                                        MODEL.ErrorSaving     = false;
                                        omemoInfo.deviceLabel = newLabel;
                                        omemoInfo.Update();
                                        MODEL.Saving = false;
                                        Logger.Info($"Device label for device {omemoInfo.deviceId} successfully updated to: '{newLabel}'.");
                                        return;
                                    }
                                }
                            }
                            else
                            {
                                Logger.Error("Failed to request device list (" + result.RESULT.ToString() + ").");
                            }
                        }
                        else
                        {
                            Logger.Error("Failed to request device list (" + result.STATE.ToString() + ").");
                        }
                    }
                    else
                    {
                        Logger.Error("Failed to update device label. Client not connected");
                    }
                }
                catch (Exception e)
                {
                    Logger.Error("Failed to update device label.", e);
                }
                MODEL.Saving      = false;
                MODEL.ErrorSaving = true;
            });
        }