Message.pm

filter: Statementstmt 48.0% 25 / 52 Branchbran 0.0% 0 / 10 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 50.0% 6 / 12 Podpod 100% 5 / 5 totaltotal 45.5% 36 / 79 CC 6 SCAR 13.9
CC
6
Cov
40%
new
6.0
src_addr_native
6.0
dst_addr_native
6.0
CRAP 4.0
« Message/SMS.pm ↑ Summary Const/Message.pm »
1 #===============================================================================
2 #
3 # FILE: Message.pm
4 #
5 # DESCRIPTION: Common mobile message class
6 #
7 # NOTES: ---
8 # AUTHOR: Michael Bochkaryov (Rattler), <misha@rattler.kiev.ua>
9 # COMPANY: Net.Style
10 # CREATED: 21.08.2009 14:24:18 EEST
11 #===============================================================================
12  
13 =head1 NAME
14  
15 NetSDS::Message - common mobile message (SMS, MMS, IM)
16  
17 =head1 SYNOPSIS
18  
19 use NetSDS::Message;
20  
21 ...
22  
23 $msg = NetSDS::Message->new(
24 src_addr => '1234@mtsgw',
25 dst_addr => '380441234567@mtsgw',
26 body => $content,
27 );
28  
29  
30 =head1 DESCRIPTION
31  
32 C<NetSDS::Message> is a superclass for other modules implementing API
33 to exact structure of some messaging data (i.e. CPA2, SMS, MMS, etc).
34  
35 This module implemented to avoid duplication of code providing common
36 functionality for all message types like managing addresses, headers,
37 preparing reply message and so on.
38  
39 =cut
40  
41 package NetSDS::Message;
42  
43 2 ▶use 5.8.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
44 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
45 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
46  
47 2 ▶use NetSDS::Util::Misc;
Subroutine BEGIN: called CC=1 SCAR=0.0
48  
49 2 ▶use base qw(NetSDS::Class::Abstract);
Subroutine BEGIN: called CC=1 SCAR=0.0
50  
51 2 ▶use version; our $VERSION = '0.021';
Subroutine BEGIN: called CC=1 SCAR=0.0
52  
53 #===============================================================================
54 #
55  
56 =head1 CLASS API
57  
58 =over
59  
60 =item B<new([...])> - class constructor
61  
62 my $object = NetSDS::SomeClass->new(%options);
63  
64 =cut
65  
66 #-----------------------------------------------------------------------
67 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
Pod documented
68  
69 0 my ( $class, %params ) = @_;
70  
71 0 my $this = $class->SUPER::new(
72 message_id => undef, # internal message id
73 src_addr => undef, # source address (addr@system)
74 dst_addr => undef, # destination address (addr@system)
75 subject => undef, # subject if exists
76 media => undef, # messaging media ('sms', 'mms', 'ussd', etc)
77 headers => {}, # optional headers
78 body => {}, # message body (depends on media)
79 external_id => undef, # message id on external system (SMSC, SDP, customer, etc)
80 format => undef, # message format ('cpa2', 'mtssmtp', 'smpp')
81 %params
82 );
83  
84 # Generate message id if absent
85 0 ▶ if ( !$this->{message_id} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($this->{"message_id"}) 0 0 0
86 0 $this->{message_id} = $this->_make_id();
87 }
88  
89 0 return $this;
90  
91 } ## end sub new
92  
93 #***********************************************************************
94  
95 =item B<message_id([$value])> - set/get message id
96  
97 $msg_id = $msg->message_id();
98  
99 =cut
100  
101 #-----------------------------------------------------------------------
102 2 __PACKAGE__->mk_accessors('message_id');
103  
104 #***********************************************************************
105  
106 =item B<src_addr()> - set/get source address
107  
108 $msg->src_addr('380121234567@operatorgw');
109  
110 =cut
111  
112 #-----------------------------------------------------------------------
113  
114 2 __PACKAGE__->mk_accessors('src_addr');
115  
116 #***********************************************************************
117  
118 =item B<src_addr_native()> - get native form of source address
119  
120 # Set address
121 $msg->src_addr('380121234567@operatorgw');
122  
123 # Get native form of address
124 $phone = $msg->src_addr_native(); # return '380121234567'
125  
126 =cut
127  
128 #-----------------------------------------------------------------------
129  
130 0 ▶sub src_addr_native {
Subroutine src_addr_native: not called CC=2 SCAR=17.9
Pod documented
131  
132 0 my ($this) = @_;
133  
134 0 ▶ if ( $this->src_addr =~ /(.*)@.*/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->src_addr =~ /(.*)\@.*/) { } 0 0 0
135 0 return $1;
136 } else {
137 0 return $this->src_addr();
138 }
139 }
140  
141 #***********************************************************************
142  
143 =item B<dst_addr()> - set/get destination address
144  
145 $dst_addr = $msg->dst_addr();
146  
147 =cut
148  
149 #-----------------------------------------------------------------------
150 2 __PACKAGE__->mk_accessors('dst_addr');
151  
152 #***********************************************************************
153  
154 =item B<dst_addr_native()> - get native form of destination address
155  
156 if ($mo_msg->dst_addr_native() eq '1234') {
157 print "Received SMS to 1234";
158 }
159  
160 =cut
161  
162 #-----------------------------------------------------------------------
163  
164 0 ▶sub dst_addr_native {
Subroutine dst_addr_native: not called CC=2 SCAR=17.9
Pod documented
165  
166 0 my ($this) = @_;
167  
168 0 ▶ if ( $this->dst_addr =~ /(.*)@.*/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->dst_addr =~ /(.*)\@.*/) { } 0 0 0
169 0 return $1;
170 } else {
171 0 return $this->dst_addr();
172 }
173 }
174  
175 #***********************************************************************
176  
177 =item B<subject()> - set/get message subject
178  
179 $msg->subject('Hello there');
180  
181 =cut
182  
183 #-----------------------------------------------------------------------
184  
185 2 __PACKAGE__->mk_accessors('subject');
186  
187 #***********************************************************************
188  
189 =item B<media()> - set/get message media
190  
191 Paramters: new media if set or none if get
192  
193 Supported media types: 'sms', 'mms', 'ussd'. In fact media types processing
194 is not responsibility of this module and implemented in other modules.
195  
196 =cut
197  
198 #-----------------------------------------------------------------------
199  
200 2 __PACKAGE__->mk_accessors('media');
201  
202 #***********************************************************************
203  
204 =item B<header($name[, $value])> - set/get header
205  
206 Paramters: header name, new header value
207  
208 Returns: header value
209  
210 Message headers implemented as hash reference and contains supplementary
211 information about message. All header name characters are lowercased
212 and all '-' replaced with '_'.
213  
214 # Set header
215 $msg->header('X-Beer', 'Guinness');
216  
217 # Get this header
218 $beer = $msg->header('x_beer');
219  
220 =cut
221  
222 #-----------------------------------------------------------------------
223  
224 0 ▶sub header {
Subroutine header: not called CC=2 SCAR=17.9
Pod documented
225  
226 0 my ( $this, $name, $value ) = @_;
227  
228 # Normalize name first
229 # All
230 0 $name =~ s/-/_/g;
231 0 $name = lc($name);
232  
233 0 ▶ if (defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 0 0 0
234 0 $this->{headers}->{$name} = $value;
235 }
236  
237 0 return $this->{headers}->{$name};
238 }
239  
240 #***********************************************************************
241  
242 =item B<format()> - get/set message format
243  
244 Paramters: new format name
245  
246 Returns: format name
247  
248 Message format provides is related to transport layer code and describe
249 data structure of message body.
250  
251 Supported formats:
252  
253 B<sms> - generic SMS data for ETSI GSM 03.40 compliant implementations.
254 See L<NetSDS::Message::SMS> for details.
255  
256 B<cpa2> - CPA2 compatible structure.
257 See L<NetSDS::Message::CPA2> for details.
258  
259 =cut
260  
261 #-----------------------------------------------------------------------
262  
263 2 __PACKAGE__->mk_accessors('format');
264  
265 #***********************************************************************
266  
267 =item B<reply()> - make reply message
268  
269 This message allows to make reply to current one.
270 Source and destination message are exchanged, media left the same.
271  
272 =cut
273  
274 #-----------------------------------------------------------------------
275  
276 0 ▶sub reply {
Subroutine reply: not called CC=1 SCAR=6.9
Pod documented
277  
278 0 my ( $this, %params ) = @_;
279  
280 # Prepare message with exchanged src_addr and dst_addr
281 0 my $reply = $this->new(
282 src_addr => $this->dst_addr,
283 dst_addr => $this->src_addr,
284 media => $this->media,
285 format => $this->format,
286 %params,
287 );
288  
289 0 $reply->_make_id();
290  
291 0 return $reply;
292  
293 }
294  
295 #***********************************************************************
296  
297 =back
298  
299 =head1 INTERNAL METHODS
300  
301 =over
302  
303 =item B<_make_id($system_name)> - generate message id
304  
305 This method implements automatic generation of message id using
306 make_uuid() routine. System name is set to 'netsds.generic' if
307 not given in arguments.
308  
309 =cut
310  
311 #-----------------------------------------------------------------------
312  
313 0 ▶sub _make_id {
Subroutine _make_id: not called CC=2 SCAR=17.9
314  
315 0 my ( $this, $system_name ) = @_;
316  
317 # Generate default system name
318 0 ▶ if ( !$system_name ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($system_name) 0 0 0
319 0 $system_name = 'netsds.generic';
320 }
321  
322 0 return make_uuid() . '@' . $system_name;
323  
324 }
325 2 1;
326  
327 __END__
« Message/SMS.pm ↑ Summary Const/Message.pm »