]> jfr.im git - irssi-scripts.git/blob - smshi.pl
smshi - also send for PMs which use nick
[irssi-scripts.git] / smshi.pl
1 use Irssi;
2 use vars qw($VERSION %IRSSI);
3
4 use LWP::UserAgent;
5 my $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
18 sub 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};
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>/);
27 return if (!$server->{usermode_away} && Irssi::settings_get_bool('smshi_away_only'));
28
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
38 my $chname = $dest->{window}->get_active_name();
39 my $sms = $server->{tag}."/".$chname.$msg;
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);
52 return unless Irssi::settings_get_bool('smshi_debug');
53 if ($res->is_success) {
54 print "Good. Sent to $to from $from: $sms";
55 } else {
56 print $req->url;
57 print $req->content;
58 print $res->status_line;
59 print $res->content;
60 }
61 }
62
63 Irssi::settings_add_bool('smshi', 'smshi_active', 0);
64 Irssi::settings_add_bool('smshi', 'smshi_away_only', 1);
65 Irssi::settings_add_bool('smshi', 'smshi_debug', 0);
66 Irssi::settings_add_str('smshi', 'smshi_sid', '');
67 Irssi::settings_add_str('smshi', 'smshi_token', '');
68 Irssi::settings_add_str('smshi', 'smshi_from', '');
69 Irssi::settings_add_str('smshi', 'smshi_to', '');
70
71 Irssi::signal_add('print text', 'msg');
72 Irssi::print('%G>>%n '.$IRSSI{name}.' '.$VERSION.' loaded');