summaryrefslogtreecommitdiff
path: root/localmarkdown2sms
blob: f768f9047f05af1f377375956f64f853f3acf6f3 (plain)
  1. #!/usr/bin/perl
  2. #
  3. # /usr/local/sbin/localmarkdown2sms
  4. # Copyright 2009 Jonas Smedegaard <dr@jones.dk>
  5. #
  6. # Send series of messages through Kannel from simplified Markdown files
  7. # * Lines starting with "#" are "keywords" activating a message series
  8. # * write only a single word
  9. # * use each keyword only once across the whole system
  10. # * use only minuscles (not majuscles, i.e. CAPITAL LETTERS)
  11. # * Lines starting with "##" express pauses
  12. # * a pause is a number + a single letter, without spaces between
  13. # * a pause line can contain multiple pauses, separated by space
  14. # Suggestion for writing style:
  15. #
  16. # * Write explicitly how to activate next series
  17. # * pick keywords tied to nex series rather than the previous
  18. # * use same instruction jargon across all series in the system
  19. use strict;
  20. use warnings;
  21. use Env qw[$DEBUG $DUMMY $NOSLEEP];
  22. use File::Spec;
  23. use File::Slurp;
  24. use Time::Duration::Parse;
  25. #use Proc::Daemon;
  26. use Proc::Fork;
  27. #use IO::Pipe;
  28. #Proc::Daemon::Init;
  29. my (%delay, %reply);
  30. my ($path) = shift @ARGV;
  31. foreach my $file (read_dir( $path )) {
  32. my ($key, $i);
  33. next unless ($file =~ /\.mdwn$/);
  34. foreach my $line (read_file( File::Spec->catfile($path, $file))) {
  35. chomp $line;
  36. my $content;
  37. # headline
  38. if ($line =~ /^(#+)\s*(.*?)\s*$/) {
  39. my $level = length($1);
  40. $content = $2;
  41. # key
  42. if ($level == 1 and $content =~ /(\w+)/) {
  43. $key = lc($1);
  44. $i = 0;
  45. if (lc($content) ne $key) {
  46. warn( "key \"$key\" extracted from fuzzy content: \"$content\"" );
  47. }
  48. if (!defined( $delay{$key})) {
  49. $delay{$key}[0] = 0;
  50. } else {
  51. warn( "skipping non-unique key: \"$key\"" );
  52. $key = undef;
  53. }
  54. # delay
  55. } elsif ($level == 2 and $content =~ /(\d+[sm](\s+|\Z))+/) {
  56. if (defined( $key)) {
  57. my $delay = parse_duration($1);
  58. if (defined($reply{$key}[$i])) {
  59. $i++;
  60. $delay{$key}[$i] = $delay{$key}[$i - 1];
  61. }
  62. $delay{$key}[$i] += $delay;
  63. if ($content ne $1) {
  64. warn( "delay (${delay}s) resolved from fuzzy content: \"$content\"" );
  65. }
  66. } else {
  67. warn( "skipping non-key'ed delay line: \"$1\"" );
  68. }
  69. } else {
  70. warn( "skipping non-parsable headline: \"$line\"");
  71. }
  72. # content
  73. } else {
  74. if ( defined( $key )) {
  75. $reply{$key}[$i] .= $line . "\n";
  76. } else {
  77. warn( "skipping non-key'ed content: \"$1\"" );
  78. }
  79. }
  80. }
  81. }
  82. my ($phone) = shift @ARGV;
  83. my ($key) = shift @ARGV;
  84. my $num_children = $#{ $reply{$key} }; # How many children we'll create
  85. $SIG{CHLD} = 'IGNORE'; # Don't worry about reaping zombies
  86. # Spawn off some children
  87. if ($DEBUG) {
  88. print STDERR "queueing $num_children replies:";
  89. for my $num ( 1 .. $num_children ) {
  90. print STDERR " [" . $delay{$key}[$num] . "s]";
  91. }
  92. print STDERR "\n";
  93. }
  94. for my $num ( 1 .. $num_children ) {
  95. $reply{$key}[$num] =~ s/^\s*(.*?)\s*$/$1/s;
  96. run_fork {
  97. child {
  98. sleep($delay{$key}[$num]) unless ($NOSLEEP);
  99. unless ($DUMMY) {
  100. system {'/usr/share/kannel/contrib/sendsms' } $phone, $reply{$key}[$num];
  101. print STDERR "Done reply #$num [" . $delay{$key}[$num] . "s]\n" if ($DEBUG);
  102. } else {
  103. print STDERR "\n[" . $delay{$key}[$num] . "s]\n";
  104. print STDERR $reply{$key}[$num] . "\n";
  105. }
  106. exit;
  107. }
  108. parent {
  109. if ($DEBUG) {
  110. my $child_pid = shift;
  111. waitpid $child_pid, 0;
  112. }
  113. }
  114. }
  115. }
  116. 1;