4 * Description: Embedded XMPP (Jabber) client
6 * Author: Michael Vogel <https://pirati.ca/profile/heluecht>
10 use Friendica\Core\Addon;
11 use Friendica\Core\Config;
12 use Friendica\Core\L10n;
13 use Friendica\Core\PConfig;
14 use Friendica\Core\Renderer;
16 function xmpp_install()
18 Addon::registerHook('addon_settings', 'addon/xmpp/xmpp.php', 'xmpp_addon_settings');
19 Addon::registerHook('addon_settings_post', 'addon/xmpp/xmpp.php', 'xmpp_addon_settings_post');
20 Addon::registerHook('page_end', 'addon/xmpp/xmpp.php', 'xmpp_script');
21 Addon::registerHook('logged_in', 'addon/xmpp/xmpp.php', 'xmpp_login');
24 function xmpp_uninstall()
26 Addon::unregisterHook('addon_settings', 'addon/xmpp/xmpp.php', 'xmpp_addon_settings');
27 Addon::unregisterHook('addon_settings_post', 'addon/xmpp/xmpp.php', 'xmpp_addon_settings_post');
28 Addon::unregisterHook('page_end', 'addon/xmpp/xmpp.php', 'xmpp_script');
29 Addon::unregisterHook('logged_in', 'addon/xmpp/xmpp.php', 'xmpp_login');
32 function xmpp_addon_settings_post()
34 if (!local_user() || empty($_POST['xmpp-settings-submit'])) {
38 PConfig::set(local_user(), 'xmpp', 'enabled', defaults($_POST, 'xmpp_enabled', false));
39 PConfig::set(local_user(), 'xmpp', 'individual', defaults($_POST, 'xmpp_individual', false));
40 PConfig::set(local_user(), 'xmpp', 'bosh_proxy', defaults($_POST, 'xmpp_bosh_proxy', ''));
42 info(L10n::t('XMPP settings updated.') . EOL);
45 function xmpp_addon_settings(App $a, &$s)
51 /* Add our stylesheet to the xmpp so we can make our settings look nice */
53 $a->page['htmlhead'] .= '<link rel="stylesheet" type="text/css" href="' . $a->getBaseURL() . '/addon/xmpp/xmpp.css' . '" media="all" />' . "\r\n";
55 /* Get the current state of our config variable */
57 $enabled = intval(PConfig::get(local_user(), 'xmpp', 'enabled'));
58 $enabled_checked = (($enabled) ? ' checked="checked" ' : '');
60 $individual = intval(PConfig::get(local_user(), 'xmpp', 'individual'));
61 $individual_checked = (($individual) ? ' checked="checked" ' : '');
63 $bosh_proxy = PConfig::get(local_user(), "xmpp", "bosh_proxy");
65 /* Add some HTML to the existing form */
66 $s .= '<span id="settings_xmpp_inflated" class="settings-block fakelink" style="display: block;" onclick="openClose(\'settings_xmpp_expanded\'); openClose(\'settings_xmpp_inflated\');">';
67 $s .= '<h3>' . L10n::t('XMPP-Chat (Jabber)') . '</h3>';
69 $s .= '<div id="settings_xmpp_expanded" class="settings-block" style="display: none;">';
70 $s .= '<span class="fakelink" onclick="openClose(\'settings_xmpp_expanded\'); openClose(\'settings_xmpp_inflated\');">';
71 $s .= '<h3>' . L10n::t('XMPP-Chat (Jabber)') . '</h3>';
74 $s .= '<div id="xmpp-settings-wrapper">';
75 $s .= '<label id="xmpp-enabled-label" for="xmpp-enabled">' . L10n::t('Enable Webchat') . '</label>';
76 $s .= '<input id="xmpp-enabled" type="checkbox" name="xmpp_enabled" value="1" ' . $enabled_checked . '/>';
77 $s .= '<div class="clear"></div>';
79 if (Config::get("xmpp", "central_userbase")) {
80 $s .= '<label id="xmpp-individual-label" for="xmpp-individual">' . L10n::t('Individual Credentials') . '</label>';
81 $s .= '<input id="xmpp-individual" type="checkbox" name="xmpp_individual" value="1" ' . $individual_checked . '/>';
82 $s .= '<div class="clear"></div>';
85 if (!Config::get("xmpp", "central_userbase") || PConfig::get(local_user(), "xmpp", "individual")) {
86 $s .= '<label id="xmpp-bosh-proxy-label" for="xmpp-bosh-proxy">' . L10n::t('Jabber BOSH host') . '</label>';
87 $s .= ' <input id="xmpp-bosh-proxy" type="text" name="xmpp_bosh_proxy" value="' . $bosh_proxy . '" />';
88 $s .= '<div class="clear"></div>';
93 /* provide a submit button */
95 $s .= '<div class="settings-submit-wrapper" ><input type="submit" name="xmpp-settings-submit" class="settings-submit" value="' . L10n::t('Save Settings') . '" /></div></div>';
100 if (empty($_SESSION['allow_api'])) {
101 $password = random_string(16);
102 PConfig::set(local_user(), 'xmpp', 'password', $password);
106 function xmpp_addon_admin(App $a, &$o)
108 $t = Renderer::getMarkupTemplate('admin.tpl', 'addon/xmpp/');
110 $o = Renderer::replaceMacros($t, [
111 '$submit' => L10n::t('Save Settings'),
112 '$bosh_proxy' => ['bosh_proxy', L10n::t('Jabber BOSH host'), Config::get('xmpp', 'bosh_proxy'), ''],
113 '$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.')],
117 function xmpp_addon_admin_post()
119 $bosh_proxy = ((!empty($_POST['bosh_proxy'])) ? trim($_POST['bosh_proxy']) : '');
120 $central_userbase = ((!empty($_POST['central_userbase'])) ? intval($_POST['central_userbase']) : false);
122 Config::set('xmpp', 'bosh_proxy', $bosh_proxy);
123 Config::set('xmpp', 'central_userbase', $central_userbase);
125 info(L10n::t('Settings updated.') . EOL);
128 function xmpp_script(App $a)
133 function xmpp_converse(App $a)
139 if (defaults($_GET, "mode", '') == "minimal") {
143 if ($a->is_mobile || $a->is_tablet) {
147 if (!PConfig::get(local_user(), "xmpp", "enabled")) {
151 if (in_array($a->query_string, ["admin/federation/"])) {
155 $a->page['htmlhead'] .= '<link type="text/css" rel="stylesheet" media="screen" href="addon/xmpp/converse/css/converse.css" />' . "\n";
156 $a->page['htmlhead'] .= '<script src="addon/xmpp/converse/builds/converse.min.js"></script>' . "\n";
158 if (Config::get("xmpp", "central_userbase") && !PConfig::get(local_user(), "xmpp", "individual")) {
159 $bosh_proxy = Config::get("xmpp", "bosh_proxy");
161 $password = PConfig::get(local_user(), "xmpp", "password", '', true);
163 if ($password == "") {
164 $password = random_string(16);
165 PConfig::set(local_user(), "xmpp", "password", $password);
168 $jid = $a->user["nickname"] . "@" . $a->getHostName() . "/converse-" . random_string(5);
170 $auto_login = "auto_login: true,
171 authentication: 'login',
173 password: '$password',
174 allow_logout: false,";
176 $bosh_proxy = PConfig::get(local_user(), "xmpp", "bosh_proxy");
181 if ($bosh_proxy == "") {
185 if (in_array($a->argv[0], ["manage", "logout"])) {
186 $additional_commands = "converse.user.logout();\n";
188 $additional_commands = "";
193 $initialize = "converse.initialize({
194 bosh_service_url: '$bosh_proxy',
196 message_carbons: false,
197 forward_messages: false,
199 sounds_path: 'addon/xmpp/converse/sounds/',
200 roster_groups: false,
201 show_controlbox_by_default: false,
203 allow_contact_removal: false,
204 allow_registration: false,
205 hide_offline_users: true,
206 allow_chat_pending_contacts: false,
207 allow_dragresize: true,
210 csi_waiting_time: 300,
211 auto_reconnect: true,
213 xhr_user_search: false
216 $a->page['htmlhead'] .= "<script>
217 require(['converse'], function (converse) {
219 converse.listen.on('ready', function (event) {