aboutsummaryrefslogtreecommitdiff
path: root/core/irc.rb
blob: 0cde75dc18d2adb10e62dd9c5855ecb6369394f8 (plain) (blame)
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
60
61
62
63
64
65
66
67
68
69
70
71
require 'socket'

require_relative 'logger'

module Dinobot
  module Core
    class IRC
      def initialize(server, port, nick, pass=nil)
        @server = server
        @port = port
        @nick = nick
        @pass = pass

        @socket = nil
        @logger = Dinobot::Core::Logger.instance
      end

      def connect
        @logger.info "Connecting to #{@server}:#{@port}."

        @socket = TCPSocket.new(@server, @port)

        puts "PASS #{@pass}" if @pass
        puts "NICK #{@nick}"
        puts "USER #{@nick} 0 * :#{@nick}"
      end

      def disconnect
        @socket.close
      end

      def connected?
        !(@socket.nil? || @socket.closed?)
      end

      def gets
        str = @socket.gets

        @logger.in str.inspect

        str
      end

      def puts(str)
        @logger.out str.inspect

        @socket.puts str
      end

      def pong(message)
        puts "PONG #{message}"
      end

      def join(channel)
        puts "JOIN #{channel}"
      end

      def part(channel)
        puts "PART #{channel}"
      end

      def privmsg(channel, message)
        puts "PRIVMSG #{channel} :#{message}"
      end

      def quit(message)
        puts "QUIT :#{message}"
      end
    end
  end
end