You are here

function mailhandler_retrieve in Mailhandler 7

Same name and namespace in other branches
  1. 5 mailhandler.module \mailhandler_retrieve()
  2. 6 mailhandler.retrieve.inc \mailhandler_retrieve()

Connect to mailbox and run message retrieval

Parameters

$mailbox: Array of mailbox configuration

$mode: String, the retrieval mode, via the ui/batch system, or automated/cron/queue

$limit: Int - the maximim number of messages to fetch on retrieval, only for 'auto' mode

1 call to mailhandler_retrieve()
mailhandler_node_process_mailbox in ./mailhandler.module
Run message retrieval and node processing on a mailbox - is a wrapper around mailhandler_retrieve

File

./mailhandler.retrieve.inc, line 415

Code

function mailhandler_retrieve($mailbox, $mode, $limit = 0) {

  // This is cast as string in hook_menu, otherwise the url argument would get used.
  $limit = (int) $limit;
  if ($result = mailhandler_open_mailbox($mailbox)) {
    $new = mailhandler_get_unread_messages($result);
  }
  switch ($mode) {
    case 'ui':
      if ($result) {

        // Batch does not support using a single stream because it makes multiple page calls.
        // The stream will be opened within mailhandler_retrieve_message
        imap_close($result, CL_EXPUNGE);
        $result = 0;
        if (!empty($new)) {
          foreach ($new as $message) {
            $message_number = !$mailbox['imap'] ? 1 : $message;
            $operations[] = array(
              'mailhandler_retrieve_message',
              array(
                $result,
                $mailbox,
                $message_number,
              ),
            );
          }
          $batch = array(
            'title' => 'Mailhandler retrieve',
            'operations' => $operations,
            'finished' => 'mailhandler_batch_finished',
            'init_message' => format_plural(count($new), 'Preparing to retrieve 1 message...', 'Preparing to retrieve @count messages...'),
            'progress_message' => t('Retrieving message @current of @total.'),
            'file' => drupal_get_path('module', 'mailhandler') . '/mailhandler.retrieve.inc',
          );
          batch_set($batch);

          // Make 'progressive' mode work.  Hack due to bug http://drupal.org/node/638712
          $batch =& batch_get();
          $batch['progressive'] = FALSE;
          batch_process('admin/content/mailhandler');
        }
        else {
          drupal_set_message(t('There are no messages to retrieve for %mail.', array(
            '%mail' => $mailbox['mail'],
          )));
        }
      }
      else {
        drupal_set_message(t('Unable to connect to %mail.', array(
          '%mail' => $mailbox['mail'],
        )));
      }
      drupal_goto('admin/content/mailhandler');
      break;
    case 'auto':
      if ($result) {
        if (!empty($new)) {
          $messages = array();
          $retrieved = 0;
          while ($new && (!$limit || $retrieved < $limit)) {
            $messages[] = mailhandler_retrieve_message($result, $mailbox, array_shift($new), $context = array());
            $retrieved++;
          }
          imap_close($result, CL_EXPUNGE);
          return $messages;
        }
      }
      else {
        watchdog('mailhandler', 'Unable to connect to %mail', array(
          '%mail' => $mailbox['mail'],
        ), WATCHDOG_ERROR);
      }
      break;
  }
}