4 * Name: Discourse Mail Connector
5 * Description: Improves mails from Discourse in mailing list mode
7 * Author: Michael Vogel <http://pirati.ca/profile/heluecht>
13 use Friendica\Core\Hook;
14 use Friendica\Core\L10n;
15 use Friendica\Core\Logger;
16 use Friendica\Core\PConfig;
17 use Friendica\Util\XML;
18 use Friendica\Content\Text\Markdown;
19 use Friendica\Util\Network;
20 Use Friendica\Util\DateTimeFormat;
22 function discourse_install()
24 Hook::register('email_getmessage', __FILE__, 'discourse_email_getmessage');
25 Hook::register('email_getmessage_end', __FILE__, 'discourse_email_getmessage_end');
26 Hook::register('addon_settings', __FILE__, 'discourse_addon_settings');
27 Hook::register('addon_settings_post', __FILE__, 'discourse_addon_settings_post');
30 function discourse_uninstall()
32 Hook::unregister('email_getmessage', __FILE__, 'discourse_email_getmessage');
33 Hook::unregister('email_getmessage_end', __FILE__, 'discourse_email_getmessage_end');
34 Hook::unregister('addon_settings', __FILE__, 'discourse_addon_settings');
35 Hook::unregister('addon_settings_post', __FILE__, 'discourse_addon_settings_post');
38 function discourse_addon_settings(App $a, &$s)
42 function discourse_addon_settings_post(App $a)
46 function discourse_email_getmessage(App $a, &$message)
48 // Logger::info('Got raw message', $message);
49 // Remove the title on comments, they don't serve any purpose there
50 if ($message['item']['parent-uri'] != $message['item']['uri']) {
51 unset($message['item']['title']);
54 if (preg_match('=topic/(.*)/(.*)@(.*)=', $message['item']['uri'], $matches)) {
55 Logger::info('Got post data', ['topic' => $matches[1], 'post' => $matches[2], 'host' => $matches[3]]);
56 if (discourse_fetch_post_from_api($message, $matches[2], $matches[3])) {
61 // Search in the text part for the link to the discourse entry and the text body
62 // The text body is used as alternative, if the fetched HTML isn't working
63 if (!empty($message['text'])) {
64 discourse_get_text($message);
67 if (!empty($message['item']['plink'])) {
68 if (preg_match('=(http.*)/t/.*/(.*\d)/(.*\d)=', $message['item']['plink'], $matches)) {
69 if (discourse_fetch_topic_from_api($message, $matches[1], $matches[1], $matches[1])) {
75 // Search in the HTML part for the discourse entry and the author profile
76 if (!empty($message['html'])) {
77 discourse_get_html($message);
81 function discourse_fetch_topic_from_api(&$message, $host, $topic, $pid)
83 $url = $host . '/t/' . $topic . '/posts.json?posts_ids[]=' . $pid;
84 $curlResult = Network::curl($url);
85 if (!$curlResult->isSuccess()) {
88 $raw = $curlResult->getBody();
89 $data = json_decode($raw, true);
90 $posts = $data['post_stream']['posts'];
91 foreach($posts as $post) {
92 if ($post['post_number'] != $pid) {
95 Logger::info('Got post data from topic', $post);
96 discourse_process_post($message, $post);
102 function discourse_fetch_post_from_api(&$message, $post, $host)
104 $url = "https://" . $host . '/posts/' . $post . '.json';
105 $curlResult = Network::curl($url);
106 if (!$curlResult->isSuccess()) {
110 $raw = $curlResult->getBody();
111 $data = json_decode($raw, true);
116 discourse_process_post($message, $data);
118 Logger::info('Got API data', $message);
122 function discourse_process_post(&$message, $post)
124 if ($post['post_number'] == 1) {
125 // Thread information
128 $nick = $post['username'];
129 $name = $post['name'];
132 $message['html'] = $post['cooked'];
133 $message['text'] = $post['raw'];
134 $message['item']['created'] = DateTimeFormat::utc($post['created_at']);
137 function discourse_get_html(&$message)
139 $doc = new DOMDocument();
140 $doc2 = new DOMDocument();
141 $doc->preserveWhiteSpace = false;
143 $html = mb_convert_encoding($message['html'], 'HTML-ENTITIES', "UTF-8");
144 @$doc->loadHTML($html, LIBXML_HTML_NODEFDTD);
146 $xpath = new DomXPath($doc);
148 // Fetch the first 'div' before the 'hr' -hopefully this fits for all systems
149 $result = $xpath->query("//hr//preceding::div[1]");
150 $div = $doc2->importNode($result->item(0), true);
151 $doc2->appendChild($div);
152 $message['html'] = $doc2->saveHTML();
153 Logger::info('Found html body', ['html' => $message['html']]);
155 $profile = discourse_get_profile($xpath);
156 if (!empty($profile)) {
157 Logger::info('Found profile', $profile);
159 $message['item']['author-avatar'] = $contact['avatar'];
160 $message['item']['author-link'] = $profile['link'];
161 $message['item']['author-name'] = $profile['name'];
166 function discourse_get_text(&$message)
168 $text = $message['text'];
169 $text = str_replace("\r", '', $text);
170 $pos = strpos($text, "\n---\n");
172 $message['text'] = trim(substr($text, 0, $pos));
173 Logger::info('Found text body', ['text' => $message['text']]);
175 $message['text'] = Markdown::toBBCode($message['text']);
177 $text = substr($text, $pos);
178 if (preg_match('=\((http.*?)\)=', $text, $link)) {
179 $message['item']['plink'] = $link[1];
180 Logger::info('Found plink', ['plink' => $message['item']['plink']]);
183 Logger::info('No separator found', ['text' => $text]);
187 function discourse_get_profile($xpath)
190 $list = $xpath->query("//td//following::img");
191 foreach ($list as $node) {
193 foreach ($node->attributes as $attribute) {
194 $attr[$attribute->name] = $attribute->value;
197 if (!empty($attr['src']) && !empty($attr['title'])
198 && !empty($attr['width']) && !empty($attr['height'])
199 && ($attr['width'] == $attr['height'])) {
200 $profile = ['avatar' => $attr['src'], 'name' => $attr['title']];
205 $list = $xpath->query("//td//following::a");
206 foreach ($list as $node) {
207 if (!empty(trim($node->textContent)) && $node->attributes->length) {
209 foreach ($node->attributes as $attribute) {
210 $attr[$attribute->name] = $attribute->value;
212 if (!empty($attr['href']) && (strpos($attr['href'], '/' . $profile['name']))) {
213 $profile['link'] = $attr['href'];
221 function discourse_email_getmessage_end(App $a, &$message)
223 // Logger::info('Got converted message', $message);