summaryrefslogtreecommitdiff
path: root/localmarkdown2sms
blob: 54e3cc90f471346e836f29a198108560682d02f6 (plain)
  1. #!/usr/bin/perl -w
  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 File::Spec;
  21. use File::Slurp;
  22. use Time::Duration::Parse;
  23. #use Proc::Daemon;
  24. use Proc::Fork;
  25. #use IO::Pipe;
  26. #Proc::Daemon::Init;
  27. my (%delay, %reply);
  28. my ($path) = shift @ARGV;
  29. foreach my $file (read_dir( $path )) {
  30. my ($key, $i);
  31. next unless ($file =~ /\.mdwn$/);
  32. foreach my $line (read_file( File::Spec->catfile($path, $file))) {
  33. chomp $line;
  34. my $content;
  35. # headline
  36. if ($line =~ /^(#+)\s*(.*?)\s*$/) {
  37. my $level = length($1);
  38. $content = $2;
  39. # key
  40. if ($level == 1 and $content =~ /(\w+)/) {
  41. $key = $1;
  42. $i = 0;
  43. if ($content ne $key) {
  44. warn( "key \"$key\" extracted from fuzzy content: \"$content\"" );
  45. }
  46. if (!defined( $delay{$key})) {
  47. $delay{$key}[0] = 0;
  48. } else {
  49. warn( "skipping non-unique key: \"$key\"" );
  50. $key = undef;
  51. }
  52. # delay
  53. } elsif ($level == 2 and $content =~ /(\d+[sm](\s+|\Z))+/) {
  54. if (defined( $key)) {
  55. my $delay = parse_duration($1);
  56. if (defined($reply{$key}[$i])) {
  57. $i++;
  58. $delay{$key}[$i] = $delay{$key}[$i - 1];
  59. }
  60. $delay{$key}[$i] += $delay;
  61. if ($content ne $1) {
  62. warn( "delay (${delay}s) resolved from fuzzy content: \"$content\"" );
  63. }
  64. } else {
  65. warn( "skipping non-key'ed delay line: \"$1\"" );
  66. }
  67. } else {
  68. warn( "skipping non-parsable headline: \"$line\"");
  69. }
  70. # content
  71. } else {
  72. if ( defined( $key )) {
  73. $reply{$key}[$i] .= $line . "\n";
  74. } else {
  75. warn( "skipping non-key'ed content: \"$1\"" );
  76. }
  77. }
  78. }
  79. }
  80. my ($phone) = shift @ARGV;
  81. my ($key) = shift @ARGV;
  82. $SIG{CHLD} = 'IGNORE'; # Don't worry about reaping zombies
  83. # Spawn off some children
  84. for my $num ( 1 .. $#{ $reply{$key} } ) {
  85. $reply{$key}[$num] =~ s/^\s*(.*?)\s*$/$1/s;
  86. run_fork { child {
  87. sleep($delay{$key}[$num]);
  88. system {'/usr/share/kannel/contrib/sendsms' } $phone, $reply{$key}[$num];
  89. # system "/bin/echo \"$phone: $reply{$key}[$num]\" >/tmp/sms_$num";
  90. # print STDERR $reply{$key}[$num];
  91. exit;
  92. } };
  93. }
  94. 1;