Skip to content

Instantly share code, notes, and snippets.

@hikiko
Last active April 27, 2019 17:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hikiko/7405c805149d0441669fcf5667b9f8e6 to your computer and use it in GitHub Desktop.
Save hikiko/7405c805149d0441669fcf5667b9f8e6 to your computer and use it in GitHub Desktop.
perl client for: https://github.com/jtsiomb/phoneline/tree/master/phonelined prints desktop notifications when someone calls at home and allows me to hang up the call from my laptop :D
#!/usr/bin/perl
use strict;
use warnings;
use diagnostics;
use Getopt::Long;
use Gtk2::Notify -init, "pc-notify";
use Socket;
my $notification;
my $hangup = "hangup\n";
my $number = "";
my $server = "192.168.1.222";
my $port = "1111";
my $help;
$| = 1;
# -d <host> -p <port> -h (for help)
GetOptions("s=s" => \$server,
"p=n" => \$port,
"h" => \$help);
if ($help) {
print qq[Help:
-s <server_ip>, default 192.168.1.222
-p <port to connect>, default 1111
-h prints this helps and exits.]."\n";
exit;
}
socket(SOCKET, PF_INET, SOCK_STREAM, (getprotobyname('tcp'))[2])
or die "Could't create a socket $!\n";
connect(SOCKET, pack_sockaddr_in($port, inet_aton($server)))
or die "Can't connect to port $port! \n";
my $sock_fh = \*SOCKET;
my $rin = my $win = my $ein;
my $sel_nfound;
while(1) {
next unless($sock_fh);
vec($rin, fileno($sock_fh), 1) = 1;
vec($win, fileno($sock_fh), 1) = 1;
vec($ein, fileno($sock_fh), 1) = 1;
$ein = $rin | $win;
$sel_nfound = select(my $rout = $rin, my $wout = $win, my $eout = $ein, undef);
unless($sel_nfound != 1) {
while (my $line = <SOCKET>) {
print "$line\n";
if ($line =~m/^ring.*$/) {
$number = "unknown";
if ($line =~ m/^ring (\d+)$/) {
$number = $1;
}
if (!$notification) {
create_notification();
} else {
update_notification();
}
}
}
}
close $sock_fh or die "Failed to close socket: $!\n";
sleep(2);
socket($sock_fh, PF_INET, SOCK_STREAM, (getprotobyname('tcp'))[2]);
connect($sock_fh, pack_sockaddr_in($port, inet_aton($server)));
}
close SOCKET or die "Failed to close socket: $!";
###############################################
sub create_notification {
$notification = Gtk2::Notify->new(
"Annoying call from:",
$number,
);
$notification->add_action('hangup', 'Hang up', \&hangup_cb);
$notification->signal_connect(closed => \&close_cb);
$notification->show;
Gtk2->main;
}
sub update_notification {
if ($number) {
$notification->update("Annoying call from:",
$number,
);
$notification->show;
Gtk2->main;
}
}
sub hangup_cb {
autoflush $sock_fh 1;
print $sock_fh $hangup;
print "a call from $number has been rejected...\n\n";
$notification->close;
Gtk2->main_quit;
}
sub close_cb {
print "$number is still calling...\n\n";
$notification->close;
Gtk2->main_quit;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment