summaryrefslogtreecommitdiff
path: root/localmarkdown2sms
diff options
context:
space:
mode:
authorJonas Smedegaard <dr@jones.dk>2009-08-08 04:27:32 +0200
committerJonas Smedegaard <dr@jones.dk>2009-08-08 04:27:32 +0200
commit6fba03979143ba544f81302fce0e18865af59759 (patch)
treec808823a7b514f80623eff5737757e52f2d53dbb /localmarkdown2sms
parent270fc815a639c32583b20b7d31ed29452e346df3 (diff)
Add new script localmarkdown2sms.
Diffstat (limited to 'localmarkdown2sms')
-rwxr-xr-xlocalmarkdown2sms105
1 files changed, 105 insertions, 0 deletions
diff --git a/localmarkdown2sms b/localmarkdown2sms
new file mode 100755
index 0000000..3e42e05
--- /dev/null
+++ b/localmarkdown2sms
@@ -0,0 +1,105 @@
+#!/usr/bin/perl -w
+#
+# /usr/local/sbin/localmarkdown2sms
+# Copyright 2009 Jonas Smedegaard <dr@jones.dk>
+#
+# 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::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( '$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;