happy_fun_time_bot 0.1.0

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/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --debug
data/Gemfile ADDED
@@ -0,0 +1,13 @@
1
+ source "http://rubygems.org"
2
+ gem 'ruby-debug'
3
+ gem 'xmpp4r'
4
+ gem 'json'
5
+ gem 'httparty'
6
+
7
+ group :development do
8
+ gem "rspec", "~> 2.3.0"
9
+ gem "yard", "~> 0.6.0"
10
+ gem "bundler", "~> 1.0.0"
11
+ gem "jeweler", "~> 1.6.0"
12
+ gem "rcov", ">= 0"
13
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,46 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ columnize (0.3.2)
5
+ crack (0.1.8)
6
+ diff-lcs (1.1.2)
7
+ git (1.2.5)
8
+ httparty (0.7.7)
9
+ crack (= 0.1.8)
10
+ jeweler (1.6.0)
11
+ bundler (~> 1.0.0)
12
+ git (>= 1.2.5)
13
+ rake
14
+ json (1.5.1)
15
+ linecache (0.43)
16
+ rake (0.9.0)
17
+ rcov (0.9.9)
18
+ rspec (2.3.0)
19
+ rspec-core (~> 2.3.0)
20
+ rspec-expectations (~> 2.3.0)
21
+ rspec-mocks (~> 2.3.0)
22
+ rspec-core (2.3.1)
23
+ rspec-expectations (2.3.0)
24
+ diff-lcs (~> 1.1.2)
25
+ rspec-mocks (2.3.0)
26
+ ruby-debug (0.10.4)
27
+ columnize (>= 0.1)
28
+ ruby-debug-base (~> 0.10.4.0)
29
+ ruby-debug-base (0.10.4)
30
+ linecache (>= 0.3)
31
+ xmpp4r (0.5)
32
+ yard (0.6.8)
33
+
34
+ PLATFORMS
35
+ ruby
36
+
37
+ DEPENDENCIES
38
+ bundler (~> 1.0.0)
39
+ httparty
40
+ jeweler (~> 1.6.0)
41
+ json
42
+ rcov
43
+ rspec (~> 2.3.0)
44
+ ruby-debug
45
+ xmpp4r
46
+ yard (~> 0.6.0)
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Grant Ammons
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.
data/README.md ADDED
@@ -0,0 +1,67 @@
1
+ <h1>Happy fun time bot</h1>
2
+
3
+ Have happy fun times with this configurable XMPP bot! It is SO easy to use!
4
+
5
+ Lets take a look!
6
+
7
+ ```ruby
8
+ #!/usr/bin/env ruby
9
+
10
+ require 'rubygems'
11
+ require 'happy_fun_time_bot'
12
+
13
+ @bot = HappyFunTimeBot.new(:jid => "xxxx@chat.hipchat.com",
14
+ :nick => "HappyFunTime Bot",
15
+ :room => "123_your_talk_chan@conf.hipchat.com",
16
+ :password => "xxxx")
17
+
18
+ @bot.add_responder('heybot') do |from, args|
19
+ "Oh HAI #{from}!!!"
20
+ end
21
+
22
+ @bot.run!
23
+ ```
24
+
25
+ ```
26
+ Bob: !heybot what's up?
27
+ HappyFunTime Bot: Oh HAI Bob!!!
28
+ ```
29
+
30
+ ### So many IdeazzZz!!
31
+
32
+ **Add responders to insert random images of dogs in costumes!**
33
+
34
+ ```
35
+ Bob: !findimage dog costume
36
+ HappyFunTime Bot: Here ya go!
37
+ ```
38
+ ![](http://spoilurpets.com/images/Lobster%20Paws%20Dog%20Costume.JPG)
39
+
40
+
41
+ **Kick off a build!**
42
+
43
+ ```
44
+ Bob: !build_the_app
45
+ HappyFunTime Bot: All tests PASSED!
46
+ ```
47
+
48
+ ![](http://thehairpin.com/wp-content/uploads/2010/12/womanpic1001_228x342.jpeg)
49
+
50
+ **Deploy your app!**
51
+
52
+ ```
53
+ Bob: !deploy
54
+ HappyFunTime Bot: Deploying now!
55
+ ```
56
+
57
+ === Bot creation Options:
58
+
59
+ * `:jid` - Required.
60
+ * `:nick` - The nickname for the bot to use.
61
+ * `:room` - Required. The room to enter.
62
+ * `:password` - The bot's password.
63
+ * `:command_regex` - The regular expression to test for a command. The default is a ! followed by a word. e.g. `/^!(.+)$/`
64
+
65
+ == Copyright
66
+
67
+ Copyright (c) 2011 Grant Ammons. See LICENSE.txt for further details.
data/Rakefile ADDED
@@ -0,0 +1,42 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "happy_fun_time_bot"
18
+ gem.homepage = "http://github.com/gammons/happy_fun_time_bot"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{Simple XMPP bot framework}
21
+ gem.description = %Q{Easily create bots that can respond to anything!}
22
+ gem.email = "grant@pipelinedealsco.com"
23
+ gem.authors = ["Grant Ammons"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rspec/core'
29
+ require 'rspec/core/rake_task'
30
+ RSpec::Core::RakeTask.new(:spec) do |spec|
31
+ spec.pattern = FileList['spec/**/*_spec.rb']
32
+ end
33
+
34
+ RSpec::Core::RakeTask.new(:rcov) do |spec|
35
+ spec.pattern = 'spec/**/*_spec.rb'
36
+ spec.rcov = true
37
+ end
38
+
39
+ task :default => :spec
40
+
41
+ require 'yard'
42
+ YARD::Rake::YardocTask.new
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,65 @@
1
+ # We want the MUC functionality to just handle shit for us. Unfortunately we
2
+ # have to override/repeat the join method in order to add the directive that
3
+ # will disable the history being sent to us. :-(
4
+ module Jabber
5
+ module MUC
6
+ class MUCClient
7
+
8
+ def join(jid, password=nil)
9
+ if active?
10
+ raise "MUCClient already active"
11
+ end
12
+
13
+ @jid = (jid.kind_of?(JID) ? jid : JID.new(jid))
14
+ activate
15
+
16
+ # Joining
17
+ pres = Presence.new
18
+ pres.to = @jid
19
+ pres.from = @my_jid
20
+ xmuc = XMUC.new
21
+ xmuc.password = password
22
+ pres.add(xmuc)
23
+
24
+ # NOTE: Adding 'maxstanzas="0"' to 'history' subelement of xmuc nixes
25
+ # the history being sent to us when we join.
26
+ history = XMPPElement.new('history')
27
+ history.add_attributes({'maxstanzas' => '0'})
28
+ xmuc.add(history)
29
+
30
+ # We don't use Stream#send_with_id here as it's unknown
31
+ # if the MUC component *always* uses our stanza id.
32
+ error = nil
33
+ @stream.send(pres) { |r|
34
+ if from_room?(r.from) and r.kind_of?(Presence) and r.type == :error
35
+ # Error from room
36
+ error = r.error
37
+ true
38
+ # type='unavailable' may occur when the MUC kills our previous instance,
39
+ # but all join-failures should be type='error'
40
+ elsif r.from == jid and r.kind_of?(Presence) and r.type != :unavailable
41
+ # Our own presence reflected back - success
42
+ if r.x(XMUCUser) and (i = r.x(XMUCUser).items.first)
43
+ @affiliation = i.affiliation # we're interested in if it's :owner
44
+ @role = i.role # :moderator ?
45
+ end
46
+
47
+ handle_presence(r, false)
48
+ true
49
+ else
50
+ # Everything else
51
+ false
52
+ end
53
+ }
54
+
55
+ if error
56
+ deactivate
57
+ raise ServerError.new(error)
58
+ end
59
+
60
+ self
61
+ end
62
+
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,67 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'rubygems'
4
+ require 'xmpp4r'
5
+ require 'xmpp4r/muc/helper/simplemucclient'
6
+ require 'open-uri'
7
+ require 'cgi'
8
+ require 'httparty'
9
+ require 'ruby-debug'
10
+
11
+ $: << File.expand_path(File.dirname(__FILE__))
12
+
13
+ require 'responder'
14
+ require 'bot/muc_client'
15
+
16
+ class HappyFunTimeBot
17
+ attr_accessor :config, :client, :muc, :responders, :command_regexp
18
+
19
+ def initialize(config = {})
20
+ config = {:command_regex => /^!(.+)$/}.merge(config)
21
+ self.client = Jabber::Client.new(config[:jid])
22
+ self.muc = Jabber::MUC::SimpleMUCClient.new(client)
23
+ self.command_regexp = /^!(.+)$/
24
+ self.responders = []
25
+ self.config = config
26
+
27
+ Jabber.debug = true if Jabber.logger = config[:debug]
28
+ self
29
+ end
30
+
31
+ def connect
32
+ client.connect
33
+ client.auth(self.config[:password])
34
+ client.send(Jabber::Presence.new.set_type(:available))
35
+
36
+ salutation = config[:nick].split(/\s+/).first
37
+
38
+ muc.on_message { |time, nick, text| process(nick, text) }
39
+
40
+ muc.join(self.config[:room] + '/' + self.config[:nick])
41
+ self
42
+ end
43
+
44
+ def add_responder(command = '', &block)
45
+ responders << Responder.new(command, &block)
46
+ end
47
+
48
+ def run!
49
+ connect
50
+ loop { sleep 1 }
51
+ end
52
+
53
+ private
54
+
55
+ def process(from, command)
56
+ return [] unless command =~ self.config[:command_regex]
57
+ responders.select {|r| r.responds_to?($1) }.map do |responder|
58
+ ret = responder.block.call(from, command.split.shift)
59
+ send_response(ret)
60
+ ret
61
+ end
62
+ end
63
+
64
+ def send_response(msg)
65
+ muc.send Jabber::Message.new(muc.room, msg)
66
+ end
67
+ end
data/lib/responder.rb ADDED
@@ -0,0 +1,12 @@
1
+ class Responder
2
+ attr_accessor :command, :block
3
+
4
+ def initialize(command, &block)
5
+ @command = command
6
+ @block = block
7
+ end
8
+
9
+ def responds_to?(command)
10
+ @command == command.split.first
11
+ end
12
+ end
@@ -0,0 +1,39 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+ require 'ruby-debug'
3
+
4
+ describe "HappyFunTimeBot" do
5
+ it "should load everything properly" do
6
+ HappyFunTimeBot
7
+ lambda { HappyFunTimeBot.new }.should_not raise_error
8
+ end
9
+
10
+ describe "commands" do
11
+ it "should have a configurable command regex" do
12
+ @bot = HappyFunTimeBot.new(:command_regex => /^#(.+)$/)
13
+ @bot.stub(:send_response)
14
+ @bot.add_responder("findimage") { |from, args| "finding an image" }
15
+ @bot.send(:process, "bob", "#findimage monkey").should == ["finding an image"]
16
+ @bot.send(:process, "bob", "!findimage monkey").should == []
17
+ end
18
+ end
19
+
20
+ describe "responding" do
21
+ before(:each) do
22
+ @bot = HappyFunTimeBot.new
23
+ @bot.stub(:send_response)
24
+ end
25
+
26
+ it "should respond if there is a responder" do
27
+ @bot.add_responder("findimage") { |from, args| "finding an image" }
28
+ @bot.send(:process, "bob", "!findimage monkey").should == ["finding an image"]
29
+ end
30
+
31
+ it "should have multiple responses" do
32
+ @bot.add_responder("findimage") { |from, args| "finding an image" }
33
+ @bot.add_responder("google") { |from, args| "googling for you, #{from}." }
34
+
35
+ @bot.send(:process, "bob", "!findimage monkey").should == ["finding an image"]
36
+ @bot.send(:process, "bob", "!google monkey").should == ["googling for you, bob."]
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,12 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+ require 'rspec'
4
+ require 'happy_fun_time_bot'
5
+
6
+ # Requires supporting files with custom matchers and macros, etc,
7
+ # in ./support/ and its subdirectories.
8
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
9
+
10
+ RSpec.configure do |config|
11
+
12
+ end
metadata ADDED
@@ -0,0 +1,213 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: happy_fun_time_bot
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Grant Ammons
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-06-10 00:00:00 -04:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ prerelease: false
23
+ name: ruby-debug
24
+ type: :runtime
25
+ version_requirements: &id001 !ruby/object:Gem::Requirement
26
+ none: false
27
+ requirements:
28
+ - - ">="
29
+ - !ruby/object:Gem::Version
30
+ hash: 3
31
+ segments:
32
+ - 0
33
+ version: "0"
34
+ requirement: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ prerelease: false
37
+ name: xmpp4r
38
+ type: :runtime
39
+ version_requirements: &id002 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ hash: 3
45
+ segments:
46
+ - 0
47
+ version: "0"
48
+ requirement: *id002
49
+ - !ruby/object:Gem::Dependency
50
+ prerelease: false
51
+ name: json
52
+ type: :runtime
53
+ version_requirements: &id003 !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ hash: 3
59
+ segments:
60
+ - 0
61
+ version: "0"
62
+ requirement: *id003
63
+ - !ruby/object:Gem::Dependency
64
+ prerelease: false
65
+ name: httparty
66
+ type: :runtime
67
+ version_requirements: &id004 !ruby/object:Gem::Requirement
68
+ none: false
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ hash: 3
73
+ segments:
74
+ - 0
75
+ version: "0"
76
+ requirement: *id004
77
+ - !ruby/object:Gem::Dependency
78
+ prerelease: false
79
+ name: rspec
80
+ type: :development
81
+ version_requirements: &id005 !ruby/object:Gem::Requirement
82
+ none: false
83
+ requirements:
84
+ - - ~>
85
+ - !ruby/object:Gem::Version
86
+ hash: 3
87
+ segments:
88
+ - 2
89
+ - 3
90
+ - 0
91
+ version: 2.3.0
92
+ requirement: *id005
93
+ - !ruby/object:Gem::Dependency
94
+ prerelease: false
95
+ name: yard
96
+ type: :development
97
+ version_requirements: &id006 !ruby/object:Gem::Requirement
98
+ none: false
99
+ requirements:
100
+ - - ~>
101
+ - !ruby/object:Gem::Version
102
+ hash: 7
103
+ segments:
104
+ - 0
105
+ - 6
106
+ - 0
107
+ version: 0.6.0
108
+ requirement: *id006
109
+ - !ruby/object:Gem::Dependency
110
+ prerelease: false
111
+ name: bundler
112
+ type: :development
113
+ version_requirements: &id007 !ruby/object:Gem::Requirement
114
+ none: false
115
+ requirements:
116
+ - - ~>
117
+ - !ruby/object:Gem::Version
118
+ hash: 23
119
+ segments:
120
+ - 1
121
+ - 0
122
+ - 0
123
+ version: 1.0.0
124
+ requirement: *id007
125
+ - !ruby/object:Gem::Dependency
126
+ prerelease: false
127
+ name: jeweler
128
+ type: :development
129
+ version_requirements: &id008 !ruby/object:Gem::Requirement
130
+ none: false
131
+ requirements:
132
+ - - ~>
133
+ - !ruby/object:Gem::Version
134
+ hash: 15
135
+ segments:
136
+ - 1
137
+ - 6
138
+ - 0
139
+ version: 1.6.0
140
+ requirement: *id008
141
+ - !ruby/object:Gem::Dependency
142
+ prerelease: false
143
+ name: rcov
144
+ type: :development
145
+ version_requirements: &id009 !ruby/object:Gem::Requirement
146
+ none: false
147
+ requirements:
148
+ - - ">="
149
+ - !ruby/object:Gem::Version
150
+ hash: 3
151
+ segments:
152
+ - 0
153
+ version: "0"
154
+ requirement: *id009
155
+ description: Easily create bots that can respond to anything!
156
+ email: grant@pipelinedealsco.com
157
+ executables: []
158
+
159
+ extensions: []
160
+
161
+ extra_rdoc_files:
162
+ - LICENSE.txt
163
+ - README.md
164
+ files:
165
+ - .document
166
+ - .rspec
167
+ - Gemfile
168
+ - Gemfile.lock
169
+ - LICENSE.txt
170
+ - README.md
171
+ - Rakefile
172
+ - VERSION
173
+ - lib/bot/muc_client.rb
174
+ - lib/happy_fun_time_bot.rb
175
+ - lib/responder.rb
176
+ - spec/happy_fun_time_bot_spec.rb
177
+ - spec/spec_helper.rb
178
+ has_rdoc: true
179
+ homepage: http://github.com/gammons/happy_fun_time_bot
180
+ licenses:
181
+ - MIT
182
+ post_install_message:
183
+ rdoc_options: []
184
+
185
+ require_paths:
186
+ - lib
187
+ required_ruby_version: !ruby/object:Gem::Requirement
188
+ none: false
189
+ requirements:
190
+ - - ">="
191
+ - !ruby/object:Gem::Version
192
+ hash: 3
193
+ segments:
194
+ - 0
195
+ version: "0"
196
+ required_rubygems_version: !ruby/object:Gem::Requirement
197
+ none: false
198
+ requirements:
199
+ - - ">="
200
+ - !ruby/object:Gem::Version
201
+ hash: 3
202
+ segments:
203
+ - 0
204
+ version: "0"
205
+ requirements: []
206
+
207
+ rubyforge_project:
208
+ rubygems_version: 1.4.2
209
+ signing_key:
210
+ specification_version: 3
211
+ summary: Simple XMPP bot framework
212
+ test_files: []
213
+