home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d6xx / d652 / notify.lha / Notify / NotifyDaily < prev    next >
Text File  |  1992-05-14  |  14KB  |  254 lines

  1. /* NotifyDaily 1.02 */
  2. /* Copyright © Michael Tanzer, 1991, 1992 */
  3. /* See additional notices in accompanying documentation */
  4.  
  5. perpetual = 0                              /* 1 = run daily at midnight    */
  6. dateformat = 'E'                           /* Date format (E, U, or O)     */
  7. datafile = 's:NotifyDaily.data'            /* Name of data file            */
  8.  
  9. /* Make sure the necessary libraries are available */
  10. if ~show('L','rexxsupport.library') then
  11.   call addlib('rexxsupport.library',0,-30)
  12. if ~show('L','rexxarplib.library') then
  13.   call addlib('rexxarplib.library',0,-30)
  14.  
  15. parse arg w .
  16. if upper(w)='HELP' | w='?' then do         /* Handle call for help         */
  17.   call help
  18.   exit
  19.   end
  20. if length(w)>0 then do                     /* Invalid parameter specified  */
  21.   w = 'Invalid parameter specified:' w'\'                                 ||,
  22.       'Format: RX NOTIFYDAILY <HELP|?>\'                                  ||,
  23.       'Enter ''RX NOTIFYDAILY ?'' for more information.\'                 ||,
  24.       'Enter ''RX NOTIFYDAILY''   to run NotifyDaily.'
  25.   call request(0,0,w,,'I''ll try that.')
  26.   exit 78
  27.   end
  28.  
  29. if perpetual then do                       /* Handle perpetual operation   */
  30.   if getenv('NotifyDaily.asynchsw')=1 then do /* Executing asynchronously  */
  31.     call setenv('NotifyDaily.asynchsw',0)  /*     Clear asynch switch      */
  32.     call bumpcount                         /*     Go increment call count  */
  33.     end
  34.   else do                                  /*   Processing user request    */
  35.     call setenv('NotifyDaily.asynchsw',1)  /*     Set switch for asynch    */
  36.     address arexx 'NotifyDaily'            /*     Start asynch task        */
  37.     exit                                   /*     Free up CLI task         */
  38.     end
  39.   end
  40.  
  41. dateformat = upper(dateformat)             /* Make sure dateformat is UC   */
  42. select                                     /* Adjust for different formats */
  43.   when dateformat='E' then do              /*   European                   */
  44.     dateyy = 7
  45.     datemm = 4
  46.     datedd = 1
  47.     template = 'dd''/''mm''/''yy'
  48.     end
  49.   when dateformat='U' then do              /*   U.S.A.                     */
  50.     dateyy = 7
  51.     datemm = 1
  52.     datedd = 4
  53.     template = 'mm''/''dd''/''yy'
  54.     end
  55.   when dateformat='O' then do              /*   Ordered                    */
  56.     dateyy = 1
  57.     datemm = 4
  58.     datedd = 7
  59.     template = 'yy''/''mm''/''dd'
  60.     end
  61.   end                                      /* Note: no 'otherwise'         */
  62.  
  63. colmax = screencols()%8-16                 /* Get max msg length           */
  64. errmax = screencols()%8-5                  /* Get max error msg length     */
  65.  
  66. /* Build day-of-week table */
  67. d.mon = '80'x                              /* Monday                       */
  68. d.tue = '40'x                              /* Tuesday                      */
  69. d.wed = '20'x                              /* Wednesday                    */
  70. d.thu = '10'x                              /* Thursday                     */
  71. d.fri = '08'x                              /* Friday                       */
  72. d.sat = '04'x                              /* Saturday                     */
  73. d.sun = '02'x                              /* Sunday                       */
  74. d.m_f = 'f8'x                              /* Monday through Friday        */
  75. d.s_s = '06'x                              /* Saturday and Sunday          */
  76. d.mwf = 'a8'x                              /* Monday, Wednesday, Friday    */
  77. d.tth = '50'x                              /* Tuesday and Thursday         */
  78. d.all = 'fe'x                              /* Every day                    */
  79.  
  80. do forever                                 /* Perpetual loop               */
  81.   w = open( 'input',datafile,'R')          /* Verify data file             */
  82.   if w~=1 then do                          /* Handle error                 */
  83.     say 'Data file' datafile 'can not be opened.'
  84.     exit 92
  85.     end
  86.   today = date(dateformat)                 /* Get today's date             */
  87.   thisyy = substr(today,dateyy,2)          /* Get today's year             */
  88.   thismm = substr(today,datemm,2)          /* Get today's month            */
  89.   thisdd = substr(today,datedd,2)          /* Get today's day              */
  90.   now = substr(time(),1,5)                 /* Get current time             */
  91.   interpret 'weekday = d.'substr(date('W'),1,3) /* Table value for weekday */
  92.   parse var now hh':'mm                    /* Get hours and minutes        */
  93.   decnow = hh||mm                          /* Save it as decimal value     */
  94.   do forever                               /* Read loop                    */
  95.     inrec = readln('input')                /*   Read a line                */
  96.     if eof('input') then leave             /*   Handle end-of-file         */
  97.     if words(inrec)=0 then iterate         /*   Skip blank lines           */
  98.     date = upper(word(inrec,1))            /*   Get the date               */
  99.     time = upper(word(inrec,2))            /*   Get the time               */
  100.     interpret 'parse var date' template    /*   Get year, month, and day   */
  101.     message = subword(inrec,3)             /*   Get the message            */
  102.     select                                 /*   Check date/day             */
  103.       when date=today then nop             /*     Match on date            */
  104.       when length(date)=3 then do          /*     Check day of week        */
  105.         day = translate(date,'_','-')      /*          Change '-' to '_'   */
  106.         if symbol('d.day')~='VAR' then do  /*          Handle invalid day  */
  107.           prompt = 'Invalid day found in'
  108.           call error
  109.           exit 89
  110.           end
  111.         day = d.day                        /*          Value from table    */
  112.         if bitand(day,weekday)~=weekday then iterate
  113.         end
  114.       when words(yy)>0 then do             /*     Check date               */
  115.         if yy~='??' & yy~=thisyy then iterate
  116.         if mm~='??' & mm~=thismm then iterate
  117.         select                             /*      Handle day formats      */
  118.           when dd='??' then nop            /*       Wildcard               */
  119.           when datatype(dd,'W') then do    /*       Date                   */
  120.             if dd~=thisdd then iterate
  121.             end
  122.           otherwise do                     /*       Weekday                */
  123.             day = translate(substr(dd,1,3),'_','-')  /* Change '-' to '_'  */
  124.             if symbol('d.day')~='VAR' then do        /* Handle invalid day */
  125.               prompt = 'Invalid day of month found in'
  126.               call error
  127.               exit 88
  128.               end
  129.             day = d.day                              /* Value from table   */
  130.             if bitand(day,weekday)~=weekday then iterate
  131.             if length(dd)>3 then do                  /* Nth 'day' of month */
  132.               w = substr(dd,4)                       /* Check 'n'          */
  133.               if (thisdd-1)%7+1~=w then iterate
  134.               end
  135.             end /* otherwise */
  136.           end /* select */
  137.         end /* when words(yy)>0 */
  138.       otherwise do                         /*   Handle unrecognised format */
  139.         prompt = 'Invalid date found in'
  140.         call error
  141.         exit 87
  142.         end
  143.       end
  144.     if length(message)>colmax then do      /*   Handle overlength message  */
  145.       prompt = 'Overlength message found in'
  146.       call error
  147.       exit 86
  148.       end
  149.     if time='NOW' then do                  /*   Issue message now          */
  150.       call 'Notify' 'add' time message     /*     Add message to list      */
  151.       iterate                              /*     Back for next record     */
  152.       end
  153.     if ~vertime(time) then do              /*   Handle invalid time        */
  154.       prompt = 'Invalid time found in'
  155.       call error
  156.       exit 85
  157.       end
  158.     if dectime<decnow then iterate         /*   Its time has passed        */
  159.     call 'Notify' 'add' time message       /*   Add message to list        */
  160.     end                                    /*   End of read loop           */
  161.   call close 'input'                       /* Close input file             */
  162.   if ~perpetual then exit                  /* Exit if no midnight rerun    */
  163.   do forever
  164.     seconds = 86400-time(s)                /*   Number of seconds to wait  */
  165.     if seconds<=600 then leave             /*   Leave loop if <10 min      */
  166.     seconds = seconds*9%10                 /*   Get 90% of seconds         */
  167.     call wait                              /*   Go wait 90% of time        */
  168.     end                                    /*   Go handle remaining 10%    */
  169.   call wait                                /* Wait until midnight          */
  170.   w = getenv('NotifyDaily.count')          /* Get call count               */
  171.   if w~=callcount then exit                /* Exit if new task started     */
  172.   end
  173.  
  174. wait:                                      /* Wait for time to pass        */
  175.   signal on halt                           /* Disable halt interrupt       */
  176.   call delay(seconds*50)                   /* Nod off                      */
  177. halt:
  178.   signal off halt                          /* Enable halt interrupt        */
  179.   return
  180.  
  181. bumpcount:                                 /* Bump call count              */
  182.   callcount = getenv('NotifyDaily.count')  /* Get previous count           */
  183.   if words(callcount)=0 then callcount = 1 /* Use 1 first time             */
  184.   else callcount = callcount+1             /* Bump count                   */
  185.   call setenv('NotifyDaily.count',callcount)  /* Store result              */
  186.   return
  187.  
  188. vertime:                                   /* Verify time                  */
  189.   parse arg hh':'mm                        /* Get hour and minute          */
  190.   if ~datatype(hh,'W') then return 0       /* HH must be a whole number    */
  191.   w = length(mm)                           /* Get length of MM             */
  192.   select                                   /* Handle different formats     */
  193.     when substr(mm,w-1)='AM' then do       /*   AM; 12-hour clock          */
  194.       mm = substr(mm,1,w-2)                /*     Drop 'AM'                */
  195.       if hh<1 | hh>12 then return 0        /*     HH must be between 1 & 12*/
  196.       hh = hh//12                          /*     Change 12 to 0           */
  197.       end
  198.     when substr(mm,w)='A' then do          /*   A; 12-hour clock           */
  199.       mm = substr(mm,1,w-1)                /*     Drop 'A'                 */
  200.       if hh<1 | hh>12 then return 0        /*     HH must be between 1 & 12*/
  201.       hh = hh//12                          /*     Change 12 to 0           */
  202.       end
  203.     when substr(mm,w-1)='PM' then do       /*   PM; 12-hour clock          */
  204.       mm = substr(mm,1,w-2)                /*     Drop 'PM'                */
  205.       if hh<1 | hh>12 then return 0        /*     HH must be between 1 & 12*/
  206.       hh = hh//12+12                       /*     Change 12 to 0, add 12   */
  207.       end
  208.     when substr(mm,w)='P' then do          /*   P; 12-hour clock           */
  209.       mm = substr(mm,1,w-1)                /*     Drop 'P'                 */
  210.       if hh<1 | hh>12 then return 0        /*     HH must be between 1 & 12*/
  211.       hh = hh//12+12                       /*     Change 12 to 0, add 12   */
  212.       end
  213.     otherwise do                           /*   24-hour clock              */
  214.       if hh<0 | hh>23 then return 0        /*     Verify HH                */
  215.       end
  216.     end
  217.   if ~datatype(mm,'W') then return 0       /* MM must be a whole number    */
  218.   if mm<0 | mm>59 then return 0            /* MM must be between 0 and 59  */
  219.   dectime = hh||right(mm,2,'0')            /* Save time as decimal value   */
  220.   return 1                                 /* Indicate success             */
  221.  
  222. error:                                     /* Invalid record in data file  */
  223.   if length(inrec)>errmax then inrec = substr(inrec,1,errmax)
  224.   prompt = prompt datafile'\'translate(inrec,'/','\')
  225.   okay = 'I''m sorry!'
  226.   cancel = 'It won''t happen again!'
  227.   call request(0,0,prompt,,okay,cancel)
  228.   return
  229.  
  230. help:                                      /* Give help                    */
  231.   select                                   /* Handle date formats          */
  232.     when dateformat='E' then df = 'dd/mm/yy'
  233.     when dateformat='U' then df = 'mm/dd/yy'
  234.     when dateformat='O' then df = 'yy/mm/dd'
  235.     end                                    /* Note: no 'otherwise'         */
  236.   w = 'NotifyDaily reads a data file ('datafile') and calls Notify\'      ||,
  237.       'to issue messages for the current day.  The data format is:\'      ||,
  238.       '      date time <message text>\'                                   ||,
  239.       'The date may be expressed either as an actual date ('df') or as\'  ||,
  240.       'a day of the week, as indicated by the following list:\'           ||,
  241.       '      mon tue wed thu fri sat sun m-f s-s mwf tth all\'            ||,
  242.       'If the date format is used, a wildcard (''??'') can be specified\' ||,
  243.       'for the year, month, and/or day.  The day can be specified as a\'  ||,
  244.       'day of the week; suffix this with a number to indicate which\'     ||,
  245.       'occurrence of that day in the month.  For example, specifying\'    ||,
  246.       'MON3 for day and wildcards for year and month will cause the\'     ||,
  247.       'message to be issued on the third monday of each month.\'          ||,
  248.       'The time may be expressed either as a time of day (hh:mm) or as\'  ||,
  249.       '''NOW'', which causes the message to be issued immediately.\'      ||,
  250.       'Times not suffixed with ''AM'' or ''PM'' (or ''A'' or ''P'') are\' ||,
  251.       'assumed to be 24-hour values, e.g. 3:00PM = 15:00; 3:00 = 03:00.'
  252.    call request(0,0,w,,'How wonderful!')
  253.   return
  254.