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