Class: Safubot::XMPP::Bot

Inherits:
Object
  • Object
show all
Includes:
Evented
Defined in:
lib/safubot/xmpp.rb

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Methods included from Evented

#bind, #emit, #on, #once, #unbind

Constructor Details

- (Bot) initialize(opts)

A new instance of Bot



159
160
161
162
163
# File 'lib/safubot/xmpp.rb', line 159

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.



129
130
131
132
133
134
135
# File 'lib/safubot/xmpp.rb', line 129

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
# File 'lib/safubot/xmpp.rb', line 114

def run_blather
		begin
 EM::run { @client.run }
		rescue Exception => e
 unless e.is_a?(Interrupt) || e.is_a?(SignalException)
Log.error "XMPP client exited unexpectedly: #{error_report(e)}"
Log.error "Restarting XMPP client in 5 seconds."
sleep 5; init_blather; run_blather
 end
		else
 Log.info "XMPP client shutdown complete."
		end
end

- (Object) send(resp)

Dispatch a Response via XMPP.



155
156
157
# File 'lib/safubot/xmpp.rb', line 155

def send(resp)
		tell(resp.request.source.from, resp.text)
end

- (Object) stop

Shuts down the Blather client.



138
139
140
141
142
143
144
145
# File 'lib/safubot/xmpp.rb', line 138

def stop
		if @client
 @state = :stopped
 @client.close
		elsif @pid
 Process.kill("TERM", @pid)
		end
end

- (Object) tell(jid, text)



147
148
149
150
151
152
# File 'lib/safubot/xmpp.rb', line 147

def tell(jid, text)
		msg = Blather::Stanza::Message.new
		msg.to = jid
		msg.body = text
		@client.write msg
end