/*
** $VER:    UMSXChange  1.2 (29 Jun 1997)
**
** Author:  Christoph Maria Dahlen <chdahlen@emails.de>
** Purpose: Do a Mail/Newsexchange via Miami (and GoodNews if running)
**
** Warning: This script contains no user-servicable parts inside, for
**          a configuration please start this script while GoodNews
**          running. I suggest you put this skript in the GoodNews ARexx
**          menu, so enter to ums.config something like this:
**
**          ( GoodNews.RexxMenu
**           "#? \"ums:rexx/UMSXChange.good \"UMSXChange\" \n"
**          )
**
** Security:Please do not let this skript work alone, because it has
**          no extensive error checking yet. This could for example
**          result in a not dropped phoneline!
**
**          Your POP3 passwort will be written readable in the prefs!
**
** ToDo   : Study ums rexx and find a way to get the UMSName for
**            the setfolder call
**          Encrypt the pop3 passwort
*/

OPTIONS RESULTS
TITLE_STRING = "UMSXChange by Christoph Dahlen"

SIGNAL on BREAK_C

/* DEBUG */
IF EXISTS("ENV:UMSXChange.debug") THEN TRACE R

/*
** Look for prefs
*/
if ~EXISTS("ENVARC:UMSXChange") THEN DO
    if ~show('P','GOODNEWS.1') THEN DO
        say TITLE_STRING;
        say "Prefs could not be found. For your first configuration please start"
        say "this skript from within GoodNews or with GoodNews running."
        return(5)
        END

/*
** Let the user set the prefs
*/
    address GOODNEWS.1
    'REQUEST "'TITLE_STRING'" "_Ok|_Cancel" "Since you are UMSXChange for the*nfirst time, some settings have*nto be made. Please answer the following*nQuestions."'
    IF RESULT = 0 THEN EXIT
    'GETSTRING DEFAULT "MIAMI:Miami" TITLE "'TITLE_STRING'" LABEL "Where to find Miami"'
    IF RESULT = "RESULT" THEN EXIT
    MIAMI_PATH = RESULT
    'GETSTRING DEFAULT "news" TITLE "'TITLE_STRING'" LABEL "IP or name of your newsserver (nntp)"'
    IF RESULT = "RESULT" THEN EXIT
    NNTP_SERVER= RESULT
   'REQUEST "'TITLE_STRING'" "_Yes|_No|_Cancel" "Does your Newsserver support NEWNEWS ?"'
    SELECT
        WHEN RESULT = 1 THEN NEWSMODE="1"
        WHEN RESULT = 2 THEN NEWSMODE="2"
        OTHERWISE EXIT
    END
    'GETSTRING DEFAULT "mail" TITLE "'TITLE_STRING'" LABEL "IP or name of your mailserver (smtp)"'
    IF RESULT = "RESULT" THEN EXIT
    SMTP_SERVER= RESULT
    'GETSTRING DEFAULT "pop3" TITLE "'TITLE_STRING'" LABEL "IP or name of your POP3 server"'
    IF RESULT = "RESULT" THEN EXIT
    POP3_SERVER= RESULT
    'GETSTRING TITLE "'TITLE_STRING'" LABEL "Account name for your POP3 server"'
    IF RESULT = "RESULT" THEN EXIT
    POP3_ACCOUNT=RESULT
    'GETSTRING TITLE "'TITLE_STRING'" LABEL "Password for your POP3 server" SECRET'
    IF RESULT = "RESULT" THEN EXIT
    POP3_PASS  = RESULT
    'REQUEST "'TITLE_STRING'" "_Yes|_No|_Cancel" "Are you using SetFolder ?"'
    IF RESULT = 2 THEN UMSACCOUNT="NOSETFOLDER"
        ELSE
        IF RESULT = 0 THEN EXIT
            ELSE DO
                'GETSTRING DEFAULT "sysop" TITLE "'TITLE_STRING'" LABEL "_Account to use for SetFolder"'
                IF RESULT = "RESULT" THEN EXIT
                UMSACCOUNT =RESULT
                END
/*
** Write the prefs
*/
    IF ~OPEN('Prefs','envarc:UMSXChange','Write') then exit 5
    WRITELN('Prefs','$VER: UMSXChange Prefs 1.0')
    WRITELN('Prefs',MIAMI_PATH)
    WRITELN('Prefs',NNTP_SERVER)
    WRITELN('Prefs',SMTP_SERVER)
    WRITELN('Prefs',POP3_SERVER)
    WRITELN('Prefs',POP3_ACCOUNT)
    WRITELN('Prefs',POP3_PASS)
    WRITELN('Prefs',UMSACCOUNT)
    WRITELN('Prefs',NEWSMODE)
    if ~CLOSE('Prefs') then exit 5;
    END
ELSE DO

/*
** Read the prefs
*/
    if ~OPEN('Prefs','envarc:UMSXChange','Read') then exit 5
    VERSION = READLN('Prefs')
    MIAMI_PATH  = READLN('Prefs')
    NNTP_SERVER = READLN('Prefs')
    SMTP_SERVER = READLN('Prefs')
    POP3_SERVER = READLN('Prefs')
    POP3_ACCOUNT= READLN('Prefs')
    POP3_PASS   = READLN('Prefs')
    UMSACCOUNT  = READLN('Prefs')
    NEWSMODE    = READLN('Prefs')
    if ~CLOSE('Prefs') then exit 5;
END

SIGNAL on ERROR

/*
** User confirmation if started by GoodNews. If not started with GoodNews
** both, news and mail will be fetched.
*/

if show('P','GOODNEWS.1') THEN DO
    address GOODNEWS.1
    'REQUEST "'||TITLE_STRING||'" "_All|_Mail|_News|_Cancel" "What Do You Want To Exchange ?"'
    MODE = RESULT
    END
    ELSE MODE=1

IF MODE = 0 THEN EXIT

/*
** Start Miami is needed
*/
IF ~show('P','MIAMI.1') THEN DO
    address command 'c:RUN >NIL: '||MIAMI_PATH;
    IF RC~=0 THEN DO
            say "Miami was not found!"
            EXIT 5
            END
    address command 'c:WAIT 3 sec'
    address command 'WaitForPort MIAMI.1'
    IF RC~=0 THEN DO
            say "Miami could not be addressed!"
            EXIT 5
            END
    MIAMI_STATUS=0
    END
ELSE DO
  MIAMI_STATUS=1
  END

/*
** Set Miami online if needed, for some reason we have to unset
** Error handling before
*/
ADDRESS MIAMI.1

SIGNAL off ERROR
IsOnline
IF RC=0 then OnLine
   ELSE MIAMI_STATUS = 2
SIGNAL on ERROR
/*
** Everything set up, let's go
** Turn on error handling again.
*/

IF (MODE=1|MODE=2) THEN DO
  address command 'ums:bin/umssmtp -h'||SMTP_SERVER
  address command 'ums:bin/umspop3 -h'||POP3_SERVER' -n'||POP3_ACCOUNT' -i'||POP3_PASS
  END
IF (MODE=1|MODE=3) THEN DO
  address command 'ums:bin/umsnntp -h' || NNTP_SERVER
  if NEWSMODE = 1 THEN address command 'ums:bin/umsnntpget -h'|| NNTP_SERVER
  if NEWSMODE = 2 THEN address command 'ums:bin/umsnntpget -g -h'|| NNTP_SERVER
END

ERROR:
BREAK_C:
IF MIAMI_STATUS = 1 THEN
  DO
        ADDRESS MIAMI.1 'OffLine'
  END
IF MIAMI_STATUS = 0 THEN
  DO
    ADDRESS MIAMI.1 'OffLine'
    ADDRESS MIAMI.1 'Quit'
  END

/*
** Invoke SetFolder if choosen in prefs
*/
IF UMSACCOUNT ~= "NOSETFOLDER" THEN DO
    address command 'ums:bin/SetFolder '||UMSACCOUNT '""'
    END

/*
** ReScan if GoodNews running
*/
if show('P','GOODNEWS.1') then address GOODNEWS.1  'RESCAN ALL'
EXIT 0
