summaryrefslogtreecommitdiff
path: root/localmarkdown2sms
blob: ddc1366f846992a1e115f76358561cb1c85676fa (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. # tidy latest reply (TODO: use sub)
  40. if (defined($i) and defined($reply{$key}[$i])) {
  41. $reply{$key}[$i] =~ s/^\s*(\w.*?)\s*$/$1/s || delete $reply{$key}[$i];
  42. }
  43. my $level = length($1);
  44. $content = $2;
  45. # key
  46. if ($level == 1 and $content =~ /(\w+)/) {
  47. $key = lc($1);
  48. $i = 0;
  49. if (lc($content) ne $key) {
  50. warn( "key \"$key\" extracted from fuzzy string \"$content\" in file \"$file\"" );
  51. }
  52. if (!defined( $delay{$key})) {
  53. $delay{$key}[0] = 0;
  54. } else {
  55. warn( "skipping non-unique key \"$key\" in file \"$file\"" );
  56. $key = undef;
  57. }
  58. # delay
  59. } elsif ($level == 2 and $content =~ /(\d+[sm](\s+|\Z))+/) {
  60. if (defined( $key)) {
  61. my $delay = parse_duration($1);
  62. if (defined($reply{$key}[$i])) {
  63. $i++;
  64. $delay{$key}[$i] = $delay{$key}[$i - 1];
  65. }
  66. $delay{$key}[$i] += $delay;
  67. if ($content ne $1) {
  68. warn( "delay (${delay}s) resolved from fuzzy string \"$content\" in file \"$file\"" );
  69. }
  70. } else {
  71. warn( "skipping non-key'ed delay line \"$1\" in file \"$file\"" );
  72. }
  73. } else {
  74. warn( "skipping non-parsable headline \"$line\" in file \"$file\"");
  75. }
  76. # reply
  77. } elsif ( defined( $key )) {
  78. $content = $reply{$key}[$i] if (defined($reply{$key}[$i]));
  79. $content .= $line . "\n";
  80. $content =~ s/^\h*$//g; # clean virtually empty lines
  81. $content =~ s/(\S)\h$/$1/g; # strip single trailing space
  82. $content =~ s/\n\n+/\n\n/g; # strip excess newlines
  83. $content =~ s/(\S)\n([^\n])/$1 $2/g; # convert newline to space
  84. $content =~ s/\h*$//g; # strip all trailing spaces
  85. $reply{$key}[$i] = $content;
  86. } else {
  87. warn( "skipping non-key'ed content \"$line\" in file \"$file\"" );
  88. }
  89. }
  90. # tidy latest reply (TODO: use sub)
  91. if (defined($i) and defined($reply{$key}[$i])) {
  92. $reply{$key}[$i] =~ s/^\s*(\w.*?)\s*$/$1/s || delete $reply{$key}[$i];
  93. }
  94. }
  95. my ($phone) = shift @ARGV;
  96. my ($key) = shift @ARGV;
  97. my $num_children = $#{ $reply{$key} } + 1; # How many children we'll create
  98. $SIG{CHLD} = 'IGNORE'; # Don't worry about reaping zombies
  99. # Spawn off some children
  100. if ($DEBUG) {
  101. print STDERR "queueing $num_children replies:";
  102. for my $num ( 0 .. $num_children - 1 ) {
  103. print STDERR " [" . $delay{$key}[$num] . "s]";
  104. }
  105. print STDERR "\n";
  106. }
  107. for my $num ( 0 .. $num_children - 1 ) {
  108. run_fork {
  109. child {
  110. sleep($delay{$key}[$num]) unless ($NOSLEEP);
  111. unless ($DUMMY) {
  112. system {'/usr/share/kannel/contrib/sendsms' } $phone, $reply{$key}[$num];
  113. print STDERR "Done reply #$num [" . $delay{$key}[$num] . "s]\n" if ($DEBUG);
  114. } else {
  115. print STDERR "\n[" . $delay{$key}[$num] . "s]\n";
  116. print STDERR $reply{$key}[$num] . "\n";
  117. }
  118. exit;
  119. }
  120. parent {
  121. if ($DEBUG) {
  122. my $child_pid = shift;
  123. waitpid $child_pid, 0;
  124. }
  125. }
  126. }
  127. }
  128. 1;