File: Legacy.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 (161 lines) | stat: -rw-r--r-- 3,646 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
package POE::Component::Jabber::Legacy;
use Filter::Template;
const XNode POE::Filter::XML::Node
use warnings;
use strict;

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

use base('POE::Component::Jabber::Protocol');

our $VERSION = '2.02';

sub get_version()
{
	return '0.9';
}

sub get_xmlns()
{
	return +NS_JABBER_CLIENT;
}

sub get_states()
{
	return [ 'set_auth', 'init_input_handler' ];
}

sub get_input_event()
{
	return 'init_input_handler';
}

sub set_auth()
{
	my ($kernel, $heap) = @_[KERNEL, HEAP];
	
	my $config = $heap->config();
	my $node = XNode->new('iq', ['type', +IQ_SET, 'id', 'AUTH']);
	my $query = $node->insert_tag('query', ['xmlns', +NS_JABBER_AUTH]);

	$query->insert_tag('username')->data($config->{'username'});

	if($config->{'plaintext'})
	{
		$query->insert_tag('password')->data($config->{'password'});
	
	} else {

		my $hashed = sha1_hex($heap->sid().$config->{'password'});
		
		$query->insert_tag('digest')->data($hashed);
	}
	
	$query->insert_tag('resource')->data($config->{'resource'});

	$kernel->yield('output_handler', $node, 1);

	$heap->jid($config->{'username'} . '@' . $config->{'hostname'} . '/' .
		$config->{'resource'});
	
	return;
}

sub init_input_handler()
{
	my ($kernel, $heap, $node) = @_[KERNEL, HEAP, ARG0];
	
	my $config = $heap->config();

	if ($config->{'debug'})
	{
		$heap->debug_message( "Recd: ".$node->to_str() );
	}
	
	if($node->name() eq 'stream:stream')
	{
		$heap->sid($node->attr('id'));
		$kernel->yield('set_auth');
		$kernel->post($heap->parent(), $heap->status(), +PCJ_AUTHNEGOTIATE);
	
	} elsif($node->name() eq 'iq') {
	
		if($node->attr('type') eq +IQ_RESULT and $node->attr('id') eq 'AUTH')
		{
			$heap->relinquish_states();
			$kernel->post($heap->parent(), $heap->status(), +PCJ_AUTHSUCCESS);
			$kernel->post($heap->parent(),$heap->status(),+PCJ_INIT_FINISHED);
		
		} elsif($node->attr('type') eq +IQ_ERROR and 
			$node->attr('id') eq 'AUTH') {

			$heap->debug_message('Authentication Failed');
			$kernel->yield('shutdown');
			$kernel->post($heap->parent(), $heap->error(), +PCJ_AUTHFAIL);
		}
	}
}

1;

__END__

=pod

=head1 NAME

POE::Component::Jabber::Legacy

=head1 SYNOPSIS

PCJ::Legacy is a Protocol implementation for the legacy (ie. Pre-XMPP) Jabber
protocol.

=head1 DESCRIPTION

PCJ::Legacy implements the simple iq:auth authentication mechanism defined in
the deprecated XEP at http://www.xmpp.org/extensions/xep-0078.html. This
Protocol class is mainly used for connecting to legacy jabber servers that do
not conform the to XMPP1.0 RFC.

=head1 METHODS

Please see PCJ::Protocol for what methods this class supports.

=head1 EVENTS

Listed below are the exported events that end up in PCJ's main session:

=over 2

=item set_auth

This handles construction and sending of the iq:auth query.

=item init_input_handler

This is our main entry point. This is used by PCJ to deliver all input events 
until we are finished. Also handles responses to authentication.

=head1 NOTES AND BUGS

Ideally, this class wouldn't be necessary, but there is a large unmoving mass 
of entrenched users and administrators that refuse to migrate to XMPP. It
largely doesn't help that debian still ships jabberd 1.4.3 which does NOT 
support XMPP.

Currently, [JX]EP-77 is NOT supported, but it is planned for the next release.
Until then, all authentication failures are treated as fatal and PCJ will be
shutdown.

=head1 AUTHOR

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

=cut