This file is indexed.

/usr/share/perl5/Device/Gsm/Sms.pm is in libdevice-gsm-perl 1.60-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
# Device::Gsm::Sms - SMS message simple class that represents a text SMS message
# Copyright (C) 2002-2009 Cosimo Streppone, cosimo@cpan.org
# Copyright (C) 2006-2011 Grzegorz Wozniak, wozniakg@gmail.com
#
# This program is free software; you can redistribute it and/or modify
# it only under the terms of Perl itself.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# Perl licensing terms for details.
#
# Commercial support is available. Write me if you are
# interested in new features or software support.
#
# $Id$

package Device::Gsm::Sms;

use strict;
use integer;

use constant SMS_DELIVER => 0x00;
use constant SMS_SUBMIT  => 0x01;
use constant SMS_STATUS  => 0x02;

use Device::Gsm::Pdu;
use Device::Gsm::Sms::Structure;
use Device::Gsm::Sms::Token;

sub _log { print @_, "\n"; }
sub _parent { $_[0]->{_parent} }

#
# new(
#     header  => '+CMGL: .....',
#     pdu     => '[encoded pdu string]',
# )
#
# creates message object
#
sub new {
    my ($proto, %opt) = @_;
    my $class = ref $proto || $proto;

    # Create new message object
    my $self = {};

    # Store gsm parent object reference
    if (exists $opt{'parent'}) {
        $self->{'_parent'} = $opt{'parent'};

        # Assume default storage for sms message
        $opt{'storage'} ||= $self->{'_parent'}->storage();
    }

    # Store options into main object
    $self->{'options'} = \%opt;

    # Hash to contain token objects after decoding (must be accessible by name)
    $self->{'tokens'} = {};

    return undef unless (exists $opt{'header'} && exists $opt{'pdu'});

    #_log("NEW SMS OBJECT");
    #_log("Header [$opt{header}]");
    #_log("PDU    [$opt{pdu}]");

    # Check for valid msg header (thanks to Pierre Hilson for his patch
    # to make this regex work also for Alcatel gsm software)
    if ($opt{'header'} =~ /\+CMGL:\s*(\d+),\s*(\d+),\s*(\w*),\s*(\d+)/o) {

        $self->{'index'} = $1;    # Position of message in SIM card
        $self->{'status'}
            = $2;    # Status of message (REC READ/UNREAD, STO, ...);
        $self->{'alpha'}  = $3;    # Alphanumeric representation of sender
        $self->{'length'} = $4;    # Final length of message
        $self->{'pdu'}     = $opt{'pdu'};        # PDU content
        $self->{'storage'} = $opt{'storage'};    # Storage (SM or ME)

        bless $self, $class;

        if ($self->decode(Device::Gsm::Sms::SMS_DELIVER)) {

            #			_log('OK, message decoded correctly!');
        }
        elsif ($self->decode(Device::Gsm::Sms::SMS_STATUS)) {

        }
        else {

            #			_log('CASINO!');
            undef $self;
        }

    }
    else {

        # Warning: could not parse message header
        undef $self;

    }

    return $self;
}

#
# time(): returns message time in ascii format
#
sub time {
    my $self = shift;
    if (my $t = $self->token('SCTS')) {
        return $t->toString();
    }
    return '';
}

#
# time_dt (): returns status message discharge time in ascii format
#
sub time_dt {
    my $self = shift;
    if (my $t = $self->token('DT')) {
        return $t->toString();
    }
    return '';
}

#
# message_ref(): returns message reference of status message
#
sub message_ref {
    my $self = shift;
    if (my $t = $self->token('MR')) {
        return $t->toString();
    }
    return '';
}

#
# type(): returns message type in ascii readable format
#
{

    # List of allowed status strings
    my @status
        = ('UNKNOWN', 'REC UNREAD', 'REC READ', 'SENT UNREAD', 'SENT READ');

    sub status () {
        my $self = shift;
        return $status[ defined $self->{'status'} ? $self->{'status'} : 0 ];
    }

}

#
# decode( CMGL_header, pdu_string )
#
# creates a new Device::Gsm::Sms object from
# PDU encoded message string returned by +CMGL commands
#
# If some error occurs, returns undef.
#
#
sub _old_decode {
    my ($header, $pdu) = @_;
    my %msg    = ();
    my $errors = 0;

    # Copy original header/pdu strings
    $msg{'_HEADER'} = $header;
    $msg{'_PDU'}    = $pdu;

    #
    # Decode header string
    #
    if ($header =~ /\+CMGL:\s*(\d+),(\d+),(\d*),(\d+)/) {
        $msg{'index'}  = $1;
        $msg{'type'}   = $2;
        $msg{'xxx'}    = $3;    # XXX
        $msg{'length'} = $4;
    }

    #
    # Decode all parts of PDU message
    #

    # ----------------------------------- SCA (service center address)
    my $sca_length = hex(substr $pdu, 0, 2);
    if ($sca_length == 0) {

        # No SCA provided, take default
        $msg{'SCA'} = undef;
    }
    else {

        # Parse SCA address
        #print STDERR "SCA length = ", $sca_length, "; ";
        #print STDERR "Parsing address ", substr( $pdu, 0, ($sca_length+1) << 1 );
        $msg{'SCA'} = Device::Gsm::Pdu::decode_address(
            substr($pdu, 0, ($sca_length + 1) << 1));

        #print STDERR ' = `', $msg{'SCA'}, "'\n";
    }

    # ----------------------------------- PDU type
    $pdu = substr $pdu => (($sca_length + 1) << 1);
    $msg{'PDU_TYPE'} = substr $pdu, 0, 2;
    undef $sca_length;

    # ----------------------------------- OA (originating address)
    $pdu = substr $pdu => 2;
    my $oa_length = hex(substr $pdu, 0, 2);

    $msg{'OA'} = Device::Gsm::Pdu::decode_address(
        substr($pdu, 0, ($oa_length + 1) << 1));
    undef $oa_length;

    # PID      (protocol identifier)
    # DCS      (data coding scheme)
    # SCTS     (service center time stamp)
    # UDL + UD (user data)
    @msg{qw/PID DCS SCTS UDL UD/} = unpack 'A2 A2 A14 A2 A*', $pdu;

    #map { $msg{$_} = hex $msg{$_} } qw/PID DCS UDL/;
    #
    # Decode USER DATA in 7/8 bit encoding
    #
    if ($msg{'DCS'} eq '00') {    # DCS_7BIT
        Device::Gsm::Pdu::decode_text7($msg{'UD'});
    }
    elsif ($msg{'DCS'} eq 'F6') {    # DCS_8BIT
        Device::Gsm::Pdu::decode_text8($msg{'UD'});
    }

    # XXX DEBUG
    #foreach( sort keys %msg ) {
    #	print STDERR 'MSG[', $_, '] = `'.$msg{$_}.'\'', "\n";
    #}

    bless \%msg, 'Device::Gsm::Sms';
}

sub decode {
    my ($self, $type) = @_;
    $self->{'type'} = $type;

    # Get list of tokens for this message (from ::Sms::Structure)
    my $cPdu = $self->{'pdu'};

    # Check that PDU is not empty
    return 0 unless $cPdu;

    # Backup copy for "backtracking"
    my $cPduCopy = $cPdu;

    my @token_names = $self->structure();
    my $decoded     = 1;

    #is udh in pdu?
    my $udh_parsed = 0;
    while (@token_names) {

        # Create new token object
        my $token = new Sms::Token(shift @token_names,
            { messageTokens => $self->{'tokens'} });
        if (!defined $token) {
            $decoded = 0;
            last;
        }

        # If decoding is completed successfully, add token object to message
        #_log('PDU BEFORE ['.$cPdu.']', length($cPdu) );

        if ($token->decode(\$cPdu)) {

            # Store token object into SMS message
            $self->{'tokens'}->{ $token->name() } = $token;

            # Catch message type indicator (MTI) and re-load structure
            # We must also skip message types 0x02 and 0x03 because we don't handle them currently
            if ($token->name() eq 'PDUTYPE') {

                my $mti  = $token->MTI();
                my $udhi = $token->UDHI();

                #               # If MTI has bit 1 on, this could be a SMS-STATUS message (0x02), or (0x03???)
                #               if( $mti >= SMS_STATUS ) {
                #                   _log('skipping unhandled message type ['.$mti.']');
                #                   return undef;
                #               }

                if ($mti != $type) {

                    #_log('token PDUTYPE, data='.$token->data().' MTI='.$token->get('MTI').' ->MTI()='.$token->MTI());
                    #
                    # This is a SMS-SUBMIT message, so:
                    #
                    # 1) change type
                    # 2) restore original PDU message
                    # 3) reload token structure
                    # 4) restart decoding
                    #
                    $self->type($type = $mti);

                    $cPdu        = $cPduCopy;
                    @token_names = $self->structure();

                    #_log('RESTARTING DECODING AFTER MTI DETECTION'); #<STDIN>;
                    redo;
                }

                if ($udh_parsed == 0 and $udhi == 1) {
                    $cPdu        = $cPduCopy;
                    @token_names = $self->structure();
                    $udh_parsed  = 1;
                    redo;
                }

                #_log('       ', $token->name(), ' DATA = ', $token->toString() );

            }

        }

        #_log('PDU AFTER  ['.$cPdu.']', length($cPdu) );

    }

    #_log("\n", 'PRESS ENTER TO CONTINUE'); <STDIN>;

    return $decoded;

}

#
# Delete an sms message
#
sub delete {
    my $self = $_[0];
    my $gsm  = $self->_parent();
    my $ok;

    # Try to delete message
    my $msg_index = $self->index();
    my $storage   = $self->storage();

    # Issue delete command
    if (ref $gsm && $storage && $msg_index >= 0) {
        $ok = $gsm->delete_sms($msg_index, $storage);
        $gsm->log->write('info',
                  'Delete sms n.'
                . $msg_index
                . ' in storage '
                . $storage . ' => '
                . ($ok ? 'OK' : '*ERROR'));
    }
    else {
        $gsm->log->write('warn',
                  'Could not delete sms n.'
                . $msg_index
                . ' in storage '
                . $storage
                . '. Internal error.');
        $ok = undef;
    }

    return $ok;
}

#
# Returns message own index number (position)
#
sub index {
    my $self = $_[0];
    return $self->{'index'};
}

#
# Returns message storage (SM - SIM card or ME - phone memory)
#
sub storage {
    my $self = $_[0];
    return $self->{'storage'};
}

#
# Only valid for SMS_SUBMIT and SMS_STATUS messages
#
sub recipient {
    my $self = shift;
    if ($self->type() == SMS_SUBMIT or $self->type() == SMS_STATUS) {
        my $t = $self->token('DA');
        return $t->toString() if $t;
    }
}

#
#Only valid for SMS_STATUS messages returns status code(in hex) extracted from status message
#Codes are explained in ST.pm
#
sub delivery_status {
    my $self = shift;
    if ($self->type() == SMS_STATUS) {
        my $t = $self->token('ST');
        return $t->toString() if $t;
    }
}

#
# Only valid for SMS_DELIVER messages (?)
#
sub sender {
    my $self = shift;
    if ($self->type() == SMS_DELIVER) {
        my $t = $self->token('OA');
        return $t->toString() if $t;
    }
}

# Alias for text()
sub content {
    return $_[0]->text();
}

sub text {
    my $self = shift;
    my $t    = $self->token('UD');
    return $t->toString() if $t;
}

#
#only valid for SMS_DELIVER messages, retuns presence of UDH
#
sub is_udh {
    my $self = shift;
    if ($self->type() == SMS_DELIVER) {
        return $self->{'tokens'}->{'PDUTYPE'}->{'_UDHI'};
    }
}

#
#only valid for SMS_DELIVER messages with UDH, returns if sms is csms
#
sub is_csms {
    my $self = shift;
    if ($self->is_udh()) {
        return $self->{'tokens'}->{'UDH'}->{'_IS_CSMS'};
    }
}

#
#only valid for SMS_DELIVER messages with UDH, retuns CSM reference number
#
sub csms_ref_num {
    my $self = shift;
    if ($self->is_csms()) {
        return $self->{'tokens'}->{'UDH'}->{'_REF_NUM'};
    }
}

#
#only valid for SMS_DELIVER messages with UDH, retuns CSM reference number
#
sub csms_ref_hex {
    my $self = shift;
    if ($self->is_csms()) {
        return $self->{'tokens'}->{'UDH'}->{'_REF_HEX'};
    }
}

#
#only valid for SMS_DELIVER messages with UDH, retuns CSM parts count
#
sub csms_parts {
    my $self = shift;
    if ($self->is_csms()) {
        return $self->{'tokens'}->{'UDH'}->{'_PARTS'};
    }
}

#
#only valid for SMS_DELIVER messages with UDH, retuns CSM current part number
#
sub csms_part_num {
    my $self = shift;
    if ($self->is_csms()) {
        return $self->{'tokens'}->{'UDH'}->{'_PART_NUM'};
    }
}

sub token ($) {
    my ($self, $token_name) = @_;
    return undef unless $token_name;

    if (exists $self->{'tokens'}->{$token_name}) {
        return $self->{'tokens'}->{$token_name};
    }
    else {
        warn('undefined token ' . $token_name . ' for this sms');
        return undef;
    }
}

#
# Returns type of sms (SMS_DELIVER || SMS_SUBMIT)
#
sub type {
    my $self = shift;
    if (@_) {
        $self->{'type'} = shift;
    }
    $self->{'type'};
}

=pod

=head1 NAME

Device::Gsm::Sms - SMS message internal class that represents a single text SMS message

=head1 SYNOPSIS

    # A list of Device::Gsm::Sms messages is returned by
    # Device::Gsm messages() method.

    use Device::Gsm;
    ...
    @sms = $gsm->messages();

    if( @sms ) {
        foreach( @sms ) {
            print $msg->storage()   , "\n";
            print $msg->recipient() , "\n";
            print $msg->sender()    , "\n";
            print $msg->content()   , "\n";
            print $msg->time()      , "\n";
            print $msg->type()      , "\n";
        }
    }

    # Or you can instance a sms message from raw PDU data
    my $msg = new Device::Gsm::Sms(
        header => '+CMGL: ...',
        pdu    => `[encoded pdu data]',
        storage=> 'ME', # or 'SC'
    );

    if( defined $msg ) {
        print $msg->recipient() , "\n";
        print $msg->sender()    , "\n";
        print $msg->content()   , "\n";  # or $msg->text()
        print $msg->time()      , "\n";
        print $msg->type()      , "\n";
    }

	$msg->delete();

=head1 DESCRIPTION

C<Device::Gsm::Sms> class implements very basic SMS message object,
that can be used to decode C<+CMGL> GSM command response to build a more
friendly high-level object.

=head1 METHODS

The following is a list of methods applicable to C<Device::Gsm::Sms> objects.

=head2 content()

See text() method.

=head2 decode()

Starts the decoding process of pdu binary data. If decoding process 
ends in success, return value is true and sms object is filled with
all proper values.

If decoding process has errors or pdu data is not provided, return
value is 0 (zero).


=head2 delete()

Delete the current SMS message from sim card.
Example:

    $gsm = Device::Gsm->new();
    ...
    my @msg = $gsm->messages();
    $msg[0] && $msg[0]->delete();

=head2 new()

Basic constructor. You can build a new C<Device::Gsm::Sms> object from the
raw B<+CMGL> header and B<PDU> data. Those data is then decoded and a new
sms object is instanced and all information filled, to be available
for subsequent method calls.

The allowed parameters to new() method are:

=over 4

=item header

This is the raw B<+CMGL> header string as modem outputs when you
issue a B<+CMGL> command

=item pdu

Binary encoded sms data

=item storage

Tells which storage to delete the message from. Check the documentation of your
phone to know valid storage values. Default values are:

=over 4

=item C<ME>

Deletes messages from gsm phone memory.

=item C<SC>

Deletes messages from sim card.

=back

=back

=head2 index()

Returns the sms message index number, that is the position of message in the
internal device memory or sim card.
This number is used for example to delete the message.

    my $gsm = Device::Gsm->new(port=>'/dev/ttyS0');
    ...
    my @messages = $gsm->messages();
    ...
    # Delete the first returned message
    my $msg = shift @messages;
    $gsm->delete_sms( $msg->index() );

=head2 recipient()

Returns the sms recipient number (destination address = DA)
as string (ex.: C<+39012345678>).

=head2 sender()

Returns the sms sender number (originating address = OA) as string.

=head2 status()

Status of the message can be one value from the following list:

=for html
<FORM><SELECT><OPTION>UNKNOWN<OPTION>REC UNREAD<OPTION>REC READ<OPTION>SENT UNREAD<OPTION>SENT READ</SELECT></FORM>

=for pod
'UNKNOWN', 'REC UNREAD', 'REC READ', 'SENT UNREAD', 'SENT READ'

=head2 storage()

Returns the storage where SMS has been read from.

=head2 text()

Returns the textual content of sms message.

=head2 token()

Returns the given PDU token of the decoded message (internal usage).

=head2 type()

SMS messages can be of two types: SMS_SUBMIT and SMS_DELIVER, that are defined by
two constants with those names. type() method returns one of these two values.

Example:

	if( $sms->type() == Device::Gsm::Sms::SMS_DELIVER ) {
		# ...
	}
	elsif( $sms->type() == Device::Gsm::Sms::SMS_SUBMIT ) {
		# ...
	}

=head1 REQUIRES

=over 4

=item *

Device::Gsm

=back

=head1 EXPORTS

None

=head1 TODO

=over 4

=item *

Complete and proof-read documentation and examples

=back

=head1 COPYRIGHT

Device::Gsm::Sms - SMS message simple class that represents a text SMS message

Copyright (C) 2002-2009 Cosimo Streppone, cosimo@cpan.org

This program is free software; you can redistribute it and/or modify
it only under the terms of Perl itself.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Perl licensing terms for details.

=head1 AUTHOR

Cosimo Streppone, cosimo@cpan.org

=head1 SEE ALSO

L<Device::Gsm>, perl(1)

=cut