App-Phoebe
view release on metacpan or search on metacpan
script/gemini-chat view on Meta::CPAN
use URI::Escape;
use IRI;
my $cert;
my $key;
my $help;
my $listen_url;
my $say_url;
GetOptions(
'help' => \$help,
'cert_file=s' => \$cert,
'key_file=s' => \$key)
or die("Error in command line arguments\n");
# Help
if ($help) {
my $parser = Pod::Text->new();
$parser->parse_file($0);
exit;
}
die "â You must provide an existing certificate and key file\n"
unless $cert and $key and -f $cert and -f $key;
# Regular arguments
decode_argv();
my ($uri) = @ARGV;
die "â You must provide an URI\n" unless $uri;
my $iri = IRI->new(value => encode_utf8 $uri);
die "â The URI '$uri' must use the gemini scheme\n" unless $iri->scheme and $iri->scheme eq 'gemini';
die "â The URI '$uri' must have an authority\n" unless $iri->authority;
warn "â Ignoring path '" . $iri->path . "'\n" if $iri->path;
warn "â Ignoring fragment '" . $iri->fragment . "'\n" if $iri->fragment;
my $host = domain_to_ascii(decode_utf8 $iri->host);
my $port = $iri->port || 1965;
my $unsafe = "^A-Za-z0-9\-\._~"; # the default
my $path = uri_escape_utf8($iri->path, $unsafe . "/"); # path separator are safe
$uri = $iri->scheme . '://' . $host . ':' . $port;
$uri .= $path if $path;
# start read loop for saying stuff
my $term = Term::ReadLine->new($uri);
my $prompt = "> ";
my $OUT = $term->OUT || \*STDOUT;
while (defined ($_ = $term->readline($prompt))) {
exit if $_ eq "quit";
# create client
my $text = uri_escape_utf8($_);
Mojo::IOLoop->client({
address => $host,
port => $port,
tls => 1,
tls_cert => $cert,
tls_key => $key,
tls_options => { SSL_verify_mode => 0x00 }} => sub {
my ($loop, $err, $stream) = @_;
die $err if $err;
$stream->on(read => sub {
my ($stream, $bytes) = @_;
if ($bytes =~ /^[123]/) {
# Do nothing
} else {
# Print server result
print "\e[31m$bytes\e[0m"; # red
}});
# Write request to the server
$stream->write("$uri?$text\r\n")});
# Start event loop if necessary
Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
# Add to history
$term->addhistory($_) if /\S/;
}
( run in 0.500 second using v1.01-cache-2.11-cpan-99c4e6809bf )