Roundcube Community Forum

Third Party Contributions => API Based Plugins => Topic started by: ibb-koeln on May 24, 2019, 07:02:46 AM

Title: Get Message-Id when message is displayed
Post by: ibb-koeln on May 24, 2019, 07:02:46 AM
Hi!

I'm totally new to Roundcube, so maybe its a noob question...

I would like to get the message-id as well as the imap uid for a mail when it is displayed to the user. I need to store these data to be able to link to a certain message from another software, ideally by opening roundcube directly showing this mail. 

I already wrote a simple plugin

PHP:
Code: [Select]
class my_plugin extends rcube_plugin {
    public $task = 'mail';
   
    var $message_id = "no-id";
    var $message = 0;
   
    function init() {
        $this->add_hook('message_load', array($this, 'message_load'));
       
        $this->register_action('plugin.get_data', array($this, 'request_data'));
        $this->include_script('my_plugin.js');
    }
   
    function request_data() {
        $rcmail = rcmail::get_instance();
        $rcmail->output->command('plugin.my_plugin_callback', array('result' => "OK", 'message_id' => $this->message_id));
    }
   
    function message_load($args) {
        $this->message_id = "message_load_called"; // "$args['object']->get_header( 'message-id', false);
    }
}

JS:
Code: [Select]
if( rcmail.env.action=='preview') {
    rcmail.addEventListener('init', function(evt) {
        rcmail.addEventListener('plugin.my_plugin_callback', receive_data);
        rcmail.http_get('plugin.get_data', '', false);
    }
});

function receive_data(response) {
    alert(JSON.stringify(response));   
}

The callback loop http_get()/receive_data() is working fine, but I never get a result for message-id, it's always the initial "no-id" as if message_load() is not called. Any hints on that?

Btw, is there any doc for the javascript rcmail object available? I already found the stuff on github (plugin/skin API) and the PHP-Documentation but nothing about the javascript object.

Regards,
Oliver
Title: Re: Get Message-Id when message is displayed
Post by: SKaero on May 24, 2019, 04:33:16 PM
What your doing doesn't work because PHP doesn't retain state between requests. What that means is when Roundcube make a request to get a message the "message_load" function would be called and "$this->message_id" would be set. But when you make the request to "plugin.my_plugin_callback" its a brand new request that doesn't have access to the data from the prior request.

What you may try and do is use the "set_env" function in the "message_load" load function to set the values you want into the JS object "rcmail.env" For example:
Code: [Select]
    function message_load($args) {
        $rc = rcube::get_instance();
        $rc->output->set_env('message_id', "message_load_called");
    }
Title: Re: Get Message-Id when message is displayed
Post by: ibb-koeln on May 27, 2019, 05:09:17 AM
 :-[ Ouch... After over 20 years Java and C++ I've to change my thinking... thanks for that and your fast reply!

So, when writing to rcmail.env, what is the right time or place to read this value? I tried to get 'message_id' inside

Code: [Select]
if( rcmail.env.action=='preview') {
    alert(rcmail.env.message_id);
    ...
}

but it's undefined. But maybe I should go another way, either. Do I have a chance to get the message and headers directly from JS? That leads to my other question: Is there any (detailed) documentation about the JS object rcmail?

Title: Re: Get Message-Id when message is displayed
Post by: ibb-koeln on May 27, 2019, 05:19:07 AM
OK, my fault... I only tried when action==preview. When looking for message_id if action==show, env.message_id is set.

This is OK for now. Thanks for your help!
Title: Re: Get Message-Id when message is displayed
Post by: SKaero on May 27, 2019, 12:48:13 PM
And just to answer your other question, there really isn't and JS documentation outside of https://github.com/roundcube/roundcubemail/wiki/Plugin-Events