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