<commit_before>require 'rubygems'
require 'sinatra'
require 'sha1'
require 'eventmachine'
require 'json'

set :public, Proc.new { File.join(root, 'public') }
set :views, Proc.new { File.join(root, 'views') }

puts "*** Must be run in production mode (-e production)"

module JsChat
  class Server
    attr_reader :connection

    def initialize(cookie)
      @cookie = cookie
      run
    end

    module EventServer
      include Rack::Utils
      alias_method :h, :escape_html

      def post_init
        puts "post_init"
        @identified = false
        @messages = []
      end

      def identified?
        @identified
      end

      def messages
        @messages
      end

      def room=(room)
        @room = room
      end

      def unbind
        puts "*** Server disconnecting.  Count now: #{JsChat::Bridge.servers.size}"
        JsChat::Bridge.servers.delete_if { |hash, server| server.connection == self }
        puts "*** Server disconnected.  Count now: #{JsChat::Bridge.servers.size}"
      end

      def receive_data(data)
        json = JSON.parse(data)

        puts "LINE FROM SERVER: #{data}"

        if @identified == false and json['identified']
          @identified = true
          @name = json['name']
          puts "*** IDENTIFIED, JOINING ROOM"
          send_data({'join' => @room}.to_json)
        elsif @identified and json['display'] == 'join'
          puts "*** Channel joined"
        elsif @identified
          @messages << sanitize_json(json).to_json
        end
      end

      def sanitize_json(json)
        # Sanitize output
        json.each do |field, value|
          if value.kind_of? String
            json[field] = h value
          elsif value.kind_of? Hash
            json[field] = sanitize_json(value)
          elsif value.kind_of? Array
            json[field] = value.collect do |v|
              if v.kind_of? String
                h v
              else
                sanitize_json(v)
              end
            end
          end
        end
        json
      end
    end

    def quit
      @connection.close_connection
    end

    def room=(room)
      @connection.room = room
    end

    def identify(name)
      @connection.send_data({'identify' => name}.to_json)
    end

    def identified?
      @connection.identified?
    end

    def run
      EM.run do
        @connection = EM.connect '0.0.0.0', 6789, EventServer
      end
    end

    def recent_messages
      messages = @connection.messages.dup
      @connection.messages.clear
      messages
    end
    
    def send_message(message)
      @connection.send_data({ 'to' => '#merk', 'send' => message }.to_json + "\n")
    end
  end

  class Bridge
    @@servers = {}
    attr_accessor :name, :server

    def initialize(cookie)
      @cookie = cookie
      setup_connection
    end

    def self.servers
      @@servers
    end

    def self.new_cookie
      SHA1.hexdigest Time.now.usec.to_s
    end

    def self.new_server(cookie)
      server = Server.new cookie
      @@servers[cookie] = server
    end

    def recent_messages
      @server.recent_messages
    end

    def setup_connection
      if @@servers[@cookie]
        @server = @@servers[@cookie]
      else
        Bridge.new_server(@cookie)
        @server = @@servers[@cookie]
      end
    end

    def polled
      return if @quit

      @polling = true
      @last_poll ||= Time.now

      if @poll_thread.nil?<commit_msg>Fixed a minor bug in web polling<commit_after>require 'rubygems'
require 'sinatra'
require 'sha1'
require 'eventmachine'
require 'json'

set :public, Proc.new { File.join(root, 'public') }
set :views, Proc.new { File.join(root, 'views') }

puts "*** Must be run in production mode (-e production)"

module JsChat
  class Server
    attr_reader :connection

    def initialize(cookie)
      @cookie = cookie
      run
    end

    module EventServer
      include Rack::Utils
      alias_method :h, :escape_html

      def post_init
        puts "post_init"
        @identified = false
        @messages = []
      end

      def identified?
        @identified
      end

      def messages
        @messages
      end

      def room=(room)
        @room = room
      end

      def unbind
        puts "*** Server disconnecting.  Count now: #{JsChat::Bridge.servers.size}"
        JsChat::Bridge.servers.delete_if { |hash, server| server.connection == self }
        puts "*** Server disconnected.  Count now: #{JsChat::Bridge.servers.size}"
      end

      def receive_data(data)
        json = JSON.parse(data)

        puts "LINE FROM SERVER: #{data}"

        if @identified == false and json['identified']
          @identified = true
          @name = json['name']
          puts "*** IDENTIFIED, JOINING ROOM"
          send_data({'join' => @room}.to_json)
        elsif @identified and json['display'] == 'join'
          puts "*** Channel joined"
        elsif @identified
          @messages << sanitize_json(json).to_json
        end
      end

      def sanitize_json(json)
        # Sanitize output
        json.each do |field, value|
          if value.kind_of? String
            json[field] = h value
          elsif value.kind_of? Hash
            json[field] = sanitize_json(value)
          elsif value.kind_of? Array
            json[field] = value.collect do |v|
              if v.kind_of? String
                h v
              else
                sanitize_json(v)
              end
            end
          end
        end
        json
      end
    end

    def quit
      @connection.close_connection
    end

    def room=(room)
      @connection.room = room
    end

    def identify(name)
      @connection.send_data({'identify' => name}.to_json)
    end

    def identified?
      @connection.identified?
    end

    def run
      EM.run do
        @connection = EM.connect '0.0.0.0', 6789, EventServer
      end
    end

    def recent_messages
      messages = @connection.messages.dup
      @connection.messages.clear
      messages
    end
    
    def send_message(message)
      @connection.send_data({ 'to' => '#merk', 'send' => message }.to_json + "\n")
    end
  end

  class Bridge
    @@servers = {}
    attr_accessor :name, :server

    def initialize(cookie)
      @cookie = cookie
      setup_connection
    end

    def self.servers
      @@servers
    end

    def self.new_cookie
      SHA1.hexdigest Time.now.usec.to_s
    end

    def self.new_server(cookie)
      server = Server.new cookie
      @@servers[cookie] = server
    end

    def recent_messages
      @server.recent_messages
    end

    def setup_connection
      if @@servers[@cookie]
        @server = @@servers[@cookie]
      else
        Bridge.new_server(@cookie)
        @server = @@servers[@cookie]
      end
    end

    def polled
      return if @quit

      @polling = true
      @last_poll = Time.now

      if @poll_thread.nil?
{'fraction_non_alphanumeric': 0.09761042722664735, 'fraction_numerical': 0.002896451846488052, 'mean_word_length': 1.8643716300290336, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '2303553', 'n_tokens_mistral': 2117, 'n_tokens_neox': 2035, 'n_words': 545}