]> jfr.im git - irssi-scripts.git/blob - smshi.pl
smshi - avoid including quotes
[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);
27 return if $stripped =~ /<.?\Q$mynick\E>/; # avoid quotes
28 return if (!$server->{usermode_away} && Irssi::settings_get_bool('smshi_away_only'));
29
30 my $msg = '';
31 for my $c (split //, $stripped) {
32 if (ord($c) > 31 && ord($c) < 127) {
33 $msg .= $c;
34 } else {
35 $msg .= '\\x'.sprintf("%02x", ord($c));
36 }
37 }
38
39 my $chname = $dest->{window}->get_active_name();
40 my $sms = $server->{tag}."/".$chname.$msg;
41
42 my $sid = Irssi::settings_get_str('smshi_sid');
43 my $token = Irssi::settings_get_str('smshi_token');
44 my $from = Irssi::settings_get_str('smshi_from');
45 my $to = Irssi::settings_get_str('smshi_to');
46
47 my $url = "https://$sid:$token\@api.twilio.com/2010-04-01/Accounts/$sid/Messages.json";
48 my $req = HTTP::Request->new('POST', $url);
49 $req->content_type('application/x-www-form-urlencoded');
50 $req->content("To=$to&From=$from&Body=$sms");
51
52 my $res = $ua->request($req);
53 return unless Irssi::settings_get_bool('smshi_debug');
54 if ($res->is_success) {
55 print "Good. Sent to $to from $from: $sms";
56 } else {
57 print $req->url;
58 print $req->content;
59 print $res->status_line;
60 print $res->content;
61 }
62 }
63
64 Irssi::settings_add_bool('smshi', 'smshi_active', 0);
65 Irssi::settings_add_bool('smshi', 'smshi_away_only', 1);
66 Irssi::settings_add_bool('smshi', 'smshi_debug', 0);
67 Irssi::settings_add_str('smshi', 'smshi_sid', '');
68 Irssi::settings_add_str('smshi', 'smshi_token', '');
69 Irssi::settings_add_str('smshi', 'smshi_from', '');
70 Irssi::settings_add_str('smshi', 'smshi_to', '');
71
72 Irssi::signal_add('print text', 'msg');
73 Irssi::print('%G>>%n '.$IRSSI{name}.' '.$VERSION.' loaded');