rumpy 0.9.14 → 0.9.15

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. data/lib/rumpy.rb +122 -100
  2. metadata +4 -4
data/lib/rumpy.rb CHANGED
@@ -21,7 +21,7 @@ module Rumpy
21
21
  file.puts pid
22
22
  end
23
23
  true
24
- end
24
+ end # def self.start(botclass)
25
25
 
26
26
  # Determine the name of pid_file, read pid from this file
27
27
  # and try to kill process with this pid
@@ -36,7 +36,7 @@ module Rumpy
36
36
  File.unlink pf
37
37
  end
38
38
  true
39
- end
39
+ end # def self.stop(botclass)
40
40
 
41
41
  # Create new instance of `botclass` and start it without detaching
42
42
  def self.run(botclass)
@@ -72,12 +72,9 @@ module Rumpy
72
72
  end
73
73
 
74
74
  @logger.info 'starting bot'
75
- @logger.debug 'initializing some variables'
76
75
  init
77
- @logger.debug 'establishing xmpp connection'
78
76
  connect
79
- @logger.debug 'clear wrong users'
80
- clear_users
77
+ prepare_users
81
78
 
82
79
  set_subscription_callback
83
80
  set_message_callback
@@ -89,31 +86,12 @@ module Rumpy
89
86
  @client.send Jabber::Presence.new.set_priority(@priority).set_status(@status)
90
87
 
91
88
  Thread.stop
92
- end
89
+ end # def start
93
90
 
94
91
  private
95
92
 
96
- def start_backend_thread
97
- Thread.new do
98
- begin
99
- loop do
100
- backend_func().each do |result|
101
- send_msg *result
102
- end
103
- end
104
- rescue ActiveRecord::StatementInvalid
105
- @logger.warn 'Statement Invalid catched'
106
- @logger.info 'Reconnecting to database'
107
- reconnect_db!
108
- retry
109
- rescue => e
110
- $logger.error e.inspect
111
- $logger.error e.backtrace
112
- end
113
- end if self.respond_to? :backend_func
114
- end
115
-
116
93
  def init
94
+ @logger.debug 'initializing some variables'
117
95
 
118
96
  xmppconfig = YAML::load_file @config_path + '/xmpp.yml'
119
97
  @logger.info 'loaded xmpp.yml'
@@ -145,131 +123,171 @@ module Rumpy
145
123
  def @main_model.find_by_jid(jid)
146
124
  super jid.strip.to_s
147
125
  end
148
- end
126
+
127
+ @queues = Hash.new do |h, k|
128
+ h[k] = Queue.new
129
+ end
130
+ end # def init
149
131
 
150
132
  def connect
133
+ @logger.debug 'establishing xmpp connection'
134
+
151
135
  @client.connect
152
136
  @client.auth @password
153
137
  @roster = Jabber::Roster::Helper.new @client
154
138
  @roster.wait_for_roster
139
+
155
140
  @logger.info 'xmpp connection established'
156
141
  end
157
142
 
158
- def clear_users
159
- @main_model.find_each do |user|
160
- items = @roster.find user.jid
161
- if items.count.zero? then
162
- @logger.info "deleting from database user with jid #{user.jid}"
163
- user.destroy
164
- end
165
- end
143
+ def prepare_users
144
+ @logger.debug 'clear wrong users'
145
+
166
146
  @roster.items.each do |jid, item|
167
- user = find_user_by_jid jid
168
- if user.nil? then
147
+ user = @main_model.find_by_jid jid
148
+ if user.nil? or item.subscription != :both then
169
149
  @logger.info "deleting from roster user with jid #{jid}"
170
150
  item.remove
171
- elsif item.subscription != :both then
172
- @logger.info "deleting from roster&database user with jid #{jid}"
173
- item.remove
151
+ end
152
+ end
153
+ @main_model.find_each do |user|
154
+ items = @roster.find user.jid
155
+ if items.empty? then
156
+ @logger.info "deleting from database user with jid #{user.jid}"
174
157
  user.destroy
158
+ else
159
+ start_user_thread user.jid
175
160
  end
176
161
  end
162
+
177
163
  @main_model.connection_pool.release_connection
178
- end
164
+ end # def prepare_users
179
165
 
180
166
  def set_subscription_callback
181
167
  @roster.add_subscription_request_callback do |item, presence|
182
168
  jid = presence.from
183
169
  @roster.accept_subscription jid
184
- @client.send Jabber::Presence.new.set_type(:subscribe).set_to(jid)
185
- send_msg jid, @lang['hello']
186
- @logger.info "#{jid} wanna subscribe"
170
+ send_msg presence.answer.set_type :subscribe
171
+ send_msg Jabber::Message.new(jid, @lang['hello']).set_type :chat
172
+
173
+ @logger.info "#{jid} just subscribed"
187
174
  end
188
175
  @roster.add_subscription_callback do |item, presence|
189
176
  begin
190
177
  case presence.type
191
178
  when :unsubscribed, :unsubscribe
192
179
  @logger.info "#{item.jid} wanna unsubscribe"
180
+ @queues[item.jid.strip.to_s].enq :unsubscribe
193
181
  item.remove
194
- remove_jid item.jid
195
182
  when :subscribed
196
183
  add_jid item.jid
197
- send_msg item.jid, @lang['authorized']
184
+ send_msg Jabber::Message.new(item.jid, @lang['authorized']).set_type :chat
198
185
  end
199
186
  rescue ActiveRecord::StatementInvalid
200
187
  @logger.warn 'Statement Invalid catched'
201
188
  @logger.info 'Reconnecting to database'
202
- reconnect_db!
189
+ @main_model.connection.reconnect!
203
190
  retry
204
191
  end
205
192
  end
206
- end
207
-
208
- def find_user_by_jid(jid)
209
- @main_model.find_by_jid jid
210
- end
211
-
212
- def reconnect_db!
213
- @main_model.connection.reconnect!
214
- end
193
+ end # def set_subscription_callback
215
194
 
216
195
  def set_message_callback
217
196
  @client.add_message_callback do |msg|
218
- begin
219
- if msg.type != :error and msg.body and msg.from then
220
- if user = find_user_by_jid(msg.from) then
221
- @logger.debug "get normal message from #{msg.from}"
222
- pars_results = parser_func msg.body
223
- @logger.debug "parsed message: #{pars_results.inspect}"
224
-
225
- message = do_func user, pars_results
226
- send_msg msg.from, message
227
- else
228
- @logger.debug "uknown user #{msg.from}"
229
- send_msg msg.from, @lang['stranger']
230
- items = @roster.find msg.from.strip.to_s
231
- items.first.last.remove unless items.empty?
232
- end
233
- end
234
- rescue ActiveRecord::StatementInvalid
235
- @logger.warn 'Statement Invalid catched!'
236
- @logger.info 'Reconnecting to database'
237
- reconnect_db!
238
- retry
239
- rescue => e
240
- @logger.error e.inspect
241
- @logger.error e.backtrace
242
- end
243
- end
244
- end
197
+ if msg.type != :error and msg.body and msg.from then
198
+ if @roster[msg.from] and @roster[msg.from].subscription == :both then
199
+ @logger.debug "got normal message from #{msg.from}"
200
+
201
+ @queues[msg.from.strip.to_s].enq msg
202
+ else # if @roster[msg.from] and @roster[msg.from].subscription == :both
203
+ @logger.debug "user not in roster: #{msg.from}"
204
+
205
+ send_msg msg.answer.set_body @lang['stranger']
206
+ end # if @roster[msg.from] and @roster[msg.from].subscription == :both
207
+ end # if msg.type != :error and msg.body and msg.from
208
+ end # @client.add_message_callback
209
+ end # def set_message_callback
245
210
 
246
211
  def set_iq_callback
247
212
  @client.add_iq_callback do |iq|
248
- @logger.debug "got iq #{iq.inspect}"
213
+ @logger.debug "got iq #{iq}"
249
214
  if iq.type == :get then # hack for pidgin (STOP USING IT)
250
- response = Jabber::Iq.new :error, iq.from
251
- response.id = iq.id
252
- if iq.elements["time"] == "<time xmlns='urn:xmpp:time'/>" then
215
+ response = iq.answer true
216
+ if iq.elements['time'] == "<time xmlns='urn:xmpp:time'/>" then
253
217
  @logger.debug 'this is time request, okay'
254
218
  response.set_type :result
255
- response.root.add REXML::Element.new('time')
256
- response.elements['time'].attributes['xmlns'] = 'urn:xmpp:time'
257
219
  tm = Time.now
258
220
  response.elements['time'].add REXML::Element.new('tzo')
259
221
  response.elements['time/tzo'].text = tm.xmlschema[-6..-1]
260
222
  response.elements['time'].add REXML::Element.new('utc')
261
223
  response.elements['time/utc'].text = tm.utc.xmlschema
262
- end
263
- @logger.debug "sending response: #{response}"
264
- @client.send response
224
+ else
225
+ response.set_type :error
226
+ end # if iq.elements['time']
227
+ send_msg response
265
228
  end
266
229
  end
267
- end
230
+ end # def set_iq_callback
268
231
 
269
- def send_msg(destination, text)
270
- return if destination.nil? or text.nil?
271
- msg = Jabber::Message.new destination, text
272
- msg.type = :chat
232
+ def start_backend_thread
233
+ Thread.new do
234
+ begin
235
+ loop do
236
+ backend_func().each do |result|
237
+ send_msg Jabber::Message.new(*result).set_type :chat
238
+ end
239
+ end
240
+ rescue ActiveRecord::StatementInvalid
241
+ @logger.warn 'Statement Invalid catched'
242
+ @logger.info 'Reconnecting to database'
243
+ @main_model.connection.reconnect!
244
+ retry
245
+ rescue => e
246
+ $logger.error e.inspect
247
+ $logger.error e.backtrace
248
+ end # begin
249
+ end if self.respond_to? :backend_func
250
+ end # def start_backend_thread
251
+
252
+ def start_user_thread(userjid)
253
+ Thread.new(userjid) do |userjid|
254
+ loop do
255
+ msg = @queues[userjid].deq
256
+
257
+ begin
258
+ if msg == :unsubscribe then
259
+ remove_jid userjid
260
+ break
261
+ end
262
+
263
+ user = @main_model.find_by_jid msg.from
264
+
265
+ pars_results = parser_func msg.body
266
+ @logger.debug "parsed message: #{pars_results.inspect}"
267
+ send_msg msg.answer.set_body do_func(user, pars_results)
268
+ rescue ActiveRecord::StatementInvalid
269
+ @logger.warn 'Statement Invalid catched!'
270
+ @logger.info 'Reconnecting to database'
271
+ @main_model.connection.reconnect!
272
+ retry
273
+ rescue ActiveRecord::ConnectionTimeoutError
274
+ @logger.warn 'ActiveRecord::ConnectionTimeoutError'
275
+ @logger.info 'sleep and retry again'
276
+ sleep 3
277
+ retry
278
+ rescue => e
279
+ @logger.error e.inspect
280
+ @logger.error e.backtrace
281
+ end # begin
282
+
283
+ @main_model.connection_pool.release_connection
284
+ end # loop do
285
+ Thread.current.join
286
+ end # Thread.new do
287
+ end # def start_user_thread(queue)
288
+
289
+ def send_msg(msg)
290
+ return if msg.nil?
273
291
  @logger.debug "sending message: #{msg}"
274
292
  @client.send msg
275
293
  end
@@ -278,6 +296,7 @@ module Rumpy
278
296
  user = @main_model.new
279
297
  user.jid = jid.strip.to_s
280
298
  user.save
299
+ start_user_thread user.jid
281
300
  @logger.info "added new user: #{jid}"
282
301
  end
283
302
 
@@ -285,8 +304,11 @@ module Rumpy
285
304
  user = @main_model.find_by_jid jid
286
305
  unless user.nil?
287
306
  @logger.info "removing user #{jid}"
307
+
308
+ @queues.delete user.jid
309
+
288
310
  user.destroy
289
311
  end
290
312
  end
291
- end
292
- end
313
+ end # module Rumpy::Bot
314
+ end # module Rumpy
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rumpy
3
3
  version: !ruby/object:Gem::Version
4
- hash: 39
4
+ hash: 37
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 9
9
- - 14
10
- version: 0.9.14
9
+ - 15
10
+ version: 0.9.15
11
11
  platform: ruby
12
12
  authors:
13
13
  - Tsokurov A.G.
@@ -16,7 +16,7 @@ autorequire:
16
16
  bindir: bin
17
17
  cert_chain: []
18
18
 
19
- date: 2011-08-14 00:00:00 +03:00
19
+ date: 2011-08-15 00:00:00 +03:00
20
20
  default_executable:
21
21
  dependencies:
22
22
  - !ruby/object:Gem::Dependency