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\Renderer;
15 use Friendica\Util\Strings;
17 function xmpp_install()
19 Hook::register('addon_settings', 'addon/xmpp/xmpp.php', 'xmpp_addon_settings');
20 Hook::register('addon_settings_post', 'addon/xmpp/xmpp.php', 'xmpp_addon_settings_post');
21 Hook::register('page_end', 'addon/xmpp/xmpp.php', 'xmpp_script');
22 Hook::register('logged_in', 'addon/xmpp/xmpp.php', 'xmpp_login');
25 function xmpp_uninstall()
27 Hook::unregister('addon_settings', 'addon/xmpp/xmpp.php', 'xmpp_addon_settings');
28 Hook::unregister('addon_settings_post', 'addon/xmpp/xmpp.php', 'xmpp_addon_settings_post');
29 Hook::unregister('page_end', 'addon/xmpp/xmpp.php', 'xmpp_script');
30 Hook::unregister('logged_in', 'addon/xmpp/xmpp.php', 'xmpp_login');
33 function xmpp_addon_settings_post()
35 if (!local_user() || empty($_POST['xmpp-settings-submit'])) {
39 DI::pConfig()->set(local_user(), 'xmpp', 'enabled', $_POST['xmpp_enabled'] ?? false);
40 DI::pConfig()->set(local_user(), 'xmpp', 'individual', $_POST['xmpp_individual'] ?? false);
41 DI::pConfig()->set(local_user(), 'xmpp', 'bosh_proxy', $_POST['xmpp_bosh_proxy'] ?? '');
43 info(DI::l10n()->t('XMPP settings updated.') . EOL);
46 function xmpp_addon_settings(App $a, &$s)
52 /* Add our stylesheet to the xmpp so we can make our settings look nice */
54 DI::page()['htmlhead'] .= '<link rel="stylesheet" type="text/css" href="' . DI::baseUrl()->get() . '/addon/xmpp/xmpp.css' . '" media="all" />' . "\r\n";
56 /* Get the current state of our config variable */
58 $enabled = intval(DI::pConfig()->get(local_user(), 'xmpp', 'enabled'));
59 $enabled_checked = (($enabled) ? ' checked="checked" ' : '');
61 $individual = intval(DI::pConfig()->get(local_user(), 'xmpp', 'individual'));
62 $individual_checked = (($individual) ? ' checked="checked" ' : '');
64 $bosh_proxy = DI::pConfig()->get(local_user(), "xmpp", "bosh_proxy");
66 /* Add some HTML to the existing form */
67 $s .= '<span id="settings_xmpp_inflated" class="settings-block fakelink" style="display: block;" onclick="openClose(\'settings_xmpp_expanded\'); openClose(\'settings_xmpp_inflated\');">';
68 $s .= '<h3>' . DI::l10n()->t('XMPP-Chat (Jabber)') . '</h3>';
70 $s .= '<div id="settings_xmpp_expanded" class="settings-block" style="display: none;">';
71 $s .= '<span class="fakelink" onclick="openClose(\'settings_xmpp_expanded\'); openClose(\'settings_xmpp_inflated\');">';
72 $s .= '<h3>' . DI::l10n()->t('XMPP-Chat (Jabber)') . '</h3>';
75 $s .= '<div id="xmpp-settings-wrapper">';
76 $s .= '<label id="xmpp-enabled-label" for="xmpp-enabled">' . DI::l10n()->t('Enable Webchat') . '</label>';
77 $s .= '<input id="xmpp-enabled" type="checkbox" name="xmpp_enabled" value="1" ' . $enabled_checked . '/>';
78 $s .= '<div class="clear"></div>';
80 if (Config::get("xmpp", "central_userbase")) {
81 $s .= '<label id="xmpp-individual-label" for="xmpp-individual">' . DI::l10n()->t('Individual Credentials') . '</label>';
82 $s .= '<input id="xmpp-individual" type="checkbox" name="xmpp_individual" value="1" ' . $individual_checked . '/>';
83 $s .= '<div class="clear"></div>';
86 if (!Config::get("xmpp", "central_userbase") || DI::pConfig()->get(local_user(), "xmpp", "individual")) {
87 $s .= '<label id="xmpp-bosh-proxy-label" for="xmpp-bosh-proxy">' . DI::l10n()->t('Jabber BOSH host') . '</label>';
88 $s .= ' <input id="xmpp-bosh-proxy" type="text" name="xmpp_bosh_proxy" value="' . $bosh_proxy . '" />';
89 $s .= '<div class="clear"></div>';
94 /* provide a submit button */
96 $s .= '<div class="settings-submit-wrapper" ><input type="submit" name="xmpp-settings-submit" class="settings-submit" value="' . DI::l10n()->t('Save Settings') . '" /></div></div>';
101 if (empty($_SESSION['allow_api'])) {
102 $password = Strings::getRandomHex(16);
103 DI::pConfig()->set(local_user(), 'xmpp', 'password', $password);
107 function xmpp_addon_admin(App $a, &$o)
109 $t = Renderer::getMarkupTemplate('admin.tpl', 'addon/xmpp/');
111 $o = Renderer::replaceMacros($t, [
112 '$submit' => DI::l10n()->t('Save Settings'),
113 '$bosh_proxy' => ['bosh_proxy', DI::l10n()->t('Jabber BOSH host'), Config::get('xmpp', 'bosh_proxy'), ''],
114 '$central_userbase' => ['central_userbase', DI::l10n()->t('Use central userbase'), 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.')],
118 function xmpp_addon_admin_post()
120 $bosh_proxy = (!empty($_POST['bosh_proxy']) ? trim($_POST['bosh_proxy']) : '');
121 $central_userbase = (!empty($_POST['central_userbase']) ? intval($_POST['central_userbase']) : false);
123 Config::set('xmpp', 'bosh_proxy', $bosh_proxy);
124 Config::set('xmpp', 'central_userbase', $central_userbase);
126 info(DI::l10n()->t('Settings updated.') . EOL);
129 function xmpp_script(App $a)
134 function xmpp_converse(App $a)
140 if (($_GET['mode'] ?? '') == 'minimal') {
144 if (DI::mode()->isMobile() || DI::mode()->isMobile()) {
148 if (!DI::pConfig()->get(local_user(), "xmpp", "enabled")) {
152 if (in_array(DI::args()->getQueryString(), ["admin/federation/"])) {
156 DI::page()['htmlhead'] .= '<link type="text/css" rel="stylesheet" media="screen" href="addon/xmpp/converse/css/converse.css" />' . "\n";
157 DI::page()['htmlhead'] .= '<script src="addon/xmpp/converse/builds/converse.min.js"></script>' . "\n";
159 if (Config::get("xmpp", "central_userbase") && !DI::pConfig()->get(local_user(), "xmpp", "individual")) {
160 $bosh_proxy = Config::get("xmpp", "bosh_proxy");
162 $password = DI::pConfig()->get(local_user(), "xmpp", "password", '', true);
164 if ($password == "") {
165 $password = Strings::getRandomHex(16);
166 DI::pConfig()->set(local_user(), "xmpp", "password", $password);
169 $jid = $a->user["nickname"] . "@" . DI::baseUrl()->getHostname() . "/converse-" . Strings::getRandomHex(5);
171 $auto_login = "auto_login: true,
172 authentication: 'login',
174 password: '$password',
175 allow_logout: false,";
177 $bosh_proxy = DI::pConfig()->get(local_user(), "xmpp", "bosh_proxy");
182 if ($bosh_proxy == "") {
186 if (in_array($a->argv[0], ["delegation", "logout"])) {
187 $additional_commands = "converse.user.logout();\n";
189 $additional_commands = "";
194 $initialize = "converse.initialize({
195 bosh_service_url: '$bosh_proxy',
197 message_carbons: false,
198 forward_messages: false,
200 sounds_path: 'addon/xmpp/converse/sounds/',
201 roster_groups: false,
202 show_controlbox_by_default: false,
204 allow_contact_removal: false,
205 allow_registration: false,
206 hide_offline_users: true,
207 allow_chat_pending_contacts: false,
208 allow_dragresize: true,
211 csi_waiting_time: 300,
212 auto_reconnect: true,
214 xhr_user_search: false
217 DI::page()['htmlhead'] .= "<script>
218 require(['converse'], function (converse) {
220 converse.listen.on('ready', function (event) {