home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 551.lha / Cookie / Cookie.bt next >
Encoding:
Text File  |  1991-09-08  |  4.0 KB  |  89 lines

  1. /************************************************************************/
  2. /*  COOKIE.BT - Respond to a "saycookie" message via BackTalk.         */
  3. /*  BackTalk looks for a message port named "BT_msg" for external ARexx    */
  4. /*  scripts.  This program opens up such a port, then sends BackTalk    */
  5. /*  a command to notify us when it sees the string "saycookie".  It    */
  6. /*  sends us the "MATCH" message when it's found.  BackTalk will also    */
  7. /*  send us a "DIE" message when it's shutting down so we can clean up.    */
  8. /*                                    */
  9. /*  Written by Rick Stevens, 29 July 1991                */
  10. /*  THIS PROGRAM IS RELEASED TO THE PUBLIC DOMAIN            */
  11. /************************************************************************/
  12. options results                /* Methinks we need results    */
  13.  
  14. myport = openport("BT_msg")        /* Open up a port for BackTalk    */
  15. if myport = 0 then do            /* Did it open?            */
  16.    MESSAGE "PORT ALREADY IN USE"    /* Nope, must be used already    */
  17.    exit                    /* Exit this script        */
  18. end                    /* END "if myport..."        */
  19.  
  20. ONSTRING "saycookie"            /* Set BT to watch for this    */
  21.  
  22. do forever                /* Loop forever            */
  23.    packet = getpkt('BT_msg')        /* Get a packet            */
  24.    do while packet = '00000000'x    /* Was it null?            */
  25.     call waitpkt('BT_msg')        /* Yes, wait for a packet    */
  26.     packet = getpkt('BT_msg')    /* Get the one that woke us    */
  27.    end                    /* END "do while packet..."    */
  28.  
  29.    arg0 = getarg(packet)        /* Extract the argument        */
  30.    call reply(packet,0)            /* Reply to the packet        */
  31.    select                /* What was the argument    */
  32.     when arg0 = 'MATCH' then call findcookie
  33.                     /* MATCH! Go get a cookie    */
  34.     when arg0 = 'DIE' then call getout
  35.                     /* BackTalk is shutting down    */
  36.     otherwise nop            /* Neither one, ignore packet    */
  37.    end                    /* END "select"            */
  38. end                    /* END "do forever"        */
  39.  
  40. /************************************************************************/
  41. /*  FINDCOOKIE - Open up the input file.  If it doesn't open, tell the    */
  42. /*  folks on CIS that the Cookie Monster ate the cookies.  If it does    */
  43. /*  open, then grab a random number, scale it to the file size, seek    */
  44. /*  to the appropriate spot, grab a cookie and send it out.  We change    */
  45. /*  our name to "Cookie Monster" first, and change it back after.    */
  46. /************************************************************************/
  47. findcookie:                /* Here's where we get a cookie    */
  48.     send "/nam Cookie Monster"        /* Change name to Cookie Monster*/
  49.     if open(infile,'S:Cookie.dat','r') = 0 then
  50.     do                /* Can we open the file?    */
  51.         send "Sorry, I ate all of the cookies!  <BURP!>"
  52.     end
  53.      else
  54.     do
  55.         flen = seek(infile,0,'E')    /* Get file length        */
  56.         fpos = time('seconds')    /* Get a seed value for RANDU    */
  57.         fpos = randu(fpos)        /* Get a position and reseed    */
  58.         fpos = fpos * flen        /* Compute a seek position    */
  59.         x = index(fpos,".")        /* Find the decimal point    */
  60.         fpos = left(fpos,x-1);    /* Extract the whole part    */
  61.         seek(infile,fpos,'B')    /* Seek to the position        */
  62.         do while x ~= '0C'x        /* While it's not a form feed...*/
  63.         x = readch(infile,1)    /* ...read another character    */
  64.         end
  65.         x = readch(infile,1)    /* Grab the LF following the FF    */
  66.  
  67.         breakflag = 0        /* Clear breakflag        */
  68.         do while breakflag = 0    /* While we've got a cookie...    */
  69.         fstring = readln(infile)/* ...read a line from file    */
  70.         if index(fstring,'0C'x) ~= 0 then
  71.             breakflag = 1    /* Found a FF, end of cookie    */
  72.         else
  73.             send fstring    /* Send string            */
  74.         end                /* END "do while breakflag..."    */
  75.  
  76.         close(infile)        /* Close the input file        */
  77.     end
  78.     send "/nam Rickly"            /* Change name back to normal    */
  79.     return                /* Return to SELECT loop    */
  80.  
  81. /************************************************************************/
  82. /*  GETOUT - Shut down the message port and exit this script since    */
  83. /*  BackTalk is shutting down.  We know this by BackTalk sending us the    */
  84. /*  "DIE" message.                            */
  85. /************************************************************************/
  86. getout:                    /* Shutting down        */
  87.     closeport("BT_msg")            /* Close the message port    */
  88.     exit                /* Leave this script        */
  89.