9bd5328e28369110f10fab6b4db2aab282a3c489
[OpenSRF.git] / src / perl / lib / OpenSRF / Transport / SlimJabber / XMPPMessage.pm
1 package OpenSRF::Transport::SlimJabber::XMPPMessage;
2 use strict; use warnings;
3 use OpenSRF::Utils::Logger qw/$logger/;
4 use OpenSRF::EX qw/:try/;
5 use strict; use warnings;
6 use XML::LibXML;
7
8 use constant JABBER_MESSAGE =>
9     "<message to='%s' from='%s' router_command='%s' router_class='%s' osrf_xid='%s'>".
10     "<thread>%s</thread><body>%s</body></message>";
11
12 sub new {
13     my $class = shift;
14     my %args = @_;
15     my $self = bless({}, $class);
16
17     if($args{xml}) {
18         $self->parse_xml($args{xml});
19
20     } else {
21         $self->{to} = $args{to} || '';
22         $self->{from} = $args{from} || '';
23         $self->{thread} = $args{thread} || '';
24         $self->{body} = $args{body} || '';
25         $self->{osrf_xid} = $args{osrf_xid} || '';
26         $self->{router_command} = $args{router_command} || '';
27         $self->{router_class} = $args{router_class} || '';
28     }
29
30     return $self;
31 }
32
33 sub to {
34     my($self, $to) = @_;
35     $self->{to} = $to if defined $to;
36     return $self->{to};
37 }
38 sub from {
39     my($self, $from) = @_;
40     $self->{from} = $from if defined $from;
41     return $self->{from};
42 }
43 sub thread {
44     my($self, $thread) = @_;
45     $self->{thread} = $thread if defined $thread;
46     return $self->{thread};
47 }
48 sub body {
49     my($self, $body) = @_;
50     $self->{body} = $body if defined $body;
51     return $self->{body};
52 }
53 sub status {
54     my($self, $status) = @_;
55     $self->{status} = $status if defined $status;
56     return $self->{status};
57 }
58 sub type {
59     my($self, $type) = @_;
60     $self->{type} = $type if defined $type;
61     return $self->{type};
62 }
63 sub err_type {
64     my($self, $err_type) = @_;
65     $self->{err_type} = $err_type if defined $err_type;
66     return $self->{err_type};
67 }
68 sub err_code {
69     my($self, $err_code) = @_;
70     $self->{err_code} = $err_code if defined $err_code;
71     return $self->{err_code};
72 }
73 sub osrf_xid {
74     my($self, $osrf_xid) = @_;
75     $self->{osrf_xid} = $osrf_xid if defined $osrf_xid;
76     return $self->{osrf_xid};
77 }
78 sub router_command {
79     my($self, $router_command) = @_;
80     $self->{router_command} = $router_command if defined $router_command;
81     return $self->{router_command};
82 }
83 sub router_class {
84     my($self, $router_class) = @_;
85     $self->{router_class} = $router_class if defined $router_class;
86     return $self->{router_class};
87 }
88
89
90 sub to_xml {
91     my $self = shift;
92
93     my $body = $self->{body};
94     $body =~ s/&/&amp;/sog;
95     $body =~ s/</&lt;/sog;
96     $body =~ s/>/&gt;/sog;
97
98     return sprintf(
99         JABBER_MESSAGE,
100         $self->{to},
101         $self->{from},
102         $self->{router_command},
103         $self->{router_class},
104         $self->{osrf_xid},
105         $self->{thread},
106         $body
107     );
108 }
109
110 sub parse_xml {
111     my($self, $xml) = @_;
112     my($doc, $err);
113
114     try {
115         $doc = XML::LibXML->new->parse_string($xml);
116     } catch Error with {
117         my $err = shift;
118         $logger->error("Error parsing message xml: $xml --- $err");
119     };
120     throw $err if $err;
121
122     my $root = $doc->documentElement;
123
124     $self->{body} = $root->findnodes('/message/body').'';
125     $self->{thread} = $root->findnodes('/message/thread').'';
126     $self->{from} = $root->getAttribute('router_from');
127     $self->{from} = $root->getAttribute('from') unless $self->{from};
128     $self->{to} = $root->getAttribute('to');
129     $self->{type} = $root->getAttribute('type');
130     $self->{osrf_xid} = $root->getAttribute('osrf_xid');
131 }
132
133
134 1;