4 * Name: Discourse Mail Connector
5 * Description: Improves mails from Discourse in mailing list mode
7 * Author: Michael Vogel <http://pirati.ca/profile/heluecht>
12 use Friendica\Content\Text\Markdown;
13 use Friendica\Core\Hook;
14 use Friendica\Core\Logger;
15 use Friendica\Core\Protocol;
16 use Friendica\Core\Renderer;
17 use Friendica\Database\DBA;
19 use Friendica\Model\Contact;
20 use Friendica\Util\DateTimeFormat;
21 use Friendica\Util\Strings;
24 * - Obtaining API tokens to be able to read non public posts as well
25 * - Handling duplicates (possibly using some non visible marker)
26 * - Fetching missing posts
27 * - Fetch topic information
28 * - Support mail free mode when write tokens are available
29 * - Fix incomplete (relative) links (hosts are missing)
32 function discourse_install()
34 Hook::register('email_getmessage', __FILE__, 'discourse_email_getmessage');
35 Hook::register('connector_settings', __FILE__, 'discourse_settings');
36 Hook::register('connector_settings_post', __FILE__, 'discourse_settings_post');
39 function discourse_settings(array &$data)
41 if (!DI::userSession()->getLocalUserId()) {
45 $enabled = intval(DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'discourse', 'enabled'));
47 $t = Renderer::getMarkupTemplate('connector_settings.tpl', 'addon/discourse/');
48 $html = Renderer::replaceMacros($t, [
49 '$enabled' => ['enabled', DI::l10n()->t('Enable processing of Discourse mailing list mails'), $enabled, DI::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.')],
53 'connector' => 'discourse',
54 'title' => DI::l10n()->t('Discourse'),
55 'image' => 'images/discourse.png',
56 'enabled' => $enabled,
61 function discourse_settings_post()
63 if (!DI::userSession()->getLocalUserId() || empty($_POST['discourse-submit'])) {
67 DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'discourse', 'enabled', intval($_POST['enabled']));
70 function discourse_email_getmessage(&$message)
72 if (empty($message['item']['uid'])) {
76 if (!DI::pConfig()->get($message['item']['uid'], 'discourse', 'enabled')) {
80 // We do assume that all Discourse servers are running with SSL
81 if (preg_match('=topic/(.*\d)/(.*\d)@(.*)=', $message['item']['uri'], $matches) &&
82 discourse_fetch_post_from_api($message, $matches[2], $matches[3])) {
83 Logger::info('Fetched comment via API (message-id mode)', ['host' => $matches[3], 'topic' => $matches[1], 'post' => $matches[2]]);
87 if (preg_match('=topic/(.*\d)@(.*)=', $message['item']['uri'], $matches) &&
88 discourse_fetch_topic_from_api($message, 'https://' . $matches[2], $matches[1], 1)) {
89 Logger::info('Fetched starting post via API (message-id mode)', ['host' => $matches[2], 'topic' => $matches[1]]);
93 // Search in the text part for the link to the discourse entry and the text body
94 if (!empty($message['text'])) {
95 $message = discourse_get_text($message);
98 if (empty($message['item']['plink']) || !preg_match('=(http.*)/t/.*/(.*\d)/(.*\d)=', $message['item']['plink'], $matches)) {
99 Logger::info('This is no Discourse post');
103 if (discourse_fetch_topic_from_api($message, $matches[1], $matches[2], $matches[3])) {
104 Logger::info('Fetched post via API (plink mode)', ['host' => $matches[1], 'topic' => $matches[2], 'id' => $matches[3]]);
108 Logger::info('Fallback mode', ['plink' => $message['item']['plink']]);
109 // Search in the HTML part for the discourse entry and the author profile
110 if (!empty($message['html'])) {
111 $message = discourse_get_html($message);
114 // Remove the title on comments, they don't serve any purpose there
115 if ($message['item']['thr-parent'] != $message['item']['uri']) {
116 unset($message['item']['title']);
120 function discourse_fetch_post($host, $topic, $pid)
122 $url = $host . '/t/' . $topic . '/' . $pid . '.json';
123 $curlResult = DI::httpClient()->get($url);
124 if (!$curlResult->isSuccess()) {
125 Logger::info('No success', ['url' => $url]);
129 $raw = $curlResult->getBody();
130 $data = json_decode($raw, true);
131 $posts = $data['post_stream']['posts'];
132 foreach($posts as $post) {
133 if ($post['post_number'] != $pid) {
134 /// @todo Possibly fetch missing posts here
137 Logger::info('Got post data from topic', $post);
141 Logger::info('Post not found', ['host' => $host, 'topic' => $topic, 'pid' => $pid]);
145 function discourse_fetch_topic_from_api(&$message, $host, $topic, $pid)
147 $post = discourse_fetch_post($host, $topic, $pid);
152 $message = discourse_process_post($message, $post, $host);
156 function discourse_fetch_post_from_api(&$message, $post, $host)
158 $hostaddr = 'https://' . $host;
159 $url = $hostaddr . '/posts/' . $post . '.json';
160 $curlResult = DI::httpClient()->get($url);
161 if (!$curlResult->isSuccess()) {
165 $raw = $curlResult->getBody();
166 $data = json_decode($raw, true);
171 $message = discourse_process_post($message, $data, $hostaddr);
173 Logger::info('Got API data', $message);
177 function discourse_get_user($post, $hostaddr)
179 $host = parse_url($hostaddr, PHP_URL_HOST);
181 // Currently unused contact fields:
182 // - display_username
187 $contact['network'] = Protocol::DISCOURSE;
188 $contact['name'] = $contact['nick'] = $post['username'];
189 if (!empty($post['name'])) {
190 $contact['name'] = $post['name'];
193 $contact['about'] = $post['user_title'];
195 if (parse_url($post['avatar_template'], PHP_URL_SCHEME)) {
196 $contact['photo'] = str_replace('{size}', '300', $post['avatar_template']);
198 $contact['photo'] = $hostaddr . str_replace('{size}', '300', $post['avatar_template']);
201 $contact['addr'] = $contact['nick'] . '@' . $host;
202 $contact['contact-type'] = Contact::TYPE_PERSON;
203 $contact['url'] = $hostaddr . '/u/' . $contact['nick'];
204 $contact['nurl'] = Strings::normaliseLink($contact['url']);
205 $contact['baseurl'] = $hostaddr;
206 Logger::info('Contact', $contact);
207 $contact['id'] = Contact::getIdForURL($contact['url'], 0, false, $contact);
208 if (!empty($contact['id'])) {
209 $avatar = $contact['photo'];
210 unset($contact['photo']);
211 DBA::update('contact', $contact, ['id' => $contact['id']]);
212 Contact::updateAvatar($contact['id'], $avatar);
213 $contact['photo'] = $avatar;
219 function discourse_process_post($message, $post, $hostaddr)
221 $host = parse_url($hostaddr, PHP_URL_HOST);
223 $message['html'] = $post['cooked'];
225 $contact = discourse_get_user($post, $hostaddr);
226 $message['item']['author-id'] = $contact['id'];
227 $message['item']['author-link'] = $contact['url'];
228 $message['item']['author-name'] = $contact['name'];
229 $message['item']['author-avatar'] = $contact['photo'];
230 $message['item']['created'] = DateTimeFormat::utc($post['created_at']);
231 $message['item']['plink'] = $hostaddr . '/t/' . $post['topic_slug'] . '/' . $post['topic_id'] . '/' . $post['post_number'];
233 if ($post['post_number'] == 1) {
234 $message['item']['parent-uri'] = $message['item']['uri'] = 'topic/' . $post['topic_id'] . '@' . $host;
236 // Remove the Discourse forum name from the subject
237 $pattern = '=\[.*\].*\s(\[.*\].*)=';
238 if (preg_match($pattern, $message['item']['title'])) {
239 $message['item']['title'] = preg_replace($pattern, '$1', $message['item']['title']);
241 /// @ToDo Fetch thread information
243 $message['item']['uri'] = 'topic/' . $post['topic_id'] . '/' . $post['id'] . '@' . $host;
244 unset($message['item']['title']);
245 if (empty($post['reply_to_post_number']) || $post['reply_to_post_number'] == 1) {
246 $message['item']['parent-uri'] = 'topic/' . $post['topic_id'] . '@' . $host;
248 $reply = discourse_fetch_post($hostaddr, $post['topic_id'], $post['reply_to_post_number']);
249 $message['item']['parent-uri'] = 'topic/' . $post['topic_id'] . '/' . $reply['id'] . '@' . $host;
256 function discourse_get_html($message)
258 $doc = new DOMDocument();
259 $doc2 = new DOMDocument();
260 $doc->preserveWhiteSpace = false;
262 $html = mb_convert_encoding($message['html'], 'HTML-ENTITIES', "UTF-8");
263 @$doc->loadHTML($html, LIBXML_HTML_NODEFDTD);
265 $xpath = new DomXPath($doc);
267 // Fetch the first 'div' before the 'hr' - hopefully this fits for all systems
268 $result = $xpath->query("//hr//preceding::div[1]");
269 $div = $doc2->importNode($result->item(0), true);
270 $doc2->appendChild($div);
271 $message['html'] = $doc2->saveHTML();
272 Logger::info('Found html body', ['html' => $message['html']]);
274 $profile = discourse_get_profile($xpath);
275 if (!empty($profile['url'])) {
276 Logger::info('Found profile', $profile);
277 $message['item']['author-id'] = Contact::getIdForURL($profile['url'], 0, false, $profile);
278 $message['item']['author-link'] = $profile['url'];
279 $message['item']['author-name'] = $profile['name'];
280 $message['item']['author-avatar'] = $profile['photo'];
286 function discourse_get_text($message)
288 $text = $message['text'];
289 $text = str_replace("\r", '', $text);
290 $pos = strpos($text, "\n---\n");
292 Logger::info('No separator found', ['text' => $text]);
296 $message['text'] = trim(substr($text, 0, $pos));
298 Logger::info('Found text body', ['text' => $message['text']]);
300 $message['text'] = Markdown::toBBCode($message['text']);
302 $text = substr($text, $pos);
303 Logger::info('Found footer', ['text' => $text]);
304 if (preg_match('=\((http.*/t/.*/.*\d/.*\d)\)=', $text, $link)) {
305 $message['item']['plink'] = $link[1];
306 Logger::info('Found plink', ['plink' => $message['item']['plink']]);
311 function discourse_get_profile($xpath)
314 $list = $xpath->query("//td//following::img");
315 foreach ($list as $node) {
317 foreach ($node->attributes as $attribute) {
318 $attr[$attribute->name] = $attribute->value;
321 if (!empty($attr['src']) && !empty($attr['title'])
322 && !empty($attr['width']) && !empty($attr['height'])
323 && ($attr['width'] == $attr['height'])) {
324 $profile = ['photo' => $attr['src'], 'name' => $attr['title']];
329 $list = $xpath->query("//td//following::a");
330 foreach ($list as $node) {
331 if (!empty(trim($node->textContent)) && $node->attributes->length) {
333 foreach ($node->attributes as $attribute) {
334 $attr[$attribute->name] = $attribute->value;
336 if (!empty($attr['href']) && (strpos($attr['href'], '/' . $profile['name']))) {
337 $profile['url'] = $attr['href'];