Skip to content

Instantly share code, notes, and snippets.

@acspike
Forked from boblmartens/aaron.rb
Created February 9, 2010 02:58
Show Gist options
  • Save acspike/298876 to your computer and use it in GitHub Desktop.
Save acspike/298876 to your computer and use it in GitHub Desktop.
require 'net/imap'
require 'base64'
require 'rexml/document'
require 'yaml'
require 'open3'
require 'pp'
class GPG
def initialize(gpg_exec, gpg_home, gpg_pass)
@gpg_exec = gpg_exec
@gpg_home = gpg_home
@gpg_pass = gpg_pass
end
def decrypt(data)
output = String.new
cmd = "\"#{@gpg_exec}\" --homedir=\"#{@gpg_home}\" --batch --passphrase-fd 0 --decrypt"
Open3.popen3(cmd) do |stdin, stdout, stderr, wait_thr|
stdin.puts @gpg_pass
stdin.puts data
stdin.close
output = stdout.read
end
output
end
end
class MessageMunger
def initialize(gpg)
@gpg = gpg
@formatter = REXML::Formatters::Default.new
end
def munge(data)
#message body has been Base64 encoded for transit...
data = Base64.decode64 data
#... and GPG encrypted
data = @gpg.decrypt data
#REXML was angry about   entities
data = data.gsub ' ', ' '
doc = REXML::Document.new data
found = {}
REXML::XPath.each(doc, '//dd') do |x|
out = ''
x.each_child {|c| @formatter.write c, out}
found[x.attributes['x:name']] = out
end
found
end
end
##custom verification callback used here only for debugging
##ssl verification failure
#VerifyCallbackProc = proc do |v, ctx|
# #p ctx.methods
# #p v, ctx.current_cert, ctx.chain
# v
#end
if $0 == __FILE__
conf = File.open('config.yaml') {|yf| YAML::load(yf)}
imap_ssl = {:ca_file=>conf[:ca_file]}
#skip verification for now
imap_ssl = {:verify_mode=>0}
gpg = GPG.new conf[:gpg_exec], conf[:gpg_home],conf[:gpg_pass]
munger = MessageMunger.new gpg
mbox = Net::IMAP.new conf[:imap_host], {:ssl=>imap_ssl}
mbox.login conf[:imap_user], conf[:imap_pass]
mbox.select 'INBOX'
mbox.search(['SUBJECT','Submission']).each do |msgid|
data = mbox.fetch(msgid, "(BODY[TEXT])")[0].attr['BODY[TEXT]']
pp munger.munge(data)
end
mbox.close
mbox.logout
mbox.disconnect
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment