Metropoli BBS
VIEWER: wiltime MODE: TEXT (ASCII)
AddExtender ("wwwsk32i.dll")
IntControl (35, 500, 0, 0, 0)
sTitle		= "ClockMan GetTime pgm"
bOK			= @TRUE
sDial		= "<dialupname>"
nMaxRedial	= <numredials>
nRedialDelay= 10
sHost		= "<timeserveraddr>"
sErrDesc	= ""
nTimeout	= 10

;Dial our host (unless user is on a direct connect)...
hConn = 0
nNumRedials = 0
if (sDial <> "")
	:DialIt
	hConn = DUNConnect (sDial)
	nErr = SGetLastErr ()
	if (!hConn)
		switch nErr
		case @SErrNotFound
			sErrDesc = "Couldn't connect to %sDial%: no such dial-up"
			break
		case @SErrBusy
			if (nNumRedials <= nMaxRedial)
				nNumRedials = nNumRedials + 1
				Delay (nRedialDelay)
				goto DialIt
			else
				sErrDesc = "Couldn't connect to %sDial%: Line busy"
			endif
			break
		case @SErrNoAnswer
			if (nNumRedials <= nMaxRedial)
				nNumRedials = nNumRedials + 1
				Delay (nRedialDelay)
				goto DialIt
			else
				sErrDesc = "Couldn't connect to %sDial%: No answer"
			endif
			break
		case @SErrVoice
			sErrDesc = "Couldn't connect to %sDial%: A human answered"
			break
		case nErr ; <--default
			sErrDesc = "Couldn't connect to %sDial% - error %nErr%"
		endswitch
		bOK = @FALSE
		goto LogIt
	else
		if (nErr == @SAlready)
			; We're already connected. Don't hang up when this event is thru...
			hConn = 0 
		endif
	endif
endif


; Create a socket...
hSock = SOpen ()
if (hSock==@SErrSocket)
    nErr = SGetLastErr ()
    sErrDesc = "Couldn't create socket - error %nErr%"
    bOK = @FALSE
    goto HangUp
endif

; Connect it up...
nRet = SConnect (hSock, sHost, "time")
if (nRet <> @TRUE)
    nErr = SGetLastErr ()
    switch nErr
        case @SErrHostName
        sErrDesc = "Host ""%sHost%"" not found. (Misspelled, or no longer exists.)"
        break

        case 10060
        sErrDesc = "Timed out trying to connect to ""%sHost%""."
        break

        case @SErrBusy
        sErrDesc = "Couldn't connect to ""%sHost%"".%@CRLF%(Server busy.)"
        break

        case @SErrNoConn
        sErrDesc = "Couldn't connect to ""%sHost%"".%@CRLF%(Server down, or they don't offer a time server.)"
        break

        case nErr ; <--default
        sErrDesc = "Couldn't connect to ""%sHost%"" - WinSock error %nErr%"
    endswitch

    bOK = @FALSE
    goto CloseSocket
endif

; Wait up to nTimeout seconds for data to arrive...
nTimeEnd = CMGetSysTime() + nTimeout
while (!SOK2Recv(hSock, 4))
    if (CMGetSysTime() > nTimeEnd)
        bOK = @FALSE
        break
    endif
    Yield ()
endwhile

if (bOK)
    dwRawTime = SRecvNum32 (hSock)
    dwPrevTime = CMGetSysTime ()
    dwNewTime = dwRawTime - 2208988800; Convert to secs since 1/1/70 from 1/1/00
    CMSetSysTime (dwNewTime)
else
    sErrDesc = "No response from time server @ ""%sHost%""."
endif

; If user hit Ctrl+Break, WIL will bring us here...
:Cancel

; Close the socket...
:CloseSocket
nRet = SClose (hSock)

; Hang up...
:HangUp
if (hConn)
    nRet = DUNDisconnect (hConn)
endif


; Log what we did...
:LogIt
if bOK == @TRUE
    ; Finally, log the adjustment...
    dwOffset = dwNewTime - dwPrevTime
    if (dwOffset > 0)
        sLog = "Turned the clock forward by %dwOffset% seconds"
    else
        if (dwOffset < 0)
            dwOffset = -dwOffset
            sLog = "Turned the clock back by %dwOffset% seconds"
        else
            sLog = "No adjustment needed to the system clock!"
        endif
    endif
else
    sLog = strcat ("Error attempting to adjust time:", @CRLF, sErrDesc)
endif

CMLogMessage (sLog)
Display (5, sTitle, sLog)
~
[ RETURN TO DIRECTORY ]