Class: JabberTee::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/jabber-tee/cli.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ CLI

Returns a new instance of CLI.



11
12
13
# File 'lib/jabber-tee/cli.rb', line 11

def initialize(args)
  @args = args
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



9
10
11
# File 'lib/jabber-tee/cli.rb', line 9

def args
  @args
end

#optionsObject (readonly)

Returns the value of attribute options.



9
10
11
# File 'lib/jabber-tee/cli.rb', line 9

def options
  @options
end

Instance Method Details

#execute!Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/jabber-tee/cli.rb', line 15

def execute!
  begin
    parse!(@args.dup)
    config = load_configuration

    if config.destination_missing?
      raise JabberTee::ConfigurationError.new("Either the --to or --room flag is required.")
    end

    client = Client.new(config)
    client.pump!
  rescue SystemExit => e
    raise
  rescue JabberTee::ConfigurationError => e
    print_error(e.message)
  rescue OptionParser::InvalidOption => e
    print_error(e.message)
  rescue Exception => e
    STDERR.puts e.backtrace
    print_error(e.message)
  end
end

#parse!(args) ⇒ Object



38
39
40
41
# File 'lib/jabber-tee/cli.rb', line 38

def parse!(args)
  parse_options(args)
  validate_args(args)
end

#parse_options(args) ⇒ Object



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
# File 'lib/jabber-tee/cli.rb', line 43

def parse_options(args)
  @options ||= {}
  @parsed_options ||= OptionParser.new do |opts|
    opts.banner = "Usage: #{File.basename($0)} [OPTIONS] "
    opts.separator <<DESC
Description:
   This is a simple tool for reading from STDIN and writing
   to both STDOUT and a remote jabber server.  It does not handle
   reading from STDERR, so you will need to re-direct 2>&1 before
   piping it to this tool.

   The configuration file should live under ~/.jabber-tee.yml.
   Please see http://github.com/gabemc/jabber-tee for more
   information.

Options:
DESC
    opts.separator "  Connecting:"
    opts.on('-u', '--username USERNAME',
            "The [email protected] name to connect to the jabber server.") do |u|
      options[:username] = u
    end
    opts.on('-p', '--password PASSWORD',
            "The password for the user to connect to the jabber server.",
            "If not given or defined in the .jabber-tee.yml file,",
            "it will be asked during runtime.") do |p|
      options[:password] = p
    end
    opts.on('-n', '--nick NICKNAME',
            "The nickname to use when connecting to the server.") do |n|
      options[:nick] = n
    end
    opts.on('-a', '--anonymous',
            "Disregards the username information and logs in using anonymous",
            "authentication.  Either the --username or the --authentication",
            "flag are required.") do |a|
      options[:anonymous] = true
    end
    opts.on('-P', '--profile PROFILE',
            "The name of the profile, as defined in the .jabber-tee.yml",
            "file to use to connect with.") do |p|
      options[:profile] = p
    end
    opts.on('--sasl',
            "By default, the connection does not use SASL authentication.",
            "This enables SASL connections") do |s|
      options[:sasl] = true
    end
    opts.on('--digest',
            "When not using SASL, you can use the digest",
            "authentication mechanism.") do |d|
      options[:digest] = true
    end
    opts.separator ""

    opts.separator "  Output: (One required)"
    opts.on('-t', '--to TO',
            "Who to send the message to.") do |t|
      options[:to] = t
    end
    opts.on('-r', '--room ROOM',
            "The room to send the messages to.") do |r|
      options[:room] = r 
    end
    opts.separator ""

    opts.separator "  Informative:"
    opts.on('-v', '--version',
            "Show the version information") do |v|
      puts "#{File.basename($0)} version: #{JabberTee::Version.to_s}"
      exit
    end
    opts.on('-h', '--help',
            "Show this help message") do
      puts opts
      exit
    end
    opts.separator ""

  end.parse!(args)
end

#validate_args(args) ⇒ Object



125
126
127
128
129
# File 'lib/jabber-tee/cli.rb', line 125

def validate_args(args)
  if args.length > 0
    raise JabberTee::ConfigurationError.new("This command takes no extra arguments: #{args[0]}")
  end
end