public void FeedEventRoomMessageTest() { int fireCount = 0; bool didFire = false; MatrixRoom room = new MatrixRoom(null, "!abc:localhost"); room.OnMessage += (n, a) => didFire = true; room.OnEvent += (n, a) => fireCount++; // NoAgeRestriction room.MessageMaximumAge = 0; var ev = new MatrixMRoomMessage(); room.FeedEvent(Utils.MockEvent(ev, age: 5000)); Assert.That(didFire, Is.True, "Message without age limit."); // AgeRestriction, Below Limit room.MessageMaximumAge = 5000; didFire = false; room.FeedEvent(Utils.MockEvent(ev, age: 2500)); Assert.That(didFire, Is.True, "Message below age limit."); // AgeRestriction, Above Limit didFire = false; room.FeedEvent(Utils.MockEvent(ev, age: 5001)); Assert.That(didFire, Is.False, "Message above age limit."); //Test Subclass didFire = false; ev = new MMessageText(); room.FeedEvent(Utils.MockEvent(ev)); Assert.That(didFire, Is.True, "Subclassed message accepted."); // OnEvent should fire each time Assert.That(fireCount, Is.EqualTo(4)); }
public MatrixEventContent GetContent(JObject jobj, JsonSerializer serializer, string type) { Type T; if (!contentTypes.TryGetValue(type, out T)) { //Console.WriteLine ("Unknown Event:" + type); return(null); } try { if (T == typeof(MatrixMRoomMessage)) { MatrixMRoomMessage message = new MatrixMRoomMessage(); serializer.Populate(jobj.CreateReader(), message); T = MessageContentType(message.msgtype); } MatrixEventContent content = (MatrixEventContent)Activator.CreateInstance(T); content.mxContent = jobj; if (type == "m.receipt") { ((MatrixMReceipt)content).ParseJObject(jobj); } else { serializer.Populate(jobj.CreateReader(), content); } return(content); } catch (Exception ex) { throw new Exception($"Failed to get content for {type}"); } }
/// <summary> /// Send a new message to the room. /// </summary> /// <param name="message">Message.</param> /// <returns>Event ID of the sent message</returns> public string SendMessage(MatrixMRoomMessage message) { var t = api.RoomMessageSend(ID, "m.room.message", message); t.Wait(); return(t.Result); }
public MatrixEventContent GetContent(JToken jobj, JsonSerializer serializer, string type) { Type T; if (contentTypes.TryGetValue(type, out T)) { if (T == typeof(MatrixMRoomMessage)) { MatrixMRoomMessage message = new MatrixMRoomMessage(); serializer.Populate(jobj.CreateReader(), message); T = MessageContentType(message.msgtype); } MatrixEventContent content = (MatrixEventContent)Activator.CreateInstance(T); if (type == "m.receipt") { ((MatrixMReceipt)content).ParseJObject((JObject)jobj); } else { serializer.Populate(jobj.CreateReader(), content); } return(content); } else { #if DEBUG var payload = jobj.ToString(); Console.WriteLine("Unknown Event:" + type); #endif return(new MatrixEventContent()); } }
private async Task <string> RoomSend(string roomId, string type, MatrixMRoomMessage msg, string txnId = "") { ThrowIfNotSupported(); JObject msgData = ObjectToJson(msg); var res = await mbackend.PutAsync( $"/_matrix/client/r0/rooms/{roomId}/send/{type}/{txnId}", true, msgData ); if (!res.error.IsOk) { throw new MatrixException(res.error.ToString()); //TODO: Need a better exception } return(res.result["event_id"].ToObject <string>()); }
public async Task <string> RoomMessageSend(string roomid, string type, MatrixMRoomMessage message) { ThrowIfNotSupported(); if (message.body == null) { throw new Exception("Missing body in message"); } if (message.msgtype == null) { throw new Exception("Missing msgtype in message"); } int txnId = rng.Next(int.MinValue, int.MaxValue); JObject msgData = ObjectToJson(message); // Send messages in order. // XXX: Mutex was removed because it's not task safe, need another mechanism. while (true) { try { var res = await mbackend.PutAsync( $"/_matrix/client/r0/rooms/{roomid}/send/{type}/{txnId}", true, msgData ); if (res.error.IsOk) { //eventSendMutex.ReleaseMutex(); return(res.result["event_id"].ToObject <string>()); } if (res.error.MatrixErrorCode == MatrixErrorCode.M_LIMIT_EXCEEDED) { int backoff = res.error.RetryAfter != -1 ? res.error.RetryAfter : 1000; //log.LogWarning($"Sending m{txnId} failed. Will retry in {backoff}ms"); await Task.Delay(backoff); } else { throw new MatrixException(res.error.ToString()); //TODO: Need a better exception } } catch (Exception) { throw; } } }
public virtual bool PlayerMessage(MatrixUser user, MatrixMRoomMessage message) { if (user.UserID == "@cardbot:half-shot.uk") { return(true); } if (message.ToString() == "START" && user.UserID == owner.UserID) { if (users.Count < MinPlayers) { room.SendMessage("Cannot start, you need " + (MinPlayers - users.Count).ToString() + " more players"); } else { GameStart(); } return(true); } return(false); }
public override bool PlayerMessage(MatrixUser user, MatrixMRoomMessage message) { if (!base.PlayerMessage(user, message)) { if (GameInSession) { if (message.body.ToLower() == "count") { string msg = "Card counts:\n"; foreach (MatrixUser u in users) { msg += user.DisplayName + " has " + hands[user.UserID].Count + " cards\n"; } room.SendMessage(msg); } else if (message.body.ToLower() == "deck") { room.SendMessage("The deck contains " + deck.Count + " cards"); } else if (lastPlayer != -1) { if (message.body.ToLower() == "cheat!" && user.UserID != users[lastPlayer].UserID) { if (!cheatCalled) { CallCheatOnPlayer(user); CheckForWinner(); } else { room.SendNotice("Cheat has already been called this round!"); } } } } } return(false); }
public void RoomMessageSend(string roomid, string type, MatrixMRoomMessage message) { bool collision = true; MatrixAPIPendingEvent evt = new MatrixAPIPendingEvent(); evt.room_id = roomid; evt.type = type; evt.content = message; if (((MatrixMRoomMessage)evt.content).body == null) { throw new Exception("Missing body in message"); } while (collision) { evt.txnId = rng.Next(1, 64); collision = pendingMessages.FirstOrDefault(x => x.txnId == evt.txnId) != default(MatrixAPIPendingEvent); } pendingMessages.Enqueue(evt); if (IsAS) { FlushMessageQueue(); } }
public override bool PlayerMessage(MatrixUser user, MatrixMRoomMessage message) { if (!base.PlayerMessage(user, message)) { if (GameInSession) { if (user.UserID != users[currentPlayer].UserID) { return(true); } string cmd = message.body.Trim().ToLower(); if (cmd.Length == 2) { int a = ((int)cmd[0]) - 96; int b = int.Parse(new string(cmd[1], 1)); if (a < 1 || a > 3 || b < 1 || b > 3) { room.SendMessage("Invalid position"); } int square = ((a - 1) * 3) + (b - 1); if (brd[square] == 0) { brd[square] = currentPlayer + 1; room.SendMessage(GetBoardString()); currentPlayer = currentPlayer == 0 ? 1 : 0; } else { room.SendMessage("That spot is taken"); } CheckForWin(); } } } return(true); }
/// <summary> /// Send a new message to the room. /// </summary> /// <param name="message">Message.</param> public void SendMessage(MatrixMRoomMessage message) { api.RoomMessageSend(ID, "m.room.message", message); }
public Task <string> SendMessageAsync(MatrixMRoomMessage message) { return(api.RoomMessageSend(ID, "m.room.message", message)); }