botfly 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
@@ -0,0 +1,22 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+ log
21
+
22
+ ## PROJECT::SPECIFIC
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Ryan Neufeld
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,17 @@
1
+ = botfly
2
+
3
+ Description goes here.
4
+
5
+ == Note on Patches/Pull Requests
6
+
7
+ * Fork the project.
8
+ * Make your feature addition or bug fix.
9
+ * Add tests for it. This is important so I don't break it in a
10
+ future version unintentionally.
11
+ * Commit, do not mess with rakefile, version, or history.
12
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
13
+ * Send me a pull request. Bonus points for topic branches.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2010 Ryan Neufeld. See LICENSE for details.
@@ -0,0 +1,46 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "botfly"
8
+ gem.summary = %Q{A quick and easy DSL for generating Jabber bots}
9
+ gem.description = %Q{Botfly is a Jabber Bot framework that lets you write Jabber bots in a modern DSL that just makes sense. Enjoy, while it's still fresh and VERY ALPHA.}
10
+ gem.email = "ryan@ryanneufeld.ca"
11
+ gem.homepage = "http://github.com/rkneufeld/botfly"
12
+ gem.authors = ["Ryan Neufeld"]
13
+ gem.add_development_dependency "rspec", ">= 1.2.9"
14
+ gem.add_dependency "xmpp4r", ">= 0.5"
15
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
16
+ end
17
+ Jeweler::GemcutterTasks.new
18
+ rescue LoadError
19
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
20
+ end
21
+
22
+ require 'spec/rake/spectask'
23
+ Spec::Rake::SpecTask.new(:spec) do |spec|
24
+ spec.libs << 'lib' << 'spec'
25
+ spec.spec_files = FileList['spec/**/*_spec.rb']
26
+ end
27
+
28
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
29
+ spec.libs << 'lib' << 'spec'
30
+ spec.pattern = 'spec/**/*_spec.rb'
31
+ spec.rcov = true
32
+ end
33
+
34
+ task :spec => :check_dependencies
35
+
36
+ task :default => :spec
37
+
38
+ require 'rake/rdoctask'
39
+ Rake::RDocTask.new do |rdoc|
40
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
41
+
42
+ rdoc.rdoc_dir = 'rdoc'
43
+ rdoc.title = "botfly #{version}"
44
+ rdoc.rdoc_files.include('README*')
45
+ rdoc.rdoc_files.include('lib/**/*.rb')
46
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,69 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{botfly}
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Ryan Neufeld"]
12
+ s.date = %q{2010-03-13}
13
+ s.description = %q{Botfly is a Jabber Bot framework that lets you write Jabber bots in a modern DSL that just makes sense. Enjoy, while it's still fresh and VERY ALPHA.}
14
+ s.email = %q{ryan@ryanneufeld.ca}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "LICENSE",
23
+ "README.rdoc",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "botfly.gemspec",
27
+ "example.rb",
28
+ "lib/botfly.rb",
29
+ "lib/botfly/bot.rb",
30
+ "lib/botfly/matcher.rb",
31
+ "lib/botfly/matcher/body_matcher.rb",
32
+ "lib/botfly/matcher/matcher.rb",
33
+ "lib/botfly/matcher/nick_matcher.rb",
34
+ "lib/botfly/matcher/subject_matcher.rb",
35
+ "lib/botfly/responder.rb",
36
+ "lib/botfly/responder/message_responder.rb",
37
+ "lib/botfly/responder/presence_responder.rb",
38
+ "lib/botfly/responder/responder.rb",
39
+ "spec/botfly_spec.rb",
40
+ "spec/spec.opts",
41
+ "spec/spec_helper.rb"
42
+ ]
43
+ s.homepage = %q{http://github.com/rkneufeld/botfly}
44
+ s.rdoc_options = ["--charset=UTF-8"]
45
+ s.require_paths = ["lib"]
46
+ s.rubygems_version = %q{1.3.5}
47
+ s.summary = %q{A quick and easy DSL for generating Jabber bots}
48
+ s.test_files = [
49
+ "spec/botfly_spec.rb",
50
+ "spec/spec_helper.rb"
51
+ ]
52
+
53
+ if s.respond_to? :specification_version then
54
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
55
+ s.specification_version = 3
56
+
57
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
58
+ s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
59
+ s.add_runtime_dependency(%q<xmpp4r>, [">= 0.5"])
60
+ else
61
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
62
+ s.add_dependency(%q<xmpp4r>, [">= 0.5"])
63
+ end
64
+ else
65
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
66
+ s.add_dependency(%q<xmpp4r>, [">= 0.5"])
67
+ end
68
+ end
69
+
@@ -0,0 +1,24 @@
1
+ require 'rubygems'
2
+ require 'botfly'
3
+
4
+ require 'yaml'
5
+
6
+ config = YAML::load(ARGF.read) if ARGF
7
+ puts config.inspect
8
+
9
+ Jabber::debug = true
10
+
11
+ bot = Botfly.login(config["jid"],config["pass"]) do
12
+ on(:message).body(/^exit$/) { reply "Goodbye!"; quit }
13
+ on(:message).nick(/rkneufeld/) do
14
+ Botfly.logger.info("Callback called")
15
+ @count ||= 0
16
+ @count += 1
17
+ reply("That's the #{@count}th message I've received.")
18
+ end
19
+
20
+ on(:presence) { puts self }
21
+ connect
22
+ end
23
+ Thread.stop
24
+ loop{}
@@ -0,0 +1,24 @@
1
+ require 'rubygems'
2
+
3
+ require 'xmpp4r'
4
+ require 'xmpp4r/muc'
5
+
6
+ require 'botfly/responder'
7
+ require 'botfly/bot'
8
+ require 'botfly/matcher'
9
+
10
+ require 'logger'
11
+
12
+ module Botfly
13
+ def Botfly.logger
14
+ @logger = Logger.new(@logfile)
15
+ return @logger
16
+ end
17
+ def Botfly.login(jid,pass,logfile=STDOUT,&block)
18
+ @logfile = logfile
19
+ Botfly.logger.info("BOTFLY: #login")
20
+ bot = Botfly::Bot.new(jid,pass)
21
+ bot.instance_eval(&block)
22
+ return bot # At this point doesn't get returned, as the thread is stopped
23
+ end
24
+ end
@@ -0,0 +1,73 @@
1
+ require 'rubygems'
2
+
3
+ module Botfly
4
+ class Bot
5
+ attr_reader :responders, :client
6
+ def initialize(jid,pass)
7
+ Botfly.logger.info(" BOT: Bot#new")
8
+ @password = pass
9
+ @jid = Jabber::JID.new(jid)
10
+ @client = Jabber::Client.new(@jid)
11
+ @responders = {}
12
+ @main_thread = Thread.current
13
+ end
14
+
15
+ def connect
16
+ Botfly.logger.info(" BOT: Connecting to #{@jid}...")
17
+ @client.connect
18
+ @client.auth(@password)
19
+ Botfly.logger.info(" BOT: Connected")
20
+ register_for_xmpp_callbacks
21
+ @client.send(Jabber::Presence.new.set_status("Carrier has arrived"))
22
+ #Thread.stop
23
+ end
24
+
25
+ def on(type, &block)
26
+ Botfly.logger.info(" BOT: Bot#on")
27
+ klass = Botfly.const_get(type.to_s.capitalize + "Responder")
28
+ (@responders[type] ||= []) << responder = klass.new(@client, self, &block)
29
+ Botfly.logger.info(" BOT: #{type.to_s.capitalize}Responder added to responder chain")
30
+ return responder
31
+ end
32
+
33
+ def quit
34
+ @client.close
35
+ @main_thread.continue
36
+ end
37
+
38
+ private
39
+
40
+ def register_for_xmpp_callbacks
41
+ Botfly.logger.info(" BOT: Registering for callbacks with client")
42
+ # @client.add_update_callback {|presence| respond_to(:update, :presence => presence) }
43
+ # @client.add_subscription_request_callback {|item, pres| } # requires Roster helper
44
+ @client.add_message_callback do |message|
45
+ Botfly.logger.debug(" CB: Got Message")
46
+ respond_to(:message, :message => message)
47
+ end
48
+ @client.add_presence_callback do |old_presence,new_presence|
49
+ Botfly.logger.debug(" CB: Got Presence")
50
+ respond_to(:presence, :old => old_presence, :new => new_presence)
51
+ end
52
+
53
+ end
54
+
55
+ def respond_to(callback_type, params)
56
+ Botfly.logger.info(" BOT: Responding to callback of type: #{callback_type}")
57
+ responders = params[:muc] ? @muc_responders[params[:muc]] : @responders
58
+ responders[callback_type].each {|r| r.callback_with params} if responders[callback_type]
59
+ end
60
+
61
+ # def register_for_muc_callbacks(client)
62
+ # params = {:muc => client}
63
+ ## client.on_join {|time,nick| respond_to(:join, params.merge!(:time=>time,:nick=>nick))}
64
+ # client.on_leave {|time,nick| respond_to(:leave, params.merge!(:time=>time,:nick=>nick))}
65
+ # client.on_message {|time,nick,text| respond_to(:message, params.merge!(:time=>time,:nick=>nick,:text=>text))}
66
+ # client.on_private_message {|time,nick,text| respond_to(:private_message, params.merge!(:time=>time,:nick=>nick,:text=>text))}
67
+ # client.on_room_message {|time,text| respond_to(:room_message, params.merge!(:time => time, :text => text))}
68
+ # client.on_self_leave {|time| respond_to(:self_leave, params.merge!(:time => time)) }
69
+ # client.on_subject {|time,nick,subject| respond_to(:subject, params.merge!(:time => time, :nick => nickname, :subject => subject))}
70
+ # end
71
+
72
+ end
73
+ end
@@ -0,0 +1,5 @@
1
+ require 'botfly/matcher/matcher'
2
+ # TODO: Scope the matchers by responder
3
+ require 'botfly/matcher/nick_matcher'
4
+ require 'botfly/matcher/body_matcher'
5
+ require 'botfly/matcher/subject_matcher'
@@ -0,0 +1,13 @@
1
+ require 'botfly/matcher'
2
+
3
+ # Msg: body, chat_state, subject, type (:chat, :error, :groupchat, :headline, :normal)
4
+ module Botfly
5
+ class BodyMatcher < Matcher
6
+ def match(params)
7
+ msg = params[:message]
8
+ Botfly.logger.debug " MCH: Matching #{@condition.inspect} against #{msg.body.inspect}"
9
+ Botfly.logger.debug " RESULT: #{(msg.body =~ @condition).inspect}"
10
+ return msg.body =~ @condition
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,11 @@
1
+ module Botfly
2
+ class Matcher
3
+ def initialize(condition)
4
+ Botfly.logger.info(" MCH: Creating Matcher")
5
+ @condition = condition
6
+ end
7
+ def match(params)
8
+ raise "AbstractMethodError: You must implement match in a concrete subclass"
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,13 @@
1
+ require 'botfly/matcher'
2
+
3
+ # Msg: body, chat_state, subject, type (:chat, :error, :groupchat, :headline, :normal)
4
+ module Botfly
5
+ class NickMatcher < Matcher
6
+ def match(params)
7
+ msg = params[:message]
8
+ Botfly.logger.debug " MCH: Matching #{@condition.inspect} against #{msg.from.inspect}"
9
+ Botfly.logger.debug " RESULT: #{(msg.from.node =~ @condition).inspect}"
10
+ return msg.from.node =~ @condition
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ require 'botfly/matcher'
2
+
3
+ # Msg: body, chat_state, subject, type (:chat, :error, :groupchat, :headline, :normal), from, to
4
+ module Botfly
5
+ class SubjectMatcher < Matcher
6
+ def match(params)
7
+ msg = params[:message]
8
+ Botfly.logger.debug " MCH: Matching #{@condition.inspect} against #{msg.subject.inspect}"
9
+ Botfly.logger.debug " RESULT: #{(msg.subject =~ @condition).inspect}"
10
+ return msg.subject.node =~ @condition
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,3 @@
1
+ require 'botfly/responder/responder'
2
+ require 'botfly/responder/message_responder'
3
+ require 'botfly/responder/presence_responder'
@@ -0,0 +1,23 @@
1
+ require 'forwardable'
2
+ module Botfly
3
+ class MessageResponder < Responder
4
+ extend Forwardable
5
+ def setup_instance_variables(params)
6
+ Botfly.logger.debug(" RSP: MessageResponder setting up instance variables")
7
+ @message = params[:message]
8
+ @body = @message.body
9
+ @chat_state = @message.chat_state
10
+ @subject = @message.subject
11
+ @type = @message.type
12
+ @from = @message.from
13
+ @to = @message.to
14
+ end
15
+
16
+ def reply(text)
17
+ Botfly.logger.debug(" RSP: MessageResponder#reply called")
18
+ msg = Jabber::Message.new(@from, text)
19
+ msg.type = :chat
20
+ @client.send(msg)
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,7 @@
1
+ module Botfly
2
+ class PresenceResponder < Responder
3
+ def setup_instance_variables(params)
4
+ Botfly.logger.debug(" RSP: #{self.class} setting up instance variables")
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,59 @@
1
+ module Botfly
2
+ class Responder
3
+ attr_reader :callback, :callback_type
4
+
5
+ def initialize(client,bot,&block)
6
+ Botfly.logger.info(" RSP: #{self.class.to_s}#new")
7
+ @matcher_chain = []
8
+ @bot = bot
9
+ @client = client
10
+ @callback = block if block_given?
11
+ end
12
+
13
+ def method_missing(method,condition,&block)
14
+ Botfly.logger.info(" RSP: Responder##{method}(#{condition.inspect})")
15
+
16
+ add_matcher(method,condition)
17
+
18
+ if block_given?
19
+ Botfly.logger.info(" RSP: Callback recorded: #{block.inspect}")
20
+ @callback = block
21
+ end
22
+
23
+ return self
24
+ end
25
+
26
+ def callback_with(params)
27
+ Botfly.logger.debug(" RSP: Launching callback with params: #{params.inspect}")
28
+
29
+ setup_instance_variables(params)
30
+ if @matcher_chain.all? {|matcher| matcher.match(params) }
31
+ Botfly.logger.debug(" RSP: All matchers passed")
32
+ cb = @callback # Ruby makes it difficult to apply & to an instance variable
33
+ instance_eval &cb
34
+ end
35
+ end
36
+
37
+ def quit
38
+ @bot.quit
39
+ end
40
+ # TODO: add other @client actions as delegates
41
+
42
+ private
43
+ def add_matcher(method, condition)
44
+ klass = Botfly.const_get(method.to_s.capitalize + "Matcher")
45
+ @matcher_chain << klass.new(condition)
46
+
47
+ Botfly.logger.debug(" RSP: Adding to matcher chain: #{@matcher_chain.inspect}")
48
+ end
49
+
50
+ # TODO: Check callback is in acceptable list - MUC subclass can override this list
51
+ def register_with_bot(callback_type)
52
+ raise "AbstractMethodError: You must implement this method in a concrete subclass"
53
+ end
54
+
55
+ def setup_instance_variables(params)
56
+ raise "AbstractMethodError: You must implement this method in a concrete subclass"
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,7 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "Botfly" do
4
+ it "fails" do
5
+ fail "hey buddy, you should probably rename this file and start specing for real"
6
+ end
7
+ end
@@ -0,0 +1 @@
1
+ --color
@@ -0,0 +1,9 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ require 'botfly'
4
+ require 'spec'
5
+ require 'spec/autorun'
6
+
7
+ Spec::Runner.configure do |config|
8
+
9
+ end
metadata ADDED
@@ -0,0 +1,97 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: botfly
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Ryan Neufeld
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-03-13 00:00:00 -06:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rspec
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 1.2.9
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: xmpp4r
27
+ type: :runtime
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: "0.5"
34
+ version:
35
+ description: Botfly is a Jabber Bot framework that lets you write Jabber bots in a modern DSL that just makes sense. Enjoy, while it's still fresh and VERY ALPHA.
36
+ email: ryan@ryanneufeld.ca
37
+ executables: []
38
+
39
+ extensions: []
40
+
41
+ extra_rdoc_files:
42
+ - LICENSE
43
+ - README.rdoc
44
+ files:
45
+ - .document
46
+ - .gitignore
47
+ - LICENSE
48
+ - README.rdoc
49
+ - Rakefile
50
+ - VERSION
51
+ - botfly.gemspec
52
+ - example.rb
53
+ - lib/botfly.rb
54
+ - lib/botfly/bot.rb
55
+ - lib/botfly/matcher.rb
56
+ - lib/botfly/matcher/body_matcher.rb
57
+ - lib/botfly/matcher/matcher.rb
58
+ - lib/botfly/matcher/nick_matcher.rb
59
+ - lib/botfly/matcher/subject_matcher.rb
60
+ - lib/botfly/responder.rb
61
+ - lib/botfly/responder/message_responder.rb
62
+ - lib/botfly/responder/presence_responder.rb
63
+ - lib/botfly/responder/responder.rb
64
+ - spec/botfly_spec.rb
65
+ - spec/spec.opts
66
+ - spec/spec_helper.rb
67
+ has_rdoc: true
68
+ homepage: http://github.com/rkneufeld/botfly
69
+ licenses: []
70
+
71
+ post_install_message:
72
+ rdoc_options:
73
+ - --charset=UTF-8
74
+ require_paths:
75
+ - lib
76
+ required_ruby_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: "0"
81
+ version:
82
+ required_rubygems_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: "0"
87
+ version:
88
+ requirements: []
89
+
90
+ rubyforge_project:
91
+ rubygems_version: 1.3.5
92
+ signing_key:
93
+ specification_version: 3
94
+ summary: A quick and easy DSL for generating Jabber bots
95
+ test_files:
96
+ - spec/botfly_spec.rb
97
+ - spec/spec_helper.rb