Official Addons for the Friendica Communications Platform. (please note that this is a clone of the repository at github, issues are handled there)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

337 lines
10 KiB

  1. <?php
  2. /**
  3. * Name: Discourse Mail Connector
  4. * Description: Improves mails from Discourse in mailing list mode
  5. * Version: 0.1
  6. * Author: Michael Vogel <http://pirati.ca/profile/heluecht>
  7. *
  8. */
  9. use Friendica\App;
  10. use Friendica\Content\Text\Markdown;
  11. use Friendica\Core\Hook;
  12. use Friendica\Core\Logger;
  13. use Friendica\Core\Protocol;
  14. use Friendica\Core\Renderer;
  15. use Friendica\Database\DBA;
  16. use Friendica\DI;
  17. use Friendica\Model\Contact;
  18. use Friendica\Util\DateTimeFormat;
  19. use Friendica\Util\Strings;
  20. /* Todo:
  21. * - Obtaining API tokens to be able to read non public posts as well
  22. * - Handling duplicates (possibly using some non visible marker)
  23. * - Fetching missing posts
  24. * - Fetch topic information
  25. * - Support mail free mode when write tokens are available
  26. * - Fix incomplete (relative) links (hosts are missing)
  27. */
  28. function discourse_install()
  29. {
  30. Hook::register('email_getmessage', __FILE__, 'discourse_email_getmessage');
  31. Hook::register('connector_settings', __FILE__, 'discourse_settings');
  32. Hook::register('connector_settings_post', __FILE__, 'discourse_settings_post');
  33. }
  34. function discourse_settings(App $a, &$s)
  35. {
  36. if (!local_user()) {
  37. return;
  38. }
  39. $enabled = intval(DI::pConfig()->get(local_user(), 'discourse', 'enabled'));
  40. $t = Renderer::getMarkupTemplate('settings.tpl', 'addon/discourse/');
  41. $s .= Renderer::replaceMacros($t, [
  42. '$title' => DI::l10n()->t('Discourse'),
  43. '$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.')],
  44. '$submit' => DI::l10n()->t('Save Settings'),
  45. ]);
  46. }
  47. function discourse_settings_post(App $a)
  48. {
  49. if (!local_user() || empty($_POST['discourse-submit'])) {
  50. return;
  51. }
  52. DI::pConfig()->set(local_user(), 'discourse', 'enabled', intval($_POST['enabled']));
  53. }
  54. function discourse_email_getmessage(App $a, &$message)
  55. {
  56. if (empty($message['item']['uid'])) {
  57. return;
  58. }
  59. if (!DI::pConfig()->get($message['item']['uid'], 'discourse', 'enabled')) {
  60. return;
  61. }
  62. // We do assume that all Discourse servers are running with SSL
  63. if (preg_match('=topic/(.*\d)/(.*\d)@(.*)=', $message['item']['uri'], $matches) &&
  64. discourse_fetch_post_from_api($message, $matches[2], $matches[3])) {
  65. Logger::info('Fetched comment via API (message-id mode)', ['host' => $matches[3], 'topic' => $matches[1], 'post' => $matches[2]]);
  66. return;
  67. }
  68. if (preg_match('=topic/(.*\d)@(.*)=', $message['item']['uri'], $matches) &&
  69. discourse_fetch_topic_from_api($message, 'https://' . $matches[2], $matches[1], 1)) {
  70. Logger::info('Fetched starting post via API (message-id mode)', ['host' => $matches[2], 'topic' => $matches[1]]);
  71. return;
  72. }
  73. // Search in the text part for the link to the discourse entry and the text body
  74. if (!empty($message['text'])) {
  75. $message = discourse_get_text($message);
  76. }
  77. if (empty($message['item']['plink']) || !preg_match('=(http.*)/t/.*/(.*\d)/(.*\d)=', $message['item']['plink'], $matches)) {
  78. Logger::info('This is no Discourse post');
  79. return;
  80. }
  81. if (discourse_fetch_topic_from_api($message, $matches[1], $matches[2], $matches[3])) {
  82. Logger::info('Fetched post via API (plink mode)', ['host' => $matches[1], 'topic' => $matches[2], 'id' => $matches[3]]);
  83. return;
  84. }
  85. Logger::info('Fallback mode', ['plink' => $message['item']['plink']]);
  86. // Search in the HTML part for the discourse entry and the author profile
  87. if (!empty($message['html'])) {
  88. $message = discourse_get_html($message);
  89. }
  90. // Remove the title on comments, they don't serve any purpose there
  91. if ($message['item']['thr-parent'] != $message['item']['uri']) {
  92. unset($message['item']['title']);
  93. }
  94. }
  95. function discourse_fetch_post($host, $topic, $pid)
  96. {
  97. $url = $host . '/t/' . $topic . '/' . $pid . '.json';
  98. $curlResult = DI::httpRequest()->get($url);
  99. if (!$curlResult->isSuccess()) {
  100. Logger::info('No success', ['url' => $url]);
  101. return false;
  102. }
  103. $raw = $curlResult->getBody();
  104. $data = json_decode($raw, true);
  105. $posts = $data['post_stream']['posts'];
  106. foreach($posts as $post) {
  107. if ($post['post_number'] != $pid) {
  108. /// @todo Possibly fetch missing posts here
  109. continue;
  110. }
  111. Logger::info('Got post data from topic', $post);
  112. return $post;
  113. }
  114. Logger::info('Post not found', ['host' => $host, 'topic' => $topic, 'pid' => $pid]);
  115. return false;
  116. }
  117. function discourse_fetch_topic_from_api(&$message, $host, $topic, $pid)
  118. {
  119. $post = discourse_fetch_post($host, $topic, $pid);
  120. if (empty($post)) {
  121. return false;
  122. }
  123. $message = discourse_process_post($message, $post, $host);
  124. return true;
  125. }
  126. function discourse_fetch_post_from_api(&$message, $post, $host)
  127. {
  128. $hostaddr = 'https://' . $host;
  129. $url = $hostaddr . '/posts/' . $post . '.json';
  130. $curlResult = DI::httpRequest()->get($url);
  131. if (!$curlResult->isSuccess()) {
  132. return false;
  133. }
  134. $raw = $curlResult->getBody();
  135. $data = json_decode($raw, true);
  136. if (empty($data)) {
  137. return false;
  138. }
  139. $message = discourse_process_post($message, $data, $hostaddr);
  140. Logger::info('Got API data', $message);
  141. return true;
  142. }
  143. function discourse_get_user($post, $hostaddr)
  144. {
  145. $host = parse_url($hostaddr, PHP_URL_HOST);
  146. // Currently unused contact fields:
  147. // - display_username
  148. // - user_id
  149. $contact = [];
  150. $contact['uid'] = 0;
  151. $contact['network'] = Protocol::DISCOURSE;
  152. $contact['name'] = $contact['nick'] = $post['username'];
  153. if (!empty($post['name'])) {
  154. $contact['name'] = $post['name'];
  155. }
  156. $contact['about'] = $post['user_title'];
  157. if (parse_url($post['avatar_template'], PHP_URL_SCHEME)) {
  158. $contact['photo'] = str_replace('{size}', '300', $post['avatar_template']);
  159. } else {
  160. $contact['photo'] = $hostaddr . str_replace('{size}', '300', $post['avatar_template']);
  161. }
  162. $contact['addr'] = $contact['nick'] . '@' . $host;
  163. $contact['contact-type'] = Contact::TYPE_PERSON;
  164. $contact['url'] = $hostaddr . '/u/' . $contact['nick'];
  165. $contact['nurl'] = Strings::normaliseLink($contact['url']);
  166. $contact['baseurl'] = $hostaddr;
  167. Logger::info('Contact', $contact);
  168. $contact['id'] = Contact::getIdForURL($contact['url'], 0, false, $contact);
  169. if (!empty($contact['id'])) {
  170. $avatar = $contact['photo'];
  171. unset($contact['photo']);
  172. DBA::update('contact', $contact, ['id' => $contact['id']]);
  173. Contact::updateAvatar($contact['id'], $avatar);
  174. $contact['photo'] = $avatar;
  175. }
  176. return $contact;
  177. }
  178. function discourse_process_post($message, $post, $hostaddr)
  179. {
  180. $host = parse_url($hostaddr, PHP_URL_HOST);
  181. $message['html'] = $post['cooked'];
  182. $contact = discourse_get_user($post, $hostaddr);
  183. $message['item']['author-id'] = $contact['id'];
  184. $message['item']['author-link'] = $contact['url'];
  185. $message['item']['author-name'] = $contact['name'];
  186. $message['item']['author-avatar'] = $contact['photo'];
  187. $message['item']['created'] = DateTimeFormat::utc($post['created_at']);
  188. $message['item']['plink'] = $hostaddr . '/t/' . $post['topic_slug'] . '/' . $post['topic_id'] . '/' . $post['post_number'];
  189. if ($post['post_number'] == 1) {
  190. $message['item']['parent-uri'] = $message['item']['uri'] = 'topic/' . $post['topic_id'] . '@' . $host;
  191. // Remove the Discourse forum name from the subject
  192. $pattern = '=\[.*\].*\s(\[.*\].*)=';
  193. if (preg_match($pattern, $message['item']['title'])) {
  194. $message['item']['title'] = preg_replace($pattern, '$1', $message['item']['title']);
  195. }
  196. /// @ToDo Fetch thread information
  197. } else {
  198. $message['item']['uri'] = 'topic/' . $post['topic_id'] . '/' . $post['id'] . '@' . $host;
  199. unset($message['item']['title']);
  200. if (empty($post['reply_to_post_number']) || $post['reply_to_post_number'] == 1) {
  201. $message['item']['parent-uri'] = 'topic/' . $post['topic_id'] . '@' . $host;
  202. } else {
  203. $reply = discourse_fetch_post($hostaddr, $post['topic_id'], $post['reply_to_post_number']);
  204. $message['item']['parent-uri'] = 'topic/' . $post['topic_id'] . '/' . $reply['id'] . '@' . $host;
  205. }
  206. }
  207. return $message;
  208. }
  209. function discourse_get_html($message)
  210. {
  211. $doc = new DOMDocument();
  212. $doc2 = new DOMDocument();
  213. $doc->preserveWhiteSpace = false;
  214. $html = mb_convert_encoding($message['html'], 'HTML-ENTITIES', "UTF-8");
  215. @$doc->loadHTML($html, LIBXML_HTML_NODEFDTD);
  216. $xpath = new DomXPath($doc);
  217. // Fetch the first 'div' before the 'hr' - hopefully this fits for all systems
  218. $result = $xpath->query("//hr//preceding::div[1]");
  219. $div = $doc2->importNode($result->item(0), true);
  220. $doc2->appendChild($div);
  221. $message['html'] = $doc2->saveHTML();
  222. Logger::info('Found html body', ['html' => $message['html']]);
  223. $profile = discourse_get_profile($xpath);
  224. if (!empty($profile['url'])) {
  225. Logger::info('Found profile', $profile);
  226. $message['item']['author-id'] = Contact::getIdForURL($profile['url'], 0, false, $profile);
  227. $message['item']['author-link'] = $profile['url'];
  228. $message['item']['author-name'] = $profile['name'];
  229. $message['item']['author-avatar'] = $profile['photo'];
  230. }
  231. return $message;
  232. }
  233. function discourse_get_text($message)
  234. {
  235. $text = $message['text'];
  236. $text = str_replace("\r", '', $text);
  237. $pos = strpos($text, "\n---\n");
  238. if ($pos == 0) {
  239. Logger::info('No separator found', ['text' => $text]);
  240. return $message;
  241. }
  242. $message['text'] = trim(substr($text, 0, $pos));
  243. Logger::info('Found text body', ['text' => $message['text']]);
  244. $message['text'] = Markdown::toBBCode($message['text']);
  245. $text = substr($text, $pos);
  246. Logger::info('Found footer', ['text' => $text]);
  247. if (preg_match('=\((http.*/t/.*/.*\d/.*\d)\)=', $text, $link)) {
  248. $message['item']['plink'] = $link[1];
  249. Logger::info('Found plink', ['plink' => $message['item']['plink']]);
  250. }
  251. return $message;
  252. }
  253. function discourse_get_profile($xpath)
  254. {
  255. $profile = [];
  256. $list = $xpath->query("//td//following::img");
  257. foreach ($list as $node) {
  258. $attr = [];
  259. foreach ($node->attributes as $attribute) {
  260. $attr[$attribute->name] = $attribute->value;
  261. }
  262. if (!empty($attr['src']) && !empty($attr['title'])
  263. && !empty($attr['width']) && !empty($attr['height'])
  264. && ($attr['width'] == $attr['height'])) {
  265. $profile = ['photo' => $attr['src'], 'name' => $attr['title']];
  266. break;
  267. }
  268. }
  269. $list = $xpath->query("//td//following::a");
  270. foreach ($list as $node) {
  271. if (!empty(trim($node->textContent)) && $node->attributes->length) {
  272. $attr = [];
  273. foreach ($node->attributes as $attribute) {
  274. $attr[$attribute->name] = $attribute->value;
  275. }
  276. if (!empty($attr['href']) && (strpos($attr['href'], '/' . $profile['name']))) {
  277. $profile['url'] = $attr['href'];
  278. break;
  279. }
  280. }
  281. }
  282. return $profile;
  283. }