From 88d4c55ec38c68c07496ed059616b15622fc83e7 Mon Sep 17 00:00:00 2001 From: uckelman Date: Sun, 31 Oct 2010 20:53:21 +0000 Subject: Major refactoring to make bridge easier to test. git-svn-id: https://vassalengine.svn.sourceforge.net/svnroot/vassalengine/site-src/trunk@7431 67b53d14-2c14-4ace-a08f-0dab2b34000c --- src/MailmanToPhpBB3.php | 108 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 src/MailmanToPhpBB3.php (limited to 'src/MailmanToPhpBB3.php') diff --git a/src/MailmanToPhpBB3.php b/src/MailmanToPhpBB3.php new file mode 100644 index 0000000..b4ef325 --- /dev/null +++ b/src/MailmanToPhpBB3.php @@ -0,0 +1,108 @@ +. +# + +require_once('Log.php'); + +require_once(__DIR__ . '/Bridge.php'); +require_once(__DIR__ . '/Message.php'); +require_once(__DIR__ . '/PhpBB3.php'); + +class MailmanToPhpBB3 { + protected $bridge; + protected $phpbb; + protected $logger; + + public function __construct(Bridge $bridge, PhpBB3 $phpbb, Log $logger) { + $this->bridge = $bridge; + $this->phpbb = $phpbb; + $this->logger = $logger; + } + + public function process(Message $msg) { + $messageId = $msg->getMessageId(); + $inReplyTo = $msg->getInReplyTo(); + $rererences = $msg->getReferences(); + $soruce = $msg->getSource(); + + $logger->info($messageId . ' received from ' . $source); + + $editId = $this->bridge->registerByMessageId($messageId, $inReplyTo); + + if ($editId === false) { + # This message has already been processed, bail out + $logger->info($messageId . ' already seen, skipping'); + exit; + } + + try { + $forumId = $topicId = null; + $postType = null; + + if ($inReplyTo) { + # Possibly a reply to an existing topic + $parentId = $this->bridge->getPostId($inReplyTo); + if ($parentId === false) { + throw new Exception('unrecognized Reply-To: ' . $inReplyTo); + } + + $ids = $this->phpbb->getTopicAndForumIds($parentId); + if ($ids === false) { + throw new Exception('unrecognized parent id: ' . $parentId); + } + + # Found the parent's forum and topic, post to those + $forumId = $ids['forum_id']; + $topicId = $ids['topic_id']; + $postType = 'reply'; + + $logger->info($messageId . ' replies to ' . $parentId); + } + else { + # A message starting a new topic, post to default forum for its source + $forumId = $this->bridge->getDefaultForumId($source); + if ($forumId === false) { + throw new Exception('unrecognized source: ' . $source); + } + + $postType = 'post'; + + $logger->info($messageId . ' is a new post'); + } + + $logger->info( + $messageId . ' will be posted to ' . $forumId . ':' . $topicId); + + # Post the message to the forum + $postId = $this->phpbb->postMessage($postType, $forumId, $topicId, $msg); + $this->bridge->setPostId($messageId, $postId); + + $logger->info($messageId . ' posted as ' . $postId); + } + catch (Exception $e) { + # Bridging failed, unregister message. + $this->bridge->unregisterMessage($editId); + throw $e; + } + } +} + +?> -- cgit v1.2.3