Skip to content

Instantly share code, notes, and snippets.

@aitor
Forked from macournoyer/lighter.rb
Created June 17, 2009 08:26
Show Gist options
  • Save aitor/131146 to your computer and use it in GitHub Desktop.
Save aitor/131146 to your computer and use it in GitHub Desktop.
# Lighter -- Campfire from the command line
# usage: ruby lighter.rb subdomain "Main Room" macournoyer@gmail
require "rubygems"
require "tinder"
require "readline"
require "highline/import"
class Lighter
def initialize(room)
@room = room
@continue = true
@message_ids = []
@sleep = 1
end
def start
trap("INT") { input }
@room.join
puts "Now in room, CTRL+C to input, input /help for help"
process while @continue
end
def stop(leave=false)
@continue = false
@room.leave if leave
end
def input
print "\b\b"
cmd = Readline::readline("> ")
return if cmd.empty?
Readline::HISTORY.push(cmd)
if cmd[0] == ?/
case cmd
when "/exit"
stop
when "/leave"
stop(true)
when "/help"
puts "commands: exit, leave, help"
end
else
@room.speak cmd
end
end
private
def process
@room.listen.reject { |m| @message_ids.include?(m[:id]) }.each do |message|
puts message[:person] + ": " + message[:message]
@message_ids << message[:id]
end
sleep @sleep
rescue Timeout::Error
puts "Timeout while listening: #{$!}, rejoining room ..."
@room.join(true)
rescue Exception
puts "Error while listening: #{$!}"
puts $@
end
end
abort "usage: #{$0} <subdomain> <room name> <email>" unless ARGV.size == 3
subdomain, room, email = ARGV
password = ask("Password: ") { |q| q.echo = false }
campfire = Tinder::Campfire.new(subdomain)
campfire.login(email, password)
room = campfire.find_room_by_name(room)
Lighter.new(room).start
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment