'remove', 'default_commands' => '', ); } public function sourceForm($source_config) { $form['if_auth_fails'] = array( '#type' => 'select', '#title' => t('If authentication fails'), '#options' => array( 'remove' => t('Do not create the node'), 'unpublish' => t('Create the node, leave it unpublished'), ), '#default_value' => isset($source_config['if_auth_fails']) ? $source_config['if_auth_fails'] : 'remove', '#description' => t('If authentication is checked, here you can choose what to do if the incoming emails does not belongs to an authenticated user.'), ); return $form; } /** * Build configuration form. */ public function configForm(&$form_state) { $form = array(); $form['default_commands'] = array( '#type' => 'textarea', '#title' => t('Default commands'), '#description' => t('A set of commands which are added to each message.'), '#default_value' => $this->config['default_commands'], ); 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->parseAttachments($message); $this->parseExtensions($message); $this->authenticate($message, $mailbox); if ($message['authenticated_uid'] == 0) { // User was not authenticated $source_config = $source->getConfigFor($this); switch ($source_config['if_auth_fails']) { case 'remove': unset($messages[$mid]); break; case 'unpublish': $message['status'] = 0; break; } } else { $this->commands($message, $mailbox, $source); } } $batch->setItems($messages); } else { if (isset($fetched['new'])) { drupal_set_message('No new messages.'); } } } /** * Parse attachments from message mimeparts. */ public function parseAttachments(&$message) { $message['attachments'] = array(); foreach ($message['mimeparts'] as $attachment) { // 'unnamed_attachment' files are not really attachments, but mimeparts like HTML or Plain Text. // We only want to save real attachments, like images and files. if ($attachment->filename !== 'unnamed_attachment') { $file = file_save_data($attachment->data, file_directory_temp() . '/' . $attachment->filename); $message['attachments'][] = new FeedsEnclosure($file, $attachment->filemime); } } // This otherwise breaks batch import. Attachments are handled above. // @TODO: Ensure it's ok to really trash this like so. unset($message['mimeparts']); } /* * Set known sources and parse additional sources from body. */ public function parseExtensions(&$message) { // Populate $message with all values from 'header' object. $parts = (array) $message['header']; foreach ($parts as $key => $value) { // Some keys are already taken, so do not overwrite them. if (!in_array($key, array('header', 'origbody', 'mimeparts', 'mailbox', 'attachments'))) { $message[$key] = $value; } } } /* * This defines sources which user's can select to map values to. */ public function getMappingSources() { $sources = parent::getMappingSources(); // Make all IMAP header keys available as selectable mapping sources. $parts = array('date', 'subject', 'message_id', 'toaddress', 'to', 'fromaddress', 'from', 'reply_toaddress', 'reply_to', 'senderaddress', 'sender', 'Recent', 'Unseen', 'Flagged', 'Answered', 'Deleted', 'Draft', 'Msgno', 'MailDate', 'Size', 'udate', 'origbody', 'mimeparts', 'attachments', ); foreach ($parts as $part) { $sources[$part] = array( 'title' => t($part), 'description' => t('IMAP header property.'), ); } $sources['authenticated_uid'] = array( 'name' => t('Authenticated UID'), 'description' => t('Authenticated UID'), ); $sources['status'] = array( 'name' => t('Published status'), 'description' => t('Published status (from commands)'), ); $sources['taxonomy'] = array( 'name' => t('Taxonomy'), 'description' => t('Taxonomy terms (from commands)'), ); // @TODO: add other sources (see http://drupal.org/node/1029352#comment-3963138) as they become available in FeedsNodeProcessor return $sources; } /* * Parse and apply commands. */ public function commands(&$message, $mailbox, $source) { if (($plugins = $mailbox->commandplugin) && is_array($plugins) && !empty($plugins)) { foreach ($plugins as $plugin) { if ($class = mailhandler_plugin_load_class('mailhandler', $plugin, 'commands_plugin', 'handler')) { $class->parse($message, $source); $class->process($message, $source); } } } } /* * Authenticate the message and set $message['authenticated_uid']. */ public function authenticate(&$message, $mailbox) { // TODO: allow for multiple authenticate plugins to be weighted and implemented. if ($plugin = $mailbox->authenticateplugin) { if ($class = mailhandler_plugin_load_class('mailhandler', $plugin, 'authenticate_plugin', 'handler')) { $class->authenticate($message, $mailbox); } } } }