diff --git a/inmail.services.yml b/inmail.services.yml index 58583ca..81a899f 100644 --- a/inmail.services.yml +++ b/inmail.services.yml @@ -1,7 +1,7 @@ services: inmail.processor: class: Drupal\inmail\MessageProcessor - arguments: ['@entity.manager', '@plugin.manager.inmail.analyzer', '@plugin.manager.inmail.handler', '@logger.channel.inmail', '@inmail.mime_parser'] + arguments: ['@entity.manager', '@plugin.manager.inmail.analyzer', '@plugin.manager.inmail.handler', '@logger.channel.inmail', '@inmail.mime_parser', '@module_handler'] plugin.manager.inmail.deliverer: class: Drupal\Core\Plugin\DefaultPluginManager arguments: diff --git a/src/InmailAnalyzerResult.php b/src/InmailAnalyzerResult.php new file mode 100644 index 0000000..8499558 --- /dev/null +++ b/src/InmailAnalyzerResult.php @@ -0,0 +1,229 @@ +sender = $sender; + } + + /** + * {@inheritdoc} + */ + public function getSender() { + return $this->sender; + } + + /** + * {@inheritdoc} + */ + public function setAccount(UserInterface $account) { + $this->account = $account; + } + + /** + * {@inheritdoc} + */ + public function getAccount() { + return $this->account; + } + + /** + * {@inheritdoc} + */ + public function isUserAuthenticated() { + return $this->account ? $this->account->isAuthenticated() : FALSE; + } + + /** + * {@inheritdoc} + */ + public function summarize() { + $summary = []; + if ($this->getSender()) { + $summary['sender'] = $this->getSender(); + } + if ($this->getSubject()) { + $summary['subject'] = $this->getSubject(); + } + if ($this->getAllContexts()) { + $summary['contexts'] = t('The result contains @contexts contexts.', ['@contexts' => implode(', ', array_keys($this->getAllContexts()))]); + } + + return $summary; + } + + /** + * {@inheritdoc} + */ + public function getBody() { + return $this->body; + } + + /** + * {@inheritdoc} + */ + public function setBody($body) { + $this->body = $body; + } + + /** + * {@inheritdoc} + */ + public function getFooter() { + return $this->footer; + } + + /** + * {@inheritdoc} + */ + public function setFooter($footer) { + $this->footer = $footer; + } + + /** + * @inheritDoc + */ + public function getSubject() { + return $this->subject; + } + + /** + * @inheritDoc + */ + public function setSubject($subject) { + $this->subject = $subject; + } + + /** + * @inheritDoc + */ + public function addContext($name, ContextInterface $context) { + $this->contexts[$name] = $context; + } + + /** + * @inheritDoc + */ + public function getAllContexts() { + return $this->contexts; + } + + /** + * @inheritDoc + */ + public function hasContext($name) { + $contexts = $this->getAllContexts(); + return isset($contexts[$name]) ? TRUE : FALSE; + } + + /** + * @inheritDoc + */ + public function getContext($name) { + return $this->contexts[$name]; + } + + /** + * @inheritDoc + */ + public function getContextsWithType($type) { + $filtered_contexts = []; + + foreach ($this->getAllContexts() as $context_name => $context) { + if ($context->getContextDefinition()->getDataType() == $type) { + $filtered_contexts[$context_name] = $context; + } + } + + return $filtered_contexts; + } + + +} diff --git a/src/InmailAnalyzerResultInterface.php b/src/InmailAnalyzerResultInterface.php new file mode 100644 index 0000000..eb401c7 --- /dev/null +++ b/src/InmailAnalyzerResultInterface.php @@ -0,0 +1,157 @@ +analyzerStorage = $entity_manager->getStorage('inmail_analyzer'); $this->analyzerManager = $analyzer_manager; $this->handlerStorage = $entity_manager->getStorage('inmail_handler'); $this->handlerManager = $handler_manager; $this->loggerChannel = $logger_channel; $this->parser = $parser; + $this->moduleHandler = $module_handler; } /** @@ -95,6 +105,15 @@ class MessageProcessor implements MessageProcessorInterface { // Analyze message. $result = new ProcessorResult(); $result->setDeliverer($deliverer); + + /** @var \Drupal\inmail\InmailAnalyzerResult $default_result */ + $default_result = $result->ensureAnalyzerResult(InmailAnalyzerResult::TOPIC, InmailAnalyzerResult::createFactory()); + // @todo: Add dependency to "user" module? + if ($this->moduleHandler->moduleExists('user')) { + // Enabled analyzers will be able to update the account. + $default_result->setAccount(User::getAnonymousUser()); + } + $analyzer_configs = $this->analyzerStorage->loadMultiple(); uasort($analyzer_configs, array($this->analyzerStorage->getEntityType()->getClass(), 'sort')); foreach ($analyzer_configs as $analyzer_config) { diff --git a/src/Plugin/inmail/Analyzer/StandardDSNAnalyzer.php b/src/Plugin/inmail/Analyzer/StandardDSNAnalyzer.php index 58662eb..23b6831 100644 --- a/src/Plugin/inmail/Analyzer/StandardDSNAnalyzer.php +++ b/src/Plugin/inmail/Analyzer/StandardDSNAnalyzer.php @@ -4,6 +4,7 @@ namespace Drupal\inmail\Plugin\inmail\Analyzer; use Drupal\inmail\BounceAnalyzerResult; use Drupal\inmail\DSNStatus; +use Drupal\inmail\InmailAnalyzerResult; use Drupal\inmail\MIME\DSNEntity; use Drupal\inmail\MIME\MessageInterface; use Drupal\inmail\ProcessorResultInterface; diff --git a/src/Plugin/inmail/Analyzer/VERPAnalyzer.php b/src/Plugin/inmail/Analyzer/VERPAnalyzer.php index c52ad5d..be3cd12 100644 --- a/src/Plugin/inmail/Analyzer/VERPAnalyzer.php +++ b/src/Plugin/inmail/Analyzer/VERPAnalyzer.php @@ -3,6 +3,7 @@ namespace Drupal\inmail\Plugin\inmail\Analyzer; use Drupal\inmail\BounceAnalyzerResult; +use Drupal\inmail\InmailAnalyzerResult; use Drupal\inmail\MIME\MessageInterface; use Drupal\inmail\ProcessorResultInterface; @@ -50,6 +51,8 @@ class VerpAnalyzer extends AnalyzerBase { public function analyze(MessageInterface $message, ProcessorResultInterface $processor_result) { /** @var \Drupal\inmail\BounceAnalyzerResult $result */ $result = $processor_result->ensureAnalyzerResult(BounceAnalyzerResult::TOPIC, BounceAnalyzerResult::createFactory()); + /** @var \Drupal\inmail\InmailAnalyzerResult $default_result */ + $default_result = $processor_result->getAnalyzerResult(InmailAnalyzerResult::TOPIC); // Split the site address to facilitate matching. $return_path = \Drupal::config('inmail.settings')->get('return_path') ?: \Drupal::config('system.site')->get('mail'); @@ -65,7 +68,9 @@ class VerpAnalyzer extends AnalyzerBase { // $matches. if (preg_match(':^' . $return_path_split[0] . '\+(.*)=(.*)@' . $return_path_split[1] . '$:', $message->getTo(), $matches)) { // Report the recipient address (alice@example.com). - $result->setRecipient($matches[1] . '@' . $matches[2]); + $address = $matches[1] . '@' . $matches[2]; + $result->setRecipient($address); + $default_result->setSender($address); } } diff --git a/tests/src/Kernel/AnalyzerTest.php b/tests/src/Kernel/AnalyzerTest.php index b288a16..bab25df 100644 --- a/tests/src/Kernel/AnalyzerTest.php +++ b/tests/src/Kernel/AnalyzerTest.php @@ -3,10 +3,13 @@ namespace Drupal\Tests\inmail\Kernel; use Drupal\inmail\BounceAnalyzerResult; +use Drupal\inmail\Entity\AnalyzerConfig; use Drupal\inmail\Entity\DelivererConfig; use Drupal\inmail\Entity\HandlerConfig; +use Drupal\inmail\InmailAnalyzerResult; use Drupal\inmail_test\Plugin\inmail\Handler\ResultKeeperHandler; use Drupal\KernelTests\KernelTestBase; +use Drupal\user\Entity\User; /** * Tests analyzers. @@ -20,7 +23,7 @@ class AnalyzerTest extends KernelTestBase { * * @var array */ - public static $modules = array('inmail', 'inmail_test'); + public static $modules = array('inmail', 'inmail_test', 'user'); /** * {@inheritdoc} @@ -70,14 +73,21 @@ EOF; /** @var \Drupal\inmail\MessageProcessorInterface $processor */ $processor = \Drupal::service('inmail.processor'); + AnalyzerConfig::create(['id' => 'test_analyzer', 'plugin' => 'test_analyzer'])->save(); HandlerConfig::create(array('id' => 'result_keeper', 'plugin' => 'result_keeper'))->save(); $processor->process($raw, DelivererConfig::create(array('id' => 'test'))); $processor_result = ResultKeeperHandler::getResult(); /** @var \Drupal\inmail\BounceAnalyzerResult $result */ $result = $processor_result->getAnalyzerResult(BounceAnalyzerResult::TOPIC); + /** @var \Drupal\inmail\InmailAnalyzerResult $default_result */ + $default_result = $processor_result->getAnalyzerResult(InmailAnalyzerResult::TOPIC); $this->assertEqual($result->getRecipient(), 'verp-parsed@example.org'); + $this->assertEquals('verp-parsed@example.org', $default_result->getSender()); + + $this->assertEquals(User::getAnonymousUser()->id(), $default_result->getAccount()->id()); + $this->assertEquals('Sample context value', $default_result->getContext('test')->getContextValue()); } /** diff --git a/tests/src/Kernel/VerpTest.php b/tests/src/Kernel/VerpTest.php index 12d2e1a..50ef5c2 100644 --- a/tests/src/Kernel/VerpTest.php +++ b/tests/src/Kernel/VerpTest.php @@ -6,6 +6,7 @@ use Drupal\inmail\BounceAnalyzerResult; use Drupal\inmail\Entity\AnalyzerConfig; use Drupal\inmail\Entity\DelivererConfig; use Drupal\inmail\Entity\HandlerConfig; +use Drupal\inmail\InmailAnalyzerResult; use Drupal\inmail_test\Plugin\inmail\Handler\ResultKeeperHandler; use Drupal\Core\Language\LanguageInterface; use Drupal\KernelTests\KernelTestBase; @@ -63,8 +64,11 @@ class VerpTest extends KernelTestBase { /** @var \Drupal\inmail\BounceAnalyzerResult $result */ $result = ResultKeeperHandler::getResult()->getAnalyzerResult(BounceAnalyzerResult::TOPIC); + /** @var \Drupal\inmail\InmailAnalyzerResult $default_result */ + $default_result = ResultKeeperHandler::getResult()->getAnalyzerResult(InmailAnalyzerResult::TOPIC); $parsed_recipient = $result->getRecipient(); $this->assertEqual($parsed_recipient, $recipient); + $this->assertEquals($default_result->getSender(), $recipient); // VERP should be skipped for messages with Cc recipients. $message = \Drupal::service('plugin.manager.mail')->mail('inmail_test', 'cc', $recipient, LanguageInterface::LANGCODE_DEFAULT);