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