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