summaryrefslogtreecommitdiff
path: root/src/PhpBB3ToMailman.php
blob: 08fb244e75a13fd5e61a693af9eaa1cdee9ce066 (plain)
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<?php

#
# 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('Mail.php');

require_once(__DIR__ . '/BBCodeParser.php');
require_once(__DIR__ . '/Bridge.php');
require_once(__DIR__ . '/PhpBB3.php');
require_once(__DIR__ . '/Util.php');
require_once(__DIR__ . '/build_email.php');

class PhpBB3ToMailman {

  protected $bridge;
  protected $phpbb;
  protected $mailer;
  protected $logger;

  public function __construct(Bridge $bridge, PhpBB3 $phpbb,
                              Mail $mailer, Log $logger) {
    $this->bridge = $bridge;
    $this->phpbb = $phpbb;
    $this->mailer = $mailer;
    $this->logger = $logger;
  }

  public function process($config, $user, $mode, $data, $post_data) {
    # Sanity check
    if (!in_array($mode, array('post', 'reply', 'quote', 'edit'))) {
      throw new Exception('unrecognized mode: ' . $mode);
    }

    $postId = $data['post_id'];
    $forumId = $data['forum_id'];

    $this->logger->info($postId . ' received from phpBB forum ' . $forumId);

    $to = $this->bridge->getLists($forumId);
    if (count($to) == 0) {
      # No lists to send to, bail out.
      return;    
    }
    $to = implode(', ', $to);

    $userName = $user->data['username'];
    $userEmail = $user->data['user_email'];
  
    $sender = 'forum-bridge@vassalengine.org';
  
    $subject = html_entity_decode(
      '[' . $post_data['forum_name'] . '] ' . $post_data['post_subject'],
      ENT_QUOTES
    );

    $time = null;
    if ($mode == 'edit') {
      # Post time is NOT updated on edit, so we get the current time
      $time = time();
    }
    else {
      $time = $this->phpbb->getPostTime($postId);
      if ($time === false) {
        throw new Exception('no post time: ' . $postId);
      }
    }

    $inReplyTo = null;
    $references = null;

    if ($mode == 'reply' || $mode == 'quote') {
      $firstId = $data['topic_first_post_id'];
      $firstMessageId = $this->bridge->getMessageId($firstId);
      if ($firstMessageId === false) {
        $this->logger->info($postId . ' replies to an unknown message');
      }
      else {
        $inReplyTo = $references = $firstMessageId;
        $this->logger->info($postId . ' replies to ' . $firstMessageId);
      }
    }
    else if ($mode == 'edit') {
      $inReplyTo = $this->bridge->getMessageId($postId);
    }

    $forumURL = 'http://' . $_SERVER['SERVER_NAME'] .
                    dirname($_SERVER['SCRIPT_NAME']);

    $editId = $this->bridge->reserveEditId($postId);
    $messageId = build_message_id($postId, $editId,
                                  $time, $_SERVER['SERVER_NAME']);

    # Assemble the message headers
    $headers = build_headers(
      $userName,
      $userEmail,
      $to,
      $sender,
      $subject,
      $mode == 'edit',
      $time,
      $messageId,
      $forumURL,
      $inReplyTo,
      $references
    );

    # Build the message body
    $parser = new BBCodeParser();
    $text = $parser->parse($data['message'], $data['bbcode_uid']);
    $text = build_text($text, $mode == 'edit');

    # Build the bridge footer
    $footer = build_footer($postId, $forumURL);

    $attachments = array();
    foreach ($data['attachment_data'] as $a) {
      $attachId = $a['attach_id'];

      $adata = $this->phpbb->getAttachmentData($attachId);
      if ($adata === false) {
        throw new Exception('unrecognized attachment id: ' . $attachId);
      }

      $adata['path'] = $phpbb_root_path . $config['upload_path'] . '/' .
                       utf8_basename($adata['physical_filename']);

      $attachments[] = $adata;
    }

    # Build the message body
    $body = build_body($headers, $text, $attachments, $footer);

    # Register the message
    $seen = !$this->bridge->registerByEditId($editId, $messageId, $inReplyTo);
    if ($seen) {
      throw new Exception('message id already seen: ' . $messageId);
    }

    try {
      # Send the message
      $err = $this->mailer->send($to, $headers, $body);
      if (PEAR::isError($err)) {
        throw new Exception('Mail::send error: ' . $err->toString());
      }

      $this->logger->info($postId . ' sent to ' . $to . ' as ' . $messageId);
    }
    catch (Exception $e) {
      # Bridging failed, unregister message.
      $this->bridge->unregisterMessage($editId);
      throw $e;
    }
  }
}

?>