<commit_before>#!/usr/bin/ruby
# encoding: utf-8

$: << File.dirname(__FILE__) 

require 'rubygems'
require 'storage'
require 'em-websocket'
require 'json'
require 'Tadpole.rb'
require 'socket'
require 'utils'


DEV_MODE = ARGV.include? "--dev"
VERBOSE_MODE = ARGV.include? "--verbose"

WHITELIST = ["http://rumpetroll.com","http://dev.rumpetroll.com","http://www.rumpetroll.com","http://rumpetroll.six12.co"]

class TadpoleConnection
	def initialize(socket, channel)
		@socket = socket
		@tadpole = Tadpole.new()
						
		socket.onopen {
		  
		  origin = socket.request["Origin"]		  
      port, ip = Socket.unpack_sockaddr_in(socket.get_peername)                          	                	  
      
      puts "Connection ##{@tadpole.id } from: #{ip}:#{port} at #{origin}" 
             	  
  	  if WHITELIST.include?(origin) || DEV_MODE
  		  @socket.send(%({"type":"welcome","id":#{@tadpole.id}}))
  		  subscribe(channel)
	    else
	      socket.close_connection 
      end            
		  		  
		}		
		
	end
	
	def subscribe(channel)
		@channel = channel
		@id = channel.subscribe {|message| @socket.send(message) }
		@socket.onmessage {|message| process_message(message) }
		@socket.onclose { unsubscribe }
	end
		
	def unsubscribe()
	  broadcast %({"type":"closed","id":#{@tadpole.id}})    
	  puts "Disconnect ##{@tadpole.id }"
		@channel.unsubscribe(@id)
	end
		
	def broadcast(message)
		# Broadcast message from client
		@channel << message
	end
	
	def process_message(data)
	  json = JSON.parse(data) rescue {};
    
    case json["type"]
    when "update"
      update_handler(json)
    when "message"  
      message_handler(json)
    end      
  end

  def update_handler(json)
    @tadpole.pos.x = json["x"]||0
    @tadpole.pos.y = json["y"]||0
    @tadpole.handle = (json["name"] || "Guest #{@tadpole.id}")[0...45]<commit_msg>Added simple spam detection to message handler.<commit_after>#!/usr/bin/ruby
# encoding: utf-8

$: << File.dirname(__FILE__) 

require 'rubygems'
require 'storage'
require 'em-websocket'
require 'json'
require 'Tadpole.rb'
require 'socket'
require 'utils'


DEV_MODE = ARGV.include? "--dev"
VERBOSE_MODE = ARGV.include? "--verbose"

WHITELIST = ["http://rumpetroll.com","http://dev.rumpetroll.com","http://www.rumpetroll.com","http://rumpetroll.six12.co"]

class TadpoleConnection
	def initialize(socket, channel)
		@socket = socket
		@tadpole = Tadpole.new()
		@last_update = 0;
		@quota = 10;
		
						
		socket.onopen {
		  
		  origin = socket.request["Origin"]		  
      port, ip = Socket.unpack_sockaddr_in(socket.get_peername)                          	                	  
      
      puts "Connection ##{@tadpole.id } from: #{ip}:#{port} at #{origin}" 
             	  
  	  if WHITELIST.include?(origin) || DEV_MODE
  		  @socket.send(%({"type":"welcome","id":#{@tadpole.id}}))
  		  subscribe(channel)
	    else
	      socket.close_connection 
      end            
		  		  
		}		
		
	end
	
	def subscribe(channel)
		@channel = channel
		@id = channel.subscribe {|message| @socket.send(message) }
		@socket.onmessage {|message| process_message(message) }
		@socket.onclose { unsubscribe }
	end
		
	def unsubscribe()
	  broadcast %({"type":"closed","id":#{@tadpole.id}})    
	  puts "Disconnect ##{@tadpole.id }"
		@channel.unsubscribe(@id)
	end
		
	def broadcast(message)
		# Broadcast message from client
		@channel << message
	end
	
	def detect_spam
	  now = Time.now.to_f
	  @quota -= 1 if now-@last_update < 0.3
    @last_update = now
    @socket.close_connection(true) if @quota < 0	 
	end
	
	def process_message(data)	  	  	  
	  json = JSON.parse(data) rescue {};

    case json["type"]
    when "update"
      update_handler(json)
    when "message"  
      detect_spam()
      message_handler(json)
    end      
  end

  def update_handler(json)
    @tadpole.pos.x = json["x"]||0
    @tadpole.pos.y = json["y"]||0
    @tadpole.handle = (json["name"] || "Guest #{@tadpole.id}")[0...45]
{'fraction_non_alphanumeric': 0.1536319919007846, 'fraction_numerical': 0.005821311060491015, 'mean_word_length': 2.7927063339731286, 'pattern_counts': {'":': 8, '<': 13, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3981058', 'n_tokens_mistral': 1541, 'n_tokens_neox': 1397, 'n_words': 254}