/*
**
**    $VER: AminetFTPMail 2.0 (17-09-1998) © Yves Grabowsky
**
*/

/*----------*/
/* add libs */
/*----------*/

IF ~show('L','tritonrexx.library') then do
   IF ~addlib("tritonrexx.library",5,-30) then do
      SAY "Couldn't open tritonrexx.library!"
      exit(20)
      end
   end
IF ~show('L','rexxtricks.library') then do
   IF ~addlib('rexxtricks.library',1,-30) then do
      say "Couldn't open rexxtricks.library"
      exit(20)
      end
   end
IF ~show('L','rexxreqtools.library') then do
   IF ~addlib("rexxreqtools.library",0,-30) then do
      say "Couldn't open rexxreqtools.library"
      exit(20)
      end
   end

/*---------*/
/* Options */
/*---------*/

OPTIONS RESULTS
OPTIONS FAILAT 20

/*----------------*/
/* Debug routines */
/*----------------*/

/* TRACE(RESULTS) */
/* TRACE(SCAN) */

/*------------*/
/* Interrupts */
/*------------*/

SIGNAL ON BREAK_C
SIGNAL ON HALT
SIGNAL ON IOERR
SIGNAL ON SYNTAX

/*-----------*/
/* some vars */
/*-----------*/

pos = "rt_reqpos=reqpos_centerscr"

/*-----------------*/
/* load configfile */
/*-----------------*/

IF ~EXISTS("S:AminetFTPMail.config") then do
   call rtezrequest("Configfile not found!",,"AminetFTPMail",pos)
   exit(20)
   end

CALL READFILE("S:AminetFTPMail.config",config)

IF config.0 ~= "7" then do
   call rtezrequest('Configfile is old!' || "0A"x || 'Start the configprogram first!',,'AminetFTPMail',pos)
   exit(20)
   end

/*----------------*/
/* format configs */
/*----------------*/

mailaddress = config.1
mailsize = config.2
readme = config.3
fontname = config.4
fontsize = config.5
multimail = config.6
delmail = config.7

/*---------------------------*/
/* search for recent mail(s) */
/*---------------------------*/

/*--------------------*/
/* multimail disabled */
/*--------------------*/

IF multimail = 0 then do
   ADDRESS YAM
   MAILINFO STEM 'mailinfo.'
   check1 = POS('Aminet',mailinfo.subject)
   IF check1 >0 then do
      check2 = POS('uploads',mailinfo.subject)
      IF check2 >0 then do
         check = 1
         end
      ELSE do
         check = 0
         end
      end
   ELSE do
      check = 0
      end
   IF check = 0 then do
      call rtezrequest('Not an Aminet Recent mail!',,'AminetFTPMail',pos)
      exit(20)
      end
   file = mailinfo.filename
   subject = mailinfo.subject
   date = WORD(mailinfo.subject,'5')
   nomulti = 1
   CALL READFILE(file,'recent')
   end

/*-------------------*/
/* multimail enabled */
/*-------------------*/

IF multimail = 1 then do
   ADDRESS YAM
   FOLDERINFO STEM 'folder.'
   ADDRESS YAM
   DO i=0 to folder.total
      ADDRESS YAM
      SETMAIL i
      MAILINFO STEM 'mailinfo.'
      aminet1 = POS('Aminet',mailinfo.subject)
      IF aminet1 >0 then do
         aminet2 = POS('uploads',mailinfo.subject)
         IF aminet2 >0 then do
            aminet = 1
            end
         ELSE do
            aminet = 0
            end
         end
      ELSE do
         aminet = 0
         end
      IF aminet = 1 then do
         /* write filename to temp */
         file.0 = 1
         file.1 = mailinfo.filename
         IF EXISTS('T:AFTPMail.temp') then do
            CALL WRITEFILE('T:AFTPMail.temp',file,A)
            end
         ELSE do
            CALL WRITEFILE('T:AFTPMail.temp',file)
            end
         /* write date to 2nd tempfile */
         date.0 = 1
         date.1 = WORD(mailinfo.subject,'5')
         IF EXISTS('T:AFTPMail-2.temp') then do
            CALL WRITEFILE('T:AFTPMail-2.temp',date,A)
            end
         ELSE do
            CALL WRITEFILE('T:AFTPMail-2.temp',date)
            end
         end
      end
   nomulti = 0
   CALL READFILE('T:AFTPMail.temp',files)
   CALL READFILE(files.1,'recent')
   mailnum = 1
   CALL READFILE('T:AFTPMail-2.temp',dates)
   date = dates.1
   end

DO o=1 to recent.0
   begin = POS('|--',recent.o)
   IF begin >0 then do
      startline = o
      LEAVE
      end
   end


CALL STEMREMOVE(recent,'1',startline)

order.0 = 0
order2.0 = 0

/*--------------------*/
/* create application */
/*--------------------*/

apptags = 'TRCA_Name     AminetFTPMail',
          'TRCA_LongName "AminetFTPMail 2.0 © Yves Grabowsky"',
          'TRCA_Version  2.0',
          'TRCA_Release  3',
          'TRCA_Date     17.09.98'

app = TR_CreateApp(apptags)

/*---------------*/
/* define window */
/*---------------*/

win = WindowTitle('AminetFTPMail'),
      ScreenTitle('AminetFTPMail 2.0 (17-09-1998) © Yves Grabowsky'),
      WindowID('1'),
      WindowPosition('TRWP_CENTERSCREEN'),
      VertGroupA,
         Space,
         NamedSeparator('AminetFTPMail 2.0'),
         Space,
         CenteredText3('Uploads:'),
         Space,
         HorizGroupA,
            Space,
            'TROB_Listview "recent" TRAT_Flags TRLV_SELECT TRAT_ID "5" TRAT_MinWidth "90" TRAT_MinHeight "15" TRAT_Value "0" TRLV_Top "0"',
            Space,
         Endgroup,
         Space,
         CenteredText3('I would like to get:'),
         Space,
         HorizGroupA,
            Space,
            'TROB_Listview "order" TRAT_Flags TRLV_SELECT TRAT_ID "6" TRAT_MinWidth "90" TRAT_MinHeight "10" TRAT_Value "0" TRLV_Top "0"',
            Space,
         Endgroup,
         Space,
         HorizGroupA,
            Space,
            Button('_Next mail','10'),
            SpaceS,
            Button('_Last mail','11'),
            Space,
            TextN('Maildate:'),
            Space,
            GroupBox,
            HorizGroupA,
               Space,
               TextID(date,'12'),
               Space,
            Endgroup,
            Space,
            TextN('Address:'),
            Space,
            GroupBox,
            HorizGroupA,
               Space,
               TextN(mailaddress),
               Space,
            Endgroup,
            Space,
            TextN('Max. Mailsize:'),
            Space,
            GroupBox,
            HorizGroupA,
               Space,
               TextN(mailsize),
               Space,
            Endgroup,
            Space,
         Endgroup,
         Space,
         HorizSeparator,
         Space,
         HorizGroupA,
            Space,
            Button('_Write the mail!','8'),
            SpaceB,
            Button('_Cancel','9'),
            Space,
         Endgroup,
         Space,
      Endgroup,
      Endproject

/*--------------*/
/* open project */
/*--------------*/

project = TR_OpenProject(app,win)

/*-----------------------*/
/* set multimail options */
/*-----------------------*/

IF nomulti = 1 then do
   CALL TR_SetAttribute(project,'10','TRAT_Disabled','1')
   CALL TR_SetAttribute(project,'11','TRAT_Disabled','1')
   end

IF nomulti = 0 then do
   CALL TR_SetAttribute(project,'11','TRAT_Disabled','1')
   IF files.0 = 1 then do
      CALL TR_SetAttribute(project,'10','TRAT_Disabled','1')
      end
   end

DO WHILE exit ~= 1
   CALL TR_Wait(app,'')
   DO WHILE TR_HandleMsg(app,event)
      IF event.trm_class = "TRMS_CLOSEWINDOW" then do
         signal quit
         end
      IF event.trm_id = "5" then do                /* upload selected */
         uploadnum = event.trm_data+1
         newvalue = order.0+1
         order.newvalue = recent.uploadnum
         order2.newvalue = recent.uplaodnum
         order.0 = order.0+1
         order2.0 = order2.0+1
         stemvar = TR_GetAttribute(project,'6','TROB_Listview')
         If stemvar = "order" then do
            CALL TR_SetAttribute(project,'6','TROB_Listview','order2')
            end
         ELSE do
            CALL TR_SetAttribute(project,'6','TROB_Listview','order')
            end
         CALL TR_SetAttribute(project,'7','TRAT_Text',order.0)
         end
      IF event.trm_id = "6" then do                /* add entry to wishlist */
         removenum = event.trm_data+1
         CALL STEMREMOVE(order,removenum,1)
         CALL STEMREMOVE(order2,removenum,1)
         stemvar = TR_GetAttribute(project,'6','TROB_Listview')
         If stemvar = "order" then do
            CALL TR_SetAttribute(project,'6','TROB_Listview','order2')
            end
         ELSE do
            CALL TR_SetAttribute(project,'6','TROB_Listview','order')
            end
         CALL TR_SetAttribute(project,'7','TRAT_Text',order.0)
         end
      IF event.trm_id = "9" then do
         signal quit
         end
      IF event.trm_id = "8" then do
         exit = 1
         end
      IF event.trm_id = "10" then do               /* next mail */
         mailnum = mailnum+1
         date = dates.mailnum
         CALL TR_SetAttribute(project,'12','TRAT_Text',date)
         IF mailnum >= files.0 then do
            CALL TR_SetAttribute(project,'10','TRAT_Disabled','1')
            disabled = 1
            file = files.mailnum
            CALL READFILE(file,recent)
            DO o=1 to recent.0
               begin = POS('|--',recent.o)
               IF begin >0 then do
                  startline = o
                  LEAVE
                  end
               end
            CALL STEMREMOVE(recent,'1',startline)
            CALL READFILE(file,recent2)
            DO o=1 to recent2.0
               begin = POS('|--',recent2.o)
               IF begin >0 then do
                  startline = o
                  LEAVE
                  end
               end
            CALL STEMREMOVE(recent2,'1',startline)
            end
         ELSE do
            file = files.mailnum
            CALL READFILE(file,recent)
            DO o=1 to recent.0
               begin = POS('|--',recent.o)
               IF begin >0 then do
                  startline = o
                  LEAVE
                  end
               end
            file = files.mailnum
            CALL READFILE(file,recent2)
            DO o=1 to recent2.0
               begin = POS('|--',recent2.o)
               IF begin >0 then do
                  startline = o
                  LEAVE
                  end
               end
            CALL STEMREMOVE(recent,'1',startline)
            CALL STEMREMOVE(recent2,'1',startline)
            end
         IF mailnum = 2 then do
            CALL TR_SetAttribute(project,'11','TRAT_Disabled','0')
            end
         stemvar = TR_GetAttribute(project,'5','TROB_Listview')
         If stemvar = "recent" then do
            CALL TR_SetAttribute(project,'5','TROB_Listview','recent2')
            end
         ELSE do
            CALL TR_SetAttribute(project,'5','TROB_Listview','recent')
            end
         end
      IF event.trm_id = '11' then do                     /* last mail */
         mailnum = mailnum-1
         date = dates.mailnum
         CALL TR_SetAttribute(project,'12','TRAT_Text',date)
         IF mailnum = 1 then do
            CALL TR_SetAttribute(project,'11','TRAT_Disabled','1')
            disabled = 1
            file = files.mailnum
            CALL READFILE(file,recent)
            DO o=1 to recent.0
               begin = POS('|--',recent.o)
               IF begin >0 then do
                  startline = o
                  LEAVE
                  end
               end
            CALL STEMREMOVE(recent,'1',startline)
            CALL READFILE(file,recent2)
            DO o=1 to recent2.0
               begin = POS('|--',recent2.o)
               IF begin >0 then do
                  startline = o
                  LEAVE
                  end
               end
            CALL STEMREMOVE(recent2,'1',startline)
            end
         ELSE do
            file = files.mailnum
            CALL READFILE(file,recent)
            DO o=1 to recent.0
               begin = POS('|--',recent.o)
               IF begin >0 then do
                  startline = o
                  LEAVE
                  end
               end
            file = files.mailnum
            CALL READFILE(file,recent2)
            DO o=1 to recent2.0
               begin = POS('|--',recent2.o)
               IF begin >0 then do
                  startline = o
                  LEAVE
                  end
               end
            CALL STEMREMOVE(recent,'1',startline)
            CALL STEMREMOVE(recent2,'1',startline)
            end
         stemvar = TR_GetAttribute(project,'5','TROB_Listview')
         If stemvar = "recent" then do
            CALL TR_SetAttribute(project,'5','TROB_Listview','recent2')
            end
         ELSE do
            CALL TR_SetAttribute(project,'5','TROB_Listview','recent')
            end
         IF disabled = 1 then do
            CALL TR_SetAttribute(project,'10','TRAT_Disabled','0')
            disabled = 0
            end
         end
      end
   end

/*--------------*/
/* close window */
/*--------------*/

CALL TR_CloseProject(project)
CALL TR_DeleteApp(app)

/*-------------------------------*/
/* format orderlist for tempfile */
/*-------------------------------*/

DO i=1 to order.0
   filename = WORD(order.i,1)
   dir = WORD(order.i,2)
   order.i = "get /pub/aminet/"dir"/"filename
   end

order2.0 = order.0

/*------------------------------------------*/
/* format second orderlist for readme files */
/*------------------------------------------*/

IF readme = 1 then do
   DO i=1 to order2.0
      dotpos = LASTPOS('.',order.i)
      order2.i = DELSTR(order.i,dotpos)
      order2.i = order2.i".readme"
      end
   end

/*-----------------*/
/* tempfile header */
/*-----------------*/

head.0 = 3
head.1 = "begin"
head.2 = "binary"
head.3 = "size" mailsize

/*--------------*/
/* tempfile end */
/*--------------*/

end.0 = 3
end.1 = "end"
end.2 = "--"
end.3 = "Mail written with AminetFTPMail © Yves Grabowsky"

/*----------------*/
/* write tempfile */
/*----------------*/

CALL WRITEFILE('T:AFTPMail.temp',head)
CALL WRITEFILE('T:AFTPMail.temp',order,A)
IF readme = 1 then do
   CALL WRITEFILE('T:AFTPMail.temp',order2,A)
   end
CALL WRITEFILE('T:AFTPMail.temp',end,A)

/*----------------------------*/
/* tell yam to write the mail */
/*----------------------------*/

/* with delete after send flag */
IF delmail = 1 then do
   ADDRESS YAM
   MAILWRITE
   WRITESUBJECT "Request"
   WRITETO mailaddress
   WRITELETTER "T:AFTPMail.temp"
   WRITEOPTIONS DELETE
   WRITEQUEUE
   end
ELSE do
   /* without delete after send flag */
   ADDRESS YAM
   MAILWRITE
   WRITESUBJECT "Request"
   WRITETO mailaddress
   WRITELETTER "T:AFTPMail.temp"
   WRITEQUEUE
   end

/*-----------------*/
/* delete tempfile */
/*-----------------*/

IF multimail = 1 then do
   ADDRESS COMMAND
   "delete >NIL: T:AFTPMail.temp"
   "delete >NIL: T:AFTPMail-2.temp"
   end
exit

quit: 

/*------*/
/* Quit */
/*------*/

CALL TR_CloseProject(project)
CALL TR_DeleteApp(app)
IF multimail = 1 then do
   ADDRESS COMMAND
   "delete >NIL: T:AFTPMail.temp"
   "delete >NIL: T:AFTPMail-2.temp"
   end
exit(0)


/*------------*/
/* Interrupts */
/*------------*/

HALT:
SAY "Program terminated trough global HALT interrupt!"
exit(20)

BREAK_C:
exit(10)


IOERR:
SAY "I/O-Error in line:" SIGL
SAY "Errorcode:" RC
SAY "Error:" ERRORTEXT(RC)
SAY "Line says:" SOURCELINE(SIGL)
exit(20)

SYNTAX:
SAY "SYNTAX Error in line:" SIGL
SAY "Errorcode:" RC
SAY "Error:" ERRORTEXT(RC)
SAY "Line says:" SOURCELINE(SIGL)
exit(20)

