rumpy 0.9.10 → 0.9.11
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.
- data/README.rdoc +5 -4
- data/lib/rumpy.rb +74 -21
- metadata +4 -4
data/README.rdoc
CHANGED
@@ -23,14 +23,15 @@ Our goal is 'DO NOT REINVENT THE WHEEL'.
|
|
23
23
|
* Start writing your class:
|
24
24
|
* Mix in Rumpy::Bot module:
|
25
25
|
include Rumpy::Bot
|
26
|
-
* Define
|
26
|
+
* Define some instance variables:
|
27
27
|
@models_path :: Path to directory, containing all ruby files with models.
|
28
28
|
@config_path :: Path to directory, containing all ruby config files
|
29
29
|
@main_model :: Symbol, that stands for main model. If your main model is, e.g. User, set @main_model to :user
|
30
30
|
@pid_file :: Optional variable, sets location of the file to which pid of detached process will be saved. If not set, it equals NameOfYourBotClass.downcase + '.pid'.
|
31
|
-
@
|
32
|
-
@
|
33
|
-
@
|
31
|
+
@log_file :: Optional variable, sets location of the file for errors. Default is STDERR.
|
32
|
+
@log_level :: Optional variable, sets the verbosity of log. Possible values are Logger::DEBUG < Logger::INFO < Logger::WARN < Logger::ERROR < Logger::FATAL < Logger::UNKNOWN. Default is Logger::INFO
|
33
|
+
@bot_name :: Optional name of the bot. Default is name of bot's class.
|
34
|
+
@bot_version :: Optional version of the bot. Default is 1.0.0.
|
34
35
|
* Write 3 methods:
|
35
36
|
backend_func() -> [[receiver, message]*] :: This optional method is running all the time in the loop. Returns array of pairs [receiver, message].
|
36
37
|
parser_func(msg) -> pars_result :: This method parses any incoming message and returs results.
|
data/lib/rumpy.rb
CHANGED
@@ -4,6 +4,7 @@ require 'xmpp4r/roster'
|
|
4
4
|
require 'xmpp4r/version'
|
5
5
|
require 'active_record'
|
6
6
|
require 'active_record/validations'
|
7
|
+
require 'logger'
|
7
8
|
|
8
9
|
module Rumpy
|
9
10
|
|
@@ -48,23 +49,29 @@ module Rumpy
|
|
48
49
|
attr_reader :pid_file
|
49
50
|
|
50
51
|
def start
|
51
|
-
@
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
52
|
+
@log_file ||= STDERR
|
53
|
+
@log_level ||= Logger::INFO
|
54
|
+
@logger = Logger.new @log_file
|
55
|
+
@logger.level = @log_level
|
56
|
+
@logger.datetime_format = "%Y-%m-%d %H:%M:%S"
|
56
57
|
Signal.trap :TERM do |signo|
|
57
|
-
@
|
58
|
+
@logger.info 'terminating'
|
59
|
+
@logger.close
|
58
60
|
exit
|
59
61
|
end
|
60
62
|
|
63
|
+
@logger.info 'starting bot'
|
64
|
+
@logger.debug 'initializing some variables'
|
61
65
|
init
|
66
|
+
@logger.debug 'establishing xmpp connection'
|
62
67
|
connect
|
68
|
+
@logger.debug 'clear wrong users'
|
63
69
|
clear_users
|
64
70
|
start_subscription_callback
|
65
71
|
start_message_callback
|
72
|
+
@logger.info 'Bot is going ONLINE'
|
66
73
|
@client.send Jabber::Presence.new.set_priority(@priority).set_status(@status)
|
67
|
-
|
74
|
+
|
68
75
|
Thread.new do
|
69
76
|
begin
|
70
77
|
loop do
|
@@ -72,9 +79,14 @@ module Rumpy
|
|
72
79
|
send_msg *result
|
73
80
|
end
|
74
81
|
end
|
82
|
+
rescue ActiveRecord::StatementInvalid
|
83
|
+
@logger.warn 'Statement Invalid catched'
|
84
|
+
@logger.info 'Reconnecting to database'
|
85
|
+
@main_model.connection.reconnect!
|
86
|
+
retry
|
75
87
|
rescue => e
|
76
|
-
$
|
77
|
-
$
|
88
|
+
$logger.error e.inspect
|
89
|
+
$logger.error e.backtrace
|
78
90
|
end
|
79
91
|
end if self.respond_to? :backend_func
|
80
92
|
Thread.stop
|
@@ -85,22 +97,32 @@ module Rumpy
|
|
85
97
|
def init
|
86
98
|
|
87
99
|
xmppconfig = YAML::load_file @config_path + '/xmpp.yml'
|
100
|
+
@logger.info 'loaded xmpp.yml'
|
101
|
+
@logger.debug "xmpp.yml: #{xmppconfig.inspect}"
|
88
102
|
@lang = YAML::load_file @config_path + '/lang.yml'
|
103
|
+
@logger.info 'loaded lang.yml'
|
104
|
+
@logger.debug "lang.yml: #{@lang.inspect}"
|
89
105
|
@jid = Jabber::JID.new xmppconfig['jid']
|
90
106
|
@priority = xmppconfig['priority']
|
91
107
|
@status = xmppconfig['status']
|
92
108
|
@password = xmppconfig['password']
|
93
109
|
@client = Jabber::Client.new @jid
|
110
|
+
Jabber::Version::SimpleResponder.new(@client, @bot_name || self.class.to_s, @bot_version || '1.0.0', RUBY_PLATFORM)
|
94
111
|
|
95
112
|
if @models_path then
|
96
113
|
dbconfig = YAML::load_file @config_path + '/database.yml'
|
114
|
+
@logger.info 'loaded database.yml'
|
115
|
+
@logger.debug "database.yml: #{dbconfig.inspect}"
|
97
116
|
ActiveRecord::Base.establish_connection dbconfig
|
117
|
+
@logger.info 'database connection established'
|
98
118
|
Dir[@models_path].each do |file|
|
99
119
|
self.class.require file
|
120
|
+
@logger.info "added models file '#{file}'"
|
100
121
|
end
|
101
122
|
end
|
102
123
|
|
103
124
|
@main_model = Object.const_get @main_model.to_s.capitalize
|
125
|
+
@logger.info "main model set to #{@main_model}"
|
104
126
|
def @main_model.find_by_jid(jid)
|
105
127
|
super jid.strip.to_s
|
106
128
|
end
|
@@ -115,19 +137,25 @@ module Rumpy
|
|
115
137
|
@client.auth @password
|
116
138
|
@roster = Jabber::Roster::Helper.new @client
|
117
139
|
@roster.wait_for_roster
|
140
|
+
@logger.info 'xmpp connection established'
|
118
141
|
end
|
119
142
|
|
120
143
|
def clear_users
|
121
144
|
@main_model.find_each do |user|
|
122
145
|
items = @roster.find user.jid
|
123
|
-
|
146
|
+
if items.count != 1 then
|
147
|
+
@logger.info "deleting from database user with jid #{user.jid}"
|
148
|
+
user.destroy
|
149
|
+
end
|
124
150
|
end
|
125
151
|
@roster.items.each do |jid, item|
|
126
152
|
user = @main_model.find_by_jid jid
|
127
153
|
if user.nil? then
|
154
|
+
@logger.info "deleting from roster user with jid #{jid}"
|
128
155
|
item.remove
|
129
156
|
next
|
130
157
|
elsif item.subscription != :both then
|
158
|
+
@logger.info "deleting from roster&database user with jid #{jid}"
|
131
159
|
item.remove
|
132
160
|
user.destroy
|
133
161
|
end
|
@@ -140,15 +168,24 @@ module Rumpy
|
|
140
168
|
@roster.accept_subscription jid
|
141
169
|
@client.send Jabber::Presence.new.set_type(:subscribe).set_to(jid)
|
142
170
|
send_msg jid, @lang['hello']
|
171
|
+
@logger.info "#{jid} wanna subscribe"
|
143
172
|
end
|
144
173
|
@roster.add_subscription_callback do |item, presence|
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
174
|
+
begin
|
175
|
+
case presence.type
|
176
|
+
when :unsubscribed, :unsubscribe
|
177
|
+
@logger.info "#{item.jid} wanna unsubscribe"
|
178
|
+
item.remove
|
179
|
+
remove_jid item.jid
|
180
|
+
when :subscribed
|
181
|
+
add_jid item.jid
|
182
|
+
send_msg item.jid, @lang['authorized']
|
183
|
+
end
|
184
|
+
rescue ActiveRecord::StatementInvalid
|
185
|
+
@logger.warn 'Statement Invalid catched'
|
186
|
+
@logger.info 'Reconnecting to database'
|
187
|
+
@main_model.connection.reconnect!
|
188
|
+
retry
|
152
189
|
end
|
153
190
|
end
|
154
191
|
end
|
@@ -158,7 +195,9 @@ module Rumpy
|
|
158
195
|
begin
|
159
196
|
if msg.type != :error and msg.body and msg.from then
|
160
197
|
if user = @main_model.find_by_jid(msg.from) then
|
198
|
+
@logger.debug "get normal message from #{msg.from}"
|
161
199
|
pars_results = parser_func msg.body
|
200
|
+
@logger.debug "parsed message: #{pars_results.inspect}"
|
162
201
|
|
163
202
|
message = ""
|
164
203
|
@mutexes[user.jid].synchronize do
|
@@ -166,21 +205,29 @@ module Rumpy
|
|
166
205
|
end
|
167
206
|
send_msg msg.from, message
|
168
207
|
else
|
208
|
+
@logger.debug "uknown user #{msg.from}"
|
169
209
|
send_msg msg.from, @lang['stranger']
|
170
210
|
items = @roster.find msg.from.strip.to_s
|
171
211
|
items.first.last.remove unless items.empty?
|
172
212
|
end
|
173
213
|
end
|
214
|
+
rescue ActiveRecord::StatementInvalid
|
215
|
+
@logger.warn 'Statement Invalid catched!'
|
216
|
+
@logger.info 'Reconnecting to database'
|
217
|
+
@main_model.connection.reconnect!
|
218
|
+
retry
|
174
219
|
rescue => e
|
175
|
-
@
|
176
|
-
@
|
220
|
+
@logger.error e.inspect
|
221
|
+
@logger.error e.backtrace
|
177
222
|
end
|
178
223
|
end
|
179
224
|
@client.add_iq_callback do |iq|
|
180
|
-
|
225
|
+
@logger.debug "got iq #{iq.inspect}"
|
226
|
+
if iq.type == :get then # hack for pidgin (STOP USING IT)
|
181
227
|
response = Jabber::Iq.new :error, iq.from
|
182
228
|
response.id = iq.id
|
183
229
|
if iq.elements["time"] == "<time xmlns='urn:xmpp:time'/>" then
|
230
|
+
@logger.debug 'this is time request, okay'
|
184
231
|
response.set_type :result
|
185
232
|
response.root.add REXML::Element.new('time')
|
186
233
|
response.elements['time'].attributes['xmlns'] = 'urn:xmpp:time'
|
@@ -190,6 +237,7 @@ module Rumpy
|
|
190
237
|
response.elements['time'].add REXML::Element.new('utc')
|
191
238
|
response.elements['time/utc'].text = tm.utc.xmlschema
|
192
239
|
end
|
240
|
+
@logger.debug "sending response: #{response}"
|
193
241
|
@client.send response
|
194
242
|
end
|
195
243
|
end
|
@@ -199,6 +247,7 @@ module Rumpy
|
|
199
247
|
return if destination.nil? or text.nil?
|
200
248
|
msg = Jabber::Message.new destination, text
|
201
249
|
msg.type = :chat
|
250
|
+
@logger.debug "sending message: #{msg}"
|
202
251
|
@client.send msg
|
203
252
|
end
|
204
253
|
|
@@ -206,11 +255,15 @@ module Rumpy
|
|
206
255
|
user = @main_model.new
|
207
256
|
user.jid = jid.strip.to_s
|
208
257
|
user.save
|
258
|
+
@logger.info "added new user: #{jid}"
|
209
259
|
end
|
210
260
|
|
211
261
|
def remove_jid(jid)
|
212
262
|
user = @main_model.find_by_jid jid
|
213
|
-
|
263
|
+
unless user.nil?
|
264
|
+
@logger.info "removing user #{jid}"
|
265
|
+
user.destroy
|
266
|
+
end
|
214
267
|
end
|
215
268
|
end
|
216
269
|
end
|
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:
|
4
|
+
hash: 45
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 9
|
9
|
-
-
|
10
|
-
version: 0.9.
|
9
|
+
- 11
|
10
|
+
version: 0.9.11
|
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-07-
|
19
|
+
date: 2011-07-20 00:00:00 +03:00
|
20
20
|
default_executable:
|
21
21
|
dependencies:
|
22
22
|
- !ruby/object:Gem::Dependency
|