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