--- amavisd_sql.php.orig        Mon Jul 25 11:27:46 2005
+++ amavisd_sql.php     Sat Aug 13 22:29:45 2005
@@ -595,21 +595,8 @@
             if (isset($this->_options[$list])) {
                 foreach ($this->_options[$list] as $sender) {
                     if (!in_array($sender, $existing[$list])) {
-                        $query = sprintf('INSERT INTO %s (%s) VALUES (?)',
-                                         $this->_mapNameToTable('senders'),
-                                         $this->_mapAttributeToField('senders', 'email'));
-                        $values = array($sender);
-
-                        /* Log the query at a DEBUG log level. */
-                        Horde::logMessage(sprintf('SAM_Driver_amavisd_sql::_store(): %s', $query),
-                                          __FILE__, __LINE__, PEAR_LOG_DEBUG);
-
-                        $result = $this->_db->query($query, $values);
-                        if (is_a($result, 'PEAR_Error')) {
-                            Horde::logMessage($result, __FILE__, __LINE__, PEAR_LOG_ERR);
-                            return $result;
-                        }

+                        /* Check if this sender address exists already. */
                         $query = sprintf('SELECT %s FROM %s WHERE %s = ?',
                                          $this->_mapAttributeToField('senders', 'id'),
                                          $this->_mapNameToTable('senders'),
@@ -620,27 +607,74 @@
                         Horde::logMessage(sprintf('SAM_Driver_amavisd_sql::_store(): %s', $query),
                                           __FILE__, __LINE__, PEAR_LOG_DEBUG);

-                        $senderID = $this->_db->getOne($query, $values);
-                        if (is_a($senderID, 'PEAR_Error')) {
-                            Horde::logMessage($senderID, __FILE__, __LINE__, PEAR_LOG_ERR);
-                            return $senderID;
-                        }
+                        $wb_result = $this->_db->getOne($query, $values);
+                        if (is_null($wb_result)) {
+                            /* Address doesn't exist, add it. */
+                            $query = sprintf('INSERT INTO %s (%s) VALUES (?)',
+                                             $this->_mapNameToTable('senders'),
+                                             $this->_mapAttributeToField('senders', 'email'));
+                            $values = array($sender);

-                        $query = sprintf('INSERT INTO %s (%s, %s, %s) VALUES (?, ?, ?)',
-                                         $this->_mapNameToTable('wblists'),
-                                         $this->_mapAttributeToField('wblists', 'recipient'),
-                                         $this->_mapAttributeToField('wblists', 'sender'),
-                                         $this->_mapAttributeToField('wblists', 'type'));
-                        $values = array($userID, $senderID, $type);
+                            /* Log the query at a DEBUG log level. */
+                            Horde::logMessage(sprintf('SAM_Driver_amavisd_sql::_store(): %s', $query),
+                                              __FILE__, __LINE__, PEAR_LOG_DEBUG);

-                        /* Log the query at a DEBUG log level. */
-                        Horde::logMessage(sprintf('SAM_Driver_amavisd_sql::_store(): %s', $query),
-                                          __FILE__, __LINE__, PEAR_LOG_DEBUG);
+                            $result = $this->_db->query($query, $values);
+                            if (is_a($result, 'PEAR_Error')) {
+                                Horde::logMessage($result, __FILE__, __LINE__, PEAR_LOG_ERR);
+                                return $result;
+                            }

-                        $result = $this->_db->query($query, $values);
-                        if (is_a($result, 'PEAR_Error')) {
-                            Horde::logMessage($result, __FILE__, __LINE__, PEAR_LOG_ERR);
-                            return $result;
+                            $query = sprintf('SELECT %s FROM %s WHERE %s = ?',
+                                             $this->_mapAttributeToField('senders', 'id'),
+                                             $this->_mapNameToTable('senders'),
+                                             $this->_mapAttributeToField('senders', 'email'));
+                            $values = array($sender);
+
+                            /* Log the query at a DEBUG log level. */
+                            Horde::logMessage(sprintf('SAM_Driver_amavisd_sql::_store(): %s', $query),
+                                              __FILE__, __LINE__, PEAR_LOG_DEBUG);
+
+                            $senderID = $this->_db->getOne($query, $values);
+                            if (is_a($senderID, 'PEAR_Error')) {
+                                Horde::logMessage($senderID, __FILE__, __LINE__, PEAR_LOG_ERR);
+                                return $senderID;
+                            }
+
+                            $query = sprintf('INSERT INTO %s (%s, %s, %s) VALUES (?, ?, ?)',
+                                             $this->_mapNameToTable('wblists'),
+                                             $this->_mapAttributeToField('wblists', 'recipient'),
+                                             $this->_mapAttributeToField('wblists', 'sender'),
+                                             $this->_mapAttributeToField('wblists', 'type'));
+                            $values = array($userID, $senderID, $type);
+
+                            /* Log the query at a DEBUG log level. */
+                            Horde::logMessage(sprintf('SAM_Driver_amavisd_sql::_store(): %s', $query),
+                                              __FILE__, __LINE__, PEAR_LOG_DEBUG);
+
+                            $result = $this->_db->query($query, $values);
+                            if (is_a($result, 'PEAR_Error')) {
+                                Horde::logMessage($result, __FILE__, __LINE__, PEAR_LOG_ERR);
+                                return $result;
+                            }
+                        } else {
+                            /* Address exists, use it's ID */
+                            $query = sprintf('INSERT INTO %s (%s, %s, %s) VALUES (?, ?, ?)',
+                                             $this->_mapNameToTable('wblists'),
+                                             $this->_mapAttributeToField('wblists', 'recipient'),
+                                             $this->_mapAttributeToField('wblists', 'sender'),
+                                             $this->_mapAttributeToField('wblists', 'type'));
+                            $values = array($userID, $wb_result, $type);
+
+                            /* Log the query at a DEBUG log level. */
+                            Horde::logMessage(sprintf('SAM_Driver_amavisd_sql::_store(): %s', $query),
+                                              __FILE__, __LINE__, PEAR_LOG_DEBUG);
+
+                            $result = $this->_db->query($query, $values);
+                            if (is_a($result, 'PEAR_Error')) {
+                                Horde::logMessage($result, __FILE__, __LINE__, PEAR_LOG_ERR);
+                                return $result;
+                            }
                         }
                     }
                 }