Class: Marvin::IRC::Client

Inherits:
AbstractClient show all
Defined in:
lib/marvin/irc/client.rb

Defined Under Namespace

Classes: EMConnection

Constant Summary collapse

@@stopped =
false

Instance Attribute Summary collapse

Attributes inherited from AbstractClient

#channels, #connection_config, #disconnect_expected, #nickname, #nicks, #pass, #port, #server

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from AbstractClient

#action, #command, configuration=, configure, #default_channels, #default_channels=, #handle_client_connected, #handle_incoming_error, #handle_incoming_join, #handle_incoming_numeric, #handle_incoming_ping, #handle_nick_taken, #handle_welcome, #host_with_port, #initialize, #join, #msg, #nick, #part, #pong, #pre_dispatching, #process_connect, #process_development, #process_disconnect, #quit, #receive_line, setup, setup?, #setup_handlers

Constructor Details

This class inherits a constructor from Marvin::AbstractClient

Instance Attribute Details

#em_connectionObject

Returns the value of attribute em_connection.



8
9
10
# File 'lib/marvin/irc/client.rb', line 8

def em_connection
  @em_connection
end

Class Method Details

.add_reconnect(c) ⇒ Object



142
143
144
145
146
147
148
# File 'lib/marvin/irc/client.rb', line 142

def add_reconnect(c)
  logger.warn "Adding timer to reconnect to #{c.server}:#{c.port} in 15 seconds"
  EventMachine.add_timer(15) do
    logger.warn "Preparing to reconnect to #{c.server}:#{c.port}"
    connect(c)
  end
end

.connect(c, &blk) ⇒ Object

Raises:

  • (ArgumentError)


123
124
125
126
127
128
129
130
# File 'lib/marvin/irc/client.rb', line 123

def connect(c, &blk)
  c = normalize_connection_options(c)
  raise ArgumentError, "Your connection options must specify a server" if !c.server?
  raise ArgumentError, "Your connection options must specify a port"   if !c.port?
  real_block = blk.present? ? proc { |c| blk.call(connection.client) } : nil
  logger.info "Connecting to #{c.server}:#{c.port} (using ssl: #{c.ssl?}) - Channels: #{c.channels.join(", ")}"
  EventMachine.connect(c.server, c.port, EMConnection, c, &real_block)
end

.run(opts = {}, force = false) ⇒ Object

Starts the EventMachine loop and hence starts up the actual networking portion of the IRC Client.



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/marvin/irc/client.rb', line 98

def run(opts = {}, force = false)
  self.development = opts[:development]
  return if @stopped && !force
  self.setup # So we have options etc
  connections_file = Marvin::Settings.root / "config" / "connections.yml"
  connections = Marvin::Nash.load_file(connections_file) rescue nil
  if connections.present?
    # Use epoll if available
    EventMachine.kqueue
    EventMachine.epoll
    EventMachine.run do
      unless connections.data == false
        connections.each_pair do |server, configuration|
          configuration ||= Marvin::Nash.new
          connect(configuration.merge(:server => server.to_s))
        end
      end
      Marvin::Distributed::Server.start if Marvin::Distributed::Handler.registered?
      @@stopped = false
    end
  else
    logger.fatal "config/connections.yml couldn't be loaded."
  end
end

.stopObject



132
133
134
135
136
137
138
139
140
# File 'lib/marvin/irc/client.rb', line 132

def stop
  return if @@stopped
  logger.debug "Telling all connections to quit"
  connections.dup.each { |connection| connection.quit }
  logger.debug "Telling Event Machine to Stop"
  EventMachine.stop_event_loop
  logger.debug "Stopped."
  @@stoped = true
end

Instance Method Details

#periodically(timing, event_callback) ⇒ Object

Registers a callback handle that will be periodically run.



168
169
170
171
172
# File 'lib/marvin/irc/client.rb', line 168

def periodically(timing, event_callback)
  EventMachine.add_periodic_timer(timing) do
    dispatch(event_callback.to_sym)
  end
end

#send_line(*args) ⇒ Object

Client specific details



87
88
89
90
91
92
# File 'lib/marvin/irc/client.rb', line 87

def send_line(*args)
  args.each do |line|
    @em_connection.send_line(line)
    dispatch :outgoing_line, :line => line
  end
end