]> git.mxchange.org Git - friendica-addons.git/blob - xmpp/xmpp.php
New addon XMPP
[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         $a->page['htmlhead'] .= '<link type="text/css" rel="stylesheet" media="screen" href="addon/xmpp/converse/css/converse.css" />'."\n";
57         $a->page['htmlhead'] .= '<script src="addon/xmpp/converse/builds/converse.min.js"></script>'."\n";
58
59         $bosh_proxy = get_config("xmpp", "bosh_proxy");
60
61         if (get_config("xmpp", "central_userbase")) {
62                 $password = get_pconfig(local_user(), "xmpp", "password");
63
64                 if ($password == "") {
65                         $password = substr(random_string(),0,16);
66                         set_pconfig(local_user(), "xmpp", "password", $password);
67                 }
68
69                 $jid = $a->user["nickname"]."@".$a->get_hostname()."/converse-".substr(random_string(),0,5);;
70
71                 $auto_login = "auto_login: true,
72                         authentication: 'login',
73                         jid: '$jid',
74                         password: '$password',
75                         allow_logout: true,
76                         auto_list_rooms: true,";
77         } else
78                 $auto_login = "";
79
80         if (in_array($a->argv[0], array("manage", "logout")))
81                 $additional_commands = "converse.user.logout();\n";
82         else
83                 $additional_commands = "";
84
85         $on_ready = "";
86         //$on_ready = "converse.rooms.open(['support@conference.pirati.ca']);\n";
87
88 //  converse.contacts.add('ike@jabber.piratenpartei.de');
89
90         $initialize = "converse.initialize({
91                                         bosh_service_url: '$bosh_proxy',
92                                         keepalive: true,
93                                         message_carbons: false,
94                                         forward_messages: false,
95                                         play_sounds: true,
96                                         sounds_path: 'addon/xmpp/converse/sounds/',
97                                         roster_groups: false,
98                                         show_controlbox_by_default: false,
99                                         show_toolbar: true,
100                                         allow_contact_removal: false,
101                                         allow_registration: false,
102                                         hide_offline_users: true,
103                                         allow_chat_pending_contacts: false,
104                                         allow_dragresize: true,
105                                         auto_away: 0,
106                                         auto_xa: 0,
107                                         csi_waiting_time: 300,
108                                         auto_reconnect: true,
109                                         $auto_login
110                                         xhr_user_search: false
111                                 });\n";
112
113         $a->page['htmlhead'] .= "<script>
114                                         require(['converse'], function (converse) {
115                                                 $initialize
116                                                 converse.listen.on('ready', function (event) {
117                                                         $on_ready
118                                                 });
119                                                 $additional_commands
120                                         });
121                                 </script>";
122 }
123
124 function xmpp_jappix(&$a,&$s) {
125         if (!local_user())
126                 return;
127
128         if ($_GET["mode"] == "minimal")
129                 return;
130
131         $bosh_proxy = get_config("xmpp", "bosh_proxy");
132
133         if (get_config("xmpp", "central_userbase")) {
134                 $password = get_pconfig(local_user(), "xmpp", "password");
135
136                 if ($password == "") {
137                         $password = substr(random_string(),0,16);
138                         set_pconfig(local_user(), "xmpp", "password", $password);
139                 }
140
141                 $user = $a->user["nickname"];
142                 $domain = $a->get_hostname();
143
144                 $auto_login = "auto_login: true,
145                         authentication: 'login',
146                         jid: '$jid',
147                         password: '$password',
148                         allow_logout: false,";
149         } else
150                 $auto_login = "";
151
152         //$a->page['htmlhead'] .= "<script type='text/javascript' src='//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js'></script>";
153
154         $a->page['htmlhead'] .= "<script type='text/javascript' src='addon/xmpp/jappix/javascripts/mini.js'></script>
155                                         <script type='text/javascript'>
156                                                 jQuery(document).ready(function() {
157                                                         JAPPIX_STATIC = 'addon/xmpp/jappix/';
158                                                         HOST_BOSH = '$bosh_proxy'
159
160                                                         JappixMini.launch({
161                                                            connection: {
162                                                              user: '$user',
163                                                              password: '$password',
164                                                              domain: '$domain'
165                                                            },
166                                                            application: {
167                                                              network: {
168                                                                autoconnect: true
169                                                              },
170                                                              interface: {
171                                                                showpane: false
172                                                              }
173                                                            }
174                                                         });
175                                                 });
176                                 </script>";
177 }
178
179 ?>