diff --git a/plugins/calendar/calendar.php b/plugins/calendar/calendar.php --- a/plugins/calendar/calendar.php +++ b/plugins/calendar/calendar.php @@ -172,6 +172,8 @@ $this->register_action('resources-owner', [$this, 'resources_owner']); $this->register_action('resources-calendar', [$this, 'resources_calendar']); $this->register_action('resources-autocomplete', [$this, 'resources_autocomplete']); + $this->register_action('talk-room-create', [$this, 'talk_room_create']); + $this->add_hook('refresh', [$this, 'refresh']); // remove undo information... @@ -3876,6 +3878,27 @@ return $args; } + /** + * Create a Nextcould Talk room + */ + public function talk_room_create() + { + require_once __DIR__ . '/lib/calendar_nextcloud_api.php'; + + $api = new calendar_nextcloud_api(); + + $name = (string) rcube_utils::get_input_value('_name', rcube_utils::INPUT_POST); + + $room_url = $api->talk_room_create($name); + + if ($room_url) { + $this->rc->output->command('plugin.talk_room_created', ['url' => $room_url]); + } + else { + $this->rc->output->command('display_message', $this->gettext('talkroomcreateerror'), 'error'); + } + } + /** * Get a list of email addresses of the current user (from login and identities) */ diff --git a/plugins/calendar/calendar_ui.js b/plugins/calendar/calendar_ui.js --- a/plugins/calendar/calendar_ui.js +++ b/plugins/calendar/calendar_ui.js @@ -3535,6 +3535,22 @@ } }; + /*** Nextcloud Talk integration ***/ + + this.talk_room_create = function() + { + var lock = rcmail.set_busy(true, 'calendar.talkroomcreating'); + + rcmail.http_post('talk-room-create', { _name: $('#edit-title').val() }, lock); + }; + + this.talk_room_created = function(data) + { + if (data.url) { + $('#edit-location').val(data.url); + } + }; + /*** startup code ***/ @@ -4184,6 +4200,7 @@ rcmail.register_command('event-sendbymail', function(p, obj, e){ cal.event_sendbymail(cal.selected_event, e); }, true); rcmail.register_command('event-copy', function(){ cal.event_copy(cal.selected_event); }, true); rcmail.register_command('event-history', function(p, obj, e){ cal.event_history_dialog(cal.selected_event); }, false); + rcmail.register_command('talk-room-create', function(){ cal.talk_room_create(); }, true); // search and export events rcmail.register_command('export', function(){ cal.export_events(cal.calendars[cal.selected_calendar]); }, true); @@ -4211,6 +4228,7 @@ rcmail.addEventListener('plugin.close_history_dialog', function(data){ cal.close_history_dialog(); }); rcmail.addEventListener('plugin.event_show_revision', function(data){ cal.event_show_dialog(data, null, true); }); rcmail.addEventListener('plugin.itip_message_processed', function(data){ cal.itip_message_processed(data); }); + rcmail.addEventListener('plugin.talk_room_created', function(data){ cal.talk_room_created(data); }); rcmail.addEventListener('requestrefresh', function(q){ return cal.before_refresh(q); }); $(window).resize(function(e) { diff --git a/plugins/calendar/config.inc.php.dist b/plugins/calendar/config.inc.php.dist --- a/plugins/calendar/config.inc.php.dist +++ b/plugins/calendar/config.inc.php.dist @@ -151,4 +151,5 @@ // See freebusy_session_auth in configuration of kolab_auth plugin. $config['calendar_freebusy_session_auth_url'] = null; -?> +// Nextcloud installation URL (for Talk integration). +$config['calendar_nextcloud_url'] = null; diff --git a/plugins/calendar/lib/calendar_nextcloud_api.php b/plugins/calendar/lib/calendar_nextcloud_api.php new file mode 100644 --- /dev/null +++ b/plugins/calendar/lib/calendar_nextcloud_api.php @@ -0,0 +1,114 @@ + + * + * Copyright (C) Apheleia IT AG + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +class calendar_nextcloud_api +{ + + /** + * Make a request to the Nextcloud API + * + * @return false|array Response data or False on failure + */ + protected function request($path, $method = 'GET', $params = []) + { + $rcmail = rcube::get_instance(); + + $url = unslashify($rcmail->config->get('calendar_nextcloud_url')); + $url .= "/ocs/v2.php/$path"; + + try { + $request_config = [ + 'store_body' => true, + 'follow_redirects' => true, + ]; + + $request = libkolab::http_request($url, $method, $request_config); + + // Authentication + $request->setAuth( + $rcmail->user->get_username(), + $rcmail->decrypt($_SESSION['password']) + ); + + // Disable CSRF prevention, and enable JSON responses + $request->setHeader([ + 'OCS-APIRequest' => 'true', + 'Accept' => 'application/json', + ]); + + if (!empty($params)) { + $request->addPostParameter($params); + } + + // Send the request + $response = $request->send(); + + $body = $response->getBody(); + $code = $response->getStatus(); + + if ($code < 400) { + return json_decode($body, true); + } + + if (strpos($body, 'loadXML($body); + $code = $doc->getElementsByTagName('statuscode')->item(0)->textContent; + $msg = $doc->getElementsByTagName('message')->item(0)->textContent; + } + else { + $msg = 'Unknown error'; + } + + throw new Exception("Nextcloud API Error: [$code] $msg"); + } + catch (Exception $e) { + rcube::raise_error($e, true, false); + } + + return false; + } + + /** + * Create a Talk room + * + * @return string|false Room URL + */ + public function talk_room_create($name = '') + { + $rcmail = rcube::get_instance(); + + $params = [ + 'roomType' => 3, + 'roomName' => $name ?: $rcmail->gettext('calendar.talkroomname'), + ]; + + $response = $this->request('apps/spreed/api/v4/room', 'POST', $params); + + if (is_array($response) && !empty($response['ocs']['data']['token'])) { + $url = unslashify($rcmail->config->get('calendar_nextcloud_url')); + return $url . '/call/' . $response['ocs']['data']['token']; + } + + return false; + } +} diff --git a/plugins/calendar/localization/en_US.inc b/plugins/calendar/localization/en_US.inc --- a/plugins/calendar/localization/en_US.inc +++ b/plugins/calendar/localization/en_US.inc @@ -321,4 +321,8 @@ $labels['arialabelresourceselection'] = 'Available resources'; $labels['arialabeleventform'] = 'Event editing form'; -?> +// Nextcloud Talk integration +$labels['createtalkroom'] = 'Create Talk room'; +$labels['talkroomcreating'] = 'Creating a Talk room for the event...'; +$labels['talkroomcreateerror'] = 'Failed to create a Talk room.'; +$labels['talkroomname'] = 'Room for an event'; diff --git a/plugins/calendar/skins/elastic/templates/eventedit.html b/plugins/calendar/skins/elastic/templates/eventedit.html --- a/plugins/calendar/skins/elastic/templates/eventedit.html +++ b/plugins/calendar/skins/elastic/templates/eventedit.html @@ -10,6 +10,9 @@
+ + +