]> jfr.im git - irssi-scripts.git/blob - smshilight.pl
658aa5095cd65d6af0c365138234d590fcefe366
[irssi-scripts.git] / smshilight.pl
1 # smshi - sends highlights via SMS, using Twilio
2 # CC0 https://creativecommons.org/publicdomain/zero/1.0/
3
4 use strict;
5 use warnings;
6
7 use Irssi;
8 use vars qw($VERSION %IRSSI);
9
10 use LWP::UserAgent;
11 my $ua = LWP::UserAgent->new;
12
13 $VERSION = "1.0";
14 $ua->agent("irssi+SMSHi/$VERSION ");
15 %IRSSI = (
16 authors => "John Runyon",
17 name => "smshi",
18 description => "send highlights via Twilio sms",
19 license => 'CC0',
20 url => 'https://github.com/zonidjan/irssi-scripts',
21 contact => 'https://github.com/zonidjan/irssi-scripts/issues'
22 );
23
24 sub got_print {
25 return unless Irssi::settings_get_bool('smshi_active');
26
27 my ($dest, $text, $stripped) = @_;
28 my $server = $dest->{server};
29 my $mynick = $server->{nick};
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
34
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
44 my $chname = $dest->{window}->get_active_name();
45 my $sms = $server->{tag}."/".$chname.$msg;
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;
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);
65 return unless Irssi::settings_get_bool('smshi_debug');
66 if ($res->is_success) {
67 print "Good. Sent to $to from $from: $sms";
68 } else {
69 print "Bad!";
70 print $req->url;
71 print $req->content;
72 print $res->status_line;
73 print $res->content;
74 }
75 }
76
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)
84
85 Irssi::signal_add('print text', 'got_print');
86 Irssi::command_bind('testsms', 'test_sms');
87 Irssi::print('%G>>%n '.$IRSSI{name}.' '.$VERSION.' loaded');