<commit_before><commit_msg>first cut at an irc connector<commit_after>require "em-irc"
require "logger"
require 'byebug'

class IRCConnector < Linkbot::Connector
  Linkbot::Connector.register('irc', self)

  def initialize(options)
    super(options)

    @nick     = options["nick"]
    @fullname = options["fullname"]
    @username = options["username"]
    @host     = options["server"]
    @port     = options["port"] || "6667"
    puts "got options #{options.inspect}"
    @rooms    = options["rooms"]
    puts "got rooms #{@rooms.inspect}"

    listen
  end

  def listen
    host = @host
    port = @port
    rooms = @rooms
    nick = @nick
    rooms = @rooms
    parent = self

    @client = EventMachine::IRC::Client.new do
      host host
      port port

      on :connect do
        puts "connected to #{host}:#{port}, setting nick #{nick}"
        nick nick
        puts "joining rooms #{rooms.inspect}"
        rooms.each do |room|
          puts "connecting to #{room}"
          join room
        end
      end

      on :join do |channel|  # called after joining a channel
        puts "joined #{channel}"
      end

      on :message do |user, room, message|  # called when being messaged
        parent.handle_message(user, room, message)
        puts "<#{user}> -> <#{room}>: #{message}"
      end
    end

    @client.run!  # start EventMachine loop
  end

  def handle_message(nick, room, msg)
    options = {room: room}

    if !Linkbot.user_exists?(nick)
      Linkbot.add_user(nick, nick)
    end

    message = Message.new(msg, Linkbot.user_id(nick), nick, self, :message, options)
    invoke_callbacks(message, options)
  end

  def send_messages(msgs, options={})
    puts "Got messages #{msgs} <<#{options}>>"
    msgs.each do |msg|
      if msg && msg.strip.length > 0 && options[:room]
        @client.message(options[:room], msg)
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.12758073054526203, 'fraction_numerical': 0.0026469031233456856, 'mean_word_length': 2.5727788279773156, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 9, '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': '1499754', 'n_tokens_mistral': 596, 'n_tokens_neox': 591, 'n_words': 171}