/*            -+- ListUsers 1.1 by Anthony Brice -+-                   */

/* Call from Spot Arexx menu in any echo - filename will be Users.<echoname> */

/*

1.0 Initial Version.

1.1 Updated to use message list window - 10 * as fast at least :)

    Fixed the non return to current message if Stop was pressed.

    Asks if it should post a message rather than Config option, once it
    has finished executing the script.

    Locks GUI while executing, and exits safely if abort is pressed now.

*/

Address SPOT
Options RESULTS

/* +++ Configuration Options +++ */

Directory='Workbench:Spot/' /* Storage directory of UserLists - must end in / or : */
Addresslist='YES'           /* Set to YES to list addresses or NO for none */
Pad=20                      /* Length of spacing after name for page formatting */
Info='YES'                  /* Set this to YES for header and footer info, or No for none */
Name='All'                  /* Name of reciever of message - if applicable */
Subject='User Listing'      /* Subject of message - if applicable */

/* +++ End of Config lines - Edit past here at your own risk +++ */

'progressclose 1'

'messagelist'
Origin='UserList v1.1 by Anthony Brice' 
'requestresponse TITLE " UserList 1.1 by Anthony Brice - 2:254/255.1" PROMPT "Create a list of all users in this Area?" GADGETS "Yes|Abort"'
If RC=0 then Exit
'lockgui'
'getareaname'
AreaName=RESULT
'getmessagenum'
CurrentMessage=RESULT
'getnummsgs'
TotalMessages=RESULT
Message=1
Users=0
Added=0

'firstmessage'
'progressopen TITLE "Creating a Userlist" PROMPT "Please Wait..."'
Requestor=RESULT
FilePath=Directory||"Users."||AreaName
Header='User Listing for Echo: '||AreaName
Footer='Generated by ListUsers.Spot created by Anthony Brice - '
Call Open(Plop,FilePath,"W")
If Info='YES' then do
  Call WriteLN(Plop," ")
  Call WriteLN(Plop,Header)
  Call WriteLN(Plop," ")
  End
Do Until Message=TotalMessages
  'nextmessage'
  Message=Message+1
  'getfrom'
  From=Left(RESULT,pad)
  Out=0
  Do I=0 to Users
    If Name.I = From then Out=1
    End
  If Out=0 then do
    Users=Users+1
    Added=Added+1
    Name.Users=From
    Line=From
    If AddressList='YES' then do
      'getfromaddress'
      FromAddress=RESULT  
      Line=Line||' - Address: '||FromAddress
      End
    Call WriteLN(Plop,Line)
    End
  'progressupdate' Requestor Message TotalMessages 'PROMPT "'added' User(s) added to Names list"'
  If RC>0 then Do
    'progressclose' Requestor
    Call Close(Plop)
    Address Command 'Delete >NIL: 'FilePath
    'gotomessage TO "'CurrentMessage'"'
    'unlockgui'
    Exit 0
    End
  End
If Info='YES' then do
  Call WriteLN(Plop," ")
  Footer=Footer||added||' Users found.'
  Call WriteLN(Plop,Footer)
  Call WriteLN(Plop," ")
  End
Call Close(Plop)
'progressclose' Requestor
'requestresponse TITLE "ListUsers Request" PROMPT "Post List as a message?" GADGETS "_Yes |_No" center'
select
  When RC=1 then do
    'write TO "'||Name||'" SUBJECT "'||Subject||'" ORIGIN "'||Origin||'" FILE "'||FilePath||'" REFLOW=OFF NOEDIT NOGUI'
    End
Otherwise
  End
'requestnotify PROMPT "User Index compiled."'
'gotomessage TO "'CurrentMessage'"'
'unlockgui'
Exit 0

