Skip to content

Instantly share code, notes, and snippets.

@pzskc383
Created July 15, 2017 00:01
Show Gist options
  • Save pzskc383/112532f463baccd5dd7bc1e997018044 to your computer and use it in GitHub Desktop.
Save pzskc383/112532f463baccd5dd7bc1e997018044 to your computer and use it in GitHub Desktop.
herp derp
#!env ruby
# offlineimap --info >| ~/tmp/offlineimap.info.output 2>&1
output = File.join(ENV['HOME'], 'tmp/offlineimap.info.output')
config = File.join(ENV['HOME'], '.offlineimaprc')
def parse_ini(file)
data = {}
f = File.open(file, 'r')
section = "no-section"
f.each_line do |l|
l = l.chomp!
if l[0] == '[' and l[l.length-1] == ']'
section = l[1..-2]
data[section] = {}
elsif l.include? '='
parts = l.split('=')
data[section][parts[0].strip] = parts[1].strip
end
end
f.close
data
end
def parse_output(file)
data = {}
f = File.open(file, 'r')
repository = "no-repo"
folderlist_started = false
f.each_line do |l|
l.chomp!
case l
when /^Remote repository/
repository = l.gsub(/^.*'(.*)':.*$/, '\1')
data[repository] = []
when /^Folderlist:/
folderlist_started = true
when ''
folderlist_started = false if folderlist_started
when /^.* -> .*$/
if folderlist_started
data[repository].push l.split(' -> ').map(&:strip)
end
end
end
f.close
data
end
def print_store(name, repo)
ret = {}
repo.each do |(key,val)|
case key
when 'type'
if val == 'Maildir'
ret['MaildirStore'] = name
elsif val == 'IMAP'
ret['IMAPStore'] = name
ret['SSLVersions'] = 'TLSv1.2'
ret['SSLType'] = 'IMAPS'
elsif val == 'Gmail'
ret['IMAPStore'] = name
ret['Host'] = 'imap.gmail.com'
ret['SSLType'] = 'IMAPS'
ret['SSLVersions'] = 'TLSv1.2'
end
when 'localfolders'
ret['Path'] = val[-1] == '/' ? val : val+'/'
when 'remotehost'
ret['Host'] = val
when 'remoteuser'
ret['User'] = val
when 'remoteport'
ret['Port'] = val
when 'auth_mechanisms'
ret['AuthMechs'] = val
when 'remotepasseval'
ret['PassCmd'] = val.gsub(/^.*\("(.*)"\).*$/,'"pass \1"')
end
end
ret.map{ |(k,v)| "#{k} #{v}" }#.join("\n") + "\n\n"
end
def print_mbsyncrc(config, folders)
lines = []
config.keys.select {|k| k.start_with? 'Account'}.each do |acc|
acckey = acc.gsub(/^Account /,'').gsub('@','-')
lines << "# {{{ #{acckey}"
["local", "remote"].each do |repotype|
repokey = config[acc]["#{repotype}repository"]
repodata = config["Repository #{repokey}"]
lines << print_store(repokey, repodata)
lines << ''
end
channels = []
folders[config[acc]["remoterepository"]].each do |m,s|
channame = "#{acckey}-#{s}"
channels << channame
lines << "Channel #{channame}"
lines << "Master :#{config[acc]['remoterepository']}:\"#{m}\""
lines << "Slave :#{config[acc]['localrepository']}:\"#{s}\""
lines << 'Sync All'
lines << ''
end
lines << ''
lines << "Group #{acckey}"
lines << "Channels #{channels.join(' ')}"
lines << ''
lines << "# }}}"
lines << ''
end
puts lines.join("\n")
end
configdata = parse_ini(config)
folderdata = parse_output(output)
print_mbsyncrc(configdata, folderdata)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment