Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
git-svn-id: http://code.macournoyer.com/svn/scout/trunk@266 ab7993de-5426-0410-a80b-baa00616f331
- Loading branch information
macournoyer
committed
Oct 16, 2007
0 parents
commit be377d6
Showing
25 changed files
with
2,205 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
Copyright (c) 2007 macournoyer | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining | ||
a copy of this software and associated documentation files (the | ||
"Software"), to deal in the Software without restriction, including | ||
without limitation the rights to use, copy, modify, merge, publish, | ||
distribute, sublicense, and/or sell copies of the Software, and to | ||
permit persons to whom the Software is furnished to do so, subject to | ||
the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be | ||
included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | ||
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | ||
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | ||
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Chat bot for Campfire (http://campfirenow.com) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
require 'config/requirements' | ||
require 'config/hoe' # setup Hoe + all gem configuration | ||
|
||
Dir['tasks/**/*.rake'].each { |rake| load rake } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
#!/usr/bin/env ruby | ||
# | ||
# Created by macournoyer on 2007-10-10. | ||
# Copyright (c) 2007. All rights reserved. | ||
|
||
begin | ||
require 'rubygems' | ||
rescue LoadError | ||
# no rubygems to load, so we fail silently | ||
end | ||
|
||
require 'optparse' | ||
require File.dirname(__FILE__) + '/../lib/scout' | ||
|
||
# NOTE: the option -p/--path= is given as an example, and should probably be replaced in your application. | ||
|
||
OPTIONS = { | ||
:subdomain => nil, | ||
:email => nil, | ||
:password => nil, | ||
:room => nil, | ||
:name => 'bot' | ||
} | ||
MANDATORY_OPTIONS = %w( subdomain email password room ) | ||
|
||
parser = OptionParser.new do |opts| | ||
opts.banner = <<BANNER | ||
Scout Chat Bot | ||
Usage: #{File.basename($0)} [options] | ||
Options are: | ||
BANNER | ||
opts.separator "" | ||
opts.on("-s", "--subdomain=SUBDOMAIN", String, | ||
"The Campfire subdomain") { |OPTIONS[:subdomain]| } | ||
opts.on("-e", "--email=EMAIL", String, | ||
"Email used to login") { |OPTIONS[:email]| } | ||
opts.on("-p", "--password=PASSWORD", String, | ||
"Password used to login") { |OPTIONS[:password]| } | ||
opts.on("-r", "--room=ROOM", String, | ||
"Name of the room to monitor") { |OPTIONS[:room]| } | ||
opts.on("-n", "--name=NAME", String, | ||
"Name of the bot") { |OPTIONS[:name]| } | ||
opts.on("-h", "--help", | ||
"Show this help message.") { puts opts; exit } | ||
opts.parse!(ARGV) | ||
|
||
if MANDATORY_OPTIONS && MANDATORY_OPTIONS.find { |option| OPTIONS[option.to_sym].nil? } | ||
puts opts; exit | ||
end | ||
end | ||
|
||
# do stuff | ||
campfire = Tinder::Campfire.new OPTIONS[:subdomain] | ||
campfire.login OPTIONS[:email], OPTIONS[:password] | ||
room = campfire.find_room_by_name OPTIONS[:room] | ||
|
||
bot = Scout::Bot.new(room, :name => OPTIONS[:name]) | ||
|
||
puts "Starting the bot, CTRL+C to stop ..." | ||
bot.listen! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
require 'scout/version' | ||
|
||
AUTHOR = 'macournoyer' # can also be an array of Authors | ||
EMAIL = "macournoyer@gmail.com" | ||
DESCRIPTION = "description of gem" | ||
GEM_NAME = 'scout' # what ppl will type to install your gem | ||
RUBYFORGE_PROJECT = 'scout' # The unix name for your project | ||
HOMEPATH = "http://#{RUBYFORGE_PROJECT}.rubyforge.org" | ||
DOWNLOAD_PATH = "http://rubyforge.org/projects/#{RUBYFORGE_PROJECT}" | ||
|
||
@config_file = "~/.rubyforge/user-config.yml" | ||
@config = nil | ||
RUBYFORGE_USERNAME = "unknown" | ||
def rubyforge_username | ||
unless @config | ||
begin | ||
@config = YAML.load(File.read(File.expand_path(@config_file))) | ||
rescue | ||
puts <<-EOS | ||
ERROR: No rubyforge config file found: #{@config_file} | ||
Run 'rubyforge setup' to prepare your env for access to Rubyforge | ||
- See http://newgem.rubyforge.org/rubyforge.html for more details | ||
EOS | ||
exit | ||
end | ||
end | ||
RUBYFORGE_USERNAME.replace @config["username"] | ||
end | ||
|
||
|
||
REV = nil | ||
# UNCOMMENT IF REQUIRED: | ||
# REV = `svn info`.each {|line| if line =~ /^Revision:/ then k,v = line.split(': '); break v.chomp; else next; end} rescue nil | ||
VERS = Scout::VERSION::STRING + (REV ? ".#{REV}" : "") | ||
RDOC_OPTS = ['--quiet', '--title', 'scout documentation', | ||
"--opname", "index.html", | ||
"--line-numbers", | ||
"--main", "README", | ||
"--inline-source"] | ||
|
||
class Hoe | ||
def extra_deps | ||
@extra_deps.reject! { |x| Array(x).first == 'hoe' } | ||
@extra_deps | ||
end | ||
end | ||
|
||
# Generate all the Rake tasks | ||
# Run 'rake -T' to see list of generated tasks (from gem root directory) | ||
hoe = Hoe.new(GEM_NAME, VERS) do |p| | ||
p.author = AUTHOR | ||
p.description = DESCRIPTION | ||
p.email = EMAIL | ||
p.summary = DESCRIPTION | ||
p.url = HOMEPATH | ||
p.rubyforge_name = RUBYFORGE_PROJECT if RUBYFORGE_PROJECT | ||
p.test_globs = ["test/**/test_*.rb"] | ||
p.clean_globs |= ['**/.*.sw?', '*.gem', '.config', '**/.DS_Store'] #An array of file patterns to delete on clean. | ||
|
||
# == Optional | ||
p.changes = p.paragraphs_of("History.txt", 0..1).join("\\n\\n") | ||
#p.extra_deps = [] # An array of rubygem dependencies [name, version], e.g. [ ['active_support', '>= 1.3.1'] ] | ||
|
||
#p.spec_extras = {} # A hash of extra values to set in the gemspec. | ||
|
||
end | ||
|
||
CHANGES = hoe.paragraphs_of('History.txt', 0..1).join("\\n\\n") | ||
PATH = (RUBYFORGE_PROJECT == GEM_NAME) ? RUBYFORGE_PROJECT : "#{RUBYFORGE_PROJECT}/#{GEM_NAME}" | ||
hoe.remote_rdoc_dir = File.join(PATH.gsub(/^#{RUBYFORGE_PROJECT}\/?/,''), 'rdoc') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
require 'fileutils' | ||
include FileUtils | ||
|
||
require 'rubygems' | ||
%w[rake hoe newgem rubigen tinder active_support].each do |req_gem| | ||
begin | ||
require req_gem | ||
rescue LoadError | ||
puts "This Rakefile requires the '#{req_gem}' RubyGem." | ||
puts "Installation: gem install #{req_gem} -y" | ||
exit | ||
end | ||
end | ||
|
||
$:.unshift(File.join(File.dirname(__FILE__), %w[.. lib])) | ||
|
||
require 'scout' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
$:.unshift File.dirname(__FILE__) | ||
|
||
require 'rubygems' | ||
require 'tinder' | ||
require 'active_support' | ||
require 'scout/version' | ||
require 'scout/bot' | ||
require 'scout/command' | ||
|
||
Dir[File.dirname(__FILE__) + '/scout/commands/*.rb'].each { |f| require "scout/commands/#{File.basename(f, '.rb')}" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
module Scout | ||
class InvalidRoom < StandardError; end | ||
|
||
class Bot | ||
attr_accessor :name, :room | ||
|
||
def initialize(room, options={}) | ||
raise InvalidRoom, 'You need to supply a Tinder::Room object' unless room.is_a? Tinder::Room | ||
@room = room | ||
@name = options[:name] || 'bot' | ||
@sleep = options[:sleep] || 1 | ||
@continue = true | ||
end | ||
|
||
def listen! | ||
trap("INT") do | ||
puts "Stoping ..." | ||
@continue = false | ||
@room.leave | ||
end | ||
|
||
while @continue | ||
@room.join | ||
process_commands | ||
sleep @sleep | ||
end | ||
end | ||
|
||
def process_commands | ||
fetch_commands.each do |command| | ||
command.process if command | ||
end | ||
end | ||
|
||
def fetch_commands | ||
@room.listen.collect do |message| | ||
Command.parse(message, self) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
module Scout | ||
class Command | ||
attr_reader :from, :bot, :command, :args | ||
|
||
class << self | ||
def parse(message, bot) | ||
if tokens = tokenize!(message, bot) | ||
person, command, args = *tokens | ||
command_class = begin | ||
# TODO make this more secure | ||
"Scout::Commands::#{command.classify}".constantize | ||
rescue NameError => e | ||
Scout::Commands::Invalid | ||
end | ||
|
||
command_class.new(person, bot, command, args) | ||
end | ||
end | ||
|
||
def tokenize!(message, bot) | ||
full, command, args = message[:message].match(/^@#{bot.name} (\w+)\s?((?:.+\s?)*)$/i).to_a | ||
[message[:person], command, args.split] if command | ||
end | ||
end | ||
|
||
def initialize(from, bot, command, args) | ||
@from = from | ||
@bot = bot | ||
@command = command | ||
@args = args | ||
end | ||
|
||
def process | ||
end | ||
|
||
def room | ||
@bot.room | ||
end | ||
|
||
def speak(message) | ||
@bot.room.speak message | ||
end | ||
|
||
def paste(message) | ||
@bot.room.paste message | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
require 'open-uri' | ||
|
||
module Scout | ||
module Commands | ||
class Define < Scout::Command | ||
def process | ||
url = "http://www.google.com/search?q=define:#{args.join('+')}" | ||
out = open(url).read.scan(/<li>(.*?)<\li>/m).flatten | ||
out.each { |s| s.gsub!(/<.*?>/m, ' ') } | ||
|
||
speak "Definitions from #{url} :" | ||
speak '-None-' if out.empty? | ||
out.each do |definition| | ||
speak definition | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
module Scout | ||
module Commands | ||
class Help < Scout::Command | ||
def process | ||
paste "=== Scout Chat Bot ===\n" + | ||
"To send a command type:\n" + | ||
" @#{bot.name} command [arguments]\n\n" + | ||
"Available commands:\n" + | ||
" say text (speak throught the bot)\n" + | ||
" vote start|stop|+1|-1 (count votes)\n" + | ||
" define word (search for definitions)\n" + | ||
" help (show this help message)" | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
module Scout | ||
module Commands | ||
class Invalid < Scout::Command | ||
def process | ||
speak "Invalid command : #{command}" | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
module Scout | ||
module Commands | ||
class Say < Scout::Command | ||
def process | ||
speak args.join(' ') | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.