# qnet-ftp.src

# $Id: qnet-ftp.src,v 1.11 2009/11/02 06:26:03 rswindell Exp $

# @format.tab-size 8, @format.use-tabs true

###############################################################################
#	  Baja module for performing FTP-based QWKnet call-outs		      #
#	        Requires Synchronet v3.00d and Baja v2.30		      #
#			Developed 2001 by Rob Swindell 			      #
###############################################################################

# Usage: *QNET-FTP HUBID ADDRESSS PASSWORD port
# Example: *QNET-FTP VERT vert.synchro.net YOURPASS 21

# You may use the qnet-ftp.bin module INSTEAD of your normal ftp/script
# QWKnet call-out routine (exec/vert.bat). You can do this by changing
#
# SCFG->Networks->QWK->Hubs->VERT->Call-out Command Line to:
#
# *QNET-FTP VERT vert.synchro.net YOURPASS port
#
# if you leave the port value blank, this script will default to port 21
#
# The asterisk is important! YOURPASS is your QWKnet password here on Vertrauen. 
#
# No modification or recompilation of this module should be necessary.
#
# Updated on Feb 7th, 2007 to support non standard ftp ports
#  -- Lloyd Hannesson dasme@dasme.org

!include sbbsdefs.inc
!include file_io.inc

# Must be FIRST function call (or module will immediately terminate)
set _online ON_LOCAL

# Variables
int i argc argv sock port flen
str tmp rep qwk hubid addr user password portstr
str cmdline

set _ftp_mode FTP_PASV

# Process command line arguments
set hubid ""
set addr ""
set password ""
set portstr ""
set port 0

# Display command line
copy cmdline str
lprintf "QNET-FTP: %s" cmdline
set argc 0
:process_args
compare_str ""
if_true
	goto end_args
	end_if
sprintf tmp "%.1s" str
shift_str 1
compare tmp " "
if_true
	add argc 1
	goto process_args
	end_if
switch argc
	case 0
		strcat hubid tmp
		end_case
	case 1
		strcat addr tmp
		end_case
	case 2
		strcat password tmp
		end_case
	case 3
		strcat portstr tmp
		end_case
	default
		goto usage
	end_switch
goto process_args

:usage
lprintf "QNET-FTP: !INCORRECT USAGE: %s" cmdline
return

:end_args
compare hubid ""
if_true
	goto usage
	end_if
compare addr ""
if_true
	goto usage
	end_if
compare password ""
if_true
	goto usage
	end_if
copy port portstr
compare port 0
if_true
	# if no Port is passed on the command line, default to 21
	set port 21
	end_if

sprintf rep "%%j%s.rep" hubid
sprintf qwk "%%j%s.qwk" hubid
chkfile qwk
if_true
	lprintf "QNET-FTP: %s already exists!" qwk
	set i 0
	:check_qwk
	sprintf qwk "%%j%s.qw%ld" hubid i
	chkfile qwk
	if_true
		lprintf "QNET-FTP: %s already exists!" qwk
		add i 1
		compare i 10
		if_equal
			goto exit
			end_if
		goto check_qwk
		end_if
	end_if

socket_open sock
if_false
	lprintf "QNET-FTP: !socket_open failure socket_error=%d" _socket_error
	goto exit
	end_if

lprintf "QNET-FTP: Connecting to %s:%u ... " addr port
socket_connect sock addr port 
crlf
if_false
	lprintf "QNET-FTP: !socket_connect failed socket_error=%d" _socket_error
	goto exit
	end_if
lprintf "QNET-FTP: Connected to %s" addr

set user "%q"
lprintf "QNET-FTP: Logging in as '%s' (PW: '%s')" user password
ftp_login sock user password
if_false
	lprintf "QNET-FTP: !ftp_login failed socket_error=%d" _socket_error
	goto exit
	end_if

lprintf "QNET-FTP: Logged in successfully"

chkfile rep
if_true
	###################
	# Send REP packet #
	###################
	lprintf "QNET-FTP: Sending REP packet: %s" rep

	sprintf str "%s.rep" hubid

	ftp_put sock rep str
	if_false
		lprintf "QNET-FTP: !ftp_put failed socket_error=%d\r\n" _socket_error
	else
		lprintf "QNET-FTP: REP packet sent successfully"
		remove_file rep
		end_if
	end_if

#######################
# Download QWK packet #
#######################
sprintf str "%s.qwk" hubid
lprintf "QNET-FTP: Downloading QWK Packet: %s" str
ftp_get sock str qwk
if_true
	get_file_length flen qwk
	compare flen 0
	if_equal
		lprintf "QNET-FTP: Downloaded 0-byte %s" qwk
		remove_file qwk
	else
		lprintf "QNET-FTP: Downloaded %s successfully" str
		end_if
else
	lprintf "QNET-FTP: %s NOT downloaded socket_error=%d (no new messages?)" str _socket_error
	remove_file qwk
	end_if

ftp_logout sock

#################
# Exit routine	#
#################
:exit
lprintf "QNET-FTP: Done."

# End of QNET-FTP.SRC
