#!/usr/bin/perl
# $File: //depot/OurNet-BBS/bin/bbscomd $ $Author: autrijus $
# $Revision: #20 $ $Change: 1681 $ $DateTime: 2001/09/03 22:41:51 $

$VERSION = '1.6';
$REVISION = "rev$1\[\@$2\]" 
    if ('$Revision: #20 $ $Change: 1681 $' =~ /(\d+)[^\d]+(\d+)/);

=head1 NAME

bbscomd - OurNet BBS Remote Access Daemon

=head1 SYNOPSIS

    % bbscomd [-acdfghx] [-b addr] [-p port] [-u key] <backend> <args>...
    % bbscomd -acfg -u melix MELIX /home/melix 2997 350

=head1 DESCRIPTION

The bbscomd starts a I<OurNet::BBS::Server> daemon listening on 
the specified port (default 7979). 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.

The C<-g> flag allows server to assume 'guest' on a failed
Authentication mode, with permission controls bits.

The C<-x> flag assumes default settings on Win32. It's not meant
to be used on other platforms.

If you don't want to bind all available IPs, specify one using
the C<-b> flag.

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

=cut

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

$|++;

no warnings 'misc'; # see //depot/ebx/build/makeebx.bat for why

my %args;

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

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

Usage: $0 [-acdfh] [-b addr] [-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('acgb:p:u:fdhx', \%args);
exec('perldoc', $0) if defined $args{h};

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

$guest = $guest ? 'guest' : undef;
$auth  = defined($auth) ? $guest ? 7 : 6 : 0;
$ciph  = $key ? 6 : 2 if defined $ciph;
$port  ||= 7979;

if ($args{x}) {
    @ARGV = (
        'MELIX', -e 'c:/cygwin/home/melix' 
	    ? 'c:/cygwin/home/melix' 
	    : 'c:/program files/melix/home/melix'
    );
}
    
no warnings 'once';
$OurNet::BBS::Server::LocalAddr = $addr if defined $addr;
$OurNet::BBS::DEBUG = $debug;

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

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, $guest)
	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
