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;
15 function xmpp_install()
17 Addon::registerHook('addon_settings', 'addon/xmpp/xmpp.php', 'xmpp_addon_settings');
18 Addon::registerHook('addon_settings_post', 'addon/xmpp/xmpp.php', 'xmpp_addon_settings_post');
19 Addon::registerHook('page_end', 'addon/xmpp/xmpp.php', 'xmpp_script');
20 Addon::registerHook('logged_in', 'addon/xmpp/xmpp.php', 'xmpp_login');
23 function xmpp_uninstall()
25 Addon::unregisterHook('addon_settings', 'addon/xmpp/xmpp.php', 'xmpp_addon_settings');
26 Addon::unregisterHook('addon_settings_post', 'addon/xmpp/xmpp.php', 'xmpp_addon_settings_post');
27 Addon::unregisterHook('page_end', 'addon/xmpp/xmpp.php', 'xmpp_script');
28 Addon::unregisterHook('logged_in', 'addon/xmpp/xmpp.php', 'xmpp_login');
31 function xmpp_addon_settings_post()
33 if (!local_user() || empty($_POST['xmpp-settings-submit'])) {
37 PConfig::set(local_user(), 'xmpp', 'enabled', defaults($_POST, 'xmpp_enabled', false));
38 PConfig::set(local_user(), 'xmpp', 'individual', defaults($_POST, 'xmpp_individual', false));
39 PConfig::set(local_user(), 'xmpp', 'bosh_proxy', defaults($_POST, 'xmpp_bosh_proxy', ''));
41 info(L10n::t('XMPP settings updated.') . EOL);
44 function xmpp_addon_settings(App $a, &$s)
50 /* Add our stylesheet to the xmpp so we can make our settings look nice */
52 $a->page['htmlhead'] .= '<link rel="stylesheet" type="text/css" href="' . $a->getBaseURL() . '/addon/xmpp/xmpp.css' . '" media="all" />' . "\r\n";
54 /* Get the current state of our config variable */
56 $enabled = intval(PConfig::get(local_user(), 'xmpp', 'enabled'));
57 $enabled_checked = (($enabled) ? ' checked="checked" ' : '');
59 $individual = intval(PConfig::get(local_user(), 'xmpp', 'individual'));
60 $individual_checked = (($individual) ? ' checked="checked" ' : '');
62 $bosh_proxy = PConfig::get(local_user(), "xmpp", "bosh_proxy");
64 /* Add some HTML to the existing form */
65 $s .= '<span id="settings_xmpp_inflated" class="settings-block fakelink" style="display: block;" onclick="openClose(\'settings_xmpp_expanded\'); openClose(\'settings_xmpp_inflated\');">';
66 $s .= '<h3>' . L10n::t('XMPP-Chat (Jabber)') . '</h3>';
68 $s .= '<div id="settings_xmpp_expanded" class="settings-block" style="display: none;">';
69 $s .= '<span class="fakelink" onclick="openClose(\'settings_xmpp_expanded\'); openClose(\'settings_xmpp_inflated\');">';
70 $s .= '<h3>' . L10n::t('XMPP-Chat (Jabber)') . '</h3>';
73 $s .= '<div id="xmpp-settings-wrapper">';
74 $s .= '<label id="xmpp-enabled-label" for="xmpp-enabled">' . L10n::t('Enable Webchat') . '</label>';
75 $s .= '<input id="xmpp-enabled" type="checkbox" name="xmpp_enabled" value="1" ' . $enabled_checked . '/>';
76 $s .= '<div class="clear"></div>';
78 if (Config::get("xmpp", "central_userbase")) {
79 $s .= '<label id="xmpp-individual-label" for="xmpp-individual">' . L10n::t('Individual Credentials') . '</label>';
80 $s .= '<input id="xmpp-individual" type="checkbox" name="xmpp_individual" value="1" ' . $individual_checked . '/>';
81 $s .= '<div class="clear"></div>';
84 if (!Config::get("xmpp", "central_userbase") || PConfig::get(local_user(), "xmpp", "individual")) {
85 $s .= '<label id="xmpp-bosh-proxy-label" for="xmpp-bosh-proxy">' . L10n::t('Jabber BOSH host') . '</label>';
86 $s .= ' <input id="xmpp-bosh-proxy" type="text" name="xmpp_bosh_proxy" value="' . $bosh_proxy . '" />';
87 $s .= '<div class="clear"></div>';
92 /* provide a submit button */
94 $s .= '<div class="settings-submit-wrapper" ><input type="submit" name="xmpp-settings-submit" class="settings-submit" value="' . L10n::t('Save Settings') . '" /></div></div>';
99 if (empty($_SESSION['allow_api'])) {
100 $password = random_string(16);
101 PConfig::set(local_user(), 'xmpp', 'password', $password);
105 function xmpp_addon_admin(App $a, &$o)
107 $t = get_markup_template('admin.tpl', 'addon/xmpp/');
109 $o = replace_macros($t, [
110 '$submit' => L10n::t('Save Settings'),
111 '$bosh_proxy' => ['bosh_proxy', L10n::t('Jabber BOSH host'), Config::get('xmpp', 'bosh_proxy'), ''],
112 '$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.')],
116 function xmpp_addon_admin_post()
118 $bosh_proxy = ((!empty($_POST['bosh_proxy'])) ? trim($_POST['bosh_proxy']) : '');
119 $central_userbase = ((!empty($_POST['central_userbase'])) ? intval($_POST['central_userbase']) : false);
121 Config::set('xmpp', 'bosh_proxy', $bosh_proxy);
122 Config::set('xmpp', 'central_userbase', $central_userbase);
124 info(L10n::t('Settings updated.') . EOL);
127 function xmpp_script(App $a)
132 function xmpp_converse(App $a)
138 if (defaults($_GET, "mode", '') == "minimal") {
142 if ($a->is_mobile || $a->is_tablet) {
146 if (!PConfig::get(local_user(), "xmpp", "enabled")) {
150 if (in_array($a->query_string, ["admin/federation/"])) {
154 $a->page['htmlhead'] .= '<link type="text/css" rel="stylesheet" media="screen" href="addon/xmpp/converse/css/converse.css" />' . "\n";
155 $a->page['htmlhead'] .= '<script src="addon/xmpp/converse/builds/converse.min.js"></script>' . "\n";
157 if (Config::get("xmpp", "central_userbase") && !PConfig::get(local_user(), "xmpp", "individual")) {
158 $bosh_proxy = Config::get("xmpp", "bosh_proxy");
160 $password = PConfig::get(local_user(), "xmpp", "password", '', true);
162 if ($password == "") {
163 $password = random_string(16);
164 PConfig::set(local_user(), "xmpp", "password", $password);
167 $jid = $a->user["nickname"] . "@" . $a->getHostName() . "/converse-" . random_string(5);
169 $auto_login = "auto_login: true,
170 authentication: 'login',
172 password: '$password',
173 allow_logout: false,";
175 $bosh_proxy = PConfig::get(local_user(), "xmpp", "bosh_proxy");
180 if ($bosh_proxy == "") {
184 if (in_array($a->argv[0], ["manage", "logout"])) {
185 $additional_commands = "converse.user.logout();\n";
187 $additional_commands = "";
192 $initialize = "converse.initialize({
193 bosh_service_url: '$bosh_proxy',
195 message_carbons: false,
196 forward_messages: false,
198 sounds_path: 'addon/xmpp/converse/sounds/',
199 roster_groups: false,
200 show_controlbox_by_default: false,
202 allow_contact_removal: false,
203 allow_registration: false,
204 hide_offline_users: true,
205 allow_chat_pending_contacts: false,
206 allow_dragresize: true,
209 csi_waiting_time: 300,
210 auto_reconnect: true,
212 xhr_user_search: false
215 $a->page['htmlhead'] .= "<script>
216 require(['converse'], function (converse) {
218 converse.listen.on('ready', function (event) {