Class: IRC::Server

Inherits:
Object
  • Object
show all
Includes:
Commands
Defined in:
lib/on_irc/server.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Commands

#join, #notice, #part, #pong, #privmsg

Constructor Details

#initialize(irc, name, config) ⇒ Server

Returns a new instance of Server.



6
7
8
9
10
11
# File 'lib/on_irc/server.rb', line 6

def initialize(irc, name, config)
  @irc = irc
  @name = name
  @config = config
  @handlers = {}
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



3
4
5
# File 'lib/on_irc/server.rb', line 3

def config
  @config
end

#connectionObject

Returns the value of attribute connection.



3
4
5
# File 'lib/on_irc/server.rb', line 3

def connection
  @connection
end

#handlersObject

Returns the value of attribute handlers.



3
4
5
# File 'lib/on_irc/server.rb', line 3

def handlers
  @handlers
end

#ircObject

Returns the value of attribute irc.



3
4
5
# File 'lib/on_irc/server.rb', line 3

def irc
  @irc
end

#nameObject

Returns the value of attribute name.



3
4
5
# File 'lib/on_irc/server.rb', line 3

def name
  @name
end

Instance Method Details

#handle_event(event) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/on_irc/server.rb', line 29

def handle_event(event)
  if @handlers[:all]
    @handlers[:all].call(@irc, event)
  elsif @irc.handlers[:all]
    @irc.handlers[:all].call(@irc, event)
  end

  if @handlers[event.command]
    @handlers[event.command].call(@irc, event)
  elsif @irc.handlers[event.command]
    @irc.handlers[event.command].call(@irc, event)
  end
end

#on(event, &block) ⇒ Object



25
26
27
# File 'lib/on_irc/server.rb', line 25

def on(event, &block)
  @handlers[event.to_s.downcase.to_sym] = Callback.new(block)
end

#receive_line(line) ⇒ Object

Eventmachine callbacks



44
45
46
47
48
49
50
# File 'lib/on_irc/server.rb', line 44

def receive_line(line)
  parsed_line = Parser.parse(line)
  event = Event.new(self, parsed_line[:prefix],
                    parsed_line[:command].downcase.to_sym,
                    parsed_line[:params])
  handle_event(event)
end

#send_cmd(cmd, *args) ⇒ Object



13
14
15
16
17
18
19
20
# File 'lib/on_irc/server.rb', line 13

def send_cmd(cmd, *args)
  # remove nil entries
  args.compact!
  # prepend last arg with : only if it exists. it's really ugly
  args[-1] = ":#{args[-1]}" if args[-1]
  args = args.join(' ').delete("\r\n")
  connection.send_data("#{cmd.to_s.upcase} #{args}\r\n")
end

#unbindObject



52
53
54
55
56
57
# File 'lib/on_irc/server.rb', line 52

def unbind
  EM.add_timer(3) do
    connection.reconnect(config.address, config.port)
    connection.post_init
  end
end