/* SnapFiles V1.1 By Glenn Wihlborg (2:200/600.39@Fidonet)

 * Tries to find possible filenames in messages.

 * $VER: SnapFiles 1.1 (1.21.94)

 * SnapFiles is offered to you under the concepts of Netmailware. That means
   if you are using this in a period of 30 days, I would lika a Netmail
   from you  :):):):) 

 * NOTICE!! You must have T: assigned to somewhere in your system */

IF ~SHOW(Libraries,'rexxsupport.library') THEN
	IF ~ADDLIB("rexxsupport.library",0,-30,0) THEN DO
		EXIT
	END

ADDRESS Spot
OPTIONS Results

SIGNAL ON BREAK_C
SIGNAL ON SYNTAX
SIGNAL ON FAILURE

Pathname = 'Outbound:' /* Path to your outbound dir, Must end with ':' or '/' */

LF ='0d'x  /* Line feed */

FileHit = 0 /* If there are any possible files I will set this to one */

FileLen = 0 /* Length of temporary file, used with progressupdate */
LineLen = 0 /* Length of one line in temp file used with progrssupdate */

Total = 0  /* Total files found */
NumExt = 7 /* Total extensions to search for */

Ext.1 = ".ZIP" /* Predefined extensions */
Ext.2 = ".LHA" /* If you put in or remove extensions */
Ext.3 = ".LZH" /* you MUST change the 'NumExt' variable to the */
Ext.4 = ".ARJ" /* right value */
Ext.5 = ".ZOO"
Ext.6 = ".DMS"
Ext.7 = ".ARC"

'ismessages' /* Are we in messages? */

IF RC>0 THEN DO
   Note = 'Use in message window only!'
   CALL Notify()
   EXIT
END
'lockgui'
'getfromaddress'
From = Result

'requestresponse TITLE "SnapFiles V1.1" PROMPT "Use Which Nodenumber?"
CENTER GADGETS _From|_Boss|_Other|_Cancel'

IF RC = 0 THEN DO
   'unlockgui'
   EXIT
END
/* Strip from address e.g. 2:200/600.39 to 2.200.600.39 */
IF RC = 1 THEN DO
   Zon = LEFT(From,POS(':',From)-1)
   From = DELSTR(From,1,POS(':',From))
   Net = LEFT(From,POS('/',From)-1)
   From = DELSTR(From,1,POS('/',From))
   Node = From
   Out = Pathname||Zon||.||Net||.||Node||'.Req'
   CALL Start()
END
/* Strip from address e.g. 2:200/600.39 to 2.200.600.0 */
IF RC = 2 THEN DO
   Zon = LEFT(From,POS(':',From)-1)
   From = DELSTR(From,1,POS(':',From))
   Net = LEFT(From,POS('/',From)-1)
   From = DELSTR(From,1,POS('/',From))
   Node = LEFT(From,POS('.',From))
   Out = Pathname||Zon||.||Net||.||Node||0||'.Req'
   CALL Start()
END
/* Use announcer's nodenumber */
IF RC = 3 THEN DO
   'requeststring TITLE "New Nodenumber" PROMPT "Format: Z.N.N.P"'
   Other = Result
   Out =  Pathname||Other||'.Req'
   CALL Start()
END

Start:
'saveascii TO "T:SnapFiles.tmp" OVERWRITE NOHEADER NOKLUDGES NOTEARLINE NOORIGIN'
	IF ~OPEN('Input','T:SnapFiles.tmp', 'READ') THEN DO  /* Temp file */
      Note = 'Unable to open T:SnapFiles.tmp'
      CALL Notify()
      'unlockgui'
      EXIT
   END
ELSE
   FileLen = WORD(STATEF("T:SnapFiles.tmp"),2)  /* Get size of file */
   'progressopen TITLE "SnapFiles V1.1"
   PROMPT "Scanning for possible filename(s)"'
   Prequest = Result
   StartTime = TIME('R')  /* Start timer */
   DO WHILE ~EOF('Input')
      Line = READLN('Input')
      LineLen = LENGTH(Line)
      FileLen = FileLen - LineLen
      'progressupdate' Prequest LineLen FileLen
         IF RC = 5 THEN DO
            CALL BailOut()
         END
      DO x = 1 to WORDS(Line)
         Hit = WORD(Line,x)
         WordPos = LASTPOS('.',Hit)
            IF RIGHT(Hit,1) ~= '.' & WordPos < 1 THEN DO
            END
         ELSE
            DO a = 1 to NumExt
            IF UPPER(RIGHT(Hit,4)) == Ext.a THEN DO
               Total = Total + 1
               CALL WriteReq()
            LEAVE
         END
      END
   END
END
'progressclose' Prequest

EndTime = TIME('E')
TotalTime = (EndTime - StartTime)
CALL CLOSE('Input')
address command 'delete >NIL: T:SnapFiles.tmp'   /* Delete temp file */

'unlockgui'

IF FileHit ~= 1 THEN DO  /* Did we find any filenames? */
   Note = 'No Possible files!!'LF'Operation took 'TotalTime' Sec.'
   CALL Notify()
   EXIT
END
ELSE
Request = 'All done found 'Total' File(s)'LF'Operation took 'TotalTime' Sec.'LF'Edit the ReqFile?'
CALL Request()

WriteReq:
FileHit = 1 /* Found one or more files */

IF ~EXISTS(Out) THEN DO
   IF ~OPEN('FileReq',Out,'W') THEN DO
      'progressclose' Prequest
      'unlockgui'
      Note = 'Can not create Req file: 'Out'.'
      CALL Notify()
      EXIT
   END
   ELSE
      CALL WRITELN('FileReq',Hit)
      CALL CLOSE('FileReq')
      RETURN
   END
ELSE
   IF ~OPEN('FileReq',Out,'A') THEN DO
      'progressclose' Prequest
      'unlockgui'
      Note = 'Can not open Req file: 'Out'.'
      CALL Notify()
      EXIT
   END
   ELSE
      CALL WRITELN('FileReq',Hit)
      CALL CLOSE('FileReq')
   RETURN
END

BailOut:  /* --- If user stops the operation, check this --- */
'progressclose' Prequest
'unlockgui'
CALL CLOSE('Input')
address command 'delete >NIL: T:SnapFiles.tmp'   /* Delete temp file */

IF FileHit ~= 1 THEN DO  /* Did we find any filenames ? */
   Note = 'Bailing out!!'LF'Found No Possible file names.'
   CALL Notify()
   EXIT
END
ELSE
Request = 'Bailing out!!'LF'Possible File(s) 'Total''LF'Edit the ReqFile?'
CALL Request()

Notify:
'requestnotify PROMPT "'Note'" CENTER'
RETURN

Request:
'requestresponse TITLE "SnapFiles V1.1" PROMPT "'Request'"
CENTER GADGETS _Edit|_Delete|_No'

IF RC = 0 THEN DO
   EXIT
END
IF RC = 1 THEN DO
   'filerequest'
   EXIT
END
IF RC = 2 THEN DO
   address command delete Out
   EXIT
END

/* --- Error handler (Hopefully it will not be used :) ) --- */
BREAK_C:
SYNTAX:
FAILURE:
'progressclose' Prequest
'unlockgui'
Note = ''Errortext(RC) 'at line' SIGL''
CALL Notify()
EXIT
END
