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'))) {
34 set_pconfig(local_user(),'xmpp','enabled',intval($_POST['xmpp_enabled']));
35 set_pconfig(local_user(),'xmpp','individual',intval($_POST['xmpp_individual']));
36 set_pconfig(local_user(),'xmpp','bosh_proxy',$_POST['xmpp_bosh_proxy']);
38 info(L10n::t('XMPP settings updated.') . EOL);
41 function xmpp_addon_settings(App $a, &$s)
46 /* Add our stylesheet to the xmpp so we can make our settings look nice */
48 $a->page['htmlhead'] .= '<link rel="stylesheet" type="text/css" href="' . $a->get_baseurl() . '/addon/xmpp/xmpp.css' . '" media="all" />' . "\r\n";
50 /* Get the current state of our config variable */
52 $enabled = intval(get_pconfig(local_user(),'xmpp','enabled'));
53 $enabled_checked = (($enabled) ? ' checked="checked" ' : '');
55 $individual = intval(get_pconfig(local_user(),'xmpp','individual'));
56 $individual_checked = (($individual) ? ' checked="checked" ' : '');
58 $bosh_proxy = get_pconfig(local_user(),"xmpp","bosh_proxy");
60 /* Add some HTML to the existing form */
61 $s .= '<span id="settings_xmpp_inflated" class="settings-block fakelink" style="display: block;" onclick="openClose(\'settings_xmpp_expanded\'); openClose(\'settings_xmpp_inflated\');">';
62 $s .= '<h3>' . L10n::t('XMPP-Chat (Jabber)') . '</h3>';
64 $s .= '<div id="settings_xmpp_expanded" class="settings-block" style="display: none;">';
65 $s .= '<span class="fakelink" onclick="openClose(\'settings_xmpp_expanded\'); openClose(\'settings_xmpp_inflated\');">';
66 $s .= '<h3>' . L10n::t('XMPP-Chat (Jabber)') . '</h3>';
69 $s .= '<div id="xmpp-settings-wrapper">';
70 $s .= '<label id="xmpp-enabled-label" for="xmpp-enabled">' . L10n::t('Enable Webchat') . '</label>';
71 $s .= '<input id="xmpp-enabled" type="checkbox" name="xmpp_enabled" value="1" ' . $enabled_checked . '/>';
72 $s .= '<div class="clear"></div>';
74 if (Config::get("xmpp", "central_userbase")) {
75 $s .= '<label id="xmpp-individual-label" for="xmpp-individual">' . L10n::t('Individual Credentials') . '</label>';
76 $s .= '<input id="xmpp-individual" type="checkbox" name="xmpp_individual" value="1" ' . $individual_checked . '/>';
77 $s .= '<div class="clear"></div>';
80 if (!Config::get("xmpp", "central_userbase") || PConfig::get(local_user(), "xmpp", "individual")) {
81 $s .= '<label id="xmpp-bosh-proxy-label" for="xmpp-bosh-proxy">' . L10n::t('Jabber BOSH host') . '</label>';
82 $s .= ' <input id="xmpp-bosh-proxy" type="text" name="xmpp_bosh_proxy" value="' . $bosh_proxy . '" />';
83 $s .= '<div class="clear"></div>';
88 /* provide a submit button */
90 $s .= '<div class="settings-submit-wrapper" ><input type="submit" name="xmpp-settings-submit" class="settings-submit" value="' . L10n::t('Save Settings') . '" /></div></div>';
93 function xmpp_login($a,$b) {
94 if (!$_SESSION["allow_api"]) {
95 $password = substr(random_string(),0,16);
96 set_pconfig(local_user(), "xmpp", "password", $password);
100 function xmpp_addon_admin(App $a, &$o)
102 $t = get_markup_template("admin.tpl", "addon/xmpp/");
104 $o = replace_macros($t, [
105 '$submit' => L10n::t('Save Settings'),
106 '$bosh_proxy' => ['bosh_proxy', L10n::t('Jabber BOSH host'), Config::get('xmpp', 'bosh_proxy'), ''],
107 '$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.')],
111 function xmpp_addon_admin_post()
113 $bosh_proxy = ((x($_POST, 'bosh_proxy')) ? trim($_POST['bosh_proxy']) : '');
114 $central_userbase = ((x($_POST, 'central_userbase')) ? intval($_POST['central_userbase']) : false);
115 Config::set('xmpp', 'bosh_proxy', $bosh_proxy);
116 Config::set('xmpp', 'central_userbase', $central_userbase);
117 info(L10n::t('Settings updated.') . EOL);
120 function xmpp_script(&$a,&$s) {
121 xmpp_converse($a,$s);
124 function xmpp_converse(&$a,&$s) {
128 if ($_GET["mode"] == "minimal")
131 if ($a->is_mobile || $a->is_tablet)
134 if (!get_pconfig(local_user(),"xmpp","enabled"))
137 if (in_array($a->query_string, array("admin/federation/")))
140 $a->page['htmlhead'] .= '<link type="text/css" rel="stylesheet" media="screen" href="addon/xmpp/converse/css/converse.css" />'."\n";
141 $a->page['htmlhead'] .= '<script src="addon/xmpp/converse/builds/converse.min.js"></script>'."\n";
143 if (get_config("xmpp", "central_userbase") && !get_pconfig(local_user(),"xmpp","individual")) {
144 $bosh_proxy = get_config("xmpp", "bosh_proxy");
146 $password = get_pconfig(local_user(), "xmpp", "password");
148 if ($password == "") {
149 $password = substr(random_string(),0,16);
150 set_pconfig(local_user(), "xmpp", "password", $password);
153 $jid = $a->user["nickname"]."@".$a->get_hostname()."/converse-".substr(random_string(),0,5);;
155 $auto_login = "auto_login: true,
156 authentication: 'login',
158 password: '$password',
159 allow_logout: false,";
161 $bosh_proxy = get_pconfig(local_user(), "xmpp", "bosh_proxy");
166 if ($bosh_proxy == "")
169 if (in_array($a->argv[0], array("manage", "logout")))
170 $additional_commands = "converse.user.logout();\n";
172 $additional_commands = "";
176 $initialize = "converse.initialize({
177 bosh_service_url: '$bosh_proxy',
179 message_carbons: false,
180 forward_messages: false,
182 sounds_path: 'addon/xmpp/converse/sounds/',
183 roster_groups: false,
184 show_controlbox_by_default: false,
186 allow_contact_removal: false,
187 allow_registration: false,
188 hide_offline_users: true,
189 allow_chat_pending_contacts: false,
190 allow_dragresize: true,
193 csi_waiting_time: 300,
194 auto_reconnect: true,
196 xhr_user_search: false
199 $a->page['htmlhead'] .= "<script>
200 require(['converse'], function (converse) {
202 converse.listen.on('ready', function (event) {