You are here

class MailhandlerParser in Mailhandler 6.2

Same name and namespace in other branches
  1. 7.2 plugins/feeds/plugins/MailhandlerParser.class.php \MailhandlerParser

Parses an IMAP stream.

Hierarchy

Expanded class hierarchy of MailhandlerParser

6 string references to 'MailhandlerParser'
MailhandlerParser.inc in plugins/feeds/plugins/MailhandlerParser.inc
mailhandler_default_feeds_importer_default in modules/mailhandler_default/mailhandler_default.feeds_importer_default.inc
Implementation of hook_feeds_importer_default().
mailhandler_help in ./mailhandler.module
Implementation of hook_help().
mailhandler_update_6202 in ./mailhandler.install
Adds new command plugins to existing Feeds importers.
mailhandler_update_6207 in ./mailhandler.install
Changes 'Body' importer mapping source to 'Body (HTML)'.

... See full list

File

plugins/feeds/plugins/MailhandlerParser.class.php, line 10
MailhandlerParser class.

View source
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);
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FeedsConfigurable::$config protected property
FeedsConfigurable::$disabled protected property CTools export enabled status of this object.
FeedsConfigurable::$export_type protected property
FeedsConfigurable::$id protected property
FeedsConfigurable::addConfig public function Similar to setConfig but adds to existing configuration. 1
FeedsConfigurable::configFormSubmit public function Submission handler for configForm(). 3
FeedsConfigurable::configFormValidate public function Validation handler for configForm(). 3
FeedsConfigurable::copy public function Copy a configuration. 1
FeedsConfigurable::existing public function Determine whether this object is persistent and enabled. I. e. it is defined either in code or in the database and it is enabled. 1
FeedsConfigurable::getConfig public function Implementation of getConfig(). 1
FeedsConfigurable::instance public static function Instantiate a FeedsConfigurable object. 1
FeedsConfigurable::setConfig public function Set configuration. 1
FeedsConfigurable::__get public function Override magic method __get(). Make sure that $this->config goes through getConfig()
FeedsConfigurable::__isset public function Override magic method __isset(). This is needed due to overriding __get().
FeedsParser::clear public function Clear all caches for results for given source.
FeedsParser::getSourceElement public function Get an element identified by $element_key of the given item. The element key corresponds to the values in the array returned by FeedsParser::getMappingSources(). 1
FeedsPlugin::hasSourceConfig public function Returns TRUE if $this->sourceForm() returns a form. Overrides FeedsSourceInterface::hasSourceConfig
FeedsPlugin::loadMappers protected static function Loads on-behalf implementations from mappers/ directory.
FeedsPlugin::save public function Save changes to the configuration of this object. Delegate saving to parent (= Feed) which will collect information from this object by way of getConfig() and store it. Overrides FeedsConfigurable::save
FeedsPlugin::sourceDelete public function A source is being deleted. Overrides FeedsSourceInterface::sourceDelete 1
FeedsPlugin::sourceFormValidate public function Validation handler for sourceForm. Overrides FeedsSourceInterface::sourceFormValidate 2
FeedsPlugin::sourceSave public function A source is being saved. Overrides FeedsSourceInterface::sourceSave 1
FeedsPlugin::__construct protected function Constructor. Overrides FeedsConfigurable::__construct
MailhandlerParser::authenticate public function
MailhandlerParser::commands public function
MailhandlerParser::configDefaults public function Override parent::configDefaults(). Overrides FeedsConfigurable::configDefaults
MailhandlerParser::configForm public function Build configuration form. Overrides FeedsConfigurable::configForm
MailhandlerParser::getMappingSources public function Declare the possible mapping sources that this parser produces. Overrides FeedsParser::getMappingSources
MailhandlerParser::parse public function Implementation of FeedsParser::parse(). Overrides FeedsParser::parse
MailhandlerParser::sourceDefaults public function Define defaults. Overrides FeedsPlugin::sourceDefaults
MailhandlerParser::sourceForm public function Callback methods, exposes source form. Overrides FeedsPlugin::sourceForm