diff --git a/js/rc_openpgpjs.js b/js/rc_openpgpjs.js index c6d081c..7db0549 100644 --- a/js/rc_openpgpjs.js +++ b/js/rc_openpgpjs.js @@ -286,17 +286,24 @@ if(window.rcmail) { /** * Get the user's public key */ - function fetchSendersPubkey() { + function fetchSendersPubkey(armored) { + + if (typeof(armored) == "undefined") { + armored = false; + } + var re = /[a-zA-Z0-9\._%+-]+@[a-zA-Z0-9\._%+-]+\.[a-zA-Z]{2,4}/g; var address = $("#_from>option:selected").html().match(re); if (address.length > 0) { var pubkey = getPubkeyForAddress(address[0]); - + if(typeof(pubkey[0]) != "undefined") { - return pubkey[0].obj; + if (armored) + return pubkey[0].armored; + else + return pubkey[0].obj; } - } return false; } @@ -308,6 +315,8 @@ if(window.rcmail) { if(!$("#openpgpjs_encrypt").is(":checked") && !$("#openpgpjs_sign").is(":checked")) { if(confirm(rcmail.gettext("continue_unencrypted", "rc_openpgpjs"))) { + // remove the public key attachment since we don't sign nor encrypt the message + removePublicKeyAttachment(); return true; } else { return false; @@ -318,6 +327,14 @@ if(window.rcmail) { return true; } + // send the user's public key to the server so it can be sent as attachment + var pubkey_sender = fetchSendersPubkey(true); + if (pubkey_sender) { + var lock = rcmail.set_busy(true, 'loading'); + rcmail.http_post('plugin.pubkey_save', { _pubkey: pubkey_sender }, lock); + } + // end send user's public key to the server + // Encrypt and sign if($("#openpgpjs_encrypt").is(":checked") && $("#openpgpjs_sign").is(":checked")) { // get the private key @@ -433,6 +450,21 @@ if(window.rcmail) { return false; } + /** + * Removes the public key attachment + * Used if the user doesn't sign nor encrypt the message + */ + function removePublicKeyAttachment() { + $("#attachment-list").each(function() { + $(this).find('li').each(function() { + if ($(this).text().indexOf('signature.asc') >= 0) { + rcmail.command('remove-attachment', $(this).attr('id')); + return false; + } + }); + }); + } + function importFromSKS(id) { rcmail.http_post("plugin.pks_search", "search=" + id + "&op=get"); return; diff --git a/rc_openpgpjs.php b/rc_openpgpjs.php index c47a93e..eceb8f5 100644 --- a/rc_openpgpjs.php +++ b/rc_openpgpjs.php @@ -37,6 +37,7 @@ function init() $this->add_hook('user_create', array($this, 'user_create')); $this->register_action('plugin.pks_search', array($this, 'hkp_search')); $this->register_action('plugin.hkp_add', array($this, 'hkp_add')); + $this->register_action('plugin.pubkey_save', array($this, 'pubkey_save')); if ($this->rc->task == 'mail') { $this->add_hook('render_page', array($this, 'render_page')); @@ -52,6 +53,10 @@ function init() // load css $this->include_stylesheet($this->local_skin_path() . '/rc_openpgpjs.css'); + // add public key attachment related hooks + $this->add_hook('message_compose', array($this, 'message_compose')); + $this->add_hook('message_sent', array($this, 'unlink_pubkey')); + if ($this->api->output->type == 'html') { // add key manager item to message menu $opts = array("command" => "open-key-manager", @@ -229,7 +234,20 @@ function hkp_add() { header("HTTP/1.1 501 Not Implemented"); die(); } - + + /** + * Saves the public key to a temporary file so we can send it as attachment + */ + function pubkey_save() { + $rcmail = rcmail::get_instance(); + $temp_dir = unslashify($rcmail->config->get('temp_dir')); + $file = $temp_dir."/".md5($_SESSION['username']).".asc"; + if(file_exists($file)) { + $pubkey = trim(get_input_value('_pubkey', RCUBE_INPUT_POST)); + file_put_contents($file, $pubkey); + } + } + /** * Handler for preferences_list hook. * Adds options blocks into Compose settings sections in Preferences. @@ -275,5 +293,48 @@ function preferences_save($p) } return $p; - } + } + + /** + * Handler for message_compose hook + * Creates a dummy publick key attachment + */ + function message_compose($args) { + $dbg = print_r($args, true); + + if ($f = $this->create_pubkey_dummy()) { + $args['attachments'][] = array('path' => $f, 'name' => "signature.asc", 'mimetype' => "text/plain"); + } + return $args; + } + + /** + * Handler for message_sent hook + * Deletes the public key from the server + */ + function unlink_pubkey($args) { + $rcmail = rcmail::get_instance(); + $temp_dir = unslashify($rcmail->config->get('temp_dir')); + $file = $temp_dir."/".md5($_SESSION['username']).".asc"; + if(file_exists($file)) { + @unlink($file); + } + } + + /** + * Creates a dummy public key file + */ + function create_pubkey_dummy() { + $rcmail = rcmail::get_instance(); + $temp_dir = unslashify($rcmail->config->get('temp_dir')); + if (!empty($temp_dir)) { + $file = $temp_dir."/".md5($_SESSION['username']).".asc"; + if(file_exists($file)) + @unlink($file); + if (file_put_contents($file, " ")) { + return $file; + } + } + return false; + } }