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