You are here

function support_client_fetch in Support Ticketing System 6

Same name and namespace in other branches
  1. 7 support.module \support_client_fetch()

Fetch mail for a specific client.

1 call to support_client_fetch()
support_fetch_client_mail in ./support.module
Automatically download messages for all active clients.
1 string reference to 'support_client_fetch'
support_menu in ./support.module
Implementation of hook_menu().

File

./support.module, line 3332
support.module

Code

function support_client_fetch($client, $manual = TRUE) {
  if (!isset($client->integrate_email) || $client->integrate_email != TRUE) {
    if ($manual) {
      drupal_set_message(t('Client not integrated with email, unable to fetch mail for !client.', array(
        '!client' => $client->name,
      )));
      drupal_goto('admin/support/clients');
    }
    return;
  }
  set_time_limit(0);
  if ($manual) {
    drupal_set_message(t('Fetching mail for !client...', array(
      '!client' => $client->name,
    )));
  }
  $connect = '{' . $client->server_name . ':' . $client->port;
  $username = $client->server_username;
  $password = $client->server_password;
  $extra = $client->extra;
  switch ($client->protocol) {
    case 0:

      // POP3
      $connect .= "/pop3{$extra}}" . $client->mailbox;
      break;
    case 1:

      // POP3S
      $connect .= "/pop3/ssl{$extra}}" . $client->mailbox;
      break;
    case 2:

      // IMAP
      $connect .= "{$extra}}" . $client->mailbox;
      break;
    case 3:

      // IMAPS
      $connect .= "/imap/ssl{$extra}}" . $client->mailbox;
      break;
    case 4:

      // Local file
      $connect = $client->mailbox;
      $username = $password = '';

      // sanity tests
      if (!file_exists($connect) && $manual) {
        drupal_set_message(t('Mail file "%connect" does not exist.', array(
          '%connect' => $connect,
        )), 'error');
      }
      else {
        if (!is_readable($connect) && $manual) {
          drupal_set_message(t('Mail file "%connect" is not readable.', array(
            '%connect' => $connect,
          )), 'error');
        }
        else {
          if (!is_writable($connect) && $manual) {
            drupal_set_message(t('Mail file "%connect" is not writable.', array(
              '%connect' => $connect,
            )));
          }
        }
      }
      break;
  }

  // Make a connection to the mail server.
  $stream = imap_open($connect, $username, $password);
  if ($stream === FALSE) {
    if ($manual) {
      drupal_set_message(t('Failed to download messages for %client, connection to mail server failed.', array(
        '%client' => $client->name,
      ), 'error'));
      if (user_access('administer support')) {

        // Dump additional debug if manually calling as administrator
        drupal_set_message(t('Mail server connection failure: connect(!connect), username(!username), password(!password)', array(
          '!connect' => $connect,
          '!username' => $username,
          '!password' => $password,
        )), 'error');
      }
    }
    if ($alerts = imap_alerts()) {
      foreach ($alerts as $alert) {
        watchdog('support', 'Imap alert: %alert for user %username', array(
          '%alert' => $alert,
          '%username' => $username,
        ));
        if ($manual) {
          drupal_set_message(t('Imap alert: %alert for user %username', array(
            '%alert' => $alert,
            '%username' => $username,
          )), 'error');
        }
      }
    }
    if ($errors = imap_errors()) {
      foreach ($errors as $error) {
        watchdog('support', 'Imap error: %error for user %username', array(
          '%error' => $error,
          '%username' => $username,
        ), WATCHDOG_NOTICE);
        if ($manual) {
          drupal_set_message(t('Imap error: %error for user %username', array(
            '%error' => $error,
            '%username' => $username,
          )), 'error');
        }
      }
    }
    if ($manual) {
      drupal_goto('admin/support/clients');
    }
    else {
      return -1;
    }
  }
  $messages_downloaded = 0;

  // check how many messages are available
  $messages_to_download = imap_num_msg($stream);
  $messages_limit = variable_get('support_download_limit', 1000);
  if ($messages_limit && $messages_limit < $messages_to_download) {

    // TODO: watchdog, there are more messages available
    $messages_to_download = $messages_limit;
  }
  for ($number = 1; $number <= $messages_to_download; $number++) {
    $message = array();
    $message['headers'] = imap_headerinfo($stream, $number);
    $message['headers_raw'] = imap_fetchbody($stream, $number, 0);
    if (is_array($message['headers']->from)) {
      $message['from'] = $message['headers']->from[0]->mailbox . '@' . $message['headers']->from[0]->host;
    }
    $strings = imap_mime_header_decode($message['headers']->subject);
    $message['subject'] = '';
    foreach ($strings as $string) {
      if ($string->charset == 'default') {
        $message['subject'] .= $string->text;
      }
      else {
        $message['subject'] .= drupal_convert_to_utf8($string->text, $string->charset);
      }
    }
    _support_identify_ticket($client, $message);
    $structure = imap_fetchstructure($stream, $number);
    $mime = _support_get_filemime($structure);
    $message['body'] = _support_get_message_body_part($stream, $number, $mime, $structure);
    if (isset($structure->parts)) {
      $parts = count($structure->parts);
      if ($parts > 1) {
        $message['attachments'] = _support_get_attachments($stream, $number, $structure, $parts);
      }
    }
    drupal_alter('support_fetch_message', $message, $client);
    $saved = support_save_message($message, $client, $manual);
    $messages_downloaded++;

    // mark message for deletion
    imap_delete($stream, $number);
  }
  imap_close($stream, CL_EXPUNGE);
  if ($manual) {
    drupal_set_message(t('Downloaded !count', array(
      '!count' => format_plural($messages_downloaded, '1 message', '@count messages'),
    )));
    drupal_goto('admin/support/clients');
  }
}