Skip to content

Commit

Permalink
Initial import of the scout project
Browse files Browse the repository at this point in the history
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
Show file tree
Hide file tree
Showing 25 changed files with 2,205 additions and 0 deletions.
20 changes: 20 additions & 0 deletions License.txt
@@ -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.
1 change: 1 addition & 0 deletions README.txt
@@ -0,0 +1 @@
Chat bot for Campfire (http://campfirenow.com)
4 changes: 4 additions & 0 deletions Rakefile
@@ -0,0 +1,4 @@
require 'config/requirements'
require 'config/hoe' # setup Hoe + all gem configuration

Dir['tasks/**/*.rake'].each { |rake| load rake }
62 changes: 62 additions & 0 deletions bin/scout
@@ -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!
70 changes: 70 additions & 0 deletions config/hoe.rb
@@ -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')
17 changes: 17 additions & 0 deletions config/requirements.rb
@@ -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'
10 changes: 10 additions & 0 deletions lib/scout.rb
@@ -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')}" }
41 changes: 41 additions & 0 deletions lib/scout/bot.rb
@@ -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
48 changes: 48 additions & 0 deletions lib/scout/command.rb
@@ -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
19 changes: 19 additions & 0 deletions lib/scout/commands/define.rb
@@ -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
16 changes: 16 additions & 0 deletions lib/scout/commands/help.rb
@@ -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
9 changes: 9 additions & 0 deletions lib/scout/commands/invalid.rb
@@ -0,0 +1,9 @@
module Scout
module Commands
class Invalid < Scout::Command
def process
speak "Invalid command : #{command}"
end
end
end
end
9 changes: 9 additions & 0 deletions lib/scout/commands/say.rb
@@ -0,0 +1,9 @@
module Scout
module Commands
class Say < Scout::Command
def process
speak args.join(' ')
end
end
end
end

0 comments on commit be377d6

Please sign in to comment.