#!/usr/bin/perl
# $File: //depot/OurNet-BBS/bin/bbscomd $ $Author: autrijus $
# $Revision: #14 $ $Change: 1221 $ $DateTime: 2001/06/19 04:19:25 $

$VERSION = '1.55-dev';
$REVISION = "rev$1\[\@$2\]" 
    if ('$Revision: #14 $ $Change: 1221 $' =~ /(\d+)[^\d]+(\d+)/);

=head1 NAME

bbscomd - OurNet BBS Remote Access Daemon

=head1 SYNOPSIS

    % bbscomd [-acdfh] [-p port] [-u key] <backend> <args>...
    % bbscomd -acf -u melix MELIX /home/melix 2997 350

=head1 DESCRIPTION

The bbscomd starts a I<OurNet::BBS::Server> daemon listening on 
the specified port (default 7978). Remote users could then start
using I<OurNet::BBS::Client> to connect, e.g.:

    use OurNet::BBS;

    my $Remote_BBS = OurNet::BBS->new('OurNet', 'remote.org');

If the C<-f> flag is specified, bbscomd will fork a new process
to run as daemon. The C<-d> flag turns on debugging.

The C<-u> specifies the pgp keyid or userid used in authorization. 
If C<-a> is supplied, the server will serve in 'authentication' 
mode with additional permission controls. Similarly, C<-c> 
disallows insecure cipher modes.

Please refer to L<OurNet::BBS> modules for more information on
usages.

=cut

use strict;
use warnings;
use OurNet::BBS;
use Getopt::Std;
use Term::ReadKey;

my %args;

if (!@ARGV) {
    die << ".";

OurNet BBS Remote Access Daemon v$main::VERSION-$main::REVISION

Usage: $0 [-acdfh] [-p port] [-u key] 
       <backend> <args>...

Type '$0 -h' to see available argument and options.

Copyright 2001 by Autrijus Tang <autrijus\@autrijus.org>.

This program is free software; you can redistribute it and/or 
modify it under the same terms as Perl itself.

See <http://www.perl.com/perl/misc/Artistic.html>.

.
}

getopts('acp:u:fdh', \%args);
exec('perldoc', $0) if defined $args{h};

my ($auth, $ciph, $port, $key, $fork, $debug) = @{args}{qw/a c p u f d/};

$auth = 6 if defined $auth;
$ciph = $key ? 6 : 2 if defined $ciph;
$port ||= 7978;

my $BBS = OurNet::BBS->new(@ARGV) or die "Cannot link to BBS: @ARGV\n";

$OurNet::BBS::DEBUG = $debug;

print "entering in debug mode, expect lots of outputs\n" 
    if $OurNet::BBS::DEBUG;

my $pass = '';

if ($key) {
    ReadMode('noecho');
    print "enter passphrase for <$key>: ";
    $pass = scalar <STDIN>;
    ReadMode('restore');
    print "\n";
}

if (!$fork or !fork()) {
    print "BBSCOM Daemon starting at port $port...\n";

    $BBS->daemonize($port, $key, $pass, $ciph, $auth)
	or die "Failed to daemonize: $!\n";
}

__END__

=head1 AUTHORS

Autrijus Tang E<lt>autrijus@autrijus.org>

=head1 COPYRIGHT

Copyright 2001 by Autrijus Tang E<lt>autrijus@autrijus.org>.

This program is free software; you can redistribute it and/or 
modify it under the same terms as Perl itself.

See L<http://www.perl.com/perl/misc/Artistic.html>

=cut
