]> jfr.im git - irssi-scripts.git/blame - smshi.pl
smshi - also send for PMs which use nick
[irssi-scripts.git] / smshi.pl
CommitLineData
3f24f7e4 1use Irssi;
2use vars qw($VERSION %IRSSI);
3
4use LWP::UserAgent;
5my $ua = LWP::UserAgent->new;
6$ua->agent("SMSHi/1.0 ");
7
8$VERSION = "0.1";
9%IRSSI = (
10 authors => "John Runyon",
11 name => "smshi",
12 description => "send highlights via sms",
13 license => 'public domain',
14 url => 'https://github.com/zonidjan/irssi-scripts',
15 contact => 'https://github.com/zonidjan'
16);
17
18sub msg {
19 return unless Irssi::settings_get_bool('smshi_active');
20
21 my ($dest, $text, $stripped) = @_;
22# my ($server, $msg, $nick, $addr, $target) = @_;
23 my $server = $dest->{server};
5ba17b0c 24 my $mynick = $server->{nick};
25 return unless ($dest->{level} & MSGLEVEL_HILIGHT)
26 or ($dest->{level} & MSGLEVEL_MSGS && index($stripped, $mynick) != -1 && $stripped !~ /<.?\Q$mynick\E>/);
3f24f7e4 27 return if (!$server->{usermode_away} && Irssi::settings_get_bool('smshi_away_only'));
28
3f24f7e4 29 my $msg = '';
30 for my $c (split //, $stripped) {
31 if (ord($c) > 31 && ord($c) < 127) {
32 $msg .= $c;
33 } else {
34 $msg .= '\\x'.sprintf("%02x", ord($c));
35 }
36 }
37
5ba17b0c 38 my $chname = $dest->{window}->get_active_name();
39 my $sms = $server->{tag}."/".$chname.$msg;
3f24f7e4 40
41 my $sid = Irssi::settings_get_str('smshi_sid');
42 my $token = Irssi::settings_get_str('smshi_token');
43 my $from = Irssi::settings_get_str('smshi_from');
44 my $to = Irssi::settings_get_str('smshi_to');
45
46 my $url = "https://$sid:$token\@api.twilio.com/2010-04-01/Accounts/$sid/Messages.json";
47 my $req = HTTP::Request->new('POST', $url);
48 $req->content_type('application/x-www-form-urlencoded');
49 $req->content("To=$to&From=$from&Body=$sms");
50
51 my $res = $ua->request($req);
7e1f9f6b 52 return unless Irssi::settings_get_bool('smshi_debug');
3f24f7e4 53 if ($res->is_success) {
7e1f9f6b 54 print "Good. Sent to $to from $from: $sms";
3f24f7e4 55 } else {
56 print $req->url;
57 print $req->content;
58 print $res->status_line;
59 print $res->content;
60 }
61}
62
63Irssi::settings_add_bool('smshi', 'smshi_active', 0);
64Irssi::settings_add_bool('smshi', 'smshi_away_only', 1);
7e1f9f6b 65Irssi::settings_add_bool('smshi', 'smshi_debug', 0);
3f24f7e4 66Irssi::settings_add_str('smshi', 'smshi_sid', '');
67Irssi::settings_add_str('smshi', 'smshi_token', '');
68Irssi::settings_add_str('smshi', 'smshi_from', '');
69Irssi::settings_add_str('smshi', 'smshi_to', '');
3f24f7e4 70
71Irssi::signal_add('print text', 'msg');
72Irssi::print('%G>>%n '.$IRSSI{name}.' '.$VERSION.' loaded');