require 'gpgme' require 'net/http' require 'yaml' class String def to_html self.gsub("\n", "
") end def strip_html ret = self.gsub(/
/, "\n").gsub(/<\/?[^>]*>/, "") ret = ret.gsub(/^- /, "") end end class PgpFilter < MouseHole::App name "PGP Filter" namespace "http://rhnh.net/" description "Verifies PGP signatures found in the page" version "0.1" def redirect host, port, path if "mail.google.com".eql?(host) && path =~ /^\/pgpirate\/(.*)/ host = "localhost" port = 3704 path = "/pgpirate/#$1" end [host, port, path] end url("http://mail.google.com/mail/h/*", :ugly_rewrite) StartPgpSignedMessage = "-----BEGIN PGP SIGNED MESSAGE-----" EndPgpSignedMessage = "-----END PGP SIGNATURE-----" StartPgpMessage = "-----BEGIN PGP MESSAGE-----" EndPgpMessage = "-----END PGP MESSAGE-----" def ugly_rewrite page cont = true buffer = page.document page.document = "" id = 1 while cont message = buffer.match(/
(.*?)<\/div>/m) if message pre = message.pre_match text = message[1].strip_html append = nil function_hash.each_pair do |key, value| if text =~ key append = value.call(pre, text, id) break end end page.document += (append ? append : pre + message[0]) buffer = message.post_match else page.document += buffer cont = false end id += 1 end end def function_hash signed_regex = /#{StartPgpSignedMessage}.*?#{EndPgpSignedMessage}/m encrypted_regex = /#{StartPgpMessage}.*?#{EndPgpMessage}/m return { signed_regex => method(:ajax_replace_signed_message).to_proc, encrypted_regex => method(:ajax_replace_encrypted_message).to_proc} end def ajax_replace_signed_message buffer, text, id plain = text.match(/\n\n(.*)\n-----BEGIN PGP SIGNATURE-----/m)[1] sig = text.match(/-----BEGIN PGP SIGNATURE-----.*?-----END PGP SIGNATURE-----/m)[0] mab = Markaby::Builder.new js = <<-EOS var ajax = new Ajax.Updater('pgpmsg#{id}', '/pgpirate/verify', { method: 'post', parameters:Form.serialize($('pgpform#{id}')), evalScripts:true }); EOS mab.div.msg(:id => "pgpmsg#{id}") do form(:id => "pgpform#{id}", :style => 'display: none') do textarea plain, :name => 'message' textarea sig, :name => 'signature' input :value => id, :name => 'id' end script :src => "http://localhost:3704/pgpirate/static/js/prototype-1.4.0.js", :language => "Javascript", :type => "text/javascript" if id == 1 script js, :language => "Javascript", :type => "text/javascript" end buffer.gsub!(/(Show original<\/a>)/, "\\1 | PGP Signature verification in progress...") return buffer + mab.to_s end def ajax_replace_encrypted_message buffer, text, id js = <<-EOS var ajax = new Ajax.Updater('pgpmsg#{id}', '/pgpirate/decrypt', { method: 'post', parameters:Form.serialize($('pgpform#{id}')), evalScripts:true }); EOS mab = Markaby::Builder.new mab.div.msg(:id => "pgpmsg#{id}") do form(:id => "pgpform#{id}", :style => 'display: none') do textarea text, :name => 'cipher' input :value => id, :name => 'id' end script :src => "http://localhost:3704/pgpirate/static/js/prototype-1.4.0.js", :language => "Javascript", :type => "text/javascript" if id == 1 script js, :language => "Javascript", :type => "text/javascript" end buffer.gsub!(/(Show original<\/a>)/, "\\1 | PGP decryption in progress...") return buffer + mab.to_s end end