jabbot 0.2.0 → 0.3.1

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.md CHANGED
@@ -1,8 +1,5 @@
1
1
  # Jabbot
2
2
 
3
- Official URL: http://github.com/badboy/jabbot/tree/master
4
- Jan-Erik Rediger (badboy\_) (http://badboy.pytalhost.de)
5
-
6
3
  ## Description
7
4
 
8
5
  Jabbot is a Ruby micro-framework for creating Jabber/MUC bots,
@@ -15,6 +12,10 @@ http://github.com/cjohansen/twibot/tree/master
15
12
  A big thank you to Christian Johansen, who wrote the code for Twibot.
16
13
  Jabbot is heavily based on his code.
17
14
 
15
+ If your curious if this code is stable enough:
16
+ I have a bot instance running on my server for quite some time now
17
+ and it works great :)
18
+
18
19
  ## Usage
19
20
 
20
21
  ### Simple example
@@ -25,8 +26,8 @@ Jabbot is heavily based on his code.
25
26
  end
26
27
 
27
28
  # Respond to query if they come from the right crowd
28
- # post "message" => "user" is just some syntax sugar
29
- # post "message", "user" will work to
29
+ # query "message" => "user" is just some syntax sugar
30
+ # query "message", "user" will work to
30
31
  query :from => [:cjno, :irbno] do |message, params|
31
32
  post "#{message.user} I agree" => message.user
32
33
  end
@@ -65,11 +66,10 @@ You can also configure with Ruby:
65
66
 
66
67
  If you don't specify login and/or password in any of these ways, Jabbot will fail
67
68
  Nick is automatically set to "jabbot" unless something different is defined
68
- If you want you can set the Jabber Resource:
69
+ If you want you can set the XMPP Resource:
69
70
 
70
71
  configure do |conf|
71
72
  conf.resource ="mybot_resource"
72
-
73
73
  end
74
74
 
75
75
  Default is "jabbot".
@@ -77,7 +77,7 @@ Default is "jabbot".
77
77
  ### "Routes"
78
78
 
79
79
  Like Sinatra, and other web app frameworks, Jabbot supports "routes":
80
- patterns to match incoming tweets and messages:
80
+ patterns to match incoming messages:
81
81
 
82
82
  message "time :country :city" do |message, params|
83
83
  time = MyTimeService.lookup(params[:country], params[:city])
@@ -95,6 +95,14 @@ Jabbot also supports regular expressions as routes:
95
95
  post "Time is #{time} in #{params[:city]}, #{params[:country]}"
96
96
  end
97
97
 
98
+ If all you need is exact word matching you can say so:
99
+
100
+ message :exact => "pattern" do |message, params|
101
+ ...
102
+ end
103
+
104
+ Internally this pattern is translated to `/\Apattern\Z/`, so you can use regex literals.
105
+
98
106
  ## Requirements
99
107
 
100
108
  xmpp4r. You'll need atleast 0.4.
@@ -10,10 +10,10 @@ require 'jabbot/bot.rb'
10
10
  require 'jabbot/config.rb'
11
11
  require 'jabbot/handlers.rb'
12
12
  require 'jabbot/macros.rb'
13
- require 'jabbot/message.rb'
14
- require 'jabbot/version.rb'
15
13
 
16
14
  module Jabbot
15
+ VERSION = '0.3.1'
16
+
17
17
  @@app_file = lambda do
18
18
  ignore = [
19
19
  /lib\/twibot.*\.rb/, # Library
@@ -41,7 +41,6 @@ module Jabbot
41
41
  # connect to Jabber and join channel
42
42
  #
43
43
  def connect
44
- #Jabber::debug = true
45
44
  @jid = Jabber::JID.new(login)
46
45
  @mucjid = Jabber::JID.new("#{channel}@#{server}")
47
46
 
@@ -60,6 +59,13 @@ module Jabbot
60
59
  end
61
60
 
62
61
  @client = Jabber::Client.new(@jid)
62
+ @client.on_exception do |*args|
63
+ $stderr.puts "got an intern EXCEPTION, args where:"
64
+ $stderr.puts args.inspect
65
+ $stderr.puts "exiting..."
66
+
67
+ exit
68
+ end
63
69
  @connected = true
64
70
  begin
65
71
  @client.connect
@@ -68,7 +74,7 @@ module Jabbot
68
74
  muc_handlers.call(@muc)
69
75
  @muc.join(@mucjid)
70
76
  rescue => errmsg
71
- STDERR.write "#{errmsg.class}\n#{errmsg}, #{errmsg.backtrace.join("\n")}"
77
+ $stderr.write "#{errmsg.class}\n#{errmsg}, #{errmsg.backtrace.join("\n")}"
72
78
  exit 1
73
79
  end
74
80
  end
@@ -79,10 +85,14 @@ module Jabbot
79
85
  def run!
80
86
  puts "Jabbot #{Jabbot::VERSION} imposing as #{login} on #{channel}@#{server}"
81
87
 
82
- trap(:INT) do
88
+ onclose_block = proc {
89
+ close
83
90
  puts "\nAnd it's a wrap. See ya soon!"
84
91
  exit
85
- end
92
+ }
93
+
94
+ Kernel.trap(:INT, onclose_block)
95
+ Kernel.trap(:QUIT, onclose_block)
86
96
 
87
97
  debug! if config[:debug]
88
98
  connect
@@ -9,7 +9,7 @@ module Jabbot
9
9
  end
10
10
 
11
11
  def dispatch(type, message)
12
- handlers[type].each { |handler| handler.dispatch(message) }
12
+ handlers[type].each {|handler| handler.dispatch(message) }
13
13
  end
14
14
 
15
15
  def handlers
@@ -34,12 +34,16 @@ module Jabbot
34
34
  class Handler
35
35
  def initialize(pattern = nil, options = {}, &blk)
36
36
  if pattern.is_a?(Hash)
37
- options = pattern
38
- pattern = nil
37
+ if pattern.keys.first == :exact
38
+ pattern = /\A#{pattern[:exact]}\Z/
39
+ else
40
+ options = pattern
41
+ pattern = nil
42
+ end
39
43
  end
40
44
 
41
45
  @options = options
42
- @options[:from].collect! { |s| s.to_s } if @options[:from] && @options[:from].is_a?(Array)
46
+ @options[:from].collect! {|s| s.to_s } if @options[:from] && @options[:from].is_a?(Array)
43
47
  @options[:from] = [@options[:from].to_s] if @options[:from] && [String, Symbol].include?(@options[:from].class)
44
48
  @handler = nil
45
49
  @handler = block_given? ? blk : nil
@@ -50,21 +54,17 @@ module Jabbot
50
54
  # Parse pattern string and set options
51
55
  #
52
56
  def pattern=(pattern)
53
- return if pattern.nil? || pattern == ""
54
-
55
- if pattern == :all
56
- return
57
- end
57
+ return if pattern.nil? || pattern == '' || pattern == :all
58
58
 
59
59
  if pattern.is_a?(Regexp)
60
60
  @options[:pattern] = pattern
61
61
  return
62
62
  end
63
63
 
64
- words = pattern.split.collect { |s| s.strip } # Get all words in pattern
64
+ words = pattern.split.collect {|s| s.strip } # Get all words in pattern
65
65
  @options[:tokens] = words.inject([]) do |sum, token| # Find all tokens, ie :symbol :like :names
66
66
  next sum unless token =~ /^:.*/ # Don't process regular words
67
- sym = token.sub(":", "").to_sym # Turn token string into symbol, ie ":token" => :token
67
+ sym = token.sub(':', '').to_sym # Turn token string into symbol, ie ":token" => :token
68
68
  regex = @options[sym] || '[^\s]+' # Fetch regex if configured, else use any character but space matching
69
69
  pattern.sub!(/(^|\s)#{token}(\s|$)/, '\1(' + regex.to_s + ')\2') # Make sure regex captures named switch
70
70
  sum << sym
@@ -186,4 +186,16 @@ context "Handler" do
186
186
  assert handler.recognize?(message)
187
187
  handler.dispatch(message)
188
188
  end
189
+
190
+ test "recognize matching messages with :exact pattern" do
191
+ handler = Jabbot::Handler.new :exact => "!pattern"
192
+ message = mock_message "dude", "!pattern"
193
+ assert handler.recognize?(message)
194
+ end
195
+
196
+ test "not recognize non-matching message with :exact patterns" do
197
+ handler = Jabbot::Handler.new :exact => "!pattern"
198
+ message = mock_message "dude", " !pattern "
199
+ assert !handler.recognize?(message)
200
+ end
189
201
  end
metadata CHANGED
@@ -1,12 +1,8 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jabbot
3
3
  version: !ruby/object:Gem::Version
4
- prerelease: false
5
- segments:
6
- - 0
7
- - 2
8
- - 0
9
- version: 0.2.0
4
+ prerelease:
5
+ version: 0.3.1
10
6
  platform: ruby
11
7
  authors:
12
8
  - badboy
@@ -14,7 +10,7 @@ autorequire:
14
10
  bindir: bin
15
11
  cert_chain: []
16
12
 
17
- date: 2010-10-03 00:00:00 +02:00
13
+ date: 2011-04-12 00:00:00 +02:00
18
14
  default_executable:
19
15
  dependencies:
20
16
  - !ruby/object:Gem::Dependency
@@ -25,9 +21,6 @@ dependencies:
25
21
  requirements:
26
22
  - - ">="
27
23
  - !ruby/object:Gem::Version
28
- segments:
29
- - 0
30
- - 4
31
24
  version: "0.4"
32
25
  type: :runtime
33
26
  version_requirements: *id001
@@ -39,10 +32,6 @@ dependencies:
39
32
  requirements:
40
33
  - - ">="
41
34
  - !ruby/object:Gem::Version
42
- segments:
43
- - 2
44
- - 10
45
- - 1
46
35
  version: 2.10.1
47
36
  type: :development
48
37
  version_requirements: *id002
@@ -64,8 +53,6 @@ files:
64
53
  - lib/jabbot/config.rb
65
54
  - lib/jabbot/handlers.rb
66
55
  - lib/jabbot/bot.rb
67
- - lib/jabbot/version.rb
68
- - lib/jabbot/message.rb
69
56
  - test/test_config.rb
70
57
  - test/test_macros.rb
71
58
  - test/test_bot.rb
@@ -86,21 +73,17 @@ required_ruby_version: !ruby/object:Gem::Requirement
86
73
  requirements:
87
74
  - - ">="
88
75
  - !ruby/object:Gem::Version
89
- segments:
90
- - 0
91
76
  version: "0"
92
77
  required_rubygems_version: !ruby/object:Gem::Requirement
93
78
  none: false
94
79
  requirements:
95
80
  - - ">="
96
81
  - !ruby/object:Gem::Version
97
- segments:
98
- - 0
99
82
  version: "0"
100
83
  requirements: []
101
84
 
102
85
  rubyforge_project:
103
- rubygems_version: 1.3.7
86
+ rubygems_version: 1.6.2
104
87
  signing_key:
105
88
  specification_version: 3
106
89
  summary: Simple framework for creating Jabber/MUC bots, inspired by Sinatra and Twibot
@@ -1,7 +0,0 @@
1
- module Jabbot
2
- Message = Struct.new(:user, :text, :time) do
3
- def to_s
4
- "#{user}: #{text}"
5
- end
6
- end
7
- end
@@ -1,3 +0,0 @@
1
- module Jabbot
2
- VERSION = '0.2.0'
3
- end