-
Notifications
You must be signed in to change notification settings - Fork 0
/
jaxl.php
812 lines (693 loc) · 23.3 KB
/
jaxl.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
<?php
/**
* Jaxl (Jabber XMPP Library)
*
* Copyright (c) 2009-2012, Abhinav Singh <[email protected]>.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* * Neither the name of Abhinav Singh nor the names of his
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRIC
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
*/
date_default_timezone_set("UTC");
declare(ticks = 1);
define('JAXL_CWD', dirname(__FILE__));
require_once JAXL_CWD.'/core/jaxl_exception.php';
require_once JAXL_CWD.'/core/jaxl_cli.php';
require_once JAXL_CWD.'/core/jaxl_loop.php';
require_once JAXL_CWD.'/xmpp/xmpp_stream.php';
require_once JAXL_CWD.'/xmpp/xmpp_roster_item.php';
require_once JAXL_CWD.'/core/jaxl_event.php';
require_once JAXL_CWD.'/core/jaxl_logger.php';
require_once JAXL_CWD.'/core/jaxl_socket_server.php';
/**
* Jaxl class extends base XMPPStream class with following functionalities:
* 1) Adds an event based wrapper over xmpp stream lifecycle
* 2) Provides restart strategy and signal handling to ensure connectivity of xmpp stream
* 3) Roster management as specified in XMPP-IM
* 4) Management of XEP's inside xmpp stream lifecycle
* 5) Adds a logging facility
* 6) Adds a cron job facility in sync with connected xmpp stream timeline
*
* @author abhinavsingh
*
*/
class JAXL extends XMPPStream {
// lib meta info
const version = '3.0.1';
const name = 'JAXL :: Jabber XMPP Library';
// cached init config array
public $cfg = array();
// event callback engine for xmpp stream lifecycle
protected $ev = null;
// reference to various xep instance objects
public $xeps = array();
// local cache of roster list
public $roster = array();
// whether jaxl must also populate local roster cache with
// received presence information about the contacts
public $manage_roster = true;
// what to do with presence sub requests
// "none" | "accept" | "mutual"
public $manage_subscribe = "none";
// path variables
public $log_level = JAXL_INFO;
public $priv_dir;
public $tmp_dir;
public $log_dir;
public $pid_dir;
public $sock_dir;
// ipc utils
private $sock;
private $cli;
// env
public $local_ip;
public $pid;
public $mode;
// current status message
public $status;
// identity
public $features = array();
public $category = 'client';
public $type = 'bot';
public $lang = 'en';
// after cth failed attempt
// retry connect after k * $retry_interval seconds
// where k is a random number between 0 and 2^c - 1.
public $retry = true;
private $retry_interval = 1;
private $retry_attempt = 0;
private $retry_max_interval = 32; // 2^5 seconds (means 5 max tries)
public function __construct($config) {
$this->cfg = $config;
// setup logger
if(isset($this->cfg['log_path'])) JAXLLogger::$path = $this->cfg['log_path'];
//else JAXLLogger::$path = $this->log_dir."/jaxl.log";
if(isset($this->cfg['log_level'])) JAXLLogger::$level = $this->log_level = $this->cfg['log_level'];
else JAXLLogger::$level = $this->log_level;
// env
$strict = isset($this->cfg['strict']) ? $this->cfg['strict'] : TRUE;
if($strict) $this->add_exception_handlers();
$this->mode = PHP_SAPI;
$this->local_ip = gethostbyname(php_uname('n'));
$this->pid = getmypid();
// jid object
$jid = @$this->cfg['jid'] ? new XMPPJid($this->cfg['jid']) : null;
// handle signals
if(extension_loaded('pcntl')) {
pcntl_signal(SIGHUP, array($this, 'signal_handler'));
pcntl_signal(SIGINT, array($this, 'signal_handler'));
pcntl_signal(SIGTERM, array($this, 'signal_handler'));
}
// create .jaxl directory in JAXL_CWD
// for our /tmp, /run and /log folders
// overwrite these using jaxl config array
$this->priv_dir = @$this->cfg['priv_dir'] ? $this->cfg['priv_dir'] : JAXL_CWD."/.jaxl";
$this->tmp_dir = $this->priv_dir."/tmp";
$this->pid_dir = $this->priv_dir."/run";
$this->log_dir = $this->priv_dir."/log";
$this->sock_dir = $this->priv_dir."/sock";
if(!is_dir($this->priv_dir)) mkdir($this->priv_dir);
if(!is_dir($this->tmp_dir)) mkdir($this->tmp_dir);
if(!is_dir($this->pid_dir)) mkdir($this->pid_dir);
if(!is_dir($this->log_dir)) mkdir($this->log_dir);
if(!is_dir($this->sock_dir)) mkdir($this->sock_dir);
// touch pid file
if($this->mode == "cli") {
touch($this->get_pid_file_path());
_info("created pid file ".$this->get_pid_file_path());
}
// include mandatory xmpp xeps
// service discovery and entity caps
// are recommended for every xmpp entity
$this->require_xep(array('0030', '0115'));
// do dns lookup, update $cfg['host'] and $cfg['port'] if not already specified
$host = @$this->cfg['host'];
$port = @$this->cfg['port'];
if((!$host || !$port) && $jid) {
// this dns lookup is blocking
_info("dns srv lookup for ".$jid->domain);
list($host, $port) = JAXLUtil::get_dns_srv($jid->domain);
}
$this->cfg['host'] = @$this->cfg['host'] ? $this->cfg['host'] : $host;
$this->cfg['port'] = @$this->cfg['port'] ? $this->cfg['port'] : $port;
// choose appropriate transport
// if 'bosh_url' cfg is defined include 0206
if(@$this->cfg['bosh_url']) {
_debug("including bosh xep");
$this->require_xep('0206');
$transport = $this->xeps['0206'];
}
else {
//list($host, $port) = JAXLUtil::get_dns_srv($jid->domain);
$stream_context = @$this->cfg['stream_context'];
$transport = new JAXLSocketClient($stream_context);
}
// lifecycle events callback
$this->ev = new JAXLEvent(defined('JAXL_MULTI_CLIENT') ? array(&$this) : array());
// initialize xmpp stream with configured transport
parent::__construct(
$transport,
$jid,
@$this->cfg['pass'],
@$this->cfg['resource'] ? 'jaxl#'.$this->cfg['resource'] : 'jaxl#'.md5(time()),
@$this->cfg['force_tls']
);
}
public function __destruct() {
// delete pid file
_info("cleaning up pid and unix sock files");
@unlink($this->get_pid_file_path());
@unlink($this->get_sock_file_path());
parent::__destruct();
}
public function add_exception_handlers() {
_info("strict mode enabled, adding exception handlers. Set 'strict'=>TRUE inside JAXL config to disable this");
set_error_handler(array('JAXLException', 'error_handler'));
set_exception_handler(array('JAXLException', 'exception_handler'));
register_shutdown_function(array('JAXLException', 'shutdown_handler'));
}
public function get_pid_file_path() {
return $this->pid_dir."/jaxl_".$this->pid.".pid";
}
public function get_sock_file_path() {
return $this->sock_dir."/jaxl_".$this->pid.".sock";
}
public function require_xep($xeps) {
if(!is_array($xeps))
$xeps = array($xeps);
foreach($xeps as $xep) {
$filename = 'xep_'.$xep.'.php';
$classname = 'XEP_'.$xep;
// include xep
require_once JAXL_CWD.'/xep/'.$filename;
$this->xeps[$xep] = new $classname($this);
// add necessary requested callback on events
foreach($this->xeps[$xep]->init() as $ev=>$cb) {
$this->add_cb($ev, array($this->xeps[$xep], $cb));
}
}
}
public function add_cb($ev, $cb, $pri=1) {
return $this->ev->add($ev, $cb, $pri);
}
public function del_cb($ref) {
$this->ev->del($ref);
}
public function set_status($status, $show='chat', $priority=10) {
$this->send($this->get_pres_pkt(
array(),
$status,
$show,
$priority
));
}
public function send_chat_msg($to, $body, $thread=null, $subject=null) {
$msg = new XMPPMsg(
array(
'type'=>'chat',
'to'=>$to,
'from'=>$this->full_jid->to_string()
),
$body,
$thread,
$subject
);
$this->send($msg);
}
public function get_vcard($jid=null, $cb=null) {
$attrs = array(
'type'=>'get',
'from'=>$this->full_jid->to_string()
);
if($jid) {
$jid = new XMPPJid($jid);
$attrs['to'] = $jid->node."@".$jid->domain;
}
$pkt = $this->get_iq_pkt(
$attrs,
new JAXLXml('vCard', 'vcard-temp')
);
if($cb) $this->add_cb('on_stanza_id_'.$pkt->id, $cb);
$this->send($pkt);
}
public function get_roster($cb=null) {
$pkt = $this->get_iq_pkt(
array(
'type'=>'get',
'from'=>$this->full_jid->to_string()
),
new JAXLXml('query', 'jabber:iq:roster')
);
if($cb) $this->add_cb('on_stanza_id_'.$pkt->id, $cb);
$this->send($pkt);
}
public function subscribe($to) {
$this->send($this->get_pres_pkt(
array('to'=>$to, 'type'=>'subscribe')
));
}
public function subscribed($to) {
$this->send($this->get_pres_pkt(
array('to'=>$to, 'type'=>'subscribed')
));
}
public function unsubscribe($to) {
$this->send($this->get_pres_pkt(
array('to'=>$to, 'type'=>'unsubscribe')
));
}
public function unsubscribed($to) {
$this->send($this->get_pres_pkt(
array('to'=>$to, 'type'=>'unsubscribed')
));
}
public function get_socket_path() {
$protocol = ($this->cfg['port'] == 5223 ? "ssl" : "tcp");
if ($this->cfg['protocol'])
$protocol = $this->cfg['protocol'];
return $protocol."://".$this->cfg['host'].":".$this->cfg['port'];
}
public function retry() {
$retry_after = pow(2, $this->retry_attempt) * $this->retry_interval;
$this->retry_attempt++;
_info("Will try to restart in ".$retry_after." seconds");
// TODO: use jaxl cron if sigalarms cannnot be used
sleep($retry_after);
$this->start();
}
public function start($opts=array()) {
// is bosh bot?
if(@$this->cfg['bosh_url']) {
$this->trans->session_start();
for(;;) {
// while any of the curl request is pending
// keep receiving response
while(sizeof($this->trans->chs) != 0) {
$this->trans->recv();
}
// if no request in queue, ping bosh end point
// and repeat recv
$this->trans->ping();
}
$this->trans->session_end();
return;
}
// is xmpp client or component?
// if on_connect event have no callbacks
// set default on_connect callback to $this->start_stream()
// i.e. xmpp client mode
if(!$this->ev->exists('on_connect'))
$this->add_cb('on_connect', array($this, 'start_stream'));
// connect to the destination host/port
if($this->connect($this->get_socket_path())) {
// reset in case we connected back after retries
$this->retry_attempt = 0;
// emit
$this->ev->emit('on_connect');
// parse opts
if(@$opts['--with-debug-shell']) $this->enable_debug_shell();
if(@$opts['--with-unix-sock']) $this->enable_unix_sock();
// run main loop
JAXLLoop::run();
// emit
$this->ev->emit('on_disconnect');
}
// if connection to the destination fails
else {
if($this->trans->errno == 61
|| $this->trans->errno == 110
|| $this->trans->errno == 111
) {
_debug("unable to connect with errno ".$this->trans->errno." (".$this->trans->errstr.")");
$this->retry();
}
else {
$this->ev->emit('on_connect_error', array(
$this->trans->errno,
$this->trans->errstr
));
}
}
}
//
// callback methods
//
// signals callback handler
// not for public api consumption
public function signal_handler($sig) {
$this->end_stream();
$this->disconnect();
$this->ev->emit('on_disconnect');
switch($sig) {
// terminal line hangup
case SIGHUP:
_debug("got sighup");
break;
// interrupt program
case SIGINT:
_debug("got sigint");
break;
// software termination signal
case SIGTERM:
_debug("got sigterm");
break;
}
exit;
}
// called internally for ipc
// not for public consumption
public function on_unix_sock_accept($_c, $addr) {
$this->sock->read($_c);
}
// this currently simply evals the incoming raw string
// know what you are doing while in production
public function on_unix_sock_request($_c, $_raw) {
_debug("evaling raw string rcvd over unix sock: ".$_raw);
$this->sock->send($_c, serialize(eval($_raw)));
$this->sock->read($_c);
}
public function enable_unix_sock() {
$this->sock = new JAXLSocketServer(
'unix://'.$this->get_sock_file_path(),
array(&$this, 'on_unix_sock_accept'),
array(&$this, 'on_unix_sock_request')
);
}
// this simply eval the incoming raw data
// inside current jaxl environment
// security is all upto you, no checks made here
public function handle_debug_shell($_raw) {
print_r(eval($_raw));
echo PHP_EOL;
JAXLCli::prompt();
}
protected function enable_debug_shell() {
$this->cli = new JAXLCli(array(&$this, 'handle_debug_shell'));
JAXLCli::prompt();
}
//
// abstract method implementation
//
protected function send_fb_challenge_response($challenge) {
$this->send($this->get_fb_challenge_response_pkt($challenge));
}
// refer https://developers.facebook.com/docs/chat/#jabber
public function get_fb_challenge_response_pkt($challenge) {
$stanza = new JAXLXml('response', NS_SASL);
$challenge = base64_decode($challenge);
$challenge = urldecode($challenge);
parse_str($challenge, $challenge_arr);
$response = http_build_query(array(
'method' => $challenge_arr['method'],
'nonce' => $challenge_arr['nonce'],
'access_token' => $this->cfg['fb_access_token'],
'api_key' => $this->cfg['fb_app_key'],
'call_id' => 0,
'v' => '1.0'
));
$stanza->t(base64_encode($response));
return $stanza;
}
public function wait_for_fb_sasl_response($event, $args) {
switch($event) {
case "stanza_cb":
$stanza = $args[0];
if($stanza->name == 'challenge' && $stanza->ns == NS_SASL) {
$challenge = $stanza->text;
$this->send_fb_challenge_response($challenge);
return "wait_for_sasl_response";
}
else {
_debug("got unhandled sasl response, should never happen here");
exit;
}
break;
default:
_debug("not catched $event, should never happen here");
exit;
break;
}
}
// someday this needs to go inside xmpp stream
public function wait_for_cram_md5_response($event, $args) {
switch($event) {
case "stanza_cb":
$stanza = $args[0];
if($stanza->name == 'challenge' && $stanza->ns == NS_SASL) {
$challenge = base64_decode($stanza->text);
$resp = new JAXLXml('response', NS_SASL);
$resp->t(base64_encode($this->jid->to_string().' '.hash_hmac('md5', $challenge, $this->pass)));
$this->send($resp);
return "wait_for_sasl_response";
}
else {
_debug("got unhandled sasl response, should never happen here");
exit;
}
break;
default:
_debug("not catched $event, should never happen here");
exit;
break;
}
}
// http://tools.ietf.org/html/rfc5802#section-5
public function get_scram_sha1_response($pass, $challenge) {
// it contains users iteration count i and the user salt
// also server will append it's own nonce to the one we specified
$decoded = $this->explode_data(base64_decode($challenge));
// r=,s=,i=
$nonce = $decoded['r'];
$salt = base64_decode($decoded['s']);
$iteration = intval($decoded['i']);
// SaltedPassword := Hi(Normalize(password), salt, i)
$salted = JAXLUtil::pbkdf2($this->pass, $salt, $iteration);
// ClientKey := HMAC(SaltedPassword, "Client Key")
$client_key = hash_hmac('sha1', $salted, "Client Key", true);
// StoredKey := H(ClientKey)
$stored_key = hash('sha1', $client_key, true);
// AuthMessage := client-first-message-bare + "," + server-first-message + "," + client-final-message-without-proof
$auth_message = '';
// ClientSignature := HMAC(StoredKey, AuthMessage)
$signature = hash_hmac('sha1', $stored_key, $auth_message, true);
// ClientProof := ClientKey XOR ClientSignature
$client_proof = $client_key ^ $signature;
$proof = 'c=biws,r='.$nonce.',p='.base64_encode($client_proof);
return base64_encode($proof);
}
public function wait_for_scram_sha1_response($event, $args) {
switch($event) {
case "stanza_cb":
$stanza = $args[0];
if($stanza->name == 'challenge' && $stanza->ns == NS_SASL) {
$challenge = $stanza->text;
$resp = new JAXLXml('response', NS_SASL);
$resp->t($this->get_scram_sha1_response($this->pass, $challenge));
$this->send($resp);
return "wait_for_sasl_response";
}
else {
_debug("got unhandled sasl response, should never happen here");
exit;
}
break;
default:
_debug("not catched $event, should never happen here");
exit;
break;
}
}
public function handle_auth_mechs($stanza, $mechanisms) {
if($this->ev->exists('on_stream_features')) {
return $this->ev->emit('on_stream_features', array($stanza));
}
// extract available mechanisms
$mechs = array();
if($mechanisms) foreach($mechanisms->childrens as $mechanism) $mechs[$mechanism->text] = true;
// check if preferred auth type exists in available mechanisms
$pref_auth = @$this->cfg['auth_type'] ? $this->cfg['auth_type'] : 'PLAIN';
$pref_auth_exists = isset($mechs[$pref_auth]) ? true : false;
_debug("pref_auth ".$pref_auth." ".($pref_auth_exists ? "exists" : "doesn't exists"));
// if pref auth exists, try it
if($pref_auth_exists) {
$mech = $pref_auth;
}
// if pref auth doesn't exists, choose one from available mechanisms
else {
foreach($mechs as $mech=>$any) {
// choose X-FACEBOOK-PLATFORM only if fb_access_token config value is available
if($mech == 'X-FACEBOOK-PLATFORM') {
if(@$this->cfg['fb_access_token']) {
break;
}
}
// else try first of the available methods
else {
break;
}
}
_error("preferred auth type not supported, trying $mech");
}
$this->send_auth_pkt($mech, @$this->jid ? $this->jid->to_string() : null, @$this->pass);
if($pref_auth == 'X-FACEBOOK-PLATFORM') {
return "wait_for_fb_sasl_response";
}
else if($pref_auth == 'CRAM-MD5') {
return "wait_for_cram_md5_response";
}
else if($pref_auth == 'SCRAM-SHA-1') {
return "wait_for_scram_sha1_response";
}
}
public function handle_auth_success() {
// if not a component
/*if(!@$this->xeps['0114']) {
$this->xeps['0030']->get_info($this->full_jid->domain, array(&$this, 'handle_domain_info'));
$this->xeps['0030']->get_items($this->full_jid->domain, array(&$this, 'handle_domain_items'));
}*/
$this->ev->emit('on_auth_success');
}
public function handle_auth_failure($reason) {
$this->ev->emit('on_auth_failure', array(
$reason
));
}
public function handle_stream_start($stanza) {
$stanza = new XMPPStanza($stanza);
$this->ev->emit('on_stream_start', array($stanza));
return array(@$this->cfg['bosh_url'] ? 'wait_for_stream_features' : 'connected', 1);
}
public function handle_iq($stanza) {
$stanza = new XMPPStanza($stanza);
// emit callback registered on stanza id's
$emited = false;
if($stanza->id && $this->ev->exists('on_stanza_id_'.$stanza->id)) {
//_debug("on stanza id callbackd");
$emited = true;
$this->ev->emit('on_stanza_id_'.$stanza->id, array($stanza));
}
// catch roster list
if($stanza->type == 'result' && ($query = $stanza->exists('query', 'jabber:iq:roster'))) {
foreach($query->childrens as $child) {
if($child->name == 'item') {
$jid = $child->attrs['jid'];
$subscription = $child->attrs['subscription'];
$groups = array();
foreach($child->childrens as $group) {
if($group->name == 'group') {
$groups[] = $group->text;
}
}
$this->roster[$jid] = new XMPPRosterItem($jid, $subscription, $groups);
}
}
// emit this event if not emited above
if(!$emited)
$this->ev->emit('on_roster_update');
}
// if managing roster
// catch contact vcard results
if($this->manage_roster && $stanza->type == 'result' && ($query = $stanza->exists('vCard', 'vcard-temp'))) {
if(@$this->roster[$stanza->from])
$this->roster[$stanza->from]->vcard = $query;
}
// on_get_iq, on_result_iq, and other events are only
// emitted if on_stanza_id_{id} wasn't emitted above
// TODO: can we add more checks here before calling back
// e.g. checks on existence of an attribute, check on 1st level child ns and so on
if(!$emited)
$this->ev->emit('on_'.$stanza->type.'_iq', array($stanza));
}
public function handle_presence($stanza) {
$stanza = new XMPPStanza($stanza);
// if managing roster
// catch available/unavailable type stanza
if($this->manage_roster) {
$type = ($stanza->type ? $stanza->type : "available");
$jid = new XMPPJid($stanza->from);
if($type == 'available') {
$this->roster[$jid->bare]->resources[$jid->resource] = $stanza;
}
else if($type == 'unavailable') {
if(@$this->roster[$jid->bare] && @$this->roster[$jid->bare]->resources[$jid->resource])
unset($this->roster[$jid->bare]->resources[$jid->resource]);
}
}
// if managing subscription requests
// we need to automate stuff here
if($stanza->type == "subscribe" && $this->manage_subscribe != "none") {
$this->subscribed($stanza->from);
if($this->manage_subscribe == "mutual")
$this->subscribe($stanza->from);
}
$this->ev->emit('on_presence_stanza', array($stanza));
}
public function handle_message($stanza) {
$stanza = new XMPPStanza($stanza);
$stanza->type = (@$stanza->type ? $stanza->type : 'normal');
$this->ev->emit('on_'.$stanza->type.'_message', array($stanza));
}
// unhandled event and arguments bubbled up
// TODO: in a lot of cases this will be called, need more checks
public function handle_other($event, $args) {
$stanza = $args[0];
$stanza = new XMPPStanza($stanza);
$ev = 'on_'.$stanza->name.'_stanza';
if($this->ev->exists($ev)) {
return $this->ev->emit($ev, array($stanza));
}
else {
_warning("event '".$event."' catched in handle_other with stanza name ".$stanza->name);
}
}
public function handle_domain_info($stanza) {
$query = $stanza->exists('query', NS_DISCO_INFO);
foreach($query->childrens as $k=>$child) {
if($child->name == 'identity') {
//echo 'identity category:'.@$child->attrs['category'].', type:'.@$child->attrs['type'].', name:'.@$child->attrs['name'].PHP_EOL;
}
else if($child->name == 'x') {
//echo 'x ns:'.$child->ns.PHP_EOL;
}
else if($child->name == 'feature') {
//echo 'feature var:'.$child->attrs['var'].PHP_EOL;
}
}
}
public function handle_domain_items($stanza) {
$query = $stanza->exists('query', NS_DISCO_ITEMS);
foreach($query->childrens as $k=>$child) {
if($child->name == 'item') {
//echo 'item jid:'.@$child->attrs['jid'].', name:'.@$child->attrs['name'].', node:'.@$child->attrs['node'].PHP_EOL;
}
}
}
}
?>