#!/usr/bin/perl # # /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 warnings; use Env qw[$DEBUG $DUMMY]; 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 = lc($1); $i = 0; if (lc($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; my $num_children = $#{ $reply{$key} }; # How many children we'll create $SIG{CHLD} = 'IGNORE'; # Don't worry about reaping zombies # Spawn off some children if ($DEBUG) { print STDERR "queueing $num_children replies:"; for my $num ( 1 .. $num_children ) { print STDERR " [" . $delay{$key}[$num] . "s]"; } print STDERR "\n"; } for my $num ( 1 .. $num_children ) { $reply{$key}[$num] =~ s/^\s*(.*?)\s*$/$1/s; run_fork { child { sleep($delay{$key}[$num]); unless ($DUMMY) { system {'/usr/share/kannel/contrib/sendsms' } $phone, $reply{$key}[$num]; print STDERR "Done reply #$num [" . $delay{$key}[$num] . "s]\n" if ($DEBUG); } else { print STDERR "\n[" . $delay{$key}[$num] . "s]\n"; print STDERR $reply{$key}[$num] . "\n"; } exit; } parent { if ($DEBUG) { my $child_pid = shift; waitpid $child_pid, 0; } } } } 1;