]>
jfr.im git - irssi-scripts.git/blob - smshilight.pl
1 # smshi - sends highlights via SMS, using Twilio
2 # CC0 https://creativecommons.org/publicdomain/zero/1.0/
6 # Master switch. Nothing will be done if this is OFF.
8 # (bool) smshi_away_only
9 # Only send SMS when away.
11 # (bool) smshi_detached_only
12 # Only send SMS when screen is detached.
15 # Show debugging info (about communications with Twilio)
20 # (string) smshi_token
24 # From number (int'l format, i.e. +12022345678)
27 # To number (int'l format, i.e. +12022345678)
29 # N.B. you are recommended to set the following values if using screen and
30 # you don't want SMS when attached, even if using screen_away (since it takes
31 # multiple seconds to detect, while our detached_only checks at the time of
34 # smshi_detached_only ON
40 use vars
qw($VERSION %IRSSI);
43 my $ua = LWP
::UserAgent-
>new;
46 $ua->agent("irssi+SMSHi/$VERSION ");
48 authors
=> "John Runyon",
50 description
=> "send highlights via Twilio sms",
52 url
=> 'https://github.com/zonidjan/irssi-scripts',
53 contact
=> 'https://github.com/zonidjan/irssi-scripts/issues'
57 return unless Irssi
::settings_get_bool
('smshi_active');
59 my ($dest, $text, $stripped) = @_;
60 my $server = $dest->{server
};
61 my $mynick = $server->{nick
};
62 return unless ($dest->{level
} & MSGLEVEL_HILIGHT
# continue if hilight...
63 || $dest->{level
} & MSGLEVEL_MSGS
&& index($stripped, $mynick) != -1); # or if it's a PM containing my nick
64 return if $stripped =~ /<[ +%@&~!]?\Q$mynick\E>/; # avoid people quoting me
65 return if (Irssi
::settings_get_bool
('smshi_away_only') && !$server->{usermode_away
}); # and obey away_only
66 print _is_attached
() if Irssi
::settings_get_bool
('smshi_debug');
67 return if (Irssi
::settings_get_bool
('smshi_detached_only') && _is_attached
()); # and obey detached_only
69 my $chname = $dest->{window
}->get_active_name();
70 my $sms = $server->{tag
}."/".$chname.$stripped;
75 return 0 if !defined($ENV{HOME
});
76 return 0 if !defined($ENV{USER
});
77 return 0 if !defined($ENV{STY
});
78 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}");
79 push(@dirs, "$ENV{SCREENDIR}/$ENV{STY}") if defined($ENV{SCREENDIR
});
87 _send_sms
("This is an SMS test.");
93 my $sid = Irssi
::settings_get_str
('smshi_sid');
94 my $token = Irssi
::settings_get_str
('smshi_token');
95 my $from = Irssi
::settings_get_str
('smshi_from');
96 my $to = Irssi
::settings_get_str
('smshi_to');
98 my $url = "https://$sid:$token\@api.twilio.com/2010-04-01/Accounts/$sid/Messages.json";
99 my $req = HTTP
::Request-
>new('POST', $url);
100 $req->content_type('application/x-www-form-urlencoded');
101 $req->content("To=$to&From=$from&Body=$sms");
103 my $res = $ua->request($req);
104 return unless Irssi
::settings_get_bool
('smshi_debug');
105 if ($res->is_success) {
106 print "Good. Sent to $to from $from: $sms";
111 print $res->status_line;
116 Irssi
::settings_add_bool
('smshi', 'smshi_active', 0); # master switch
117 Irssi
::settings_add_bool
('smshi', 'smshi_away_only', 1); # send only when away?
118 Irssi
::settings_add_bool
('smshi', 'smshi_detached_only', 0); # send only when screen detached?
119 Irssi
::settings_add_bool
('smshi', 'smshi_debug', 0); # show debugging info
120 Irssi
::settings_add_str
('smshi', 'smshi_sid', ''); # Twilio SID
121 Irssi
::settings_add_str
('smshi', 'smshi_token', ''); # Twilio token
122 Irssi
::settings_add_str
('smshi', 'smshi_from', ''); # From number (+12022345678)
123 Irssi
::settings_add_str
('smshi', 'smshi_to', ''); # To number (+12022345678)
125 Irssi
::signal_add
('print text', 'got_print');
126 Irssi
::command_bind
('testsms', 'test_sms');
127 Irssi
::print('%G>>%n '.$IRSSI{name
}.' '.$VERSION.' loaded');