4 * Name: Discourse Mail Connector
5 * Description: Improves mails from Discourse in mailing list mode
7 * Author: Michael Vogel <http://pirati.ca/profile/heluecht>
11 use Friendica\Core\Hook;
12 use Friendica\Core\L10n;
13 use Friendica\Core\Logger;
14 use Friendica\Core\Renderer;
15 use Friendica\Core\Protocol;
16 use Friendica\Database\DBA;
18 use Friendica\Model\Contact;
19 use Friendica\Content\Text\Markdown;
20 use Friendica\Util\Network;
21 use Friendica\Util\Strings;
22 Use Friendica\Util\DateTimeFormat;
25 * - Obtaining API tokens to be able to read non public posts as well
26 * - Handling duplicates (possibly using some non visible marker)
27 * - Fetching missing posts
28 * - Fetch topic information
29 * - Support mail free mode when write tokens are available
30 * - Fix incomplete (relative) links (hosts are missing)
33 function discourse_install()
35 Hook::register('email_getmessage', __FILE__, 'discourse_email_getmessage');
36 Hook::register('connector_settings', __FILE__, 'discourse_settings');
37 Hook::register('connector_settings_post', __FILE__, 'discourse_settings_post');
40 function discourse_settings(App $a, &$s)
46 $enabled = intval(DI::pConfig()->get(local_user(), 'discourse', 'enabled'));
48 $t = Renderer::getMarkupTemplate('settings.tpl', 'addon/discourse/');
49 $s .= Renderer::replaceMacros($t, [
50 '$title' => L10n::t('Discourse'),
51 '$enabled' => ['enabled', L10n::t('Enable processing of Discourse mailing list mails'), $enabled, L10n::t('If enabled, incoming mails from Discourse will be improved so they look much better. To make it work, you have to configure the e-mail settings in Friendica. You also have to enable the mailing list mode in Discourse. Then you have to add the Discourse mail account as contact.')],
52 '$submit' => L10n::t('Save Settings'),
56 function discourse_settings_post(App $a)
58 if (!local_user() || empty($_POST['discourse-submit'])) {
62 DI::pConfig()->set(local_user(), 'discourse', 'enabled', intval($_POST['enabled']));
65 function discourse_email_getmessage(App $a, &$message)
67 if (empty($message['item']['uid'])) {
71 if (!DI::pConfig()->get($message['item']['uid'], 'discourse', 'enabled')) {
75 // We do assume that all Discourse servers are running with SSL
76 if (preg_match('=topic/(.*\d)/(.*\d)@(.*)=', $message['item']['uri'], $matches) &&
77 discourse_fetch_post_from_api($message, $matches[2], $matches[3])) {
78 Logger::info('Fetched comment via API (message-id mode)', ['host' => $matches[3], 'topic' => $matches[1], 'post' => $matches[2]]);
82 if (preg_match('=topic/(.*\d)@(.*)=', $message['item']['uri'], $matches) &&
83 discourse_fetch_topic_from_api($message, 'https://' . $matches[2], $matches[1], 1)) {
84 Logger::info('Fetched starting post via API (message-id mode)', ['host' => $matches[2], 'topic' => $matches[1]]);
88 // Search in the text part for the link to the discourse entry and the text body
89 if (!empty($message['text'])) {
90 $message = discourse_get_text($message);
93 if (empty($message['item']['plink']) || !preg_match('=(http.*)/t/.*/(.*\d)/(.*\d)=', $message['item']['plink'], $matches)) {
94 Logger::info('This is no Discourse post');
98 if (discourse_fetch_topic_from_api($message, $matches[1], $matches[2], $matches[3])) {
99 Logger::info('Fetched post via API (plink mode)', ['host' => $matches[1], 'topic' => $matches[2], 'id' => $matches[3]]);
103 Logger::info('Fallback mode', ['plink' => $message['item']['plink']]);
104 // Search in the HTML part for the discourse entry and the author profile
105 if (!empty($message['html'])) {
106 $message = discourse_get_html($message);
109 // Remove the title on comments, they don't serve any purpose there
110 if ($message['item']['parent-uri'] != $message['item']['uri']) {
111 unset($message['item']['title']);
115 function discourse_fetch_post($host, $topic, $pid)
117 $url = $host . '/t/' . $topic . '/' . $pid . '.json';
118 $curlResult = Network::curl($url);
119 if (!$curlResult->isSuccess()) {
120 Logger::info('No success', ['url' => $url]);
124 $raw = $curlResult->getBody();
125 $data = json_decode($raw, true);
126 $posts = $data['post_stream']['posts'];
127 foreach($posts as $post) {
128 if ($post['post_number'] != $pid) {
129 /// @todo Possibly fetch missing posts here
132 Logger::info('Got post data from topic', $post);
136 Logger::info('Post not found', ['host' => $host, 'topic' => $topic, 'pid' => $pid]);
140 function discourse_fetch_topic_from_api(&$message, $host, $topic, $pid)
142 $post = discourse_fetch_post($host, $topic, $pid);
147 $message = discourse_process_post($message, $post, $host);
151 function discourse_fetch_post_from_api(&$message, $post, $host)
153 $hostaddr = 'https://' . $host;
154 $url = $hostaddr . '/posts/' . $post . '.json';
155 $curlResult = Network::curl($url);
156 if (!$curlResult->isSuccess()) {
160 $raw = $curlResult->getBody();
161 $data = json_decode($raw, true);
166 $message = discourse_process_post($message, $data, $hostaddr);
168 Logger::info('Got API data', $message);
172 function discourse_get_user($post, $hostaddr)
174 $host = parse_url($hostaddr, PHP_URL_HOST);
176 // Currently unused contact fields:
177 // - display_username
182 $contact['network'] = Protocol::DISCOURSE;
183 $contact['name'] = $contact['nick'] = $post['username'];
184 if (!empty($post['name'])) {
185 $contact['name'] = $post['name'];
188 $contact['about'] = $post['user_title'];
190 if (parse_url($post['avatar_template'], PHP_URL_SCHEME)) {
191 $contact['photo'] = str_replace('{size}', '300', $post['avatar_template']);
193 $contact['photo'] = $hostaddr . str_replace('{size}', '300', $post['avatar_template']);
196 $contact['addr'] = $contact['nick'] . '@' . $host;
197 $contact['contact-type'] = Contact::TYPE_PERSON;
198 $contact['url'] = $hostaddr . '/u/' . $contact['nick'];
199 $contact['nurl'] = Strings::normaliseLink($contact['url']);
200 $contact['baseurl'] = $hostaddr;
201 Logger::info('Contact', $contact);
202 $contact['id'] = Contact::getIdForURL($contact['url'], 0, true, $contact);
203 if (!empty($contact['id'])) {
204 $avatar = $contact['photo'];
205 unset($contact['photo']);
206 DBA::update('contact', $contact, ['id' => $contact['id']]);
207 Contact::updateAvatar($avatar, 0, $contact['id']);
208 $contact['photo'] = $avatar;
214 function discourse_process_post($message, $post, $hostaddr)
216 $host = parse_url($hostaddr, PHP_URL_HOST);
218 $message['html'] = $post['cooked'];
220 $contact = discourse_get_user($post, $hostaddr);
221 $message['item']['author-id'] = $contact['id'];
222 $message['item']['author-link'] = $contact['url'];
223 $message['item']['author-name'] = $contact['name'];
224 $message['item']['author-avatar'] = $contact['photo'];
225 $message['item']['created'] = DateTimeFormat::utc($post['created_at']);
226 $message['item']['plink'] = $hostaddr . '/t/' . $post['topic_slug'] . '/' . $post['topic_id'] . '/' . $post['post_number'];
228 if ($post['post_number'] == 1) {
229 $message['item']['parent-uri'] = $message['item']['uri'] = 'topic/' . $post['topic_id'] . '@' . $host;
231 // Remove the Discourse forum name from the subject
232 $pattern = '=\[.*\].*\s(\[.*\].*)=';
233 if (preg_match($pattern, $message['item']['title'])) {
234 $message['item']['title'] = preg_replace($pattern, '$1', $message['item']['title']);
236 /// @ToDo Fetch thread information
238 $message['item']['uri'] = 'topic/' . $post['topic_id'] . '/' . $post['id'] . '@' . $host;
239 unset($message['item']['title']);
240 if (empty($post['reply_to_post_number']) || $post['reply_to_post_number'] == 1) {
241 $message['item']['parent-uri'] = 'topic/' . $post['topic_id'] . '@' . $host;
243 $reply = discourse_fetch_post($hostaddr, $post['topic_id'], $post['reply_to_post_number']);
244 $message['item']['parent-uri'] = 'topic/' . $post['topic_id'] . '/' . $reply['id'] . '@' . $host;
251 function discourse_get_html($message)
253 $doc = new DOMDocument();
254 $doc2 = new DOMDocument();
255 $doc->preserveWhiteSpace = false;
257 $html = mb_convert_encoding($message['html'], 'HTML-ENTITIES', "UTF-8");
258 @$doc->loadHTML($html, LIBXML_HTML_NODEFDTD);
260 $xpath = new DomXPath($doc);
262 // Fetch the first 'div' before the 'hr' - hopefully this fits for all systems
263 $result = $xpath->query("//hr//preceding::div[1]");
264 $div = $doc2->importNode($result->item(0), true);
265 $doc2->appendChild($div);
266 $message['html'] = $doc2->saveHTML();
267 Logger::info('Found html body', ['html' => $message['html']]);
269 $profile = discourse_get_profile($xpath);
270 if (!empty($profile['url'])) {
271 Logger::info('Found profile', $profile);
272 $message['item']['author-id'] = Contact::getIdForURL($profile['url'], 0, true, $profile);
273 $message['item']['author-link'] = $profile['url'];
274 $message['item']['author-name'] = $profile['name'];
275 $message['item']['author-avatar'] = $profile['photo'];
281 function discourse_get_text($message)
283 $text = $message['text'];
284 $text = str_replace("\r", '', $text);
285 $pos = strpos($text, "\n---\n");
287 Logger::info('No separator found', ['text' => $text]);
291 $message['text'] = trim(substr($text, 0, $pos));
293 Logger::info('Found text body', ['text' => $message['text']]);
295 $message['text'] = Markdown::toBBCode($message['text']);
297 $text = substr($text, $pos);
298 Logger::info('Found footer', ['text' => $text]);
299 if (preg_match('=\((http.*/t/.*/.*\d/.*\d)\)=', $text, $link)) {
300 $message['item']['plink'] = $link[1];
301 Logger::info('Found plink', ['plink' => $message['item']['plink']]);
306 function discourse_get_profile($xpath)
309 $list = $xpath->query("//td//following::img");
310 foreach ($list as $node) {
312 foreach ($node->attributes as $attribute) {
313 $attr[$attribute->name] = $attribute->value;
316 if (!empty($attr['src']) && !empty($attr['title'])
317 && !empty($attr['width']) && !empty($attr['height'])
318 && ($attr['width'] == $attr['height'])) {
319 $profile = ['photo' => $attr['src'], 'name' => $attr['title']];
324 $list = $xpath->query("//td//following::a");
325 foreach ($list as $node) {
326 if (!empty(trim($node->textContent)) && $node->attributes->length) {
328 foreach ($node->attributes as $attribute) {
329 $attr[$attribute->name] = $attribute->value;
331 if (!empty($attr['href']) && (strpos($attr['href'], '/' . $profile['name']))) {
332 $profile['url'] = $attr['href'];