]>
Commit | Line | Data |
---|---|---|
a40c10bb | 1 | # smshi - sends highlights via SMS, using Twilio |
a9e4d9f2 | 2 | # CC0 https://creativecommons.org/publicdomain/zero/1.0/ |
3 | ||
4 | use strict; | |
5 | use warnings; | |
a40c10bb | 6 | |
3f24f7e4 | 7 | use Irssi; |
8 | use vars qw($VERSION %IRSSI); | |
9 | ||
10 | use LWP::UserAgent; | |
11 | my $ua = LWP::UserAgent->new; | |
3f24f7e4 | 12 | |
a9e4d9f2 | 13 | $VERSION = "1.0"; |
14 | $ua->agent("irssi+SMSHi/$VERSION "); | |
3f24f7e4 | 15 | %IRSSI = ( |
6b5063e9 | 16 | authors => "John Runyon", |
17 | name => "smshi", | |
18 | description => "send highlights via Twilio sms", | |
a9e4d9f2 | 19 | license => 'CC0', |
6b5063e9 | 20 | url => 'https://github.com/zonidjan/irssi-scripts', |
21 | contact => 'https://github.com/zonidjan/irssi-scripts/issues' | |
3f24f7e4 | 22 | ); |
23 | ||
a40c10bb | 24 | sub got_print { |
3f24f7e4 | 25 | return unless Irssi::settings_get_bool('smshi_active'); |
26 | ||
27 | my ($dest, $text, $stripped) = @_; | |
3f24f7e4 | 28 | my $server = $dest->{server}; |
5ba17b0c | 29 | my $mynick = $server->{nick}; |
a40c10bb | 30 | return unless ($dest->{level} & MSGLEVEL_HILIGHT) # continue if hilight... |
31 | or ($dest->{level} & MSGLEVEL_MSGS && index($stripped, $mynick) != -1); # or if it's a PM containing my nick | |
32 | return if $stripped =~ /<.?\Q$mynick\E>/; # avoid people quoting me | |
33 | return if (!$server->{usermode_away} && Irssi::settings_get_bool('smshi_away_only')); # and obey away_only | |
3f24f7e4 | 34 | |
3f24f7e4 | 35 | my $msg = ''; |
36 | for my $c (split //, $stripped) { | |
37 | if (ord($c) > 31 && ord($c) < 127) { | |
38 | $msg .= $c; | |
39 | } else { | |
40 | $msg .= '\\x'.sprintf("%02x", ord($c)); | |
41 | } | |
42 | } | |
43 | ||
5ba17b0c | 44 | my $chname = $dest->{window}->get_active_name(); |
45 | my $sms = $server->{tag}."/".$chname.$msg; | |
a40c10bb | 46 | _send_sms($sms); |
47 | } | |
48 | sub test_sms { | |
49 | _send_sms("This is an SMS test."); | |
50 | } | |
51 | sub _send_sms { | |
52 | my $sms = shift; | |
3f24f7e4 | 53 | |
54 | my $sid = Irssi::settings_get_str('smshi_sid'); | |
55 | my $token = Irssi::settings_get_str('smshi_token'); | |
56 | my $from = Irssi::settings_get_str('smshi_from'); | |
57 | my $to = Irssi::settings_get_str('smshi_to'); | |
58 | ||
59 | my $url = "https://$sid:$token\@api.twilio.com/2010-04-01/Accounts/$sid/Messages.json"; | |
60 | my $req = HTTP::Request->new('POST', $url); | |
61 | $req->content_type('application/x-www-form-urlencoded'); | |
62 | $req->content("To=$to&From=$from&Body=$sms"); | |
63 | ||
64 | my $res = $ua->request($req); | |
7e1f9f6b | 65 | return unless Irssi::settings_get_bool('smshi_debug'); |
3f24f7e4 | 66 | if ($res->is_success) { |
7e1f9f6b | 67 | print "Good. Sent to $to from $from: $sms"; |
3f24f7e4 | 68 | } else { |
a40c10bb | 69 | print "Bad!"; |
3f24f7e4 | 70 | print $req->url; |
71 | print $req->content; | |
72 | print $res->status_line; | |
73 | print $res->content; | |
74 | } | |
75 | } | |
76 | ||
65c68420 | 77 | Irssi::settings_add_bool('smshi', 'smshi_active', 0); # master switch |
78 | Irssi::settings_add_bool('smshi', 'smshi_away_only', 1); # send only when away? | |
79 | Irssi::settings_add_bool('smshi', 'smshi_debug', 0); # show debugging info | |
80 | Irssi::settings_add_str('smshi', 'smshi_sid', ''); # Twilio SID | |
81 | Irssi::settings_add_str('smshi', 'smshi_token', ''); # Twilio token | |
82 | Irssi::settings_add_str('smshi', 'smshi_from', ''); # From number (+12022345678) | |
83 | Irssi::settings_add_str('smshi', 'smshi_to', ''); # To number (+12022345678) | |
3f24f7e4 | 84 | |
a40c10bb | 85 | Irssi::signal_add('print text', 'got_print'); |
86 | Irssi::command_bind('testsms', 'test_sms'); | |
3f24f7e4 | 87 | Irssi::print('%G>>%n '.$IRSSI{name}.' '.$VERSION.' loaded'); |