static void PostMessage(long threadId, string eof) { StringBuilder textBuilder = new StringBuilder(); while (true) { String readedLine = ReadLine(); if (readedLine.Equals(eof)) { break; } textBuilder.Append(readedLine); textBuilder.Append('\n'); } using (Context context = new Context(config)) { long recepientId = context.MessagesThread .Where(u => u.Id == threadId) .Select(u => u.WithUser) .Single(); CMessage message = new CMessage(InfoTools.GetMessageThreadPublicId(context, threadId) , textBuilder.ToString(), DateTime.Now, recepientId, settings.UserPublicId); PushOperations.Insert(context, message, recepientId, settings.UserPublicId); context.SaveChanges(); } }
private async void OnContactTapped(object sender, ItemTappedEventArgs e) { try { if (nameLabel.Text == null || nameLabel.Text == "") { throw new Exception("Please, enter the thread name!"); } if (e.Item != null) { var contact = (Contacts)e.Item; CMessageThread thread; using (Context context = new Context(settings.Config)) { thread = new CMessageThread(context, nameLabel.Text, false, contact.PublicId, settings.UserPublicId); PushOperations.Insert(context, thread, contact.PublicId, settings.UserPublicId); } await Navigation.PopModalAsync(); await app.MainPage.Navigation.PushModalAsync(new NavigationPage(new MessageView(app, thread.PublicId))); } } catch (Exception ex) { ShowError(ex); } }
private async void SaveUser() { try { UContact contact = new UContact(this.contact); contact.NickName = nickNameEntry.Text; contact.AlarmPermission = alarmSwitch.IsToggled; using (Context context = new Context(settings.Config)) { PushOperations.Update(context, contact, settings.UserPublicId, settings.UserPublicId); } if (contact.Trusted != trustedSwitch.IsToggled) { await SaveTrustification(trustedSwitch.IsToggled); } await Navigation.PopModalAsync(); } catch (Exception ex) { app.logger.LogException(this, ex); ShowError(ex.Source, ex.Message); } }
public void UntrustContact(int contactId) { ThrowExceptionIfNotConnected(); lock (mutex) { if (contactId == this.UserId) { throw new Exception("You really don't want untrust yourself."); } Log("Sending UNTRUST_CONTACT command."); BinaryEncoder.SendCommand(stream, ConnectionCommand.UNTRUST_CONTACT); BinaryEncoder.SendInt(stream, contactId); using (Context context = new Context(config)) { var contact = new UContact(context.Contacts .Where(u => u.PublicId == contactId) .SingleOrDefault()) { Trusted = false }; PushOperations.SendJsonCapsula(context, contact.GetSelfUpdate(), UserId, UserId); context.SaveChanges(); } } Push(); }
public void TrustContact(int contactId) { Pull(); Log("Sending TRUST_CONTACT command."); TextEncoder.SendCommand(stream, ConnectionCommand.TRUST_CONTACT); TextEncoder.SendInt(stream, contactId); using (Context context = new Context(config)) { var contact = context.Contacts .Where(u => u.PublicId == contactId) .SingleOrDefault(); contact.Trusted = 1; context.SaveChanges(); if (contact.SendAesKey == null) { AESPassword password = AESPassword.GenerateAESPassword(); JAESKey key = new JAESKey(contactId, password); PushOperations.SendIJType(context, key, UserId, UserId); X509Certificate2 cert = X509Certificate2Utils.ImportFromPem( context.Contacts .Where(u => u.PublicId == contactId) .Select(u => u.PublicCertificate) .SingleOrDefault()); BinaryEncoder.SendBytes(stream, RSAEncoder.Encrypt(password.Password, cert)); context.SaveChanges(); } } Push(); }
private async void AddUser() { try { CContact contact = new CContact() { AlarmPermission = alarmSwitch.IsToggled, NickName = nickNameEntry.Text, PublicCertificate = this.contact.PublicCertificate, PublicId = this.contact.PublicId, Trusted = this.contact.Trusted == 1, UserName = this.contact.UserName }; using (Context context = new Context(settings.Config)) { PushOperations.Insert(context, contact, settings.UserPublicId, settings.UserPublicId); } if (contact.Trusted != trustedSwitch.IsToggled) { await SaveTrustification(trustedSwitch.IsToggled); } await Navigation.PopModalAsync(); } catch (Exception ex) { app.logger.LogException(this, ex); ShowError(ex.Source, ex.Message); } }
private async void send_Clicked(object sender, EventArgs e) { try { var strToSend = toSend.Text ?? ""; toSend.Text = ""; await Task.Run(() => { using (Context context = new Context(app.settings.Config)) { if (!context.Contacts .Where(u => u.PublicId == withUserId) .Select(u => u.Trusted == 1) .Single()) { throw new Exception("Can't communicate with untrusted user."); } CMessage message = new CMessage(threadId, strToSend, DateTime.Now, withUserId, app.settings.UserPublicId); PushOperations.Insert(context, message, withUserId, app.settings.UserPublicId); } Update(); }); } catch (Exception ex) { var task = ShowError(ex); } }
static void PostThread(long userId, string name) { using (Context context = new Context(config)) { CMessageThread thread = new CMessageThread(context, name, false, userId, settings.UserPublicId); PushOperations.Insert(context, thread, userId, settings.UserPublicId); context.SaveChanges(); } }
static void RenameThread(long threadId, string newName) { using (Context context = new Context(config)) { var toRename = new UMessageThread(context.MessagesThread .Where(u => u.Id == threadId) .Single(), settings.UserPublicId); toRename.Name = newName; PushOperations.Update(context, toRename, toRename.WithUser, settings.UserPublicId); context.SaveChanges(); } }
static void SetNickName(int userId, string nickName) { using (Context context = new Context(config)) { UContact contact = new UContact( context.Contacts .Where(u => u.PublicId == userId) .Single()); contact.NickName = nickName; PushOperations.Update(context, contact, settings.UserPublicId, settings.UserPublicId); } }
private async void delete_Clicked(object sender, EventArgs e) { var answer = await DisplayAlert("Delete", "Do you really want to delete this thread?", "Yes", "No"); if (answer) { using (Context context = new Context(app.settings.Config)) { DMessageThread thread = new DMessageThread(context.MessagesThread .Where(u => u.PublicId == threadId) .Single(), app.settings.UserPublicId); PushOperations.Delete(context, thread); } await Navigation.PopModalAsync(); } }
static void SaveUser(SearchCServerCapsula searchCapsula) { using (Context context = new Context(config)) { CContact contact = new CContact() { AlarmPermission = false, NickName = null, PublicCertificate = searchCapsula.PemCertificate, PublicId = searchCapsula.UserId, ReceiveAesKey = null, SendAesKey = null, Trusted = false, UserName = searchCapsula.UserName }; PushOperations.Insert(context, contact, settings.UserPublicId, settings.UserPublicId); } }
public void DoDelete(Context context) { foreach (var message in context.Messages .Where(u => u.IdMessagesThread == thread.PublicId)) { DMessage dMessage = new DMessage(message, myUserId); dMessage.DoDelete(context); } context.MessagesThread.Remove(thread); var jthread = new JMessageThread() { PublicId = thread.PublicId, DoOnlyDelete = true }; PushOperations.SendIJType(context, jthread, myUserId, myUserId); //PushOperations.DeleteBlobMessage(context, thread.GetBlobId(), myUserId); //Piggy bug fix //throw new NotSupportedException("Not supported, because developer of this app is absolutly useless."); }
public void DoDelete(Context context) { context.Messages.Remove(message); PushOperations.DeleteBlobMessage(context, message.GetBlobId(), myUserId); }
public void TrustContact(int contactId) { ThrowExceptionIfNotConnected(); Pull(); lock (mutex) { Log("Sending TRUST_CONTACT command."); BinaryEncoder.SendCommand(stream, ConnectionCommand.TRUST_CONTACT); BinaryEncoder.SendInt(stream, contactId); using (Context context = new Context(config)) { var contact = new UContact(context.Contacts .Where(u => u.PublicId == contactId) .SingleOrDefault()) { Trusted = true }; if (contact.SendAesKey == null) { Log("Sending new key."); BinaryEncoder.SendInt(stream, 1); AESPassword password = AESPassword.GenerateAESPassword(); contact.SendAesKey = password.Password; X509Certificate2 recepientCert = X509Certificate2Utils.ImportFromPem( context.Contacts .Where(u => u.PublicId == contactId) .Select(u => u.PublicCertificate) .SingleOrDefault()); byte[] toSend = RSAEncoder.EncryptAndSign(password.Password, recepientCert, MyCertificate); BinaryEncoder.SendBytes(stream, toSend); } else { Log("No new key will be sended."); BinaryEncoder.SendInt(stream, 0); } if (contactId != this.UserId) { PushOperations.SendJsonCapsula(context, contact.GetSelfUpdate(), UserId, UserId); } else { var me = context.Contacts .Where(u => u.PublicId == UserId) .Single(); me.SendAesKey = contact.SendAesKey; me.ReceiveAesKey = contact.ReceiveAesKey; } context.SaveChanges(); } Log("Trustification has been done."); } Push(); }
public int Pull() { int changes = 0; ThrowExceptionIfNotConnected(); lock (mutex) { Log("Sending PULL command."); BinaryEncoder.SendCommand(stream, ConnectionCommand.PULL); #if (DEBUG) Log("Sending ClientPullCapsula."); #endif ClientPullCapsula clientCapsula; using (Context context = new Context(config)) { clientCapsula = new ClientPullCapsula() { AesKeysUserIds = context.Contacts .Where(u => u.ReceiveAesKey == null) .Select(u => u.PublicId) .ToArray() }; } TextEncoder.SendJson(stream, clientCapsula); ServerPullCapsula capsula = TextEncoder.ReadJson <ServerPullCapsula>(stream); #if (DEBUG) Log("Received ServerPullCapsula."); #endif changes += capsula.AesKeysUserIds.Count; changes += capsula.Messages.Count; using (Context context = new Context(config)) { #if (DEBUG) Log("Receiving and saving AES keys."); #endif foreach (var id in capsula.AesKeysUserIds) { var user = new UContact(context.Contacts.Where(con => con.PublicId == id).SingleOrDefault()); try { user.ReceiveAesKey = RSAEncoder.DecryptAndVerify(BinaryEncoder.ReceiveBytes(stream), MyCertificate, X509Certificate2Utils.ImportFromPem(user.PublicCertificate)); } catch (Exception ex) { Log($"Loading of Receive AESKey from {user.PublicId} has failed."); logger.LogException(this, ex); } PushOperations.Update(context, user, UserId, UserId); } context.SaveChanges(); #if (DEBUG) Log("Receiving and saving messages."); #endif foreach (PullMessage metaMessage in capsula.Messages) { BlobMessages metaBlob = new BlobMessages() { PublicId = metaMessage.PublicId, SenderId = metaMessage.SenderId, Failed = 0, DoDelete = 0 }; context.BlobMessages.Add(metaBlob); context.SaveChanges(); try { PullMessageParser.ParseEncryptedMessage(context, BinaryEncoder.ReceiveBytes(stream), metaBlob.SenderId, metaBlob.Id, UserId); } catch (Exception ex) { Log($"Loading of message {metaMessage.PublicId} has failed."); metaBlob.Failed = 1; logger.LogException(this, ex); } context.SaveChanges(); } } Log("Pull have been done."); } return(changes); }
public void DoDelete(Context context) { context.Alarms.Remove(alarm); PushOperations.DeleteBlobMessage(context, alarm.GetBlobId(), myUserId); }
public void DoDelete(Context context) { context.ContactsDetail.Remove(contactDetail); PushOperations.DeleteBlobMessage(context, contactDetail.GetBlobId(), myUserId); }