<commit_before>require 'ponder/callback'
require 'ponder/connection'
require 'ponder/irc'
require 'ostruct'

module Ponder
  class Thaum
    include IRC
    if RUBY_VERSION >= '1.9'
      require 'ponder/delegate'
      include Delegate
    end
    
    attr_reader :config
    attr_accessor :connected, :traffic_logger, :error_logger, :console_logger
    
    def initialize
      @config = OpenStruct.new(:server             => 'localhost',
                               :port               => 6667,
                               :nick               => 'Ponder',
                               :username           => 'Ponder',
                               :real_name          => 'Ponder',
                               :verbose            => true,
                               :logging            => false,
                               :reconnect          => true,
                               :reconnect_interval => 30
                              )
      
      @empty_logger   = Logger::BlindIo.new
      @traffic_logger = @empty_logger
      @error_logger   = @empty_logger
      @console_logger = Logger::Twoflogger.new($stdout)
      
      @observers = 0
      @temp_socket = []
      
      @connected = false
      @reloading = false
      
      # user callbacks
      @callbacks = Hash.new { |hash, key| hash[key] = [] } ## old: Hash.new []
      
      # observer synchronizer
      @mutex_observer = Mutex.new
      
      # standard callbacks for PING, VERSION and TIME
      on :query, /^\001PING \d+\001$/ do |env|
        time = env[:message].scan(/\d+/)[0]
        notice env[:nick], "\001PING #{time}\001"
      end
      
      on :query, /^\001VERSION\001$/ do |env|
        notice env[:nick], "\001VERSION Ponder #{Ponder::VERSION} (http://github.com/tbuehlmann/ponder)\001"
      end
      
      on :query, /^\001TIME\001$/ do |env|
        notice env[:nick], "\001TIME #{Time.now.strftime('%a %b %d %H:%M:%S %Y')}\001"
      end
    end
    
    def configure(&block)
      unless @reloading
        block.call(@config)
        
        # logger changes (if differing from initialize)
        if @config.logging
          @traffic_logger = @config.traffic_logger ? @config.traffic_logger : Logger::Twoflogger.new(Ponder.root.join('logs', 'traffic.log'))
          @error_logger = @config.error_logger ? @config.error_logger : Logger::Twoflogger.new(Ponder.root.join('logs', 'error.log'))
        end
        
        unless @config.verbose
          @console_logger = @empty_logger
        end
      end
    end
    
    def on(event_types = [:channel], match = //, &block)
      if event_types.is_a?(Array)
        callbacks = event_types.map { |event_type| Callback.new(event_type, match, block) }
      else
        callbacks = [Callback.new(event_types, match, block)]<commit_msg>added auto_rename functionality if Nickname is already in use<commit_after>require 'ponder/callback'
require 'ponder/connection'
require 'ponder/irc'
require 'ostruct'

module Ponder
  class Thaum
    include IRC
    if RUBY_VERSION >= '1.9'
      require 'ponder/delegate'
      include Delegate
    end
    
    attr_reader :config
    attr_accessor :connected, :traffic_logger, :error_logger, :console_logger
    
    def initialize
      @config = OpenStruct.new(:server             => 'localhost',
                               :port               => 6667,
                               :nick               => 'Ponder',
                               :username           => 'Ponder',
                               :real_name          => 'Ponder',
                               :verbose            => true,
                               :logging            => false,
                               :reconnect          => true,
                               :reconnect_interval => 30,
                               :auto_rename        => true
                              )
      
      @empty_logger   = Logger::BlindIo.new
      @traffic_logger = @empty_logger
      @error_logger   = @empty_logger
      @console_logger = Logger::Twoflogger.new($stdout)
      
      @observers = 0
      @temp_socket = []
      
      @connected = false
      @reloading = false
      
      # user callbacks
      @callbacks = Hash.new { |hash, key| hash[key] = [] } ## old: Hash.new []
      
      # observer synchronizer
      @mutex_observer = Mutex.new
      
      # standard callbacks for PING, VERSION, TIME and Nickname is already in use
      on :query, /^\001PING \d+\001$/ do |env|
        time = env[:message].scan(/\d+/)[0]
        notice env[:nick], "\001PING #{time}\001"
      end
      
      on :query, /^\001VERSION\001$/ do |env|
        notice env[:nick], "\001VERSION Ponder #{Ponder::VERSION} (http://github.com/tbuehlmann/ponder)\001"
      end
      
      on :query, /^\001TIME\001$/ do |env|
        notice env[:nick], "\001TIME #{Time.now.strftime('%a %b %d %H:%M:%S %Y')}\001"
      end
      
      on 433 do
        rename "#{@config.nick}#{rand(10)}#{rand(10)}#{rand(10)}"
      end
    end
    
    def configure(&block)
      unless @reloading
        block.call(@config)
        
        # logger changes (if differing from initialize)
        if @config.logging
          @traffic_logger = @config.traffic_logger ? @config.traffic_logger : Logger::Twoflogger.new(Ponder.root.join('logs', 'traffic.log'))
          @error_logger = @config.error_logger ? @config.error_logger : Logger::Twoflogger.new(Ponder.root.join('logs', 'error.log'))
        end
        
        unless @config.verbose
          @console_logger = @empty_logger
        end
        
        # remove auto_rename callback (if differing from initialize)
        unless @config.auto_rename
          @callbacks.delete(433)
        end
      end
    end
    
    def on(event_types = [:channel], match = //, &block)
      if event_types.is_a?(Array)
        callbacks = event_types.map { |event_type| Callback.new(event_type, match, block) }
      else
        callbacks = [Callback.new(event_types, match, block)]
{'fraction_non_alphanumeric': 0.11690677264447756, 'fraction_numerical': 0.0172213942705746, 'mean_word_length': 1.6329555361813426, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 24, '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': '4397537', 'n_tokens_mistral': 1822, 'n_tokens_neox': 1739, 'n_words': 379}