Fixed file reading for --ems-melody-file-send
[gsmperl.git] / GSM / examples / commandline / gsmcmd
1 #! /usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 #
7 # Load some modules
8 use GSM::SMS::Config;
9 use GSM::SMS::NBS;
10 use GSM::SMS::Bitmap;
11 use GSM::SMS::NBS::Message;
12 use Getopt::Long;
13 use Carp;
14 use GSM::SMS::NBS::Lib;
15 require GSM::SMS::NBS::Alcatel;
16
17 $|=1;     # No output buffering
18
19
20 my $EMStext="";
21 my @EMSudh;
22
23 #
24 # Arguments
25 my $opt_verbose;
26 my $opt_transport;
27 my $opt_ring_send;
28 my $opt_logo_group_send;
29 my $opt_ems_force_variable;
30 my $opt_ems_picture_wrap=1;
31 my $opt_ems_animation_wrap=1;
32 my $opt_alcatel_name;
33 my $opt_alcatel_picture_send;
34 my $opt_alcatel_animation_send;
35 my $opt_alcatel_mseq_melody_file_send;
36 GetOptions( 
37         "v|verbose"                       => \$opt_verbose,
38         "transport:s"                     => \$opt_transport,
39         "ring-send:s"                     => \$opt_ring_send,
40         "logo-group-send:s"               => \$opt_logo_group_send,
41         "ems-force-variable"              => \$opt_ems_force_variable,
42         "ems-picture-send:s"              => \&ems_picture_send,
43         "ems-picture-wrap!"               => \$opt_ems_picture_wrap,
44         "ems-animation-send:s"            => \&ems_animation_send,
45         "ems-animation-wrap!"             => \$opt_ems_animation_wrap,
46         "ems-melody-file-send:s"          => \&ems_melody_file_send,
47         "ems-text:s"                      => \&ems_text,
48         "alcatel-name:s"                  => \$opt_alcatel_name,
49         "alcatel-picture-send:s"          => \$opt_alcatel_picture_send,
50         "alcatel-animation-send:s"        => \$opt_alcatel_animation_send,
51         "alcatel-mseq-melody-file-send:s" => \$opt_alcatel_mseq_melody_file_send,
52 );
53
54 unless ( @ARGV ) {
55         print <<EOT;
56 Usage: $0 [--verbose] [--transport=<transport.conf>]
57        [--ring-send=<rtttl file>]
58        [--logo-group-send=<image file>]
59        [--ems-force-variable]
60        [--ems-picture-send=<image file>]
61        [--ems-animation-send=<image file>]
62        [--ems-melody-file-send=<iMelody file>]
63        [--alcatel-name=<resource name>]
64        [--alcatel-picture-send=<image file>]
65        [--alcatel-animation-send=<image file>]
66        [--alcatel-mseq-melody-file-send=<MSEQ file>]
67        <phone numbers>...
68 EOT
69         exit(1);
70 }
71
72 #
73 # Initialize
74 my $nbs = GSM::SMS::NBS->new( $opt_transport ? $opt_transport : "/dev/null" );
75
76 my $msisdn;
77 my $message;
78 my $timestamp;
79 my $transportname;
80 my $port;
81
82 undef $/; # Read files at once, NEVER set it before as at least NBS->new() needs line-reading!
83
84 for $msisdn (@ARGV) {
85
86         if ($opt_ring_send) {
87                 local (*F);
88                 open F, $opt_ring_send;
89                 my $rtttl_string = <F>;
90                 close F;
91                 $nbs->sendRTTTL($msisdn, $rtttl_string);
92         }
93
94         if ($opt_logo_group_send) {
95                 $nbs->sendGroupGraphic_file($msisdn, $opt_logo_group_send);
96         }
97
98         if (@EMSudh || $EMStext ne "") {
99                 $nbs->sendsms($msisdn, $EMStext, udh=>[ @EMSudh ]);
100         }
101
102         if ($opt_alcatel_picture_send) {
103                 my $bitmap = GSM::SMS::Bitmap->new($opt_alcatel_picture_send);
104                 my ($msg, $udh) = GSM::SMS::NBS::Alcatel->alcatel_picture($bitmap);
105                 $udh->[0]{"name"}=$opt_alcatel_name if defined $opt_alcatel_name;
106                 $nbs->sendsms($msisdn, $msg, udh=>$udh);
107         }
108
109         if ($opt_alcatel_animation_send) {
110                 my @names=split /:/,$opt_alcatel_animation_send;
111                 my %imgmap=();
112                 my @list=map {
113                                 if (/^\d+$/) {
114                                         $_;
115                                 } else {
116                                         $imgmap{$_}=GSM::SMS::Bitmap->new($_) if (!exists $imgmap{$_});
117                                         $imgmap{$_};
118                                 }
119                 } @names;
120                 # It needs to be used for compatibility with Alcatel 311 (firmware 101.01)
121                 #  - reported/tested by Daniel Vesely <jude@atspraha.cz>
122                 warn "You probably need to specify initial frame display delay, converting anyway"
123                                 if "GSM::SMS::Bitmap" eq ref $list[0];
124                 my ($msg, $udh) = GSM::SMS::NBS::Alcatel->alcatel_animation(@list);
125                 $udh->[0]{"name"}=$opt_alcatel_name if defined $opt_alcatel_name;
126                 $nbs->sendsms($msisdn, $msg, udh=>$udh);
127         }
128
129         if ($opt_alcatel_mseq_melody_file_send) {
130                 local (*F);
131                 open F, $opt_alcatel_mseq_melody_file_send;
132                 my $mseq_melody_file_data = <F>;
133                 close F;
134                 my ($msg, $udh) = GSM::SMS::NBS::Alcatel->alcatel_mseq_melody_file($mseq_melody_file_data);
135                 $nbs->sendsms($msisdn, $msg, udh=>$udh);
136         }
137 }
138
139 exit(0);
140
141 sub ems_picture_send
142 {
143 my($keyword,$arg)=@_;
144
145         my $bitmap = GSM::SMS::Bitmap->new($arg);
146         $bitmap->crop(0xFF*8,0xFF);
147         my ($width, $height) = ($bitmap->{"width"}, $bitmap->{"height"});
148
149         # pixel-payload of concat8 SMS w/one IEI_PICTURE_VARIABLE is 129 bytes
150         my $stripe=int(129/int(($width+7)/8));
151         carp "Image width too big to fit in one SMS part" if $stripe<=0;        # ==0
152         # Will we need some newlines?
153         if ($opt_ems_picture_wrap && $stripe<$height) {
154                 for (my $y=0;$y<$height;$y+=$stripe) {
155                         push @EMSudh,{
156                                         "type"=>"ems_picture",
157                                         TEXT_POSITION=>length($EMStext),
158                                         "bitmap"=>GSM::SMS::Bitmap->new($bitmap,0,$y,$width,min($stripe,$height-$y)),
159                                         "force_variable"=>$opt_ems_force_variable,
160                                         };
161                         $EMStext.="\n" if $y+$stripe<$height;
162                         }
163                 return;
164                 }
165         push @EMSudh,{
166                         "type"=>"ems_picture",
167                         TEXT_POSITION=>length($EMStext),
168                         "bitmap"=>$bitmap,
169                         "force_variable"=>$opt_ems_force_variable,
170                         };
171 }
172
173 sub ems_animation_send
174 {
175 my($keyword,$arg)=@_;
176
177         my @names=split /:/,$arg;
178         die "Required exactly 4 colon (':') delimited filenames for --ems-animation-send" if 4 != @names;
179         my @bitmaps=map { GSM::SMS::Bitmap->new($_); } @names;
180         my($width,$height)=GSM::SMS::NBS::Message::ems_animation_maxsize(\@bitmaps);
181         # Will we need some newlines?
182         if ($opt_ems_animation_wrap && $height>16) {
183                 my $height_now;
184                 for (my $y=0;$y<$height;$y+=$height_now) {
185                         $height_now=($y+8>=$height ? 8 : 16);
186                         # we don't do horizontal split here
187                         push @EMSudh,{
188                                         "type"=>"ems_animation",
189                                         TEXT_POSITION=>length($EMStext),
190                                         "bitmaps"=>[ map({
191                                                         GSM::SMS::Bitmap->new($_,0,$y,$width,$height_now);
192                                                         } @bitmaps) ],
193                                         };
194                         $EMStext.="\n" if $y+$height_now<$height;
195                         }
196                 return;
197                 }
198         push @EMSudh,{
199                         "type"=>"ems_animation",
200                         TEXT_POSITION=>length($EMStext),
201                         "bitmaps"=>\@bitmaps,
202                         };
203 }
204
205 sub ems_melody_file_send
206 {
207 my($keyword,$arg)=@_;
208
209         local (*F);
210         open F, $arg;
211         local $/=undef();
212         my $ems_melody_file_data = <F>;
213         close F;
214         warn "EMS iMelody file format not recognized, wrapping anyway"
215                         if $ems_melody_file_data!~/^BEGIN:IMELODY\r\n.*END:IMELODY\r\n$/s;
216         push @EMSudh,{
217                         "type"=>"ems_melody",
218                         TEXT_POSITION=>length($EMStext),
219                         "textdata"=>$ems_melody_file_data,
220                         };
221 }
222
223 sub ems_text
224 {
225 my($keyword,$arg)=@_;
226
227         $EMStext.=$arg;
228 }