Class: Safubot::XMPP::Bot
- Inherits:
-
Object
- Object
- Safubot::XMPP::Bot
- Includes:
- Evented
- Defined in:
- lib/safubot/xmpp.rb
Instance Attribute Summary (collapse)
-
- (Object) client
readonly
Returns the value of attribute client.
-
- (Object) jid
readonly
Returns the value of attribute jid.
-
- (Object) pid
readonly
Returns the value of attribute pid.
-
- (Object) state
readonly
Returns the value of attribute state.
Instance Method Summary (collapse)
-
- (Object) init_blather
Sets our Blather::Client event processor running.
-
- (Bot) initialize(opts)
constructor
A new instance of Bot.
-
- (Object) run
Starts our Blather client running in a new process.
-
- (Object) run_blather
Runs the Blather client.
-
- (Object) send(resp)
Dispatch a Response via XMPP.
-
- (Object) stop
Shuts down the Blather client.
- - (Object) tell(jid, text)
Methods included from Evented
#bind, #emit, #on, #once, #unbind
Constructor Details
- (Bot) initialize(opts)
A new instance of Bot
167 168 169 170 171 |
# File 'lib/safubot/xmpp.rb', line 167 def initialize(opts) @jid = opts[:jid] @password = opts[:password] @state = :stopped end |
Instance Attribute Details
- (Object) client (readonly)
Returns the value of attribute client
75 76 77 |
# File 'lib/safubot/xmpp.rb', line 75 def client @client end |
- (Object) jid (readonly)
Returns the value of attribute jid
75 76 77 |
# File 'lib/safubot/xmpp.rb', line 75 def jid @jid end |
- (Object) pid (readonly)
Returns the value of attribute pid
75 76 77 |
# File 'lib/safubot/xmpp.rb', line 75 def pid @pid end |
- (Object) state (readonly)
Returns the value of attribute state
75 76 77 |
# File 'lib/safubot/xmpp.rb', line 75 def state @state end |
Instance Method Details
- (Object) init_blather
Sets our Blather::Client event processor running.
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/safubot/xmpp.rb', line 78 def init_blather @client = Blather::Client.setup(@jid, @password) @client.register_handler(:ready) do Log.info "XMPP client is online at #{@client.jid.stripped} :D" emit(:ready) end @client.register_handler(:subscription, :request?) do |s| Log.info "Approving subscription request from: #{s.from}" @client.write s.approve! end @client.register_handler(:message, :chat?, :body) do |msg| unless msg.body.match(/^\?OTR:/) emit(:request, Message.from(msg).make_request) end end @client.register_handler(:disconnected) do sleep 1 # HACK (Mispy): Give the state a chance to change when we're stopped. if @state == :running Log.warn("XMPP disconnected; attempting reconnection in 5 seconds.") sleep 5; @client.connect end end @client.register_handler(:error) do |e| Log.error "Unhandled Blather error: #{error_report(e)}" end @state = :running end |
- (Object) run
Starts our Blather client running in a new process.
131 132 133 134 135 136 137 |
# File 'lib/safubot/xmpp.rb', line 131 def run @pid = Process.fork do Signal.trap("TERM") { stop } init_blather run_blather end end |
- (Object) run_blather
Runs the Blather client.
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/safubot/xmpp.rb', line 114 def run_blather begin EM::run { @client.run } rescue Exception => e if e.is_a?(Interrupt) || e.is_a?(SignalException) stop elsif @state == :running Log.error "XMPP client exited unexpectedly: #{error_report(e)}" Log.error "Restarting XMPP client in 5 seconds." sleep 5; init_blather; run_blather end end end |
- (Object) send(resp)
Dispatch a Response via XMPP.
159 160 161 162 163 164 165 |
# File 'lib/safubot/xmpp.rb', line 159 def send(resp) if @state == :running tell(resp.request.source.from, resp.text) else on(:ready) { send(resp) } end end |
- (Object) stop
Shuts down the Blather client.
140 141 142 143 144 145 146 147 148 149 |
# File 'lib/safubot/xmpp.rb', line 140 def stop if @client @state = :stopped @client.close @client = nil Log.info "XMPP client shutdown complete." elsif @pid Process.kill("TERM", @pid) end end |
- (Object) tell(jid, text)
151 152 153 154 155 156 |
# File 'lib/safubot/xmpp.rb', line 151 def tell(jid, text) msg = Blather::Stanza::Message.new msg.to = jid msg.body = text @client.write msg end |