File Coverage

blib/lib/Protocol/XMPP/Base.pm
Criterion Covered Total %
statement 24 74 32.4
branch 0 20 0.0
condition n/a
subroutine 8 20 40.0
pod 8 10 80.0
total 40 124 32.2


line stmt bran cond sub pod time code
1             package Protocol::XMPP::Base;
2              
3 31     31   288280 use strict;
  31         66  
  31         1074  
4 31     31   163 use warnings;
  31         64  
  31         1616  
5 31     31   148 use parent qw(Mixin::Event::Dispatch);
  31         64  
  31         212  
6              
7             our $VERSION = '0.007'; ## VERSION
8              
9 31     31   190440 use constant EVENT_DISPATCH_ON_FALLBACK => 0;
  31         69  
  31         2216  
10              
11 31     31   20655 use Future;
  31         520931  
  31         4552  
12 31     31   1406 use Scalar::Util ();
  31         62  
  31         2855  
13              
14             # For debug messages
15 31     31   159 use Time::HiRes qw{time};
  31         62  
  31         203  
16 31     31   21532 use POSIX qw{strftime};
  31         246947  
  31         204  
17              
18             =head1 NAME
19              
20             Protocol::XMPP::Base - base class for L
21              
22             =head1 SYNOPSIS
23              
24             =head1 DESCRIPTION
25              
26             =head1 METHODS
27              
28             =cut
29              
30             =head2 new
31              
32             Constructor. Stores all parameters on $self, including the top level stack item as L.
33              
34             =cut
35              
36             sub new {
37 0     0 1   my $class = shift;
38             return bless {
39 0     0     future_factory => sub { Future->new },
40             @_
41 0           }, $class;
42             }
43              
44             sub new_future {
45 0     0 0   shift->{future_factory}->()
46             }
47              
48             =head2 debug
49              
50             Helper method for displaying a debug message. Only displayed if the debug flag was passed to L.
51              
52             =cut
53              
54             sub debug {
55 0     0 1   my $self = shift;
56 0 0         return $self unless $self->{debug};
57              
58 0 0         if(!ref $self->{debug}) {
59 0           my $now = Time::HiRes::time;
60 0           warn strftime("%Y-%m-%d %H:%M:%S", gmtime($now)) . sprintf(".%03d", int($now * 1000.0) % 1000.0) . " @_\n";
61 0           return $self;
62             }
63 0 0         if(ref $self->{debug} eq 'CODE') {
64 0           $self->{debug}->(@_);
65 0           return $self;
66             }
67 0           die "Unknown debug setting " . $self->{debug};
68             }
69              
70             =head2 _ref_to_xml
71              
72             Convert an arrayref to an XML fragment.
73              
74             Input such as the following:
75              
76             [ 'iq', type => 'set', id => 'xyz', _content => [ [ 'session', _ns => 'xmpp-session' ] ] ]
77              
78             would be converted to:
79              
80             'set' id=>'xyz'>
81              
82             =cut
83              
84             sub _ref_to_xml {
85 0     0     my $self = shift;
86 0           my $ref = shift;
87 0           my ($element, %attr) = @$ref;
88 0 0         $attr{xmlns} = 'urn:ietf:params:xml:ns:' . delete($attr{_ns}) if $attr{_ns};
89 0           my $unterm = delete $attr{_unterminated};
90 0           my $content = delete $attr{_content};
91 0           my $str = '<' . join(' ', $element, map { $_ . "='" . $attr{$_} . "'" } sort keys %attr);
  0            
92 0 0         if(ref $content) {
    0          
93 0           $str .= '>';
94 0           $str .= $self->_ref_to_xml($_) foreach @$content;
95 0 0         $str .= "" unless $unterm;
96             } elsif(defined $content) {
97 0           $str .= '>';
98 0           $str .= $content;
99 0 0         $str .= "" unless $unterm;
100             } else {
101 0 0         $str .= $unterm ? '>' : '/>';
102             }
103 0           return $str;
104             }
105              
106             =head1 PROXY METHODS
107              
108             The following methods are proxied to the L class via L.
109              
110             =cut
111              
112             =head2 is_loggedin
113              
114             Accessor for the loggedin state - will call the appropriate on_(login|logout) event when
115             changing state.
116              
117             =cut
118              
119             sub is_loggedin {
120 0     0 1   my $self = shift;
121 0           return $self->stream->is_loggedin(@_);
122             }
123              
124             sub is_authorised {
125 0     0 0   my $self = shift;
126 0           return $self->stream->is_authorised(@_);
127             }
128              
129             =head2 write_xml
130              
131             Write XML reference to stream.
132              
133             =cut
134              
135             sub write_xml {
136 0     0 1   my $self = shift;
137 0           return $self->stream->write_xml(@_);
138             }
139              
140             =head2 write_text
141              
142             Write XML reference to stream.
143              
144             =cut
145              
146             sub write_text {
147 0     0 1   my $self = shift;
148 0           return $self->stream->write_text(@_);
149             }
150              
151             =head2 dispatch_event
152              
153             Pass through an event (on_XXX handler).
154              
155             =cut
156              
157             sub dispatch_event {
158 0     0 1   my $self = shift;
159 0           return $self->stream->dispatch_event(@_);
160             }
161              
162              
163             =head2 stream
164              
165             Returns the active L object.
166              
167             =cut
168              
169             sub stream {
170 0     0 1   my $self = shift;
171 0 0         if(@_) {
172 0           my $stream = shift;
173 0           Scalar::Util::weaken $stream;
174 0           $self->{stream} = $stream;
175 0           return $self;
176             }
177 0           return $self->{stream};
178             }
179              
180             =head2 next_id
181              
182             Returns the next ID for to use in outgoing messages.
183              
184             =cut
185              
186             sub next_id {
187 0     0 1   my $self = shift;
188 0           return $self->stream->next_id(@_);
189             }
190              
191             1;
192              
193             __END__