public void SendChat(string message) { // chat message //HtmlUtility utility = HtmlUtility.Instance; //message = utility.StripHtml(message); String user_ip = HttpContext.Current.Request.UserHostAddress; // this wasn't working / refreshing / allowing for a user to be // blocked and ignored mid-session // -- removing this as it's slow... //List<Block> blocked_ips = blockService.getBlocks(blockType.blockIP, blockWhat.blockChat); //if ((from m in blocked_ips select m.Value).Contains(user_ip)) //{ // Clients.Caller.errorMessage("You are in timeout."); // return; //} ChatMessage error = new ChatMessage(); error.ChatDate = DateTime.UtcNow; error.Nick = ""; // ignore blank messages and long messages if (message.Length == 0) { return; } if (message.Length > 2000) { error.Message = "Message was too long."; Clients.Caller.addMessage(error); return; } // get the current user ChatVisitor current_user; bool get_user = Visitors.TryGetValue(Context.ConnectionId, out current_user); if (get_user == false) { error.Message = "Can't find user."; Clients.Caller.addMessage(error); Clients.Caller.addSimpleMessage("chatbot: Can't find user."); return; } ChatMessage chat_1 = new ChatMessage(); ChatMessage chat = new ChatMessage(); chat.Nick = current_user.Handle + ":"; chat.Message = message; chat.ChatDate = DateTime.UtcNow; chat.StoredInDB = false; chat.Room = current_user.Room; chat.IP = HttpContext.Current.Request.UserHostAddress; bool handled = false; // // // COMMAND HANDLER!!!! // - available commands: // 1: /join n # where n is a number less than 10, join a room // if (message.StartsWith("/join")) { string room = message.Replace("/join ", ""); int room_number; if (int.TryParse(room, out room_number) == false) { room_number = 1; error.Message = "Unable to join room. Not a valid room number: <b>" + room + "</b>."; Clients.Caller.addMessage(error); Clients.Caller.addSimpleMessage("chatbot: " + error.Message); handled = true; } else { LeaveAndJoinGroup(room_number.ToString()); handled = true; } } if (message.StartsWith("/w ")) { // /w {username} message // WHISPER TIME string[] message_parameters = message.Split(' '); string whisper_to = message_parameters[1]; string extracted_message = message.Replace("/w " + whisper_to + " ", ""); bool found_person = false; chat_1.Nick = current_user.Handle + " *whisper*:"; chat_1.Message = extracted_message; chat_1.ChatDate = DateTime.UtcNow; chat_1.StoredInDB = false; chat_1.Room = current_user.ConnectionId; chat.ChatDate = DateTime.UtcNow; chat.Nick = current_user.Handle + " *whispered to " + whisper_to + "*:"; chat.Message = extracted_message; chat.StoredInDB = false; chat.Room = current_user.ConnectionId; foreach (ChatVisitor c in Visitors.Values) { if (c.Handle == whisper_to) { found_person = true; Clients.Client(c.ConnectionId).addMessage(chat_1); Clients.Client(c.ConnectionId).addSimpleMessage(chat_1.Nick + " " + chat_1.Message); Clients.Caller.addMessage(chat); Clients.Caller.addSimpleMessage(chat.Nick + " " + chat.Message); } } if (found_person == false) { chat_1.Nick = "chatbot:"; chat_1.Message = "could not find this user: "******" " + chat_1.Message); } handled = true; } if (message.StartsWith("/clear")) { if (HttpContext.Current.User.IsInRole("Mod")) { ChatMessage reboot = new ChatMessage(); // this has to be Now because the rest of the // uptime stuff uses Now instead of UtcNow DateTime lastChatDate = Messages.Last().ChatDate; chatStarted = lastChatDate; HttpContext.Current.Cache.Insert("startChatDate", lastChatDate, null, DateTime.UtcNow.AddDays(1), TimeSpan.Zero); // reset messages list Messages = new List<ChatMessage>(); reboot.ChatDate = DateTime.UtcNow; reboot.Room = "1"; reboot.Nick = "chatbot:"; reboot.Message = "The chat log was cleared by a moderator."; reboot.StoredInDB = false; Clients.Caller.addMessage(reboot); Clients.Caller.addSimpleMessage(reboot.Nick + " " + reboot.Message); } else { ChatMessage not_mod = new ChatMessage(); not_mod.Nick = "chatbot:"; not_mod.Message = "You must be logged into a mod account to run this command."; Clients.Caller.addMessage(not_mod); Clients.Caller.addSimpleMessage(not_mod.Nick + " " + not_mod.Message); } handled = true; } if (message.StartsWith("/help")) { chat_1.Nick = "chatbot:"; chat_1.Message = "Hey, friend! Welcome to the chat. Here's some secret commands you can do: "; chat_1.ChatDate = DateTime.UtcNow; chat_1.StoredInDB = false; chat_1.Room = current_user.Room; Clients.Caller.addMessage(chat_1); Clients.Caller.addSimpleMessage(chat_1.Nick + " " + chat_1.Message); chat_1.Message = "/help [type this to show this message]"; Clients.Caller.addMessage(chat_1); Clients.Caller.addSimpleMessage(chat_1.Nick + " " + chat_1.Message); chat_1.Message = "/join 2 [type this to join room 2, or any other numbered room]"; Clients.Caller.addMessage(chat_1); Clients.Caller.addSimpleMessage(chat_1.Nick + " " + chat_1.Message); chat_1.Message = "/ask Does she think about me? [ask the 8-ball a yes/no question :) ]"; Clients.Caller.addMessage(chat_1); Clients.Caller.addSimpleMessage(chat_1.Nick + " " + chat_1.Message); chat_1.Message = "/w seth lol this is a whisper [send a private message to user seth, only seen by you and them]"; Clients.Caller.addMessage(chat_1); Clients.Caller.addSimpleMessage(chat_1.Nick + " " + chat_1.Message); handled = true; } if (message.StartsWith("/ask ")) { message = message.Replace("/ask ", ""); List<String> magic_8_ball = new List<String>(); magic_8_ball.Add("It is certain"); magic_8_ball.Add("It is decidedly so"); magic_8_ball.Add("Without a doubt"); magic_8_ball.Add("Yes, definitely"); magic_8_ball.Add("You may rely on it"); magic_8_ball.Add("As I see it, yes"); magic_8_ball.Add("Most likely"); magic_8_ball.Add("Outlook is good"); magic_8_ball.Add("Yes"); magic_8_ball.Add("Signs point to yes"); magic_8_ball.Add("Reply hazy, try again"); magic_8_ball.Add("Ask again later"); magic_8_ball.Add("Better not tell you now"); magic_8_ball.Add("Cannot predict now"); magic_8_ball.Add("Concentrate and ask again"); magic_8_ball.Add("Don't count on it"); magic_8_ball.Add("My reply is no"); magic_8_ball.Add("My sources say no"); magic_8_ball.Add("Outlook not so good"); magic_8_ball.Add("Very doubtful"); chat_1.Nick = "chatbot:"; chat_1.Message = current_user.Handle + " asked the magic 8-ball: " + message; chat_1.ChatDate = DateTime.UtcNow; chat_1.StoredInDB = false; chat_1.Room = current_user.Room; // Call the addMessage method on all the clients in the room Clients.Group(current_user.Room).addMessage(chat_1); Clients.Group(current_user.Room).addSimpleMessage(chat_1.Nick + " " + chat_1.Message); ChatMessage chat_2 = new ChatMessage(); Random rand = new Random(); chat_2.Nick = "magic 8 ball:"; chat_2.Message = "And the answer is: " + magic_8_ball[rand.Next(magic_8_ball.Count)]; chat_2.ChatDate = DateTime.UtcNow; chat_2.StoredInDB = false; chat_2.Room = current_user.Room; Clients.Group(current_user.Room).addMessage(chat_2); Clients.Group(current_user.Room).addSimpleMessage(chat_1.Nick + " " + chat_1.Message); Messages.Add(chat_1); Messages.Add(chat_2); handled = true; } if (message.StartsWith("/stats")) { String stats_message = ""; String stats_intro = "General stats: "; String check_user = ""; chat_1 = new ChatMessage(); if (message != "/stats") { check_user = message.Replace("/stats ", ""); stats_intro = "Stats for " + check_user + ": "; } message = _chatService.GetStats(check_user); chat_1.Nick = "chatbot:"; chat_1.Message = stats_intro + message; chat_1.ChatDate = DateTime.UtcNow; chat_1.StoredInDB = false; chat_1.Room = current_user.Room; Clients.Caller.addMessage(chat_1); Clients.Caller.addSimpleMessage(chat_1.Nick + " " + chat_1.Message); handled = true; } //if (message.StartsWith("/report ")) { // string report_msg = message.Replace("/report ", ""); //} if (message.StartsWith("/ips")) { if (HttpContext.Current.User.IsInRole("Mod")) { ChatMessage ip_line = new ChatMessage(); ip_line.Nick = "chatbot:"; ip_line.Message = "Here are the IP address of the " + Visitors.Values.Count + " chatters."; Clients.Caller.addMessage(ip_line); Clients.Caller.addSimpleMessage(ip_line.Nick + " " + ip_line.Message); foreach (ChatVisitor chatter in Visitors.Values) { ip_line = new ChatMessage(); ip_line.Message = chatter.IP; ip_line.Nick = chatter.Handle; Clients.Caller.addMessage(ip_line); Clients.Caller.addSimpleMessage(ip_line.Nick + " " + ip_line.Message); } } else { ChatMessage not_mod = new ChatMessage(); not_mod.Nick = "chatbot:"; not_mod.Message = "You must be logged into a mod account to run this command."; Clients.Caller.addMessage(not_mod); Clients.Caller.addSimpleMessage(not_mod.Nick + " " + not_mod.Message); } handled = true; } if (handled == false) { // Call the addMessage method on all the clients in the room Clients.Group(current_user.Room).addMessage(chat); string simple_chat = chat.Nick + " " + chat.Message; Clients.Group(current_user.Room).addSimpleMessage(simple_chat); Messages.Add(chat); } // every message, update the database UpdateDatabase(); //TimeSpan uptime = DateTime.Now - Started; //Clients.Group("admins").addMessage(String.Format("{0} active users. {1} max users. uptime is {2}", Visitors.Count, Max, uptime.Hours + " hours, " + uptime.Minutes + " minutes.")); }
public override Task OnDisconnected() { if (Global_Connections > 0) { Global_Connections--; } Connection_Ids.Remove(Context.ConnectionId); // get the current user ChatVisitor current_user; bool get_user = Visitors.TryGetValue(Context.ConnectionId, out current_user); if (get_user == true) { ChatMessage chat = new ChatMessage(); chat.Nick = "Left:"; chat.Message = current_user.Handle; chat.ChatDate = DateTime.UtcNow; chat.Room = current_user.Room; // not sure this is necessary when sending to client Clients.Group(current_user.Room).addMessage(chat); Messages.Add(chat); Visitors.Remove(current_user.ConnectionId); } TimeSpan uptime = DateTime.Now - Started; Clients.Group("admins").addMessage(String.Format("{0} active users. {1} max users. uptime is {2}", Connection_Ids.Count, Max, uptime.Hours + " hours, " + uptime.Minutes + " minutes.")); return Clients.All.leave(Context.ConnectionId, DateTime.Now.ToString()); }
public void JoinGroup(string room, bool display_welcome) { if (Visitors.ContainsKey(Context.ConnectionId) == false) { Debug.Print("Phantom user tried to connect."); return; } // check to see if the user is joining the mod room, // make sure they are mod... if they are not, // send them to room 1? if (room.Equals("4300")) { if (!HttpContext.Current.User.IsInRole("Mod")) { ChatMessage not_mod = new ChatMessage(); not_mod.Nick = "chatbot:"; not_mod.Message = "You must be logged into a mod account to join room 4300."; JoinGroup("1", false); Clients.Caller.addMessage(not_mod); Clients.Caller.addSimpleMessage(not_mod.Nick + " " + not_mod.Message); return; } } // get the current user ChatVisitor current_user = Visitors[Context.ConnectionId]; current_user.Room = room; ChatMessage announced = new ChatMessage(); announced.Nick = "Joined:"; announced.Message = current_user.Handle; announced.ChatDate = DateTime.UtcNow; announced.Room = room; // well this seems silly, but want to make sure numbers work // add to the group Groups.Add(Context.ConnectionId, room); // send the message to the group about the new person if (display_welcome == true) { Clients.OthersInGroup(room).addMessage(announced); Clients.OthersInGroup(room).addSimpleMessage(announced.Nick + " " + announced.Message); } StringBuilder who_is_here = new StringBuilder(); List<ChatVisitor> people_in_room = (from m in Visitors.Values where m.Room.Equals(room) select m).ToList(); foreach (ChatVisitor nick in people_in_room) { who_is_here.Append(nick.Handle + ", "); } who_is_here.Remove(who_is_here.Length - 2, 2); ChatMessage chat = new ChatMessage(); chat.Nick = "In chat room " + room + ":"; chat.Message = who_is_here.ToString(); chat.ChatDate = DateTime.UtcNow; List<ChatMessage> chat_backlog = new List<ChatMessage>(); StringBuilder simple_chat_backlog = new StringBuilder(); chat_backlog = (from m in Messages where m.Room.Equals(room) orderby m.ChatDate descending select m).Take(200).ToList(); chat_backlog.Reverse(); chat_backlog.Add(chat); foreach (var c in chat_backlog) { simple_chat_backlog.AppendLine(c.Nick + " " + c.Message); } Clients.Caller.addBacklog(chat_backlog); Clients.Caller.addSimpleBacklog(simple_chat_backlog.ToString()); Messages.Add(announced); }
public void LeaveAndJoinGroup(string room) { // get the current user ChatVisitor current_user = Visitors[Context.ConnectionId]; // send a message to that group that the user has left ChatMessage chat = new ChatMessage(); chat.ChatDate = DateTime.UtcNow; chat.Message = current_user.Handle + " left this room to go somewhere else."; chat.Room = current_user.Room; chat.Nick = "Left"; Clients.OthersInGroup(current_user.Room).addMessage(chat); Messages.Add(chat); // leave the user's current room Groups.Remove(current_user.ConnectionId, current_user.Room); // tell the client to clear it's history ChatMessage reset = new ChatMessage(); reset.Room = "reset-channel"; Clients.Caller.addMessage(reset); // join the new room JoinGroup(room, true); }
public void SendChat(string message) { // chat message String user_ip = Context.Request.GetHttpContext().Request.UserHostAddress; ChatMessage error = new ChatMessage(); error.ChatDate = DateTime.UtcNow; error.Nick = ""; // ignore blank messages and long messages if (message.Length == 0) { return; } if (message.Length > 2000) { error.Message = "Message was too long."; Clients.Caller.addMessage(error); return; } // get the current user ChatVisitor current_user; bool get_user = Visitors.TryGetValue(Context.ConnectionId, out current_user); if (get_user == false) { error.Message = "Can't find user."; Clients.Caller.addMessage(error); Clients.Caller.addSimpleMessage("chatbot: Can't find user."); return; } ChatMessage chat_1 = new ChatMessage(); ChatMessage chat = new ChatMessage(); chat.Nick = current_user.Handle + ":"; chat.Message = message; chat.ChatDate = DateTime.UtcNow; chat.StoredInDB = false; chat.Room = current_user.Room; chat.IP = Context.Request.GetHttpContext().Request.UserHostAddress; bool handled = false; // // // COMMAND HANDLER!!!! // - available commands: // 1: /join n # where n is a number less than 10, join a room // if (message.StartsWith("/join")) { string room = message.Replace("/join ", ""); int room_number; if (int.TryParse(room, out room_number) == false) { room_number = 1; error.Message = "Unable to join room. Not a valid room number: <b>" + room + "</b>."; Clients.Caller.addMessage(error); Clients.Caller.addSimpleMessage("chatbot: " + error.Message); handled = true; } else { LeaveAndJoinGroup(room_number.ToString()); handled = true; } } if (message.StartsWith("/ask ")) { message = message.Replace("/ask ", ""); List<String> magic_8_ball = new List<String>(); magic_8_ball.Add("It is certain"); magic_8_ball.Add("It is decidedly so"); magic_8_ball.Add("Without a doubt"); magic_8_ball.Add("Yes, definitely"); magic_8_ball.Add("You may rely on it"); magic_8_ball.Add("As I see it, yes"); magic_8_ball.Add("Most likely"); magic_8_ball.Add("Outlook is good"); magic_8_ball.Add("Yes"); magic_8_ball.Add("Signs point to yes"); magic_8_ball.Add("Reply hazy, try again"); magic_8_ball.Add("Ask again later"); magic_8_ball.Add("Better not tell you now"); magic_8_ball.Add("Cannot predict now"); magic_8_ball.Add("Concentrate and ask again"); magic_8_ball.Add("Don't count on it"); magic_8_ball.Add("My reply is no"); magic_8_ball.Add("My sources say no"); magic_8_ball.Add("Outlook not so good"); magic_8_ball.Add("Very doubtful"); chat_1.Nick = "chatbot:"; chat_1.Message = current_user.Handle + " asked the magic 8-ball: " + message; chat_1.ChatDate = DateTime.UtcNow; chat_1.StoredInDB = false; chat_1.Room = current_user.Room; // Call the addMessage method on all the clients in the room Clients.Group(current_user.Room).addMessage(chat_1); Clients.Group(current_user.Room).addSimpleMessage(chat_1.Nick + " " + chat_1.Message); ChatMessage chat_2 = new ChatMessage(); Random rand = new Random(); chat_2.Nick = "magic 8 ball:"; chat_2.Message = "And the answer is: " + magic_8_ball[rand.Next(magic_8_ball.Count)]; chat_2.ChatDate = DateTime.UtcNow; chat_2.StoredInDB = false; chat_2.Room = current_user.Room; Clients.Group(current_user.Room).addMessage(chat_2); Clients.Group(current_user.Room).addSimpleMessage(chat_1.Nick + " " + chat_1.Message); Messages.Add(chat_1); Messages.Add(chat_2); handled = true; } if (message.StartsWith("/stats")) { String stats_message = ""; String stats_intro = "General stats: "; String check_user = ""; chat_1 = new ChatMessage(); if (message != "/stats") { check_user = message.Replace("/stats ", ""); stats_intro = "Stats for " + check_user + ": "; } message = "stats"; // _chatService.GetStats(check_user); chat_1.Nick = "chatbot:"; chat_1.Message = stats_intro + message; chat_1.ChatDate = DateTime.UtcNow; chat_1.StoredInDB = false; chat_1.Room = current_user.Room; Clients.Caller.addMessage(chat_1); Clients.Caller.addSimpleMessage(chat_1.Nick + " " + chat_1.Message); handled = true; } //if (message.StartsWith("/report ")) { // string report_msg = message.Replace("/report ", ""); //} if (message.StartsWith("/ips")) { if (Context.Request.GetHttpContext().User.IsInRole("Mod")) { ChatMessage ip_line = new ChatMessage(); ip_line.Nick = "chatbot:"; ip_line.Message = "Here are the IP address of the " + Visitors.Values.Count + " chatters."; Clients.Caller.addMessage(ip_line); Clients.Caller.addSimpleMessage(ip_line.Nick + " " + ip_line.Message); foreach (ChatVisitor chatter in Visitors.Values) { ip_line = new ChatMessage(); ip_line.Message = chatter.IP; ip_line.Nick = chatter.Handle; Clients.Caller.addMessage(ip_line); Clients.Caller.addSimpleMessage(ip_line.Nick + " " + ip_line.Message); } } else { ChatMessage not_mod = new ChatMessage(); not_mod.Nick = "chatbot:"; not_mod.Message = "You must be logged into a mod account to run this command."; Clients.Caller.addMessage(not_mod); Clients.Caller.addSimpleMessage(not_mod.Nick + " " + not_mod.Message); } handled = true; } if (handled == false) { // Call the addMessage method on all the clients in the room Clients.Group(current_user.Room).addMessage(chat); string simple_chat = chat.Nick + " " + chat.Message; Clients.Group(current_user.Room).addSimpleMessage(simple_chat); Messages.Add(chat); } // every message, update the database UpdateDatabase(); //TimeSpan uptime = DateTime.Now - Started; //Clients.Group("admins").addMessage(String.Format("{0} active users. {1} max users. uptime is {2}", Visitors.Count, Max, uptime.Hours + " hours, " + uptime.Minutes + " minutes.")); }