home *** CD-ROM | disk | FTP | other *** search
- /************************************************************************/
- /* COOKIE.BT - Respond to a "saycookie" message via BackTalk. */
- /* BackTalk looks for a message port named "BT_msg" for external ARexx */
- /* scripts. This program opens up such a port, then sends BackTalk */
- /* a command to notify us when it sees the string "saycookie". It */
- /* sends us the "MATCH" message when it's found. BackTalk will also */
- /* send us a "DIE" message when it's shutting down so we can clean up. */
- /* */
- /* Written by Rick Stevens, 29 July 1991 */
- /* THIS PROGRAM IS RELEASED TO THE PUBLIC DOMAIN */
- /************************************************************************/
- options results /* Methinks we need results */
-
- myport = openport("BT_msg") /* Open up a port for BackTalk */
- if myport = 0 then do /* Did it open? */
- MESSAGE "PORT ALREADY IN USE" /* Nope, must be used already */
- exit /* Exit this script */
- end /* END "if myport..." */
-
- ONSTRING "saycookie" /* Set BT to watch for this */
-
- do forever /* Loop forever */
- packet = getpkt('BT_msg') /* Get a packet */
- do while packet = '00000000'x /* Was it null? */
- call waitpkt('BT_msg') /* Yes, wait for a packet */
- packet = getpkt('BT_msg') /* Get the one that woke us */
- end /* END "do while packet..." */
-
- arg0 = getarg(packet) /* Extract the argument */
- call reply(packet,0) /* Reply to the packet */
- select /* What was the argument */
- when arg0 = 'MATCH' then call findcookie
- /* MATCH! Go get a cookie */
- when arg0 = 'DIE' then call getout
- /* BackTalk is shutting down */
- otherwise nop /* Neither one, ignore packet */
- end /* END "select" */
- end /* END "do forever" */
-
- /************************************************************************/
- /* FINDCOOKIE - Open up the input file. If it doesn't open, tell the */
- /* folks on CIS that the Cookie Monster ate the cookies. If it does */
- /* open, then grab a random number, scale it to the file size, seek */
- /* to the appropriate spot, grab a cookie and send it out. We change */
- /* our name to "Cookie Monster" first, and change it back after. */
- /************************************************************************/
- findcookie: /* Here's where we get a cookie */
- send "/nam Cookie Monster" /* Change name to Cookie Monster*/
- if open(infile,'S:Cookie.dat','r') = 0 then
- do /* Can we open the file? */
- send "Sorry, I ate all of the cookies! <BURP!>"
- end
- else
- do
- flen = seek(infile,0,'E') /* Get file length */
- fpos = time('seconds') /* Get a seed value for RANDU */
- fpos = randu(fpos) /* Get a position and reseed */
- fpos = fpos * flen /* Compute a seek position */
- x = index(fpos,".") /* Find the decimal point */
- fpos = left(fpos,x-1); /* Extract the whole part */
- seek(infile,fpos,'B') /* Seek to the position */
- do while x ~= '0C'x /* While it's not a form feed...*/
- x = readch(infile,1) /* ...read another character */
- end
- x = readch(infile,1) /* Grab the LF following the FF */
-
- breakflag = 0 /* Clear breakflag */
- do while breakflag = 0 /* While we've got a cookie... */
- fstring = readln(infile)/* ...read a line from file */
- if index(fstring,'0C'x) ~= 0 then
- breakflag = 1 /* Found a FF, end of cookie */
- else
- send fstring /* Send string */
- end /* END "do while breakflag..." */
-
- close(infile) /* Close the input file */
- end
- send "/nam Rickly" /* Change name back to normal */
- return /* Return to SELECT loop */
-
- /************************************************************************/
- /* GETOUT - Shut down the message port and exit this script since */
- /* BackTalk is shutting down. We know this by BackTalk sending us the */
- /* "DIE" message. */
- /************************************************************************/
- getout: /* Shutting down */
- closeport("BT_msg") /* Close the message port */
- exit /* Leave this script */
-