summaryrefslogtreecommitdiff
path: root/src/list_post_receive.php
blob: 9de017453e4d01cb9ddf87b3136a5d78871f3ca5 (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
<?php

# TODO: logging!
# TODO: Refactor postMessage().

require_once('Log.php');
$logger = &Log::singleton('file', '/var/log/listbridge', 'test2');

try {
  require_once('/var/www/bridge/src/Bridge.php');
  require_once('/var/www/bridge/src/MailmanLib.php');
  require_once('/var/www/bridge/src/MailmanMessage.php');
  require_once('/var/www/bridge/src/PhpBB3.php');
  
  # Read the message from STDIN
#  $url = 'php://stdin';
  
#  $input = read_raw_message($url);
#  $msg = new MailmanMessage($input);

  if (!isset($_POST['message'])) {
    throw new Exception('No message in POST');
  }

  $msg = new MailmanMessage($_POST['message']);
  
  $messageId = $msg->getMessageId();
  $inReplyTo = $msg->getInReplyTo();
  $rererences = $msg->getReferences();
  $soruce = $msg->getSource();
  
  $logger->info($messageId . ' received from ' . $source);

  $bridge = new Bridge();
  $editId = $bridge->registerByMessageId($messageId, $inReplyTo);
 
  if ($editId === false) {
    # This message has already been processed, bail out
    $logger->info($messageId . ' already seen, skipping');
    exit;
  }

  try {
    $phpbb = new PhpBB3();
  
    $forumId = $topicId = null;
    $postType = null;
  
    if ($inReplyTo) { 
      # Possibly a reply to an existing topic
      $parentId = $bridge->getPostId($inReplyTo);
      if ($parentId === false) {
        throw new Exception('unrecognized Reply-To: ' . $inReplyTo);
      }

      $ids = $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 = $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 = $phpbb->postMessage($postType, $forumId, $topicId, $msg);
    $bridge->setPostId($messageId, $postId);

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

?>