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