Message/SMS.pm

filter: Statementstmt 29.9% 29 / 97 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 47.3% 9 / 19 Podpod 100% 10 / 10 totaltotal 28.9% 48 / 166 CC 19 SCAR 23.3
CC
19
Cov
22%
coding
20.0
mclass
20.0
udh
20.0
CRAP 10.3
« Util/SMS.pm ↑ Summary Message.pm »
1 #===============================================================================
2 #
3 # FILE: SMS.pm
4 #
5 # DESCRIPTION: SMS message representation
6 #
7 # NOTES: ---
8 # AUTHOR: Michael Bochkaryov (Rattler), <misha@rattler.kiev.ua>
9 # COMPANY: Net.Style
10 # CREATED: 14.08.2008 15:43:06 EEST
11 #===============================================================================
12  
13 =head1 NAME
14  
15 NetSDS::Message::SMS - short message according to ETSI GSM 03.40
16  
17 =head1 SYNOPSIS
18  
19 use NetSDS::Message::SMS;
20  
21 ...
22  
23 $msg = NetSDS::Message::SMS->new();
24 $msg->udh(conv_hex_str('050102030405');
25 $msg->ud('Hello there');
26  
27 $msg->coding(COD_7BIT);
28  
29 print "SM: " . $msg->message_body();
30  
31 =head1 DESCRIPTION
32  
33 This class provides API to SMS message data structure.
34  
35 =cut
36  
37 package NetSDS::Message::SMS;
38  
39 2 ▶use 5.8.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
42  
43 2 ▶use base qw(NetSDS::Message Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
44  
45 2 ▶use version; our $VERSION = '0.021';
Subroutine BEGIN: called CC=1 SCAR=0.0
46  
47 2 ▶use NetSDS::Util::Convert; # Data conversion routines
Subroutine BEGIN: called CC=1 SCAR=0.0
48 2 ▶use NetSDS::Util::String; # String processing routines
Subroutine BEGIN: called CC=1 SCAR=0.0
49 2 ▶use NetSDS::Util::SMS; # SMS related data processing
Subroutine BEGIN: called CC=1 SCAR=0.0
50 2 ▶use NetSDS::Const::Message; # Messaging related constants
Subroutine BEGIN: called CC=1 SCAR=0.0
51  
52 2 our @EXPORT = qw(
53 create_long_sm
54 );
55  
56 #===============================================================================
57  
58 =head1 CLASS API
59  
60 =over
61  
62 =item B<new()> - class constructor
63  
64 Implements SMS constructor.
65  
66 my $object = NetSDS::Message::SMS->new();
67  
68 =cut
69  
70 #-----------------------------------------------------------------------
71 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
Pod documented
72  
73 0 my ( $class, %params ) = @_;
74  
75 0 my $this = $class->SUPER::new(
76 media => 'sms', # Bearer for messages
77 headers => {
78 udhi => 0, # UDH Indicator
79 mclass => undef, # No message class by default
80 coding => COD_7BIT, # Default GSM charset (ETSI GSM 03.38)
81 mwi => undef, # Message Waiting Indicator (see DCS description)
82 priority => 0, # Non-priority messages
83 },
84 body => {
85 udh => undef, # User Data Headers
86 ud => undef, # User Data
87 },
88 format => 'sms', # Generic SMS data
89 %params,
90 );
91  
92 # Set SMS coding
93 0 ▶ if ( defined $params{coding} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params{"coding"}) 0 0 0
94 0 $this->coding( $params{coding} );
95 }
96  
97 0 return $this;
98  
99 } ## end sub new
100  
101 #***********************************************************************
102  
103 =item B<coding()> - set/get SMS coding
104  
105 Paramters: new coding to set
106  
107 Returns: message coding
108  
109 $msg->coding(COD_UCS2);
110  
111 =cut
112  
113 #-----------------------------------------------------------------------
114  
115 0 ▶sub coding {
Subroutine coding: not called CC=4 SCAR=30.0
Pod documented
116  
117 0 my ( $this, $coding ) = @_;
118  
119 0 ▶ if ( defined $coding ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $coding) 0 0 0
120  
121 # Check if coding is correct
122 0 $coding += 0;
123 0 ▶ if ( ( $coding < 0 ) or ( $coding > 2 ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($coding < 0 or $coding > 2) 0 0 0
Condition0%0/3
$coding < 0 or $coding > 2
l!l&&r!l&&!r
000
MC/DC0%0/2
$coding < 0 or $coding > 2
$coding < 0 $coding > 2
Truth table0%0/3
A or B
A= $coding < 0B= $coding > 2
ABresult
1X1
011
000
124 0 return $this->error("Cant set unknown data coding for SMS");
125 }
126 0 $this->header( 'coding', $coding );
127 }
128  
129 0 return $this->header('coding');
130  
131 }
132  
133 #***********************************************************************
134  
135 =item B<mclass()> - set/get message class
136  
137 Paramters: new message class value
138  
139 $msg->mclass(0); # Send as Flash SMS
140  
141 =cut
142  
143 #-----------------------------------------------------------------------
144  
145 0 ▶sub mclass {
Subroutine mclass: not called CC=4 SCAR=30.0
Pod documented
146  
147 0 my ( $this, $mclass ) = @_;
148  
149 0 ▶ if ( defined $mclass ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $mclass) 0 0 0
150  
151 # Check if message class is correct
152 0 $mclass += 0;
153 0 ▶ if ( ( $mclass < 0 ) or ( $mclass > 3 ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mclass < 0 or $mclass > 3) 0 0 0
Condition0%0/3
$mclass < 0 or $mclass > 3
l!l&&r!l&&!r
000
MC/DC0%0/2
$mclass < 0 or $mclass > 3
$mclass < 0 $mclass > 3
Truth table0%0/3
A or B
A= $mclass < 0B= $mclass > 3
ABresult
1X1
011
000
154 0 return $this->error("Cant set unknown message class for SMS");
155 }
156 0 $this->header( 'mclass', $mclass );
157 }
158  
159 0 return $this->header('mclass');
160  
161 }
162  
163 #***********************************************************************
164  
165 =item B<udh()> - set/get UDH
166  
167 Paramters: UDH as binary string
168  
169 Returns: UDH
170  
171 $msg->udh(conv_hex_str('050.02130405');
172  
173 =cut
174  
175 #-----------------------------------------------------------------------
176  
177 0 ▶sub udh {
Subroutine udh: not called CC=4 SCAR=30.0
Pod documented
178  
179 0 my ( $this, $udh ) = @_;
180  
181 0 ▶ if ($udh) {
Branch: 1 branch
BranchTrueFalseTotal
if ($udh) 0 0 0
182  
183 0 $this->header( 'udhi', 1 );
184  
185 # Retrieve UDH length in bytes (1st
186 0 my ($udhl) = unpack( "C*", bytes::substr( $udh, 0, 1 ) );
187  
188 # Check if UDH isn't more than maximum SMS size
189 0 ▶ if ( $udhl > 139 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($udhl > 139) 0 0 0
190 0 return $this->error("Cant set UDH more than 139 bytes");
191 }
192  
193 # Check if UDHL is correct
194 0 ▶ if ( ( $udhl + 1 ) != bytes::length($udh) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($udhl + 1 != &bytes::length($udh)) 0 0 0
195 0 return $this->error("Incorrect UDHL in UDH");
196 }
197  
198 0 $this->{body}->{udh} = str_decode($udh);
199 }
200  
201 0 return $this->{body}->{udh};
202  
203 } ## end sub udh
204  
205 #***********************************************************************
206  
207 =item B<ud()> - set/get user data
208  
209 Paramters: user data as binary string
210  
211 Returns: user data
212  
213 =cut
214  
215 #-----------------------------------------------------------------------
216  
217 0 ▶sub ud {
Subroutine ud: not called CC=2 SCAR=17.9
Pod documented
218  
219 0 my ( $this, $ud ) = @_;
220  
221 0 ▶ if ( defined $ud ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ud) 0 0 0
222 0 $this->{body}->{ud} = str_decode($ud);
223 }
224  
225 0 return $this->{body}->{ud};
226  
227 }
228  
229 #***********************************************************************
230  
231 =item B<esm_class()> - get esm_class from message
232  
233 See 5.2.12 chapter of SMPP 3.4 specification for details.
234  
235 $esm_class = $msg->esm_class();
236  
237 =cut
238  
239 #-----------------------------------------------------------------------
240  
241 0 ▶sub esm_class {
Subroutine esm_class: not called CC=3 SCAR=24.8
Pod documented
242  
243 0 my ($this) = @_;
244  
245 0 my $esm_class = 0b00000000;
246  
247 # Set UDHI to 1 if UDH exists
248 0 ▶ if ( $this->udh() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->udh) 0 0 0
249 0 $esm_class += 0b01000000; # Set UDHI indicator
250 }
251  
252 # Set DLR indicator if 'dlr' header presents
253 0 ▶ if ( $this->header('dlr') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->header("dlr")) 0 0 0
254 0 $esm_class += 0b00001000; # Set DLR indicator
255 }
256  
257 0 return $esm_class;
258 }
259  
260 #***********************************************************************
261  
262 =item B<dcs()> - get data coding scheme
263  
264 Returns data coding schmeme in accordance with ETSI GSM 03.38
265  
266 $dcs = $msg->dcs();
267  
268 =cut
269  
270 #-----------------------------------------------------------------------
271  
272 0 ▶sub dcs {
Subroutine dcs: not called CC=2 SCAR=17.9
Pod documented
273  
274 0 my ($this) = @_;
275  
276 0 my $dcs = 0b00000000;
277  
278 # If have message class, bit 4 of DCS must be 1
279 # Message class value set in 1 and 0 bits
280 0 my $mclass = $this->header('mclass');
281 0 ▶ if ( defined $mclass ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $mclass) 0 0 0
282 0 $dcs += 0b00010000; # Has message class
283 0 $dcs += $mclass + 0; # Message class value
284 }
285  
286 # Add message coding (bits 2 and 3)
287 0 $dcs += ( ( $this->header('coding') << 2 ) & 0b00001100 );
288  
289 0 return $dcs;
290 }
291  
292 #***********************************************************************
293  
294 =item B<message_body()> - return SMS message body
295  
296 Returns: SMS body as byte string (UDH + UD)
297  
298 $msg_hex = conv_str_hex($msg->message_body);
299  
300 =cut
301  
302 #-----------------------------------------------------------------------
303  
304 0 ▶sub message_body {
Subroutine message_body: not called CC=2 SCAR=17.9
Pod documented
305  
306 0 my ($this) = @_;
307  
308 0 ▶ return $this->udh ? $this->udh . $this->ud : $this->ud;
Branch: 1 branch
BranchTrueFalseTotal
$this->udh ? : 0 0 0
309 }
310  
311 #***********************************************************************
312  
313 =item B<text($string, $coding)> - set SM data from text string
314  
315 Paramters: string, SMS coding
316  
317 # Set SMS text
318 $msg->text('Just some string', COD_7BIT);
319  
320 This will set UDH to undef and UD to string in GSM 03.38.
321  
322 =cut
323  
324 #-----------------------------------------------------------------------
325  
326 0 ▶sub text {
Subroutine text: not called CC=3 SCAR=24.8
Pod documented
327  
328 0 my ( $self, $str, $coding ) = @_;
329 0 $str = str_decode($str);
330  
331 0 $self->coding($coding);
332 0 $self->{body}->{udh} = undef; # only short text SMS
333  
334 # Convert UTF-8 string to proper encoding
335 0 ▶ if ( $coding == COD_7BIT ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($coding == '0') { } 0 0 0
elsif ($coding == '2') { } 0 0 0
336 0 $self->ud( str_recode( $str, 'UTF-8', 'GSM0338' ) );
337 } elsif ( $coding == COD_UCS2 ) {
338 0 $self->ud( str_recode( $str, 'UTF-8', 'UCS-2BE' ) );
339 } else {
340 0 return $self->error('Unknown encoding for text SM');
341 }
342  
343 0 return $self->ud();
344  
345 } ## end sub text
346  
347  
348 #***********************************************************************
349  
350 =back
351  
352 =head1 EXPORTED FUNCTIONS
353  
354 =over
355  
356 =item B<create_long_sm($text, $coding)> - concatenated SMS sequence
357  
358 Paramters: text string (UTF-8), SMS coding
359  
360 Returns: array of NetSDS::Message::SMS objects
361  
362 # Create 300 character string
363 my $long_str = 'abc'x100;
364  
365 my @parts = create_long_sm($long_str, COD_7BIT);
366  
367 =cut
368  
369 #-----------------------------------------------------------------------
370  
371 0 ▶sub create_long_sm {
Subroutine create_long_sm: not called CC=2 SCAR=17.9
Pod documented
372  
373 0 my ( $str, $coding ) = @_;
374  
375 # Parse string
376 0 my @parts = NetSDS::Util::SMS::split_text( $str, $coding );
377  
378 # Create array of SMS objects
379 0 my @res = ();
380 0 foreach my $part (@parts) {
381 0 my $msg = NetSDS::Message::SMS->new(
382 coding => $coding,
383 );
384 0 $msg->udh( $part->{udh} );
385 0 $msg->ud( $part->{ud} );
386 0 push @res, $msg;
387 }
388 0 return @res;
389 }
390  
391 2 1;
392  
393 __END__
« Util/SMS.pm ↑ Summary Message.pm »