Top Level Namespace

Defined Under Namespace

Modules: ScreenXTV

Constant Summary collapse

HOST =
"screenx.tv"

Instance Method Summary collapse

Instance Method Details

#auth(conf) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/screenxtv.rb', line 135

def auth(conf)
  loop do
    username=readline "username> "
    return false if username.size==0
    password=readpswd "password> "
    return false if password.nil? || password.size==0

    socket=kvconnect HOST,8000
    socket.send('init',{user:username,password:password}.to_json)
    key,value=socket.recv
    if key=='auth'
      conf['user']=username
      conf['auth_key']=value
      return true
    end
  end
end

#kvconnect(host, port) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/screenxtv.rb', line 104

def kvconnect(host,port)
  socket=TCPSocket.open host, port
  class << socket
    def init_mutex
      @mutex=Mutex.new
    end
    def send(key,value)
      @mutex.synchronize{
        keylen=key.bytesize
        vallen=value.bytesize
        self.write keylen.chr
        self.write key
        self.write (vallen>>8).chr+(vallen&0xff).chr
        self.write value
      }
    end
    def recv
      [self.readline.chop,JSON.parse("["+self.readline+"]")[0]]
    end
  end
  socket.init_mutex
  socket
end

#readline(prompt = "> ") ⇒ Object



69
70
71
72
73
# File 'lib/screenxtv.rb', line 69

def readline(prompt="> ")
  s=Readline.readline(prompt,true)
  if !s then exit end
  s.strip
end

#readpswd(prompt = '> ') ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/screenxtv.rb', line 74

def readpswd(prompt='> ')
  print prompt
  STDIN.raw{
    s=""
    loop do
      c=STDIN.getch
      case c
      when "\x03"
        print "\r\n"
        return nil
      when "\r","\n"
        print "\r\n"
        return s
      when "\x7f"
        if s.length>0
          s=s.slice 0,s.length-1
        else
          print "\a"
        end
      when 'a'..'z','A'..'Z','0'..'9','_'
        s+=c
      else
        print "\a"
      end
      print "\r\e[K#{prompt}#{s.gsub /./,'*'}" # delete this
    end
  }
end

#show_info(info) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/screenxtv.rb', line 47

def show_info(info)
  broadcasting_url="http://#{HOST}/#{info['url']}"
  private_flag=!!info['private']
  authorized=info['authorized']
  print "Broadcasting URL: \e[1m#{broadcasting_url}\e[m\n"
  print "Chat page       : \e[1m#{broadcasting_url}?chat\e[m\n"
  if info['private']
    print "This is a private casting.\n"
    print "The only person who knows the URL can watch this screen.\n"
  elsif !info['authorized']
    print "This URL is not reserved and chat messages will be deleted after broadcasting.\n";
    print "If you want to reserve this URL, please create your account.\n"
  end
end

#showHelpObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/screenxtv.rb', line 27

def showHelp
  print <<EOS
Usage:
  screenxtv [options]

Options:
  -u, [--url]      # Select a url (e.g. yasulab, tompng)
  -c, [--color]    # Select a color (options: black/white/green/novel)
  -t, [--title]    # Select a title (e.g. Joe's Codestream)
  -r, [--reset]    # Reset your default configuration (e.g. url, color, title)
  -f CONFIG_FILE   # Path to a preset configuration
  -e, [--execute]  # Execute specified Program
  -p, [--private]  # Broadcast your terminal privately (anyone who has the link can access)
  -h, [--help]     # Show this help message and quit
  -v, [--version]  # Show ScreenX TV Ruby Client version number and quit
EOS
  exit
end

#showVersionObject



23
24
25
26
# File 'lib/screenxtv.rb', line 23

def showVersion
  print "ScreenX TV Ruby Client #{ScreenXTV::VERSION}\n" #is there any good way to do this?
  exit
end

#stop(msg) ⇒ Object



128
129
130
131
132
133
# File 'lib/screenxtv.rb', line 128

def stop msg
  height,width=STDOUT.winsize
  print "\e[?1l\e[>\e[1;#{height}r\e[#{height};1H\e[K"
  print msg+"\r\n"
  exit
end