public static SignalMessage IncreaseReceiptCountLocked(SignalServiceEnvelope envelope) { SignalMessage m; bool set_mark = false; lock (DBLock) { using (var ctx = new SignalDBContext()) { m = ctx.Messages.SingleOrDefault(t => t.ComposedTimestamp == envelope.getTimestamp()); if (m != null) { m.Receipts++; if (m.Status == SignalMessageStatus.Confirmed) { m.Status = SignalMessageStatus.Received; set_mark = true; } } else { ctx.EarlyReceipts.Add(new SignalEarlyReceipt() { DeviceId = (uint)envelope.getSourceDevice(), Timestamp = envelope.getTimestamp(), Username = envelope.getSource() }); } ctx.SaveChanges(); } } return(set_mark? m : null); }
private SignalServiceDataMessage createSignalServiceMessage(SignalServiceEnvelope envelope, DataMessage content) { SignalServiceGroup groupInfo = createGroupInfo(envelope, content); List <SignalServiceAttachment> attachments = new List <SignalServiceAttachment>(); bool endSession = ((content.Flags & (uint)DataMessage.Types.Flags.EndSession) != 0); bool expirationUpdate = ((content.Flags & (uint)DataMessage.Types.Flags.ExpirationTimerUpdate) != 0); foreach (AttachmentPointer pointer in content.Attachments) { attachments.Add(new SignalServiceAttachmentPointer(pointer.Id, pointer.ContentType, pointer.Key.ToByteArray(), envelope.getRelay(), pointer.SizeOneofCase == AttachmentPointer.SizeOneofOneofCase.Size ? pointer.Size : 0, pointer.ThumbnailOneofCase == AttachmentPointer.ThumbnailOneofOneofCase.Thumbnail ? pointer.Thumbnail.ToByteArray() : null, pointer.DigestOneofCase == AttachmentPointer.DigestOneofOneofCase.Digest ? pointer.Digest.ToByteArray() : null, pointer.FileNameOneofCase == AttachmentPointer.FileNameOneofOneofCase.FileName ? pointer.FileName : null, pointer.FlagsOneofCase == AttachmentPointer.FlagsOneofOneofCase.Flags && (pointer.Flags & (uint)AttachmentPointer.Types.Flags.VoiceMessage) != 0)); } return(new SignalServiceDataMessage() { Timestamp = envelope.getTimestamp(), Group = groupInfo, Attachments = attachments, Body = content.Body, EndSession = endSession, ExpiresInSeconds = (int)content.ExpireTimer, ExpirationUpdate = expirationUpdate }); }
private Pair <long, long> insertPlaceholder(SignalServiceEnvelope envelope) { var database = DatabaseFactory.getTextMessageDatabase(); //getEncryptingSmsDatabase(context); IncomingTextMessage textMessage = new IncomingTextMessage(envelope.getSource(), envelope.getSourceDevice(), envelope.getTimestamp(), "", May <SignalServiceGroup> .NoValue); textMessage = new IncomingEncryptedMessage(textMessage, ""); return(database.InsertMessageInbox(textMessage)); }
private void HandleSessionResetMessage(SignalServiceEnvelope envelope, SignalServiceContent content, SignalServiceDataMessage dataMessage, bool isSync, long timestamp) { SignalMessageDirection type; SignalContact author; SignalMessageStatus status; string prefix; string conversationId; long composedTimestamp; if (isSync) { var sent = content.SynchronizeMessage.getSent().ForceGetValue(); type = SignalMessageDirection.Synced; status = SignalMessageStatus.Confirmed; composedTimestamp = sent.getTimestamp(); author = null; prefix = "You have"; conversationId = sent.getDestination().ForceGetValue(); } else { status = 0; type = SignalMessageDirection.Incoming; author = SignalDBContext.GetOrCreateContactLocked(envelope.getSource(), timestamp, this); prefix = $"{author.ThreadDisplayName} has"; composedTimestamp = envelope.getTimestamp(); conversationId = envelope.getSource(); } LibsignalDBContext.DeleteAllSessions(conversationId); SignalMessage sm = new SignalMessage() { Direction = type, Type = SignalMessageType.SessionReset, Status = status, Author = author, Content = new SignalMessageContent() { Content = $"{prefix} reset the session." }, ThreadId = conversationId, DeviceId = (uint)envelope.getSourceDevice(), Receipts = 0, ComposedTimestamp = composedTimestamp, ReceivedTimestamp = timestamp, }; Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async() => { await UIHandleIncomingMessage(sm); }).AsTask().Wait(); }
public static void IncreaseReceiptCountLocked(SignalServiceEnvelope envelope, MainPageViewModel mpvm) { SignalMessage m; bool set_mark = false; lock (DBLock) { using (var ctx = new SignalDBContext()) { m = ctx.Messages.SingleOrDefault(t => t.ComposedTimestamp == envelope.getTimestamp()); if (m != null) { m.Receipts++; if (m.Status == SignalMessageStatus.Confirmed) { m.Status = SignalMessageStatus.Received; set_mark = true; } } else { ctx.EarlyReceipts.Add(new SignalEarlyReceipt() { DeviceId = (uint)envelope.getSourceDevice(), Timestamp = envelope.getTimestamp(), Username = envelope.getSource() }); } ctx.SaveChanges(); } } if (set_mark) { Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => { mpvm.UIUpdateMessageBox(m); }).AsTask().Wait(); } }
private void HandleSessionResetMessage(SignalServiceEnvelope envelope, SignalServiceContent content, SignalServiceDataMessage dataMessage, bool isSync, long timestamp) { SignalMessageDirection type; SignalContact author; SignalMessageStatus status; SignalConversation conversation; string prefix; string conversationId; long composedTimestamp; if (isSync) { var sent = content.SynchronizeMessage.getSent().ForceGetValue(); type = SignalMessageDirection.Synced; status = SignalMessageStatus.Confirmed; composedTimestamp = sent.getTimestamp(); author = null; prefix = "You have"; conversationId = sent.getDestination().ForceGetValue(); } else { status = 0; type = SignalMessageDirection.Incoming; author = SignalDBContext.GetOrCreateContactLocked(envelope.getSource(), timestamp); prefix = $"{author.ThreadDisplayName} has"; composedTimestamp = envelope.getTimestamp(); conversationId = envelope.getSource(); } LibsignalDBContext.DeleteAllSessions(conversationId); conversation = SignalDBContext.GetOrCreateContactLocked(conversationId, 0); SignalMessage sm = new SignalMessage() { Direction = type, Type = SignalMessageType.SessionReset, Status = status, Author = author, Content = new SignalMessageContent() { Content = $"{prefix} reset the session." }, ThreadId = conversationId, DeviceId = (uint)envelope.getSourceDevice(), Receipts = 0, ComposedTimestamp = composedTimestamp, ReceivedTimestamp = timestamp, }; SignalLibHandle.Instance.SaveAndDispatchSignalMessage(sm, conversation); }
private void handleMessage(SignalServiceEnvelope envelope, bool sendExplicitReceipt) { var worker = App.Current.Worker; long messageId = DatabaseFactory.getPushDatabase().Insert(envelope); if (sendExplicitReceipt) { worker.AddTaskActivities(new DeliveryReceiptTask(envelope.getSource(), envelope.getTimestamp(), envelope.getRelay())); } worker.AddTaskActivities(new PushDecryptTask(messageId, envelope.getSource())); }
private SignalServiceDataMessage createSignalServiceMessage(SignalServiceEnvelope envelope, DataMessage content) { SignalServiceGroup groupInfo = createGroupInfo(envelope, content); List <SignalServiceAttachment> attachments = new List <SignalServiceAttachment>(); bool endSession = ((content.Flags & (uint)DataMessage.Types.Flags.END_SESSION) != 0); bool expirationUpdate = ((content.Flags & (uint)DataMessage.Types.Flags.EXPIRATION_TIMER_UPDATE) != 0); foreach (AttachmentPointer pointer in content.AttachmentsList) { attachments.Add(new SignalServiceAttachmentPointer(pointer.Id, pointer.ContentType, pointer.Key.ToByteArray(), envelope.getRelay(), pointer.HasSize ? new May <uint>(pointer.Size) : May <uint> .NoValue, pointer.HasThumbnail ? new May <byte[]>(pointer.Thumbnail.ToByteArray()) : May <byte[]> .NoValue)); } return(new SignalServiceDataMessage(envelope.getTimestamp(), groupInfo, attachments, content.Body, endSession, (int)content.ExpireTimer, expirationUpdate)); }
public long Insert(SignalServiceEnvelope envelope) { // TODO check if exists var push = new Push() { Type = envelope.getType(), Source = envelope.getSource(), DeviceId = envelope.getSourceDevice(), LegacyMessage = envelope.hasLegacyMessage() ? Base64.encodeBytes(envelope.getLegacyMessage()) : "", Content = envelope.hasContent() ? Base64.encodeBytes(envelope.getContent()) : "", Timestamp = TimeUtil.GetDateTime(envelope.getTimestamp()) }; try { conn.Insert(push); } catch (Exception e) { Debug.WriteLine(e.Message); } return(push.PushId); }
private void handleUntrustedIdentityMessage(SignalServiceEnvelope envelope, May <long> smsMessageId) { try { var database = DatabaseFactory.getTextMessageDatabase(); //getEncryptingSmsDatabase(context); Recipients recipients = RecipientFactory.getRecipientsFromString(envelope.getSource(), false); long recipientId = recipients.getPrimaryRecipient().getRecipientId(); PreKeySignalMessage whisperMessage = new PreKeySignalMessage(envelope.getLegacyMessage()); IdentityKey identityKey = whisperMessage.getIdentityKey(); String encoded = Base64.encodeBytes(envelope.getLegacyMessage()); IncomingTextMessage textMessage = new IncomingTextMessage(envelope.getSource(), envelope.getSourceDevice(), envelope.getTimestamp(), encoded, May <SignalServiceGroup> .NoValue); if (!smsMessageId.HasValue) { IncomingPreKeyBundleMessage bundleMessage = new IncomingPreKeyBundleMessage(textMessage, encoded); Pair <long, long> messageAndThreadId = database.InsertMessageInbox(bundleMessage); database.SetMismatchedIdentity(messageAndThreadId.first(), recipientId, identityKey); //MessageNotifier.updateNotification(context, masterSecret.getMasterSecret().orNull(), messageAndThreadId.second); } else { var messageId = smsMessageId.ForceGetValue(); database.UpdateMessageBody(messageId, encoded); database.MarkAsPreKeyBundle(messageId); database.SetMismatchedIdentity(messageId, recipientId, identityKey); } } catch (InvalidMessageException e) { throw new InvalidOperationException(e.Message); } catch (InvalidVersionException e) { throw new InvalidOperationException(e.Message); } }
private void HandleSignalMessage(SignalServiceEnvelope envelope, SignalServiceContent content, SignalServiceDataMessage dataMessage, bool isSync, long timestamp) { SignalMessageDirection type; SignalContact author; SignalMessageStatus status; string threadId; long composedTimestamp; string body = dataMessage.Body != null ? dataMessage.Body : ""; if (dataMessage.Group != null) { var rawId = dataMessage.Group.GroupId; threadId = Base64.encodeBytes(rawId); var g = SignalDBContext.GetOrCreateGroupLocked(threadId, timestamp, this); if (!g.CanReceive) { SignalServiceGroup group = new SignalServiceGroup() { Type = SignalServiceGroup.GroupType.REQUEST_INFO, GroupId = rawId }; SignalServiceDataMessage requestInfoMessage = new SignalServiceDataMessage() { Group = group, Timestamp = Util.CurrentTimeMillis() }; MessageSender.sendMessage(envelope.getSourceAddress(), requestInfoMessage); } composedTimestamp = envelope.getTimestamp(); } else { if (isSync) { var sent = content.SynchronizeMessage.getSent().ForceGetValue(); threadId = SignalDBContext.GetOrCreateContactLocked(sent.getDestination().ForceGetValue(), timestamp, this).ThreadId; composedTimestamp = sent.getTimestamp(); } else { threadId = SignalDBContext.GetOrCreateContactLocked(envelope.getSource(), timestamp, this).ThreadId; composedTimestamp = envelope.getTimestamp(); } } if (isSync) { type = SignalMessageDirection.Synced; status = SignalMessageStatus.Confirmed; author = null; } else { status = 0; type = SignalMessageDirection.Incoming; author = SignalDBContext.GetOrCreateContactLocked(envelope.getSource(), timestamp, this); } List <SignalAttachment> attachments = new List <SignalAttachment>(); SignalMessage message = new SignalMessage() { Direction = type, Status = status, Author = author, Content = new SignalMessageContent() { Content = body }, ThreadId = threadId, DeviceId = (uint)envelope.getSourceDevice(), Receipts = 0, ComposedTimestamp = composedTimestamp, ReceivedTimestamp = timestamp, AttachmentsCount = (uint)attachments.Count, Attachments = attachments }; if (dataMessage.Attachments != null) { var receivedAttachments = dataMessage.Attachments; foreach (var receivedAttachment in receivedAttachments) { var pointer = receivedAttachment.asPointer(); SignalAttachment sa = new SignalAttachment() { Message = message, Status = (uint)SignalAttachmentStatus.Default, SentFileName = pointer.FileName, ContentType = "", Key = pointer.Key, Relay = pointer.Relay, StorageId = pointer.Id }; attachments.Add(sa); } } Debug.WriteLine("received message: " + message.Content); if (type == SignalMessageDirection.Incoming) { if (App.WindowActive) { Utils.TryVibrate(true); } else { SendTileNotification(message); SendMessageNotification(message); } } Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async() => { await UIHandleIncomingMessage(message); }).AsTask().Wait(); }
private void HandleGroupUpdateMessage(SignalServiceEnvelope envelope, SignalServiceContent content, SignalServiceDataMessage dataMessage, bool isSync, long timestamp) { if (dataMessage.Group != null) //TODO check signal droid: group messages have different types! { SignalServiceGroup group = dataMessage.Group; string groupid = Base64.encodeBytes(group.GroupId); SignalGroup g = new SignalGroup(); string displayname = "Unknown group"; string avatarfile = null; if (group.Name != null) { displayname = group.Name; } var dbgroup = SignalDBContext.InsertOrUpdateGroupLocked(groupid, displayname, avatarfile, true, timestamp, this); if (group.Members != null) { foreach (var member in group.Members) { SignalDBContext.InsertOrUpdateGroupMembershipLocked(dbgroup.Id, SignalDBContext.GetOrCreateContactLocked(member, 0, this).Id); } } /* insert message into conversation */ SignalMessageDirection type; SignalContact author; SignalMessageStatus status; string prefix; long composedTimestamp; if (isSync) { var sent = content.SynchronizeMessage.getSent().ForceGetValue(); type = SignalMessageDirection.Synced; status = SignalMessageStatus.Confirmed; composedTimestamp = sent.getTimestamp(); author = null; prefix = "You have"; } else { status = 0; type = SignalMessageDirection.Incoming; author = SignalDBContext.GetOrCreateContactLocked(envelope.getSource(), timestamp, this); prefix = $"{author.ThreadDisplayName} has"; composedTimestamp = envelope.getTimestamp(); } SignalMessage sm = new SignalMessage() { Direction = type, Type = SignalMessageType.GroupUpdate, Status = status, Author = author, Content = new SignalMessageContent() { Content = $"{prefix} updated the group." }, ThreadId = groupid, DeviceId = (uint)envelope.getSourceDevice(), Receipts = 0, ComposedTimestamp = composedTimestamp, ReceivedTimestamp = timestamp, }; Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async() => { await UIHandleIncomingMessage(sm); }).AsTask().Wait(); } else { Debug.WriteLine("received group update without group info!"); } }
private void handleReceipt(SignalServiceEnvelope envelope) { Log.Debug($"Received receipt: (XXXXX, {envelope.getTimestamp()})"); DatabaseFactory.getMessageDatabase().incrementDeliveryReceiptCount(envelope.getSource(), (long)envelope.getTimestamp()); }
private void HandleSignalMessage(SignalServiceEnvelope envelope, SignalServiceContent content, SignalServiceDataMessage dataMessage, bool isSync, long timestamp) { SignalMessageDirection type; SignalContact author; SignalMessageStatus status; SignalConversation conversation; long composedTimestamp; string body = dataMessage.Body ?? ""; if (dataMessage.Group != null) { var rawId = dataMessage.Group.GroupId; var threadId = Base64.encodeBytes(rawId); conversation = SignalDBContext.GetOrCreateGroupLocked(threadId, timestamp); if (!conversation.CanReceive) { SignalServiceGroup group = new SignalServiceGroup() { Type = SignalServiceGroup.GroupType.REQUEST_INFO, GroupId = rawId }; SignalServiceDataMessage requestInfoMessage = new SignalServiceDataMessage() { Group = group, Timestamp = Util.CurrentTimeMillis() }; //MessageSender.sendMessage(envelope.getSourceAddress(), requestInfoMessage); TODO } composedTimestamp = envelope.getTimestamp(); } else { if (isSync) { var sent = content.SynchronizeMessage.getSent().ForceGetValue(); conversation = SignalDBContext.GetOrCreateContactLocked(sent.getDestination().ForceGetValue(), timestamp); composedTimestamp = sent.getTimestamp(); } else { conversation = SignalDBContext.GetOrCreateContactLocked(envelope.getSource(), timestamp); composedTimestamp = envelope.getTimestamp(); } } if (isSync) { type = SignalMessageDirection.Synced; status = SignalMessageStatus.Confirmed; author = null; } else { status = 0; type = SignalMessageDirection.Incoming; author = SignalDBContext.GetOrCreateContactLocked(envelope.getSource(), timestamp); } List <SignalAttachment> attachments = new List <SignalAttachment>(); SignalMessage message = new SignalMessage() { Direction = type, Status = status, Author = author, Content = new SignalMessageContent() { Content = body }, ThreadId = conversation.ThreadId, DeviceId = (uint)envelope.getSourceDevice(), Receipts = 0, ComposedTimestamp = composedTimestamp, ReceivedTimestamp = timestamp, AttachmentsCount = (uint)attachments.Count, Attachments = attachments }; if (dataMessage.Attachments != null) { var receivedAttachments = dataMessage.Attachments; foreach (var receivedAttachment in receivedAttachments) { var pointer = receivedAttachment.asPointer(); SignalAttachment sa = new SignalAttachment() { Message = message, Status = (uint)SignalAttachmentStatus.Default, SentFileName = pointer.FileName, ContentType = "", Key = pointer.Key, Relay = pointer.Relay, StorageId = pointer.Id }; attachments.Add(sa); } } SignalLibHandle.Instance.SaveAndDispatchSignalMessage(message, conversation); }
private void HandleGroupUpdateMessage(SignalServiceEnvelope envelope, SignalServiceContent content, SignalServiceDataMessage dataMessage, bool isSync, long timestamp) { if (dataMessage.Group != null) //TODO check signal droid: group messages have different types! { SignalServiceGroup group = dataMessage.Group; string groupid = Base64.encodeBytes(group.GroupId); SignalGroup g = new SignalGroup(); string displayname = "Unknown group"; string avatarfile = null; if (group.Name != null) { displayname = group.Name; } var dbgroup = SignalDBContext.InsertOrUpdateGroupLocked(groupid, displayname, avatarfile, true, timestamp); if (group.Members != null) { foreach (var member in group.Members) { SignalDBContext.InsertOrUpdateGroupMembershipLocked(dbgroup.Id, SignalDBContext.GetOrCreateContactLocked(member, 0).Id); } } /* insert message into conversation */ SignalMessageDirection type; SignalContact author; SignalMessageStatus status; string prefix; long composedTimestamp; if (isSync) { var sent = content.SynchronizeMessage.getSent().ForceGetValue(); type = SignalMessageDirection.Synced; status = SignalMessageStatus.Confirmed; composedTimestamp = sent.getTimestamp(); author = null; prefix = "You have"; } else { status = 0; type = SignalMessageDirection.Incoming; author = SignalDBContext.GetOrCreateContactLocked(envelope.getSource(), timestamp); prefix = $"{author.ThreadDisplayName} has"; composedTimestamp = envelope.getTimestamp(); } SignalMessage sm = new SignalMessage() { Direction = type, Type = SignalMessageType.GroupUpdate, Status = status, Author = author, Content = new SignalMessageContent() { Content = $"{prefix} updated the group." }, ThreadId = groupid, DeviceId = (uint)envelope.getSourceDevice(), Receipts = 0, ComposedTimestamp = composedTimestamp, ReceivedTimestamp = timestamp, }; SignalDBContext.SaveMessageLocked(sm); dbgroup.MessagesCount += 1; if (sm.Direction == SignalMessageDirection.Incoming) { dbgroup.UnreadCount += 1; } else { dbgroup.UnreadCount = 0; dbgroup.LastSeenMessageIndex = dbgroup.MessagesCount; } dbgroup.LastMessage = sm; SignalLibHandle.Instance.DispatchAddOrUpdateConversation(dbgroup, sm); } else { Logger.LogError("HandleGroupUpdateMessage() received group update without group info"); } }
private void HandleExpirationUpdateMessage(SignalServiceEnvelope envelope, SignalServiceContent content, SignalServiceDataMessage message, bool isSync, long timestamp) { SignalMessageDirection type; SignalContact author; SignalMessageStatus status; string prefix; SignalConversation conversation; long composedTimestamp; if (isSync) { var sent = content.SynchronizeMessage.getSent().ForceGetValue(); type = SignalMessageDirection.Synced; status = SignalMessageStatus.Confirmed; composedTimestamp = sent.getTimestamp(); author = null; prefix = "You have"; if (message.Group != null) { conversation = SignalDBContext.GetOrCreateGroupLocked(Base64.encodeBytes(message.Group.GroupId), 0); } else { conversation = SignalDBContext.GetOrCreateContactLocked(sent.getDestination().ForceGetValue(), 0); } } else { status = 0; type = SignalMessageDirection.Incoming; author = SignalDBContext.GetOrCreateContactLocked(envelope.getSource(), timestamp); prefix = $"{author.ThreadDisplayName} has"; composedTimestamp = envelope.getTimestamp(); if (message.Group != null) { conversation = SignalDBContext.GetOrCreateGroupLocked(Base64.encodeBytes(message.Group.GroupId), 0); } else { conversation = SignalDBContext.GetOrCreateContactLocked(envelope.getSource(), 0); } } SignalDBContext.UpdateExpiresInLocked(conversation, (uint)message.ExpiresInSeconds); SignalMessage sm = new SignalMessage() { Direction = type, Type = SignalMessageType.ExpireUpdate, Status = status, Author = author, Content = new SignalMessageContent() { Content = $"{prefix} set the expiration timer to {message.ExpiresInSeconds} seconds." }, ThreadId = conversation.ThreadId, DeviceId = (uint)envelope.getSourceDevice(), Receipts = 0, ComposedTimestamp = composedTimestamp, ReceivedTimestamp = timestamp, }; SignalLibHandle.Instance.SaveAndDispatchSignalMessage(sm, conversation); }