/* 
VMB V0.01 Voice-Mailbox - Unregistered Release 
This program is ShareWare.
Copyright © 1995 by Stephan Eisler 
Thanks to Carsten Pluntke for ZVT!
*/
	TRACE(OFF)
	num	=	1			/* Mailbox Nummer */
	key	=	0			/* Mailbox Key */
	msg	=	0			/* Mailbox Messages */
	msp	=	1
	i	=	0
	passcount=      1
	nu	=	1
	opc	=	1
	re	=	0
	hu	=	1			/* Hang up */
	m	=	0
	b	= 	0

	n 	= 	0			/* Sort Mail */
	c	=	0			/* Sort Mail */ 
	cal 	= 	16			/* Time (siehe unten....) */
	ru	=	0
	cfg	=	"VMB:VMBDAT/VMB.cfg"

	signal on syntax
/*	signal on Error
	signal on IOerr		*/




	OPEN(VOCi,cfg,"R")			/* Read Datas! */	
	DO until eof(VOCi)	
	 i = i + 1
	 VOC.i = word(readln(VOCi),1)
	end
	CLOSE(cfg)
	
	select
	 when i ~= 80 then call coerr 
	 when VOC.i ~= "Copyright_©_1995_by_Stephan_Eisler" then call coerr
	 otherwise call cook
	end                          
coerr:
	say "Error in VMB.cfg ...."
	exit
cook:		

	if ~showlist('p','ZVT.1') then do
   	 ADDRESS COMMAND "run "VOC.4" CO=VMB:VMBDAT/VMB.set >NIL:"
   	  do i = 1 to 10
      	  if ~showlist('p','ZVT.1') then call delay 20
      	  else leave i
   	 end
	end


	address ZVT.1
	options results


	open(log,VOC.7,"A")
	call status(40 date() time())
	
	settname "VMB:CALVOC/"

	call status(41)
	playfile VOC.9 1

MainMenu:
	call status(42)
	playfile VOC.10 0
	DTMF = result
	call status(43 DTMF)
	select                                  
	  when DTMF = 0 then call operator              
	  when DTMF = 9 then call timeans	/* Zeitansage */
	  when DTMF = "#" then call exit
	  when DTMF = "" then call bjnoDTMF	/* bis jetzt kein DTMF */
       	  when DTMF = "d" then call exit	/* user abbruch ??? */
	  otherwise call entermailbox           
	end                          

bjnoDTMF:
	call status(44)
	read1key
	DTMF = result                     
	select                            
	  when DTMF = 0 then call operator
	  when DTMF = 9 then call timeans	/* Zeitansage */
	  when DTMF = "#" then call exit
	  when DTMF = "*" then call MainMenu
	  when DTMF = "" then call innoDTMF	/* immer noch kein DTMF */
	  otherwise call entermailbox     
	end                               

innoDTMF:
	call status(45)
	playfile VOC.11 1
	call operator(1)
nextf:	
	recpdo = VOC.33|| nu
	if ~exists(recpdo) then do
	 call status(46 recpdo)
	 playfile VOC.29 1
	 sendbeep 1
	 recordfile recpdo 180 0		/* 180=3*60 => 3 Min */
	 DTMF = result
	 if DTMF = "s" then do			/* SILENCE erkannt */
	  call status(59 recpdo)
          address command "C:delete "|| recpdo ||  " "
	 end 	
	end
	else do
	 nu = nu + 1
	 call nextf
	end
	
	call exit

operator:
	PARSE ARG re
	opc = 1
opnext:
	call status(47)
	address command "C:sound "|| VOC.8 ||  " "
	
	if ru = 0 then do 
	 ru = 1
	 address command "run >nil: VMB:VMBDAT/VMBa >nil:"
	end
	gettname
	test = result
	if test = "VMB:CALVOC/temp" then call opmeldetsich
 
        playfile VOC.27 0       
        DTMF = result                       
        if DTMF = "*" then call MainMenu
	if DTMF = "#" then call exit
	
	opc = opc + 1
	if opc > 5 then do
	 playfile VOC.28 0
	 if re = 1 then call nextf
	 else call MainMenu
	end
	call opnext

entermailbox:
	sendbeep 1
	i = 1
embrnm:
	i = i + 1
	if i > 10 then call hangup		/* usr hat evt. aufgelegt */
	read1key
	DTMF1 = result
	if DTMF1 = "" then call embrnm
        if DTMF1 = "*" then call MainMenu
        if DTMF1 = "#" then call exit    
	sendbeep 1
	mailboxnumber = DTMF || DTMF1
	call status(48 mailboxnumber)
	playfile VOC.5 || mailboxnumber 1 	/* nur bis 59 !!! */
	mbdatanr = VOC.34|| mailboxnumber
	IF ~exists(mbdatanr) THEN DO 		/* Check Mailbox Number */
	 call status(49 mailboxnumber)
	 playfile VOC.12 1
	 call MainMenu 	
	end
	else do
	 OPEN(datas,mbdatanr,"R")		
	  num = readln(datas)
	  key = readln(datas)		
	 CLOSE(datas)
	end


sortmail:
	pre 	=	"mail" || num
	files 	= 	showdir(VOC.32,'f')
	anz 	= 	words(files)
loop:
	n = n + 1
	if n > anz then call all
	if pre = left(word(files,n),6) then do
	 c = c + 1
	 mail.c = word(files,n)
	 tim.c = right(mail.c,14)
	end
	call loop
all:	
	last = 1
	do n = 1 to c 
	 m = n + 1
	 if  tim.n > tim.m then do
	  last = 0
 	  sto = tim.n
	  tim.n = tim.m
	  tim.m = sto
	  sto = mail.n
	  mail.n = mail.m
	  mail.m = sto
	 end
	end n
	if last = 0 then call all
	msg = c

	call status(50 mailboxnumber)
	playfile VOC.31|| mailboxnumber 1

	hu = 1
mbmenu:
	call status(51)
	playfile VOC.13 0
        DTMF = result                      
        select                             
          when DTMF = 1 then call recordmessy 
          when DTMF = 2 then call passwordcheck
          when DTMF = "#" then call exit
	  when DTMF = "*" then call MainMenu
	  when DTMF = "" then call mbmenu
	  otherwise call Invalidinput      
        end                                
	call mbmenu


recordmessy:
	playfile VOC.25 1
	msp=msg+1
	ti = time()        
	st = substr(ti,1,2)
	mi = substr(ti,4,2)
	se = substr(ti,7,2)
	call status(52 VOC.32|| pre || date('s')|| st || mi || se )
	sendbeep 1
	recordfile  VOC.32|| pre || date('s')|| st || mi || se 240 0
	DTMF = result
/*	if DTMF = "s" then do			/* SILENCE erkannt */
	 call status(59 VOC.32|| num || msp)
	 address command "C:delete "|| VOC.32|| num || msp ||  " "
	end 	                                ---- geht nicht ??? */
	call MainMenu

passwordcheck:
	passcount = 1
pwcheck: 
	call status(53)
	playfile VOC.15 1
	pass=readkeys(4)
	call status(54 pass)
	if key = pass then do
	 hu = 1
	 call passrigh
	end 
	else do 
	 passcount = passcount + 1
	  if passcount > 3 then do 
	   playfile VOC.16 1 		/* zu viele Fehlversuche! */
	   call MainMenu
          end
	 playfile VOC.14 1
	 call pwcheck
	end
passrigh:
	call status(55)
	playfile VOC.17 0
        DTMF = result                          
        select                                 
          when DTMF = 1 then call CheckVoiceMail  
          when DTMF = 2 then call ChangeGreeting
          when DTMF = 3 then call ChangePassword
          when DTMF = "#" then call exit
	  when DTMF = "*" then call MainMenu     
          when DTMF = "" then call passrigh
	  otherwise call Invalidinput          
        end                                    
	call passrigh

CheckVoiceMail:
	msp = 1
	call status(56)
	if msg = 0 then do
	 playfile VOC.18 || msg 1
	 call passrigh
	end
	if msg < 10 then do 
	 playfile VOC.18 || msg 1
	end
	else do
	 playfile VOC.18 || 11 1
	end

PlayMail:
 	call status(57 msp)
	playfile VOC.32|| mail.msp 0		
	DTMF = result    
        select                                 
         when DTMF = "#" then call exit
	 when DTMF = "*" then call MainMenu     
         otherwise do
	  hu = 1
	  call nextmsgmenu
 	 end 	 
        end                                    

nextmsgmenu:
	call status(58)
	playfile VOC.19 0
        DTMF = result
	select                                  
          when DTMF = 1 then do 
	   msp = msp + 1
	   if msp > msg then call lastmsg
	   call PlayMail
	  end
	  when DTMF = 2 then call PlayMail
          when DTMF = 3 then call Deletemsg
          when DTMF = "#" then call exit
	  when DTMF = "*" then call MainMenu      
          when DTMF = "" then call nextmsgmenu
	  otherwise call Invalidinput
        end                         
	call nextmsgmenu
	
deletemsg:
	call status(59)
	call status(59 VOC.32|| mail.msp)
	address command "C:delete "|| mail.msp ||  " "
	hu = 1
	call nextmsgmenu

lastmsg:
	call status(60)
	playfile VOC.20 1
	call nextmsgmenu

ChangeGreeting:
	playfile VOC.21 1
	call status(61 VOC.31|| num)
	sendbeep 20
	recordfile VOC.31|| num 30 0
	 DTMF = result
/*	 if DTMF = "s" then do			/* SILENCE erkannt */
	  address command "C:delete "|| VOC.31|| num ||  " "
	 end 	           				??????? */
	call passrigh

ChangePassword:					
	call status(62)
	playfile VOC.22 1
	nkey1=readkeys(4)
	playfile VOC.23 1
	nkey2=readkeys(4)
	if nkey1 ~= nkey2 then do 
	 playfile VOC.24 1
	 call ChangePassword
	end
	else do 	
	 key=nkey1
	 call status(63 key)
	 OPEN(datas,mbdatanr,"W")		
          writeln(datas,num)
          writeln(datas,key)
          writeln(datas,msg)
	 close(datas)
	end
	call passrigh

timeans:
	if ~exists(VOC.6) then do 
	 address command "C:makedir  "|| VOC.6 ||  " "
	 address command "C:copy  "|| VOC.5 VOC.6 || "/" ||  " all >nil:"
	end
	VOC.5 = VOC.6 || "/"
ansage:
	TIME(R)
	ti = time()
	st = substr(ti,1,2)
	mi = substr(ti,4,2)
	se = substr(ti,7,2)
	se = se + cal
	if se > 59 then do
	 se = se - 59
	 if length(se) = 1 then se = 0 || se
	 mi = mi + 1
	 if length(mi) = 1 then mi = 0 || mi
	end
	call status(67 st mi se)
        call play("BnTie")
	call play(st)	
	call play("Uhr")
        if mi = 1 then do
	 call play("1neMu")
	end
	else do 	
         call play(mi)     
	 call play("MinutenUnd")
	end
        if se = 1 then do
	 call play("1neS")
	end 
	else do 
	 call play(se)     
	 call play("Sekunden")
	end
	i = 1
tcoun: if TIME(E) < cal then do
/*	i = i + 1 ; say i                     >>>  Test cal ! */
	call tcoun
	end
	sendbeep 1

	call ansage 

play:
	parse arg file 
	playfile VOC.5 || file 0
        DTMF = result              
        select
	  when DTMF = "#" then call exit
	  when DTMF ~= "" then call MainMenu      
          otherwise return
	end
	
	call MainMenu

readkeys:                          
	PARSE ARG keys 
	i = 1
read1:	read1key 
	key.i = result			
	showstatus "VMB_" || key.i
	if key.i = "*" then call MainMenu
	if key.i = "#" then call Exit
	if i ~= keys then do
	 i = i + 1
	 call read1	
	end
	else do
        nkey = key.1 || key.2 || key.3 || key.4
        showstatus "VMB_" || nkey
	end	
	RETURN nkey  

Invalidinput:                          
	hu = hu +1
	if hu > 10 then call hangup		/* anrufer hat evt. aufgelegt*/
	call status(64)
	playfile VOC.14 0             
	if DTMF = "#" then call exit
	if DTMF = "*" then call MainMenu      
        return

status:
	parse arg sta sta1 sta2 sta3
	showstatus VOC.sta || sta1 || sta2 || sta3
	writeln(log,VOC.sta ||  sta1 || sta2 || sta3)
	return
	
hangup:
	call status(66)
	call exit

opmeldetsich:	
	settname "VMB:CALVOC/"
	call status(70)
	playfile VOC.30 1
	call status(65 date() time())
        writeln(log,'-')
	close(log)
	EXIT

Error: ; Syntax: ; IOerr:                                               
 	say "VMB ERROR ! ErrorCode #"  rc || sigl  
 	say "»»» Please Notify Stephan Tel.: +49 (0)7244-2199 «««"                                  
 	say "Exiting..."                                                             
 
        call status(69 rc sigl)
	writeln(log,'-')
	close(log)
	EXIT                                                                   	

exit:
	playfile VOC.26 1
	call status(65 date() time())
        writeln(log,'-')
	close(log)
	EXIT
