Metropoli BBS
VIEWER: wilexchange MODE: TEXT (ASCII)
AddExtender ("wwwsk32i.dll")
IntControl (35, 500, 0, 0, 0)
bOK = @TRUE
sDial      = "<dialupname>"
nMaxRedial = <numredials>
nRedialDelay = 10
sPgmPath   = "<pgmpath>"
sPgmDir    = "<pgmdir>"
bAllSvcs   = <allservices>
sServices  = "<services>"
sErrDesc   = ""
nWait      = 5 ; How many minutes to wait for Exchange to finish its work
sWinTitle  = "~ - Microsoft Exchange"

;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


; Run Exchange & make it do things...
if (!WinExist(sWinTitle))
	DirChange (sPgmDir)
	Run (sPgmPath, "")
	while (!WinExist(sWinTitle))
		Delay (1)
	endwhile
	Delay (6)
endif

WinActivate (sWinTitle)
sWinTitle = WinGetActive () ; Get actual window name

; Main processing...
if (bAllSvcs)
	SendKeysTo (sWinTitle, "!td~")
	Delay (10)
else
	SendkeysTo (sWinTitle, "!td") ; Force 3rd level menu to be created...
	Delay (1)
	SendkeysTo (sWinTitle, "{esc 3}") ; Back out of menu selection
	Delay (1)

	for nSvc = 1 to ItemCount(sServices,@TAB)
		sService = ItemExtract (nSvc, sServices, @TAB)
		gosub WinSelectMenuItem
		Delay (10)

		; Wait for nWait minutes, or till user kills off Exchange manually...
		for n=1 to nWait * 6
			if (WinExist (sWinTitle))
				Delay (10)
				if (!WinExist(sWinTitle)) then goto Cancel
				MouseMove (500+n, 500, "", "")
			endif
		next n
	next nSvc
endif


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

; Hang up if we're on a dial-up connection...
:HangUp
if (hConn)
	nRet = DUNDisconnect (hConn)
endif


; Log what we did...
:LogIt
if bOK == @TRUE
	sLog = "Ran Exchange"
else
	sLog = strcat ("Error attempting to automate Exchange:", @CRLF, sErrDesc)
endif

CMLogMessage (sLog)
Display (8, "ClockMan95", sLog)
exit


; Select the menu item corresponding to service "sService"...
:WinSelectMenuItem
WSMsSysDir = DirWindows (1)
WSMhUser32 = DllLoad ("%WSMsSysDir%user32.dll")
;Debug(@ON)
WSMhwnd = IntControl(21, sWinTitle, 0, 0, 0)
WSMhMenu = DllCall(WSMhUser32, long:"GetMenu", long:WSMhwnd)
WSMhMenu = DllCall(WSMhUser32, long:"GetSubMenu", long:WSMhMenu, long:3)
WSMhMenu = DllCall(WSMhUser32, long:"GetSubMenu", long:WSMhMenu, long:0)
if (!WSMhMenu)
	CMLogMessage ("No Services menu found!")
else
	WSMhItem = BinaryAlloc(80)
	BinaryEODSet(WSMhItem, 80)
	WSMnSvcs = DllCall(WSMhUser32, long:"GetMenuItemCount", long:WSMhMenu)
	for WSMn = 2 to WSMnSvcs-1 ; skipping over "All services" & horiz separator
		DllCall(WSMhUser32, long:"GetMenuStringA", long:WSMhMenu, long:WSMn, lpbinary:WSMhItem, long:80, long:1024)
		WSMsItem = BinaryPeekStr(WSMhItem, 0, 80)
		if strindex (WSMsItem, sService, 0, @FWDSCAN) == 1
			; We found the menu item we're looking for...
			WSMnItem = WSMn - 1
			SendKeysTo (sWinTitle, "^%WSMnItem%") ; Ctrl+1, etc.
		endif
	next WSMn
	BinaryFree (WSMhItem)
endif
DllFree (WSMhUser32)

return
~
[ RETURN TO DIRECTORY ]