]> git.mxchange.org Git - friendica-addons.git/blob - xmpp/xmpp.php
Don't activate chat on mobile
[friendica-addons.git] / xmpp / xmpp.php
1 <?php
2 /**
3  * Name: XMPP (Jabber)
4  * Description: Embedded XMPP (Jabber) client
5  * Version: 0.1
6  * Author: Michael Vogel <https://pirati.ca/profile/heluecht>
7  */
8
9 function xmpp_install() {
10         register_hook('page_end', 'addon/xmpp/xmpp.php', 'xmpp_script');
11         register_hook('logged_in', 'addon/xmpp/xmpp.php', 'xmpp_login');
12 }
13
14 function xmpp_uninstall() {
15         unregister_hook('page_end', 'addon/xmpp/xmpp.php', 'xmpp_script');
16         unregister_hook('logged_in', 'addon/xmpp/xmpp.php', 'xmpp_login');
17 }
18
19 function xmpp_login($a,$b) {
20         if (!$_SESSION["allow_api"]) {
21                 $password = substr(random_string(),0,16);
22                 set_pconfig(local_user(), "xmpp", "password", $password);
23         }
24 }
25
26 function xmpp_plugin_admin(&$a, &$o){
27         $t = get_markup_template("admin.tpl", "addon/xmpp/");
28
29         $o = replace_macros($t, array(
30                 '$submit' => t('Save Settings'),
31                 '$bosh_proxy'       => array('bosh_proxy', t('BOSH proxy'),            get_config('xmpp', 'bosh_proxy'), ''),
32                 '$central_userbase' => array('central_userbase', t('Use central userbase'), get_config('xmpp', 'central_userbase'), 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.')),
33         ));
34 }
35
36 function xmpp_plugin_admin_post(&$a){
37         $bosh_proxy       = ((x($_POST,'bosh_proxy')) ?       trim($_POST['bosh_proxy']) : '');
38         $central_userbase = ((x($_POST,'central_userbase')) ? intval($_POST['central_userbase']) : false);
39         set_config('xmpp','bosh_proxy',$bosh_proxy);
40         set_config('xmpp','central_userbase',$central_userbase);
41         info( t('Settings updated.'). EOL );
42 }
43
44 function xmpp_script(&$a,&$s) {
45         xmpp_converse($a,$s);
46         //xmpp_jappix($a,$s);
47 }
48
49 function xmpp_converse(&$a,&$s) {
50         if (!local_user())
51                 return;
52
53         if ($_GET["mode"] == "minimal")
54                 return;
55
56         if ($a->is_mobile || $a->is_tablet)
57                 return;
58
59         $a->page['htmlhead'] .= '<link type="text/css" rel="stylesheet" media="screen" href="addon/xmpp/converse/css/converse.css" />'."\n";
60         $a->page['htmlhead'] .= '<script src="addon/xmpp/converse/builds/converse.min.js"></script>'."\n";
61
62         $bosh_proxy = get_config("xmpp", "bosh_proxy");
63
64         if (get_config("xmpp", "central_userbase")) {
65                 $password = get_pconfig(local_user(), "xmpp", "password");
66
67                 if ($password == "") {
68                         $password = substr(random_string(),0,16);
69                         set_pconfig(local_user(), "xmpp", "password", $password);
70                 }
71
72                 $jid = $a->user["nickname"]."@".$a->get_hostname()."/converse-".substr(random_string(),0,5);;
73
74                 $auto_login = "auto_login: true,
75                         authentication: 'login',
76                         jid: '$jid',
77                         password: '$password',
78                         allow_logout: true,
79                         auto_list_rooms: true,";
80         } else
81                 $auto_login = "";
82
83         if (in_array($a->argv[0], array("manage", "logout")))
84                 $additional_commands = "converse.user.logout();\n";
85         else
86                 $additional_commands = "";
87
88         $on_ready = "";
89         //$on_ready = "converse.rooms.open(['support@conference.pirati.ca']);\n";
90
91 //  converse.contacts.add('ike@jabber.piratenpartei.de');
92
93         $initialize = "converse.initialize({
94                                         bosh_service_url: '$bosh_proxy',
95                                         keepalive: true,
96                                         message_carbons: false,
97                                         forward_messages: false,
98                                         play_sounds: true,
99                                         sounds_path: 'addon/xmpp/converse/sounds/',
100                                         roster_groups: false,
101                                         show_controlbox_by_default: false,
102                                         show_toolbar: true,
103                                         allow_contact_removal: false,
104                                         allow_registration: false,
105                                         hide_offline_users: true,
106                                         allow_chat_pending_contacts: false,
107                                         allow_dragresize: true,
108                                         auto_away: 0,
109                                         auto_xa: 0,
110                                         csi_waiting_time: 300,
111                                         auto_reconnect: true,
112                                         $auto_login
113                                         xhr_user_search: false
114                                 });\n";
115
116         $a->page['htmlhead'] .= "<script>
117                                         require(['converse'], function (converse) {
118                                                 $initialize
119                                                 converse.listen.on('ready', function (event) {
120                                                         $on_ready
121                                                 });
122                                                 $additional_commands
123                                         });
124                                 </script>";
125 }
126
127 function xmpp_jappix(&$a,&$s) {
128         if (!local_user())
129                 return;
130
131         if ($_GET["mode"] == "minimal")
132                 return;
133
134         $bosh_proxy = get_config("xmpp", "bosh_proxy");
135
136         if (get_config("xmpp", "central_userbase")) {
137                 $password = get_pconfig(local_user(), "xmpp", "password");
138
139                 if ($password == "") {
140                         $password = substr(random_string(),0,16);
141                         set_pconfig(local_user(), "xmpp", "password", $password);
142                 }
143
144                 $user = $a->user["nickname"];
145                 $domain = $a->get_hostname();
146
147                 $auto_login = "auto_login: true,
148                         authentication: 'login',
149                         jid: '$jid',
150                         password: '$password',
151                         allow_logout: false,";
152         } else
153                 $auto_login = "";
154
155         //$a->page['htmlhead'] .= "<script type='text/javascript' src='//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js'></script>";
156
157         $a->page['htmlhead'] .= "<script type='text/javascript' src='addon/xmpp/jappix/javascripts/mini.js'></script>
158                                         <script type='text/javascript'>
159                                                 jQuery(document).ready(function() {
160                                                         JAPPIX_STATIC = 'addon/xmpp/jappix/';
161                                                         HOST_BOSH = '$bosh_proxy'
162
163                                                         JappixMini.launch({
164                                                            connection: {
165                                                              user: '$user',
166                                                              password: '$password',
167                                                              domain: '$domain'
168                                                            },
169                                                            application: {
170                                                              network: {
171                                                                autoconnect: true
172                                                              },
173                                                              interface: {
174                                                                showpane: false
175                                                              }
176                                                            }
177                                                         });
178                                                 });
179                                 </script>";
180 }
181
182 ?>