public void SendMail(User to, User from, String message) { if (!UserExists(to)) throw new Exception("Invalid Recipient: " + to.Username); if (!UserExists(from)) throw new Exception("Invalid Sender: " + from.Username); var mail = new Mail(); mail.To = to.Username; mail.From = from.Username; mail.Message = message; this.PendingMail.InsertOnSubmit(mail); this.SubmitChanges(); }
public void TestMultipleMessagesWithSameToFrom() { var from = new User("fromUser", "fromUserpwd"); var to = new User("toUser", "toUserpwd"); Search.database.registerUser(from); Search.database.registerUser(to); String message = "hello toUser"; String message2 = "hello toUser second"; Search.database.SendMail(to, from, message); Search.database.SendMail(to, from, message2); var rec = Search.database.CheckMail(to.Username); Mail m1 = new Mail(to, from, message); Mail m2 = new Mail(to, from, message2); var expected = new List<Mail>(); expected.Add(m1); expected.Add(m2); Assert.AreEqual(expected.First().Message, rec.First().Message); expected.Remove(expected.First()); rec.Remove(rec.First()); Assert.AreEqual(expected.First().Message, rec.First().Message); }
public void onRecieve(string message) { string[] parsed = splitMessage(message.TrimEnd((char)0x00)); // Message will contrain two parts // Type string type = parsed[0]; // Params string param = parsed[1]; if (("login".Equals(type))) { Console.WriteLine("Login " + param); string[] userPass = param.Split(' '); bool returner; try { if (userPass.Length > 2) { throw new Exception(); } returner = UserRegistration.login(userPass[0], userPass[1]); username = userPass[0]; //Console.WriteLine("User Registration thinks : " + returner); } catch { returner = false; sock.send(iSocket.aSocket.stringToBytes(returner.ToString(), iSocket.constants.MAXPACKETSIZE)); //sock.close(); throw; } sock.send(iSocket.aSocket.stringToBytes(returner.ToString(), iSocket.constants.MAXPACKETSIZE)); } else if ("register".Equals(type)) { string[] userPass = param.Split(' '); bool returner; try { if (userPass.Length > 2) { throw new Exception(); } string newUsername = userPass[0]; string newUserpass = userPass[1]; Console.WriteLine("register " + newUsername + ' ' + newUserpass); returner = UserRegistration.register(new User(newUsername, newUserpass)); //Console.WriteLine("User Registration thinks : " + returner); } catch { returner = false; sock.send(iSocket.aSocket.stringToBytes(returner.ToString(), iSocket.constants.MAXPACKETSIZE)); //sock.close(); throw; } sock.send(iSocket.aSocket.stringToBytes(returner.ToString(), iSocket.constants.MAXPACKETSIZE)); } else if (username != null) { if ("requestfile".Equals(type)) { Console.WriteLine("Requestfile " + param); sendFile(param); } else if ("ownedfiles".Equals(type)) { Console.WriteLine("Ownedfiles " + param); ownedFiles(param); } else if ("searchtag".Equals(type)) { Console.WriteLine("searchtag " + param); searchtag(message); } else if ("alltags".Equals(type)) { Console.WriteLine("alltags " + param); alltags(); } else if ("searchownedfiles".Equals(type)) { Console.WriteLine("searchownedfiles " + param); searchownedfiles(message); } else if ("quit".Equals(type)) { Console.WriteLine("Quit " + param); quit(); } else if ("addfile".Equals(type)) { string[] userPass = param.Split(' '); bool returner; try { Console.WriteLine("addfile " + userPass[0] + ' ' + userPass[1]); returner = Search.database.AddOwnership(userPass[0], userPass[1]); //Console.WriteLine("User Registration thinks : " + returner); } catch { returner = true; sock.send(iSocket.aSocket.stringToBytes(returner.ToString(), iSocket.constants.MAXPACKETSIZE)); //sock.close(); throw; } sock.send(iSocket.aSocket.stringToBytes(returner.ToString(), iSocket.constants.MAXPACKETSIZE)); } else if ("message".Equals(type)) { string ret = "False"; try { string[] args = param.Split(' '); String tempMessage = param.Substring(param.IndexOf(' ')); //Send mail requires them to be users Console.WriteLine("message T:F:M" + ' ' + args[0] + ' ' + username + ' ' + tempMessage); Search.database.SendMail(new User(args[0], null), new User(username, null), tempMessage); ret = "True"; } catch { ret = "False"; } sock.send(iSocket.aSocket.stringToBytes(ret.ToString(), iSocket.constants.MAXPACKETSIZE)); } else if ("checkmail".Equals(type)) { Console.WriteLine("checkmail " + username); List <Mail> mail = Search.database.CheckMail(username); // if there is no mail just say that if (mail.Count == 0) { sock.send(iSocket.aSocket.stringToBytes(mail.Count.ToString() + ' ', iSocket.constants.MAXPACKETSIZE)); return; } int transfers = mail.Count; Mail m = mail[0]; String sender = m.From; String mailMessage = m.Message; int id = m.ID; sock.send(iSocket.aSocket.stringToBytes(mail.Count.ToString() + ' ' + sender + ' ' + id + ' ' + mailMessage, iSocket.constants.MAXPACKETSIZE)); Console.WriteLine(sender + ' ' + id + ' ' + mailMessage); mail.RemoveAt(0); while (mail.Count > 0) { m = mail[0]; sender = m.From; mailMessage = m.Message; id = m.ID; sock.send(iSocket.aSocket.stringToBytes(sender + ' ' + id + ' ' + mailMessage + ' ', iSocket.constants.MAXPACKETSIZE)); mail.RemoveAt(0); Console.WriteLine(sender + ' ' + id + ' ' + mailMessage); } } } }