jabber-bot 1.0.1 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/HISTORY +8 -0
- data/LICENSE +1 -1
- data/README +12 -10
- data/lib/jabber/bot.rb +268 -171
- metadata +2 -2
data/HISTORY
CHANGED
@@ -1,5 +1,13 @@
|
|
1
1
|
Jabber::Bot Release History
|
2
2
|
|
3
|
+
Version 1.1.0 (24 March 2007)
|
4
|
+
* Supports Jabber presence, status message and priority.
|
5
|
+
* Built-in 'help' command now accepts an optional <command> argument.
|
6
|
+
* New master?(jabber_id) method.
|
7
|
+
* jabber method now a read-only attribute.
|
8
|
+
* More command parsing regex cleanup.
|
9
|
+
* RDoc cleanup.
|
10
|
+
|
3
11
|
Version 1.0.1 (21 March 2007)
|
4
12
|
* Fix debug puts remnant in parse_command.
|
5
13
|
* Fix message.sub regex during command callback in parse_command.
|
data/LICENSE
CHANGED
data/README
CHANGED
@@ -2,13 +2,13 @@
|
|
2
2
|
|
3
3
|
Easily create powerful Jabber bots to do your bidding.
|
4
4
|
|
5
|
-
Jabber::Bot makes it simple to create and command your own Jabber bot with
|
6
|
-
little fuss. By adding custom commands powered by regular expressions to your
|
7
|
-
bot's repertoire, you and your new bot will be able to accomplish nearly
|
5
|
+
Jabber::Bot makes it simple to create and command your own Jabber bot with
|
6
|
+
little fuss. By adding custom commands powered by regular expressions to your
|
7
|
+
bot's repertoire, you and your new bot will be able to accomplish nearly
|
8
8
|
anything.
|
9
9
|
|
10
10
|
Author:: Brett Stimmerman (mailto:brettstimmerman@gmail.com)
|
11
|
-
Version:: 1.0
|
11
|
+
Version:: 1.1.0
|
12
12
|
Copyright:: Copyright (c) 2007 Brett Stimmerman. All rights reserved.
|
13
13
|
License:: New BSD License (http://opensource.org/licenses/bsd-license.php)
|
14
14
|
Website:: http://socket7.net/software/jabber-bot
|
@@ -21,14 +21,16 @@ Website:: http://socket7.net/software/jabber-bot
|
|
21
21
|
== Basic Usage
|
22
22
|
|
23
23
|
# Create a public Jabber::Bot to do your bidding
|
24
|
-
|
24
|
+
config = {
|
25
|
+
:name => 'SampleBot'
|
25
26
|
:jabber_id => 'bot@example.com',
|
26
27
|
:password => 'password',
|
27
28
|
:master => 'master@example.com',
|
28
29
|
:is_public => true
|
29
30
|
}
|
30
|
-
|
31
|
-
|
31
|
+
|
32
|
+
bot = Jabber::Bot.new(config)
|
33
|
+
|
32
34
|
# Give your bot a private command, 'rand'
|
33
35
|
bot.add_command(
|
34
36
|
:syntax => 'rand',
|
@@ -41,10 +43,10 @@ Website:: http://socket7.net/software/jabber-bot
|
|
41
43
|
:syntax => 'puts <string>',
|
42
44
|
:description => 'Write something to $stdout',
|
43
45
|
:regex => /^puts\s+.+$/,
|
44
|
-
:
|
46
|
+
:alias => [ :alias => 'p <string>', :regex => /^p\s+.+$/ ],
|
45
47
|
:is_public => true
|
46
|
-
) do |message|
|
47
|
-
puts message
|
48
|
+
) do |sender, message|
|
49
|
+
puts "#{sender} says '#{message'"
|
48
50
|
"'#{message}' written to $stdout"
|
49
51
|
end
|
50
52
|
|
data/lib/jabber/bot.rb
CHANGED
@@ -30,7 +30,7 @@ require 'rubygems'
|
|
30
30
|
require 'xmpp4r-simple'
|
31
31
|
|
32
32
|
module Jabber
|
33
|
-
|
33
|
+
|
34
34
|
# = Jabber::Bot
|
35
35
|
#
|
36
36
|
# Jabber::Bot makes it simple to create and command a Jabber bot with little
|
@@ -38,123 +38,134 @@ module Jabber
|
|
38
38
|
# repertoire, you and your new bot will be able to accomplish nearly anything.
|
39
39
|
#
|
40
40
|
# Author:: Brett Stimmerman (mailto:brettstimmerman@gmail.com)
|
41
|
-
# Version:: 1.0
|
41
|
+
# Version:: 1.1.0
|
42
42
|
# Copyright:: Copyright (c) 2007 Brett Stimmerman. All rights reserved.
|
43
43
|
# License:: New BSD License (http://opensource.org/licenses/bsd-license.php)
|
44
44
|
# Website:: http://socket7.net/software/jabber-bot
|
45
45
|
#
|
46
46
|
class Bot
|
47
|
-
|
48
|
-
#
|
49
|
-
#
|
50
|
-
|
51
|
-
|
52
|
-
#
|
47
|
+
|
48
|
+
# Direct access to the underlying
|
49
|
+
# Jabber::Simple[http://xmpp4r-simple.rubyforge.org/] object.
|
50
|
+
attr_reader :jabber
|
51
|
+
|
52
|
+
# Creates a new Jabber::Bot object with the specified +config+ Hash, which
|
53
|
+
# must contain +jabber_id+, +password+, and +master+ at a minimum.
|
54
|
+
#
|
55
|
+
# You may optionally give your bot a custom +name+. If +name+ is omitted,
|
56
|
+
# the username portion of +jabber_id+ is used instead.
|
57
|
+
#
|
58
|
+
# You may choose to restrict a Jabber::Bot to listen only to its master(s),
|
59
|
+
# or make it +public+.
|
53
60
|
#
|
54
|
-
#
|
55
|
-
#
|
61
|
+
# You may optionally specify a Jabber +presence+, +status+, and +priority+.
|
62
|
+
# If omitted, they each default to +nil+.
|
56
63
|
#
|
57
|
-
#
|
58
|
-
#
|
59
|
-
#
|
64
|
+
# By default, a Jabber::Bot has only a single command, 'help [<command>]',
|
65
|
+
# which displays a help message for the specified command, or all commands
|
66
|
+
# if <command> is omitted.
|
60
67
|
#
|
61
|
-
#
|
68
|
+
# If you choose to make a public bot, only the commands you specify as
|
69
|
+
# public, as well as the default 'help' command, will be public.
|
70
|
+
#
|
71
|
+
# # A minimally confiugured private bot with a single master.
|
62
72
|
# bot = Jabber::Bot.new(
|
63
|
-
#
|
64
|
-
#
|
65
|
-
#
|
66
|
-
# :master => 'master@example.com'
|
73
|
+
# :jabber_id => 'bot@example.com',
|
74
|
+
# :password => 'password',
|
75
|
+
# :master => 'master@example.com'
|
67
76
|
# )
|
68
77
|
#
|
69
|
-
# # A public bot with mutliple masters
|
70
|
-
#
|
78
|
+
# # A highly configured public bot with a custom name, mutliple masters,
|
79
|
+
# # Jabber presence, status, and priority.
|
80
|
+
# masters = ['master1@example.com', 'master2@example.com']
|
81
|
+
#
|
71
82
|
# bot = Jabber::Bot.new(
|
72
|
-
#
|
73
|
-
#
|
74
|
-
#
|
75
|
-
#
|
76
|
-
#
|
83
|
+
# :name => 'PublicBot',
|
84
|
+
# :jabber_id => 'bot@example.com',
|
85
|
+
# :password => 'password',
|
86
|
+
# :master => masters,
|
87
|
+
# :is_public => true,
|
88
|
+
# :presence => :chat,
|
89
|
+
# :priority => 5,
|
90
|
+
# :status => 'Hello, I am PublicBot.'
|
77
91
|
# )
|
78
92
|
#
|
79
|
-
def initialize(
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
elsif bot_config[:password].nil?
|
84
|
-
abort 'You must specify a :password'
|
85
|
-
elsif bot_config[:master].nil? or bot_config[:master].length == 0
|
86
|
-
abort 'You must specify at least one :master'
|
87
|
-
end
|
88
|
-
|
89
|
-
@bot_config = bot_config
|
90
|
-
|
91
|
-
@bot_config[:is_public] = false if @bot_config[:is_public].nil?
|
93
|
+
def initialize(config)
|
94
|
+
@config = config
|
95
|
+
|
96
|
+
@config[:is_public] ||= false
|
92
97
|
|
93
|
-
if @
|
94
|
-
@
|
98
|
+
if @config[:name].nil? or @config[:name].length == 0
|
99
|
+
@config[:name] = @config[:jabber_id].sub(/@.+$/, '')
|
95
100
|
end
|
96
|
-
|
97
|
-
unless
|
98
|
-
@
|
101
|
+
|
102
|
+
unless @config[:master].is_a?(Array)
|
103
|
+
@config[:master] = [@config[:master]]
|
99
104
|
end
|
100
|
-
|
105
|
+
|
101
106
|
@commands = { :spec => [], :meta => {} }
|
102
|
-
|
107
|
+
|
103
108
|
add_command(
|
104
|
-
:syntax => 'help',
|
105
|
-
:description => 'Display
|
106
|
-
|
107
|
-
:
|
108
|
-
:
|
109
|
-
|
109
|
+
:syntax => 'help [<command>]',
|
110
|
+
:description => 'Display help for the given command, or all commands' +
|
111
|
+
' if no command is specified',
|
112
|
+
:regex => /^help(\s+?.+?)?$/,
|
113
|
+
:alias => [ :syntax => '? [<command>]', :regex => /^\?(\s+?.+?)?$/ ],
|
114
|
+
:is_public => @config[:is_public]
|
115
|
+
) { |sender, message| help_message(sender, message) }
|
110
116
|
end
|
111
|
-
|
117
|
+
|
112
118
|
# Add a command to the bot's repertoire.
|
113
119
|
#
|
114
120
|
# Commands consist of a metadata Hash and a callback block. The metadata
|
115
|
-
# Hash *must* contain the command syntax
|
116
|
-
#
|
117
|
-
#
|
121
|
+
# Hash *must* contain the command +syntax+, a +description+ for display with
|
122
|
+
# the builtin 'help' command, and a regular expression (+regex+) to detect
|
123
|
+
# the presence of the command in an incoming message.
|
118
124
|
#
|
119
125
|
# The metadata Hash may optionally contain an array of command aliases. An
|
120
|
-
# alias consists of an alias syntax and regex
|
121
|
-
# understand command shorthands. For example, the default 'help' command
|
122
|
-
# an alias '?'. Saying either 'help' or '?' will trigger the same
|
123
|
-
# callback block.
|
126
|
+
# +alias+ consists of an alias +syntax+ and +regex+. Aliases allow the bot
|
127
|
+
# to understand command shorthands. For example, the default 'help' command
|
128
|
+
# has an alias '?'. Saying either 'help' or '?' will trigger the same
|
129
|
+
# command callback block.
|
124
130
|
#
|
125
|
-
# The metadata Hash may optionally contain
|
131
|
+
# The metadata Hash may optionally contain a +is_public+ flag, indicating
|
126
132
|
# the bot should respond to *anyone* issuing the command, not just the bot
|
127
|
-
# master(s).
|
133
|
+
# master(s). Public commands are only truly public if the bot itself has
|
128
134
|
# been made public.
|
129
135
|
#
|
130
|
-
# The specified callback block will be triggered when the bot receives a
|
131
|
-
# message that matches the given command regex (or an alias regex). The
|
132
|
-
# callback block will have access to the sender and the message text (not
|
133
|
-
# including the command), and should either return a String response
|
134
|
-
#
|
135
|
-
# delivered to the
|
136
|
-
#
|
136
|
+
# The specified callback block will be triggered when the bot receives a
|
137
|
+
# message that matches the given command regex (or an alias regex). The
|
138
|
+
# callback block will have access to the sender and the message text (not
|
139
|
+
# including the command itsef), and should either return a String response
|
140
|
+
# or +nil+. If a callback block returns a String response, the response will
|
141
|
+
# be delivered to the Jabber id that issued the command.
|
142
|
+
#
|
137
143
|
# Examples:
|
138
144
|
#
|
139
|
-
# # Say
|
145
|
+
# # Say 'puts foo' or 'p foo' and 'foo' will be written to $stdout.
|
140
146
|
# # The bot will also respond with "'foo' written to $stdout."
|
141
147
|
# add_command(
|
142
148
|
# :syntax => 'puts <string>',
|
143
149
|
# :description => 'Write something to $stdout',
|
144
|
-
# :regex => /^puts\s
|
145
|
-
#
|
146
|
-
#
|
150
|
+
# :regex => /^puts\s+.+$/,
|
151
|
+
# :alias => [ :syntax => 'p <string>', :regex => /^p\s+.+$/ ]
|
152
|
+
# ) do |sender, message|
|
153
|
+
# puts "#{sender} says #{message}."
|
147
154
|
# "'#{message}' written to $stdout."
|
148
155
|
# end
|
149
156
|
#
|
150
|
-
# #
|
157
|
+
# # 'puts!' is a non-responding version of 'puts', and has two aliases,
|
158
|
+
# # 'p!' and '!'
|
151
159
|
# add_command(
|
152
160
|
# :syntax => 'puts! <string>',
|
153
161
|
# :description => 'Write something to $stdout (without response)',
|
154
162
|
# :regex => /^puts!\s+.+$/,
|
155
|
-
# :alias => [
|
156
|
-
#
|
157
|
-
#
|
163
|
+
# :alias => [
|
164
|
+
# { :syntax => 'p! <string>', :regex => /^p!\s+.+$/ },
|
165
|
+
# { :syntax => '! <string>', :regex => /^!\s+/.+$/ }
|
166
|
+
# ]
|
167
|
+
# ) do |sender, message|
|
168
|
+
# puts "#{sender} says #{message}."
|
158
169
|
# nil
|
159
170
|
# end
|
160
171
|
#
|
@@ -167,49 +178,32 @@ module Jabber
|
|
167
178
|
# ) { rand(10).to_s }
|
168
179
|
#
|
169
180
|
def add_command(command, &callback)
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
:is_public => is_public
|
179
|
-
}
|
180
|
-
|
181
|
-
# Add the command spec. The command spec is used by parse_command.
|
182
|
-
@commands[:spec] << {
|
183
|
-
:regex => command[:regex],
|
184
|
-
:callback => callback,
|
185
|
-
:is_public => is_public
|
186
|
-
}
|
187
|
-
|
181
|
+
name = command_name(command[:syntax])
|
182
|
+
|
183
|
+
# Add the command meta - used in the 'help' command response.
|
184
|
+
add_command_meta(name, command)
|
185
|
+
|
186
|
+
# Add the command spec - used for parsing incoming commands.
|
187
|
+
add_command_spec(command, callback)
|
188
|
+
|
188
189
|
# Add any command aliases to the command meta and spec
|
189
190
|
unless command[:alias].nil?
|
190
|
-
command[:alias].each
|
191
|
-
@commands[:meta][syntax][:syntax] << a[:syntax]
|
192
|
-
|
193
|
-
@commands[:spec] << {
|
194
|
-
:regex => a[:regex],
|
195
|
-
:callback => callback,
|
196
|
-
:is_public => is_public
|
197
|
-
}
|
198
|
-
end
|
191
|
+
command[:alias].each { |a| add_command_alias(name, a, callback) }
|
199
192
|
end
|
200
193
|
end
|
201
|
-
|
202
|
-
# Connect the bot, making
|
194
|
+
|
195
|
+
# Connect the bot, making it available to accept commands.
|
203
196
|
def connect
|
204
|
-
@jabber = Jabber::Simple.new(@
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
197
|
+
@jabber = Jabber::Simple.new(@config[:jabber_id], @config[:password])
|
198
|
+
|
199
|
+
presence(@config[:presence], @config[:status], @config[:priority])
|
200
|
+
|
201
|
+
deliver(@config[:master], "#{@config[:name]} reporting for duty.")
|
202
|
+
|
203
|
+
start_listener_thread
|
210
204
|
end
|
211
|
-
|
212
|
-
# Deliver a message to the specified recipient(s).
|
205
|
+
|
206
|
+
# Deliver a message to the specified recipient(s). Accepts a single
|
213
207
|
# recipient or an Array of recipients.
|
214
208
|
def deliver(to, message)
|
215
209
|
if to.is_a?(Array)
|
@@ -218,83 +212,186 @@ module Jabber
|
|
218
212
|
@jabber.deliver(to, message)
|
219
213
|
end
|
220
214
|
end
|
221
|
-
|
222
|
-
#
|
223
|
-
#
|
224
|
-
def
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
@commands[:meta].sort.each do |command|
|
230
|
-
if command[1][:is_public] == true || is_master
|
231
|
-
command[1][:syntax].each { |syntax| help_message += "#{syntax}\n" }
|
232
|
-
help_message += " #{command[1][:description]}\n\n"
|
233
|
-
end
|
215
|
+
|
216
|
+
# Disconnect the bot. Once the bot has been disconnected, there is no way
|
217
|
+
# to restart it by issuing a command.
|
218
|
+
def disconnect
|
219
|
+
if @jabber.connected?
|
220
|
+
deliver(@config[:master], "#{@config[:name]} disconnecting...")
|
221
|
+
@jabber.disconnect
|
234
222
|
end
|
235
|
-
|
236
|
-
return help_message
|
237
|
-
end
|
238
|
-
|
239
|
-
# Direct access to the underlying
|
240
|
-
# Jabber::Simple[http://xmpp4r-simple.rubyforge.org/] object.
|
241
|
-
def jabber
|
242
|
-
return @jabber
|
243
223
|
end
|
244
|
-
|
245
|
-
#
|
224
|
+
|
225
|
+
# Returns an Array of masters
|
246
226
|
def master
|
247
|
-
|
227
|
+
@config[:master]
|
228
|
+
end
|
229
|
+
|
230
|
+
# Returns +true+ if the given Jabber id is a master, +false+ otherwise.
|
231
|
+
def master?(jabber_id)
|
232
|
+
@config[:master].include? jabber_id
|
233
|
+
end
|
234
|
+
|
235
|
+
# Sets the bot presence, status message and priority.
|
236
|
+
def presence(presence=nil, status=nil, priority=nil)
|
237
|
+
@config[:presence] = presence
|
238
|
+
@config[:status] = status
|
239
|
+
@config[:priority] = priority
|
240
|
+
|
241
|
+
status_message = Presence.new(presence, status, priority)
|
242
|
+
@jabber.send!(status_message) if @jabber.connected?
|
243
|
+
end
|
244
|
+
|
245
|
+
# Sets the bot presence. If you need to set more than just the presence,
|
246
|
+
# use presence() instead.
|
247
|
+
#
|
248
|
+
# Available values for presence are:
|
249
|
+
#
|
250
|
+
# * nil : online
|
251
|
+
# * :chat : free for chat
|
252
|
+
# * :away : away from the computer
|
253
|
+
# * :dnd : do not disturb
|
254
|
+
# * :xa : extended away
|
255
|
+
#
|
256
|
+
def presence=(presence)
|
257
|
+
presence(presence, @config[:status], @config[:priority])
|
258
|
+
end
|
259
|
+
|
260
|
+
# Set the bot priority. Priority is an integer from -127 to 127. If you need
|
261
|
+
# to set more than just the priority, use presence() instead.
|
262
|
+
def priority=(priority)
|
263
|
+
presence(@config[:presence], @config[:status], priority)
|
248
264
|
end
|
249
|
-
|
250
|
-
#
|
265
|
+
|
266
|
+
# Set the status message. A status message is just a String, e.g. 'I am
|
267
|
+
# here.' or 'Out to lunch.' If you need to set more than just the status
|
268
|
+
# message, use presence() instead.
|
269
|
+
def status=(status)
|
270
|
+
presence(@config[:presence], status, @config[:priority])
|
271
|
+
end
|
272
|
+
|
273
|
+
private
|
274
|
+
|
275
|
+
# Add a command alias for the given original +command_name+
|
276
|
+
def add_command_alias(command_name, alias_command, callback) #:nodoc:
|
277
|
+
original_command = @commands[:meta][command_name]
|
278
|
+
original_command[:syntax] << alias_command[:syntax]
|
279
|
+
|
280
|
+
alias_name = command_name(alias_command[:syntax])
|
281
|
+
|
282
|
+
alias_command[:is_public] = original_command[:is_public]
|
283
|
+
|
284
|
+
add_command_meta(alias_name, original_command, true)
|
285
|
+
add_command_spec(alias_command, callback)
|
286
|
+
end
|
287
|
+
|
288
|
+
# Add a command meta
|
289
|
+
def add_command_meta(name, command, is_alias=false) #:nodoc:
|
290
|
+
syntax = command[:syntax]
|
291
|
+
|
292
|
+
@commands[:meta][name] = {
|
293
|
+
:syntax => syntax.is_a?(Array) ? syntax : [syntax],
|
294
|
+
:description => command[:description],
|
295
|
+
:is_public => command[:is_public] || false,
|
296
|
+
:is_alias => is_alias
|
297
|
+
}
|
298
|
+
end
|
299
|
+
|
300
|
+
# Add a command spec
|
301
|
+
def add_command_spec(command, callback) #:nodoc:
|
302
|
+
@commands[:spec] << {
|
303
|
+
:regex => command[:regex],
|
304
|
+
:callback => callback,
|
305
|
+
:is_public => command[:is_public] || false
|
306
|
+
}
|
307
|
+
end
|
308
|
+
|
309
|
+
# Extract the command name from the given syntax
|
310
|
+
def command_name(syntax) #:nodoc:
|
311
|
+
if syntax.include? ' '
|
312
|
+
name = syntax.sub(/^(\S+).*/, '\1')
|
313
|
+
else
|
314
|
+
name = syntax
|
315
|
+
end
|
316
|
+
|
317
|
+
name
|
318
|
+
end
|
319
|
+
|
320
|
+
# Returns the default help message describing the bot's command repertoire.
|
321
|
+
# Commands are sorted alphabetically by name, and are displayed according
|
322
|
+
# to the bot's and the commands's _public_ attribute.
|
323
|
+
def help_message(sender, command_name) #:nodoc:
|
324
|
+
if command_name.nil? or command_name.length == 0
|
325
|
+
# Display help for all commands
|
326
|
+
help_message = "I understand the following commands:\n\n"
|
327
|
+
|
328
|
+
@commands[:meta].sort.each do |command|
|
329
|
+
# Thank you, Hash.sort
|
330
|
+
command = command[1]
|
331
|
+
|
332
|
+
if !command[:is_alias] and (command[:is_public] or master? sender)
|
333
|
+
command[:syntax].each { |syntax| help_message += "#{syntax}\n" }
|
334
|
+
help_message += " #{command[:description]}\n\n"
|
335
|
+
end
|
336
|
+
end
|
337
|
+
else
|
338
|
+
# Display help for the given command
|
339
|
+
command = @commands[:meta][command_name]
|
340
|
+
|
341
|
+
if command.nil?
|
342
|
+
help_message = "I don't understand '#{command_name}' Try saying" +
|
343
|
+
" 'help' to see what commands I understand."
|
344
|
+
else
|
345
|
+
help_message = ''
|
346
|
+
command[:syntax].each { |syntax| help_message += "#{syntax}\n" }
|
347
|
+
help_message += " #{command[:description]} "
|
348
|
+
end
|
349
|
+
end
|
350
|
+
|
351
|
+
help_message
|
352
|
+
end
|
353
|
+
|
354
|
+
# Parses the given command message for the presence of a known command by
|
251
355
|
# testing it against each known command's regex. If a known command is
|
252
|
-
# found, the command parameters are passed on to the callback block, minus
|
356
|
+
# found, the command parameters are passed on to the callback block, minus
|
253
357
|
# the command trigger. If a String result is present it is delivered to the
|
254
358
|
# sender.
|
255
359
|
#
|
256
360
|
# If the bot has not been made public, commands from anyone other than the
|
257
361
|
# bot master(s) will be silently ignored.
|
258
362
|
def parse_command(sender, message) #:nodoc:
|
259
|
-
is_master =
|
260
|
-
|
261
|
-
if @
|
262
|
-
|
363
|
+
is_master = master? sender
|
364
|
+
|
365
|
+
if @config[:is_public] or is_master
|
263
366
|
@commands[:spec].each do |command|
|
264
367
|
if command[:is_public] or is_master
|
265
368
|
unless (message.strip =~ command[:regex]).nil?
|
266
|
-
|
267
|
-
|
268
|
-
|
369
|
+
params = nil
|
370
|
+
|
371
|
+
if message.include? ' '
|
372
|
+
params = message.sub(/^\S+\s+(.*)$/, '\1')
|
373
|
+
end
|
374
|
+
|
375
|
+
response = command[:callback].call(sender, params)
|
269
376
|
deliver(sender, response) unless response.nil?
|
270
377
|
return
|
271
378
|
end
|
272
379
|
end
|
273
380
|
end
|
274
|
-
|
275
|
-
response = "I don't understand '#{message.strip}
|
381
|
+
|
382
|
+
response = "I don't understand '#{message.strip}' Try saying 'help' " +
|
276
383
|
"to see what commands I understand."
|
277
|
-
deliver(sender, response)
|
278
|
-
|
384
|
+
deliver(sender, response)
|
279
385
|
end
|
280
386
|
end
|
281
|
-
|
282
|
-
# Disconnect the bot. Once the bot has been disconnected, there is no way
|
283
|
-
# to restart it by issuing a command.
|
284
|
-
def disconnect
|
285
|
-
if @jabber.connected?
|
286
|
-
deliver(@bot_config[:master], "#{@bot_config[:name]} disconnecting...")
|
287
|
-
@jabber.disconnect
|
288
|
-
end
|
289
|
-
end
|
290
|
-
|
387
|
+
|
291
388
|
# Creates a new Thread dedicated to listening for incoming chat messages.
|
292
|
-
# When a chat message is received, the bot checks if the sender is its
|
293
|
-
# master.
|
389
|
+
# When a chat message is received, the bot checks if the sender is its
|
390
|
+
# master. If so, it is tested for the presence commands, and processed
|
294
391
|
# accordingly. If the bot itself or the command issued is not made public,
|
295
392
|
# a message sent by anyone other than the bot's master is silently ignored.
|
296
393
|
#
|
297
|
-
# Only the chat message type is supported.
|
394
|
+
# Only the chat message type is supported. Other message types such as
|
298
395
|
# error and groupchat are not supported.
|
299
396
|
def start_listener_thread #:nodoc:
|
300
397
|
listener_thread = Thread.new do
|
@@ -302,22 +399,22 @@ module Jabber
|
|
302
399
|
@jabber.received_messages do |message|
|
303
400
|
# Remove the Jabber resourse, if any
|
304
401
|
sender = message.from.to_s.sub(/\/.+$/, '')
|
305
|
-
|
306
|
-
if message.type == :chat
|
307
|
-
parse_thread = Thread.new do
|
402
|
+
|
403
|
+
if message.type == :chat
|
404
|
+
parse_thread = Thread.new do
|
308
405
|
parse_command(sender, message.body)
|
309
406
|
end
|
310
|
-
|
407
|
+
|
311
408
|
parse_thread.join
|
312
409
|
end
|
313
410
|
end
|
314
|
-
|
411
|
+
|
315
412
|
sleep 1
|
316
413
|
end
|
317
414
|
end
|
318
|
-
|
415
|
+
|
319
416
|
listener_thread.join
|
320
417
|
end
|
321
|
-
|
418
|
+
|
322
419
|
end
|
323
420
|
end
|
metadata
CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.0
|
|
3
3
|
specification_version: 1
|
4
4
|
name: jabber-bot
|
5
5
|
version: !ruby/object:Gem::Version
|
6
|
-
version: 1.0
|
7
|
-
date: 2007-03-
|
6
|
+
version: 1.1.0
|
7
|
+
date: 2007-03-24 00:00:00 -07:00
|
8
8
|
summary: Jabber::Bot makes it simple to create and command your own Jabber bot with little fuss. By adding custom commands powered by regular expressions to your bot's repertoire, you and your new bot will be able to accomplish nearly anything.
|
9
9
|
require_paths:
|
10
10
|
- lib
|