Module: Vmail

Extended by:
Vmail
Included in:
Vmail
Defined in:
lib/vmail.rb,
lib/vmail/options.rb,
lib/vmail/version.rb,
lib/vmail/imap_client.rb,
lib/vmail/message_formatter.rb,
lib/vmail/contacts_extractor.rb

Defined Under Namespace

Classes: ContactsExtractor, ImapClient, MessageFormatter, Options

Constant Summary collapse

INSTRUCTIONS =
<<-EOF
Please visit http://danielchoi.com/software/vmail.html for instructions
on how to configure and run vmail.

EOF
VERSION =
"0.3.8"

Instance Method Summary collapse

Instance Method Details

#startObject



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

def start

  vim = ENV['VMAIL_VIM'] || 'vim'

  ENV['VMAIL_BROWSER'] ||= 'open'

  # check for lynx
  if `which lynx` == ''
    puts "You need to install lynx on your system in order to see html-only messages"
    sleep 3
  end
  opts = Vmail::Options.new(ARGV)
  opts.config

  config = opts.config
  contacts_file = opts.contacts_file

  logfile = (vim == 'mvim') ? STDERR : 'vmail.log'
  config.merge! 'logfile' => logfile

  puts "starting vmail imap client for #{config['username']}"

  drb_uri = begin 
              Vmail::ImapClient.daemon config
            rescue 
              puts "Failure:", $!
              exit(1)
            end

  server = DRbObject.new_with_uri drb_uri

  # TODO this is useless if we're using mvim
  server.window_width = `stty size`.strip.split(' ')[1]

  mailbox = if ARGV[0] =~ /^\d+/ 
              "INBOX"
            else 
              ARGV.shift || 'INBOX' 
            end

  server.select_mailbox mailbox

  query = ARGV.empty? ? [100, 'ALL'] : ARGV
  if query.size == 1 && query[0] =~ /^\d/
    query << "ALL"
  end
  puts "mailbox: #{mailbox}"
  puts "query: #{query.inspect}"
  
  buffer_file = "vmailbuffer"
  puts "using buffer file: #{buffer_file}"
  File.open(buffer_file, "w") do |file|
    file.puts "fetching messages. please wait..."  
  end

  # invoke vim
  vimscript = File.expand_path("../vmail.vim", __FILE__)
  vim_command = "DRB_URI='#{drb_uri}' VMAIL_CONTACTS_FILE=#{contacts_file} VMAIL_MAILBOX=#{String.shellescape(mailbox)} VMAIL_QUERY=#{String.shellescape(query.join(' '))} #{vim} -S #{vimscript} #{buffer_file}"
  puts vim_command
  system(vim_command)

  if vim == 'mvim'
    DRb.thread.join
  end

  File.delete(buffer_file)

  puts "closing imap connection"  
  begin
    Timeout::timeout(5) do 
      $gmail.close
    end
  rescue Timeout::Error
    puts "close connection attempt timed out"
  end
  puts "bye"
  exit
end