jabber-bot 1.2.0 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. data/HISTORY +3 -0
  2. data/LICENSE +1 -1
  3. data/README.rdoc +3 -3
  4. data/lib/jabber/bot.rb +35 -24
  5. metadata +4 -4
data/HISTORY CHANGED
@@ -1,5 +1,8 @@
1
1
  Jabber::Bot Release History
2
2
 
3
+ Version 1.3.0 (15 May 2011)
4
+ * Allow multiple parameters for bot commands. (v0n)
5
+
3
6
  Version 1.2.0 (23 April 2011)
4
7
  * Optionally specify a startup message. (nstielau)
5
8
  * Optionally disable the misunderstood command reply. (nstielau)
data/LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2009 Brett Stimmerman <brettstimmerman@gmail.com>
1
+ Copyright (c) 2011 Brett Stimmerman <brettstimmerman@gmail.com>
2
2
  All rights reserved.
3
3
 
4
4
  Redistribution and use in source and binary forms, with or without
@@ -6,7 +6,7 @@ Jabber::Bot makes it simple to create and command your own Jabber bot. Bots
6
6
  are created by defining commands powered by regular expressions and Ruby.
7
7
 
8
8
  *Author*:: Brett Stimmerman (mailto:brettstimmerman@gmail.com)
9
- *Version*:: 1.2.0 (2011-04-23)
9
+ *Version*:: 1.3.0 (2011-05-15)
10
10
  *Copyright*:: Copyright (c) 2011 Brett Stimmerman. All rights reserved.
11
11
  *License*:: New BSD License (http://opensource.org/licenses/bsd-license.php)
12
12
  *Website*:: http://github.com/brettstimmerman/jabber-bot
@@ -43,8 +43,8 @@ are created by defining commands powered by regular expressions and Ruby.
43
43
  bot.add_command(
44
44
  :syntax => 'puts <string>',
45
45
  :description => 'Write something to $stdout',
46
- :regex => /^puts\s+.+$/,
47
- :alias => [ :alias => 'p <string>', :regex => /^p\s+.+$/ ],
46
+ :regex => /^puts\s+(.+)$/,
47
+ :alias => [ :alias => 'p <string>', :regex => /^p\s+(.+)$/ ],
48
48
  :is_public => true
49
49
  ) do |sender, message|
50
50
  puts "#{sender} says '#{message}'"
@@ -1,5 +1,5 @@
1
1
  #--
2
- # Copyright (c) 2009 Brett Stimmerman <brettstimmerman@gmail.com>
2
+ # Copyright (c) 2011 Brett Stimmerman <brettstimmerman@gmail.com>
3
3
  # All rights reserved.
4
4
  #
5
5
  # Redistribution and use in source and binary forms, with or without
@@ -82,7 +82,7 @@ module Jabber
82
82
 
83
83
  @config[:is_public] ||= false
84
84
 
85
- if @config[:name].nil? or @config[:name].length == 0
85
+ if @config[:name].nil? || @config[:name].length == 0
86
86
  @config[:name] = @config[:jabber_id].sub(/@.+$/, '')
87
87
  end
88
88
 
@@ -113,6 +113,14 @@ module Jabber
113
113
  # the builtin 'help' command, and a regular expression (+regex+) to detect
114
114
  # the presence of the command in an incoming message.
115
115
  #
116
+ # The command parameter(s) will be parsed from group(s) (text between
117
+ # parenthesis) in the +regex+. If there's none, one, or more than one
118
+ # occurrence, the callback block will receive respectively nil, a String,
119
+ # or an Array.
120
+ # e.g. With a command defined like this: /^cmd\s+(.+)\s+(.+)\s+(.+)$/,
121
+ # writing "cmd foo bar 42" will send ["foo", "bar", "42"] to the callback
122
+ # block.
123
+ #
116
124
  # The metadata Hash may optionally contain an array of command aliases. An
117
125
  # +alias+ consists of an alias +syntax+ and +regex+. Aliases allow the bot
118
126
  # to understand command shorthands. For example, the default 'help' command
@@ -126,8 +134,8 @@ module Jabber
126
134
  #
127
135
  # The specified callback block will be triggered when the bot receives a
128
136
  # message that matches the given command regex (or an alias regex). The
129
- # callback block will have access to the sender and the message text (not
130
- # including the command itsef), and should either return a String response
137
+ # callback block will have access to the sender and the parameter(s) (not
138
+ # including the command itself), and should either return a String response
131
139
  # or +nil+. If a callback block returns a String response, the response will
132
140
  # be delivered to the Jabber id that issued the command.
133
141
  #
@@ -138,8 +146,8 @@ module Jabber
138
146
  # add_command(
139
147
  # :syntax => 'puts <string>',
140
148
  # :description => 'Write something to $stdout',
141
- # :regex => /^puts\s+.+$/,
142
- # :alias => [ :syntax => 'p <string>', :regex => /^p\s+.+$/ ]
149
+ # :regex => /^puts\s+(.+)$/,
150
+ # :alias => [ :syntax => 'p <string>', :regex => /^p\s+(.+)$/ ]
143
151
  # ) do |sender, message|
144
152
  # puts "#{sender} says #{message}."
145
153
  # "'#{message}' written to $stdout."
@@ -150,10 +158,10 @@ module Jabber
150
158
  # add_command(
151
159
  # :syntax => 'puts! <string>',
152
160
  # :description => 'Write something to $stdout (without response)',
153
- # :regex => /^puts!\s+.+$/,
154
- # :alias => [
155
- # { :syntax => 'p! <string>', :regex => /^p!\s+.+$/ },
156
- # { :syntax => '! <string>', :regex => /^!\s+/.+$/ }
161
+ # :regex => /^puts!\s+(.+)$/,
162
+ # :alias => [
163
+ # { :syntax => 'p! <string>', :regex => /^p!\s+(.+)$/ },
164
+ # { :syntax => '! <string>', :regex => /^!\s+(.+)$/ }
157
165
  # ]
158
166
  # ) do |sender, message|
159
167
  # puts "#{sender} says #{message}."
@@ -312,7 +320,7 @@ module Jabber
312
320
  # Commands are sorted alphabetically by name, and are displayed according
313
321
  # to the bot's and the commands's _public_ attribute.
314
322
  def help_message(sender, command_name) #:nodoc:
315
- if command_name.nil? or command_name.length == 0
323
+ if command_name.nil? || command_name.length == 0
316
324
  # Display help for all commands
317
325
  help_message = "I understand the following commands:\n\n"
318
326
 
@@ -320,7 +328,7 @@ module Jabber
320
328
  # Thank you, Hash.sort
321
329
  command = command[1]
322
330
 
323
- if !command[:is_alias] and (command[:is_public] or master? sender)
331
+ if !command[:is_alias] && (command[:is_public] || master?(sender))
324
332
  command[:syntax].each { |syntax| help_message += "#{syntax}\n" }
325
333
  help_message += " #{command[:description]}\n\n"
326
334
  end
@@ -344,28 +352,31 @@ module Jabber
344
352
 
345
353
  # Parses the given command message for the presence of a known command by
346
354
  # testing it against each known command's regex. If a known command is
347
- # found, the command parameters are passed on to the callback block, minus
348
- # the command trigger. If a String result is present it is delivered to the
355
+ # found, the command parameters are parsed from groups defined in the
356
+ # regex, if any. They are passed on to the callback block this way:
357
+ # +nil+ if there's no parameter, a String if there's just one occurrence,
358
+ # or an Array if there's more than one occurence.
359
+ #
360
+ # If the callback returns a non- +nil+ value, it will be delivered to the
349
361
  # sender.
350
362
  #
351
363
  # If an unkown command is found, the bot will default to displaying the
352
- # help message. You can disable this by setting the configuration
353
- # setting ':misunderstood_message' to false.
364
+ # help message. You can disable this by setting +:misunderstood_message+
365
+ # to false in the bot configuration.
354
366
  #
355
367
  # If the bot has not been made public, commands from anyone other than the
356
368
  # bot master(s) will be silently ignored.
357
369
  def parse_command(sender, message) #:nodoc:
358
- is_master = master? sender
370
+ is_master = master?(sender)
359
371
 
360
- if @config[:is_public] or is_master
372
+ if @config[:is_public] || is_master
361
373
  @commands[:spec].each do |command|
362
- if command[:is_public] or is_master
363
- unless (message.strip =~ command[:regex]).nil?
364
- params = nil
374
+ if command[:is_public] || is_master
375
+ match = message.strip.match(command[:regex])
365
376
 
366
- if message.include? ' '
367
- params = message.sub(/^\S+\s+(.*)$/, '\1')
368
- end
377
+ unless match.nil?
378
+ params = match.captures # Pass an array,
379
+ params = params.pop if params.count < 2 # a string, or nil.
369
380
 
370
381
  response = command[:callback].call(sender, params)
371
382
  deliver(sender, response) unless response.nil?
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jabber-bot
3
3
  version: !ruby/object:Gem::Version
4
- hash: 31
4
+ hash: 27
5
5
  prerelease:
6
6
  segments:
7
7
  - 1
8
- - 2
8
+ - 3
9
9
  - 0
10
- version: 1.2.0
10
+ version: 1.3.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Brett Stimmerman
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-04-23 00:00:00 Z
18
+ date: 2011-05-15 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: xmpp4r-simple