4 * Description: Embedded XMPP (Jabber) client
6 * Author: Michael Vogel <https://pirati.ca/profile/heluecht>
9 use Friendica\Core\Addon;
10 use Friendica\Core\Config;
11 use Friendica\Core\L10n;
12 use Friendica\Core\PConfig;
14 function xmpp_install()
16 Addon::registerHook('addon_settings', 'addon/xmpp/xmpp.php', 'xmpp_addon_settings');
17 Addon::registerHook('addon_settings_post', 'addon/xmpp/xmpp.php', 'xmpp_addon_settings_post');
18 Addon::registerHook('page_end', 'addon/xmpp/xmpp.php', 'xmpp_script');
19 Addon::registerHook('logged_in', 'addon/xmpp/xmpp.php', 'xmpp_login');
22 function xmpp_uninstall()
24 Addon::unregisterHook('addon_settings', 'addon/xmpp/xmpp.php', 'xmpp_addon_settings');
25 Addon::unregisterHook('addon_settings_post', 'addon/xmpp/xmpp.php', 'xmpp_addon_settings_post');
26 Addon::unregisterHook('page_end', 'addon/xmpp/xmpp.php', 'xmpp_script');
27 Addon::unregisterHook('logged_in', 'addon/xmpp/xmpp.php', 'xmpp_login');
30 function xmpp_addon_settings_post()
32 if (!local_user() || (!x($_POST, 'xmpp-settings-submit'))) {
35 PConfig::set(local_user(), 'xmpp', 'enabled', intval($_POST['xmpp_enabled']));
36 PConfig::set(local_user(), 'xmpp', 'individual', intval($_POST['xmpp_individual']));
37 PConfig::set(local_user(), 'xmpp', 'bosh_proxy', $_POST['xmpp_bosh_proxy']);
39 info(L10n::t('XMPP settings updated.') . EOL);
42 function xmpp_addon_settings(App $a, &$s)
48 /* Add our stylesheet to the xmpp so we can make our settings look nice */
50 $a->page['htmlhead'] .= '<link rel="stylesheet" type="text/css" href="' . $a->get_baseurl() . '/addon/xmpp/xmpp.css' . '" media="all" />' . "\r\n";
52 /* Get the current state of our config variable */
54 $enabled = intval(PConfig::get(local_user(), 'xmpp', 'enabled'));
55 $enabled_checked = (($enabled) ? ' checked="checked" ' : '');
57 $individual = intval(PConfig::get(local_user(), 'xmpp', 'individual'));
58 $individual_checked = (($individual) ? ' checked="checked" ' : '');
60 $bosh_proxy = PConfig::get(local_user(), "xmpp", "bosh_proxy");
62 /* Add some HTML to the existing form */
63 $s .= '<span id="settings_xmpp_inflated" class="settings-block fakelink" style="display: block;" onclick="openClose(\'settings_xmpp_expanded\'); openClose(\'settings_xmpp_inflated\');">';
64 $s .= '<h3>' . L10n::t('XMPP-Chat (Jabber)') . '</h3>';
66 $s .= '<div id="settings_xmpp_expanded" class="settings-block" style="display: none;">';
67 $s .= '<span class="fakelink" onclick="openClose(\'settings_xmpp_expanded\'); openClose(\'settings_xmpp_inflated\');">';
68 $s .= '<h3>' . L10n::t('XMPP-Chat (Jabber)') . '</h3>';
71 $s .= '<div id="xmpp-settings-wrapper">';
72 $s .= '<label id="xmpp-enabled-label" for="xmpp-enabled">' . L10n::t('Enable Webchat') . '</label>';
73 $s .= '<input id="xmpp-enabled" type="checkbox" name="xmpp_enabled" value="1" ' . $enabled_checked . '/>';
74 $s .= '<div class="clear"></div>';
76 if (Config::get("xmpp", "central_userbase")) {
77 $s .= '<label id="xmpp-individual-label" for="xmpp-individual">' . L10n::t('Individual Credentials') . '</label>';
78 $s .= '<input id="xmpp-individual" type="checkbox" name="xmpp_individual" value="1" ' . $individual_checked . '/>';
79 $s .= '<div class="clear"></div>';
82 if (!Config::get("xmpp", "central_userbase") || PConfig::get(local_user(), "xmpp", "individual")) {
83 $s .= '<label id="xmpp-bosh-proxy-label" for="xmpp-bosh-proxy">' . L10n::t('Jabber BOSH host') . '</label>';
84 $s .= ' <input id="xmpp-bosh-proxy" type="text" name="xmpp_bosh_proxy" value="' . $bosh_proxy . '" />';
85 $s .= '<div class="clear"></div>';
90 /* provide a submit button */
92 $s .= '<div class="settings-submit-wrapper" ><input type="submit" name="xmpp-settings-submit" class="settings-submit" value="' . L10n::t('Save Settings') . '" /></div></div>';
97 if (!$_SESSION["allow_api"]) {
98 $password = random_string(16);
99 PConfig::set(local_user(), "xmpp", "password", $password);
103 function xmpp_addon_admin(App $a, &$o)
105 $t = get_markup_template("admin.tpl", "addon/xmpp/");
107 $o = replace_macros($t, [
108 '$submit' => L10n::t('Save Settings'),
109 '$bosh_proxy' => ['bosh_proxy', L10n::t('Jabber BOSH host'), Config::get('xmpp', 'bosh_proxy'), ''],
110 '$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.')],
114 function xmpp_addon_admin_post()
116 $bosh_proxy = ((x($_POST, 'bosh_proxy')) ? trim($_POST['bosh_proxy']) : '');
117 $central_userbase = ((x($_POST, 'central_userbase')) ? intval($_POST['central_userbase']) : false);
118 Config::set('xmpp', 'bosh_proxy', $bosh_proxy);
119 Config::set('xmpp', 'central_userbase', $central_userbase);
120 info(L10n::t('Settings updated.') . EOL);
123 function xmpp_script(App $a)
128 function xmpp_converse(App $a)
134 if ($_GET["mode"] == "minimal") {
138 if ($a->is_mobile || $a->is_tablet) {
142 if (!PConfig::get(local_user(), "xmpp", "enabled")) {
146 if (in_array($a->query_string, ["admin/federation/"])) {
150 $a->page['htmlhead'] .= '<link type="text/css" rel="stylesheet" media="screen" href="addon/xmpp/converse/css/converse.css" />' . "\n";
151 $a->page['htmlhead'] .= '<script src="addon/xmpp/converse/builds/converse.min.js"></script>' . "\n";
153 if (Config::get("xmpp", "central_userbase") && !PConfig::get(local_user(), "xmpp", "individual")) {
154 $bosh_proxy = Config::get("xmpp", "bosh_proxy");
156 $password = PConfig::get(local_user(), "xmpp", "password", '', true);
158 if ($password == "") {
159 $password = random_string(16);
160 PConfig::set(local_user(), "xmpp", "password", $password);
163 $jid = $a->user["nickname"] . "@" . $a->get_hostname() . "/converse-" . random_string(5);
165 $auto_login = "auto_login: true,
166 authentication: 'login',
168 password: '$password',
169 allow_logout: false,";
171 $bosh_proxy = PConfig::get(local_user(), "xmpp", "bosh_proxy");
176 if ($bosh_proxy == "") {
180 if (in_array($a->argv[0], ["manage", "logout"])) {
181 $additional_commands = "converse.user.logout();\n";
183 $additional_commands = "";
188 $initialize = "converse.initialize({
189 bosh_service_url: '$bosh_proxy',
191 message_carbons: false,
192 forward_messages: false,
194 sounds_path: 'addon/xmpp/converse/sounds/',
195 roster_groups: false,
196 show_controlbox_by_default: false,
198 allow_contact_removal: false,
199 allow_registration: false,
200 hide_offline_users: true,
201 allow_chat_pending_contacts: false,
202 allow_dragresize: true,
205 csi_waiting_time: 300,
206 auto_reconnect: true,
208 xhr_user_search: false
211 $a->page['htmlhead'] .= "<script>
212 require(['converse'], function (converse) {
214 converse.listen.on('ready', function (event) {