roflbot 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1 @@
1
+ pkg/*
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Jeremy Stephens
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 ADDED
@@ -0,0 +1 @@
1
+ roflbot is an AIM bot.
@@ -0,0 +1,54 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "roflbot"
8
+ gem.summary = %Q{roflbot is a simple AIM bot}
9
+ gem.description = %Q{roflbot is a simple AIM bot, yo}
10
+ gem.email = "viking415@gmail.com"
11
+ gem.homepage = "http://github.com/viking/roflbot"
12
+ gem.authors = ["Jeremy Stephens"]
13
+ gem.add_dependency "net-toc"
14
+ gem.add_dependency "treetop"
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 'rake/testtask'
23
+ Rake::TestTask.new(:test) do |test|
24
+ test.libs << 'lib' << 'test'
25
+ test.pattern = 'test/**/test_*.rb'
26
+ test.verbose = true
27
+ end
28
+
29
+ begin
30
+ require 'rcov/rcovtask'
31
+ Rcov::RcovTask.new do |test|
32
+ test.libs << 'test'
33
+ test.pattern = 'test/**/test_*.rb'
34
+ test.verbose = true
35
+ end
36
+ rescue LoadError
37
+ task :rcov do
38
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
39
+ end
40
+ end
41
+
42
+ task :test => :check_dependencies
43
+
44
+ task :default => :test
45
+
46
+ require 'rake/rdoctask'
47
+ Rake::RDocTask.new do |rdoc|
48
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
49
+
50
+ rdoc.rdoc_dir = 'rdoc'
51
+ rdoc.title = "roflbot #{version}"
52
+ rdoc.rdoc_files.include('README*')
53
+ rdoc.rdoc_files.include('lib/**/*.rb')
54
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.0
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env ruby
2
+ require 'roflbot'
3
+ Roflbot::Runner.new
@@ -0,0 +1,13 @@
1
+ require 'forwardable'
2
+ require 'optparse'
3
+ require 'yaml'
4
+ require 'rubygems'
5
+ require 'net/toc'
6
+ require 'treetop'
7
+
8
+ module Roflbot
9
+ end
10
+
11
+ require File.dirname(__FILE__) + "/roflbot/base"
12
+ require File.dirname(__FILE__) + "/roflbot/sentence_bot"
13
+ require File.dirname(__FILE__) + "/roflbot/runner"
@@ -0,0 +1,57 @@
1
+ module Roflbot
2
+ class Base
3
+ class Expectation
4
+ attr_reader :response
5
+
6
+ def initialize(regexp)
7
+ @regexp = regexp
8
+ end
9
+
10
+ def matches?(message)
11
+ @regexp =~ message
12
+ end
13
+
14
+ def responds(message = nil, &block)
15
+ if block_given?
16
+ @response = block
17
+ else
18
+ @response = message
19
+ end
20
+ end
21
+ end
22
+
23
+ extend Forwardable
24
+ def initialize(username, password, options = {})
25
+ @username = username
26
+ @password = password
27
+ @options = options
28
+ @expectations = []
29
+
30
+ @client = Net::TOC.new(@username, @password)
31
+ @client.on_im { |m, b, a| on_im(m, b, a) }
32
+ end
33
+
34
+ def_delegator :@client, :connect
35
+ def_delegator :@client, :disconnect
36
+ def_delegator :@client, :wait
37
+
38
+ def expects(regexp)
39
+ exp = Expectation.new(regexp)
40
+ @expectations << exp
41
+ exp
42
+ end
43
+
44
+ def on_im(message, buddy, auto_response)
45
+ @expectations.each do |expectation|
46
+ next if !expectation.matches?(message)
47
+ case expectation.response
48
+ when Proc
49
+ buddy.send_im(expectation.response.call)
50
+ when String
51
+ buddy.send_im(expectation.response)
52
+ end
53
+ break
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,19 @@
1
+ module Roflbot
2
+ class Runner
3
+ def initialize(argv = ARGV)
4
+ username = nil
5
+ password = nil
6
+ options = {}
7
+ OptionParser.new do |opts|
8
+ opts.on("-u", "--username USERNAME") { |u| username = u }
9
+ opts.on("-p", "--password PASSWORD") { |p| password = p }
10
+ opts.on("-c", "--config FILENAME") { |c| options = YAML.load_file(c) }
11
+ end.parse!(argv)
12
+
13
+ bot = SentenceBot.new(username, password, options)
14
+ bot.connect
15
+ Signal.trap("INT") { bot.disconnect }
16
+ bot.wait
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,42 @@
1
+ grammar Sentence
2
+ rule phrase
3
+ token* {
4
+ def to_s(env = {})
5
+ elements.collect { |x| x.to_s(env) }.join
6
+ end
7
+ }
8
+ end
9
+
10
+ rule token
11
+ literal / variable
12
+ end
13
+
14
+ rule literal
15
+ (space / word / punctuation) {
16
+ def to_s(env = {})
17
+ text_value
18
+ end
19
+ }
20
+ end
21
+
22
+ rule variable
23
+ "(" word ")" {
24
+ def to_s(env = {})
25
+ arr = env[word.text_value]
26
+ arr[rand(arr.length)]
27
+ end
28
+ }
29
+ end
30
+
31
+ rule space
32
+ ' '+
33
+ end
34
+
35
+ rule word
36
+ [a-zA-Z0-9]+
37
+ end
38
+
39
+ rule punctuation
40
+ [,.!?]
41
+ end
42
+ end
@@ -0,0 +1,21 @@
1
+ Treetop.load(File.dirname(__FILE__) + "/sentence.tt")
2
+
3
+ module Roflbot
4
+ class SentenceBot < Base
5
+ def initialize(*args)
6
+ super
7
+ parser = SentenceParser.new
8
+ @sentences = []
9
+ @options.delete("sentences").each do |sentence|
10
+ node = parser.parse(sentence)
11
+ @sentences << node
12
+ end
13
+ self.expects(/^.*$/).responds { generated_message }
14
+ end
15
+
16
+ def generated_message
17
+ node = @sentences[rand(@sentences.length)]
18
+ node.to_s(@options)
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,68 @@
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{roflbot}
8
+ s.version = "0.0.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Jeremy Stephens"]
12
+ s.date = %q{2010-06-29}
13
+ s.default_executable = %q{roflbot}
14
+ s.description = %q{roflbot is a simple AIM bot, yo}
15
+ s.email = %q{viking415@gmail.com}
16
+ s.executables = ["roflbot"]
17
+ s.extra_rdoc_files = [
18
+ "LICENSE",
19
+ "README"
20
+ ]
21
+ s.files = [
22
+ ".gitignore",
23
+ "LICENSE",
24
+ "README",
25
+ "Rakefile",
26
+ "VERSION",
27
+ "bin/roflbot",
28
+ "lib/roflbot.rb",
29
+ "lib/roflbot/base.rb",
30
+ "lib/roflbot/runner.rb",
31
+ "lib/roflbot/sentence.tt",
32
+ "lib/roflbot/sentence_bot.rb",
33
+ "roflbot.gemspec",
34
+ "test/fixtures/bogus.yml",
35
+ "test/helper.rb",
36
+ "test/roflbot/test_base.rb",
37
+ "test/roflbot/test_runner.rb",
38
+ "test/roflbot/test_sentence_bot.rb"
39
+ ]
40
+ s.homepage = %q{http://github.com/viking/roflbot}
41
+ s.rdoc_options = ["--charset=UTF-8"]
42
+ s.require_paths = ["lib"]
43
+ s.rubygems_version = %q{1.3.6}
44
+ s.summary = %q{roflbot is a simple AIM bot}
45
+ s.test_files = [
46
+ "test/helper.rb",
47
+ "test/roflbot/test_sentence_bot.rb",
48
+ "test/roflbot/test_runner.rb",
49
+ "test/roflbot/test_base.rb"
50
+ ]
51
+
52
+ if s.respond_to? :specification_version then
53
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
54
+ s.specification_version = 3
55
+
56
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
57
+ s.add_runtime_dependency(%q<net-toc>, [">= 0"])
58
+ s.add_runtime_dependency(%q<treetop>, [">= 0"])
59
+ else
60
+ s.add_dependency(%q<net-toc>, [">= 0"])
61
+ s.add_dependency(%q<treetop>, [">= 0"])
62
+ end
63
+ else
64
+ s.add_dependency(%q<net-toc>, [">= 0"])
65
+ s.add_dependency(%q<treetop>, [">= 0"])
66
+ end
67
+ end
68
+
@@ -0,0 +1 @@
1
+ foo: bar
@@ -0,0 +1,29 @@
1
+ require 'test/unit'
2
+ require 'rubygems'
3
+ require 'mocha'
4
+ require 'delegate'
5
+ require 'ruby-debug'
6
+ require File.dirname(__FILE__) + "/../lib/roflbot"
7
+
8
+ class FakeClient < Mocha::Mock
9
+ attr_reader :on_im_block
10
+ def on_im(&block)
11
+ @on_im_block = block
12
+ end
13
+ end
14
+
15
+ class Test::Unit::TestCase
16
+ def setup
17
+ @client = FakeClient.new
18
+ @buddy = stub("Buddy")
19
+ Net::TOC.stubs(:new).returns(@client)
20
+ end
21
+
22
+ def receive_im(message, auto = false)
23
+ @client.on_im_block.call(message, @buddy, auto)
24
+ end
25
+
26
+ def fixture_filename(name)
27
+ File.dirname(__FILE__) + "/fixtures/#{name}"
28
+ end
29
+ end
@@ -0,0 +1,44 @@
1
+ require File.dirname(__FILE__) + "/../helper"
2
+
3
+ module Roflbot
4
+ class TestBase < Test::Unit::TestCase
5
+ def test_new
6
+ Net::TOC.expects(:new).with("roflbot", "password").returns(@client)
7
+ roflbot = Base.new("roflbot", "password")
8
+ end
9
+
10
+ def test_connect
11
+ roflbot = Base.new("roflbot", "password")
12
+ @client.expects(:connect)
13
+ roflbot.connect
14
+ end
15
+
16
+ def test_wait
17
+ roflbot = Base.new("roflbot", "password")
18
+ @client.expects(:wait)
19
+ roflbot.wait
20
+ end
21
+
22
+ def test_disconnect
23
+ roflbot = Base.new("roflbot", "password")
24
+ @client.expects(:disconnect)
25
+ roflbot.disconnect
26
+ end
27
+
28
+ def test_responds_with_string
29
+ roflbot = Base.new("roflbot", "password")
30
+ roflbot.expects(/^hey$/).responds("oh hai")
31
+
32
+ @buddy.expects(:send_im).with("oh hai")
33
+ receive_im("hey")
34
+ end
35
+
36
+ def test_responds_with_block
37
+ roflbot = Base.new("roflbot", "password")
38
+ roflbot.expects(/^hey$/).responds { "omg wtf" }
39
+
40
+ @buddy.expects(:send_im).with("omg wtf")
41
+ receive_im("hey")
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,11 @@
1
+ require File.dirname(__FILE__) + "/../helper"
2
+
3
+ module Roflbot
4
+ class TestRunner < Test::Unit::TestCase
5
+ def test_runs_sentence_bot
6
+ bot = mock("SentenceBot", :connect => nil, :wait => nil)
7
+ SentenceBot.expects(:new).with("foo", "bar", { "foo" => "bar" }).returns(bot)
8
+ Runner.new(%W{-u foo -p bar -c #{fixture_filename("bogus.yml")}})
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,22 @@
1
+ require File.dirname(__FILE__) + "/../helper"
2
+
3
+ module Roflbot
4
+ class TestSentenceBot < Test::Unit::TestCase
5
+ def test_base_superclass
6
+ assert_equal Base, SentenceBot.superclass
7
+ end
8
+
9
+ def test_responses
10
+ bot = SentenceBot.new('dudeguy', 'password', {
11
+ "sentences" => ["Hey (noun), (ending)"],
12
+ "noun" => %w{dude guy},
13
+ "ending" => ["sup?", "I love you."]
14
+ })
15
+
16
+ @buddy.expects(:send_im).times(20).with do |message|
17
+ message =~ /^Hey (dude|guy), (sup\?|I love you.)$/
18
+ end
19
+ 20.times { |_| receive_im("hey") }
20
+ end
21
+ end
22
+ end
metadata ADDED
@@ -0,0 +1,105 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: roflbot
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 0
9
+ version: 0.0.0
10
+ platform: ruby
11
+ authors:
12
+ - Jeremy Stephens
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-06-29 00:00:00 -05:00
18
+ default_executable: roflbot
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: net-toc
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 0
29
+ version: "0"
30
+ type: :runtime
31
+ version_requirements: *id001
32
+ - !ruby/object:Gem::Dependency
33
+ name: treetop
34
+ prerelease: false
35
+ requirement: &id002 !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ segments:
40
+ - 0
41
+ version: "0"
42
+ type: :runtime
43
+ version_requirements: *id002
44
+ description: roflbot is a simple AIM bot, yo
45
+ email: viking415@gmail.com
46
+ executables:
47
+ - roflbot
48
+ extensions: []
49
+
50
+ extra_rdoc_files:
51
+ - LICENSE
52
+ - README
53
+ files:
54
+ - .gitignore
55
+ - LICENSE
56
+ - README
57
+ - Rakefile
58
+ - VERSION
59
+ - bin/roflbot
60
+ - lib/roflbot.rb
61
+ - lib/roflbot/base.rb
62
+ - lib/roflbot/runner.rb
63
+ - lib/roflbot/sentence.tt
64
+ - lib/roflbot/sentence_bot.rb
65
+ - roflbot.gemspec
66
+ - test/fixtures/bogus.yml
67
+ - test/helper.rb
68
+ - test/roflbot/test_base.rb
69
+ - test/roflbot/test_runner.rb
70
+ - test/roflbot/test_sentence_bot.rb
71
+ has_rdoc: true
72
+ homepage: http://github.com/viking/roflbot
73
+ licenses: []
74
+
75
+ post_install_message:
76
+ rdoc_options:
77
+ - --charset=UTF-8
78
+ require_paths:
79
+ - lib
80
+ required_ruby_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ segments:
85
+ - 0
86
+ version: "0"
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ segments:
92
+ - 0
93
+ version: "0"
94
+ requirements: []
95
+
96
+ rubyforge_project:
97
+ rubygems_version: 1.3.6
98
+ signing_key:
99
+ specification_version: 3
100
+ summary: roflbot is a simple AIM bot
101
+ test_files:
102
+ - test/helper.rb
103
+ - test/roflbot/test_sentence_bot.rb
104
+ - test/roflbot/test_runner.rb
105
+ - test/roflbot/test_base.rb