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

#initialize(opts) ⇒ Bot

Returns a new instance of Bot.



171
172
173
174
175
# File 'lib/safubot/xmpp.rb', line 171

def initialize(opts)
		@jid = opts[:jid]
		@password = opts[:password]
		@state = :stopped
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



75
76
77
# File 'lib/safubot/xmpp.rb', line 75

def client
  @client
end

#jidObject (readonly)

Returns the value of attribute jid.



75
76
77
# File 'lib/safubot/xmpp.rb', line 75

def jid
  @jid
end

#pidObject (readonly)

Returns the value of attribute pid.



75
76
77
# File 'lib/safubot/xmpp.rb', line 75

def pid
  @pid
end

#stateObject (readonly)

Returns the value of attribute state.



75
76
77
# File 'lib/safubot/xmpp.rb', line 75

def state
  @state
end

Instance Method Details

#forkObject

Starts our Blather client running in a new process.



136
137
138
139
140
141
# File 'lib/safubot/xmpp.rb', line 136

def fork
		@pid = Process.fork do
 Signal.trap("TERM") { stop }
 run
		end
end

#init_blatherObject

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
# 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 2 # 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

#runObject

Starts our Blather client running.



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

def run
		init_blather
		run_blather
end

#run_blatherObject

Runs the Blather client.



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/safubot/xmpp.rb', line 113

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

#send(resp) ⇒ Object

Dispatch a Response via XMPP.



163
164
165
166
167
168
169
# File 'lib/safubot/xmpp.rb', line 163

def send(resp)
		if @state == :running
 tell(resp.request.source.from, resp.text)
		else
 on(:ready) { send(resp) }
		end
end

#stopObject

Shuts down the Blather client.



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

def stop
		if @client
 @state = :stopped
 @client.close
 @client = nil
 Log.info "XMPP client shutdown complete."
		elsif @pid
 Process.kill("KILL", @pid)
		end
end

#tell(jid, text) ⇒ Object



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

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