Method: IMAPClient.process_args

Defined in:
lib/imap_client.rb

.process_args(args, extra_options) ⇒ Object

Handles processing of args.



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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/imap_client.rb', line 16

def self.process_args(args, extra_options)
  opts_file = File.expand_path '~/.imap_cleanse'
  options = {}

  if File.exist? opts_file then
    unless File.stat(opts_file).mode & 077 == 0 then
      $stderr.puts "WARNING! #{opts_file} is group/other readable or writable!"
      $stderr.puts "WARNING! I'm not doing a thing until you fix it!"
      exit 1
    end

    File.readlines(opts_file).map { |l| l.chomp.split '=', 2 }.each do |k,v|
      v = true  if v == 'true'
      v = false if v == 'false'
      v = Integer(v) rescue v
      (options[k.intern] = v) rescue nil
    end
  end

  options[:SSL]      ||= true
  options[:Username] ||= ENV['USER']
  options[:Root]     ||= 'mail'
  options[:Noop]     ||= false
  options[:Verbose]  ||= false

  extra_options.each do |k,(v,m)|
    options[k]       ||= v
  end

  opts = OptionParser.new do |opts|
    opts.banner = "Usage: #{File.basename $0} [options]"
    opts.separator ''
    opts.separator 'Options may also be set in the options file ~/.imap_cleanse.'
    opts.separator ''
    opts.separator 'Example ~/.imap_cleanse:'
    opts.separator "\tHost=mail.example.com"
    opts.separator "\tPassword=my password"

    opts.separator ''
    opts.separator 'Connection options:'

    opts.on("-H", "--host HOST",
            "IMAP server host",
            "Default: #{options[:Host].inspect}",
            "Options file name: Host") do |host|
      options[:Host] = host
    end

    opts.on("-P", "--port PORT",
            "IMAP server port",
            "Default: The correct port SSL/non-SSL mode",
            "Options file name: Port") do |port|
      options[:Port] = port
    end

    opts.on("-s", "--[no-]ssl",
            "Use SSL for IMAP connection",
            "Default: #{options[:SSL].inspect}",
            "Options file name: SSL") do |ssl|
      options[:SSL] = ssl
    end

    opts.separator ''
    opts.separator 'Login options:'

    opts.on("-u", "--username USERNAME",
            "IMAP username",
            "Default: #{options[:Username].inspect}",
            "Options file name: Username") do |username|
      options[:Username] = username
    end

    opts.on("-p", "--password PASSWORD",
            "IMAP password",
            "Default: Read from ~/.imap_cleanse",
            "Options file name: Password") do |password|
      options[:Password] = password
    end

    authenticators = Net::IMAP.send :class_variable_get, :@@authenticators
    auth_types = authenticators.keys.sort.join ', '
    opts.on("-a", "--auth AUTH",
            "IMAP authentication type override",
            "Authentication type will be auto-",
            "discovered",
            "Default: #{options[:Auth].inspect}",
            "Valid values: #{auth_types}",
            "Options file name: Auth") do |auth|
      options[:Auth] = auth
    end

    opts.separator ''
    opts.separator "#{self} options:"

    opts.on("-r", "--root ROOT",
            "Root of mailbox hierarchy",
            "Default: #{options[:Root].inspect}",
            "Options file name: Root") do |root|
      options[:Root] = root
    end

    opts.on("-b", "--boxes BOXES",
            "Comma-separated list of mailbox name",
            "prefixes to search",
            "Default: #{options[:Boxes].inspect}",
            "Options file name: Boxes") do |boxes|
      options[:Boxes] = boxes
    end

    yield opts, options

    opts.on("-n", "--noop",
            "Perform no destructive operations",
            "Best used with the verbose option",
            "Default: #{options[:Noop].inspect}",
            "Options file name: Noop") do |noop|
      options[:Noop] = noop
    end

    opts.on("-v", "--[no-]verbose",
            "Be verbose",
            "Default: #{options[:Verbose].inspect}",
            "Options file name: Verbose") do |verbose|
      options[:Verbose] = verbose
    end

    opts.separator ''

    opts.on("-h", "--help",
            "You're looking at it") do
      $stderr.puts opts
      exit 1
    end

    opts.separator ''
  end

  opts.parse! args

  options[:Port] ||= options[:SSL] ? 993 : 143

  if options[:Host].nil? or
     options[:Password].nil? or
     options[:Boxes].nil? or
     extra_options.any? { |k,v| options[k].nil? } then
    $stderr.puts opts
    $stderr.puts
    $stderr.puts "Host name not set"     if options[:Host].nil?
    $stderr.puts "Password not set"      if options[:Password].nil?
    $stderr.puts "Boxes option not set"  if options[:Boxes].nil?
    extra_options.each do |k,(v,msg)|
      $stderr.puts msg if options[k].nil?
    end
    exit 1
  end

  return options
end