Class: EventMachine::IRC::Client

Inherits:
Object
  • Object
show all
Includes:
DslAccessor, Commands, Responses
Defined in:
lib/em-irc/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Commands

#admin, #away, #channel_mode, #die, #error, #info, #invite, #ison, #join, #kick, #kill, #links, #list, #lusers, #mode, #motd, #names, #nick, #notice, #oper, #part, #part_all, #pass, #ping, #pong, #privmsg, #quit, #rehash, #restart, #server_connect, #service, #servlist, #squery, #squit, #stats, #summon, #time, #topic, #trace, #user, #userhost, #users, #version, #wallops, #who, #whois, #whowas

Constructor Details

#initialize(options = {}) {|client| ... } ⇒ Client

Build a new unconnected IRC client

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :host (String)
  • :port (String)
  • :ssl (Boolean)
  • :realname (String)

Yields:

  • (client)

    new instance for decoration



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/em-irc/client.rb', line 49

def initialize(options = {}, &blk)
  options.symbolize_keys!
  options = {
    :host =>     '127.0.0.1',
    :port =>     '6667',
    :ssl =>      false,
    :realname => 'Anonymous Annie'
  }.merge!(options)

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

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

Instance Attribute Details

#channelsObject (readonly)

Set of channels that this client is connected to



33
34
35
# File 'lib/em-irc/client.rb', line 33

def channels
  @channels
end

Instance Method Details

#channel?(string) ⇒ Boolean (protected)

Returns:

  • (Boolean)


174
175
176
# File 'lib/em-irc/client.rb', line 174

def channel?(string)
  !!(string =~ /^(#|&)/)
end

#connectEventMachine::Connection

Creates a Eventmachine TCP connection with :host and :port. It should be called after callbacks are registered.

Returns:

  • (EventMachine::Connection)

See Also:



79
80
81
# File 'lib/em-irc/client.rb', line 79

def connect
  self.conn ||= EventMachine::connect(@host, @port, Dispatcher, :parent => self, :ssl => @ssl)
end

#connected?Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/em-irc/client.rb', line 84

def connected?
  @connected
end

#on(name, &blk) ⇒ Object

Register a callback with ‘:name` as one of the following, and a block with the same number of params.

Examples:

on(:join) {|channel| puts channel}

:connect - called after connection to server established

:join
  @param who [String]
  @param channel [String]
  @param names [Array]

:message, :privmsg - called on channel message or nick message
  @param source [String]
  @param target [String]
  @param message [String]

:raw - called for all messages from server
  @param raw_hash [Hash] same format as return of #parse_message


123
124
125
126
127
# File 'lib/em-irc/client.rb', line 123

def on(name, &blk)
  # TODO: I thought Hash.new([]) would work, but it gets empted out
  # TODO: normalize aliases :privmsg, :message, etc
  (@callbacks[name.to_sym] ||= []) << blk
end

#run!Object

Start running the client



89
90
91
92
93
94
95
96
97
98
99
# File 'lib/em-irc/client.rb', line 89

def run!
  EM.epoll
  EventMachine.run do
    trap("TERM") { EM::stop }
    trap("INT")  { EM::stop }
    connect
    log Logger::INFO, "Starting IRC client..."
  end
  log Logger::INFO, "Stopping IRC client"
  @logger.close if @logger
end