summaryrefslogtreecommitdiff
path: root/localmarkdown2sms
blob: 71ca9ea009fe2f6abc6d659eae4913feaab7447a (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::Fork;
  27. use Encode;
  28. use LWP::UserAgent;
  29. use URI::Escape;
  30. my $sms_url = $ENV{SMS_URL} || "http://localhost:13013/cgi-bin/sendsms";
  31. my $sms_user = $ENV{SMS_USER} || "tester";
  32. my $sms_pw = $ENV{SMS_PW} || "foobar";
  33. my $sms_phone = $ENV{SMS_PHONE};
  34. my (%file, %delay, %reply);
  35. my ($path) = shift @ARGV;
  36. my ($phone) = shift @ARGV;
  37. my ($key) = lc (shift @ARGV);
  38. if ($debug) {
  39. Log::Log4perl->easy_init($DEBUG);
  40. } elsif ($INFO) {
  41. Log::Log4perl->easy_init($INFO);
  42. } elsif ($WARN) {
  43. Log::Log4perl->easy_init($WARN);
  44. } elsif ($ERROR) {
  45. Log::Log4perl->easy_init($ERROR);
  46. }
  47. foreach my $file (read_dir( $path )) {
  48. my ($key, $i, $skipkey, $skipcontent);
  49. # suppress repeated warnings for same issue
  50. my ($warn_nonkey_delay, $warn_nonkey_content);
  51. next unless ($file =~ /\.mdwn$/);
  52. foreach my $line (read_file( File::Spec->catfile($path, $file))) {
  53. chomp $line;
  54. my $content;
  55. # headline
  56. if ($line =~ /^(#+)\s*(.*?)\s*$/) {
  57. # tidy latest reply
  58. if (defined($key) and defined($reply{$key}[$i])) {
  59. $reply{$key}[$i] = &tidymsg($reply{$key}[$i]);
  60. ($reply{$key}[$i]) || delete $reply{$key}[$i];
  61. }
  62. my $level = length($1);
  63. $content = $2;
  64. # key
  65. if ($level == 1 and $content =~ /(\w+)/) {
  66. $key = lc($1);
  67. $i = 0;
  68. $skipkey = undef;
  69. $skipcontent = undef;
  70. if (lc($content) ne $key) {
  71. WARN "key \"$key\" extracted from fuzzy string \"$content\" in file \"$file\"";
  72. }
  73. if (!defined( $delay{$key})) {
  74. $delay{$key}[0] = 0;
  75. $warn_nonkey_delay = undef;
  76. $warn_nonkey_content = undef;
  77. } else {
  78. WARN "skipping non-unique key \"$key\" in file \"$file\"";
  79. $key = undef;
  80. $skipkey = 1;
  81. $skipcontent = 1;
  82. }
  83. # delay
  84. } elsif ($level == 2 and $content =~ /((\d+[sm](\s+|\Z))+)/) {
  85. $skipcontent = undef;
  86. if (defined( $key)) {
  87. my $delay = parse_duration($1);
  88. if (defined($reply{$key}[$i])) {
  89. $i++;
  90. $delay{$key}[$i] = $delay{$key}[$i - 1];
  91. }
  92. $delay{$key}[$i] += $delay;
  93. if ($content ne $1) {
  94. WARN "delay (${delay}s) resolved from fuzzy string \"$content\" in file \"$file\"";
  95. }
  96. } elsif ($skipkey or $warn_nonkey_delay) {
  97. # skipping - already warned about it...
  98. } else {
  99. WARN "ignoring non-key'ed delay line \"$1\" in file \"$file\"";
  100. $warn_nonkey_delay = 1;
  101. $skipcontent = 1;
  102. }
  103. } else {
  104. WARN "ignoring non-parsable headline \"$line\" in file \"$file\"";
  105. $skipcontent = 1;
  106. }
  107. # reply
  108. } else {
  109. $content = $line . "\n";
  110. # ikiwiki directives - strip from content and parse for tags
  111. $content =~ s/(?<!\\)\[\[([^\[\]]*)(?<!\\)\]\]//gs and do {
  112. my $directive_string = $1;
  113. my ($directive, $directive_content);
  114. $directive_string =~ /^\s*\!(tag|taglink)\s*((\s*?\b\w+)+)/ and $file{$file}{'directive'}{'tag'} = [ split /\s+/, $2 ];
  115. };
  116. if ( defined( $key ) and not ($skipcontent)) {
  117. $reply{$key}[$i] .= $content;
  118. } elsif ($skipkey or $skipcontent or $warn_nonkey_content) {
  119. # skipping - already warned about it...
  120. } else {
  121. $content =~ /\S/s && WARN "skipping non-key'ed content \"$line\" in file \"$file\"";
  122. $warn_nonkey_content = 1;
  123. }
  124. }
  125. }
  126. # tidy latest reply
  127. if (defined($key) and defined($reply{$key}[$i])) {
  128. $reply{$key}[$i] = &tidymsg($reply{$key}[$i]);
  129. ($reply{$key}[$i]) || delete $reply{$key}[$i];
  130. }
  131. }
  132. sub tidymsg {
  133. my $msg = shift @_;
  134. $msg =~ s/^\h*$//g; # clean virtually empty lines
  135. $msg =~ s/(\S)\h$/$1/g; # strip single trailing space
  136. $msg =~ s/\n\n+/\n\n/g; # strip excess newlines
  137. $msg =~ s/(\S)\n([^\n])/$1 $2/g; # convert newline to space
  138. $msg =~ s/\h*$//g; # strip all trailing spaces
  139. $msg =~ s/^\s*(\w*?.*?)\s*$/$1/s; # strip surrounding space
  140. return $msg;
  141. }
  142. sub sendmsg {
  143. my ($phone, $desc, $msg) = @_;
  144. unless ($dummy) {
  145. my $ua = LWP::UserAgent->new(agent => "localmarkdown2sms");
  146. $ua->timeout(10);
  147. my $url = $sms_url
  148. . '?username=' . uri_escape($sms_user)
  149. . '&password=' . uri_escape($sms_pw)
  150. . '&to=' . uri_escape($phone);
  151. $url .= '&from=' . uri_escape($sms_phone) if ($sms_phone);
  152. $url .= '&message=' . uri_escape(encode("cp1252", $msg));
  153. my $response = $ua->request(HTTP::Request->new('GET', $url));
  154. unless ($response->is_success) {
  155. ERROR $response->status_line;
  156. }
  157. DEBUG "Done $desc";
  158. } else {
  159. print STDERR "\n --> $phone: $desc\n";
  160. print STDERR $msg . "\n";
  161. }
  162. }
  163. my $num_children = $#{ $reply{$key} } + 1; # How many children we'll create
  164. if (0 == $num_children) {
  165. &sendmsg($phone, "fallback message", "Hmmm, strange, the word \"$key\" is unknown. Perhaps you typed it wrong?\n\nPlease try again.");
  166. exit;
  167. }
  168. if (0 == $num_children) {
  169. }
  170. $SIG{CHLD} = 'IGNORE'; # Don't worry about reaping zombies
  171. # Spawn off some children
  172. if ($debug) {
  173. DEBUG "queueing $num_children replies:";
  174. for my $num ( 0 .. $num_children - 1 ) {
  175. DEBUG " [" . $delay{$key}[$num] . "s]";
  176. }
  177. # DEBUG "\n";
  178. }
  179. for my $num ( 0 .. $num_children - 1 ) {
  180. run_fork {
  181. child {
  182. sleep($delay{$key}[$num]) unless ($nosleep);
  183. &sendmsg($phone, "reply #$num [" . $delay{$key}[$num] . "s]", $reply{$key}[$num]);
  184. exit;
  185. }
  186. parent {
  187. if ($debug) {
  188. my $child_pid = shift;
  189. waitpid $child_pid, 0;
  190. }
  191. }
  192. }
  193. }
  194. 1;