4 * Description: Embedded XMPP (Jabber) client
6 * Author: Michael Vogel <https://pirati.ca/profile/heluecht>
11 use Friendica\Core\Hook;
12 use Friendica\Core\Renderer;
14 use Friendica\Util\Strings;
16 function xmpp_install()
18 Hook::register('addon_settings', 'addon/xmpp/xmpp.php', 'xmpp_addon_settings');
19 Hook::register('addon_settings_post', 'addon/xmpp/xmpp.php', 'xmpp_addon_settings_post');
20 Hook::register('page_end', 'addon/xmpp/xmpp.php', 'xmpp_script');
21 Hook::register('logged_in', 'addon/xmpp/xmpp.php', 'xmpp_login');
24 function xmpp_addon_settings_post()
26 if (!local_user() || empty($_POST['xmpp-settings-submit'])) {
30 DI::pConfig()->set(local_user(), 'xmpp', 'enabled', $_POST['xmpp_enabled'] ?? false);
31 DI::pConfig()->set(local_user(), 'xmpp', 'individual', $_POST['xmpp_individual'] ?? false);
32 DI::pConfig()->set(local_user(), 'xmpp', 'bosh_proxy', $_POST['xmpp_bosh_proxy'] ?? '');
35 function xmpp_addon_settings(App $a, &$s)
41 /* Add our stylesheet to the xmpp so we can make our settings look nice */
43 DI::page()['htmlhead'] .= '<link rel="stylesheet" type="text/css" href="' . DI::baseUrl()->get() . '/addon/xmpp/xmpp.css' . '" media="all" />' . "\r\n";
45 /* Get the current state of our config variable */
47 $enabled = intval(DI::pConfig()->get(local_user(), 'xmpp', 'enabled'));
48 $enabled_checked = (($enabled) ? ' checked="checked" ' : '');
50 $individual = intval(DI::pConfig()->get(local_user(), 'xmpp', 'individual'));
51 $individual_checked = (($individual) ? ' checked="checked" ' : '');
53 $bosh_proxy = DI::pConfig()->get(local_user(), "xmpp", "bosh_proxy");
55 /* Add some HTML to the existing form */
56 $s .= '<span id="settings_xmpp_inflated" class="settings-block fakelink" style="display: block;" onclick="openClose(\'settings_xmpp_expanded\'); openClose(\'settings_xmpp_inflated\');">';
57 $s .= '<h3>' . DI::l10n()->t('XMPP-Chat (Jabber)') . '</h3>';
59 $s .= '<div id="settings_xmpp_expanded" class="settings-block" style="display: none;">';
60 $s .= '<span class="fakelink" onclick="openClose(\'settings_xmpp_expanded\'); openClose(\'settings_xmpp_inflated\');">';
61 $s .= '<h3>' . DI::l10n()->t('XMPP-Chat (Jabber)') . '</h3>';
64 $s .= '<div id="xmpp-settings-wrapper">';
65 $s .= '<label id="xmpp-enabled-label" for="xmpp-enabled">' . DI::l10n()->t('Enable Webchat') . '</label>';
66 $s .= '<input id="xmpp-enabled" type="checkbox" name="xmpp_enabled" value="1" ' . $enabled_checked . '/>';
67 $s .= '<div class="clear"></div>';
69 if (DI::config()->get("xmpp", "central_userbase")) {
70 $s .= '<label id="xmpp-individual-label" for="xmpp-individual">' . DI::l10n()->t('Individual Credentials') . '</label>';
71 $s .= '<input id="xmpp-individual" type="checkbox" name="xmpp_individual" value="1" ' . $individual_checked . '/>';
72 $s .= '<div class="clear"></div>';
75 if (!DI::config()->get("xmpp", "central_userbase") || DI::pConfig()->get(local_user(), "xmpp", "individual")) {
76 $s .= '<label id="xmpp-bosh-proxy-label" for="xmpp-bosh-proxy">' . DI::l10n()->t('Jabber BOSH host') . '</label>';
77 $s .= ' <input id="xmpp-bosh-proxy" type="text" name="xmpp_bosh_proxy" value="' . $bosh_proxy . '" />';
78 $s .= '<div class="clear"></div>';
83 /* provide a submit button */
85 $s .= '<div class="settings-submit-wrapper" ><input type="submit" name="xmpp-settings-submit" class="settings-submit" value="' . DI::l10n()->t('Save Settings') . '" /></div></div>';
90 if (empty($_SESSION['allow_api'])) {
91 $password = Strings::getRandomHex(16);
92 DI::pConfig()->set(local_user(), 'xmpp', 'password', $password);
96 function xmpp_addon_admin(App $a, &$o)
98 $t = Renderer::getMarkupTemplate('admin.tpl', 'addon/xmpp/');
100 $o = Renderer::replaceMacros($t, [
101 '$submit' => DI::l10n()->t('Save Settings'),
102 '$bosh_proxy' => ['bosh_proxy', DI::l10n()->t('Jabber BOSH host'), DI::config()->get('xmpp', 'bosh_proxy'), ''],
103 '$central_userbase' => ['central_userbase', DI::l10n()->t('Use central userbase'), DI::config()->get('xmpp', 'central_userbase'), DI::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.')],
107 function xmpp_addon_admin_post()
109 $bosh_proxy = (!empty($_POST['bosh_proxy']) ? trim($_POST['bosh_proxy']) : '');
110 $central_userbase = (!empty($_POST['central_userbase']) ? intval($_POST['central_userbase']) : false);
112 DI::config()->set('xmpp', 'bosh_proxy', $bosh_proxy);
113 DI::config()->set('xmpp', 'central_userbase', $central_userbase);
116 function xmpp_script(App $a)
121 function xmpp_converse(App $a)
127 if (($_GET['mode'] ?? '') == 'minimal') {
131 if (DI::mode()->isMobile() || DI::mode()->isMobile()) {
135 if (!DI::pConfig()->get(local_user(), "xmpp", "enabled")) {
139 if (in_array(DI::args()->getQueryString(), ["admin/federation/"])) {
143 DI::page()['htmlhead'] .= '<link type="text/css" rel="stylesheet" media="screen" href="addon/xmpp/converse/css/converse.css" />' . "\n";
144 DI::page()['htmlhead'] .= '<script src="addon/xmpp/converse/builds/converse.min.js"></script>' . "\n";
146 if (DI::config()->get("xmpp", "central_userbase") && !DI::pConfig()->get(local_user(), "xmpp", "individual")) {
147 $bosh_proxy = DI::config()->get("xmpp", "bosh_proxy");
149 $password = DI::pConfig()->get(local_user(), "xmpp", "password", '', true);
151 if ($password == "") {
152 $password = Strings::getRandomHex(16);
153 DI::pConfig()->set(local_user(), "xmpp", "password", $password);
156 $jid = $a->user["nickname"] . "@" . DI::baseUrl()->getHostname() . "/converse-" . Strings::getRandomHex(5);
158 $auto_login = "auto_login: true,
159 authentication: 'login',
161 password: '$password',
162 allow_logout: false,";
164 $bosh_proxy = DI::pConfig()->get(local_user(), "xmpp", "bosh_proxy");
169 if ($bosh_proxy == "") {
173 if (in_array($a->argv[0], ["delegation", "logout"])) {
174 $additional_commands = "converse.user.logout();\n";
176 $additional_commands = "";
181 $initialize = "converse.initialize({
182 bosh_service_url: '$bosh_proxy',
184 message_carbons: false,
185 forward_messages: false,
187 sounds_path: 'addon/xmpp/converse/sounds/',
188 roster_groups: false,
189 show_controlbox_by_default: false,
191 allow_contact_removal: false,
192 allow_registration: false,
193 hide_offline_users: true,
194 allow_chat_pending_contacts: false,
195 allow_dragresize: true,
198 csi_waiting_time: 300,
199 auto_reconnect: true,
201 xhr_user_search: false
204 DI::page()['htmlhead'] .= "<script>
205 require(['converse'], function (converse) {
207 converse.listen.on('ready', function (event) {