From 6e8f2a7448d9bf5a87603b197816027f3dd4bb4c Mon Sep 17 00:00:00 2001
From: Aleksander Machniak <alec@alec.pl>
Date: Sat, 16 Mar 2013 14:01:10 -0400
Subject: [PATCH] Notify about a new mail only if it's UNSEEN (#1388965)

---
 plugins/newmail_notifier/newmail_notifier.php |   63 ++++++++++++++++++-------------
 plugins/newmail_notifier/package.xml          |    6 +-
 program/steps/mail/check_recent.inc           |    4 +-
 program/lib/Roundcube/rcube_imap.php          |   10 +++-
 program/lib/Roundcube/rcube_storage.php       |    5 +-
 5 files changed, 52 insertions(+), 36 deletions(-)

diff --git a/plugins/newmail_notifier/newmail_notifier.php b/plugins/newmail_notifier/newmail_notifier.php
index e985c8b4..912ff4f 100644
--- a/plugins/newmail_notifier/newmail_notifier.php
+++ b/plugins/newmail_notifier/newmail_notifier.php
@@ -35,6 +35,9 @@
 
     private $rc;
     private $notified;
+    private $opt = array();
+    private $exceptions = array();
+
 
     /**
      * Plugin initialization
@@ -49,13 +52,34 @@
             $this->add_hook('preferences_save', array($this, 'prefs_save'));
         }
         else { // if ($this->rc->task == 'mail') {
-            $this->add_hook('new_messages', array($this, 'notify'));
             // add script when not in ajax and not in frame
             if ($this->rc->output->type == 'html' && empty($_REQUEST['_framed'])) {
                 $this->add_texts('localization/');
                 $this->rc->output->add_label('newmail_notifier.title', 'newmail_notifier.body');
                 $this->include_script('newmail_notifier.js');
             }
+
+            if ($this->rc->action == 'refresh') {
+                // Load configuration
+                $this->load_config();
+
+                $this->opt['basic']   = $this->rc->config->get('newmail_notifier_basic');
+                $this->opt['sound']   = $this->rc->config->get('newmail_notifier_sound');
+                $this->opt['desktop'] = $this->rc->config->get('newmail_notifier_desktop');
+
+                if (!empty($this->opt)) {
+                    // Get folders to skip checking for
+                    $exceptions = array('drafts_mbox', 'sent_mbox', 'trash_mbox');
+                    foreach ($exceptions as $folder) {
+                        $folder = $this->rc->config->get($folder);
+                        if (strlen($folder) && $folder != 'INBOX') {
+                            $this->exceptions[] = $folder;
+                        }
+                    }
+
+                    $this->add_hook('new_messages', array($this, 'notify'));
+                }
+             }
         }
     }
 
@@ -132,43 +156,30 @@
      */
     function notify($args)
     {
-        // Already notified or non-automatic check
-        if ($this->notified || !empty($_GET['_refresh'])) {
+        // Already notified or unexpected input
+        if ($this->notified || empty($args['diff']['new'])) {
             return $args;
         }
 
-        // Get folders to skip checking for
-        if (empty($this->exceptions)) {
-            $this->delimiter = $this->rc->storage->get_hierarchy_delimiter();
-
-            $exceptions = array('drafts_mbox', 'sent_mbox', 'trash_mbox');
-            foreach ($exceptions as $folder) {
-                $folder = $this->rc->config->get($folder);
-                if (strlen($folder) && $folder != 'INBOX') {
-                    $this->exceptions[] = $folder;
-                }
-            }
-        }
-
-        $mbox = $args['mailbox'];
+        $mbox      = $args['mailbox'];
+        $storage   = $this->rc->get_storage();
+        $delimiter = $storage->get_hierarchy_delimiter();
 
         // Skip exception (sent/drafts) folders (and their subfolders)
         foreach ($this->exceptions as $folder) {
-            if (strpos($mbox.$this->delimiter, $folder.$this->delimiter) === 0) {
+            if (strpos($mbox.$delimiter, $folder.$delimiter) === 0) {
                 return $args;
             }
         }
 
-        $this->notified = true;
+        // Check if any of new messages is UNSEEN
+        $deleted = $this->rc->config->get('skip_deleted') ? 'UNDELETED ' : '';
+        $search  = $deleted . 'UNSEEN UID ' . $args['diff']['new'];
+        $unseen  = $storage->search_once($mbox, $search);
 
-        // Load configuration
-        $this->load_config();
+        if ($unseen->count()) {
+            $this->notified = true;
 
-        $basic   = $this->rc->config->get('newmail_notifier_basic');
-        $sound   = $this->rc->config->get('newmail_notifier_sound');
-        $desktop = $this->rc->config->get('newmail_notifier_desktop');
-
-        if ($basic || $sound || $desktop) {
             $this->rc->output->command('plugin.newmail_notifier',
                 array('basic' => $basic, 'sound' => $sound, 'desktop' => $desktop));
         }
diff --git a/plugins/newmail_notifier/package.xml b/plugins/newmail_notifier/package.xml
index d3de25f..ea0fcd9 100644
--- a/plugins/newmail_notifier/package.xml
+++ b/plugins/newmail_notifier/package.xml
@@ -19,10 +19,10 @@
 		<email>alec@alec.pl</email>
 		<active>yes</active>
 	</lead>
-	<date>2012-02-07</date>
+	<date>2013-03-16</date>
 	<version>
-		<release>0.4</release>
-		<api>0.3</api>
+		<release>0.5</release>
+		<api>0.5</api>
 	</version>
 	<stability>
 		<release>stable</release>
diff --git a/program/lib/Roundcube/rcube_imap.php b/program/lib/Roundcube/rcube_imap.php
index 8d9c375..0aa059c 100644
--- a/program/lib/Roundcube/rcube_imap.php
+++ b/program/lib/Roundcube/rcube_imap.php
@@ -1096,16 +1096,17 @@
 
 
     /**
-     * Returns current status of folder
+     * Returns current status of a folder (compared to the last time use)
      *
      * We compare the maximum UID to determine the number of
      * new messages because the RECENT flag is not reliable.
      *
      * @param string $folder Folder name
+     * @param array  $diff   Difference data
      *
-     * @return int   Folder status
+     * @return int Folder status
      */
-    public function folder_status($folder = null)
+    public function folder_status($folder = null, &$diff = array())
     {
         if (!strlen($folder)) {
             $folder = $this->folder;
@@ -1126,6 +1127,9 @@
         // got new messages
         if ($new['maxuid'] > $old['maxuid']) {
             $result += 1;
+            // get new message UIDs range, that can be used for example
+            // to get the data of these messages
+            $diff['new'] = ($old['maxuid'] + 1 < $new['maxuid'] ? ($old['maxuid']+1).':' : '') . $new['maxuid'];
         }
         // some messages has been deleted
         if ($new['cnt'] < $old['cnt']) {
diff --git a/program/lib/Roundcube/rcube_storage.php b/program/lib/Roundcube/rcube_storage.php
index 8a36f1f..700d12f 100644
--- a/program/lib/Roundcube/rcube_storage.php
+++ b/program/lib/Roundcube/rcube_storage.php
@@ -807,13 +807,14 @@
 
 
     /**
-     * Returns current status of a folder
+     * Returns current status of a folder (compared to the last time use)
      *
      * @param string $folder Folder name
+     * @param array  $diff   Difference data
      *
      * @return int Folder status
      */
-    abstract function folder_status($folder = null);
+    abstract function folder_status($folder = null, &$diff = array());
 
 
     /**
diff --git a/program/steps/mail/check_recent.inc b/program/steps/mail/check_recent.inc
index d3c14a3..3649d14 100644
--- a/program/steps/mail/check_recent.inc
+++ b/program/steps/mail/check_recent.inc
@@ -52,12 +52,12 @@
     }
 
     // Get mailbox status
-    $status = $RCMAIL->storage->folder_status($mbox_name);
+    $status = $RCMAIL->storage->folder_status($mbox_name, $diff);
 
     if ($status & 1) {
         // trigger plugin hook
         $RCMAIL->plugins->exec_hook('new_messages',
-            array('mailbox' => $mbox_name, 'is_current' => $is_current));
+            array('mailbox' => $mbox_name, 'is_current' => $is_current, 'diff' => $diff));
     }
 
     rcmail_send_unread_count($mbox_name, true, null,

--
Gitblit v1.9.1