Class: SiriProxy::CommandLine

Inherits:
Object
  • Object
show all
Defined in:
lib/siriproxy/command_line.rb

Constant Summary collapse

<<-EOS
Siri Proxy is a proxy server for Apple's Siri "assistant." The idea is to allow for the creation of custom handlers for different actions. This can allow developers to easily add functionality to Siri.

See: http://github.com/plamoni/SiriProxy/

Usage: siriproxy COMMAND OPTIONS

Commands:
server            Start up the Siri proxy server
gencerts          Generate a the certificates needed for SiriProxy
bundle            Install any dependancies needed by plugins
console           Launch the plugin test console 
update [dir]      Updates to the latest code from GitHub or from a provided directory
help              Show this usage information

Options:
  Option                           Command       Description
EOS

Instance Method Summary collapse

Constructor Details

#initializeCommandLine

Returns a new instance of CommandLine.



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/siriproxy/command_line.rb', line 33

def initialize
  @branch = nil
  parse_options
  command     = ARGV.shift
  subcommand  = ARGV.shift
  case command
  when 'server'           then run_server(subcommand)
  when 'gencerts'         then gen_certs
  when 'bundle'           then run_bundle(subcommand)
  when 'console'          then run_console
  when 'update'           then update(subcommand)
  when 'help'             then usage
  when 'dnsonly'          then dns
  else                    usage
  end
end

Instance Method Details

#dnsObject



146
147
148
149
150
151
# File 'lib/siriproxy/command_line.rb', line 146

def dns
  require 'siriproxy/dns'
  $APP_CONFIG.use_dns = true
  server = SiriProxy::Dns.new
  server.run(Logger::DEBUG)
end

#gen_certsObject



110
111
112
113
114
115
# File 'lib/siriproxy/command_line.rb', line 110

def gen_certs
  ca_name = @ca_name ||= ""
  command = File.join(File.dirname(__FILE__), '..', "..", "scripts", 'gen_certs.sh')
  sp_root = File.join(File.dirname(__FILE__), '..', "..")
  puts `#{command} "#{sp_root}" "#{ca_name}"`
end

#run_bundle(subcommand = '') ⇒ Object



82
83
84
85
# File 'lib/siriproxy/command_line.rb', line 82

def run_bundle(subcommand='')
  setup_bundler_path
  puts `bundle #{subcommand} #{ARGV.join(' ')}`
end

#run_consoleObject



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
75
76
77
78
79
80
# File 'lib/siriproxy/command_line.rb', line 50

def run_console
  load_code
  init_plugins

  # this is ugly, but works for now
  SiriProxy::PluginManager.class_eval do
    def respond(text, options={})
      puts "=> #{text}"
    end
    def process(text)
      super(text)
    end
    def send_request_complete_to_iphone
    end
    def no_matches
      puts "No plugin responded"
    end
  end
  SiriProxy::Plugin.class_eval do
    def last_ref_id
      0
    end
    def send_object(object, options={:target => :iphone})
      puts "=> #{object}"
    end
  end

  cora = SiriProxy::PluginManager.new
  repl = -> prompt { print prompt; cora.process(gets.chomp!) }
  loop { repl[">> "] }
end

#run_server(subcommand = 'start') ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/siriproxy/command_line.rb', line 87

def run_server(subcommand='start')
  load_code
  init_plugins
  start_server
  # @todo: support for forking server into bg and start/stop/restart
  # subcommand ||= 'start'
  # case subcommand
  # when 'start'    then start_server
  # when 'stop'     then stop_server
  # when 'restart'  then restart_server
  # end
end

#start_serverObject



100
101
102
103
104
105
106
107
108
# File 'lib/siriproxy/command_line.rb', line 100

def start_server
  if $APP_CONFIG.server_ip
    require 'siriproxy/dns'
    dns_server = SiriProxy::Dns.new
    dns_server.start()
  end
  proxy = SiriProxy.new
  proxy.start()
end

#update(directory = nil) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/siriproxy/command_line.rb', line 117

def update(directory=nil)
  if(directory)
    puts "=== Installing from '#{directory}' ==="
    puts `cd #{directory} && rake install`
    puts "=== Bundling ===" if $?.exitstatus == 0
    puts `siriproxy bundle` if $?.exitstatus == 0
    puts "=== SUCCESS ===" if $?.exitstatus == 0
    
    exit $?.exitstatus
  else
    branch_opt = @branch ? "-b #{@branch}" : ""
    @branch = "master" if @branch == nil
    puts "=== Installing latest code from git://github.com/plamoni/SiriProxy.git [#{@branch}] ==="

 tmp_dir = "/tmp/SiriProxy.install." + (rand 9999).to_s.rjust(4, "0")

 `mkdir -p #{tmp_dir}`
    puts `git clone #{branch_opt} git://github.com/plamoni/SiriProxy.git #{tmp_dir}`  if $?.exitstatus == 0
    puts "=== Performing Rake Install ===" if $?.exitstatus == 0
    puts `cd #{tmp_dir} && rake install`  if $?.exitstatus == 0
    puts "=== Bundling ===" if $?.exitstatus == 0
    puts `siriproxy bundle`  if $?.exitstatus == 0
    puts "=== Cleaning Up ===" and puts `rm -rf #{tmp_dir}` if $?.exitstatus == 0
    puts "=== SUCCESS ===" if $?.exitstatus == 0

    exit $?.exitstatus
  end 
end

#usageObject



153
154
155
# File 'lib/siriproxy/command_line.rb', line 153

def usage
  puts "\n#{@option_parser}\n"
end