Created
August 11, 2017 21:08
-
-
Save oki/800c15e7d5d622d810dbbd7dc99ad76c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use strict; | |
use warnings; | |
use IO::Socket::UNIX; | |
$|++; | |
my $SOCK_PATH = "/tmp/unix-domain-socket-test.sock"; | |
unlink $SOCK_PATH; | |
use Socket; | |
use IO::Handle; | |
socket(SERV, PF_UNIX, SOCK_STREAM,0); | |
unlink $SOCK_PATH; | |
bind(SERV,sockaddr_un($SOCK_PATH)) or print "ERROR!"; | |
listen(SERV,1); | |
while (accept(CLIENT,SERV)) { | |
if (fork() == 0) { | |
CLIENT->autoflush(1); | |
print CLIENT "Welcome.\n"; | |
while (my $line = <CLIENT>) { | |
print "$line"; | |
chomp($line); | |
if ($line eq "ping") { | |
print CLIENT "pong!\n"; | |
} else { | |
print CLIENT "THX for [$line]\n"; | |
} | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use strict; | |
use warnings; | |
use Irssi; | |
use POSIX; | |
use Time::HiRes qw/sleep/; | |
use IO::Socket::UNIX; | |
use JSON; | |
$|++; | |
my $SOCK_PATH = "/tmp/unix-domain-socket-test.sock"; | |
my $socket = IO::Socket::UNIX->new( | |
Type => SOCK_STREAM(), | |
Peer => $SOCK_PATH, | |
); | |
die "Can't create socket: $!" unless $socket; | |
my $child_pid; | |
sub push_to_socket { | |
my ($socket, $data) = @_; | |
print $socket encode_packet($data); | |
print $socket "\n"; | |
} | |
sub socket_handler { | |
my $json = shift; | |
Irssi::print($json, Irssi::MSGLEVEL_CLIENTCRAP); | |
# decode json from string | |
# push command to irssi | |
} | |
sub pipe_and_fork { | |
my ($read_handle, $write_handle); | |
pipe($read_handle, $write_handle); | |
my $oldfh = select($write_handle); | |
$| = 1; | |
select $oldfh; | |
$child_pid = fork(); | |
if (not defined $child_pid) { | |
_error("Can't fork: Aborting"); | |
close($read_handle); | |
close($write_handle); | |
return; | |
} | |
if ($child_pid > 0) { # this is the parent (Irssi) | |
close ($write_handle); | |
Irssi::pidwait_add($child_pid); | |
my $job = $child_pid; | |
my $tag; | |
my @args = ($read_handle, \$tag, $job); | |
$tag = Irssi::input_add(fileno($read_handle), | |
Irssi::INPUT_READ, | |
\&child_input, | |
\@args); | |
} else { # child | |
print $write_handle "Waiting for data...\n"; | |
print $socket "ping\n"; | |
while (my $line = <$socket>) { | |
print $write_handle $line; | |
} | |
print $write_handle "__DONE__\n"; | |
close $write_handle; | |
POSIX::_exit(1); | |
} | |
} | |
sub child_input { | |
my $args = shift; | |
my ($read_handle, $input_tag_ref, $job) = @$args; | |
my $data = <$read_handle>; | |
if ($data =~ m/__DONE__/) { | |
close($read_handle); | |
Irssi::input_remove($$input_tag_ref); | |
_msg("child finished"); | |
} else { | |
socket_handler($data); | |
# _msg("Received from child: $data"); | |
} | |
} | |
sub _error { | |
my ($msg) = @_; | |
my $win = Irssi::active_win(); | |
$win->print($msg, Irssi::MSGLEVEL_CLIENTERROR); | |
} | |
sub encode_packet { | |
my $data = shift; | |
my $json = JSON->new->encode($data); | |
my $len = length $json; | |
print ( CRAP "JSON: $json"); | |
return "$len#$json"; | |
} | |
sub sig_msg_public { | |
my ($server, $msg, $nick, $address, $target) = @_; | |
my %data = ( | |
chatnet => $server->{chatnet}, | |
address => $server->{address}, | |
msg => $msg, | |
nick => $nick, | |
target => $target, | |
address => $address | |
); | |
push_to_socket($socket, \%data); | |
} | |
Irssi::signal_add("message public", \&sig_msg_public); | |
&pipe_and_fork(); | |
sub UNLOAD { | |
Irssi::print "Unloading..."; | |
kill 9, $child_pid; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment