/usr/bin/sup-cmd is in sup-mail 0.12.1+git20120407.aaa852f-1+deb7u1.
This file is owned by root:root, with mode 0o755.
The actual contents of the file can be viewed below.
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 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 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 | #!/usr/bin/ruby1.9.1
#require 'rubygems'
require 'trollop'
require 'sup'
require 'sup/client'
require 'pp'
require 'yaml'
include Redwood
SUB_COMMANDS = %w(query count label add)
global_opts = Trollop::options do
#version = "sup-cmd (sup #{Redwood::VERSION})"
banner <<EOS
Connect to a running sup-server.
Usage:
sup-cmd [global options] command [options]
Valid commands: #{SUB_COMMANDS * ', '}
Global options:
EOS
opt :host, "server address", :type => :string, :default => 'localhost', :short => 'o'
opt :port, "server port", :type => :int, :default => 4300
opt :socket, "unix domain socket path", :type => :string, :default => nil
opt :verbose
conflicts :host, :socket
conflicts :port, :socket
stop_on SUB_COMMANDS
end
cmd = ARGV.shift
cmd_opts = case cmd
when "query"
Trollop.options do
opt :offset, "Offset", :default => 0, :type => :int
opt :limit, "Limit", :type => :int
opt :raw, "Retrieve raw message text", :default => false
end
when "count"
Trollop.options do
end
when "label"
Trollop.options do
opt :add_labels, "Labels to add", :default => ""
opt :remove_labels, "Labels to remove", :default => ""
end
when "add"
Trollop.options do
opt :labels, "Labels separated by commas", :default => ""
opt :mbox, "Treat input files as mboxes", :default => false
end
else
Trollop::die "unrecognized command #{cmd.inspect}"
end
class SupCmd < Redwood::Client
def initialize cmd, args, opts
@cmd = cmd
@opts = opts
@args = args
super()
end
def get_query
@args.first or fail "query argument required"
end
def connection_established
case @cmd
when "query"
query get_query, @opts[:offset], @opts[:limit], @opts[:raw] do |result|
if result
puts YAML.dump(result['summary'])
puts YAML.dump(result['raw']) if @opts[:raw]
else
close_connection
end
end
when "count"
count(get_query) do |x|
puts x
close_connection
end
when "label"
label get_query, @opts[:remove_labels].split(','), @opts[:add_labels].split(',') do
close_connection
end
when "add"
ARGF.binmode
labels = @opts[:labels].split(',')
get_message = lambda do
return ARGF.gets(nil) unless @opts[:mbox]
str = ""
l = ARGF.gets
str << l until ARGF.closed? || ARGF.eof? || MBox::is_break_line?(l = ARGF.gets)
str.empty? ? nil : str
end
i_s = i = 0
t = Time.now
while raw = get_message[]
i += 1
t_d = Time.now - t
if t_d >= 5
i_d = i - i_s
puts "indexed #{i} messages (#{i_d/t_d} m/s)" if global_opts[:verbose]
t = Time.now
i_s = i
end
add raw, labels do
close_connection
end
end
else
fail "#{@cmd} command unimplemented"
close_connection
end
end
def unbind
EM.stop
end
end
EM.run do
if global_opts[:socket]
EM.connect global_opts[:socket], SupCmd, cmd, ARGV, cmd_opts.merge(global_opts)
else
EM.connect global_opts[:host], global_opts[:port], SupCmd, cmd, ARGV, cmd_opts.merge(global_opts)
end
end
exit 0
|