:pserver:anonymous@intra.tektonica.com:/opt/cvs - gsmperl - Fri Dec 21 07:37 CET...
[gsmperl.git] / GSM / SMS / examples / slashdot / slashdot
1 #!/usr/bin/perl
2
3
4 use lib '../../../..';
5
6
7
8 #
9 # Use modules
10 use LWP::Simple;
11 use File::stat;
12 use Getopt::Long;
13 use GSM::SMS::NBS;
14
15
16 #
17 # No output buffering
18 $|++;
19
20 #
21 # Get arguments
22 my $ARG_TRANSPORTCFG;
23 my $ARG_VERBOSE;
24 my $ARG_CACHEFILE;
25 my $ARG_ACL;
26 GetOptions(
27         "transport=s"   => \$ARG_TRANSPORTCFG,  
28         "verbose"               => \$ARG_VERBOSE,
29         "cachefile=s"   => \$ARG_CACHEFILE,
30         "acl=s"                 => \$ARG_ACL
31                         );
32
33 unless ( $ARG_CACHEFILE && $ARG_ACL && $ARG_TRANSPORTCFG ) {
34 print <<EOT;
35 Usage: $0 --transport=<file with transport config> --cachefile=<file to keep cache> --acl=<comma seperated msisdn regexs> [--verbose]
36
37     transport        File that contains the transport configuration.
38     
39     cachefile        File to keep latest headlines. Slashdot asks only to hit
40                      the server 1 time in an hour, so we obey.
41     
42     acl              Comma seperated list of regular expression of msisdn
43                      to allow the service.
44                      e.g.:
45                      --acl=".*"                   : allow everybody 
46                      --acl="^\+32475,^\+32478"    : allow these prefixes
47                      --acl="^\+32475000000"       : allow this number
48     
49     verbose          Print out info.
50
51     To access /. through a proxy ( bash ):
52     export http_proxy=http://proxy:port
53    
54
55 EOT
56 exit(1);
57 }
58
59
60 #
61 # Configuration
62 my $CFG_TIMEOUT = 60*60;                # 60 minutes, as asked by slashdot ...
63 my @CFG_ACL = split /,/, $ARG_ACL;
64
65
66 #
67 # Let's go
68 verb( join( " ", split( //, "SLASHDOT HEADLINES") ) . "\n\n" );
69
70 #
71 # Start server
72
73 my $nbs = GSM::SMS::NBS->new( $ARG_TRANSPORTCFG );
74
75 my $message;
76 my $timestamp;
77 my $transportname;
78 my $port;
79
80 while (1) {
81                 verb( "waiting for message ..." );
82                 # blocking receive
83                 $nbs->receive(  \$msisdn, 
84                         \$message, 
85                         \$timestamp, 
86                         \$transportname, 
87                         \$port, 
88                         1 );    
89         
90         verb(<<EOT
91
92 received a message:
93 msisdn:        $msisdn
94 timestamp:     $timestamp
95 transport:     $transportname
96 port:          $port
97 --------------------------------------------------------------------------
98 $message
99 --------------------------------------------------------------------------
100 EOT
101 );
102         # only text messages
103         unless ( $port ) {
104                 # acl check
105                 if ( grep { $msisdn =~ /$_/ } @CFG_ACL ) {
106                         verb( "acl pass\n" );
107                         # check for code word
108                         if (  $message =~ /^sld/i ) {  
109         
110                                 $stats = stat($ARG_CACHEFILE);
111
112                                 unless ($stats && (time - $stats->mtime) < $CFG_TIMEOUT) {
113                                         verb( "Getting new SLASHDOT headlines\n" );
114                                         getstore('http://www.slashdot.org/slashdot.xml', $ARG_CACHEFILE); 
115                                 }
116
117                                 open XML,$ARG_CACHEFILE or die("Cannot open $ARG_CACHEFILE for read: $!");
118                                 my $data = join "", <XML>;
119                                 close XML;
120                                 my $msg="";
121                                 my @msg;
122                                 while ($data =~ m#<title>(.*?)<\/title>#gsi) {
123                                         my $line = "*".$1."\n";
124                                         if (length($msg.$line)>160 || $msg eq "") {
125                                                 if ($msg ne "") {
126                                                         push @msg, $msg;
127                                                 }
128                                                 $msg="SLASHDOT #pa/#fr\n\n";
129                                         }
130                                         $msg.=$line;
131                                 }
132                                 push @msg, $msg;
133
134                                 my $from = sprintf("%02d",$#msg+1);
135                                 for($i=0;$i<=$#msg;$i++) {
136                                         my $page = sprintf("%02d",$i+1);
137                                         $msg[$i]=~s/#pa/$page/;
138                                         $msg[$i]=~s/#fr/$from/;
139                                         verb( "=" x 75 . "\n" );
140                                         verb( $msg[$i] );
141                                         verb( "." x 75 . "\n\n");
142                                         $nbs->sendSMSTextMessage( $msisdn, $msg[$i] );
143                                 }
144                         }
145                 }       
146         }
147 }
148 exit(0);
149
150 #
151 # Verbose function
152 sub verb {
153         print shift if $ARG_VERBOSE;
154 }