]> git.mxchange.org Git - friendica-addons.git/blob - discourse/discourse.php
f53db17cf1df68d3e89b81e16d61d3ba7a1e488a
[friendica-addons.git] / discourse / discourse.php
1 <?php
2
3 /**
4  * Name: Discourse Mail Connector
5  * Description: Improves mails from Discourse in mailing list mode
6  * Version: 0.1
7  * Author: Michael Vogel <http://pirati.ca/profile/heluecht>
8  *
9  */
10 use Friendica\App;
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;
17 use Friendica\DI;
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;
23
24 /* Todo:
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)
31 */
32
33 function discourse_install()
34 {
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');
38 }
39
40 function discourse_settings(App $a, &$s)
41 {
42         if (!local_user()) {
43                 return;
44         }
45
46         $enabled = intval(DI::pConfig()->get(local_user(), 'discourse', 'enabled'));
47
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'),
53         ]);
54 }
55
56 function discourse_settings_post(App $a)
57 {
58         if (!local_user() || empty($_POST['discourse-submit'])) {
59                 return;
60         }
61
62         DI::pConfig()->set(local_user(), 'discourse', 'enabled', intval($_POST['enabled']));
63 }
64
65 function discourse_email_getmessage(App $a, &$message)
66 {
67         if (empty($message['item']['uid'])) {
68                 return;
69         }
70
71         if (!DI::pConfig()->get($message['item']['uid'], 'discourse', 'enabled')) {
72                 return;
73         }
74
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]]);
79                 return;
80         }
81
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]]);
85                 return;
86         }
87
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);
91         }
92
93         if (empty($message['item']['plink']) || !preg_match('=(http.*)/t/.*/(.*\d)/(.*\d)=', $message['item']['plink'], $matches)) {
94                 Logger::info('This is no Discourse post');
95                 return;
96         }
97
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]]);
100                 return;
101         }
102
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);
107         }
108
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']);
112         }
113 }
114
115 function discourse_fetch_post($host, $topic, $pid)
116 {
117         $url = $host . '/t/' . $topic . '/' . $pid . '.json';
118         $curlResult = Network::curl($url);
119         if (!$curlResult->isSuccess()) {
120                 Logger::info('No success', ['url' => $url]);
121                 return false;
122         }
123
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
130                         continue;
131                 }
132                 Logger::info('Got post data from topic', $post);
133                 return $post;
134         }
135
136         Logger::info('Post not found', ['host' => $host, 'topic' => $topic, 'pid' => $pid]);
137         return false;
138 }
139
140 function discourse_fetch_topic_from_api(&$message, $host, $topic, $pid)
141 {
142         $post = discourse_fetch_post($host, $topic, $pid);
143         if (empty($post)) {
144                 return false;
145         }
146
147         $message = discourse_process_post($message, $post, $host);
148         return true;
149 }
150
151 function discourse_fetch_post_from_api(&$message, $post, $host)
152 {
153         $hostaddr = 'https://' . $host;
154         $url = $hostaddr . '/posts/' . $post . '.json';
155         $curlResult = Network::curl($url);
156         if (!$curlResult->isSuccess()) {
157                 return false;
158         }
159
160         $raw = $curlResult->getBody();
161         $data = json_decode($raw, true);
162         if (empty($data)) {
163                 return false;
164         }
165
166         $message = discourse_process_post($message, $data, $hostaddr);
167
168         Logger::info('Got API data', $message);
169         return true;
170 }
171
172 function discourse_get_user($post, $hostaddr)
173 {
174         $host = parse_url($hostaddr, PHP_URL_HOST);
175
176         // Currently unused contact fields:
177         // - display_username
178         // - user_id
179
180         $contact = [];
181         $contact['uid'] = 0;
182         $contact['network'] = Protocol::DISCOURSE;
183         $contact['name'] = $contact['nick'] = $post['username'];
184         if (!empty($post['name'])) {
185                 $contact['name'] = $post['name'];
186         }
187
188         $contact['about'] = $post['user_title'];
189
190         if (parse_url($post['avatar_template'], PHP_URL_SCHEME)) {
191                 $contact['photo'] = str_replace('{size}', '300', $post['avatar_template']);
192         } else {
193                 $contact['photo'] = $hostaddr . str_replace('{size}', '300', $post['avatar_template']);
194         }
195
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;
209         }
210
211         return $contact;
212 }
213
214 function discourse_process_post($message, $post, $hostaddr)
215 {
216         $host = parse_url($hostaddr, PHP_URL_HOST);
217
218         $message['html'] = $post['cooked'];
219
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'];
227
228         if ($post['post_number'] == 1) {
229                 $message['item']['parent-uri'] = $message['item']['uri'] = 'topic/' . $post['topic_id'] . '@' . $host;
230
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']);
235                 }
236                 /// @ToDo Fetch thread information
237         } else {
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;
242                 } else {
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;
245                 }
246         }
247
248         return $message;
249 }
250
251 function discourse_get_html($message)
252 {
253         $doc = new DOMDocument();
254         $doc2 = new DOMDocument();
255         $doc->preserveWhiteSpace = false;
256
257         $html = mb_convert_encoding($message['html'], 'HTML-ENTITIES', "UTF-8");
258         @$doc->loadHTML($html, LIBXML_HTML_NODEFDTD);
259
260         $xpath = new DomXPath($doc);
261
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']]);
268
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'];
276         }
277
278         return $message;
279 }
280
281 function discourse_get_text($message)
282 {
283         $text = $message['text'];
284         $text = str_replace("\r", '', $text);
285         $pos = strpos($text, "\n---\n");
286         if ($pos == 0) {
287                 Logger::info('No separator found', ['text' => $text]);
288                 return $message;
289         }
290
291         $message['text'] = trim(substr($text, 0, $pos));
292
293         Logger::info('Found text body', ['text' => $message['text']]);
294
295         $message['text'] = Markdown::toBBCode($message['text']);
296
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']]);
302         }
303         return $message;
304 }
305
306 function discourse_get_profile($xpath)
307 {
308         $profile = [];
309         $list = $xpath->query("//td//following::img");
310         foreach ($list as $node) {
311                 $attr = [];
312                 foreach ($node->attributes as $attribute) {
313                         $attr[$attribute->name] = $attribute->value;
314                 }
315
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']];
320                         break;
321                 }
322         }
323
324         $list = $xpath->query("//td//following::a");
325         foreach ($list as $node) {
326                 if (!empty(trim($node->textContent)) && $node->attributes->length) {
327                         $attr = [];
328                         foreach ($node->attributes as $attribute) {
329                                 $attr[$attribute->name] = $attribute->value;
330                         }
331                         if (!empty($attr['href']) && (strpos($attr['href'], '/' . $profile['name']))) {
332                                 $profile['url'] = $attr['href'];
333                                 break;
334                         }
335                 }
336         }
337         return $profile;
338 }