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