/**/ 
v="$VER: Chat Wplrx  User WPL Chat Utility                Williamson 54.11"
/* RexxMSG "" REXX "MAIL:rexx/Chat $(baud) $(line) $(username)" */
options RESULTS
options failat 99
signal on syntax
signal on halt
signal on ioerr
signal on break_c
signal on break_d
'String $(chatmode)'
if upper(RESULT)="SYSOP" then forced=1
else forced=0
cr='\r\n';nl='0a'x;bs='08'x;quote='"';cls='\014'

l_mailer=lower(GetClip('SHELTER'))
parse arg baud port username
call send(cls)
if forced then do
    call send(cr||'The sysop is coming online'||cr)
    'Set chat ONLINE'
end;else do
    if username~="" then fname=strip(username)
    else do
        fname=wpl_prompt(60,cr||' Please enter your name: ')
        if fname="" then do
            call send(cr||'Sorry, your name is required in order to chat with sysop'||cr)
            call send(cr||'If you wish, you can leave a message with NOTE'||cr)
            call cleanup()
            exit 0
        end
        'Set fname' fname
    end
    call logname(fname)
    call send(cr||'Paging sysop....hit Q<cr> to quit'||cr)
    speech=fname' wants to talk.'
    cmd='Say "'speech'"'
    address COMMAND cmd

    do i=0 to 200
        call writech(STDOUT,"."||'07'x)
        x=nprompt(1,"."||'07'x)
        if index(upper(x),'Q')>0 then break
        call delay(60)
        'CheckCarrier'
        if RC~=0 then do
            call cleanup
            exit
        end
        'String $(chat)'
        chatstat=RESULT
        if upper(chatstat)="ONLINE" then leave
    end
    if upper(chatstat)~="ONLINE" then do
        call send(cr||'Sorry, the sysop seems to be unavailable'||cr)
        call send(cr||'If you wish, you can leave a message with the NOTE command.'||cr)
        call cleanup
        exit
    end
    resp=wpl_prompt(100,cls||'Sysop online, what is the Subject? ')
end

do while chatstat="ONLINE"
    'CheckCarrier'
    if RC~=0 then leave
    resp=getstring(300)
    'String $(chat)'
    chatstat=RESULT
end
call cleanup()
exit 0


logname:
'String $(p.login)'
namepos=RESULT
Address "LOGPROC" 'PutLine 'l_mailer'wplstat'port namepos arg(1) time()
return

send:
'Print' quote||arg(1)||quote
'Send' quote||arg(1)||quote
return

wpl_prompt:
'Print' quote||arg(2)||quote
nprompt:
'Send' quote||arg(2)||quote
getstring:
'GetInbound E0 'arg(1)
'String $(event)'
if upper(RESULT) = 'CARRIER' then do
    Address "LOGPROC" 'PutLog 'l_mailer'wpl' left(time(),5) port "CHAT:" fname 'Dropped Carrier'
    call cleanup
    exit
end;else if upper(RESULT) = 'LOGIN' then do
    'String $(namebuf)'
    return (RESULT)
end
x=""
return x

lower:
return(bitor(arg(1),'20'x))
cleanup:
    'Clear chat chatmode'
return 0
break_c:
break_d:
    call cleanup
    exit 10
novalue: 
call template_oops "Novalue" sigl
syntax:
call template_oops "Syntax(RC=" || RC || ")" sigl
failure:
call template_oops "Failure(RC=" || RC || ")" sigl
ioerr:
call template_oops "IOErr" sigl 
halt:
call template_oops "Halt" sigl 
template_oops:
parse arg what badline
say 'ERR:'what "Line:"badline
call cleanup
exit(40)
/**/
