]> jfr.im git - irssi-scripts.git/blobdiff - smshilight.pl
no autorun smshilight
[irssi-scripts.git] / smshilight.pl
index 8afe36c6a0fac7c03f72222db905328326b44880..4766d6f98e7a9382d69a87dfa655421cc795a363 100644 (file)
@@ -1,18 +1,54 @@
 # smshi - sends highlights via SMS, using Twilio
+# CC0 https://creativecommons.org/publicdomain/zero/1.0/
+
+# settings:
+# (bool)   smshi_active
+#   Master switch. Nothing will be done if this is OFF.
+#
+# (bool)   smshi_away_only
+#   Only send SMS when away.
+#
+# (bool)   smshi_detached_only
+#   Only send SMS when screen is detached.
+#
+# (bool)   smshi_debug
+#   Show debugging info (about communications with Twilio)
+#
+# (string) smshi_sid
+#   Twilio SID
+#
+# (string) smshi_token
+#   Twilio token
+#
+# (string) smshi_from
+#   From number (int'l format, i.e. +12022345678)
+#
+# (string) smshi_to
+#   To number   (int'l format, i.e. +12022345678)
+#
+# N.B. you are recommended to set the following values if using screen and
+# you don't want SMS when attached, even if using screen_away (since it takes
+# multiple seconds to detect, while our detached_only checks at the time of
+# the highlight):
+#   smshi_away_only OFF
+#   smshi_detached_only ON
+
+use strict;
+use warnings;
 
 use Irssi;
 use vars qw($VERSION %IRSSI);
 
 use LWP::UserAgent;
 my $ua = LWP::UserAgent->new;
-$ua->agent("irssi+SMSHi/1.0 ");
 
-$VERSION = "0.1";
+$VERSION = "2.0";
+$ua->agent("irssi+SMSHi/$VERSION ");
 %IRSSI = (
        authors     => "John Runyon",
        name        => "smshi",
        description => "send highlights via Twilio sms",
-       license     => 'public domain',
+       license     => 'CC0',
        url         => 'https://github.com/zonidjan/irssi-scripts',
        contact     => 'https://github.com/zonidjan/irssi-scripts/issues'
 );
@@ -23,27 +59,34 @@ sub got_print {
        my ($dest, $text, $stripped) = @_;
        my $server = $dest->{server};
        my $mynick = $server->{nick};
-       return unless ($dest->{level} & MSGLEVEL_HILIGHT) # continue if hilight...
-                  or ($dest->{level} & MSGLEVEL_MSGS && index($stripped, $mynick) != -1); # or if it's a PM containing my nick
-       return if $stripped =~ /<.?\Q$mynick\E>/; # avoid people quoting me
-       return if (!$server->{usermode_away} && Irssi::settings_get_bool('smshi_away_only')); # and obey away_only
-
-       my $msg = '';
-       for my $c (split //, $stripped) {
-               if (ord($c) > 31 && ord($c) < 127) {
-                       $msg .= $c;
-               } else {
-                       $msg .= '\\x'.sprintf("%02x", ord($c));
-               }
-       }
+       return unless ($dest->{level} & MSGLEVEL_HILIGHT # continue if hilight...
+                   || $dest->{level} & MSGLEVEL_MSGS && index($stripped, $mynick) != -1); # or if it's a PM containing my nick
+       return if $stripped =~ /<[ +%@&~!]?\Q$mynick\E>/; # avoid people quoting me
+       return if (Irssi::settings_get_bool('smshi_away_only') && !$server->{usermode_away}); # and obey away_only
+       print _is_attached() if Irssi::settings_get_bool('smshi_debug');
+       return if (Irssi::settings_get_bool('smshi_detached_only') && _is_attached()); # and obey detached_only
 
        my $chname = $dest->{window}->get_active_name();
-       my $sms = $server->{tag}."/".$chname.$msg;
+       my $sms = $server->{tag}."/".$chname.$stripped;
        _send_sms($sms);
 }
+
+sub _is_attached {
+       return 0 if !defined($ENV{HOME});
+       return 0 if !defined($ENV{USER});
+       return 0 if !defined($ENV{STY});
+       my @dirs = ("/run/screen/S-$ENV{USER}/$ENV{STY}", "/usr/tmp/screens/S-$ENV{USER}/$ENV{STY}", "$ENV{HOME}/.screen/$ENV{STY}", "/tmp/screens/S-$ENV{USER}/$ENV{STY}");
+       push(@dirs, "$ENV{SCREENDIR}/$ENV{STY}") if defined($ENV{SCREENDIR});
+       for (@dirs) {
+               return 1 if -x;
+       }
+       return 0;
+}
+
 sub test_sms {
        _send_sms("This is an SMS test.");
 }
+
 sub _send_sms {
        my $sms = shift;
 
@@ -70,13 +113,14 @@ sub _send_sms {
        }
 }
 
-Irssi::settings_add_bool('smshi', 'smshi_active', 0);    # master switch
-Irssi::settings_add_bool('smshi', 'smshi_away_only', 1); # send only when away?
-Irssi::settings_add_bool('smshi', 'smshi_debug', 0);     # show debugging info
-Irssi::settings_add_str('smshi', 'smshi_sid', '');       # Twilio SID
-Irssi::settings_add_str('smshi', 'smshi_token', '');     # Twilio token
-Irssi::settings_add_str('smshi', 'smshi_from', '');      # From number (+12022345678)
-Irssi::settings_add_str('smshi', 'smshi_to', '');        # To number (+12022345678)
+Irssi::settings_add_bool('smshi', 'smshi_active', 0);        # master switch
+Irssi::settings_add_bool('smshi', 'smshi_away_only', 1);     # send only when away?
+Irssi::settings_add_bool('smshi', 'smshi_detached_only', 0); # send only when screen detached?
+Irssi::settings_add_bool('smshi', 'smshi_debug', 0);         # show debugging info
+Irssi::settings_add_str('smshi', 'smshi_sid', '');           # Twilio SID
+Irssi::settings_add_str('smshi', 'smshi_token', '');         # Twilio token
+Irssi::settings_add_str('smshi', 'smshi_from', '');          # From number (+12022345678)
+Irssi::settings_add_str('smshi', 'smshi_to', '');            # To number (+12022345678)
 
 Irssi::signal_add('print text', 'got_print');
 Irssi::command_bind('testsms', 'test_sms');