using System; using System.Collections.Generic; using System.Security.Cryptography; namespace server; public class Data { public Dictionary Keys { set; get; } = []; public Dictionary> Messages { set; get; } = []; public RSA? GetKey(string Phone) { return Keys.TryGetValue(Phone, out RSA? value) ? value : null; } public List? GetMessages(string Phone, int limit = -1) { // Check we have a RSA key for the phone and get the messages if (!Keys.ContainsKey(Phone)) { return null; } if (Messages.TryGetValue(Phone, out Queue? value)) { List msgs = new(limit == -1 ? value.Count : Math.Min(value.Count, limit)); int count = 0; while (count != limit && value.TryDequeue(out byte[]? m)) { count += 1; msgs.Add(m); } return msgs; } else { // generate a new queue because one doesnt already exists Messages[Phone] = new Queue(); return []; // no messages were in the list so no reason to attempt to send any message } } public bool AddMessage(string Phone, byte[] Message) { if (Keys.ContainsKey(Phone)) { if (Messages.TryGetValue(Phone, out var value)) { value.Enqueue(Message); } else { Messages[Phone] = new Queue([Message]); } return true; } else { return false; } } public bool PeekMessages(string Phone) { return Messages.TryGetValue(Phone, out Queue? value) && value.TryPeek(out var _); } }