Class: Vmail::Options

Inherits:
Object
  • Object
show all
Defined in:
lib/vmail/options.rb

Direct Known Subclasses

SendOptions

Constant Summary collapse

DEFAULT_CONTACTS_FILENAME =
"vmail-contacts.txt"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ Options

Returns a new instance of Options.



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/vmail/options.rb', line 9

def initialize(argv)
  config_file_locations = ['.vmailrc', "#{ENV['HOME']}/.vmailrc"]
  @config_file = config_file_locations.detect do |path|
    File.exists?(File.expand_path(path))
  end
  @contacts_file = [DEFAULT_CONTACTS_FILENAME, "#{ENV['HOME']}/#{DEFAULT_CONTACTS_FILENAME}"].detect  do |path|
    File.exists?(File.expand_path(path))
  end
  @config = {}
  parse argv
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



7
8
9
# File 'lib/vmail/options.rb', line 7

def config
  @config
end

#contacts_fileObject

Returns the value of attribute contacts_file.



8
9
10
# File 'lib/vmail/options.rb', line 8

def contacts_file
  @contacts_file
end

Instance Method Details

#parse(argv) ⇒ Object



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
# File 'lib/vmail/options.rb', line 21

def parse(argv)
  OptionParser.new do |opts|
    opts.banner = "Usage:  vmail [ options ] [ limit ] [ imap search query ]" 
    opts.separator ""
    opts.separator "Specific options:"
    opts.on("-c", "--config path", String, "Path to config file") do |config_file|
      @config_file = config_file
    end
    opts.on("-t", "--contacts path", String, "Path to contacts file") do |file|
      @contacts_file = file
    end
    opts.on("-g[n]", "--getcontacts[n]", Integer, "Generate contacts file. n is number of emails to scan (default 500).") do |n| 
      @get_contacts = true
      @max_messages_to_scan = n || 500
    end
    opts.on("-v", "--version", "Show version") do
      require 'vmail/version'
      puts "vmail #{Vmail::VERSION}\nCopyright 2010 Daniel Choi under the MIT license"
      exit
    end
    opts.on("-h", "--help", "Show this message") do
      puts opts
      exit
    end
    opts.separator ""
    opts.separator INSTRUCTIONS

    begin
      opts.parse!(argv)
      if @config_file && File.exists?(@config_file)
        puts "using config file: #{@config_file}"
      else
        puts <<EOF

Missing config file! 

#{INSTRUCTIONS}
EOF
        exit(1)
      end

      if @contacts_file.nil?
        puts "No contacts file found for auto-completion. See help for how to generate it."
        sleep 0.5
      else
        puts "using contacts file: #{@contacts_file}"
      end

      @config = YAML::load(File.read(@config_file))
      if @config['password'].nil?
        @config['password'] = ask("Enter gmail password (won't be visible & won't be persisted):") {|q| q.echo = false}
      end

      if @get_contacts
        require 'vmail/contacts_extractor'
        extractor = ContactsExtractor.new(@config['username'], @config['password'])
        File.open(DEFAULT_CONTACTS_FILENAME, 'w') do |file|
          extractor.extract(@max_messages_to_scan) do |address| 
            STDERR.print '.'
            file.puts(address.strip)
            STDERR.flush 
          end
        end
        STDERR.print "\n"
        puts "saved file to #{DEFAULT_CONTACTS_FILENAME}"
        puts "sorting address..."
        cmd = "sort #{DEFAULT_CONTACTS_FILENAME} | uniq > vmail-tmp.txt" 
        cmd2 = "mv vmail-tmp.txt #{DEFAULT_CONTACTS_FILENAME}"
        `#{cmd}`
        `#{cmd2}`
        puts "done"
        exit
      end

    rescue OptionParser::ParseError => e
      STDERR.puts e.message, "\n", opts
    end

  end
end