summaryrefslogtreecommitdiff
path: root/lib/live_feed_bot.rb
blob: 3688eab92cb79be2e706e0861ce7520a977b9e6c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# frozen_string_literal: true
class LiveFeedBot
  def initialize
    @semafor = Mutex.new
    @config = Booru::CONFIG.livefeed
    @redis = Redis.new(host: 'localhost', driver: :hiredis)
    @sock = nil
    @ok_send = false

    @subscriber = Thread.new do
      @redis.subscribe 'modfeed' do |on|
        on.message do |_channel, message|
          putirc "PRIVMSG #{@config[:channels][:modfeed]} :#{message}" if @ok_send
        end
      end
    end
  end

  def run_irc_conn
    @sock = TCPSocket.new(
      @config[:irc_host],
      @config[:irc_port]
    )
    @ok_send = false

    putirc "NICK #{@config[:irc_nick]}"
    putirc "USER #{@config[:irc_user]} * * :#{@config[:irc_real]}"

    while line = @sock.gets
      line.chomp!
      puts "irc >>> #{line}"

      split = line.split(' ')

      if split[0] == 'PING'
        putirc "PONG #{split[1..].join(' ')}"
      elsif split[1] == '376'
        putirc "JOIN #{@config[:channels][:modfeed]}"
        @ok_send = true
      end
    end
  rescue
    @sock = nil
  end

  private
  def open_irc_conn(proxy)

  end

  def putirc(line)
    return unless @sock

    @semafor.synchronize do
      @sock.puts line
      puts "irc <<< #{line}"
    end
  end
end