1df248a586cd9e64d7d3a4f69f842d249b4c19cb
[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 //use DOMDocument;
11 //use DOMXPath;
12 use Friendica\App;
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;
21
22 function discourse_install()
23 {
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');
28 }
29
30 function discourse_uninstall()
31 {
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');
36 }
37
38 function discourse_addon_settings(App $a, &$s)
39 {
40 }
41
42 function discourse_addon_settings_post(App $a)
43 {
44 }
45
46 function discourse_email_getmessage(App $a, &$message)
47 {
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']);
52         }
53
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])) {
57                         return;
58                 }
59         }
60
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);
65         }
66
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])) {
70                                 return;
71                         }
72                 }
73         }
74
75         // Search in the HTML part for the discourse entry and the author profile
76         if (!empty($message['html'])) {
77                 discourse_get_html($message);
78         }
79 }
80
81 function discourse_fetch_topic_from_api(&$message, $host, $topic, $pid)
82 {
83         $url = $host . '/t/' . $topic . '/posts.json?posts_ids[]=' . $pid;
84         $curlResult = Network::curl($url);
85         if (!$curlResult->isSuccess()) {
86                 return false;
87         }
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) {
93                         continue;
94                 }
95                 Logger::info('Got post data from topic', $post);
96                 discourse_process_post($message, $post);
97                 return true;
98         }
99         return false;
100 }
101
102 function discourse_fetch_post_from_api(&$message, $post, $host)
103 {
104         $url = "https://" . $host . '/posts/' . $post . '.json';
105         $curlResult = Network::curl($url);
106         if (!$curlResult->isSuccess()) {
107                 return false;
108         }
109
110         $raw = $curlResult->getBody();
111         $data = json_decode($raw, true);
112         if (empty($data)) {
113                 return false;
114         }
115
116         discourse_process_post($message, $data);
117
118         Logger::info('Got API data', $message);
119         return true;
120 }
121
122 function discourse_process_post(&$message, $post)
123 {
124         if ($post['post_number'] == 1) {
125                 // Thread information
126         }
127
128         $nick = $post['username'];
129         $name = $post['name'];
130         // User information
131
132         $message['html'] = $post['cooked'];
133         $message['text'] = $post['raw'];
134         $message['item']['created'] = DateTimeFormat::utc($post['created_at']);
135 }
136
137 function discourse_get_html(&$message)
138 {
139         $doc = new DOMDocument();
140         $doc2 = new DOMDocument();
141         $doc->preserveWhiteSpace = false;
142
143         $html = mb_convert_encoding($message['html'], 'HTML-ENTITIES', "UTF-8");
144         @$doc->loadHTML($html, LIBXML_HTML_NODEFDTD);
145
146         $xpath = new DomXPath($doc);
147
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']]);
154
155         $profile = discourse_get_profile($xpath);
156         if (!empty($profile)) {
157                 Logger::info('Found profile', $profile);
158 /*
159                 $message['item']['author-avatar'] = $contact['avatar'];
160                 $message['item']['author-link'] = $profile['link'];
161                 $message['item']['author-name'] = $profile['name'];
162 */
163         }
164 }
165
166 function discourse_get_text(&$message)
167 {
168         $text = $message['text'];
169         $text = str_replace("\r", '', $text);
170         $pos = strpos($text, "\n---\n");
171         if ($pos > 0) {
172                 $message['text'] = trim(substr($text, 0, $pos));
173                 Logger::info('Found text body', ['text' => $message['text']]);
174
175                 $message['text'] = Markdown::toBBCode($message['text']);
176
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']]);
181                 }
182         } else {
183                 Logger::info('No separator found', ['text' => $text]);
184         }
185 }
186
187 function discourse_get_profile($xpath)
188 {
189         $profile = [];
190         $list = $xpath->query("//td//following::img");
191         foreach ($list as $node) {
192                 $attr = [];
193                 foreach ($node->attributes as $attribute) {
194                         $attr[$attribute->name] = $attribute->value;
195                 }
196
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']];
201                         break;
202                 }
203         }
204
205         $list = $xpath->query("//td//following::a");
206         foreach ($list as $node) {
207                 if (!empty(trim($node->textContent)) && $node->attributes->length) {
208                         $attr = [];
209                         foreach ($node->attributes as $attribute) {
210                                 $attr[$attribute->name] = $attribute->value;
211                         }
212                         if (!empty($attr['href']) && (strpos($attr['href'], '/' . $profile['name']))) {
213                                 $profile['link'] = $attr['href'];
214                                 break;
215                         }
216                 }
217         }
218         return $profile;
219 }
220
221 function discourse_email_getmessage_end(App $a, &$message)
222 {
223 //      Logger::info('Got converted message', $message);
224 }