Permalink
Browse files

FIRST

  • Loading branch information...
0 parents commit 778597b3e393e01e14fec94142c4cd94dd59c178 @nakajima committed May 13, 2011
Showing with 311 additions and 0 deletions.
  1. +1 −0 .gitignore
  2. +3 −0 Gemfile
  3. +30 −0 Gemfile.lock
  4. +48 −0 README.md
  5. +26 −0 lib/poundie.rb
  6. +49 −0 lib/poundie/campfire/message.rb
  7. +33 −0 lib/poundie/campfire/user.rb
  8. +51 −0 lib/poundie/plugin.rb
  9. +15 −0 lib/poundie/plugins/greeter.rb
  10. +55 −0 lib/poundie/runner.rb
@@ -0,0 +1 @@
+test.rb
@@ -0,0 +1,3 @@
+source :rubygems
+
+gem "tinder"
@@ -0,0 +1,30 @@
+GEM
+ remote: http://rubygems.org/
+ specs:
+ activesupport (3.0.7)
+ addressable (2.2.6)
+ eventmachine (0.12.10)
+ faraday (0.5.7)
+ addressable (~> 2.2.4)
+ multipart-post (~> 1.1.0)
+ rack (>= 1.1.0, < 2)
+ mime-types (1.16)
+ multipart-post (1.1.1)
+ rack (1.2.2)
+ simple_oauth (0.1.5)
+ tinder (1.4.4)
+ activesupport
+ eventmachine
+ faraday (~> 0.5.1)
+ mime-types
+ multipart-post
+ twitter-stream
+ twitter-stream (0.1.13)
+ eventmachine (~> 0.12.8)
+ simple_oauth (~> 0.1.4)
+
+PLATFORMS
+ ruby
+
+DEPENDENCIES
+ tinder
@@ -0,0 +1,48 @@
+-# POUNDIE #-
+
+Poundie is a Campfire bot.
+
+Here's how you use it:
+
+ require "rubygems"
+ require "poundie"
+
+ # Specify which plugins to use
+ Poundie.use :greeter
+
+ # Start listening
+ Poundie.start
+
+Poundie ships with one built-in plugin, called `:greeter`. Here's how a
+plugin works:
+
+ # Your plugin needs inherit from Poundie::Plugin
+ class Greeter < Poundie::Plugin
+
+ # Next, register your plugin. This will be used to activate it.
+ register :greeter
+
+ # This specifies which messages should trigger the plugin's behavior
+ match do |message|
+ message.body == "greet"
+ end
+
+ # This specifies the plugin's behavior
+ action do |message|
+ speak "Hello, #{message.user.name}"
+ end
+ end
+
+Pretty easy! The message that gets passed to `match` and `action` is:
+
+ message.body # text of the message
+ message.type # TextMessage, PasteMessage, SoundMessage, or TweetMessage
+ message.text? # message is text?
+ message.paste? # message is paste?
+ message.sound? # message is sound?
+ message.tweet? # message is tweet?
+ message.created_at # when the message was posted
+ message.user.name # name of the user who posted the message
+ message.user.email # email address of the user
+ message.user.admin? # is the user an admin?
+ message.user.created_at # when the user was created
@@ -0,0 +1,26 @@
+$LOAD_PATH << File.dirname(__FILE__)
+
+require "tinder"
+require "yaml"
+require "readline"
+
+require "poundie/runner"
+require "poundie/plugin"
+require "poundie/campfire/user"
+require "poundie/campfire/message"
+require "poundie/plugins/greeter"
+
+trap('INT') { puts "ok bye!" ; exit! }
+
+module Poundie
+ def self.start(name="default")
+ name.gsub!(/\W/, '_')
+ runner = Runner.new(name)
+ runner.run
+ end
+
+ def self.use(plugin)
+ puts "Using #{plugin}"
+ Poundie::Plugin.active << Poundie::Plugin.list[plugin]
+ end
+end
@@ -0,0 +1,49 @@
+module Poundie
+ module Campfire
+ class Message
+ def initialize(hash)
+ @hash = hash
+ end
+
+ def id
+ @hash[:id]
+ end
+
+ def body
+ @hash[:body]
+ end
+
+ def room_id
+ @hash[:room_id]
+ end
+
+ def created_at
+ @hash[:created_at]
+ end
+
+ def user
+ Poundie::Campfire::User.new(@hash[:user])
+ end
+
+ def type
+ @hash[:type]
+ end
+
+ def text?
+ type == "TextMessage"
+ end
+
+ def paste?
+ type == "PasteMessage"
+ end
+
+ def sound?
+ type == "SoundMessage"
+ end
+
+ def tweet?
+ type == "TweetMessage"
+ end
+ end
+ end
+end
@@ -0,0 +1,33 @@
+module Poundie
+ module Campfire
+ class User
+ def initialize(hash)
+ @hash = hash
+ end
+
+ def id
+ @hash[:id]
+ end
+
+ def name
+ @hash[:name]
+ end
+
+ def email_address
+ @hash[:email_address]
+ end
+
+ def admin?
+ @hash[:admin]
+ end
+
+ def created_at
+ @hash[:admin]
+ end
+
+ def type
+ @hash[:type]
+ end
+ end
+ end
+end
@@ -0,0 +1,51 @@
+module Poundie
+ class Plugin
+ def self.list
+ @list ||= {}
+ end
+
+ def self.active
+ @active ||= []
+ end
+
+ def self.register(name)
+ puts "Registering #{name}: #{self}"
+ Poundie::Plugin.list[name] = self
+ end
+
+ def self.match(&block)
+ define_method(:match?, &block)
+ end
+
+ def self.action(&block)
+ define_method(:perform, &block)
+ end
+
+ def initialize(room)
+ @room = room
+ end
+
+ def call(message)
+ message = Poundie::Campfire::Message.new(message)
+ match?(message) && perform(message)
+ end
+
+ private
+
+ def speak(msg)
+ @room.speak(msg)
+ end
+
+ def paste(msg)
+ @room.paste(msg)
+ end
+
+ def tweet(url)
+ @room.tweet(url)
+ end
+
+ def play(sound)
+ @room.play(sound)
+ end
+ end
+end
@@ -0,0 +1,15 @@
+module Poundie
+ module Plugins
+ class Greeter < Poundie::Plugin
+ register :greeter
+
+ match do |message|
+ message.body == "greet"
+ end
+
+ action do |message|
+ speak "Hello, #{message.user.name}"
+ end
+ end
+ end
+end
@@ -0,0 +1,55 @@
+module Poundie
+ class Runner
+ CONFIG_FILE = File.join(ENV["HOME"], ".poundie_")
+
+ def initialize(name)
+ @name = name
+ configure
+ end
+
+ def run
+ trap('INT') { exit! }
+ room.listen do |line|
+ Thread.new do
+ plugins.each { |plugin|
+ plugin.call(line)
+ }
+ end
+ end
+ end
+
+ private
+
+ def plugins
+ @plugins ||= Poundie::Plugin.active.map do |plugin|
+ plugin.new(room)
+ end
+ end
+
+ def room
+ @room ||= campfire.rooms.detect { |r| r.name == @room_name }
+ end
+
+ def campfire
+ @campfire ||= Tinder::Campfire.new(@subdomain, :token => @token)
+ end
+
+ def configure
+ if File.exists?(CONFIG_FILE + @name)
+ @subdomain, @token, @room_name = YAML.load_file(CONFIG_FILE + @name)
+ else
+ puts "Configuring..."
+ @subdomain = ask("Enter Campfire Subdomain: ")
+ @token = ask("Enter Campfire Token: ")
+ @room_name = ask("Enter Campfire Room Name: ")
+ File.open(CONFIG_FILE + @name, "w+") { |file|
+ YAML.dump([@subdomain, @token, @room_name], file)
+ }
+ end
+ end
+
+ def ask(msg)
+ Readline.readline(msg)
+ end
+ end
+end

0 comments on commit 778597b

Please sign in to comment.