Class: IrcClient

Inherits:
Object
  • Object
show all
Defined in:
lib/irc_client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(server, nick, options = {}) ⇒ IrcClient

Returns a new instance of IrcClient.



14
15
16
17
18
19
# File 'lib/irc_client.rb', line 14

def initialize(server, nick, options = {})
  @socket = TCPSocket.open(server, options[:port] || 6667)
  socket.puts "PASS #{options[:password]}" if options[:password]
  socket.puts "NICK #{nick}"
  socket.puts "USER #{nick} #{nick} #{nick} :#{nick}"
end

Instance Attribute Details

#channelObject

Returns the value of attribute channel.



12
13
14
# File 'lib/irc_client.rb', line 12

def channel
  @channel
end

#socketObject

Returns the value of attribute socket.



12
13
14
# File 'lib/irc_client.rb', line 12

def socket
  @socket
end

Instance Method Details

#join(channel, key = nil) ⇒ Object



21
22
23
24
# File 'lib/irc_client.rb', line 21

def join(channel, key = nil)
  self.channel = channel
  socket.puts "JOIN ##{self.channel} #{key}".strip
end

#leaveObject



30
31
32
# File 'lib/irc_client.rb', line 30

def leave
  socket.puts "PART ##{channel}"
end

#quitObject



38
39
40
41
# File 'lib/irc_client.rb', line 38

def quit
  socket.puts "QUIT"
  socket.gets until socket.eof?
end

#run(&block) ⇒ Object



26
27
28
# File 'lib/irc_client.rb', line 26

def run(&block)
  instance_eval(&block) if block_given?
end

#say(message) ⇒ Object



34
35
36
# File 'lib/irc_client.rb', line 34

def say(message)
  socket.puts "PRIVMSG ##{channel} :#{message}" if channel
end