示例#1
0
 public void UpdateView(OmemoDeviceModel device)
 {
     if (!(device is null))
     {
         MODEL.Label = string.IsNullOrEmpty(device.deviceLabel) ? (device.deviceId == 0 ? "-" : device.deviceId.ToString()) : device.deviceLabel;
     }
 }
示例#2
0
        public void StoreDevices(List <Tuple <OmemoProtocolAddress, string> > devices, string bareJid)
        {
            IEnumerable <OmemoDeviceModel> newDevices = devices.Select(d => new OmemoDeviceModel(d.Item1)
            {
                deviceLabel = d.Item2
            });

            List <OmemoDeviceModel> oldDevices;
            AbstractModel           model;

            if (string.Equals(bareJid, dbAccount.bareJid))
            {
                oldDevices = dbAccount.omemoInfo.devices;
                model      = dbAccount.omemoInfo;
            }
            else
            {
                OmemoChatInformationModel omemoChatInfo;
                using (SemaLock semaLock = DataCache.INSTANCE.NewChatSemaLock())
                {
                    omemoChatInfo = DataCache.INSTANCE.GetChat(dbAccount.bareJid, bareJid, semaLock)?.omemoInfo;
                }
                model      = omemoChatInfo;
                oldDevices = omemoChatInfo.devices;

                if (model is null)
                {
                    throw new InvalidOperationException("Failed to store devices. Chat '" + bareJid + "' does not exist.");
                }
            }

            using (MainDbContext ctx = new MainDbContext())
            {
                // Remove old:
                for (int i = 0; i < oldDevices.Count; i++)
                {
                    OmemoDeviceModel device = oldDevices[i];
                    if (newDevices.Where(d => d.deviceId == device.deviceId).FirstOrDefault() is null)
                    {
                        ctx.Devices.Remove(device);
                        oldDevices.RemoveAt(i);
                    }
                }

                // Add/Update existing:
                foreach (OmemoDeviceModel device in newDevices)
                {
                    OmemoDeviceModel dev = oldDevices.Where(d => d.deviceId == device.deviceId).FirstOrDefault();
                    if (!(dev is null))
                    {
                        dev.deviceLabel = device.deviceLabel;
                        ctx.Devices.Update(dev);
                    }
示例#3
0
        public void OnQrCodeScannerShown(QrCodeScannerDialogDataTemplate model)
        {
            if (model.Success)
            {
                Uri uri = null;
                try
                {
                    uri = new Uri(model.QrCode);
                }
                catch (Exception e)
                {
                    Logger.Error("Failed to parse OMEMO fingerprint XMPP URI. Malformed URI: " + model.QrCode, e);
                    return;
                }

                if (string.Equals(uri.LocalPath.ToLowerInvariant(), MODEL.Chat.Chat.bareJid.ToLowerInvariant()))
                {
                    IUriAction action = UriUtils.parse(uri);

                    if (action is OmemoFingerprintUriAction fingerprintUriAction)
                    {
                        OmemoDeviceModel device = MODEL.Chat.Chat.omemoInfo.devices.Where(d => d.deviceId == fingerprintUriAction.FINGERPRINT.ADDRESS.DEVICE_ID).FirstOrDefault();
                        using (MainDbContext ctx = new MainDbContext())
                        {
                            if (device is null)
                            {
                                device = new OmemoDeviceModel(fingerprintUriAction.FINGERPRINT.ADDRESS)
                                {
                                    fingerprint = new OmemoFingerprintModel(fingerprintUriAction.FINGERPRINT)
                                    {
                                        trusted = true
                                    }
                                };
                                ctx.Add(device);
                                MODEL.Chat.Chat.omemoInfo.devices.Add(device);
                                ctx.Update(MODEL.Chat.Chat.omemoInfo);
                            }
                            else
                            {
                                device.fingerprint.FromOmemoFingerprint(fingerprintUriAction.FINGERPRINT);
                                device.fingerprint.trusted = true;
                                ctx.Update(device.fingerprint.identityKey);
                                ctx.Update(device.fingerprint);
                            }
                        }

                        Logger.Info("Scanned OMEMO fingerprint successful.");
                        Logger.Debug("Fingerprint: " + fingerprintUriAction.FINGERPRINT.ADDRESS.ToString());
                        LoadFingerprints();
                    }
                    else
                    {
                        Logger.Warn("Failed to parse OMEMO fingerprint XMPP URI. Not an " + nameof(OmemoFingerprintUriAction) + ".");
                    }
                }
                else
                {
                    Logger.Warn("Failed to parse OMEMO fingerprint XMPP URI. Wrong chat: " + uri.LocalPath);
                }
            }
        }