4 * Description: Embedded XMPP (Jabber) client
6 * Author: Michael Vogel <https://pirati.ca/profile/heluecht>
11 use Friendica\Core\Config;
12 use Friendica\Core\Hook;
13 use Friendica\Core\L10n;
14 use Friendica\Core\PConfig;
15 use Friendica\Core\Renderer;
17 use Friendica\Util\Strings;
19 function xmpp_install()
21 Hook::register('addon_settings', 'addon/xmpp/xmpp.php', 'xmpp_addon_settings');
22 Hook::register('addon_settings_post', 'addon/xmpp/xmpp.php', 'xmpp_addon_settings_post');
23 Hook::register('page_end', 'addon/xmpp/xmpp.php', 'xmpp_script');
24 Hook::register('logged_in', 'addon/xmpp/xmpp.php', 'xmpp_login');
27 function xmpp_uninstall()
29 Hook::unregister('addon_settings', 'addon/xmpp/xmpp.php', 'xmpp_addon_settings');
30 Hook::unregister('addon_settings_post', 'addon/xmpp/xmpp.php', 'xmpp_addon_settings_post');
31 Hook::unregister('page_end', 'addon/xmpp/xmpp.php', 'xmpp_script');
32 Hook::unregister('logged_in', 'addon/xmpp/xmpp.php', 'xmpp_login');
35 function xmpp_addon_settings_post()
37 if (!local_user() || empty($_POST['xmpp-settings-submit'])) {
41 PConfig::set(local_user(), 'xmpp', 'enabled', $_POST['xmpp_enabled'] ?? false);
42 PConfig::set(local_user(), 'xmpp', 'individual', $_POST['xmpp_individual'] ?? false);
43 PConfig::set(local_user(), 'xmpp', 'bosh_proxy', $_POST['xmpp_bosh_proxy'] ?? '');
45 info(L10n::t('XMPP settings updated.') . EOL);
48 function xmpp_addon_settings(App $a, &$s)
54 /* Add our stylesheet to the xmpp so we can make our settings look nice */
56 DI::page()['htmlhead'] .= '<link rel="stylesheet" type="text/css" href="' . DI::baseUrl()->get() . '/addon/xmpp/xmpp.css' . '" media="all" />' . "\r\n";
58 /* Get the current state of our config variable */
60 $enabled = intval(DI::pConfig()->get(local_user(), 'xmpp', 'enabled'));
61 $enabled_checked = (($enabled) ? ' checked="checked" ' : '');
63 $individual = intval(DI::pConfig()->get(local_user(), 'xmpp', 'individual'));
64 $individual_checked = (($individual) ? ' checked="checked" ' : '');
66 $bosh_proxy = DI::pConfig()->get(local_user(), "xmpp", "bosh_proxy");
68 /* Add some HTML to the existing form */
69 $s .= '<span id="settings_xmpp_inflated" class="settings-block fakelink" style="display: block;" onclick="openClose(\'settings_xmpp_expanded\'); openClose(\'settings_xmpp_inflated\');">';
70 $s .= '<h3>' . L10n::t('XMPP-Chat (Jabber)') . '</h3>';
72 $s .= '<div id="settings_xmpp_expanded" class="settings-block" style="display: none;">';
73 $s .= '<span class="fakelink" onclick="openClose(\'settings_xmpp_expanded\'); openClose(\'settings_xmpp_inflated\');">';
74 $s .= '<h3>' . L10n::t('XMPP-Chat (Jabber)') . '</h3>';
77 $s .= '<div id="xmpp-settings-wrapper">';
78 $s .= '<label id="xmpp-enabled-label" for="xmpp-enabled">' . L10n::t('Enable Webchat') . '</label>';
79 $s .= '<input id="xmpp-enabled" type="checkbox" name="xmpp_enabled" value="1" ' . $enabled_checked . '/>';
80 $s .= '<div class="clear"></div>';
82 if (Config::get("xmpp", "central_userbase")) {
83 $s .= '<label id="xmpp-individual-label" for="xmpp-individual">' . L10n::t('Individual Credentials') . '</label>';
84 $s .= '<input id="xmpp-individual" type="checkbox" name="xmpp_individual" value="1" ' . $individual_checked . '/>';
85 $s .= '<div class="clear"></div>';
88 if (!Config::get("xmpp", "central_userbase") || DI::pConfig()->get(local_user(), "xmpp", "individual")) {
89 $s .= '<label id="xmpp-bosh-proxy-label" for="xmpp-bosh-proxy">' . L10n::t('Jabber BOSH host') . '</label>';
90 $s .= ' <input id="xmpp-bosh-proxy" type="text" name="xmpp_bosh_proxy" value="' . $bosh_proxy . '" />';
91 $s .= '<div class="clear"></div>';
96 /* provide a submit button */
98 $s .= '<div class="settings-submit-wrapper" ><input type="submit" name="xmpp-settings-submit" class="settings-submit" value="' . L10n::t('Save Settings') . '" /></div></div>';
101 function xmpp_login()
103 if (empty($_SESSION['allow_api'])) {
104 $password = Strings::getRandomHex(16);
105 PConfig::set(local_user(), 'xmpp', 'password', $password);
109 function xmpp_addon_admin(App $a, &$o)
111 $t = Renderer::getMarkupTemplate('admin.tpl', 'addon/xmpp/');
113 $o = Renderer::replaceMacros($t, [
114 '$submit' => L10n::t('Save Settings'),
115 '$bosh_proxy' => ['bosh_proxy', L10n::t('Jabber BOSH host'), Config::get('xmpp', 'bosh_proxy'), ''],
116 '$central_userbase' => ['central_userbase', L10n::t('Use central userbase'), Config::get('xmpp', 'central_userbase'), L10n::t('If enabled, users will automatically login to an ejabberd server that has to be installed on this machine with synchronized credentials via the "auth_ejabberd.php" script.')],
120 function xmpp_addon_admin_post()
122 $bosh_proxy = (!empty($_POST['bosh_proxy']) ? trim($_POST['bosh_proxy']) : '');
123 $central_userbase = (!empty($_POST['central_userbase']) ? intval($_POST['central_userbase']) : false);
125 Config::set('xmpp', 'bosh_proxy', $bosh_proxy);
126 Config::set('xmpp', 'central_userbase', $central_userbase);
128 info(L10n::t('Settings updated.') . EOL);
131 function xmpp_script(App $a)
136 function xmpp_converse(App $a)
142 if (($_GET['mode'] ?? '') == 'minimal') {
146 if (DI::mode()->isMobile() || DI::mode()->isMobile()) {
150 if (!DI::pConfig()->get(local_user(), "xmpp", "enabled")) {
154 if (in_array(DI::args()->getQueryString(), ["admin/federation/"])) {
158 DI::page()['htmlhead'] .= '<link type="text/css" rel="stylesheet" media="screen" href="addon/xmpp/converse/css/converse.css" />' . "\n";
159 DI::page()['htmlhead'] .= '<script src="addon/xmpp/converse/builds/converse.min.js"></script>' . "\n";
161 if (Config::get("xmpp", "central_userbase") && !DI::pConfig()->get(local_user(), "xmpp", "individual")) {
162 $bosh_proxy = Config::get("xmpp", "bosh_proxy");
164 $password = DI::pConfig()->get(local_user(), "xmpp", "password", '', true);
166 if ($password == "") {
167 $password = Strings::getRandomHex(16);
168 PConfig::set(local_user(), "xmpp", "password", $password);
171 $jid = $a->user["nickname"] . "@" . DI::baseUrl()->getHostname() . "/converse-" . Strings::getRandomHex(5);
173 $auto_login = "auto_login: true,
174 authentication: 'login',
176 password: '$password',
177 allow_logout: false,";
179 $bosh_proxy = DI::pConfig()->get(local_user(), "xmpp", "bosh_proxy");
184 if ($bosh_proxy == "") {
188 if (in_array($a->argv[0], ["delegation", "logout"])) {
189 $additional_commands = "converse.user.logout();\n";
191 $additional_commands = "";
196 $initialize = "converse.initialize({
197 bosh_service_url: '$bosh_proxy',
199 message_carbons: false,
200 forward_messages: false,
202 sounds_path: 'addon/xmpp/converse/sounds/',
203 roster_groups: false,
204 show_controlbox_by_default: false,
206 allow_contact_removal: false,
207 allow_registration: false,
208 hide_offline_users: true,
209 allow_chat_pending_contacts: false,
210 allow_dragresize: true,
213 csi_waiting_time: 300,
214 auto_reconnect: true,
216 xhr_user_search: false
219 DI::page()['htmlhead'] .= "<script>
220 require(['converse'], function (converse) {
222 converse.listen.on('ready', function (event) {