/// <summary> /// Handles when a user attempts to join a chat. /// </summary> /// <param name="incomingMsg">The message used to /// login to a chat with.</param> private void JoinChatCommand(Message incomingMsg) { string hashword2 = incomingMsg.message.Substring(0, HASHED_PW_SIZE); int id = -1; if (int.TryParse(incomingMsg.message.Substring(HASHED_PW_SIZE, incomingMsg.message.Length - HASHED_PW_SIZE), out id)) { ChatroomLogic tempChatroom2 = chatroomList.idToChatroom(id); if (tempChatroom2 != null) { if (ChatroomList.chatroomServices.AddUser( tempChatroom2.chatroomID, this.userID, hashword2)) { tempChatroom2.Subscribe(this); tempChatroom2.RegisteredUsers.Add(this.userID); messageService.SendMessage( new Message { chatID = -1, command = "ACK", message = "Chatroom login succeded" }); SendChatroomList(); SendChatHistory(tempChatroom2.chatroomID); } else { messageService.SendMessage( new Message { chatID = -1, command = "EXCEPTION", message = "Chatroom login failed" }); return; } } } else { messageService.SendMessage( new Message { chatID = -1, command = "EXCEPTION", message = "Bad chatroom id" }); } }
/// <summary> /// Attempts to create a new chat for the client. /// </summary> /// <param name="incomingMsg">Details about the new chat.</param> private void NewChatCommand(Message incomingMsg) { //message = password for chatroom //chatID = wether it is a private room. 1 = direct message //room, 0 = normal password protected room ChatroomLogic tempChatroom = new ChatroomLogic(); string chatname = incomingMsg.message.Substring(HASHED_PW_SIZE, incomingMsg.message.Length - HASHED_PW_SIZE); string hashword = incomingMsg.message.Substring(0, HASHED_PW_SIZE); if (ChatroomList.chatroomServices.CreateChatroom(chatname, tempChatroom.chatroomID, this.userID, hashword, incomingMsg.chatID)) { chatroomList.addChat(tempChatroom); tempChatroom.name = chatname; tempChatroom.Subscribe(this); tempChatroom.RegisteredUsers.Add(this.userID); messageService.SendMessage(new Message { chatID = -1, command = "ACK", message = "Chatroom has been created" }); SendChatroomList(); SendChatHistory(tempChatroom.chatroomID); } else { messageService.SendMessage(new Message { chatID = -1, command = "EXCEPTION", message = "Chatroom name already taken" }); } }
/// <summary> /// Subscribes this client to a chat. Any new information to the ChatroomLogic that /// this object is subsribed to will come through the /// OnNext() function. /// </summary> /// <param name="crl"></param> public void subsribeToChat(ChatroomLogic crl) { crl.Subscribe(this); crl.RegisteredUsers.Add(this.userID); chatrooms.Add(crl); }