<commit_before>﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Net;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Microsoft.Security.Application;
using SignalR.Hubs;
using SignalR.Samples.Hubs.Chat.ContentProviders;

namespace SignalR.Samples.Hubs.Chat {
    public class Chat : Hub, IDisconnect {
        private static readonly Dictionary<string, ChatUser> _users = new Dictionary<string, ChatUser>(StringComparer.OrdinalIgnoreCase);
        private static readonly Dictionary<string, HashSet<string>> _userRooms = new Dictionary<string, HashSet<string>>(StringComparer.OrdinalIgnoreCase);
        private static readonly Dictionary<string, ChatRoom> _rooms = new Dictionary<string, ChatRoom>(StringComparer.OrdinalIgnoreCase);

        private static readonly List<IContentProvider> _contentProviders = new List<IContentProvider>() {
            new ImageContentProvider(),
            new YouTubeContentProvider(),
            new CollegeHumorContentProvider()
        };


        public bool OldVersion {
            get {
                string version = Caller.version;
                return String.IsNullOrEmpty(version) ||
                        new Version(version) < typeof(Chat).Assembly.GetName().Version;
            }
        }

        public bool Join() {            
            Caller.version = typeof(Chat).Assembly.GetName().Version.ToString();

            // Check the user id cookie
            var cookie = Context.Cookies["userid"];
            if (cookie == null) {
                return false;
            }

            ChatUser user = _users.Values.FirstOrDefault(u => u.Id == cookie.Value);

            if (user != null) {
                // Update the users's client id mapping
                user.ClientId = Context.ClientId;

                // Set some client state
                Caller.id = user.Id;
                Caller.name = user.Name;
                Caller.hash = user.Hash;

                // Leave all rooms
                HashSet<string> rooms;
                if (_userRooms.TryGetValue(user.Name, out rooms)) {
                    foreach (var room in rooms) {
                        Clients[room].leave(user);
                        ChatRoom chatRoom = _rooms[room];
                        chatRoom.Users.Remove(user.Name);
                    }
                }

                _userRooms[user.Name] = new HashSet<string>(StringComparer.OrdinalIgnoreCase);

                // Add this user to the list of users
                Caller.addUser(user);
                return true;
            }

            return false;
        }

        public void Send(string content) {
            if (OldVersion) {
                Caller.addMessage(Guid.NewGuid().ToString(), "SERVER", "Chat was just updated, please refresh you browser and rejoin " + Caller.room);
                return;
            }

            content = Sanitizer.GetSafeHtmlFragment(content);

            if (!TryHandleCommand(content)) {
                string roomName = Caller.room;
                string name = Caller.name;

                EnsureUserAndRoom();
<commit_msg>Throw an exception when there's a server update<commit_after>﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Net;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Microsoft.Security.Application;
using SignalR.Hubs;
using SignalR.Samples.Hubs.Chat.ContentProviders;

namespace SignalR.Samples.Hubs.Chat {
    public class Chat : Hub, IDisconnect {
        private static readonly Dictionary<string, ChatUser> _users = new Dictionary<string, ChatUser>(StringComparer.OrdinalIgnoreCase);
        private static readonly Dictionary<string, HashSet<string>> _userRooms = new Dictionary<string, HashSet<string>>(StringComparer.OrdinalIgnoreCase);
        private static readonly Dictionary<string, ChatRoom> _rooms = new Dictionary<string, ChatRoom>(StringComparer.OrdinalIgnoreCase);

        private static readonly List<IContentProvider> _contentProviders = new List<IContentProvider>() {
            new ImageContentProvider(),
            new YouTubeContentProvider(),
            new CollegeHumorContentProvider()
        };


        public bool OldVersion {
            get {
                string version = Caller.version;
                return String.IsNullOrEmpty(version) ||
                        new Version(version) < typeof(Chat).Assembly.GetName().Version;
            }
        }

        public bool Join() {            
            Caller.version = typeof(Chat).Assembly.GetName().Version.ToString();

            // Check the user id cookie
            var cookie = Context.Cookies["userid"];
            if (cookie == null) {
                return false;
            }

            ChatUser user = _users.Values.FirstOrDefault(u => u.Id == cookie.Value);

            if (user != null) {
                // Update the users's client id mapping
                user.ClientId = Context.ClientId;

                // Set some client state
                Caller.id = user.Id;
                Caller.name = user.Name;
                Caller.hash = user.Hash;

                // Leave all rooms
                HashSet<string> rooms;
                if (_userRooms.TryGetValue(user.Name, out rooms)) {
                    foreach (var room in rooms) {
                        Clients[room].leave(user);
                        ChatRoom chatRoom = _rooms[room];
                        chatRoom.Users.Remove(user.Name);
                    }
                }

                _userRooms[user.Name] = new HashSet<string>(StringComparer.OrdinalIgnoreCase);

                // Add this user to the list of users
                Caller.addUser(user);
                return true;
            }

            return false;
        }

        public void Send(string content) {
            if (OldVersion) {
                throw new InvalidOperationException("Chat was just updated, please refresh you browser and rejoin " + Caller.room);
            }
            
            content = Sanitizer.GetSafeHtmlFragment(content);

            if (!TryHandleCommand(content)) {
                string roomName = Caller.room;
                string name = Caller.name;

                EnsureUserAndRoom();

{'fraction_non_alphanumeric': 0.08766580271382833, 'fraction_numerical': 0.0, 'mean_word_length': 1.8771929824561404, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4171139', 'n_tokens_mistral': 1657, 'n_tokens_neox': 1565, 'n_words': 396}