Skip to content

Instantly share code, notes, and snippets.

@simonswine
Last active August 29, 2015 14:06
Show Gist options
  • Save simonswine/525ec39d86a3f6ceb1bb to your computer and use it in GitHub Desktop.
Save simonswine/525ec39d86a3f6ceb1bb to your computer and use it in GitHub Desktop.
Sync Keywords from Maildir to IMAP Server by comparing timstamps
require 'date'
require 'net/imap'
dictionary = {
'a' => 'Junk',
'b' => 'NonJunk',
'c' => '$label5',
'd' => '$label2',
'e' => '$label3',
'f' => '$label1',
'g' => '$label4',
'h' => '$Forwarded',
'i' => 'done',
}
mails = {}
Dir["/xxx/Maildir/cur/*"].each do |file|
unixtime = File.basename(file).split('.')[0]
flags = []
File.basename(file).split(',')[-1].split('').each do |char|
flags << dictionary[char] if dictionary.has_key? char
end
mails[unixtime] = flags
end
imap = Net::IMAP.new('xxx')
imap.starttls
imap.login('xxx', 'xxx')
imap.select('INBOX')
imap.search(["ALL"]).reverse_each do |message_id|
date = imap.fetch(message_id, "INTERNALDATE")[0].attr["INTERNALDATE"]
unixtime = DateTime.strptime(date, "%d-%b-%Y %H:%M:%S %z").strftime('%s')
flag_before = imap.fetch(message_id,"FLAGS")[0].attr["FLAGS"]
# skip non imported mails
next unless flag_before.include?'maildir-import'
# next if timestamp not found
next unless mails.keys.include? unixtime
# labels to write
flags=mails[unixtime]
next unless flags.length > 0
p flags
p imap.store(message_id, "+FLAGS", flags)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment