jnewland (owner)

Revisions

gist: 131041 Download_button fork
public
Public Clone URL: git://gist.github.com/131041.git
Embed All Files: show embed
lighter.rb #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#! /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