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