-
-
Save jnewland/131041 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#! /usr/bin/env ruby | |
# Lighter -- Campfire from the command line | |
# usage: ruby lighter.rb subdomain "Main Room" macournoyer@gmail ssl | |
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> <ssl>" unless ARGV.size >= 3 | |
subdomain, room, email, ssl = ARGV | |
password = ask("Password: ") { |q| q.echo = false } | |
campfire = Tinder::Campfire.new(subdomain, :ssl => !!ssl) | |
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