IChat/Conversation.pm untested

filter: Statementstmt 0.0% 0 / 93 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 19 Podpod 0.0% 0 / 11 totaltotal 0.0% 0 / 146 CC 10 SCAR 21.3
CC
10
Cov
0%
receive
30.0
init
6.0
socket
6.0
CRAP 8.4
1 package Net::IChat::Conversation;
2  
3 use strict;
4 use warnings;
5  
6 use EO;
7 use XML::LibXML;
8 use Class::Accessor::Chained;
9 use Net::IChat::Message::Text;
10 use Net::IChat::Message::File;
11 use base qw( EO Class::Accessor::Chained );
12  
13 our $VERSION = '0.01';
14  
15 Net::IChat::Conversation->mk_accessors( qw( parser conversing_with ) );
16  
17 sub init {
18 my $self = shift;
19 if ($self->SUPER::init( @_ )) {
20 $self->parser( XML::LibXML->new );
21 return 1;
22 }
23 return 0;
24 }
25  
26 sub resetparser {
27 my $self = shift;
28 $self->parser( XML::LibXML->new );
29 }
30  
31 sub socket {
32 my $self = shift;
33 if (@_) {
34 $self->{ socket } = shift;
35 $self->startstream( @_ );
36 return $self;
37 }
38 return $self->{ socket };
39 }
40  
41 sub fetch_clients {
42 my $self = shift;
43 my $hash= { map { ($_->address => $_) } @{ Net::IChat->clients } };
44 return $hash;
45 }
46  
47 sub determine_other_end {
48 my $self = shift;
49 $self->{ client_cache } ||= $self->fetch_clients;
50 my $peername = $self->socket->peerhost;
51 my $peerport = $self->socket->peerport;
52 my $hosttext = $peername;
53 my $ent = $self->{ client_cache }->{ $hosttext };
54 if (!defined( $ent )) {
55 $self->{ client_cache } = $self->fetch_clients;
56 }
57 $self->conversing_with( $self->{ client_cache }->{ $hosttext } );
58 return 1;
59 }
60  
61 sub startstream {
62 my $self = shift;
63 my $to = shift;
64 my $sock = $self->socket;
65  
66 $self->determine_other_end;
67  
68 my $stream = sprintf(
69 qq{<stream:stream %s xmlns="jabber:client" xmlns:stream="http://etherx.jabber.org/streams">\r\n},
70 ($to) ? qq{to="$to"} : ''
71 );
72 $sock->print( qq{<?xml version="1.0" encoding="UTF-8" ?>\r\n} );
73 $sock->print( $stream );
74 ## throw away pointless information
75 ( undef, undef ) = ( $sock->getline, $sock->getline );
76 }
77  
78 sub receive {
79 my $self = shift;
80 my $text = '';
81 while( my $line = $self->socket->getline() ) {
82 my $echo = $line;
83 chomp $echo;
84 $text .= $line;
85 my $doc = eval { $self->parser->parse_string( $text ) };
86 if (!$@) {
87 # $self->resetparser;
88  
89 ## this is a really nasty hack
90 if (index($text,'<id') > -1) {
91 $text = '';
92 next;
93 }
94  
95 if ($doc->findvalue( '//message/body' )) {
96 return $self->message->parse( $doc );
97 } elsif ($doc->findvalue( '//iq' )) {
98 return $self->file->parse( $doc );
99 } else {
100 }
101  
102 $text = '';
103 } else {
104 }
105 }
106 }
107  
108 sub send {
109 my $self = shift;
110 my $mesg = shift;
111 if (ref($mesg)) {
112 $self->socket->print( $mesg->serialize );
113 return 1;
114 } else {
115 my $msg = $self->message;
116 $msg->body( $mesg );
117 $self->socket->print( $msg->serialize );
118 }
119 }
120  
121 sub end {
122 my $self = shift;
123 $self->socket->print( "</stream:stream>" );
124 $self->socket->close();
125 }
126  
127 sub message {
128 return Net::IChat::Message::Text->new();
129 }
130  
131 sub file {
132 return Net::IChat::Message::File->new();
133 }
134  
135 1;
136