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