Implemented connection type "tcp" (GCT_TCP), use <hostname>:<port> as "port"
[gnokii.git] / smsd / sms-sendsms
1 #! /usr/bin/perl
2 #
3 # $Id$
4
5 use strict;
6 use warnings;
7
8 use Getopt::Long;
9 use Pg;
10
11
12 my($pg_conn,$pg_result);
13
14 my($opt_db,$opt_sendtext,$opt_sendringtone,$opt_sendoplogo,$opt_sendcalleridlogo,$opt_fromfile,$opt_netcode,$opt_longtext,$opt_longudh,$opt_udhpresent,$opt_eightbit,$opt_smsc);
15
16
17 sub enbool
18 {
19 my($ref)=@_;
20
21         $$ref=($$ref ? "'t'" : "'f'");
22 }
23
24 sub ennull
25 {
26 my($ref)=@_;
27
28         $$ref=(defined($$ref) ? "'$$ref'" : "NULL");
29 }
30
31
32 Getopt::Long::Configure("bundling");
33
34 $opt_db="dbname=sms";
35
36 die() if (!GetOptions(
37                 "d|db"             ,\$opt_db,
38                 "sendtext"         ,\$opt_sendtext,
39                 "sendringtone"     ,\$opt_sendringtone,
40                 "sendoplogo"       ,\$opt_sendoplogo,
41                 "sendcalleridlogo" ,\$opt_sendcalleridlogo,
42                 "fromfile!"        ,\$opt_fromfile,
43                 "netcode=s"        ,\$opt_netcode,
44                   "longtext"       ,\$opt_longtext,
45                   "longudh"        ,\$opt_longudh,
46                   "udh!"           ,\$opt_udhpresent,
47                 "8|eightbit"       ,\$opt_eightbit,
48                   "smsc=s"         ,\$opt_smsc,
49                 ));
50 my($type);
51 $type="ringtone"     if $opt_sendringtone;
52 $type="oplogo"       if $opt_sendoplogo;
53 $type="calleridlogo" if $opt_sendcalleridlogo;
54 $type="text"         if !defined $type;
55
56 $opt_eightbit=1 if $type ne "text";
57
58 die "Argument conflict" if 0
59         || ($opt_longtext && $opt_longudh)
60         || (1 < defined($opt_sendtext)+defined($opt_sendringtone)+defined($opt_sendoplogo)+defined($opt_sendcalleridlogo))
61         || ($type ne "text") && $opt_udhpresent
62         ;
63
64 die "Destination number required" if !@ARGV;
65 my($destination)=shift @ARGV;
66
67 undef $/;
68 die "Filename on commandline required" if $opt_fromfile && !@ARGV;
69 my($text)=($opt_fromfile ? shift(@ARGV) : <STDIN>);
70 $text="" if !defined($text);
71 die "Message too long" if !$opt_longtext && !$opt_longudh && 160<length($text);
72
73 my($hexencode)=!$opt_fromfile;  #filenames are not hexencoded
74 my($hextext);
75 if ($hexencode) {
76         $hextext="";
77         $hextext.=sprintf("%02X",ord($1)) while ($text=~s/^(.)//s);
78         }
79 else {
80         $hextext=$text;
81         }
82
83 die "Excessive arguments: @ARGV" if @ARGV;
84
85 $pg_conn=Pg::connectdb($opt_db);
86 die $pg_conn->errorMessage() unless PGRES_CONNECTION_OK eq $pg_conn->status;
87
88 enbool(\$opt_fromfile);
89 ennull(\$opt_netcode);
90 enbool(\$opt_udhpresent);
91 $opt_longudh=!$opt_longtext;
92 enbool(\$opt_longudh);
93 enbool(\$opt_eightbit);
94 ennull(\$opt_smsc);
95 enbool(\$hexencode);
96
97 $pg_result=$pg_conn->exec(
98                 "INSERT INTO outbox (number,text,hexencode,type,fromfile,netcode,udhpresent,longuseudh,eightbit,smsc)"
99                 ." VALUES ('$destination','$hextext',$hexencode,'$type',$opt_fromfile,$opt_netcode,$opt_udhpresent,$opt_longudh,$opt_eightbit,$opt_smsc);");
100 die $pg_conn->errorMessage() unless PGRES_COMMAND_OK eq $pg_result->resultStatus();