blib/lib/XML/CompileX/Transport/SOAPXMPP.pm untested

filter: Statementstmt 0.0% 0 / 78 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 124 CC 11 SCAR 41.3
CC
11
Cov
0%
_prepare_call
110.0
init
6.0
consume_iq_reply
2.0
CRAP 62.4
1 { package XML::CompileX::Transport::SOAPXMPP;
2 use strict;
3 use warnings;
4 use Encode;
5 use base qw(XML::Compile::Transport);
6 our $VERSION = '1.0';
7  
8 sub init {
9 my ($self, $args) = @_;
10 $self->SUPER::init($args);
11  
12 # defaults
13 $self->wait_iq_reply(1);
14  
15 for (qw(connection force_stanza_types wait_iq_reply)) {
16 $self->{$_} = $args->{$_} if exists $args->{$_};
17 }
18  
19 $self;
20 }
21  
22 # the code returned here is executed receiving a text and is
23 # expected to return text
24 sub _prepare_call {
25 my ($self, $args) = @_;
26 my $hook = $args->{hook};
27 my $kind = $args->{kind};
28 $hook ||= sub {
29 my ($messageref, $trace) = @_;
30 my $message = $$messageref;
31 # this is the standard code, it is overriden by a given
32 # hook that might be sent to this method.
33  
34 die 'No XMPP connection while trying to send message'
35 unless $trace->{connection};
36  
37 if ($trace->{stanza_type} eq 'message') {
38 $trace->{message_id} = $trace->{connection}->send_message
39 ($trace->{address}, 'normal',
40 sub {
41 my $writer = shift;
42 $message =~ s/\<\?[^\?]+\?\>//;
43 $writer->raw(encode('utf8',$message));
44 });
45 $self->last_sent_message_id($trace->{message_id});
46  
47 return '';
48  
49 } else {
50 $trace->{eventwatcher} = AnyEvent->condvar
51 if $trace->{wait_iq_reply};
52  
53 $trace->{iq_id} = $trace->{connection}->send_iq
54 ('set',
55 sub {
56 my $writer = shift;
57 $message =~ s/\<\?[^\?]+\?\>//;
58 $writer->raw(encode('utf8',$message));
59 },
60 sub {
61 my $node = shift;
62  
63 if ($node) {
64 my $id = $node->attr('id');
65 $self->iq_replies({}) unless $self->iq_replies;
66 $self->iq_replies->{$id} = $node;
67 }
68  
69 $trace->{eventwatcher}->broadcast()
70 if $trace->{eventwatcher};
71  
72 },
73 to => $trace->{address});
74 $self->last_sent_iq_id($trace->{iq_id});
75  
76 if ($trace->{eventwatcher}) {
77 $trace->{eventwatcher}->wait;
78  
79 $trace->{node} = $self->consume_iq_reply($trace->{iq_id});
80 return '' unless $trace->{node};
81 my $content = join '',
82 $trace->{node}->text,
83 map { $_->as_string } $trace->{node}->nodes;
84  
85 return \$content;
86  
87 } else {
88 return '';
89 }
90 }
91 };
92 sub {
93 my ($messageref, $trace) = @_;
94 my $transport_manager = $self;
95  
96 $trace->{kind} = $kind;
97 $trace->{stanza_type} = $self->force_stanza_types;
98 $trace->{stanza_type} ||= $kind eq 'one-way' ? 'message' : 'iq';
99 $trace->{wait_iq_reply} = $self->wait_iq_reply;
100 $trace->{address} = $self->address;
101 $trace->{connection} = $self->connection;
102  
103 return $hook->($messageref, $trace);
104 }
105 }
106  
107 sub consume_iq_reply {
108 my ($self, $id) = @_;
109 $self->{iq_replies} ||= {};
110 return delete $self->{iq_replies}{$id};
111 }
112  
113 # I would use Class::Accessor::Fast, but I wouldn't like to mess with
114 # XML::Compile::Transport inheritance.
115 for my $name (qw(connection force_stanza_types wait_iq_reply addrs
116 last_sent_message_id last_sent_iq_id iq_replies)) {
117 no strict 'refs';
118 *{$name} = sub {
119 my $self = shift;
120 $self->{$name} = $_[0] if @_;
121 return $self->{$name};
122 };
123 }
124 }
125  
126 __PACKAGE__
127  
128 __END__