Skip to content

Commit

Permalink
Improved/fix determining whether MDNs are available in a mailbox
Browse files Browse the repository at this point in the history
  • Loading branch information
slusarz committed Nov 18, 2014
1 parent b68f9fd commit 0ffc4bc
Show file tree
Hide file tree
Showing 10 changed files with 159 additions and 105 deletions.
6 changes: 3 additions & 3 deletions imp/lib/Ajax/Application/Handler/Dynamic.php
Expand Up @@ -889,15 +889,15 @@ public function sendMDN()
return false;
}

list($mbox, $uid) = $this->_base->indices->getSingle();
$injector->getInstance('IMP_Message_Ui')->MDNCheck(
new IMP_Indices($mbox, $uid),
$this->_base->indices->mdnCheck(
$contents->getHeaderAndMarkAsSeen(),
true
);

$notification->push(_("The Message Disposition Notification was sent successfully."), 'horde.success');

list($mbox,) = $this->_base->indices->getSingle();

$result = new stdClass;
$result->buid = $this->_base->vars->buid;
$result->mbox = $mbox->form_to;
Expand Down
2 changes: 1 addition & 1 deletion imp/lib/Ajax/Application/ShowMessage.php
Expand Up @@ -268,7 +268,7 @@ public function showMessage($args)
/* Do MDN processing now. */
switch ($registry->getView()) {
case $registry::VIEW_DYNAMIC:
if ($imp_ui->MDNCheck(new IMP_Indices($mbox, $uid), $mime_headers)) {
if ($this->_indices->mdnCheck($mime_headers)) {
$status = new IMP_Mime_Status(null, array(
_("The sender of this message is requesting notification from you when you have read this message."),
sprintf(_("Click %s to send the notification message."), Horde::link('#', '', '', '', '', '', '', array('id' => 'send_mdn_link')) . _("HERE") . '</a>')
Expand Down
8 changes: 3 additions & 5 deletions imp/lib/Factory/Maillog.php
Expand Up @@ -31,11 +31,9 @@ public function create(Horde_Injector $injector)
{
global $conf, $injector, $registry;

$storage = array();

if ($injector->getInstance('IMP_Factory_Imap')->create()->isImap()) {
$storage[] = new IMP_Maillog_Storage_Mdnsent();
}
$storage = array(
new IMP_Maillog_Storage_Mdnsent()
);

$driver = isset($conf['maillog']['driver'])
? $conf['maillog']['driver']
Expand Down
75 changes: 75 additions & 0 deletions imp/lib/Indices.php
Expand Up @@ -776,6 +776,81 @@ public function flag(array $add = array(), array $remove = array(),
return $ret;
}

/**
* Check if we need to send a MDN, and send if needed.
*
* @param Horde_Mime_Headers $headers The headers of the message.
* @param boolean $confirmed Has the MDN request been confirmed?
*
* @return boolean True if the MDN request needs to be confirmed.
*/
public function MDNCheck($headers, $confirmed = false)
{
global $conf, $injector, $prefs;

if (!($pref_val = $prefs->getValue('send_mdn'))) {
return false;
}

/* Check to see if an MDN has been requested. */
$mdn = new Horde_Mime_Mdn($headers);
if (!($return_addr = $mdn->getMdnReturnAddr())) {
return false;
}

/* Check to see if MDN information can be stored. */
$log_msg = new IMP_Maillog_Message($this);
$log_ob = new IMP_Maillog_Log_Mdn();
$maillog = $injector->getInstance('IMP_Maillog');

if (!$maillog->storage->isAvailable($log_msg, $log_ob) ||
count($maillog->getLog($log_msg, array('IMP_Maillog_Log_Mdn')))) {
return false;
}

/* See if we need to query the user. */
if (!$confirmed &&
((intval($pref_val) == 1) ||
$mdn->userConfirmationNeeded())) {
try {
if ($injector->getInstance('Horde_Core_Hooks')->callHook('mdn_check', 'imp', array($headers))) {
return true;
}
} catch (Horde_Exception_HookNotSet $e) {
return true;
}
}

/* Send out the MDN now. */
$success = false;
try {
$mdn->generate(
false,
$confirmed,
'displayed',
$conf['server']['name'],
$injector->getInstance('IMP_Mail'),
array(
'charset' => 'UTF-8',
'from_addr' => $injector->getInstance('Horde_Core_Factory_Identity')->create()->getDefaultFromAddress()
)
);

$maillog->log($log_msg, $log_ob);

$success = true;
} catch (Exception $e) {}

$injector->getInstance('IMP_Sentmail')->log(
IMP_Sentmail::MDN,
'',
$return_addr,
$success
);

return false;
}

/* ArrayAccess methods. */

/**
Expand Down
13 changes: 13 additions & 0 deletions imp/lib/Maillog/Storage/Base.php
Expand Up @@ -65,4 +65,17 @@ abstract public function deleteLogs(array $msgs);
*/
abstract public function getChanges($ts);

/**
* Is this storage driver available for the given message/log type?
*
* @param IMP_Maillog_Message $msg A message object.
* @param IMP_Maillog_Log_Base $log Log entry.
*
* @return boolean True if the storage driver can handle data for the
* given input.
*/
abstract public function isAvailable(
IMP_Maillog_Message $msg, IMP_Maillog_Log_Base $log
);

}
15 changes: 15 additions & 0 deletions imp/lib/Maillog/Storage/Composite.php
Expand Up @@ -89,4 +89,19 @@ public function getChanges($ts)
return $out;
}

/**
*/
public function isAvailable(
IMP_Maillog_Message $msg, IMP_Maillog_Log_Base $log
)
{
foreach ($this->_drivers as $val) {
if ($val->isAvailable($msg, $log)) {
return true;
}
}

return false;
}

}
24 changes: 16 additions & 8 deletions imp/lib/Maillog/Storage/History.php
Expand Up @@ -54,7 +54,9 @@ public function saveLog(
IMP_Maillog_Message $msg, IMP_Maillog_Log_Base $log
)
{
global $conf;
if (!$this->isAvailable($msg, $log)) {
return false;
}

$data = array(
'action' => $log->action,
Expand All @@ -66,13 +68,6 @@ public function saveLog(
case 'redirect':
$data['recipients'] = $log->recipients;
break;

case 'mdn':
/* Unless configured, this driver doesn't support MDN. */
if (empty($conf['maillog']['mdn_history'])) {
return false;
}
break;
}

try {
Expand Down Expand Up @@ -202,6 +197,19 @@ public function getChanges($ts)
return $out;
}

/**
*/
public function isAvailable(
IMP_Maillog_Message $msg, IMP_Maillog_Log_Base $log
)
{
global $conf;

/* Unless configured, this driver doesn't support MDN. */
return (!($log instanceof IMP_Maillog_Log_Mdn) ||
!empty($conf['maillog']['mdn_history']));
}

/**
* Generate the unique log ID for an event.
*
Expand Down
34 changes: 24 additions & 10 deletions imp/lib/Maillog/Storage/Mdnsent.php
Expand Up @@ -28,7 +28,7 @@ public function saveLog(
IMP_Maillog_Message $msg, IMP_Maillog_Log_Base $log
)
{
if (!$msg->indices || !($log instanceof IMP_Maillog_Log_Mdn)) {
if (!$this->isAvailable($msg, $log)) {
return false;
}

Expand All @@ -43,22 +43,19 @@ public function saveLog(
*/
public function getLog(IMP_Maillog_Message $msg, array $types = array())
{
if (!$msg->indices ||
(!empty($types) && !in_array('IMP_Maillog_Log_Mdn', $types))) {
$log_ob = new IMP_Maillog_Log_Mdn();

if ((!empty($types) && !in_array('IMP_Maillog_Log_Mdn', $types)) ||
!$this->isAvailable($msg, $log_ob)) {
return array();
}

list($mbox, $uid) = $msg->indices->getSingle();

if (!$mbox->permflags->allowed(Horde_Imap_Client::FLAG_MDNSENT)) {
return array();
}
$imp_imap = $mbox->imp_imap;

$query = new Horde_Imap_Client_Fetch_Query();
$query->flags();

$imp_imap = $mbox->imp_imap;

try {
$flags = $imp_imap->fetch($mbox, $query, array(
'ids' => $imp_imap->getIdsOb($uid)
Expand All @@ -68,7 +65,7 @@ public function getLog(IMP_Maillog_Message $msg, array $types = array())
}

return in_array(Horde_Imap_Client::FLAG_MDNSENT, $flags)
? array(new IMP_Maillog_Log_Mdn())
? array($log_ob)
: array();
}

Expand All @@ -87,4 +84,21 @@ public function getChanges($ts)
return array();
}

/**
*/
public function isAvailable(
IMP_Maillog_Message $msg, IMP_Maillog_Log_Base $log
)
{
if (!($log instanceof IMP_Maillog_Log_Mdn) ||
!$msg->indices) {
return false;
}

list($mbox,) = $msg->indices->getSingle();

return (!$mbox->readonly &&
($mbox->permflags->allowed(Horde_Imap_Client::FLAG_MDNSENT)));
}

}
9 changes: 9 additions & 0 deletions imp/lib/Maillog/Storage/Null.php
Expand Up @@ -51,4 +51,13 @@ public function getChanges($ts)
return array();
}

/**
*/
public function isAvailable(
IMP_Maillog_Message $msg, IMP_Maillog_Log_Base $log
)
{
return false;
}

}
78 changes: 0 additions & 78 deletions imp/lib/Message/Ui.php
Expand Up @@ -65,84 +65,6 @@ public function getUserHeaders()
return $user_hdrs;
}

/**
* Check if we need to send a MDN, and send if needed.
*
* @param IMP_Indices $indices Indices object of the message.
* @param Horde_Mime_Headers $headers The headers of the message.
* @param boolean $confirmed Has the MDN request been confirmed?
*
* @return boolean True if the MDN request needs to be confirmed.
*/
public function MDNCheck(
IMP_Indices $indices, $headers, $confirmed = false
)
{
global $conf, $injector, $prefs;

$maillog = $injector->getInstance('IMP_Maillog');
$pref_val = $prefs->getValue('send_mdn');

list($mbox, ) = $indices->getSingle();

if (!$pref_val || $mbox->readonly) {
return false;
}

/* Check to see if an MDN has been requested. */
$mdn = new Horde_Mime_Mdn($headers);
if (!($return_addr = $mdn->getMdnReturnAddr())) {
return false;
}

$log_msg = new IMP_Maillog_Message($indices);
if (count($maillog->getLog($log_msg, array('IMP_Maillog_Log_Mdn')))) {
return false;
}

/* See if we need to query the user. */
if (!$confirmed &&
((intval($pref_val) == 1) ||
$mdn->userConfirmationNeeded())) {
try {
if ($injector->getInstance('Horde_Core_Hooks')->callHook('mdn_check', 'imp', array($headers))) {
return true;
}
} catch (Horde_Exception_HookNotSet $e) {
return true;
}
}

/* Send out the MDN now. */
$success = false;
try {
$mdn->generate(
false,
$confirmed,
'displayed',
$conf['server']['name'],
$injector->getInstance('IMP_Mail'),
array(
'charset' => 'UTF-8',
'from_addr' => $injector->getInstance('Horde_Core_Factory_Identity')->create()->getDefaultFromAddress()
)
);

$maillog->log($log_msg, new IMP_Maillog_Log_Mdn());

$success = true;
} catch (Exception $e) {}

$injector->getInstance('IMP_Sentmail')->log(
IMP_Sentmail::MDN,
'',
$return_addr,
$success
);

return false;
}

/**
* Returns e-mail information for a mailing list.
*
Expand Down

0 comments on commit 0ffc4bc

Please sign in to comment.