Class: Buddhy::Connection

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConnection

Returns a new instance of Connection.



7
8
9
10
# File 'lib/buddhy/connection.rb', line 7

def initialize
  @listening = false
  @listener = Listener.new
end

Class Method Details

.open(server) ⇒ Object



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

def self.open(server)
  new.connect(server)
end

Instance Method Details

#closeObject



37
38
39
40
# File 'lib/buddhy/connection.rb', line 37

def close
  @socket.puts "PART #{@channel}"
  @socket.puts "QUIT"
end

#connect(server) ⇒ Object



16
17
18
19
20
# File 'lib/buddhy/connection.rb', line 16

def connect(server)
  @socket = TCPSocket.open(server, 6667)
  @listener.register(/PING/) { @socket.puts "PONG" }
  self
end

#join(channel) ⇒ Object



32
33
34
35
# File 'lib/buddhy/connection.rb', line 32

def join(channel)
  @channel = channel
  @socket.puts "JOIN #{channel}"
end

#join_channel(bot, channel) ⇒ Object



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

def join_channel(bot, channel)
  (bot.nick, bot.name)
  join(channel)
end

#listen(pattern, &block) ⇒ Object



46
47
48
# File 'lib/buddhy/connection.rb', line 46

def listen(pattern, &block)
  @listener.register(pattern, &block)
end

#listen!Object



50
51
52
53
# File 'lib/buddhy/connection.rb', line 50

def listen!
  setup_trap
  start_listening
end

#login(nick, name) ⇒ Object



27
28
29
30
# File 'lib/buddhy/connection.rb', line 27

def (nick, name)
  @socket.puts "USER #{nick} 0 * #{name}" # no idea what `0 *` means
  @socket.puts "NICK #{nick}"
end

#say(msg) ⇒ Object



42
43
44
# File 'lib/buddhy/connection.rb', line 42

def say(msg)
  @socket.puts "PRIVMSG #{@channel} :#{msg}"
end