/* BulkMail.thor - Neil Bothwick - v1.0 (8/1/96)           */
/* Searches the User database for Comments containing the  */
/* supplied string, and then sends each match a copy of    */
/* the currently selected EMail event.                     */


/* Parts of this script have been 'borrowed' from    */
/* SelectedReply2Enter.thor by Troels Walsted Hansen */


options results

EVE_ENTERMSG      =  0            /* Event is enter   */
EDF_DELETED       = '00000001'x   /* Event is deleted */
EDF_DONE          = '00000004'x   /* Event is done    */
EDF_FREEZE        = '00000020'x   /* Event is frozen  */

/* needs THOR and bbsread.library functions */

p = address() || ' ' || show('P',,)
thorport = pos('THOR.',p)

if thorport > 0 then thorport = word(substr(p,thorport),1)
else do
    say 'No THOR port found!'
        exit 10
    end

if ~show('p', 'BBSREAD') then do
    address command
    "run >nil: `GetEnv THOR/THORPath`bin/LoadBBSRead"
    "WaitForPort BBSREAD"
    end

/* get current BBS selected event number */

address(thorport)
CURRENTSYSTEM stem CURRENT
System = CURRENT.BBSNAME
drop CURRENT
GETSELECTEDEVENT
if(rc ~= 0) then do
    address(thorport)
    errstring = THOR.LASTERROR
    if rc = 5 then errstring = 'Event window not open'
    REQUESTNOTIFY '"'errstring'"' '"Abort"'
    exit
    end


/* Get event details */

EventNo = result
address(bbsread)
READBREVENT '"'System'"' eventnr EventNo datastem EVENTDATA tagsstem EVENTTAGS
if(rc ~= 0) then call BBSError()

/* Make sure it is an EMail event*/

if upper(EVENTTAGS.CONFERENCE) ~= 'EMAIL' then do
    address(thorport)
    REQUESTNOTIFY '"Selected event is not an EMail event"' '"Abort"'
    exit
    end

/* Make sure it is an Enter event*/

if EVENTDATA.EVENTTYPE ~= EVE_ENTERMSG then do
    address(thorport)
    REQUESTNOTIFY '"Selected event is not an Enter event"' '"Abort"'
    exit
    end

/* Get string to match comment field on */

address(thorport)
REQUESTSTRING TITLE '"Comment string"' BT '"_OK|_Cancel"' BODY '"Enter string to search for\nin User Database Comments"' ID '"'EVENTTAGS.TOADDR'"'
MatchString = result
if(rc ~= 0) then do
    address(thorport)
    if (rc ~= 5) then REQUESTNOTIFY '"'THOR.LASTERROR'"' '"Abort"'
    exit
    end

/* Search User Database for matches */

address(bbsread)
SEARCHBRUSER '"'System'"' STEM SearchResult SEARCH '"#?'MatchString'#?"' COMMENT
if(rc ~= 0) then call BBSError()

if SearchResult.COUNT = 0 then do
    address(thorport)
    REQUESTNOTIFY '"No matches for' MatchString 'found in User database"' '"OK"'
    exit
    end

address(thorport)
REQUESTNOTIFY '"'SearchResult.COUNT 'matches found"' '"_OK|_Cancel"'
if (result ~= 1) then exit

/* Read addresses from User Database */

address(bbsread)
do User = 1 to SearchResult.COUNT
    UserNo = SearchResult.User.USERNR
    READBRUSER BBSNAME '"'System'"' USERNR SearchResult.User.USERNR TAGSSTEM USERTAGS
    if(rc ~= 0) then call BBSError()

    /* Replace address with that from database and write event */

    EVENTTAGS.TOADDR = USERTAGS.ADDRESS
    WRITEBREVENT BBSNAME '"'System'"' EVENT EVE_ENTERMSG STEM EVENTTAGS
    if(rc ~= 0) then call BBSError()
    end


address(thorport)
REQUESTNOTIFY '"'User-1 'Email events created\nDelete original event?"' '"_Yes|_No"'
if (result = 1) then do
    address(bbsread)
    UPDATEBREVENT bbsname '"'System'"' eventnr EventNo SETDELETED
    end

exit

/*==================================================================================*/

BBSError:
    address(thorport)
    REQUESTNOTIFY '"'BBSREAD.LASTERROR'"' '"Abort"'
    exit

