Class: IRC::Connection

Inherits:
Object
  • Object
show all
Includes:
MonitorMixin
Defined in:
lib/irc/connection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host = nil, port = 6667) ⇒ Connection

Returns a new instance of Connection.



10
11
12
13
# File 'lib/irc/connection.rb', line 10

def initialize host = nil, port = 6667
  @host = host
  @port = port
end

Instance Attribute Details

#hostObject

Returns the value of attribute host.



9
10
11
# File 'lib/irc/connection.rb', line 9

def host
  @host
end

#portObject

Returns the value of attribute port.



9
10
11
# File 'lib/irc/connection.rb', line 9

def port
  @port
end

Instance Method Details

#connected?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/irc/connection.rb', line 19

def connected?
  !disconnected?
end

#disconnectObject



63
64
65
66
# File 'lib/irc/connection.rb', line 63

def disconnect
  quit
  socket.close
end

#disconnected?Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/irc/connection.rb', line 15

def disconnected?
  !@socket || @socket.closed?
end

#join(*channels) ⇒ Object



27
28
29
30
31
32
# File 'lib/irc/connection.rb', line 27

def join *channels
  channels.flatten.each do |channel|
    @channel = channel
    write "JOIN #{channel}"
  end
end

#listenObject



73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/irc/connection.rb', line 73

def listen
  unless @listening
    loop do
      @listening = true
      socket.lines "\r\n" do |line|
        message = Message.new(line, self)
        Callback.handle message
      end
      @listening = false
    end
  end
end

#nick(_nick, realname = nil) ⇒ Object



40
41
42
43
44
# File 'lib/irc/connection.rb', line 40

def nick _nick, realname = nil
  realname ||= _nick
  write "NICK #{_nick}"
  write "USER #{_nick} localhost #{@host} :#{realname}"
end

#part(*channels) ⇒ Object



34
35
36
37
38
# File 'lib/irc/connection.rb', line 34

def part *channels
  channels.flatten.each do |channel|
    write "PART #{channel}"
  end
end

#pong(value) ⇒ Object



59
60
61
# File 'lib/irc/connection.rb', line 59

def pong value
  write "PONG #{value}"
end

#privmsg(content, *recipients) ⇒ Object



46
47
48
49
50
51
52
53
# File 'lib/irc/connection.rb', line 46

def privmsg content, *recipients
  recipients = @channel unless recipients.any?
  recipients = [recipients].flatten.join(',')

  msg = "PRIVMSG #{recipients} :#{content}"
  write msg.slice!(0,500)
  privmsg msg, recipients if msg != ''
end

#quit(message = nil) ⇒ Object



55
56
57
# File 'lib/irc/connection.rb', line 55

def quit message = nil
  write "QUIT :#{message}"
end

#socketObject



68
69
70
# File 'lib/irc/connection.rb', line 68

def socket
  @socket ||= TCPSocket.open(@host, @port)
end

#write(*strings) ⇒ Object



23
24
25
# File 'lib/irc/connection.rb', line 23

def write *strings
  socket.print *strings.map { |str| str + "\r\n" }
end