/
LIB3/
LIB3/D/ADMIN/
LIB3/D/ADMIN/OBJ/
LIB3/D/ADMIN/ROOM/W/
LIB3/D/HOME/
LIB3/D/HOME/CITY/ARENA/
LIB3/D/HOME/CITY/ITEMS/
LIB3/D/HOME/CITY/POSTOFFI/
LIB3/DOC/
LIB3/GLOBAL/SPECIAL/
LIB3/GLOBAL/VIRTUAL/
LIB3/NET/
LIB3/NET/CONFIG/
LIB3/NET/DAEMON/CHARS/
LIB3/NET/GOPHER/
LIB3/NET/INHERIT/
LIB3/NET/OBJ/
LIB3/NET/SAVE/
LIB3/NET/VIRTUAL/
LIB3/OBJ/B_DAY/
LIB3/OBJ/HANDLERS/TERM_TYP/
LIB3/PLAYERS/B/
LIB3/PLAYERS/N/
LIB3/ROOM/
LIB3/SAVE/
LIB3/SAVE/BOARDS/
LIB3/SAVE/ENVIRON/
LIB3/SAVE/POST/
LIB3/STD/COMMANDS/SHADOWS/
LIB3/STD/CREATOR/
LIB3/STD/DOM/
LIB3/STD/EFFECTS/
LIB3/STD/EFFECTS/HEALING/
LIB3/STD/EFFECTS/OTHER/
LIB3/STD/EFFECTS/POISONS/
LIB3/STD/ENVIRON/
LIB3/STD/GUILDS/
LIB3/STD/LIQUIDS/
LIB3/STD/ROOM/
LIB3/STD/TRIGGER/SHADOW/
LIB3/W/
LIB3/W/BANNOR/
LIB3/W/NEWSTYLE/
/* a file downloader
 */
inherit "std/object";
#define THIS_DIR this_player()->query_current_path() + "/"

#define TRUE 1
#define FALSE 0

object  ob;
string  file, title;
int     expand_line, local_quiet, first_loop;


/* Function prototypes */
void    setup();
void    init();
static  expandline();
static  quiet();
int     dl( string str );
static  save_func( string str );
static  loop();


void    setup()
{
    set_name( "downloader" );
    set_main_plural( "of Bannor's downloaders" );
    add_plural( "downloaders" );
    add_alias( "dnl" );
    set_short( "Bannor's downloader" );
}				/* setup() */


string long( string str, int dark )
{
    return( 
	       "A network file receiver.  Made by Bannor.\n" +
	       "To download a file to the mud type: dl <filename>\n" +
	       "You can then have your comm program do a line by line handshake.\n" +
	       "A '@' will be sent when the down loader is ready for the next line\n" +
	       "of input.\n\n" +
	       "Other commands:\n" +
	       "    quiet: toggle active downloading say to the room.\n" +
	       "    expandline: toggle end of file from blank line to/from\n" +
	       "                \"**END**\"\n\n" +
	       "Current status: " +
	       ((local_quiet) ? "Dnl will NOT echo to the room.\n" :
		"Dnl will echo to the room.\n") +
	       "                Terminate download with " +
	       ((expand_line) ? "**END**\n" : "a blank line.\n") +
	       "\n" );

}				/* long() */


void    init()
{
    ::init();
    seteuid( geteuid( this_player() ) );
    if( this_player( 1 )->query_creator() )
    {
	add_action( "quiet", "quiet" );
	add_action( "expandline", "expandline" );
	add_action( "dl", "dl" );
	local_quiet = TRUE;
    }
    reset_drop();
}				/* init() */


static  expandline()
{
    if( expand_line == TRUE )
    {
	expand_line = FALSE;
	write( "Dnl: setting terminate download with a blank line.\n" );
    }
    else
    {
	expand_line = TRUE;
	write( "Dnl: setting terminate download with \"**END**\".\n" );
    }
    return 1;
}				/* expandline() */


static  quiet()
{
    if( local_quiet == TRUE )
    {
	local_quiet = FALSE;
	write( "The downloader will echo to the room.\n" );
    }
    else
    {
	local_quiet = TRUE;
	write( "The downloader will not echo to the room.\n" );
    }
    return 1;
}				/* quiet() */


int     dl( string str )
{
    string  backup;

    if( !str )
    {
	write( "Down load what?\n" );
	return 1;
    }
    seteuid( geteuid( this_player() ) );
    first_loop = TRUE;
    file = THIS_DIR + str;

    if( !"/secure/master"->valid_write( file, this_player( 1 ), "dl" ) )
    {
	write( "Sorry, you do not have write permision for this file.\n" );
	return( 1 );
    }

    backup = file + ".bak";
    write( "%^BOLD%^Receiving: " + file + "%^RESET%^\n" );
    title = this_player()->query_title();
    this_player()->set_title( "*** is using Bannor's downloader ***" );

    if( read_file( backup, 1 ) )
	rm( backup );
    if( read_file( file, 1 ) )
	rename( file, backup );
    loop();
    return 1;
}				/* dl() */

static  save_func( string str )
{
    if( ((str == "") && (!expand_line)) ||
	    ((str == "**END**") && (expand_line)) )
    {
	/* all done */
	this_player()->set_title( title );
	return 1;
    }
    str = str + "\n";		/* input_to() strips following \n, so stick it back */
    write_file( file, str );
    if( first_loop == TRUE )
    {
	/* this adds one line of 'buffering' for telnet */
	first_loop = FALSE;
	write( "@" );
    }
    loop();
}				/* save_func() */

static  loop()
{
    if( !local_quiet )
	say( "Please wait (silently), i'm downloading.\n" );
    write( "@" );
    input_to( "save_func" );
}				/* loop() */