1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
|
<?php
#
# $Id: Bridge.php 7051 2010-07-29 13:23:57Z uckelman $
#
# forum-list bridge
# Copyright (C) 2010 Joel Uckelman
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
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();
$source = $msg->getSource();
$this->logger->info($messageId . ' received from ' . $source);
$editId = $this->bridge->registerByMessageId($messageId, $inReplyTo);
if ($editId === false) {
# This message has already been processed, bail out
$this->logger->info($messageId . ' already seen, skipping');
exit;
}
try {
list($postType, $forumId, $topicId) =
find_destination($source, $inReplyTo, $messageId);
$this->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);
$this->logger->info($messageId . ' posted as ' . $postId);
}
catch (Exception $e) {
# Bridging failed, unregister message.
$this->bridge->unregisterMessage($editId);
throw $e;
}
}
protected function find_destination($source, $inReplyTo, $messageId) {
$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';
$this->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';
$this->logger->info($messageId . ' is a new post');
}
return array($postType, $forumId, $topicId);
}
}
?>
|