use strict; use warnings;

# to read the following docs, you can use "perldoc coords.pl"

=head1 NAME

urlopener_remote - weechat script to open urls on a remote host

=head1 DESCRIPTION

this script can help you to open urls on a local host when running
weechat remotely. however, such setup is not the most
trivial. PLEASE READ:

=head1 SETUP

it requires a three stage setup. on the weechat installation, just
load the script as usual, i.e. by

  /perl load urlopener_remote.pl

on the local computer, you will need to set up an url opening
daemon, which can be done quite easily if you have "netcat"
(sometimes also called "nc") installed. in a terminal window, run

  while true; do xdg-open $(netcat -l -p 23337); done&

if you only have perl, things look a bit more awkward but something
like this should do:

=for comment

 perl -MSocket -E'socket my$s,PF_INET,SOCK_STREAM,getprotobyname"tcp";
                 setsockopt$s,SOL_SOCKET,SO_REUSEADDR,pack"l",1;
                       bind$s,sockaddr_in(23337,INADDR_LOOPBACK);
                     listen$s,SOMAXCONN;
          for(;accept my$c,$s;close$c)
                   {$_=<$c>;chomp;fork==0&&exec"xdg-open",$_}'&

=cut

=pod

now connect to your weechat on a remote server using the following
command:

  ssh -R 23337:localhost:23337 -t YOUR.SSH.SERVER.ADDRESS

the important part here is to connect the ports using the -R
switch. if you now click on a url using the "coords.pl" script, it
will get sent to the netcat above and opened with xdg-open (which is
hopefully your favourite web browser)

=cut

use Socket;

weechat::register('urlopener_remote', 'Nei <anti.teamidiot.de>', '0.0', 'GPL3', 'sends urls to a socket', 'stop_urlopener_remote', '') || return;
weechat::hook_signal('urlopen', 'urlopen_action', '');

init_urlopener_remote();

sub urlopen_action {
	my (undef, undef, $url) = @_;
	socket my $s, PF_INET, SOCK_STREAM, getprotobyname 'tcp';
	connect $s, sockaddr_in 23337, INADDR_LOOPBACK;
	print $s $url, "\015\012";
	weechat::WEECHAT_RC_OK
}

sub init_urlopener_remote {
	weechat::WEECHAT_RC_OK;
}

sub stop_urlopener_remote {
	weechat::WEECHAT_RC_OK;
}
