File: Jabber.pm

package info (click to toggle)
libpoe-component-jabber-perl 2.02-1
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 232 kB
  • ctags: 88
  • sloc: perl: 2,623; makefile: 44
file content (1127 lines) | stat: -rw-r--r-- 25,210 bytes parent folder | download
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
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
package POE::Component::Jabber;
use Filter::Template;
const XNode POE::Filter::XML::Node
use warnings;
use strict;

use POE;
use POE::Wheel::ReadWrite;
use POE::Wheel::SocketFactory;
use POE::Component::Jabber::Error;
use POE::Component::Jabber::Status;
use POE::Component::Jabber::ProtocolFactory;
use POE::Filter::XML;
use POE::Filter::XML::Node;
use POE::Filter::XML::NS(':JABBER');
use Digest::MD5('md5_hex');
use Carp;

use constant 
{
	'_pcj_config' 		=>	0,
	'_pcj_sock'			=>	1,
	'_pcj_sfwheel'		=>	2,
	'_pcj_wheel'		=>	3,
	'_pcj_id'			=>	4,
	'_pcj_sid'			=>	5,
	'_pcj_jid'			=>	6,
	'_pcj_helper'		=>	7,
	'_pcj_shutdown'		=>	8,
	'_pcj_parent'		=> 	9,
	'_pcj_input'		=>	10,
	'_pcj_error'		=>	11,
	'_pcj_status'		=>	12,
	'_pcj_pending'		=>	13,
	'_pcj_queue'		=>	14,
	'_pcj_init_finished'=>	15,

};

our $VERSION = '2.02';

sub new()
{
	my $class = shift;
	my $self = [];
	$self->[+_pcj_pending] = {};

	bless($self, $class);

	my $me = $class . '->new()';
	Carp::confess "$me requires an even number of arguments" if(@_ & 1);
	
	$self->_gather_options(\@_);
	
	my $args = $self->[+_pcj_config];

	$self->[+_pcj_helper] =
		POE::Component::Jabber::ProtocolFactory::get_guts
		(
			$args->{'connectiontype'}
		);

	$args->{'version'}	||= $self->[+_pcj_helper]->get_version();
	$args->{'xmlns'}	||= $self->[+_pcj_helper]->get_xmlns();
	$args->{'alias'}	||= $class;
	$args->{'stream'}	||= +XMLNS_STREAM;
	$args->{'debug'}	||= 0 ;
	$args->{'resource'}	||= md5_hex(time().rand().$$.rand().$^T.rand());
	

	if(!defined($args->{'stateparent'}))
	{
	
		my $session = $poe_kernel->get_active_session();

		if($session != $poe_kernel)
		{
			$self->[+_pcj_parent] = $session->ID();
		
		} else {

			Carp::confess "$me requires either an active session or the name" .
			' of a session that has the provided events.';
		}
	
	} else {

		$self->[+_pcj_parent] = $args->{'stateparent'};
	}

	Carp::confess "$me requires ConnectionType to be defined" if not defined
		$args->{'connectiontype'};
	Carp::confess "$me requires Username to be defined" if not defined
		$args->{'username'};
	Carp::confess "$me requires Password to be defined" if not defined
		$args->{'password'};
	Carp::confess "$me requires Hostname to be defined" if not defined
		$args->{'hostname'};
	Carp::confess "$me requires IP to be defined" if not defined
		$args->{'ip'};
	Carp::confess "$me requires Port to be defined" if not defined
		$args->{'port'};
	
	Carp::confess "$me requires InputEvent to be defined" if not defined
		$args->{'states'}->{'inputevent'};
		$self->[+_pcj_input] = $args->{'states'}->{'inputevent'};
	Carp::confess "$me requires ErrorEvent to be defined" if not defined
		$args->{'states'}->{'errorevent'};
		$self->[+_pcj_error] = $args->{'states'}->{'errorevent'};
	Carp::confess "$me requires StatusEvent to be defined" if not defined
		$args->{'states'}->{'statusevent'};
		$self->[+_pcj_status] = $args->{'states'}->{'statusevent'};
	
	$self->[+_pcj_helper] = 
		POE::Component::Jabber::ProtocolFactory::get_guts
		(
			$args->{'connectiontype'}
		);

	POE::Session->create
	(
		'object_states' =>
		[
			$self => 
			[
				'_start',
				'initiate_stream',
				'connect',
				'_connect',
				'connected',
				'disconnected',
				'shutdown',
				'output_handler',
				'debug_output_handler',
				'input_handler',
				'debug_input_handler',
				'return_to_sender',
				'connect_error',
				'server_error',
				'flushed',
				'_stop',
				'purge_queue',
				'debug_purge_queue',
			],

			$self =>
			{
				'reconnect' => 'connect'
			},

			$self->[+_pcj_helper] => $self->[+_pcj_helper]->get_states(),

		],
		
		'options' => 
		{
			'trace' => $args->{'debug'}, 
			'debug' => $args->{'debug'},
		},

		'heap' => $self,
	);


	return $self;

}

sub wheel()
{
	if(@_ > 1)
	{
		my ($self, $arg) = @_;
		$self->[+_pcj_wheel] = $arg;

	} else {

		return shift(@_)->[+_pcj_wheel];
	}
}

sub sock()
{
	if(@_ > 1)
	{
		my ($self, $arg) = @_;
		$self->[+_pcj_sock] = $arg;

	} else {

		return shift(@_)->[+_pcj_sock];
	}
}

sub config()
{
	if(@_ > 1)
	{
		my ($self, $arg) = @_;
		$self->[+_pcj_config] = $arg;

	} else {

		return shift(@_)->[+_pcj_config];
	}
}

sub sid()
{
	if(@_ > 1)
	{
		my ($self, $arg) = @_;
		$self->[+_pcj_sid] = $arg;
	
	} else {

		return shift(@_)->[+_pcj_sid];
	}
}

sub jid()
{
	if(@_ > 1)
	{
		my ($self, $arg) = @_;
		$self->[+_pcj_jid] = $arg;
	
	} else {

		return shift(@_)->[+_pcj_jid];
	}
}

sub parent()
{
	if(@_ > 1)
	{
		my ($self, $arg) = @_;
		$self->[+_pcj_parent] = $arg;
	
	} else {

		return shift(@_)->[+_pcj_parent];
	}
}

sub input()
{
	if(@_ > 1)
	{
		my ($self, $arg) = @_;
		$self->[+_pcj_input] = $arg;

	} else {

		return shift(@_)->[+_pcj_input];
	}
}

sub error()
{
	if(@_ > 1)
	{
		my ($self, $arg) = @_;
		$self->[+_pcj_error] = $arg;
	
	} else {

		return shift(@_)->[+_pcj_error];
	}
}

sub status()
{
	if(@_ > 1)
	{
		my ($self, $arg) = @_;
		$self->[+_pcj_status] = $arg;

	} else {

		return shift(@_)->[+_pcj_status];
	}
}

sub pending()
{
	if(@_ > 1)
	{
		my ($self, $arg) = @_;
		$self->[+_pcj_pending] = $arg;
	
	} else {

		return shift(@_)->[+_pcj_pending];
	}
}

sub queue()
{
	if(@_ > 1)
	{
		my ($self, $arg) = @_;
		$self->[+_pcj_queue] = $arg;

	} else {

		return shift(@_)->[+_pcj_queue];
	}
}

sub _gather_options()
{
	my ($self, $args) = @_;
	
	my $opts = {};

	while(@$args != 0)
	{
		my $key = lc(shift(@{$args}));
		my $value = shift(@{$args});
		
		if(ref($value) eq 'HASH')
		{
			my $hash = {};
			foreach my $sub_key (keys %$value)
			{
				$hash->{lc($sub_key)} = $value->{$sub_key};
			}
			$opts->{$key} = $hash;
			next;
		}
		$opts->{$key} = $value;
	}

	$self->[+_pcj_config] = $opts;

	return $self;
}

sub connect_error()
{
	my ($kernel, $self, $call, $code, $err) = @_[KERNEL, OBJECT, ARG0..ARG2];

	$self->debug_message("Connect Error: $call: $code -> $err\n");

	$kernel->post($self->[+_pcj_parent],
		$self->[+_pcj_error],
		+PCJ_CONNECTFAIL, 
		$call, $code, $err);
	
	return;
}

sub _start()
{	
	my ($kernel, $self) = @_[KERNEL, OBJECT];
	$kernel->alias_set($self->[+_pcj_config]->{'alias'});
	$self->_reset();

	if($self->[+_pcj_config]->{'debug'})
	{
		$kernel->state('output_handler', $self, 'debug_output_handler');
		$kernel->state('purge_queue', $self, 'debug_purge_queue');
	}

	$self->[+_pcj_queue] = [];

	return;
}

sub _stop()
{
	my ($kernel, $self) = @_[KERNEL, OBJECT];
	$kernel->alias_remove($_) for $kernel->alias_list();
	return;
}


sub _reset()
{
	my $self = shift;
	
	$self->[+_pcj_sid] = 0;
	$self->[+_pcj_pending] = {};
	$self->[+_pcj_init_finished] = 0;
	$self->[+_pcj_id] ||= Digest::SHA1->new();
	$self->[+_pcj_id]->add(time().rand().$$.rand().$^T.rand());
	$self->[+_pcj_wheel] = undef;
	$self->[+_pcj_sfwheel] = undef;
	$self->[+_pcj_sock]->close() if defined($self->[+_pcj_sock]);
	$self->[+_pcj_sock] = undef;
	return;
}

sub connect()
{
	my ($kernel, $self, $ip, $port) = @_[KERNEL, OBJECT, ARG0, ARG1];
	
	$self->[+_pcj_config]->{'ip'} = $ip if defined $ip;
	$self->[+_pcj_config]->{'port'} = $port if defined $port;

	$self->_reset();
	$kernel->yield('_connect');

	$kernel->post($self->[+_pcj_parent], $self->[+_pcj_status], +PCJ_CONNECT);
	return;
}

sub _connect()
{
	my ($kernel, $self) = @_[KERNEL, OBJECT];
	
	$self->[+_pcj_sfwheel] = POE::Wheel::SocketFactory->new
		(
			'RemoteAddress'	=>	$self->[+_pcj_config]->{'ip'},
			'RemotePort'	=>	$self->[+_pcj_config]->{'port'},
			'SuccessEvent'  =>  'connected',
			'FailureEvent'  =>  'connect_error',
		);
	
	
	$kernel->post(
		$self->[+_pcj_parent], 
		$self->[+_pcj_status], 
		+PCJ_CONNECTING);
	
	return;

}

sub _default()
{
	my ($event) = $_[ARG0];

	$_[OBJECT]->debug_message($event . ' was not caught');
}

sub return_to_sender()
{
	my ($kernel, $self, $session, $sender, $event, $node) = 
		@_[KERNEL, OBJECT, SESSION, SENDER, ARG0, ARG1];
	
	my $attrs = $node->get_attrs();
	my $pid;
	
	if(exists($attrs->{'id'}))
	{
		if(exists($self->[+_pcj_pending]->{$attrs->{'id'}}))
		{
			$self->debug_message('OVERRIDING USER DEFINED ID!');
			
			$pid = $self->[+_pcj_id]->add(
				$self->[+_pcj_id]->clone()->hexdigest())
					->clone()->hexdigest();

			$node->attr('id', $pid);
		}

		$pid = $attrs->{'id'};
		
	} else {
		
		$pid = $self->[+_pcj_id]->add(
			$self->[+_pcj_id]->clone()->hexdigest())
				->clone()->hexdigest();
		
		$node->attr('id', $pid);
	}
	
	my $state = $session == $sender ? 1 : undef;
	$self->[+_pcj_pending]->{$pid} = [];
	$self->[+_pcj_pending]->{$pid}->[0] = $sender->ID();
	$self->[+_pcj_pending]->{$pid}->[1] = $event;
	
	$kernel->yield('output_handler', $node, $state);

	$kernel->post($self->[+_pcj_parent], $self->[+_pcj_status], +PCJ_RTS_START);
	
	return;

}

sub connected()
{
	my ($kernel, $self, $sock) = @_[KERNEL, OBJECT, ARG0];

	$self->[+_pcj_sock] = $sock;
	$self->[+_pcj_sfwheel] = undef;

	my $input = $self->[+_pcj_helper]->get_input_event() || 
		Carp::confess('No input event defined in helper!');
	my $error = $self->[+_pcj_helper]->get_error_event();
	my $flushed = $self->[+_pcj_helper]->get_flushed_event();
	
	$kernel->state('input_handler', $self->[+_pcj_helper], $input);
	$kernel->state('server_error', $self->[+_pcj_helper], $error) if $error;
	$kernel->state('flushed', $self->[+_pcj_helper], $flushed) if $flushed;

	$self->[+_pcj_wheel] = POE::Wheel::ReadWrite->new
	(
		'Handle'		=> $self->[+_pcj_sock],
		'Filter'		=> POE::Filter::XML->new(),
		'InputEvent'	=> 'input_handler',
		'ErrorEvent'	=> 'server_error',
		'FlushedEvent'	=> 'flushed',
	);

	$kernel->yield('initiate_stream');

	$kernel->post($self->[+_pcj_parent], $self->[+_pcj_status], +PCJ_CONNECTED);
	
	return;
}

sub relinquish_states()
{
	my $self = shift;
	
	if($self->[+_pcj_config]->{'debug'})
	{
		$poe_kernel->state('input_handler', $self, 'debug_input_handler');

	} else {
		
		$poe_kernel->state('input_handler', $self, 'input_handler');
	}
	
	$poe_kernel->state('server_error', $self, 'server_error');
	$poe_kernel->state('flushed', $self, 'flushed');

	$self->[+_pcj_init_finished] = 1;
	return;
}

sub initiate_stream()
{
	my ($kernel, $self, $sender, $session) = 
		@_[KERNEL, OBJECT, SENDER, SESSION];

	my $element = XNode->new
	(
		'stream:stream',
		[
			'to', $self->[+_pcj_config]->{'hostname'}, 
			'xmlns', $self->[+_pcj_config]->{'xmlns'}, 
			'xmlns:stream', $self->[+_pcj_config]->{'stream'}, 
			'version', $self->[+_pcj_config]->{'version'}
		]
	)->stream_start(1);
	
	my $state = $session == $sender ? 1 : undef;
	$kernel->yield('output_handler', $element, $state);

	$kernel->post(
		$self->[+_pcj_parent], 
		$self->[+_pcj_status], 
		+PCJ_STREAMSTART);
	
	return;
}

sub disconnected()
{	
	my ($kernel, $self) = @_[KERNEL, OBJECT];
	$kernel->post(
		$self->[+_pcj_parent], 
		$self->[+_pcj_error], 
		+PCJ_SOCKETDISCONNECT);
	return;
}

sub flushed()
{
	my ($kernel, $self, $session) = @_[KERNEL, OBJECT, SESSION];

	if($self->[+_pcj_shutdown])
	{
		$kernel->call($session, 'disconnected');
		$kernel->post(
			$self->[+_pcj_parent], 
			$self->[+_pcj_status], 
			+PCJ_SHUTDOWN_FINISH);
	}
	
	return;
}
	

sub shutdown()
{
	my ($kernel, $self) = @_[KERNEL, OBJECT];

	my $node = XNode->new('stream:stream')->stream_end(1);
	
	$self->[+_pcj_shutdown] = 1;

	$self->[+_pcj_wheel]->put($node);

	$kernel->post($self->[+_pcj_parent], $self->[+_pcj_status], +PCJ_STREAMEND);
	$kernel->post(
		$self->[+_pcj_parent], 
		$self->[+_pcj_status], 
		+PCJ_SHUTDOWN_START);
	
	
	return;
}

sub debug_purge_queue()
{
	my ($kernel, $self, $sender, $session) = 
		@_[KERNEL, OBJECT, SENDER, SESSION];
	
	my $items = [];

	while(my $item = shift(@{$self->[+_pcj_queue]}))
	{
		push(@$items, $item);
	}
	
	$self->debug_message( 'Items pulled from queue: ' . scalar(@$items));
	
	my $state = $sender == $session ? 1 : undef;

	foreach(@$items)
	{	
		$kernel->yield('output_handler', $_, $state);
	}

	return;
}

sub purge_queue()
{
	my ($kernel, $self, $sender, $session) =
		@_[KERNEL, OBJECT, SENDER, SESSION];
	
	my $items = [];

	while(my $item = shift(@{$self->[+_pcj_queue]}))
	{
		push(@$items, $item);
	}
	
	my $state = $sender == $session ? 1 : undef;

	foreach(@$items)
	{
		$kernel->yield('output_handler', $_, $state);
	}
	
	return;
}


sub debug_output_handler()
{
	my ($kernel, $self, $node, $state) = @_[KERNEL, OBJECT, ARG0, ARG1];

	if(defined($self->[+_pcj_wheel]))
	{
		if($self->[+_pcj_init_finished] || $state)
		{	
			$self->debug_message('Sent: ' . $node->to_str());
			$self->[+_pcj_wheel]->put($node);
			$kernel->post(
				$self->[+_pcj_parent],
				$self->[+_pcj_status],
				+PCJ_NODESENT);

		} else {
			
			$self->debug_message('Still initialising.');
			$self->debug_message('Queued: ' . $node->to_str());
			push(@{$self->[+_pcj_queue]}, $node);
			$self->debug_message(
				'Queued COUNT: ' . scalar(@{$self->[+_pcj_queue]}));
			$kernel->post(
				$self->[+_pcj_parent],
				$self->[+_pcj_status],
				+PCJ_NODEQUEUED);
		}

	} else {
		
		$self->debug_message('There is no wheel present.');
		$self->debug_message('Queued: ' . $node->to_str());
		$self->debug_message(
			'Queued COUNT: ' . scalar(@{$self->[+_pcj_queue]}));
		push(@{$self->[+_pcj_queue]}, $node);
		$kernel->post(
			$self->[+_pcj_parent],
			$self->[+_pcj_error],
			+PCJ_SOCKETDISCONNECT);
		$kernel->post(
			$self->[+_pcj_parent],
			$self->[+_pcj_status],
			+PCJ_NODEQUEUED);
	}
	
	return;
}

sub output_handler()
{
	my ($kernel, $self, $node, $state) = @_[KERNEL, OBJECT, ARG0, ARG1];

	if(defined($self->[+_pcj_wheel]))
	{
		if($self->[+_pcj_init_finished] || $state)
		{
			$self->[+_pcj_wheel]->put($node);
			$kernel->post(
				$self->[+_pcj_parent], 
				$self->[+_pcj_status], 
				+PCJ_NODESENT);
		
		} else {

			push(@{$self->[+_pcj_queue]}, $node);
			$kernel->post(
				$self->[+_pcj_parent],
				$self->[+_pcj_status],
				+PCJ_NODEQUEUED);
		}

	} else {

		push(@{$self->[+_pcj_queue]}, $node);
		$kernel->post(
			$self->[+_pcj_parent],
			$self->[+_pcj_error],
			+PCJ_SOCKETDISCONNECT);
		$kernel->post(
			$self->[+_pcj_parent],
			$self->[+_pcj_status],
			+PCJ_NODEQUEUED);
	}
	return;
}

sub input_handler()
{
	my ($kernel, $self, $node) = @_[KERNEL, OBJECT, ARG0];

	$kernel->post(
		$self->[+_pcj_parent], 
		$self->[+_pcj_status], 
		+PCJ_NODERECEIVED);
	
	my $attrs = $node->get_attrs();		
	
	if(exists($attrs->{'id'}))
	{
		if(defined($self->[+_pcj_pending]->{$attrs->{'id'}}))
		{
			my $array = delete $self->[+_pcj_pending]->{$attrs->{'id'}};
			$kernel->post($array->[0], $array->[1], $node);
			$kernel->post(
				$self->[+_pcj_parent], 
				$self->[+_pcj_status], 
				+PCJ_RTS_FINISH);
			return;
		}
	}

	$kernel->post($self->[+_pcj_parent], $self->[+_pcj_input], $node);
	return;
}

sub debug_input_handler()
{
	my ($kernel, $self, $node) = @_[KERNEL, OBJECT, ARG0];

	$kernel->post(
		$self->[+_pcj_parent], 
		$self->[+_pcj_status], 
		+PCJ_NODERECEIVED);
	
	$self->debug_message("Recd: ".$node->to_str());

	my $attrs = $node->get_attrs();

	if(exists($attrs->{'id'}))
	{
		if(defined($self->[+_pcj_pending]->{$attrs->{'id'}}))
		{
			my $array = delete $self->[+_pcj_pending]->{$attrs->{'id'}};
			$kernel->post($array->[0], $array->[1], $node);
			$kernel->post(
				$self->[+_pcj_parent], 
				$self->[+_pcj_status], 
				+PCJ_RTS_FINISH);
			return;
		}
	}
	
	$kernel->post($self->[+_pcj_parent], $self->[+_pcj_input], $node);
	return;
}

sub server_error()
{
	my ($kernel, $self, $call, $code, $err) = @_[KERNEL, OBJECT, ARG0..ARG2];
	
	$self->debug_message("Server Error: $call: $code -> $err\n");
	
	$self->[+_pcj_wheel] = undef;

	$kernel->post($self->[+_pcj_parent],
		$self->[+_pcj_error], 
		+PCJ_SOCKETFAIL, 
		$call, $code, $err);
	return;
}

sub debug_message()
{	
	my $self = shift;
	warn "\n", scalar (localtime (time)), ': ' . shift(@_) ."\n";

	return;
}

1;

__END__

=pod

=head1 NAME

POE::Component::Jabber - A POE Component for communicating over Jabber

=head1 SYNOPSIS

 use POE;
 use POE::Component::Jabber;
 use POE::Component::Jabber::Error;
 use POE::Component::Jabber::Status;
 use POE::Component::Jabber::ProtocolFactory;
 use POE::Filter::XML::Node;
 use POE::Filter::XML::NS qw/ :JABBER :IQ /;

 POE::Component::Jabber->new(
   IP => 'jabber.server',
   PORT => '5222',
   HOSTNAME => 'jabber.server',
   USERNAME => 'username',
   PASSWORD => 'password',
   ALIAS => 'PCJ',
   STATES => {
	 StatusEvent => 'StatusHandler',
	 InputEvent => 'InputHandler',
	 ErrorEvent => 'ErrorHandler',
   }
 );
 
 $poe_kernel->post('PCJ', 'connect', $node);
 $poe_kernel->post('PCJ', 'output_handler', $node);
 $poe_kernel->post('PCJ', 'return_to_sender', $node);

=head1 DESCRIPTION

PCJ is a communications component that fits within the POE framework and
provides the raw low level footwork of initiating a connection, negotiatating
various protocol layers, and authentication necessary for the end developer
to focus more on the business end of implementing a client or service.

=head1 METHODS

=over 4

=item new()

Accepts many named, required arguments which are listed below. new() will
return a reference to the newly created reference to a PCJ object and should
be stored. There are many useful methods that can be called on the object to
gather various bits of information such as your negotiated JID.

=over 2

=item IP

The IP address in dotted quad, or the FQDN for the server.

=item PORT

The remote port of the server to connect.

=item HOSTNAME

The hostname of the server. Used in addressing.

=item USERNAME

The username to be used in authentication (OPTIONAL for jabberd14 service
connections).

=item PASSWORD

The password to be used in authentication.

=item RESOURCE

The resource that will be used for binding and session establishment 
(OPTIONAL: resources aren't necessary for initialization of service oriented
connections, and if not provided for client connections will be automagically 
generated).

=item ALIAS

The alias the component should register for use within POE. Defaults to
the class name.

=item CONNECTIONTYPE

This is the type of connection you wish to esablish. Please use the constants
provided in PCJ::ProtocolFactory for the basis of this argument. There is no
default.

=item VERSION

If for whatever reason you want to override the protocol version gathered from
your ConnectionType, this is the place to do it. Please understand that this 
value SHOULD NOT be altered, but it is documented here just in case.

=item XMLNS

If for whatever reason you want to override the protocol's default XML
namespace that is gathered from your ConnectionType, use this variable. Please
understand that this value SHOULD NOT be altered, but is documented here just
in case.

=item STREAM

If for whatever reason you want to override the xmlns:stream attribute in the
<stream:stream/> this is the argument to use. This SHOULD NOT ever need to be
altered, but it is available and documented just in case.

=item STATEPARENT

The alias or session id of the session you want the component to contact. This
is optional provided to instantiate PCJ within another POE::Session. In that
case, that session will be assumed to be the recepient of events.

=item STATES

=over 2

=item StatusEvent

The StatusEvent will receive an event for every status change within PCJ. This
is useful for providing feedback to the end user on what exactly the client or
service is doing. Please see POE::Component::Jabber::Status for exported 
constants and what they signify.

=item InputEvent

The InputEvent will receive an event for every jabber packet that comes through
the connection once fully initialized. ARG0 will be a reference to a 
POE::Filter::XML::Node. Please see POE::Filter::XML::Node documentation for
ways to get the information out of the Nodes and construct Nodes of your own.

=item ErrorEvent

The error event will be fired upon a number of detected error conditions within
PCJ. Please see the POE::Component::Jabber::Error documentation for possible 
error states.

=back

=item DEBUG

If bool true, will enable debugging and tracing within the component. All XML
sent or received through the component will be printed to STDERR

=back

=item wheel() [Protected]

wheel() returns the currently stored POE::Wheel reference. If provided an
argument, that argument will replace the current POE::Wheel stored.

=item sock() [Protected]

sock() returns the current socket being used for communication. If provided an
argument, that argument will replace the current socket stored.

=item sid() [Protected]

sid() returns the session ID that was given by the server upon the initial
connection. If provided an argument, that argument will replace the current 
session id stored.

=item config() [Protected]

config() returns the configuration structure (HASH reference) of PCJ that is 
used internally. It contains values that are either defaults or were 
calculated based on arguments provided in the constructor. If provided an 
argument, that argument will replace the current configuration.

=item parent() [Public]

parent() returns either the session ID from the intantiating session, or the
alias or ID provided in the constructor. If provided an argument, that argument
will replace the current parent seesion ID or alias stored.

=item input() [Public]

input() returns the current event used by PCJ to deliver input events. If
provided an argument, that argument will replace the current input event used.

=item status() [Public]

status() returns the current event used by PCJ to deliver status events. If
provided an argument, that argument will replace the current status event used.

=item error() [Public]

error() returns the current event used by PCJ to deliver error events. If 
provided an argument, that argument will replace the current error event used.

=item pending() [Protected]

pending() returns a hash reference to the currently pending return_to_sender
transactions keyed by the 'id' attribute of the XML node. If provided an
argument, that argument will replace the pending queue.

=item queue() [Protected]

queue() returns an array reference containing the Nodes sent when there was 
no suitable initialized connection available. Index zero is the first Node
placed into the queue with index one being the second, and so on. See under
the EVENTS section, 'purge_queue' for more information.

=item _reset() [Private]

_reset() returns PCJ back to its initial state and returns nothing;

=item _gather_options() [Private]

_gather_options() takes an array reference of the arguments provided to new()
(ie. \@_) and populates its internal configuration with the values (the same 
configuration returned by config()).

=item relinquish_states() [Protected]

relinquish_states() is used by Protocol subclasses to return control of the
events back to the core of PCJ. It is typically called when the event 
PCJ_INIT_FINISH is fired to the status event handler.

=head1 EVENTS

=over 4

=item 'output_handler'

This is the event that you use to push data over the wire. It accepts only one
argument, a reference to a POE::Filter::XML::Node.

=item 'return_to_sender'

This event takes (1) a POE::Filter::XML::Node and gives it a unique id, and 
(2) a return event and places it in the state machine. Upon receipt of 
response to the request, the return event is fired with the response packet.
Note: the return event is post()ed in the context of the provided or default
parant session.

=item 'shutdown'

The shutdown event terminates the XML stream which in turn will trigger the
end of the socket's life.

=item 'connect' and 'reconnect'

This event can take (1) the ip address of a new server and (2) the port. This
event may also be called without any arguments and it will force the component
to [re]connect.

=item 'purge_queue'

If Nodes are sent to the output_handler when there isn't a fully initialized
connection, the Nodes are placed into a queue. PCJ will not automatically purge
this queue when a suitable connection DOES become available because there is no
way to tell if the packets are still valid or not. It is up to the end 
developer to decide this and fire this event. Packets will be setn in the order
in which they were received.

=back

=head1 NOTES AND BUGS

This is a connection broker. This should not be considered a first class
client or service. This broker basically implements whatever core
functionality is required to get the end developer to the point of writing
upper level functionality quickly. 

In the case of XMPP what is implemented:
XMPP Core.
A small portion of XMPP IM (session binding).

Legacy:
Basic authentication via iq:auth. (No presence management, no roster 
management)

JABBERD14:
Basic handshake. (No automatic addressing management of the 'from' attribute)

JABBERD20:
XMPP Core like semantics.
Domain binding. (No route packet enveloping or presence management)

With the major version increase, significant changes have occured in how PCJ
handles itself and how it is constructed. PCJ no longer connects when it is
instantiated. The 'connect' event must be post()ed for PCJ to connect.

For example implementations using all four current aspects, please see the 
examples/ directory in the distribution.

=head1 AUTHOR

Copyright (c) 2003-2007 Nicholas Perez. Distributed under the GPL.

=cut