/*
 * Poll_GMS
 *
 * USAGE: ret = 'mail/Poll_GMS'(method,args,...)
 *
 * Poll_GMS is an ARexx interface to GMS...
 *
 * REQUIREMENTS:
 *  GMS
 *  REXX:dos/error
 *
 * $(C): (1995, Rocco Coluccelli, Bologna)
 * $VER: Poll_GMS 0.10 (11.Nov.1996)
 */

OPTIONS RESULTS

SIGNAL ON ERROR
SIGNAL ON SYNTAX
SIGNAL ON BREAK_C
SIGNAL ON HALT

lib = "gms_mailer_rexx.library"; IF ~SHOW('L',lib) THEN CALL ADDLIB(lib,0,-30)
lib = "rexxsupport.library"; IF ~SHOW('L',lib) THEN CALL ADDLIB(lib,0,-30)

/*
 *	(OPEN)
 *	(CALL,nodes,retries delay line,beforecmd,aftercmd)
 *		node.1.1 | node.2.1 node.2.2 ... | node.n.1 ...
 *		line retries delay
 *		before session command
 *		after session command
 *	(POLL...)
 *		Same as CALL, but close the mailer before exit...
 *	(QUIT)
 */
method = UPPER(ARG(1))

PARSE VALUE ARG(3) WITH n_retr n_wait n_line .
IF n_line = "" THEN DO
	n_retr = 10; n_wait = 10; n_line = 0; END

is_open = gms(n_line)
IF is_open THEN CALL GMS_Mailer_System_Alloc("gms_mailer",n_line,'nonew')

IF method = "QUIT" THEN CALL quit(0)

/*
 *	Try to start GMS...
 */
IF ~is_open THEN CALL GMS_Mailer_System_Alloc("gms_mailer",n_line,'nolock')
IF method = "OPEN" THEN CALL exit(0)


/*
 *	Is GMS ready to start? ...
 */
CALL GMS_Mailer_Status_Report("gms_mailer",'async')
DO i = 1 WHILE (result ~= "IDLE") & (i < 120)
	ADDRESS COMMAND 'Wait 1'
	CALL GMS_Mailer_Status_Report("gms_mailer",'async')
END
IF result ~= "IDLE" THEN CALL exit(20)


IF method = "CALL" | method = "POLL" THEN DO
	nodes = COMPRESS(ARG(2),"-")

	DO UNTIL nodes = ""
		INTERPRET ARG(4)		/* Before Session */
		PARSE VAR nodes numbers "|" nodes
		ret = poll(numbers,n_retr,n_wait)
		INTERPRET ARG(5)		/* After Session */
		IF ret = 0 THEN nodes = nodes "|" numbers
		IF ret > 1 THEN CALL exit(ret)
	END
END
IF method = "POLL" THEN CALL quit(0)
CALL exit(0)


/*
 *	exit(5)		incoming call
 *	exit(10)	general error
 */
poll: PROCEDURE
	DO i = 1 TO ARG(2)
		CALL GMS_Mailer_Modem_Reset("gms_mailer")
		DO n = 1 TO WORDS(ARG(1)); number = WORD(ARG(1),n)
			IF DATATYPE(number,'N') THEN
				CALL GMS_Mailer_Call_Number("gms_mailer",number,0,0)
			ELSE
				CALL GMS_Mailer_Call_Node("gms_mailer",number,0,0)

			stat = GMS_Mailer_Status("gms_mailer")
			IF stat = 0 THEN BREAK
			IF FIND("1 10 12",stat) > 0 THEN CALL exit(10)
			IF FIND("6",stat) > 0 THEN CALL exit(5)
		END
		IF stat = 11 THEN RETURN 0
		IF stat = 5 THEN ADDRESS COMMAND 'Wait' ARG(3)
	END
	RETURN 1
/*
 *	GMS status report
 *
x	 0:  All ok.
x	 1:  User break.
	 2:  Remote system do not respond.
	 3:  Account limit.
	 4:  Baudrate too low.
x	 5:  BUSY
x	 6:  VOICE
	 7:  NO DIALTONE
	 8:  NO CARRIER
	 9:  RINGING limit (NO ANSWER)
x	10: ERROR
x	11: CONNECT
x	12: Serial device owned
 *
 */


gms: PROCEDURE
	PARSE VALUE SHOWLIST('W',,"|")"|" WITH "GMS Mailer" . ": Line " n "|"
	RETURN (n ~= "") & (n = ARG(1))


break_c:
halt:
error:
syntax:
	errc = RC
	PARSE SOURCE infln
	CALL 'dos/error'(errc,SIGL,"GMS Mailer:" GMS_Mailer_LastError() || '0a'x || infln,SOURCELINE(SIGL))
	CALL exit(errc)

/*
 *	exit(0)		All done.
 *	exit(5)		WARN
 *	exit(10)	ERROR
 *	exit(20)	FAIL
 */
quit:
	CALL GMS_Mailer_Session_Quit("gms_mailer",'async')
	CALL GMS_Mailer_System_Free("gms_mailer")
exit:
	CALL GMS_Mailer_FreeScriptData()
	EXIT ARG(1)
