]>
Commit | Line | Data |
---|---|---|
a40c10bb | 1 | # smshi - sends highlights via SMS, using Twilio |
a9e4d9f2 | 2 | # CC0 https://creativecommons.org/publicdomain/zero/1.0/ |
3 | ||
24be0b3b | 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 | ||
a9e4d9f2 | 36 | use strict; |
37 | use warnings; | |
a40c10bb | 38 | |
3f24f7e4 | 39 | use Irssi; |
40 | use vars qw($VERSION %IRSSI); | |
41 | ||
42 | use LWP::UserAgent; | |
43 | my $ua = LWP::UserAgent->new; | |
3f24f7e4 | 44 | |
24be0b3b | 45 | $VERSION = "2.0"; |
a9e4d9f2 | 46 | $ua->agent("irssi+SMSHi/$VERSION "); |
3f24f7e4 | 47 | %IRSSI = ( |
6b5063e9 | 48 | authors => "John Runyon", |
49 | name => "smshi", | |
50 | description => "send highlights via Twilio sms", | |
a9e4d9f2 | 51 | license => 'CC0', |
6b5063e9 | 52 | url => 'https://github.com/zonidjan/irssi-scripts', |
53 | contact => 'https://github.com/zonidjan/irssi-scripts/issues' | |
3f24f7e4 | 54 | ); |
55 | ||
a40c10bb | 56 | sub got_print { |
3f24f7e4 | 57 | return unless Irssi::settings_get_bool('smshi_active'); |
58 | ||
59 | my ($dest, $text, $stripped) = @_; | |
3f24f7e4 | 60 | my $server = $dest->{server}; |
5ba17b0c | 61 | my $mynick = $server->{nick}; |
24be0b3b | 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 | |
3f24f7e4 | 68 | |
5ba17b0c | 69 | my $chname = $dest->{window}->get_active_name(); |
24be0b3b | 70 | my $sms = $server->{tag}."/".$chname.$stripped; |
a40c10bb | 71 | _send_sms($sms); |
72 | } | |
24be0b3b | 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 | ||
a40c10bb | 86 | sub test_sms { |
87 | _send_sms("This is an SMS test."); | |
88 | } | |
24be0b3b | 89 | |
a40c10bb | 90 | sub _send_sms { |
91 | my $sms = shift; | |
3f24f7e4 | 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); | |
7e1f9f6b | 104 | return unless Irssi::settings_get_bool('smshi_debug'); |
3f24f7e4 | 105 | if ($res->is_success) { |
7e1f9f6b | 106 | print "Good. Sent to $to from $from: $sms"; |
3f24f7e4 | 107 | } else { |
a40c10bb | 108 | print "Bad!"; |
3f24f7e4 | 109 | print $req->url; |
110 | print $req->content; | |
111 | print $res->status_line; | |
112 | print $res->content; | |
113 | } | |
114 | } | |
115 | ||
24be0b3b | 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) | |
3f24f7e4 | 124 | |
a40c10bb | 125 | Irssi::signal_add('print text', 'got_print'); |
126 | Irssi::command_bind('testsms', 'test_sms'); | |
3f24f7e4 | 127 | Irssi::print('%G>>%n '.$IRSSI{name}.' '.$VERSION.' loaded'); |