You are here

MailhandlerParser.class.php in Mailhandler 6.2

Same filename and directory in other branches
  1. 7.2 plugins/feeds/plugins/MailhandlerParser.class.php

MailhandlerParser class.

File

plugins/feeds/plugins/MailhandlerParser.class.php
View source
<?php

/**
 * @file
 * MailhandlerParser class.
 */

/**
 * Parses an IMAP stream.
 */
class MailhandlerParser extends FeedsParser {

  /**
   * Override parent::configDefaults().
   */
  public function configDefaults() {
    return array(
      'available_commands' => 'status',
      'authenticate_plugin' => 'MailhandlerAuthenticateDefault',
      'auth_required' => TRUE,
      'default_commands' => 'status: 1',
      'commands_failed_auth' => 'status: 0',
    );
  }

  /**
   * Define defaults.
   */
  public function sourceDefaults() {
    return array(
      'auth_required' => $this->config['auth_required'],
      'default_commands' => $this->config['default_commands'],
      'commands_failed_auth' => $this->config['commands_failed_auth'],
    );
  }
  public function sourceForm($source_config) {
    $form['auth_required'] = array(
      '#type' => 'checkbox',
      '#title' => t('Skip messages that fail authentication'),
      '#default_value' => isset($source_config['auth_required']) ? $source_config['auth_required'] : '',
      '#description' => t('If checked, only messages that belong to an authenticated user will be imported.'),
    );
    $plugins = mailhandler_get_plugins('mailhandler', 'commands');
    foreach ($plugins as $plugin_name => $plugin) {
      if ($class = mailhandler_plugin_load_class('mailhandler', $plugin_name, 'commands', 'handler')) {
        $class
          ->sourceForm($form, $source_config);
      }
    }
    return $form;
  }

  /**
   * Build configuration form.
   */
  public function configForm(&$form_state) {
    $form = array();
    ctools_include('plugins');
    $form['authenticate_plugin'] = array(
      '#type' => 'select',
      '#title' => t('Authentication plugin'),
      '#description' => t('Choose an authentication plugin'),
      '#options' => _mailhandler_build_options(mailhandler_get_plugins('mailhandler', 'authenticate')),
      '#default_value' => $this->config['authenticate_plugin'],
      '#required' => FALSE,
    );
    $plugins = mailhandler_get_plugins('mailhandler', 'commands');
    foreach ($plugins as $plugin_name => $plugin) {
      if ($class = mailhandler_plugin_load_class('mailhandler', $plugin_name, 'commands', 'handler')) {
        $class
          ->configForm($form, $form_state, $this->config);
      }
    }
    return $form;
  }

  /**
   * Implementation of FeedsParser::parse().
   */
  public function parse(FeedsImportBatch $batch, FeedsSource $source) {
    $fetched = $batch
      ->getRaw();
    $mailbox = $fetched['mailbox'];
    $messages = $fetched['messages'];
    if (!empty($messages)) {
      foreach ($messages as $mid => &$message) {
        $this
          ->authenticate($message, $mailbox);
        if ($class = mailhandler_plugin_load_class('mailhandler', $mailbox->settings['retrieve'], 'retrieve', 'handler')) {
          $class
            ->purge_message($mailbox, $message);
        }
        if ($message['authenticated_uid'] == 0) {

          // User was not authenticated
          module_invoke_all('mailhandler_auth', 'auth_failed', $message);
          $source_config = $source
            ->getConfigFor($this);
          if ($source_config['auth_required']) {
            mailhandler_report('warning', 'User could not be authenticated. Please check your Mailhandler authentication plugin settings.');
            unset($messages[$mid]);
            continue;
          }
        }
        $this
          ->commands($message, $source);
      }
      $batch
        ->setItems($messages);
    }
    else {
      if (isset($fetched['new'])) {
        mailhandler_report('info', 'No new messages.');
      }
    }
  }

  /*
   * This defines sources which user's can select to map values to.
   */
  public function getMappingSources() {
    $sources = parent::getMappingSources();
    $sources['authenticated_uid'] = array(
      'name' => t('User ID'),
      'description' => t('The authenticated Drupal user ID'),
    );
    $plugins = mailhandler_get_plugins('mailhandler', 'commands');
    foreach ($plugins as $plugin_name => $plugin) {
      $plugin = mailhandler_plugin_load_class('mailhandler', $plugin_name, 'commands', 'handler');
      $sources = array_merge($sources, $plugin
        ->getMappingSources($this->config));
    }
    return $sources;
  }

  /*
   * Parse and apply commands.
   */
  public function commands(&$message, $source) {
    $plugins = mailhandler_get_plugins('mailhandler', 'commands');
    foreach ($plugins as $plugin_name => $plugin) {
      if ($class = mailhandler_plugin_load_class('mailhandler', $plugin_name, 'commands', 'handler')) {
        $class
          ->parse($message, $source, $this);
        $class
          ->process($message, $source);
      }
    }
  }

  /*
   * Authenticate the message and set $message['authenticated_uid'].
   */
  public function authenticate(&$message, $mailbox) {
    if ($plugin = $this->config['authenticate_plugin']) {
      if ($class = mailhandler_plugin_load_class('mailhandler', $plugin, 'authenticate', 'handler')) {
        $message['authenticated_uid'] = $class
          ->authenticate($message, $mailbox);
      }
    }
  }

}

Classes

Namesort descending Description
MailhandlerParser Parses an IMAP stream.