Merge pull request 'Tumblr: Fixed token exchange' (#1468) from heluecht/friendica...
[friendica-addons.git/.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
11 use Friendica\App;
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;
18 use Friendica\DI;
19 use Friendica\Model\Contact;
20 use Friendica\Util\DateTimeFormat;
21 use Friendica\Util\Strings;
22
23 /* Todo:
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)
30 */
31
32 function discourse_install()
33 {
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');
37 }
38
39 function discourse_settings(array &$data)
40 {
41         if (!DI::userSession()->getLocalUserId()) {
42                 return;
43         }
44
45         $enabled = intval(DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'discourse', 'enabled'));
46
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.')],
50         ]);
51
52         $data = [
53                 'connector' => 'discourse',
54                 'title'     => DI::l10n()->t('Discourse'),
55                 'image'     => 'images/discourse.png',
56                 'enabled'   => $enabled,
57                 'html'      => $html,
58         ];
59 }
60
61 function discourse_settings_post()
62 {
63         if (!DI::userSession()->getLocalUserId() || empty($_POST['discourse-submit'])) {
64                 return;
65         }
66
67         DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'discourse', 'enabled', intval($_POST['enabled']));
68 }
69
70 function discourse_email_getmessage(&$message)
71 {
72         if (empty($message['item']['uid'])) {
73                 return;
74         }
75
76         if (!DI::pConfig()->get($message['item']['uid'], 'discourse', 'enabled')) {
77                 return;
78         }
79
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]]);
84                 return;
85         }
86
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]]);
90                 return;
91         }
92
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);
96         }
97
98         if (empty($message['item']['plink']) || !preg_match('=(http.*)/t/.*/(.*\d)/(.*\d)=', $message['item']['plink'], $matches)) {
99                 Logger::info('This is no Discourse post');
100                 return;
101         }
102
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]]);
105                 return;
106         }
107
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);
112         }
113
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']);
117         }
118 }
119
120 function discourse_fetch_post($host, $topic, $pid)
121 {
122         $url = $host . '/t/' . $topic . '/' . $pid . '.json';
123         $curlResult = DI::httpClient()->get($url);
124         if (!$curlResult->isSuccess()) {
125                 Logger::info('No success', ['url' => $url]);
126                 return false;
127         }
128
129         $raw = $curlResult->getBodyString();
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
135                         continue;
136                 }
137                 Logger::info('Got post data from topic', $post);
138                 return $post;
139         }
140
141         Logger::info('Post not found', ['host' => $host, 'topic' => $topic, 'pid' => $pid]);
142         return false;
143 }
144
145 function discourse_fetch_topic_from_api(&$message, $host, $topic, $pid)
146 {
147         $post = discourse_fetch_post($host, $topic, $pid);
148         if (empty($post)) {
149                 return false;
150         }
151
152         $message = discourse_process_post($message, $post, $host);
153         return true;
154 }
155
156 function discourse_fetch_post_from_api(&$message, $post, $host)
157 {
158         $hostaddr = 'https://' . $host;
159         $url = $hostaddr . '/posts/' . $post . '.json';
160         $curlResult = DI::httpClient()->get($url);
161         if (!$curlResult->isSuccess()) {
162                 return false;
163         }
164
165         $raw = $curlResult->getBodyString();
166         $data = json_decode($raw, true);
167         if (empty($data)) {
168                 return false;
169         }
170
171         $message = discourse_process_post($message, $data, $hostaddr);
172
173         Logger::info('Got API data', $message);
174         return true;
175 }
176
177 function discourse_get_user($post, $hostaddr)
178 {
179         $host = parse_url($hostaddr, PHP_URL_HOST);
180
181         // Currently unused contact fields:
182         // - display_username
183         // - user_id
184
185         $contact = [];
186         $contact['uid'] = 0;
187         $contact['network'] = Protocol::DISCOURSE;
188         $contact['name'] = $contact['nick'] = $post['username'];
189         if (!empty($post['name'])) {
190                 $contact['name'] = $post['name'];
191         }
192
193         $contact['about'] = $post['user_title'];
194
195         if (parse_url($post['avatar_template'], PHP_URL_SCHEME)) {
196                 $contact['photo'] = str_replace('{size}', '300', $post['avatar_template']);
197         } else {
198                 $contact['photo'] = $hostaddr . str_replace('{size}', '300', $post['avatar_template']);
199         }
200
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;
214         }
215
216         return $contact;
217 }
218
219 function discourse_process_post($message, $post, $hostaddr)
220 {
221         $host = parse_url($hostaddr, PHP_URL_HOST);
222
223         $message['html'] = $post['cooked'];
224
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'];
232
233         if ($post['post_number'] == 1) {
234                 $message['item']['parent-uri'] = $message['item']['uri'] = 'topic/' . $post['topic_id'] . '@' . $host;
235
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']);
240                 }
241                 /// @ToDo Fetch thread information
242         } else {
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;
247                 } else {
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;
250                 }
251         }
252
253         return $message;
254 }
255
256 function discourse_get_html($message)
257 {
258         $doc = new DOMDocument();
259         $doc2 = new DOMDocument();
260         $doc->preserveWhiteSpace = false;
261
262         $html = mb_convert_encoding($message['html'], 'HTML-ENTITIES', "UTF-8");
263         @$doc->loadHTML($html, LIBXML_HTML_NODEFDTD);
264
265         $xpath = new DomXPath($doc);
266
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']]);
273
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'];
281         }
282
283         return $message;
284 }
285
286 function discourse_get_text($message)
287 {
288         $text = $message['text'];
289         $text = str_replace("\r", '', $text);
290         $pos = strpos($text, "\n---\n");
291         if ($pos == 0) {
292                 Logger::info('No separator found', ['text' => $text]);
293                 return $message;
294         }
295
296         $message['text'] = trim(substr($text, 0, $pos));
297
298         Logger::info('Found text body', ['text' => $message['text']]);
299
300         $message['text'] = Markdown::toBBCode($message['text']);
301
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']]);
307         }
308         return $message;
309 }
310
311 function discourse_get_profile($xpath)
312 {
313         $profile = [];
314         $list = $xpath->query("//td//following::img");
315         foreach ($list as $node) {
316                 $attr = [];
317                 foreach ($node->attributes as $attribute) {
318                         $attr[$attribute->name] = $attribute->value;
319                 }
320
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']];
325                         break;
326                 }
327         }
328
329         $list = $xpath->query("//td//following::a");
330         foreach ($list as $node) {
331                 if (!empty(trim($node->textContent)) && $node->attributes->length) {
332                         $attr = [];
333                         foreach ($node->attributes as $attribute) {
334                                 $attr[$attribute->name] = $attribute->value;
335                         }
336                         if (!empty($attr['href']) && (strpos($attr['href'], '/' . $profile['name']))) {
337                                 $profile['url'] = $attr['href'];
338                                 break;
339                         }
340                 }
341         }
342         return $profile;
343 }