-
-
Save ericboehs/674939 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 # | |
# # | |
# Installation: # | |
# gem install tinder # | |
# chmod +x lighter # | |
# # | |
# Usage: # | |
# ./lighter <subdomain> <user> <room> # | |
# # | |
# Example: # | |
# ./lighter brightbit ericboehs "Awesome People Room" # | |
# # | |
# Commands: # | |
# /help shows list of commands # | |
# /leave parts the room (no /join yet) # | |
# /saveconfig saves configuration in ~/.lighter # | |
# /exit quits lighter # | |
# # | |
############################################################# | |
require "tinder" | |
require "readline" | |
require "highline/import" | |
require "yaml" | |
class Lighter | |
def initialize | |
unless ARGV.size == 3 | |
files = ['/etc/lighter.yml', ENV['HOME'] + '/.lighter/lighter.yml', './lighter.yml'] | |
files.each { |f| @file = f if File::exists?(f) } | |
@config = open(@file) { |f| YAML.load(f) } if @file | |
abort "usage: lighter <subdomain> <user> <room name>" unless @config | |
@subdomain = @config['subdomain'] | |
@user = @config['user'] | |
@pass = @config['pass'] | |
@room = @config['room'] | |
else | |
@subdomain, @user, @room = ARGV | |
@pass = ask("Password: ") { |q| q.echo = false } | |
end | |
@campfire = Tinder::Campfire.new @subdomain, :username => @user, :password => @pass | |
@room = @campfire.find_room_by_name(@room) | |
@continue = true | |
@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 | |
raise "exit" | |
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, saveconfig" | |
when "/saveconfig" | |
file = "#{ENV['HOME']}/.lighter/" | |
Dir::mkdir(file) unless File::exists?(file) | |
config = { | |
"subdomain" => @subdomain, | |
"user" => @user, | |
"pass" => @pass, | |
"room" => @room.name | |
} | |
open("#{ENV['HOME']}/.lighter/lighter.yml", 'w') {|f| YAML.dump(config, f)} | |
puts "Configuration saved to #{ENV['HOME']}/.lighter/lighter.yml." | |
puts "You may start lighter without any arguments now." | |
end | |
else | |
@room.speak cmd | |
end | |
end | |
private | |
def process | |
@room.listen do |message| | |
puts message[:user][:name] + ": " + message[:body] | |
end | |
rescue Timeout::Error | |
puts "Timeout while listening: #{$!}, rejoining room ..." | |
@room.join(true) | |
rescue "exit" | |
puts "Exiting..." | |
rescue Exception => e | |
unless e.message == "exit" | |
puts "Error while listening: #{$!}" | |
puts $@ | |
end | |
end | |
end | |
Lighter.new.start |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment