From 4224bc51ec10a8652e15173fc9a03c82ef32857f Mon Sep 17 00:00:00 2001 From: drlight17 <37434652+drlight17@users.noreply.github.com> Date: Sun, 26 Feb 2023 19:33:53 +0300 Subject: [PATCH] v.0.9.1-beta update --- composer.json | 37 ++++++ config.inc.php.dist | 70 +++++++---- converse.php | 190 +++++++++++++++++++---------- js/converse-rcmail.js | 270 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 483 insertions(+), 84 deletions(-) create mode 100644 composer.json create mode 100644 js/converse-rcmail.js diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..3193827 --- /dev/null +++ b/composer.json @@ -0,0 +1,37 @@ +{ + "name": "converse", + "description": "Adds support of conversejs to RC.", + "homepage": "https://github.com/conversejs/roundcube-plugin/", + "license": "MIT", + "type": "roundcube-plugin", + "version": "0.9.1-beta", + "authors": [ + { + "name": "Yuri Samoilov", + "email": "root@drlight.fun", + "role": "Developer" + + }, + { + "name": "Dennis Schridde", + "email": "https://github.com/devurandom", + "role": "Developer" + + }, + { + "name": "Priyadi Iman Nurcahyo", + "email": "http://priyadi.net", + "role": "Developer" + + } + ], + "require": { + "php": ">=5.4.0", + "roundcube/plugin-installer": ">=0.1.2" + }, + "extra": { + "roundcube": { + "min-version": "1.4" + } + } +} \ No newline at end of file diff --git a/config.inc.php.dist b/config.inc.php.dist index 2cff364..d49b0d7 100644 --- a/config.inc.php.dist +++ b/config.inc.php.dist @@ -4,10 +4,30 @@ // $args['user'] : IMAP username // $args['pass'] : IMAP password +// XMPP auth method for conversejs (possible values are "login" and "prebind") +// Login method will use credentials_url and prebind - prebind_url. For prebind you MUST +// specify converse_xmpp_bosh_prebind_url and converse_xmpp_bosh_url parameters below and set converse_xmpp_conn_method to websocket! +$rcmail_config['converse_xmpp_auth_method'] = 'login'; + +// XMPP connection method (possible values are "websocket" and "bosh") +// For BOSH you MUST specify converse_xmpp_bosh_prebind_url and converse_xmpp_bosh_url. +// For websocket you MUST specify converse_xmpp_websocket_url +// websocket is highly recommended as more fast, reliable and stable connection method +$rcmail_config['converse_xmpp_conn_method'] = 'websocket'; + +// Hostname of websocket endpoint, used by web browsers (called by Javascript code), +// this can be a relative URL +$rcmail_config['converse_xmpp_websocket_url']= function($args) { + //return '/ws'; + return 'wss://your-xmpp.domain.com:7443/ws'; + +}; + // Hostname of BOSH endpoint, used for prebinding only (called by PHP), // this must be an absolute URL $rcmail_config['converse_xmpp_bosh_prebind_url']= function($args) { - return 'http://localhost:5280/http-bind'; + //return 'http://localhost:5280/http-bind'; + return 'https://your-xmpp.domain.com:7443/http-bind'; // return sprintf('http://%s/http-bind', $_SERVER['HTTP_HOST']); // return sprintf('http://%s/http-bind', $args['host']); }; @@ -16,13 +36,16 @@ $rcmail_config['converse_xmpp_bosh_prebind_url']= function($args) { // this can be a relative URL $rcmail_config['converse_xmpp_bosh_url']= function($args) { //return '/http-bind'; - return 'http://localhost:5280/http-bind'; + return 'https://your-xmpp.domain.com:7443/http-bind'; + //return 0; }; // Hostname portion of XMPP username aka XMPP domain (bare JID), example: "example.net" $rcmail_config['converse_xmpp_hostname']= function($args) { + return 'domain.com'; + //return; // return preg_replace('/^.*@/', '', $args['user']); - return $args['host']; + // return $args['host']; }; // Username portion of XMPP username (bare JID), example: "user" @@ -38,40 +61,52 @@ $rcmail_config['converse_xmpp_password']= function($args) { return $args['pass']; }; +// URL prefix of the CDN where Converse.js is being included from. +// May point to a local path, if you use the same directory structure as +// on the CDN (dist/ + css/). +//$rcmail_config['converse_cdn'] = 'https://cdn.conversejs.org/5.0.5'; // last version with previous avatar design and more stable user connection status +$rcmail_config['converse_cdn'] = 'https://cdn.conversejs.org/10.1.2'; +//$rcmail_config['converse_cdn'] = 'https://cdn.conversejs.org/9.1.1'; + // Additional converse.js option to use // refer to https://conversejs.org/docs/html/index.html#configuration-variables -// some options are overriden: expose_rid_and_sid, bosh_service_url, debug, -// prebind, jid, sid, rid +// some options are overriden by plugin so there is no need to fill them: +// bosh_service_url, debug, prebind, password, login, jid, sid, rid, auto_login, auto_reconnect +// !!! allow_logout will be overridden only if prebind auth method is configured !!! $rcmail_config['converse_config'] = array( 'blacklisted_plugins' => array( + //'converse-controlbox' + ), + 'whitelisted_plugins' => array( + //'converse-notification' ), 'show_message_avatar' => true, 'show_send_button' => true, - //'view_mode' => 'overlayed', 'allow_dragresize' => true, 'allow_contact_removal' => false, 'allow_registration' => false, + 'allow_logout' => true, 'allow_user_trust_override' => false, - 'auto_login' => true, - 'auto_reconnect' => true, - 'allow_logout' => false, 'allow_contact_requests' => false, 'show_client_info' => false, 'visible_toolbar_buttons' => array( - 'spoiler' => true, + 'spoiler' => false, 'call' => false, 'emoji' => true, 'toggle_occupants' => true ), - 'theme' => 'default', - 'dark_theme' => 'concord', + //'theme' => 'default', // may be set to forced override RC theme. available: default (browser/system), concord, dracula + 'dark_theme' => 'concord', // available dracula and concord (default hardcoded in plugin as I don't like dracula =)) - will be useful if theme is not configured or default (browser/system) is set 'auto_away' => 300, 'allow_adhoc_commands' => false, 'omemo_default' => true, + 'discover_connection_methods' => false, + 'play_sounds' => true, + 'sounds_path' => $rcmail_config['converse_cdn'].'/sounds/', 'singleton' => false //for helpdesk like chat ); -// Always embed chat even if prebinding is not configured or failed +// Always embed chat even if auth is failed. Setting it to true and manually filling login and password is not very secure as plugin saves your jid and password in browser local storage until the end of RC or conversejs session $rcmail_config['converse_xmpp_enable_always'] = false; // Enable debug mode @@ -80,12 +115,3 @@ $rcmail_config['converse_xmpp_debug'] = false; // Enable development mode $rcmail_config['converse_xmpp_devel_mode'] = false; -// Configure XMPP resource prefix. XMPP resource is set to this variable -// appended with a unique id. Defaults to 'roundcube-'. -$rcmail_config['converse_xmpp_resource_prefix'] = 'roundcube-'; - -// URL prefix of the CDN where Converse.js is being included from. -// May point to a local path, if you use the same directory structure as -// on the CDN (dist/ + css/). -//$rcmail_config['converse_cdn'] = 'https://cdn.conversejs.org/5.0.5'; -$rcmail_config['converse_cdn'] = 'https://cdn.conversejs.org/10.0.0'; diff --git a/converse.php b/converse.php index 8ece617..6aa4253 100644 --- a/converse.php +++ b/converse.php @@ -2,13 +2,15 @@ /** * Converse.js based XMPP chat plugin plugin for Roundcube webmail - * + * @version 0.9.1-alpha + * @license MIT * @author Priyadi Iman Nurcahyo * @author Thomas Bruederli + * @author Yuri Samoilov * * Copyright (C) 2013, Priyadi Iman Nurcahyo http://priyadi.net * Copyright (C) 2013, The Roundcube Dev Team - * + * Copyright (C) 2023, Samoilov Yuri Olegovich https://drlight.fun * This software is published under the MIT license. * * Permission is hereby granted, free of charge, to any person obtaining a @@ -41,17 +43,80 @@ function debug_to_console($data) { function init() { $this->load_config(); - // we at least require a BOSH url in config - if ($this->_config_get('converse_xmpp_bosh_url') || $this->_config_get('converse_xmpp_enable_always')) { - $rcmail = rcube::get_instance(); - //$TEST = $rcmail->decrypt($_SESSION['password']); + $rcmail = rcube::get_instance(); + + $dont_show_error = true; + $error_mes = ""; + // check converse_xmpp_conn_method, converse_xmpp_auth_method, converse_xmpp_hostname and add errors + if (($this->_config_get('converse_xmpp_conn_method')!=="bosh") && (($this->_config_get('converse_xmpp_conn_method'))!=='websocket')) { + $dont_show_error = false; + $error_mes .= " converse_xmpp_conn_method"; + } + if (($this->_config_get('converse_xmpp_auth_method')!=="login") && (($this->_config_get('converse_xmpp_auth_method'))!=='prebind')) { + $dont_show_error = false; + $error_mes .= " converse_xmpp_auth_method"; + } + // dont mess prebind with websocket! + if (($this->_config_get('converse_xmpp_conn_method')!=="bosh") && (($this->_config_get('converse_xmpp_auth_method'))=='prebind')) { + $dont_show_error = false; + $error_mes .= " converse_xmpp_auth_method and converse_xmpp_conn_method"; + } + // if you specify prebind auth method you must set converse_xmpp_bosh_prebind_url! + if (((($this->_config_get('converse_xmpp_bosh_prebind_url')=="") || (is_null($this->_config_get('converse_xmpp_bosh_prebind_url'))))) && (($this->_config_get('converse_xmpp_auth_method'))=='prebind')) { + $dont_show_error = false; + $error_mes .= " converse_xmpp_bosh_prebind_url and converse_xmpp_auth_method"; + } + if (($this->_config_get('converse_xmpp_hostname')=="") || (is_null($this->_config_get('converse_xmpp_hostname')))) { + $dont_show_error = false; + $error_mes .= " converse_xmpp_hostname"; + $this->debug_to_console ($error_mes); + } + if ($dont_show_error) { + $args = $_SESSION['converse_xmpp_prebind']; + + if (strpos($args['user'], '@')) { + list($args['user'], $args['host']) = preg_split('/@/', $args['user']); + } + // get jid and password by get request to converse plugin URL + if ($_REQUEST['_type'] == 'login') { + $creds_arr = array('jid' => $args['user'].'@'.$this->_config_get('converse_xmpp_hostname'), 'password' => $rcmail->decrypt($args['pass'])); + echo json_encode($creds_arr); + exit(); + } + // get jid, sid, and rid by get request to converse plugin URL + if ($_REQUEST['_type'] == 'prebind') { + require_once(__DIR__ . '/php/xmpp-prebind-php/lib/XmppPrebind.php'); + $xsess = new XmppPrebind($this->_config_get('converse_xmpp_hostname'), $args['bosh_prebind_url'],'converse.js-'.uniqid(), false, false); + $success = true; + try { + $xsess->connect($args['user'], $rcmail->decrypt($args['pass'])); + $xsess->auth(); + } catch (Exception $e) { + rcube::raise_error("Converse-XMPP: Prebind failure: " . $e->getMessage()); + //$rcmail->output->command('display_message', $this->gettext('passwordforbidden'), 'error'); + $success = false; + } + if ($success) { + echo json_encode($xsess->getSessionInfo()); + } else { + echo json_encode(array("jid"=>$args['user'].'@'.$this->_config_get('converse_xmpp_hostname'),"rid"=>0,"sid"=>0)); + } + exit(); + } + if (!$rcmail->output->ajax_call && empty($_REQUEST['_framed']) && $this->_config_get('converse_prebind', array(), 1) > 0) { $this->add_texts('localization/', false); - $this->add_hook('render_page', array($this, 'render_page')); - //samoilov tries to bind or prebind + // try to authenticate $this->add_hook('authenticate', array($this, 'authenticate')); + $this->add_hook('render_page', array($this, 'render_page')); } + } else { + $this->debug_to_console("Converse failure! Check".$error_mes." variable(s) in config.inc.php! All mentioned variables MUST be filled with proper values!"); + $rcmail->output->command('display_message', 'Converse failure! Check browser debug log!', 'error'); + //rcube::raise_error("Converse failure "); + return; } + $this->debug = $this->_config_get('converse_xmpp_debug', array(), false); $this->devel_mode = $this->_config_get('converse_xmpp_devel_mode', array(), false); @@ -64,9 +129,8 @@ function render_page($event) { $rcmail = rcube::get_instance(); - // samoilov add exclusion managesieve plugin as there is some conflict with it causing error 500 - if ($this->already_present == 1 || $rcmail->task == 'login' || $rcmail->action == 'plugin.managesieve' || $rcmail->action == 'plugin.managesieve-vacation' || !empty($_REQUEST['_extwin'])) - return; + if ($this->already_present == 1 || $rcmail->task == 'login' || !empty($_REQUEST['_extwin'])) + return; // map session language with converse.js locale $locale = 'en'; @@ -102,68 +166,70 @@ function render_page($event) { $converse_prop = array_merge($this->converseconfig, $converse_prop); - // samoilov prebind for check RC creds with xmpp server + login to auth with xmpp server - require_once(__DIR__ . '/php/xmpp-prebind-php/lib/XmppPrebind.php'); $args = $_SESSION['converse_xmpp_prebind']; - + if (strpos($args['user'], '@')) { list($args['user'], $args['host']) = preg_split('/@/', $args['user']); } - $xsess = new XmppPrebind($this->_config_get('converse_xmpp_hostname'), $args['bosh_prebind_url'],$this->_config_get('converse_xmpp_resource_prefix').$args['user'], false, false); - $success = true; - try { - $xsess->connect($args['user'], $rcmail->decrypt($args['pass'])); - $xsess->auth(); - } catch (Exception $e) { - rcube::raise_error("Converse-XMPP: Prebind failure: " . $e->getMessage()); - $success = false; + // get server protocol + if (isset($_SERVER['HTTPS']) && + ($_SERVER['HTTPS'] == 'on' || $_SERVER['HTTPS'] == 1) || + isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && + $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https') { + $protocol = 'https://'; } - // samoilov check if always bind enabled or prebind is successful - if (($success)||($this->_config_get('converse_xmpp_enable_always'))) { - - // samoilov login method with password (NOT secured!) - // TODO samoilov add resource_prefix uid for connection stability in multipages of RC (uniqid())? - $converse_prop['jid'] = $args['user'].'@'.$this->_config_get('converse_xmpp_hostname').'/'.$this->_config_get('converse_xmpp_resource_prefix').$args['user']/*.'-'.uniqid()*/; - $converse_prop['password'] = $rcmail->decrypt($args['pass']); - $converse_prop['authentication'] = 'login'; - - //TODO samoilov prebind method with rid, sid, jid (secured) - need to modify converse-bosh.js to operate rid, sid, jid instead of prebind_url - /*$sinfo = json_encode($xsess->getSessionInfo()); // array containing sid, rid and jid converts to JSON - $converse_prop['authentication'] = 'prebind'; - $converse_prop['bosh_service_url'] = $args['bosh_url']; - $converse_prop['prebind_url'] = 'https://drlight.fun:8081/plugins/converse/prebind.php'; - $converse_prop['jid'] = $args['user'].'@'.$this->_config_get('converse_xmpp_hostname').'/'.$this->_config_get('converse_xmpp_resource_prefix').$args['user'];*/ - - // samoilov initialize converse.js from CDN and + custom css to fix some bugs + else { + $protocol = 'http://'; + } + $converse_prop['jid'] = $args['user'].'@'.$this->_config_get('converse_xmpp_hostname'); // add unique resource id + $converse_prop['conn_method'] = $this->_config_get('converse_xmpp_conn_method'); + $converse_prop['domain'] = $this->_config_get('converse_xmpp_hostname'); + // save converse CDN version for theme switch function check in converse-rcmail.js + $converse_prop['cdn_version'] = explode('.', explode('/', $this->_config_get('converse_cdn'))[3])[0]; + // check configured connection method from config.inc.php + if ($this->_config_get('converse_xmpp_conn_method') == 'bosh') { + // check configured auth method from config.inc.php + if ($this->_config_get('converse_xmpp_auth_method') == 'login') { + $converse_prop['authentication'] = 'login'; + $converse_prop['credentials_url'] = $protocol.$_SERVER[HTTP_HOST].'/?_action=plugin.converse&_type='.$converse_prop['authentication']; + } else if ($this->_config_get('converse_xmpp_auth_method') == 'prebind') { + // prebind method with rid, sid, jid (secured) + $converse_prop['authentication'] = 'prebind'; + $converse_prop['bosh_service_url'] = $args['bosh_url']; + $converse_prop['prebind_url'] = $protocol.$_SERVER[HTTP_HOST].'/?_action=plugin.converse&_type='.$converse_prop['authentication']; + } else { + $rcmail->session->remove('xmpp'); + return; + } + } else if ($this->_config_get('converse_xmpp_conn_method') == 'websocket') { + $converse_prop['websocket_url'] = $this->_config_get('converse_xmpp_websocket_url'); + $converse_prop['authentication'] = 'login'; + $converse_prop['credentials_url'] = $protocol.$_SERVER[HTTP_HOST].'/?_action=plugin.converse&_type='.$converse_prop['authentication']; + + } else { + $rcmail->session->remove('xmpp'); + $rcmail->output->command('display_message', 'Converse failure! There is no appropriate connection method (bosh or websocket) configured!', 'error'); + return; + } + + // initialize converse.js from CDN and + custom css to fix some bugs $this->include_script($this->converse_cdn . '/dist/converse.js'); $this->include_stylesheet($this->converse_cdn . '/dist/converse.css'); $this->include_stylesheet('css/custom.css'); - // TODO samoilov add omemo encryption support (no messages received, error) - //$this->include_script('js/libsignal-protocol.js'); - echo ''; - - $this->api->output->add_script(' - var args = '.$rcmail->output->json_serialize($converse_prop).'; - args.locales_url = "' . $this->converse_cdn . '/locale/{{{locale}}}/LC_MESSAGES/converse.json"; - args.i18n = "'.$locale.'"; - rcmail_converse_init(converse, args); - ', 'foot'); + // TODO add omemo encryption support (no messages received, error for now) + //$this->include_script('https://cdn.conversejs.org/3rdparty/libsignal-protocol.min.js'); + // add converse init script + $this->include_script('js/converse-rcmail.js'); + $this->api->output->add_script(' + var always = '.json_encode($this->_config_get("converse_xmpp_enable_always")).'; + var args = '.$rcmail->output->json_serialize($converse_prop).'; + args.locales_url = "' . $this->converse_cdn . '/locale/{{{locale}}}/LC_MESSAGES/converse.json"; + args.i18n = "'.$locale.'"; + rcmail_converse_init(converse, args, always); + ', 'foot'); $this->already_present = 1; - } else { - $rcmail->session->remove('xmpp'); - } } diff --git a/js/converse-rcmail.js b/js/converse-rcmail.js new file mode 100644 index 0000000..37fa0f9 --- /dev/null +++ b/js/converse-rcmail.js @@ -0,0 +1,270 @@ +function rcmail_converse_init(converse, args, always) +{ + + // init delay function + const delay = ms => new Promise(res => setTimeout(res, ms)); + + // fix of conversejs ui boxes resizing on the pages with iframes + async function resize_fix() { + await delay (1000); // TODO we need to use some _converse.api (start****Resize is not suitable as there are the same code rows and bugs) to check when to bind events to conversejs controlbox instead of 1000 ms delay =) + $(".dragresize").mousedown(function () { + $('iframe').css('pointer-events', 'none'); + }); + $(".dragresize").mouseup(function () { + $('iframe').css('pointer-events', 'initial'); + }); + } + // function to show conn method and auth method + /*function debug_converse (){ + console.log("Auth method: "+args.authentication); + console.log("Conn method: "+args.conn_method); + }*/ + + // function to save creds depend on auth type (for prebind - rid, sid and jid, for login - password and jid) + function save_creds_to_storage (connection){ + window.localStorage.setItem('saved_jid', connection.bare_jid); + if (args.authentication == 'prebind') { + if (connection.connection._proto.sid != "0") { + window.localStorage.setItem('saved_sid', connection.connection._proto.sid); + } + if (connection.connection._proto.rid != "1") { + window.localStorage.setItem('saved_rid', connection.connection._proto.rid); + } + } else { + if (window.localStorage.getItem('saved_pass') == null) { + window.localStorage.setItem('saved_pass', connection.connection.pass); + } + + } + } + + // conversejs init function + async function conversejs_init(args) { + //debug_converse (); + converse.initialize(args, function(e){}); + converse.plugins.add('converse-rcmail', { + initialize: function () { + plugin_registered = true; + __converse_api = this._converse.api; + rc_logged_out = false; + // if always is on dont hide conversejs + //console.log (args); + __converse_api.listen.once('connectionInitialized', async function(e){ + if (!(args.show)) { + $('#conversejs').hide(); + } + // check for autologin failure; if connection fails turn allow_logout to true + // TODO async function waiting for connection with 2s delay - maybe use some api? + await delay(2000); + //console.log (this); + //return; + + if ((this.connection != null)||(window.localStorage.getItem('prebind_autologin_failed') !== null )) { + // TODO bosh prebind autologin failure in v > 5 - works because of js error as this.connection.connected is undefined + // TODO in <= 5 - no login password promt, only disconnected status + + if (!(this.connection.connected)) { + //console.log("Connection failed!"); + if ((args.cdn_version <6)&&(window.localStorage.getItem('prebind_autologin_failed') == null )) { + if (args.conn_method == "websocket") { // bosh login exit fix? + window.localStorage.setItem('prebind_autologin_failed', true); + //__converse_api.user.logout(); + } + } + $('#toggle-controlbox').removeClass('hidden'); + $('#conversejs').show(); + } else { + if (this.connection.authenticated) { + $('#conversejs').show(); + } + } + } else { + //console.log("Prebind connection failed!"); + window.localStorage.setItem('prebind_autologin_failed', true); + location.reload(); + } + }); + // listen to bind resize_fix actions to all conversejs dragresize events + __converse_api.listen.on('controlBoxOpened', function(e){ + resize_fix(); + }); + __converse_api.listen.on('chatBoxInitialized', function(e){ + resize_fix(); + }); + __converse_api.listen.on('chatRoomInitialized', function(e){ + resize_fix(); + }); + // clear creds + __converse_api.listen.once('logout', function(e){ + if (!(rc_logged_out)) { + window.localStorage.setItem('logged_out', true); + } + clear_saved_creds(); + location.reload(); // to show logon form + }); + __converse_api.listen.once('discoInitialized', function(e){ + if (this.connection.authenticated) { + save_creds_to_storage(this); + window.localStorage.removeItem('logged_out'); + window.localStorage.removeItem('prebind_autologin_failed'); + if (args.cdn_version > 5) { + $('#toggle-controlbox').removeClass('hidden'); + } + $('#conversejs').show(); //works only with bosh - prebind + + } + }); + // bind to RC logout button to logout from conversejs + $( ".logout" ).on( "click", function() { + rc_logged_out = true; + __converse_api.connection.disconnect(); + window.localStorage.clear(); + }); + } + }); + } + + function clear_saved_creds() { + window.localStorage.removeItem('saved_pass'); + window.localStorage.removeItem('saved_rid'); + window.localStorage.removeItem('saved_sid'); + window.localStorage.removeItem('saved_jid'); + } + + + function theme_sync () { + // set default dark theme to concord if dark_theme var is not defined or defined with not proper value (for now they are dracula and concord) in config.inc.php + if (typeof args.dark_theme == 'undefined') { + if (typeof args.theme == 'undefined') { + args.dark_theme = 'concord'; + } else { + if ((args.theme == 'dracula'||args.theme == 'concord')) { + args.dark_theme = args.theme; + } else { + args.dark_theme = 'concord'; + } + } + } + + if (typeof args.theme == 'undefined'){ + // bind to dark/light mode RC button to switch conversejs theme after click. if not set default dark-theme is hardcoded to concord. to change - see 6 and 11 code rows + $( ".theme" ).on( "click", function() { + if (document.cookie.indexOf('colorMode=light') > -1) { + //console.log("RC theme is dark."); + if ((typeof args.dark_theme !== 'undefined')&&(args.dark_theme !== 'default')) { + $('#conversejs').toggleClass('theme-default theme-'+args.dark_theme); + } else { + $('#conversejs').toggleClass('theme-default theme-'+args.dark_theme_saved); + } + } else { + //console.log("RC theme is light."); + if ((typeof args.dark_theme !== 'undefined')&&(args.dark_theme !== 'default')) { + $('#conversejs').toggleClass('theme-'+args.dark_theme+' theme-default'); + } else { + $('#conversejs').toggleClass('theme-'+args.dark_theme_saved+' theme-default'); + } + } + + }); + + // change conversejs theme based on rc mode at initialization + args.dark_theme_saved = args.dark_theme; + + if (document.cookie.indexOf('colorMode=dark')==document.cookie.indexOf('colorMode=light')) + { + //console.log("RC theme is not set. Force setting it to system default!"); + if (window.matchMedia('(prefers-color-scheme: light)').matches) { + document.cookie = "colorMode=light"; + } else { + document.cookie = "colorMode=dark"; + } + } + + if (!(document.cookie.indexOf('colorMode=dark') > -1)) { + //console.log("RC theme is light."); + args.dark_theme = 'default'; + } + args.theme = args.dark_theme; + } else { + console.log ("You have fixed conversejs theme to browser theme by setting theme="+args['theme']+" parameter in config.inc.php. Current dark_theme parameter is set to "+args['dark_theme']+". Comment it out if you want to sync conversejs theme with RC theme."); + }; + } + + //whitelist plugin, so converse doesn't refuse to load it + if (args.whitelisted_plugins instanceof Array) { + args.whitelisted_plugins.push('converse-rcmail'); + } else { + args.whitelisted_plugins=['converse-rcmail']; + } + + if (args.cdn_version > 5) { + theme_sync (); + } else { + console.log ("You use too old conversejs CDN in settings for theming support."); + } + + + // hide conversejs ui until authenticated except for always embed enabled + + if (always == true) { + //console.log("Always is on."); + args.show=true; + } else { + console.log("Always is off. In case of auth failure conversejs UI will not appear!"); + args.show=false; + } + // check for saved creds + if ((window.localStorage.getItem('saved_jid') !== null) && (window.localStorage.getItem('saved_rid') !== null) && (window.localStorage.getItem('saved_sid') !== null)) { + //console.log("Found saved rid, sid and jid."); + args.jid = window.localStorage.getItem('saved_jid'); + if ((window.localStorage.getItem('saved_sid')=="0") || (window.localStorage.getItem('saved_rid')=="1")) { + //console.log("Bad saved sid or rid. Using saved pass."); + //args.jid = '@'+args.domain; + // clean up rid and sid + window.localStorage.removeItem('saved_rid'); + window.localStorage.removeItem('saved_sid'); + args.authentication = 'login'; + args.password = window.localStorage.getItem('saved_pass'); + } else { + args.sid = window.localStorage.getItem('saved_sid'); + args.rid = window.localStorage.getItem('saved_rid'); + } + + + } else if ((window.localStorage.getItem('saved_jid') !== null) && (window.localStorage.getItem('saved_pass') !== null)) { + //console.log("Found saved jid and password."); + args.jid = window.localStorage.getItem('saved_jid'); + args.password = window.localStorage.getItem('saved_pass'); + args.authentication = 'login'; // need this to not save rid and sid + } + + // check if logged out by button + if(window.localStorage.getItem('logged_out')==='true'){ + //console.log("Logged out by button! Now you need to relogin into RC!"); + args.credentials_url =''; + } + + // check if auth method is prebind + if (args.authentication=='prebind') { + //if (args.conn_method=='bosh') { + args.auto_login = true; + args.allow_logout = false; // turn off logout button for prebind method as it will try to login again + } else { + args.auto_login = false; + } + + // check if prebind autologin failed + + if (window.localStorage.getItem('prebind_autologin_failed')=='true') { + //console.log("Cant't auto login using prebind method! Switched to login method."); + args.prebind_url=''; + args.credentials_url=window.location.href+'/?_action=plugin.converse&_type=login'; + args.authentication = 'login'; + args.auto_login = true; + args.allow_logout = true; // turn on logout button for prebind_autologin_failed case + } + + args.auto_reconnect = false; + + conversejs_init(args); +} \ No newline at end of file