protected override void OnRun(MessageContent content, ServerCommandArgs args) { if (string.IsNullOrEmpty(content.RoomName)) { throw new ArgumentException("RoomName"); } if (content.Users == null) { throw new ArgumentNullException("Users"); } if (string.Equals(content.RoomName, ServerModel.MainRoomName)) { ServerModel.Api.SendSystemMessage(args.ConnectionId, "Невозможно удалить пользователей из основной комнаты."); return; } if (!RoomExists(content.RoomName, args.ConnectionId)) { return; } using (var server = ServerModel.Get()) { var room = server.Rooms[content.RoomName]; if (!room.Admin.Equals(args.ConnectionId)) { ServerModel.Api.SendSystemMessage(args.ConnectionId, "Вы не являетесь администратором комнаты. Операция отменена."); return; } var sendingContent = new ClientRoomClosedCommand.MessageContent { Room = room }; foreach (var user in content.Users) { if (!room.ContainsUser(user.Nick)) { continue; } if (user.Equals(room.Admin)) { ServerModel.Api.SendSystemMessage(args.ConnectionId, "Невозможно удалить из комнаты администратора."); continue; } room.RemoveUser(user.Nick); ServerModel.Server.SendMessage(user.Nick, ClientRoomClosedCommand.CommandId, sendingContent); } RefreshRoom(server, room); } }
protected override void OnRun(MessageContent content, ServerCommandArgs args) { if (string.IsNullOrEmpty(content.Message)) { throw new ArgumentException("Message"); } if (string.IsNullOrEmpty(content.RoomName)) { throw new ArgumentException("RoomName"); } if (!RoomExists(content.RoomName, args.ConnectionId)) { return; } using (var server = ServerModel.Get()) { var room = server.Rooms[content.RoomName]; if (!room.Users.Contains(args.ConnectionId)) { ServerModel.Api.SendSystemMessage(args.ConnectionId, "Вы не можете отправить сообщение, т.к. не входите в состав этой комнаты."); return; } if (content.MessageId != null && !room.IsMessageBelongToUser(args.ConnectionId, content.MessageId.Value)) { ServerModel.Api.SendSystemMessage(args.ConnectionId, "Вы не можете редактировать это сообщение."); return; } Message message = null; if (content.MessageId == null) { message = room.AddMessage(args.ConnectionId, content.Message); } else { message = room.GetMessage(content.MessageId.Value); message.Text = content.Message; } var sendingContent = new ClientOutRoomMessageCommand.MessageContent { Message = message.Text, RoomName = content.RoomName, Sender = args.ConnectionId, MessageId = message.Id }; foreach (string user in room.Users.Where(u => u != null)) { ServerModel.Server.SendMessage(user, ClientOutRoomMessageCommand.CommandId, sendingContent); } } }
protected override void OnRun(MessageContent content, ServerCommandArgs args) { if (string.IsNullOrEmpty(content.RoomName)) { throw new ArgumentException("RoomName"); } if (content.Users == null) { throw new ArgumentNullException("Users"); } if (string.Equals(content.RoomName, ServerModel.MainRoomName)) { ServerModel.Api.SendSystemMessage(args.ConnectionId, MessageId.RoomAccessDenied); return; } if (!RoomExists(content.RoomName, args.ConnectionId)) { return; } using (var server = ServerModel.Get()) { var room = server.Rooms[content.RoomName]; if (!room.Admin.Equals(args.ConnectionId)) { ServerModel.Api.SendSystemMessage(args.ConnectionId, MessageId.RoomAccessDenied); return; } var sendingContent = new ClientRoomClosedCommand.MessageContent { Room = room }; foreach (var user in content.Users) { if (!room.ContainsUser(user.Nick)) { continue; } if (user.Equals(room.Admin)) { ServerModel.Api.SendSystemMessage(args.ConnectionId, MessageId.RoomAccessDenied); continue; } room.RemoveUser(user.Nick); ServerModel.Server.SendMessage(user.Nick, ClientRoomClosedCommand.CommandId, sendingContent); } RefreshRoom(server, room); } }
protected override void OnRun(MessageContent content, CommandArgs args) { if (string.IsNullOrEmpty(content.RoomName)) { throw new ArgumentException("content.RoomName"); } if (content.Users == null) { throw new ArgumentNullException("content.Users"); } if (content.RoomName == ServerChat.MainRoomName) { ServerModel.Api.Perform(new ServerSendSystemMessageAction(args.ConnectionId, SystemMessageId.RoomAccessDenied)); return; } using (var server = ServerModel.Get()) { Room room; if (!TryGetRoom(server.Chat, content.RoomName, args.ConnectionId, out room)) { return; } if (room.Admin != args.ConnectionId) { ServerModel.Api.Perform(new ServerSendSystemMessageAction(args.ConnectionId, SystemMessageId.RoomAccessDenied)); return; } var sendingContent = new ClientRoomClosedCommand.MessageContent { RoomName = room.Name }; foreach (var userNick in content.Users) { if (!room.IsUserExist(userNick)) { continue; } if (userNick == room.Admin) { ServerModel.Api.Perform(new ServerSendSystemMessageAction(args.ConnectionId, SystemMessageId.RoomAccessDenied)); continue; } room.RemoveUser(userNick); ServerModel.Server.SendMessage(userNick, ClientRoomClosedCommand.CommandId, sendingContent); } RefreshRoom(server.Chat, room); } }
protected override void OnRun(MessageContent content, ServerCommandArgs args) { if (string.IsNullOrEmpty(content.Message)) { throw new ArgumentException("Message"); } if (string.IsNullOrEmpty(content.RoomName)) { throw new ArgumentException("RoomName"); } using (var server = ServerModel.Get()) { Room room; if (!TryGetRoom(server, content.RoomName, args.ConnectionId, out room)) { return; } if (!room.Users.Contains(args.ConnectionId)) { ServerModel.Api.SendSystemMessage(args.ConnectionId, SystemMessageId.RoomAccessDenied); return; } if (content.MessageId != null && !room.IsMessageBelongToUser(args.ConnectionId, content.MessageId.Value)) { ServerModel.Api.SendSystemMessage(args.ConnectionId, SystemMessageId.MessageEditAccessDenied); return; } Message message = null; if (content.MessageId == null) { message = room.AddMessage(args.ConnectionId, content.Message); } else { message = room.GetMessage(content.MessageId.Value); message.Text = content.Message; } var sendingContent = new ClientOutRoomMessageCommand.MessageContent { Message = message, RoomName = content.RoomName, }; foreach (var user in room.Users) { ServerModel.Server.SendMessage(user, ClientOutRoomMessageCommand.CommandId, sendingContent); } } }
protected override void OnRun(MessageContent content, CommandArgs args) { if (content.UserDto == null) { throw new ArgumentNullException(nameof(content.UserDto)); } var userId = content.UserDto.Id; if (userId == UserId.Empty) { throw new ArgumentException(nameof(userId)); } if (userId.IsTemporary) { SendFail(args.ConnectionId, SystemMessageId.NotRegisteredBadName); return; } var userCertificate = ServerModel.Server.GetCertificate(args.ConnectionId); if (!string.Equals(userId.Thumbprint, userCertificate.Thumbprint, StringComparison.OrdinalIgnoreCase)) { SendFail(args.ConnectionId, SystemMessageId.NotRegisteredBadThumbprint); return; } using (var server = ServerModel.Get()) { var chat = server.Chat; if (chat.IsNickExist(userId.Nick)) { SendFail(args.ConnectionId, SystemMessageId.NotRegisteredNameAlreadyExist); return; } ServerModel.Logger.WriteInfo("User login: {0}", userId); chat.AddUser(new User(content.UserDto)); var mainRoom = chat.GetRoom(ServerChat.MainRoomName); mainRoom.AddUser(userId); Register(userId, args.ConnectionId); var userDtos = chat.GetRoomUserDtos(mainRoom.Name); SendRefresh(userId, mainRoom, userDtos); SendOpened(userId, mainRoom, userDtos); // Notify ServerModel.Notifier.ConnectionRegistered(new ConnectionEventArgs(userId)); } }
public void Perform() { using (var server = ServerModel.Get()) { var emptyRooms = new List<string>(); foreach (var room in server.Chat.GetRooms()) { if (!room.Users.Contains(_nick)) continue; room.RemoveUser(_nick); if (room.Name != ServerChat.MainRoomName) { if (room.IsEmpty) emptyRooms.Add(room.Name); else { if (room.Admin == _nick) { room.Admin = room.Users.FirstOrDefault(); if (room.Admin != null) ServerModel.Api.Perform(new ServerSendSystemMessageAction(room.Admin, SystemMessageId.RoomAdminChanged, room.Name)); } } } foreach (var userNick in room.Users) { var sendingContent = new ClientRoomRefreshedCommand.MessageContent { Room = room.ToDto(userNick), Users = server.Chat.GetRoomUserDtos(room.Name) }; ServerModel.Server.SendMessage(userNick, ClientRoomRefreshedCommand.CommandId, sendingContent); } } // Remove all empty rooms foreach (var emptyRoomName in emptyRooms) server.Chat.RemoveRoom(emptyRoomName); // Removing user from chat after all rooms processing server.Chat.RemoveUser(_nick); } ServerModel.Notifier.ConnectionUnregistered(new ConnectionEventArgs(_nick)); // Closing the connection after model clearing if (_removeConnection) ServerModel.Server.CloseConnection(_nick); }
protected override void OnRun(MessageContent content, CommandArgs args) { if (string.IsNullOrEmpty(content.RoomName)) { throw new ArgumentException("content.RoomName"); } if (content.RoomName == ServerChat.MainRoomName) { ServerModel.Api.Perform(new ServerSendSystemMessageAction(args.ConnectionId, SystemMessageId.RoomCantLeaveMainRoom)); return; } using (var server = ServerModel.Get()) { Room room; if (!TryGetRoom(server.Chat, content.RoomName, args.ConnectionId, out room)) { return; } if (!room.IsUserExist(args.ConnectionId)) { ServerModel.Api.Perform(new ServerSendSystemMessageAction(args.ConnectionId, SystemMessageId.RoomAccessDenied)); return; } var closeRoomContent = new ClientRoomClosedCommand.MessageContent { RoomName = room.Name }; ServerModel.Server.SendMessage(args.ConnectionId, ClientRoomClosedCommand.CommandId, closeRoomContent); room.RemoveUser(args.ConnectionId); if (room.IsEmpty) { server.Chat.RemoveRoom(room.Name); } else { if (room.Admin == args.ConnectionId) { room.Admin = room.Users.FirstOrDefault(); if (room.Admin != null) { ServerModel.Api.Perform(new ServerSendSystemMessageAction(room.Admin, SystemMessageId.RoomAdminChanged, room.Name)); } } RefreshRoom(server.Chat, room); } } }
/// <summary> /// Удаляет пользователя и закрывает соединение с ним. /// </summary> /// <param name="nick">Ник пользователя, соединение котрого будет закрыто.</param> public void RemoveUser(string nick) { ServerModel.Server.CloseConnection(nick); using (var server = ServerModel.Get()) { foreach (string roomName in server.Rooms.Keys) { Room room = server.Rooms[roomName]; if (!room.Users.Contains(nick)) { continue; } room.RemoveUser(nick); server.Users.Remove(nick); if (string.Equals(room.Admin, nick)) { room.Admin = room.Users.FirstOrDefault(); if (room.Admin != null) { string message = string.Format("Вы назначены администратором комнаты {0}.", room.Name); ServerModel.API.SendSystemMessage(room.Admin, message); } } var sendingContent = new ClientRoomRefreshedCommand.MessageContent { Room = room, Users = room.Users.Select(n => server.Users[n]).ToList() }; foreach (string user in room.Users) { if (user == null) { continue; } ServerModel.Server.SendMessage(user, ClientRoomRefreshedCommand.Id, sendingContent); } } } ServerModel.Notifier.Unregistered(new ServerRegistrationEventArgs { Nick = nick }); }
public void Run(ServerCommandArgs args) { var receivedContent = Serializer.Deserialize <MessageContent>(args.Message); if (string.IsNullOrEmpty(receivedContent.RoomName)) { throw new ArgumentException("RoomName"); } if (string.Equals(receivedContent.RoomName, ServerModel.MainRoomName)) { ServerModel.API.SendSystemMessage(args.ConnectionId, "Невозможно выйти из основной комнаты."); return; } if (!RoomExists(receivedContent.RoomName, args.ConnectionId)) { return; } using (var server = ServerModel.Get()) { var room = server.Rooms[receivedContent.RoomName]; if (!room.Users.Contains(args.ConnectionId)) { ServerModel.API.SendSystemMessage(args.ConnectionId, "Вы и так не входите в состав этой комнаты."); return; } room.RemoveUser(args.ConnectionId); var closeRoomContent = new ClientRoomClosedCommand.MessageContent { Room = room }; ServerModel.Server.SendMessage(args.ConnectionId, ClientRoomClosedCommand.Id, closeRoomContent); if (string.Equals(room.Admin, args.ConnectionId)) { room.Admin = room.Users.FirstOrDefault(); if (room.Admin != null) { string message = string.Format("Вы назначены администратором комнаты \"{0}\".", room.Name); ServerModel.API.SendSystemMessage(room.Admin, message); } } RefreshRoom(server, room); } }
public void IntroduceConnections(string senderId, IPEndPoint senderPoint, string requestId, IPEndPoint requestPoint) { using (var context = ServerModel.Get()) { var content = new ClientWaitPeerConnectionCommand.MessageContent { RequestPoint = requestPoint, SenderPoint = senderPoint, RemoteInfo = context.Users[senderId], }; ServerModel.Server.SendMessage(requestId, ClientWaitPeerConnectionCommand.CommandId, content); } }
private static void ClearMainRoom(MessageContent content, CommandArgs args) { using (var server = ServerModel.Get()) { var room = server.Chat.GetRoom(ServerChat.MainRoomName); var messageIds = room.Messages.Select(m => m.Id).ToArray(); room.RemoveMessages(messageIds); foreach (var nick in room.Users) { ServerModel.Api.Perform(new ServerRemoveMessagesAction(nick, room.Name, messageIds)); } } }
private static void RemoveMessage(AdminCommandArgs adminArgs, CommandArgs args) { if (adminArgs.Parameters.Length < 2) { ServerModel.Api.Perform(new ServerSendSystemMessageAction(args.ConnectionId, SystemMessageId.TextCommandInvalidParams)); return; } using (var server = ServerModel.Get()) { var name = adminArgs.Parameters[0]; var messageIds = adminArgs.Parameters .Skip(1) .Select(idStr => { if (!long.TryParse(idStr, out long messageId)) { return(-1); } return(messageId); }) .Where(id => id > 0) .ToArray(); if (messageIds.Length == 0) { ServerModel.Api.Perform(new ServerSendSystemMessageAction(args.ConnectionId, SystemMessageId.TextCommandInvalidParams)); return; } var room = server.Chat.TryGetRoom(name); if (room == null) { ServerModel.Api.Perform(new ServerSendSystemMessageAction(args.ConnectionId, SystemMessageId.TextCommandInvalidParams)); return; } foreach (var messageId in messageIds) { room.RemoveMessage(messageId); } foreach (var userId in room.Users) { ServerModel.Api.Perform(new ServerRemoveMessagesAction(userId, name, messageIds)); } } }
protected override void OnRun(MessageContent content, ServerCommandArgs args) { if (string.IsNullOrEmpty(content.RoomName)) { throw new ArgumentException("RoomName"); } using (var server = ServerModel.Get()) { Room room; if (!TryGetRoom(server, content.RoomName, args.ConnectionId, out room)) { return; } var file = room.Files.Find(f => f.Id == content.FileId); if (file == null) { return; } if (!room.ContainsUser(args.ConnectionId)) { ServerModel.Api.SendSystemMessage(args.ConnectionId, SystemMessageId.RoomAccessDenied); return; } if (room.Admin != args.ConnectionId && file.Id.Owner != args.ConnectionId) { ServerModel.Api.SendSystemMessage(args.ConnectionId, SystemMessageId.FileRemoveAccessDenied); return; } room.Files.Remove(file); ServerModel.Api.SendSystemMessage(args.ConnectionId, SystemMessageId.FileRemoved, file.Name); var postedFileDeletedContent = new ClientPostedFileDeletedCommand.MessageContent { RoomName = room.Name, FileId = file.Id }; foreach (string user in room.Users) { ServerModel.Server.SendMessage(user, ClientPostedFileDeletedCommand.CommandId, postedFileDeletedContent); } } }
public void RemoveUser(string nick) { ServerModel.Server.CloseConnection(nick); using (var server = ServerModel.Get()) { foreach (string roomName in server.Rooms.Keys) { var room = server.Rooms[roomName]; if (!room.Users.Contains(nick)) { continue; } room.RemoveUser(nick); server.Users.Remove(nick); if (string.Equals(room.Admin, nick)) { room.Admin = room.Users.FirstOrDefault(); if (room.Admin != null) { ServerModel.Api.SendSystemMessage(room.Admin, MessageId.RoomAdminChanged, room.Name); } } var sendingContent = new ClientRoomRefreshedCommand.MessageContent { Room = room, Users = room.Users.Select(n => server.Users[n]).ToList() }; foreach (string user in room.Users) { if (user == null) { continue; } ServerModel.Server.SendMessage(user, ClientRoomRefreshedCommand.CommandId, sendingContent); } } } ServerModel.Notifier.Unregistered(new ServerRegistrationEventArgs { Nick = nick }); }
protected override void OnRun(MessageContent content, ServerCommandArgs args) { if (content.User == null) { throw new ArgumentNullException("User"); } if (content.User.Nick == null) { throw new ArgumentNullException("User.Nick"); } if (content.User.Nick.Contains(Connection.TempConnectionPrefix)) { SendFail(args.ConnectionId, SystemMessageId.NotRegisteredBadName); return; } using (var server = ServerModel.Get()) { var room = server.Rooms[ServerModel.MainRoomName]; var userExist = room.Users.Any(nick => string.Equals(content.User.Nick, nick)); if (userExist) { SendFail(args.ConnectionId, SystemMessageId.NotRegisteredNameAlreadyExist); return; } else { ServerModel.Logger.WriteInfo("User login: {0}", content.User.Nick); server.Users.Add(content.User.Nick, content.User); room.AddUser(content.User.Nick); Register(content.User.Nick, args.ConnectionId); var users = ServerModel.Api.GetRoomUsers(server, room); SendRefresh(content.User.Nick, room, users); SendOpened(content.User.Nick, room, users); // Notify ServerModel.Notifier.Registered(new ServerRegistrationEventArgs { Nick = content.User.Nick }); } } }
protected override void OnRun(MessageContent content, CommandArgs args) { if (string.IsNullOrEmpty(content.RoomName)) { throw new ArgumentException(nameof(content.RoomName)); } using (var server = ServerModel.Get()) { Room room; if (!TryGetRoom(server.Chat, content.RoomName, args.ConnectionId, out room)) { return; } var file = room.TryGetFile(content.FileId); if (file == null) { return; } if (!room.IsUserExist(args.ConnectionId)) { ServerModel.Api.Perform(new ServerSendSystemMessageAction(args.ConnectionId, SystemMessageId.RoomAccessDenied)); return; } if (room.Admin != args.ConnectionId && file.Id.Owner != args.ConnectionId) { ServerModel.Api.Perform(new ServerSendSystemMessageAction(args.ConnectionId, SystemMessageId.FileRemoveAccessDenied)); return; } room.RemoveFile(file.Id); ServerModel.Api.Perform(new ServerSendSystemMessageAction(args.ConnectionId, SystemMessageId.FileRemoved, file.Name)); var postedFileDeletedContent = new ClientFileRemovedCommand.MessageContent { RoomName = room.Name, FileId = file.Id }; foreach (var userId in room.Users) { ServerModel.Server.SendMessage(userId, ClientFileRemovedCommand.CommandId, postedFileDeletedContent); } } }
private static void Unban(AdminCommandArgs adminArgs, CommandArgs args) { if (adminArgs.Parameters.Length != 2) { ServerModel.Api.Perform(new ServerSendSystemMessageAction(args.ConnectionId, SystemMessageId.TextCommandInvalidParams)); return; } using (var server = ServerModel.Get()) { var nick = adminArgs.Parameters[0]; var thumbprint = adminArgs.Parameters[1]; var userId = new UserId(nick, thumbprint); ServerModel.Server.Bans.Unban(userId); } }
private static void ShowMessageId(AdminCommandArgs adminArgs, CommandArgs args) { if (adminArgs.Parameters.Length != 2) { ServerModel.Api.Perform(new ServerSendSystemMessageAction(args.ConnectionId, SystemMessageId.TextCommandInvalidParams)); return; } using (var server = ServerModel.Get()) { var roomName = adminArgs.Parameters[0]; var timeStr = adminArgs.Parameters[1]; var room = server.Chat.TryGetRoom(roomName); if (room == null) { ServerModel.Api.Perform(new ServerSendSystemMessageAction(args.ConnectionId, SystemMessageId.TextCommandInvalidParams)); return; } if (!DateTime.TryParse(timeStr, out DateTime time)) { ServerModel.Api.Perform(new ServerSendSystemMessageAction(args.ConnectionId, SystemMessageId.TextCommandInvalidParams)); return; } Message minMssage = null; foreach (var message in room.Messages) { if (minMssage == null) { minMssage = message; continue; } var diff = message.Time.Subtract(time); var minDiff = minMssage.Time.Subtract(time); if (Math.Abs(diff.TotalMilliseconds) < Math.Abs(minDiff.TotalMilliseconds)) { minMssage = message; } } ServerModel.Api.Perform(new ServerSendSystemMessageAction(args.ConnectionId, SystemMessageId.TextCommandMessageId, minMssage.Id.ToString())); } }
protected override void OnRun(MessageContent content, CommandArgs args) { if (content.UserDto == null) { throw new ArgumentNullException("content.UserDto"); } if (content.UserDto.Nick == null) { throw new ArgumentNullException("content.UserDto.Nick"); } if (content.UserDto.Nick.Contains(Connection.TempConnectionPrefix)) { SendFail(args.ConnectionId, SystemMessageId.NotRegisteredBadName); return; } using (var server = ServerModel.Get()) { var chat = server.Chat; if (chat.IsUserExist(content.UserDto.Nick)) { SendFail(args.ConnectionId, SystemMessageId.NotRegisteredNameAlreadyExist); return; } else { ServerModel.Logger.WriteInfo("User login: {0}", content.UserDto.Nick); chat.AddUser(new User(content.UserDto)); var mainRoom = chat.GetRoom(ServerChat.MainRoomName); mainRoom.AddUser(content.UserDto.Nick); Register(content.UserDto.Nick, args.ConnectionId); var userDtos = chat.GetRoomUserDtos(mainRoom.Name); SendRefresh(content.UserDto.Nick, mainRoom, userDtos); SendOpened(content.UserDto.Nick, mainRoom, userDtos); // Notify ServerModel.Notifier.ConnectionRegistered(new ConnectionEventArgs(content.UserDto.Nick)); } } }
public void Perform() { using (var server = ServerModel.Get()) { var senderUser = server.Chat.GetUser(_senderId); var content = new ClientWaitPeerConnectionCommand.MessageContent { RequestIpAddress = _requestPoint.Address.GetAddressBytes(), RequestPort = _requestPoint.Port, SenderIpAddress = _senderPoint.Address.GetAddressBytes(), SenderPort = _senderPoint.Port, RemoteInfo = new UserDto(senderUser), }; ServerModel.Server.SendMessage(_requestId, ClientWaitPeerConnectionCommand.CommandId, content); } }
public void Run(ServerCommandArgs args) { var receivedContent = Serializer.Deserialize <MessageContent>(args.Message); if (receivedContent.File == null) { throw new ArgumentNullException("File"); } if (string.IsNullOrEmpty(receivedContent.RoomName)) { throw new ArgumentException("RoomName"); } if (!RoomExists(receivedContent.RoomName, args.ConnectionId)) { return; } using (var context = ServerModel.Get()) { var room = context.Rooms[receivedContent.RoomName]; if (!room.Users.Contains(args.ConnectionId)) { ServerModel.API.SendSystemMessage(args.ConnectionId, "Вы не входите в состав этой комнаты."); return; } if (room.Files.FirstOrDefault(file => file.Equals(receivedContent.File)) == null) { room.Files.Add(receivedContent.File); } var sendingContent = new ClientFilePostedCommand.MessageContent { File = receivedContent.File, RoomName = receivedContent.RoomName }; foreach (string user in room.Users) { ServerModel.Server.SendMessage(user, ClientFilePostedCommand.Id, sendingContent); } } }
protected override void OnRun(MessageContent content, ServerCommandArgs args) { if (string.IsNullOrEmpty(content.RoomName)) { throw new ArgumentException("RoomName"); } if (string.Equals(content.RoomName, ServerModel.MainRoomName)) { ServerModel.Api.SendSystemMessage(args.ConnectionId, MessageId.RoomCantLeaveMainRoom); return; } if (!RoomExists(content.RoomName, args.ConnectionId)) { return; } using (var server = ServerModel.Get()) { var room = server.Rooms[content.RoomName]; if (!room.Users.Contains(args.ConnectionId)) { ServerModel.Api.SendSystemMessage(args.ConnectionId, MessageId.RoomAccessDenied); return; } room.RemoveUser(args.ConnectionId); var closeRoomContent = new ClientRoomClosedCommand.MessageContent { Room = room }; ServerModel.Server.SendMessage(args.ConnectionId, ClientRoomClosedCommand.CommandId, closeRoomContent); if (string.Equals(room.Admin, args.ConnectionId)) { room.Admin = room.Users.FirstOrDefault(); if (room.Admin != null) { ServerModel.Api.SendSystemMessage(room.Admin, MessageId.RoomAdminChanged, room.Name); } } RefreshRoom(server, room); } }
/// <summary> /// Проверяет существует ли комната. Если нет отправляет вызвавщему команду соединению сообщение об ошибке. /// А также команду закрытия комнаты. /// </summary> /// <param name="RoomName">Название комнаты.</param> /// <param name="connectionId">Id соединения.</param> /// <returns>Возвращает ложь если комнаты не существует.</returns> protected static bool RoomExists(string roomName, string connectionId) { bool result; using (var context = ServerModel.Get()) result = context.Rooms.ContainsKey(roomName); if (!result) { var closeRoomContent = new ClientRoomClosedCommand.MessageContent { Room = new Room(null, roomName) }; ServerModel.Server.SendMessage(connectionId, ClientRoomClosedCommand.Id, closeRoomContent); ServerModel.API.SendSystemMessage(connectionId, "На свервере нет комнаты с таким именем."); } return(result); }
protected override void OnRun(MessageContent content, ServerCommandArgs args) { if (content.File == null) { throw new ArgumentNullException("File"); } if (string.IsNullOrEmpty(content.RoomName)) { throw new ArgumentException("RoomName"); } if (!RoomExists(content.RoomName, args.ConnectionId)) { return; } using (var context = ServerModel.Get()) { var room = context.Rooms[content.RoomName]; if (!room.Users.Contains(args.ConnectionId)) { ServerModel.Api.SendSystemMessage(args.ConnectionId, MessageId.RoomAccessDenied); return; } if (room.Files.FirstOrDefault(file => file.Equals(content.File)) == null) { room.Files.Add(content.File); } var sendingContent = new ClientFilePostedCommand.MessageContent { File = content.File, RoomName = content.RoomName }; foreach (string user in room.Users) { ServerModel.Server.SendMessage(user, ClientFilePostedCommand.CommandId, sendingContent); } } }
public void RemoveUser(string nick) { using (var server = ServerModel.Get()) { foreach (var room in server.Rooms.Values) { if (!room.Users.Contains(nick)) { continue; } room.RemoveUser(nick); if (room.Admin == nick) { room.Admin = room.Users.FirstOrDefault(); if (room.Admin != null) { ServerModel.Api.SendSystemMessage(room.Admin, SystemMessageId.RoomAdminChanged, room.Name); } } var sendingContent = new ClientRoomRefreshedCommand.MessageContent { Room = room, Users = GetRoomUsers(server, room) }; foreach (var user in room.Users) { ServerModel.Server.SendMessage(user, ClientRoomRefreshedCommand.CommandId, sendingContent); } } // Removing user from model after all rooms server.Users.Remove(nick); } // Closing the connection after model clearing ServerModel.Server.CloseConnection(nick); ServerModel.Notifier.Unregistered(new ServerRegistrationEventArgs { Nick = nick }); }
protected override void OnRun(MessageContent content, CommandArgs args) { if (string.IsNullOrEmpty(content.RoomName)) { throw new ArgumentNullException("content.RoomName"); } using (var server = ServerModel.Get()) { if (server.Chat.IsRoomExist(content.RoomName)) { ServerModel.Api.Perform(new ServerSendSystemMessageAction(args.ConnectionId, SystemMessageId.RoomAlreadyExist)); return; } Room room = null; if (content.Type == RoomType.Chat) { var textRoom = new ServerRoom(args.ConnectionId, content.RoomName); server.Chat.AddRoom(textRoom); room = textRoom; } if (content.Type == RoomType.Voice) { var voiceRoom = new ServerVoiceRoom(args.ConnectionId, content.RoomName); server.Chat.AddVoiceRoom(voiceRoom); room = voiceRoom; } if (room == null) { throw new ArgumentException("content.RoomType"); } var sendingContent = new ClientRoomOpenedCommand.MessageContent { Room = room.ToDto(args.ConnectionId), Users = server.Chat.GetRoomUserDtos(room.Name) }; ServerModel.Server.SendMessage(args.ConnectionId, ClientRoomOpenedCommand.CommandId, sendingContent); } }
protected override void OnRun(MessageContent content, CommandArgs args) { if (string.IsNullOrEmpty(content.RoomName)) { throw new ArgumentException("content.RoomName"); } if (content.File == null) { throw new ArgumentNullException("content.File"); } using (var server = ServerModel.Get()) { Room room; if (!TryGetRoom(server.Chat, content.RoomName, args.ConnectionId, out room)) { return; } if (!room.IsUserExist(args.ConnectionId)) { ServerModel.Api.Perform(new ServerSendSystemMessageAction(args.ConnectionId, SystemMessageId.RoomAccessDenied)); return; } if (!room.IsFileExist(content.File.Id)) { room.AddFile(new FileDescription(content.File)); } var sendingContent = new ClientFilePostedCommand.MessageContent { File = content.File, RoomName = content.RoomName }; foreach (var user in room.Users) { ServerModel.Server.SendMessage(user, ClientFilePostedCommand.CommandId, sendingContent); } } }
protected override void OnRun(MessageContent content, CommandArgs args) { if (string.IsNullOrEmpty(content.RoomName)) { throw new ArgumentException("content.RoomName"); } if (string.IsNullOrEmpty(content.NewAdmin)) { throw new ArgumentNullException("content.NewAdmin"); } if (content.RoomName == ServerChat.MainRoomName) { ServerModel.Api.Perform(new ServerSendSystemMessageAction(args.ConnectionId, SystemMessageId.RoomAccessDenied)); return; } using (var server = ServerModel.Get()) { Room room; if (!TryGetRoom(server.Chat, content.RoomName, args.ConnectionId, out room)) { return; } if (room.Admin != args.ConnectionId) { ServerModel.Api.Perform(new ServerSendSystemMessageAction(args.ConnectionId, SystemMessageId.RoomAccessDenied)); return; } if (!room.IsUserExist(content.NewAdmin)) { ServerModel.Api.Perform(new ServerSendSystemMessageAction(args.ConnectionId, SystemMessageId.RoomUserNotExist)); return; } room.Admin = content.NewAdmin; ServerModel.Api.Perform(new ServerSendSystemMessageAction(content.NewAdmin, SystemMessageId.RoomAdminChanged, room.Name)); } }
protected override void OnRun(MessageContent content, ServerCommandArgs args) { if (string.IsNullOrEmpty(content.RoomName)) { throw new ArgumentException("RoomName"); } if (string.IsNullOrEmpty(content.NewAdmin)) { throw new ArgumentNullException("NewAdmin"); } if (string.Equals(content.RoomName, ServerModel.MainRoomName)) { ServerModel.Api.SendSystemMessage(args.ConnectionId, SystemMessageId.RoomAccessDenied); return; } using (var server = ServerModel.Get()) { Room room; if (!TryGetRoom(server, content.RoomName, args.ConnectionId, out room)) { return; } if (!room.Admin.Equals(args.ConnectionId)) { ServerModel.Api.SendSystemMessage(args.ConnectionId, SystemMessageId.RoomAccessDenied); return; } if (!room.Users.Contains(content.NewAdmin)) { ServerModel.Api.SendSystemMessage(args.ConnectionId, SystemMessageId.RoomUserNotExist); return; } room.Admin = content.NewAdmin; ServerModel.Api.SendSystemMessage(content.NewAdmin, SystemMessageId.RoomAdminChanged, room.Name); } }