:pserver:anonymous@intra.tektonica.com:/opt/cvs - gsmperl - Fri Dec 21 07:37 CET...
[gsmperl.git] / GSM / SMS / Spool.pm
1 package GSM::SMS::Spool;
2 use Carp;
3 # implement spool functions
4
5 use Exporter;
6 @ISA = ('Exporter');
7 @EXPORT = qw(add_to_spool remove_from_spool create_spoolname read_from_spool);
8 $VERSION = '0.1';
9
10 sub add_to_spool {
11         my ($msisdn, $pdu, $dir) = @_;
12         local (*F);
13         
14         my $filename = create_spoolname($msisdn, $pdu);
15         open F, ">".$dir."/".$filename;
16         print F $pdu;
17         close F;
18 }
19
20
21 sub remove_from_spool {
22         my ($file, $dir) = @_;
23         
24         unlink( $dir."/".$file ) or die $!;
25 }
26
27 sub create_spoolname {
28         my ($msisdn, $pdu) = @_;
29         
30         $msisdn =~ s/^\+//;
31         my $filename = $msisdn . "_" . $$ . time . substr($pdu,-32);
32         return $filename;
33 }
34
35 sub read_from_spool {
36         my      ($dir, $n) = @_;
37         local (*DIR);
38         my ($file, $count, @arr);
39         # return array with $n==0:<all>:$n messages from spooldir
40         $count = 0;
41         opendir(DIR, $dir) or croak "Could not read directory $dir ($!)";
42         while ( defined($file = readdir(DIR)) && ( ($n && $count<$n) || !$n) ) {
43                 next if $file =~ /^\.\.?$/;
44                 $count++;
45                 if ($file =~ /(.+?)_.+/) {
46                         my $msisdn = $1;
47                         # contents of file
48                         local (*F);
49                         open F, $dir . "/" . $file;
50                         undef $/;
51                         my $contents = <F>;
52                         close F;
53                         my $msg = {};
54                         $msg->{'msisdn'} = $msisdn;
55                         $msg->{'pdu'} = $contents;
56                         $msg->{'file'} = $file;
57                         push(@arr, $msg);
58                 }
59         }
60         closedir(DIR);
61         return @arr;
62 }
63
64 1;
65
66 =head1 NAME
67
68 GSM::SMS::Spool
69
70 =head1 DESCRIPTION
71
72 Implements a simple filesystem spool mechanism to temporarily store incoming and outgoing SMS messages.
73
74 =head1 METHODS
75
76 =head2 add_to_spool( $msisdn, $pdu, $dir )
77
78 Add a message to the spool dir.
79
80 =head2 remove_from_spool( $file, $dir )
81
82 Remove a message from a spool dir.
83
84 =head2 create_spoolname( $msisdn, $dir )
85
86 Create the filename for a spool message. Internal function.
87
88 =head2 read_from_spool( $dir, $n )
89
90 Read n messages from the spool.
91
92 =head1 AUTHOR
93
94 Johan Van den Brande <johan@vandenbrande.com>