summaryrefslogtreecommitdiff
path: root/localmarkdown2sms
blob: f313c145df3a02da1d0461a280cafc2e84df353b (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 $info $warn $dummy $nosleep];
  22. use Log::Log4perl qw(:easy);
  23. use File::Spec;
  24. use File::Slurp;
  25. use Time::Duration::Parse;
  26. #use Proc::Daemon;
  27. use Proc::Fork;
  28. #use IO::Pipe;
  29. #Proc::Daemon::Init;
  30. my (%file, %delay, %reply);
  31. my ($path) = shift @ARGV;
  32. my ($phone) = shift @ARGV;
  33. my ($key) = lc (shift @ARGV);
  34. if ($debug) {
  35. Log::Log4perl->easy_init($DEBUG);
  36. } elsif ($INFO) {
  37. Log::Log4perl->easy_init($INFO);
  38. } elsif ($WARN) {
  39. Log::Log4perl->easy_init($WARN);
  40. } elsif ($ERROR) {
  41. Log::Log4perl->easy_init($ERROR);
  42. }
  43. foreach my $file (read_dir( $path )) {
  44. my ($key, $i, $skipkey, $skipcontent);
  45. # suppress repeated warnings for same issue
  46. my ($warn_nonkey_delay, $warn_nonkey_content);
  47. next unless ($file =~ /\.mdwn$/);
  48. foreach my $line (read_file( File::Spec->catfile($path, $file))) {
  49. chomp $line;
  50. my $content;
  51. # headline
  52. if ($line =~ /^(#+)\s*(.*?)\s*$/) {
  53. # tidy latest reply (TODO: use sub)
  54. if (defined($key) and defined($reply{$key}[$i])) {
  55. $reply{$key}[$i] =~ s/^\s*(\w.*?)\s*$/$1/s || delete $reply{$key}[$i];
  56. }
  57. my $level = length($1);
  58. $content = $2;
  59. # key
  60. if ($level == 1 and $content =~ /(\w+)/) {
  61. $key = lc($1);
  62. $i = 0;
  63. $skipkey = undef;
  64. $skipcontent = undef;
  65. if (lc($content) ne $key) {
  66. WARN "key \"$key\" extracted from fuzzy string \"$content\" in file \"$file\"";
  67. }
  68. if (!defined( $delay{$key})) {
  69. $delay{$key}[0] = 0;
  70. $warn_nonkey_delay = undef;
  71. $warn_nonkey_content = undef;
  72. } else {
  73. WARN "skipping non-unique key \"$key\" in file \"$file\"";
  74. $key = undef;
  75. $skipkey = 1;
  76. $skipcontent = 1;
  77. }
  78. # delay
  79. } elsif ($level == 2 and $content =~ /((\d+[sm](\s+|\Z))+)/) {
  80. $skipcontent = undef;
  81. if (defined( $key)) {
  82. my $delay = parse_duration($1);
  83. if (defined($reply{$key}[$i])) {
  84. $i++;
  85. $delay{$key}[$i] = $delay{$key}[$i - 1];
  86. }
  87. $delay{$key}[$i] += $delay;
  88. if ($content ne $1) {
  89. WARN "delay (${delay}s) resolved from fuzzy string \"$content\" in file \"$file\"";
  90. }
  91. } elsif ($skipkey) {
  92. # skipping - already warned about it...
  93. } else {
  94. WARN "ignoring non-key'ed delay line \"$1\" in file \"$file\"" unless ($warn_nonkey_delay);
  95. $warn_nonkey_delay = 1;
  96. $skipcontent = 1;
  97. }
  98. } else {
  99. WARN "ignoring non-parsable headline \"$line\" in file \"$file\"";
  100. $skipcontent = 1;
  101. }
  102. # reply
  103. } else {
  104. $content = $line . "\n";
  105. # ikiwiki directives - strip from content and parse for tags
  106. $content =~ s/(?<!\\)\[\[([^\[\]]*)(?<!\\)\]\]//gs and do {
  107. my $directive_string = $1;
  108. my ($directive, $directive_content);
  109. $directive_string =~ /^\s*\!(tag|taglink)\s*((\s*?\b\w+)+)/ and $file{$file}{'directive'}{'tag'} = [ split /\s+/, $2 ];
  110. };
  111. if ( defined( $key ) and not ($skipcontent)) {
  112. $content = $reply{$key}[$i] . $content if (defined($reply{$key}[$i]));
  113. $content =~ s/^\h*$//g; # clean virtually empty lines
  114. $content =~ s/(\S)\h$/$1/g; # strip single trailing space
  115. $content =~ s/\n\n+/\n\n/g; # strip excess newlines
  116. $content =~ s/(\S)\n([^\n])/$1 $2/g; # convert newline to space
  117. $content =~ s/\h*$//g; # strip all trailing spaces
  118. $reply{$key}[$i] = $content;
  119. } elsif ($skipkey or $skipcontent) {
  120. # skipping - already warned about it...
  121. } else {
  122. WARN "skipping non-key'ed content \"$line\" in file \"$file\"" unless ($warn_nonkey_content);
  123. $warn_nonkey_content = 1;
  124. }
  125. }
  126. }
  127. # tidy latest reply (TODO: use sub)
  128. if (defined($key) and defined($reply{$key}[$i])) {
  129. $reply{$key}[$i] =~ s/^\s*(\w.*?)\s*$/$1/s || delete $reply{$key}[$i];
  130. }
  131. }
  132. sub sendmsg {
  133. my ($phone, $desc, $msg) = @_;
  134. unless ($dummy) {
  135. DEBUG "Exec'ing \"...sendsms $phone ...\"";
  136. system '/usr/share/kannel/contrib/sendsms', $phone, $msg;
  137. DEBUG "Done $desc";
  138. } else {
  139. print STDERR "\n --> $phone: $desc\n";
  140. print STDERR $msg . "\n";
  141. }
  142. }
  143. my $num_children = $#{ $reply{$key} } + 1; # How many children we'll create
  144. if (0 == $num_children) {
  145. &sendmsg($phone, "fallback message", "Hmmm, strange, the word \"$key\" is unknown. Perhaps you typed it wrong?\n\nPlease try again.");
  146. exit;
  147. }
  148. if (0 == $num_children) {
  149. }
  150. $SIG{CHLD} = 'IGNORE'; # Don't worry about reaping zombies
  151. # Spawn off some children
  152. if ($debug) {
  153. DEBUG "queueing $num_children replies:";
  154. for my $num ( 0 .. $num_children - 1 ) {
  155. DEBUG " [" . $delay{$key}[$num] . "s]";
  156. }
  157. # DEBUG "\n";
  158. }
  159. for my $num ( 0 .. $num_children - 1 ) {
  160. run_fork {
  161. child {
  162. sleep($delay{$key}[$num]) unless ($nosleep);
  163. &sendmsg($phone, "reply #$num [" . $delay{$key}[$num] . "s]", $reply{$key}[$num]);
  164. exit;
  165. }
  166. parent {
  167. if ($debug) {
  168. my $child_pid = shift;
  169. waitpid $child_pid, 0;
  170. }
  171. }
  172. }
  173. }
  174. 1;