]> jfr.im git - irssi-scripts.git/blob - smshilight.pl
no autorun smshilight
[irssi-scripts.git] / smshilight.pl
1 # smshi - sends highlights via SMS, using Twilio
2 # CC0 https://creativecommons.org/publicdomain/zero/1.0/
3
4 # settings:
5 # (bool) smshi_active
6 # Master switch. Nothing will be done if this is OFF.
7 #
8 # (bool) smshi_away_only
9 # Only send SMS when away.
10 #
11 # (bool) smshi_detached_only
12 # Only send SMS when screen is detached.
13 #
14 # (bool) smshi_debug
15 # Show debugging info (about communications with Twilio)
16 #
17 # (string) smshi_sid
18 # Twilio SID
19 #
20 # (string) smshi_token
21 # Twilio token
22 #
23 # (string) smshi_from
24 # From number (int'l format, i.e. +12022345678)
25 #
26 # (string) smshi_to
27 # To number (int'l format, i.e. +12022345678)
28 #
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
32 # the highlight):
33 # smshi_away_only OFF
34 # smshi_detached_only ON
35
36 use strict;
37 use warnings;
38
39 use Irssi;
40 use vars qw($VERSION %IRSSI);
41
42 use LWP::UserAgent;
43 my $ua = LWP::UserAgent->new;
44
45 $VERSION = "2.0";
46 $ua->agent("irssi+SMSHi/$VERSION ");
47 %IRSSI = (
48 authors => "John Runyon",
49 name => "smshi",
50 description => "send highlights via Twilio sms",
51 license => 'CC0',
52 url => 'https://github.com/zonidjan/irssi-scripts',
53 contact => 'https://github.com/zonidjan/irssi-scripts/issues'
54 );
55
56 sub got_print {
57 return unless Irssi::settings_get_bool('smshi_active');
58
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
68
69 my $chname = $dest->{window}->get_active_name();
70 my $sms = $server->{tag}."/".$chname.$stripped;
71 _send_sms($sms);
72 }
73
74 sub _is_attached {
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});
80 for (@dirs) {
81 return 1 if -x;
82 }
83 return 0;
84 }
85
86 sub test_sms {
87 _send_sms("This is an SMS test.");
88 }
89
90 sub _send_sms {
91 my $sms = shift;
92
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');
97
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");
102
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";
107 } else {
108 print "Bad!";
109 print $req->url;
110 print $req->content;
111 print $res->status_line;
112 print $res->content;
113 }
114 }
115
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)
124
125 Irssi::signal_add('print text', 'got_print');
126 Irssi::command_bind('testsms', 'test_sms');
127 Irssi::print('%G>>%n '.$IRSSI{name}.' '.$VERSION.' loaded');