summaryrefslogtreecommitdiff
path: root/ruby/maildir-to-imap
blob: 24d619430785c6cdd1e152f1d19e982a5ef06e6b (plain)
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
#!/usr/bin/env ruby
# source: http://wiki.colar.net/ruby_script_to_upload_convert_a_maildir_inbox_to_an_imap_server

require 'net/imap'

if ARGV.length < 5 then
  STDERR.write "args: <host> <user> <pass> <maildir> <imapdir> [test]"
  exit
end

imap_host = ARGV[0]
imap_user = ARGV[1]
imap_pass = ARGV[2]
mail_dir  = ARGV[3]+"/"
imap_dir  = ARGV[4]
TEST_MODE = ARGV[5]!=nil

def upload_dir(imap, mail_dir, imap_dir)
  imap.examine(imap_dir)
  msgs = Dir.entries(mail_dir).each_with_index do |f,j|
    if !File.directory?(f) then
      STDERR.write "adding #{mail_dir+f} to: #{imap_dir}\n"
      if !TEST_MODE then
        imap.append(imap_dir, File.open(mail_dir+f).read,[:Seen])
        STDERR.write "##{j}\n"
      end
    end
  end
end

imap = Net::IMAP.new(imap_host, ssl:true)
imap.authenticate('PLAIN', imap_user, imap_pass)

begin
  imap.create(imap_dir)
rescue
  STDERR.write "failed creating: #{imap_dir}, that's probably ok\n"
end

upload_dir(imap, mail_dir, imap_dir)