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_uninstall()
26 Hook::unregister('addon_settings', 'addon/xmpp/xmpp.php', 'xmpp_addon_settings');
27 Hook::unregister('addon_settings_post', 'addon/xmpp/xmpp.php', 'xmpp_addon_settings_post');
28 Hook::unregister('page_end', 'addon/xmpp/xmpp.php', 'xmpp_script');
29 Hook::unregister('logged_in', 'addon/xmpp/xmpp.php', 'xmpp_login');
32 function xmpp_addon_settings_post()
34 if (!local_user() || empty($_POST['xmpp-settings-submit'])) {
38 DI::pConfig()->set(local_user(), 'xmpp', 'enabled', $_POST['xmpp_enabled'] ?? false);
39 DI::pConfig()->set(local_user(), 'xmpp', 'individual', $_POST['xmpp_individual'] ?? false);
40 DI::pConfig()->set(local_user(), 'xmpp', 'bosh_proxy', $_POST['xmpp_bosh_proxy'] ?? '');
43 function xmpp_addon_settings(App $a, &$s)
49 /* Add our stylesheet to the xmpp so we can make our settings look nice */
51 DI::page()['htmlhead'] .= '<link rel="stylesheet" type="text/css" href="' . DI::baseUrl()->get() . '/addon/xmpp/xmpp.css' . '" media="all" />' . "\r\n";
53 /* Get the current state of our config variable */
55 $enabled = intval(DI::pConfig()->get(local_user(), 'xmpp', 'enabled'));
56 $enabled_checked = (($enabled) ? ' checked="checked" ' : '');
58 $individual = intval(DI::pConfig()->get(local_user(), 'xmpp', 'individual'));
59 $individual_checked = (($individual) ? ' checked="checked" ' : '');
61 $bosh_proxy = DI::pConfig()->get(local_user(), "xmpp", "bosh_proxy");
63 /* Add some HTML to the existing form */
64 $s .= '<span id="settings_xmpp_inflated" class="settings-block fakelink" style="display: block;" onclick="openClose(\'settings_xmpp_expanded\'); openClose(\'settings_xmpp_inflated\');">';
65 $s .= '<h3>' . DI::l10n()->t('XMPP-Chat (Jabber)') . '</h3>';
67 $s .= '<div id="settings_xmpp_expanded" class="settings-block" style="display: none;">';
68 $s .= '<span class="fakelink" onclick="openClose(\'settings_xmpp_expanded\'); openClose(\'settings_xmpp_inflated\');">';
69 $s .= '<h3>' . DI::l10n()->t('XMPP-Chat (Jabber)') . '</h3>';
72 $s .= '<div id="xmpp-settings-wrapper">';
73 $s .= '<label id="xmpp-enabled-label" for="xmpp-enabled">' . DI::l10n()->t('Enable Webchat') . '</label>';
74 $s .= '<input id="xmpp-enabled" type="checkbox" name="xmpp_enabled" value="1" ' . $enabled_checked . '/>';
75 $s .= '<div class="clear"></div>';
77 if (DI::config()->get("xmpp", "central_userbase")) {
78 $s .= '<label id="xmpp-individual-label" for="xmpp-individual">' . DI::l10n()->t('Individual Credentials') . '</label>';
79 $s .= '<input id="xmpp-individual" type="checkbox" name="xmpp_individual" value="1" ' . $individual_checked . '/>';
80 $s .= '<div class="clear"></div>';
83 if (!DI::config()->get("xmpp", "central_userbase") || DI::pConfig()->get(local_user(), "xmpp", "individual")) {
84 $s .= '<label id="xmpp-bosh-proxy-label" for="xmpp-bosh-proxy">' . DI::l10n()->t('Jabber BOSH host') . '</label>';
85 $s .= ' <input id="xmpp-bosh-proxy" type="text" name="xmpp_bosh_proxy" value="' . $bosh_proxy . '" />';
86 $s .= '<div class="clear"></div>';
91 /* provide a submit button */
93 $s .= '<div class="settings-submit-wrapper" ><input type="submit" name="xmpp-settings-submit" class="settings-submit" value="' . DI::l10n()->t('Save Settings') . '" /></div></div>';
98 if (empty($_SESSION['allow_api'])) {
99 $password = Strings::getRandomHex(16);
100 DI::pConfig()->set(local_user(), 'xmpp', 'password', $password);
104 function xmpp_addon_admin(App $a, &$o)
106 $t = Renderer::getMarkupTemplate('admin.tpl', 'addon/xmpp/');
108 $o = Renderer::replaceMacros($t, [
109 '$submit' => DI::l10n()->t('Save Settings'),
110 '$bosh_proxy' => ['bosh_proxy', DI::l10n()->t('Jabber BOSH host'), DI::config()->get('xmpp', 'bosh_proxy'), ''],
111 '$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.')],
115 function xmpp_addon_admin_post()
117 $bosh_proxy = (!empty($_POST['bosh_proxy']) ? trim($_POST['bosh_proxy']) : '');
118 $central_userbase = (!empty($_POST['central_userbase']) ? intval($_POST['central_userbase']) : false);
120 DI::config()->set('xmpp', 'bosh_proxy', $bosh_proxy);
121 DI::config()->set('xmpp', 'central_userbase', $central_userbase);
124 function xmpp_script(App $a)
129 function xmpp_converse(App $a)
135 if (($_GET['mode'] ?? '') == 'minimal') {
139 if (DI::mode()->isMobile() || DI::mode()->isMobile()) {
143 if (!DI::pConfig()->get(local_user(), "xmpp", "enabled")) {
147 if (in_array(DI::args()->getQueryString(), ["admin/federation/"])) {
151 DI::page()['htmlhead'] .= '<link type="text/css" rel="stylesheet" media="screen" href="addon/xmpp/converse/css/converse.css" />' . "\n";
152 DI::page()['htmlhead'] .= '<script src="addon/xmpp/converse/builds/converse.min.js"></script>' . "\n";
154 if (DI::config()->get("xmpp", "central_userbase") && !DI::pConfig()->get(local_user(), "xmpp", "individual")) {
155 $bosh_proxy = DI::config()->get("xmpp", "bosh_proxy");
157 $password = DI::pConfig()->get(local_user(), "xmpp", "password", '', true);
159 if ($password == "") {
160 $password = Strings::getRandomHex(16);
161 DI::pConfig()->set(local_user(), "xmpp", "password", $password);
164 $jid = $a->user["nickname"] . "@" . DI::baseUrl()->getHostname() . "/converse-" . Strings::getRandomHex(5);
166 $auto_login = "auto_login: true,
167 authentication: 'login',
169 password: '$password',
170 allow_logout: false,";
172 $bosh_proxy = DI::pConfig()->get(local_user(), "xmpp", "bosh_proxy");
177 if ($bosh_proxy == "") {
181 if (in_array($a->argv[0], ["delegation", "logout"])) {
182 $additional_commands = "converse.user.logout();\n";
184 $additional_commands = "";
189 $initialize = "converse.initialize({
190 bosh_service_url: '$bosh_proxy',
192 message_carbons: false,
193 forward_messages: false,
195 sounds_path: 'addon/xmpp/converse/sounds/',
196 roster_groups: false,
197 show_controlbox_by_default: false,
199 allow_contact_removal: false,
200 allow_registration: false,
201 hide_offline_users: true,
202 allow_chat_pending_contacts: false,
203 allow_dragresize: true,
206 csi_waiting_time: 300,
207 auto_reconnect: true,
209 xhr_user_search: false
212 DI::page()['htmlhead'] .= "<script>
213 require(['converse'], function (converse) {
215 converse.listen.on('ready', function (event) {