Class: IrcMachine::Session

Inherits:
Object
  • Object
show all
Includes:
Commands
Defined in:
lib/irc_machine/session.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Commands

#join, #msg, #nick, #part, #password, #quit, #raw, #user

Constructor Details

#initialize(options) ⇒ Session

Returns a new instance of Session.



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/irc_machine/session.rb', line 9

def initialize(options)
  IrcMachine::Plugin::Reloader.load_all

  @options = OpenStruct.new(options)
  @state = State.new
  @router = HttpRouter.new(self)
  @plugins = [
    Core.new(self),
    Plugin::Hello.new(self),
    Plugin::Reloader.new(self),
    Plugin::GithubNotifier.new(self)
  ]
end

Instance Attribute Details

#irc_connectionObject

Returns the value of attribute irc_connection.



7
8
9
# File 'lib/irc_machine/session.rb', line 7

def irc_connection
  @irc_connection
end

#optionsObject (readonly)

Returns the value of attribute options.



5
6
7
# File 'lib/irc_machine/session.rb', line 5

def options
  @options
end

#stateObject (readonly)

Returns the value of attribute state.



6
7
8
# File 'lib/irc_machine/session.rb', line 6

def state
  @state
end

Instance Method Details

#disconnectedObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/irc_machine/session.rb', line 52

def disconnected
  if @shutdown
    log "Stopping EventMachine"
    EM.stop
  else
    log "Waiting to reconnect"
    EM.add_timer(2) do
      log "Reconnecting to #{options.server}:#{options.port}"
      irc_connection.reconnect options.server, options.port
      @state.reset
      dispatch :connected
    end
  end
end

#log(message) ⇒ Object



71
72
73
# File 'lib/irc_machine/session.rb', line 71

def log message
  puts "! " << message if options.verbose
end

#receive_line(line) ⇒ Object



67
68
69
# File 'lib/irc_machine/session.rb', line 67

def receive_line(line)
  dispatch :receive_line, line
end

#startObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/irc_machine/session.rb', line 23

def start
  EM.run do

    signal_traps

    log "Connecting to #{options.server}:#{options.port}"
    EM.connect(
      options.server,
      options.port,
      IrcConnection,
      {:ssl => @options.ssl, :session => self}
    ) do |c|
      self.irc_connection = c
    end

    log "Starting HTTP API on port #{options.http_port}"
    EM.start_server "0.0.0.0", options.http_port, HttpServer do |c|
      c.router = @router
    end

    log "Starting UDP API on port #{options.udp_port}"
    EM.open_datagram_socket "0.0.0.0", options.udp_port, UdpServer do |c|
      c.session = self
    end

    dispatch :connected
  end
end