Author Topic: run server side command check for new messages  (Read 5042 times)

Offline ear9mrn

  • Newbie
  • *
  • Posts: 2
run server side command check for new messages
« on: August 19, 2010, 09:03:52 PM »
Hi,

This is not a request as such, I am just interested to know how to do it as I am sure it is not that difficult.

I have roundcube that I use to access my dovecot server. The server itself is a sendmail setup that sends outgoing mail via my ISP and incoming mail is grabbed from my domain host via a cron job that runs every 5 minutes for each user, see below.
 
Code: [Select]
user getmail --getmaildir /home/user/.getmail -rgetmailrc

I would like it if I could run this code every time the "check for new messages" icon is hit, then have a delay until it is complete and then obviously show any new messages. As it is at the moment I have to wait ~5 minutes for the cron job to run before the check for new mail finds anything. It would also have to be able to run for which ever user is logged in. User names are all consitent with roundcube/sendmail etc.

I am sure it is not hard to do, just after a couple of hours of digging around the in the code I could  not figure out a simple hack to make this happen.

Any pointers to the part of the code where the magic happens would be great as I could probably figure out the syntax etc.

This may be feature that others would be interested in, I am nor sure.


Thanks,

Pete.

Offline SKaero

  • Administrator
  • Hero Member
  • *****
  • Posts: 5,879
    • SKaero - Custom Roundcube development
run server side command check for new messages
« Reply #1 on: August 20, 2010, 12:30:12 AM »
It could be done with a plugin, just use the new_messages hook.

Offline ear9mrn

  • Newbie
  • *
  • Posts: 2
[solved]
« Reply #2 on: August 21, 2010, 08:18:16 PM »
So I got it working....:):):)

I actually had to used the 'mailboxes_list' hook rather than the one suggested above. In order for this to work I had to modify my sudo'ers file to allow apache to run as a user. I am sure there is better ways to do this..

Code: [Select]

##allow apache to run getmail
Cmnd_Alias GETMAIL = /usr/bin/getmail

##allow apache run getmail
apache  ALL=(ALL) NOPASSWD: GETMAIL


I also had to comment out the following line to allow apache to run without a tty.

Code: [Select]


#Defaults    requiretty



Some may find these changes a little risky from a security standpoint. If anyone has any suggestions of how apache could run getmail on behalf of a user I am interested to hear.

If anyone thinks this is worth the effort let me know and I can post it to the official plugins list (how do I do that ???).

And for anyone else who may be interested here is the plugin that I created that runs getmail every time the check new messages icon is hit.

Code: [Select]



/**
 * Getmail (Run GetMail to fetch mail from mail server to local server)
 *
 * plugin to run getmail when checking for new messges
 *
 * @version 0.1
 * @author Pete Nevill
 *
 */
class getmail extends rcube_plugin
{
  public $task = 'mail';

  function init()
  {
    $this->add_hook('mailboxes_list', array($this, 'get_mail'));
  }

  function get_mail($args)
  {

$rcmail = rcmail::get_instance();
$user = $rcmail->user->data['username'];
$log_config = rcmail::get_instance()->config->get('log_driver');

$log_entry = shell_exec('sudo -u ' . $user . ' getmail -v --getmaildir /home/' . $user . '/.getmail --rcfile getmailrc');

    if ($log_config == 'syslog'){
        syslog(LOG_WARNING, 'Roundcube GetMail plugin: ' . $log_entry);
    } elseif ($log_config == 'file'){
        error_log('['.date('d-M-Y H:i:s O')."]: Roundcube GetMail plugin: ".$log_entry."\n", 3, "logs/userlogins");

    } else {
    $rcmail->output->command('display_message', 'The RoundCube GetMail Plugin was unable to retrieve the log driver form the config, please check your config file for log_driver.', 'error');

    }

  }
}
?>