]> jfr.im git - irssi-scripts.git/blob - smshi.pl
smshi - document settings
[irssi-scripts.git] / smshi.pl
1 # smshi - sends highlights via SMS, using Twilio
2
3 use Irssi;
4 use vars qw($VERSION %IRSSI);
5
6 use LWP::UserAgent;
7 my $ua = LWP::UserAgent->new;
8 $ua->agent("irssi+SMSHi/1.0 ");
9
10 $VERSION = "0.1";
11 %IRSSI = (
12 authors => "John Runyon",
13 name => "smshi",
14 description => "send highlights via Twilio sms",
15 license => 'public domain',
16 url => 'https://github.com/zonidjan/irssi-scripts',
17 contact => 'https://github.com/zonidjan/irssi-scripts/issues'
18 );
19
20 sub got_print {
21 return unless Irssi::settings_get_bool('smshi_active');
22
23 my ($dest, $text, $stripped) = @_;
24 my $server = $dest->{server};
25 my $mynick = $server->{nick};
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
30
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
40 my $chname = $dest->{window}->get_active_name();
41 my $sms = $server->{tag}."/".$chname.$msg;
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;
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);
61 return unless Irssi::settings_get_bool('smshi_debug');
62 if ($res->is_success) {
63 print "Good. Sent to $to from $from: $sms";
64 } else {
65 print "Bad!";
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); # master switch
74 Irssi::settings_add_bool('smshi', 'smshi_away_only', 1); # send only when away?
75 Irssi::settings_add_bool('smshi', 'smshi_debug', 0); # show debugging info
76 Irssi::settings_add_str('smshi', 'smshi_sid', ''); # Twilio SID
77 Irssi::settings_add_str('smshi', 'smshi_token', ''); # Twilio token
78 Irssi::settings_add_str('smshi', 'smshi_from', ''); # From number (+12022345678)
79 Irssi::settings_add_str('smshi', 'smshi_to', ''); # To number (+12022345678)
80
81 Irssi::signal_add('print text', 'got_print');
82 Irssi::command_bind('testsms', 'test_sms');
83 Irssi::print('%G>>%n '.$IRSSI{name}.' '.$VERSION.' loaded');