home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 616.lha / Notify_v1.01 / NotifyDaily < prev    next >
Text File  |  1992-02-21  |  12KB  |  225 lines

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