summaryrefslogtreecommitdiff
path: root/bocelli/core/irc.rb
blob: 4264e2d01d81e5d3941429e0bd654a6622b7227a (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
require 'socket'

module Bocelli
  module Core
    module IRC
      def configure(host, port, nick, pass = nil)
        @host = host
        @port = port
        @nick = nick
        @pass = pass

        @socket = nil
      end

      def connect(&block)
        @socket = TCPSocket.new(@host, @port)

        sputs("PASS #{@pass}") if @pass
        sputs("NICK #{@nick}")
        sputs("USER #{@nick} 0 * :#{@nick}")

        instance_eval(&block) if block_given?
      end

      def sgets
        str = @socket.gets
        str.chomp! unless str.nil?

        puts '<< ' + str.inspect

        str
      end

      def sputs(str)
        puts '>> ' + str.inspect

        @socket.puts(str)
      end

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

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

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