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