#!/usr/bin/perl -w # # /usr/local/sbin/localmarkdown2sms # Copyright 2009 Jonas Smedegaard # # Send series of messages through Kannel from simplified Markdown files # * Lines starting with "#" are "keywords" activating a message series # * write only a single word # * use each keyword only once across the whole system # * use only minuscles (not majuscles, i.e. CAPITAL LETTERS) # * Lines starting with "##" express pauses # * a pause is a number + a single letter, without spaces between # * a pause line can contain multiple pauses, separated by space # Suggestion for writing style: # # * Write explicitly how to activate next series # * pick keywords tied to nex series rather than the previous # * use same instruction jargon across all series in the system use strict; use File::Spec; use File::Slurp; use Time::Duration::Parse; #use Proc::Daemon; use Proc::Fork; #use IO::Pipe; #Proc::Daemon::Init; my (%delay, %reply); my ($path) = shift @ARGV; foreach my $file (read_dir( $path )) { my ($key, $i); next unless ($file =~ /\.mdwn$/); foreach my $line (read_file( File::Spec->catfile($path, $file))) { chomp $line; my $content; # headline if ($line =~ /^(#+)\s*(.*?)\s*$/) { my $level = length($1); $content = $2; # key if ($level == 1 and $content =~ /(\w+)/) { $key = $1; $i = 0; if ($content ne $key) { warn( "key \"$key\" extracted from fuzzy content: \"$content\"" ); } if (!defined( $delay{$key})) { $delay{$key}[0] = 0; } else { warn( "skipping non-unique key: \"$key\"" ); $key = undef; } # delay } elsif ($level == 2 and $content =~ /(\d+[sm](\s+|\Z))+/) { if (defined( $key)) { my $delay = parse_duration($1); if (defined($reply{$key}[$i])) { $i++; $delay{$key}[$i] = $delay{$key}[$i - 1]; } $delay{$key}[$i] += $delay; if ($content ne $1) { warn( "delay (${delay}s) resolved from fuzzy content: \"$content\"" ); } } else { warn( "skipping non-key'ed delay line: \"$1\"" ); } } else { warn( "skipping non-parsable headline: \"$line\""); } # content } else { if ( defined( $key )) { $reply{$key}[$i] .= $line . "\n"; } else { warn( "skipping non-key'ed content: \"$1\"" ); } } } } my ($phone) = shift @ARGV; my ($key) = shift @ARGV; $SIG{CHLD} = 'IGNORE'; # Don't worry about reaping zombies # Spawn off some children for my $num ( 1 .. $#{ $reply{$key} } ) { $reply{$key}[$num] =~ s/^\s*(.*?)\s*$/$1/s; run_fork { child { sleep($delay{$key}[$num]); system {'/usr/share/kannel/contrib/sendsms' } $phone, $reply{$key}[$num]; # system "/bin/echo \"$phone: $reply{$key}[$num]\" >/tmp/sms_$num"; # print STDERR $reply{$key}[$num]; exit; } }; } 1;