Class: Krtek::Irc::Client

Inherits:
Object
  • Object
show all
Includes:
DslAccessor, Krtek::Irc::Commands::RFC1459, Responses
Defined in:
lib/krtek/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Responses

#handle_parsed_message, included, #parse_message

Methods included from Krtek::Irc::Commands::RFC1459

#join, #nick, #ping, #pong, #privmsg, #server_connect, #user

Methods included from DslAccessor

included

Constructor Details

#initialize(opts = {}, &block) ⇒ Client

Returns a new instance of Client.



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
# File 'lib/krtek/client.rb', line 20

def initialize(opts = {}, &block)
  opts.symbolize_keys!
  opts = {
    :realname  => Faker::Name.name,
    :host      => "127.0.0.1",
    :port      => "6667",
    :ssl       => false,
    :username  => Faker::Name.first_name
  }.merge!(opts)

  @host      = opts[:host]
  @port      = opts[:port]
  @ssl       = opts[:ssl]
  @realname  = opts[:realname]
  @username  = opts[:username]
  @connected = false
  @channels  = Set.new
  @callbacks = Hash.new

  if block_given?
    if block.arity == 1
      yield self
    else
      instance_eval(&block)
    end
  end
end

Instance Attribute Details

#callbacksObject (readonly)

Returns the value of attribute callbacks.



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

def callbacks
  @callbacks
end

#channelsObject (readonly)

Returns the value of attribute channels.



11
12
13
# File 'lib/krtek/client.rb', line 11

def channels
  @channels
end

#connectionObject

Returns the value of attribute connection.



10
11
12
# File 'lib/krtek/client.rb', line 10

def connection
  @connection
end

#usernameObject (readonly)

Returns the value of attribute username.



13
14
15
# File 'lib/krtek/client.rb', line 13

def username
  @username
end

Instance Method Details

#connectObject



48
49
50
51
# File 'lib/krtek/client.rb', line 48

def connect
  self.connection ||= EM::connect(@host, @port, Handler,
                                  :client => self, :ssl => @ssl)
end

#connected?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/krtek/client.rb', line 53

def connected?
  @connected
end

#on(name, &block) ⇒ Object



93
94
95
# File 'lib/krtek/client.rb', line 93

def on(name, &block)
  (@callbacks[name.to_sym] ||= []) << block
end

#readyObject



72
73
74
75
76
# File 'lib/krtek/client.rb', line 72

def ready
  @connected = true
  user(@username, Faker::Internet.domain_word, Faker::Internet.domain_word, @realname)
  trigger(:connect)
end

#receive_data(data) ⇒ Object



63
64
65
66
67
68
69
70
# File 'lib/krtek/client.rb', line 63

def receive_data(data)
  data.split("\r\n").each do |message|
    trigger(:raw, message)
    parsed = parse_message(message)
    handle_parsed_message(parsed)
    trigger(:parsed, parsed)
  end
end

#run!Object



82
83
84
85
86
87
88
89
90
91
# File 'lib/krtek/client.rb', line 82

def run!
  EM.epoll
  EM.run do
    trap("TERM") { EM.stop }
    trap("INT") { EM.stop }
    connect
    puts "Starting IRC Client..."
  end
  puts "Stopping IRC Client..."
end

#send_data(message) ⇒ Object



57
58
59
60
61
# File 'lib/krtek/client.rb', line 57

def send_data(message)
  return false unless connected?
  message = message + "\r\n"
  self.connection.send_data(message)
end

#trigger(name, *args) ⇒ Object



97
98
99
# File 'lib/krtek/client.rb', line 97

def trigger(name, *args)
  (@callbacks[name.to_sym] || []).each {|blk| blk.call(*args)}
end

#unbindObject



78
79
80
# File 'lib/krtek/client.rb', line 78

def unbind
  trigger(:disconnect)
end