home *** CD-ROM | disk | FTP | other *** search
/ Dream 49 / Amiga_Dream_49.iso / amiga / workbench / commodoties / ezcron.lha / ezcron / EZCronD < prev    next >
Text File  |  1998-01-27  |  14KB  |  436 lines

  1. /* $VER: EZCrond by Jim Hines v3.016 ⌐1995-1998 All Rights Reserved
  2. ** Thanks must go to Gene Heskett for his help in coding some functions.
  3. **
  4. */
  5.  
  6.     csi            = '9b'x
  7.     Ital        = csi'3m'
  8.     bold        = csi'1m'
  9.     norm        = csi'0m'
  10.     black        = csi'31m'
  11.     white        = csi'32m'
  12.     blue        = csi'33m'
  13.     LF             = '0a'x                    /* LINEFEED         */
  14.     VER            = 'EZCronD v3.016'        /* Version String     */
  15.     yearpre        = left(date('S'), 2)    /* Year Prefix         */
  16.     
  17.  
  18.     SIGNAL ON ERROR
  19.     SIGNAL ON IOERR
  20.     SIGNAL ON SYNTAX
  21.     SIGNAL ON HALT
  22.     OPTIONS FAILAT 20
  23.     OPTIONS RESULTS
  24.  
  25.     if ~show('L','rexxsupport.library') then
  26.         call addlib('rexxsupport.library',0,-30)
  27.  
  28.     if ~exists('EZCron:prefs/config.prefs') then configfile = 't:cron.config'
  29.     else do
  30.         call open('configprefs', 'EZCron:Prefs/config.prefs', 'R')
  31.         configfile = readln('configprefs')
  32.         call close('configprefs')
  33.     end
  34.  
  35.     if ~exists('env:EZCStartTime') then do
  36.         call open(startinfo, 'env:EZCStartTime', 'W')
  37.         call writeln(startinfo, date(USA) time())
  38.         call writeln(startinfo, VER)
  39.         call close(startinfo)
  40.     end
  41.  
  42.     if exists('s:cron.config') then address command 'copy s:cron.config' configfile
  43.     else do
  44.         call rtezrequest('A config file does NOT exist.'||LF||'Use EZCron to create one.', "Okay",'EZCronD Error',)
  45.         exit
  46.     end
  47.  
  48.     if showlist('P', 'EZCROND') then do
  49.         say 'Exiting EZCron... This may take several seconds.'
  50.         address 'EZCROND' STOP
  51.         exit
  52.     end
  53.     else call startrtn()
  54.     exit
  55.  
  56.     /* =======LOOP== */
  57. startrtn:
  58.     call readprefsfiles()
  59.     call parseconfig()
  60.     do i = 1 to event.0
  61.         if event.i.time = 'startup' then do
  62.             if event.i.sfx ~= '-' then do
  63.                 address command 'run' prefs.6 event.i.sfx
  64.             end
  65.             if event.i.command = 'TEXTREMINDER' then address command 'run <>NIL: rx ezcron:rexx/Reminder.rexx' event.i.txt
  66.             else address command 'run >NIL:' event.i.command event.i.pargs
  67.         end
  68.     end
  69.     call openport('EZCROND')
  70.     say VER
  71.     say '⌐1995-98 Jim Hines'
  72.  
  73.     if showlist('P', 'EZCRONPREFS') then address 'EZCRONPREFS' refresh
  74.  
  75.     /* =======PARSE CONFIG FILE & TIMER SECTION == */
  76.     do forever
  77.         sec            = right(time('N'),2)    /* get seconds */
  78.         sec2        = 60 - sec                /* 60 (seconds) minus sec) */
  79.         delayvar    = sec2 * 50                /* sec2 x 50 ticks or 1 second sets the delay time */
  80.         call delay(delayvar)                /* 3000 would be equal to 1 minute */
  81.  
  82.         pkt = getpkt('EZCROND')
  83.         if pkt ~= '0000 0000'x then call aport(pkt)
  84.  
  85.         clock        = left(time(), 5)        /* 22:58    */
  86.         date        = Date('USA')            /* 08/04/92    */
  87.         date2        = Date('S')                /* 19920804    */
  88.         date3        = Date(W, date2, 'S')    /* Friday    */
  89.  
  90.         call parseconfig()
  91.  
  92.         do i = 1 to event.0
  93.             parse var clock ahrs ':' amin                    /* Actual Hrs and Mins */
  94.             ahrs1 = left(ahrs, 1)
  95.             ahrs2 = right(ahrs, 1)
  96.             amin1 = left(amin, 1)
  97.             amin2 = right(amin, 1)
  98.  
  99.             parse var date amm'/'add'/'ayy                    /* Actual mm dd yy */
  100.             amm1 = left(amm, 1)
  101.             amm2 = right(amm, 1)
  102.             add1 = left(add, 1)
  103.             add2 = right(add, 1)
  104.             ayy1 = left(ayy, 1)
  105.             ayy2 = right(ayy, 1)
  106.  
  107.             timescount = 1                                    /* Get multi-times fields */
  108.             if pos('|', event.i.time, 1) > 0 then do
  109.                 readline = event.i.time
  110.                 do while pos('|', readline) > 0
  111.                     a = pos('|', readline)
  112.                     readline = right(readline, length(readline) - a)
  113.                     timescount = timescount + 1
  114.                 end
  115.             end
  116.  
  117.             do t = 1 to timescount
  118.                 parse var event.i.time event.i.time.t '|' event.i.time
  119.             end
  120.  
  121.             do t = 1 to timescount
  122.                 parse var event.i.time.t ehrs.t':'emin.t      /* Event Hrs and Mins */
  123.                 if pos('#', event.i.time.t, 1) > 0 then do
  124.                     ehrs1.t = left(ehrs.t, 1)
  125.                     ehrs2.t = right(ehrs.t, 1)
  126.                     emin1.t = left(emin.t, 1)
  127.                     emin2.t = right(emin.t, 1)
  128.                 end
  129.             end
  130.  
  131.             datescount = 1                                    /* Get multi-date fields */
  132.             if pos('|', event.i.date, 1) > 0 then do
  133.                 readline = event.i.date
  134.                 do while pos('|', readline) > 0
  135.                     a = pos('|', readline)
  136.                     readline = right(readline, length(readline) - a)
  137.                     datescount = datescount + 1
  138.                 end
  139.             end
  140.  
  141.             do t = 1 to datescount
  142.                 parse var event.i.date event.i.date.t '|' event.i.date
  143.             end
  144.  
  145.             do t = 1 to datescount
  146.                 parse var event.i.date.t emm.t'/'edd.t'/'eyy.t
  147.                 if pos('#', event.i.date.t, 1) > 0 then do
  148.                     emm1.t = left(emm.t, 1)
  149.                     emm2.t = right(emm.t, 1)
  150.                     edd1.t = left(edd.t, 1)
  151.                     edd2.t = right(edd.t, 1)
  152.                     eyy1.t = left(eyy.t, 1)
  153.                     eyy2.t = right(eyy.t, 1)
  154.                 end
  155.             end
  156.  
  157.             parse var event.i.startdate sdmm'/'sddd'/'sdyy  /* Event Startdate mm dd yy */
  158.             if pos('#', event.i.startdate, 1) > 0 then do
  159.                 sdmm1 = left(sdmm, 1)
  160.                 sdmm2 = right(sdmm, 1)
  161.                 sddd1 = left(sddd, 1)
  162.                 sddd2 = right(sddd, 1)
  163.                 sdyy1 = left(sdyy, 1)
  164.                 sdyy2 = right(sdyy, 1)
  165.             end
  166.  
  167.             parse var event.i.enddate edmm'/'eddd'/'edyy      /* Event Enddate mm dd yy */
  168.             if pos('#', event.i.enddate, 1) > 0 then do
  169.                 edmm1 = left(edmm, 1)
  170.                 edmm2 = right(edmm, 1)
  171.                 eddd1 = left(eddd, 1)
  172.                 eddd2 = right(eddd, 1)
  173.                 edyy1 = left(edyy, 1)
  174.                 edyy2 = right(edyy, 1)
  175.             end
  176.  
  177.     /* ======= Misc Routines == */
  178.             if event.i.pargs = '-' then event.i.pargs = ''
  179.             if event.i.sfx = '-' then event.i.sfx = ''
  180.             if event.i.txt = '-' then event.i.txt = ''
  181.  
  182.     /* ======= Date Routines == */
  183.             do t = 1 to datescount
  184.                 /* Days of Week */
  185.                 if event.i.date.t = 'sun' & date3 = 'Sunday' then event.i.date.t = date
  186.                 if event.i.date.t = 'mon' & date3 = 'Monday' then event.i.date.t = date
  187.                 if event.i.date.t = 'tue' & date3 = 'Tuesday' then event.i.date.t = date
  188.                 if event.i.date.t = 'wed' & date3 = 'Wednesday' then event.i.date.t = date
  189.                 if event.i.date.t = 'thu' & date3 = 'Thursday' then event.i.date.t = date
  190.                 if event.i.date.t = 'fri' & date3 = 'Friday' then event.i.date.t = date
  191.                 if event.i.date.t = 'sat' & date3 = 'Saturday' then event.i.date.t = date
  192.                 /* WildCard Support */
  193.                 if pos('#', event.i.date.t, 1) > 0 then do
  194.                     if emm1.t = '#' then emm1.t = amm1
  195.                     if emm2.t = '#' then emm2.t = amm2
  196.                     if edd1.t = '#' then edd1.t = add1
  197.                     if edd2.t = '#' then edd2.t = add2
  198.                     if eyy1.t = '#' then eyy1.t = ayy1
  199.                     if eyy2.t = '#' then eyy2.t = ayy2
  200.                     event.i.date.t = emm1.t||emm2.t'/'edd1.t||edd2.t'/'eyy1.t||eyy2.t
  201.                 end
  202.                 /* Third Friday, 2nd Tues, etc routine */
  203.                 if pos('every', event.i.date.t, 1) > 0 then do
  204.                      parse var event.i.date.t var1 '_' num '_' dow  /* every 2 wed */
  205.                     del3 = date(n)   /* 20 Apr 88 */
  206.                     parse var del3 del1 cm del2
  207.                     drop del1 del2 /* dont even need them */
  208.                     if cm = 'Jan' then cm = 31;if cm = 'Feb' then cm = 28 /* Will need to be changed for leap */
  209.                     if cm = 'Mar' then cm = 31;if cm = 'Apr' then cm = 30
  210.                     if cm = 'May' then cm = 31;if cm = 'Jun' then cm = 30
  211.                     if cm = 'Jul' then cm = 31;if cm = 'Aug' then cm = 31
  212.                     if cm = 'Sep' then cm = 30;if cm = 'Oct' then cm = 31
  213.                     if cm = 'Nov' then cm = 30;if cm = 'Dec' then cm = 31
  214.                     countvar = 0
  215.                     do z = 1 to cm
  216.                         currdate = date(s)                /* 19951221 year month day     */
  217.                         yearmonth = left(currdate, 6)    /* 199512     year month        */
  218.                         if z < 10 then do
  219.                             zdate = yearmonth'0'z   /* 19951201 Add the 0 back for single digit days*/
  220.                             newz = '0'z
  221.                         end
  222.                         else do
  223.                             zdate = yearmonth''z   /* 19951231 */
  224.                             newz = z
  225.                         end
  226.                         myday = Date(W, zdate, 'S')   /* Friday   */
  227.                         myday = left(myday, 3)         /* Fri         */
  228.                         myday = translate(myday, 'abcdefghijklmnopqrstuvwxyz',,
  229.                                         'ABCDEFGHIJKLMNOPQRSTUVWXYZ') /* fri */
  230.                         if myday = dow then countvar = countvar + 1
  231.                         if myday = dow & countvar = num then do
  232.                             year = left(zdate, 4)             /* 1995     */
  233.                             year2 = right(year, 2)             /*   95     */
  234.                             month = right(zdate, 4)            /*     1221 */
  235.                             month2 = left(month, 2)         /*       12    */
  236.                             event.i.date.t = month2'/'newz'/'year2
  237.                             leave
  238.                         end
  239.                     end
  240.                 end
  241.             end
  242.  
  243.     /* ======= Time Routine == */
  244.             do t = 1 to timescount
  245.                 if pos('#', event.i.time.t, 1) > 0 then do
  246.                     if ehrs1.t = '#' then ehrs1.t = ahrs1
  247.                     if ehrs2.t = '#' then ehrs2.t = ahrs2
  248.                     if emin1.t = '#' then emin1.t = amin1
  249.                     if emin2.t = '#' then emin2.t = amin2
  250.                     event.i.time.t = ehrs1.t||ehrs2.t':'emin1.t||emin2.t
  251.                 end
  252.             end
  253.  
  254.     /* ======= Time Range routine == */
  255.             do t = 1 to timescount
  256.                 if event.i.rng1 ~= '--:--' & event.i.rng2 ~= '--:--' then do
  257.                     strng = event.i.rng1
  258.                     parse var strng sthrs':'stmin
  259.                     if pos('#', strng, 1) > 0 then do
  260.                         sthrs1 = left(sthrs, 1)
  261.                         sthrs2 = right(sthrs, 1)
  262.                         stmin1 = left(stmin, 1)
  263.                         stmin2 = right(stmin, 1)
  264.                         if sthrs1 = '#' then sthrs1 = ahrs1
  265.                         if sthrs2 = '#' then sthrs2 = ahrs2
  266.                         if stmin1 = '#' then stmin1 = amin1
  267.                         if stmin2 = '#' then stmin2 = amin2
  268.                         sthrs = sthrs1||sthrs2
  269.                         stmin = stmin1||stmin2
  270.                     end
  271.                     strngdrop = 60 * sthrs
  272.                     strng = strngdrop + stmin
  273.  
  274.                     endrng = event.i.rng2
  275.                     parse var endrng edhrs':'edmin
  276.                     if pos('#', endrng, 1) > 0 then do
  277.                         edhrs1 = left(edhrs, 1)
  278.                         edhrs2 = right(edhrs, 1)
  279.                         edmin1 = left(edmin, 1)
  280.                         edmin2 = right(edmin, 1)
  281.                         if edhrs1 = '#' then edhrs1 = ahrs1
  282.                         if edhrs2 = '#' then edhrs2 = ahrs2
  283.                         if edmin1 = '#' then edmin1 = amin1
  284.                         if edmin2 = '#' then edmin2 = amin2
  285.                         edhrs = edhrs1||edhrs2
  286.                         edmin = edmin1||edmin2
  287.                     end
  288.                     edrngdrop = 60 * edhrs
  289.                     edrng = edrngdrop + edmin
  290.                     currtim = time(m)                     /* current time in minutes since midnight */
  291.                     a = 0
  292.                     if (strng > edrng) & ((currtim < strng) & (currtim > edrng)) then event.i.time.t = 0
  293.                     if (strng < edrng) & ((currtim < strng) | (currtim > edrng)) then event.i.time.t = 0
  294.                     drop strng sthrs sthrs2 sthrs2 stmin stmin1 stmin2 endrng edhrs edhrs1 edhrs2 edmin edmin1 edmin2 currtim
  295.                 end
  296.             end
  297.  
  298.     /* ======= Date Range Routine == */
  299.             if event.i.startdate ~= '--/--/--' & event.i.enddate ~= '--/--/--' then do
  300.                 if pos('#', event.i.startdate, 1) > 0 | pos('#', event.i.enddate, 1) > 0 then do
  301.                     /* Start Range */
  302.                     if sdmm1 = '#' then sdmm1 = amm1
  303.                     if sdmm2 = '#' then sdmm2 = amm2
  304.                     if sddd1 = '#' then sddd1 = add1
  305.                     if sddd2 = '#' then sddd2 = add2
  306.                     if sdyy1 = '#' then sdyy1 = ayy1
  307.                     if sdyy2 = '#' then sdyy2 = ayy2
  308.                     event.i.startdate = sdmm1||sdmm2'/'sddd1||sddd2'/'sdyy1||sdyy2
  309.  
  310.                     /* End Range */
  311.                     if edmm1 = '#' then edmm1 = amm1
  312.                     if edmm2 = '#' then edmm2 = amm2
  313.                     if eddd1 = '#' then eddd1 = add1
  314.                     if eddd2 = '#' then eddd2 = add2
  315.                     if edyy1 = '#' then edyy1 = ayy1
  316.                     if edyy2 = '#' then edyy2 = ayy2
  317.                     event.i.enddate = edmm1||edmm2'/'eddd1||eddd2'/'edyy1||edyy2
  318.                 end
  319.                 a = b = 0
  320.                 sdt = event.i.startdate
  321.                 edt = event.i.enddate
  322.                 parse var sdt tmm'/'tdd'/'tyy
  323.                 sdt = date(c, yearpre''tyy''tmm''tdd, S)    /* # days since Jan 1st, 1900 */
  324.                 parse var edt tmm'/'tdd'/'tyy
  325.                 edt = date(c,yearpre||tyy||tmm||tdd, S)
  326.                 currdt = date(usa)
  327.                 parse var currdt tmm'/'tdd'/'tyy
  328.                 currdt = date(c, yearpre||tyy||tmm||tdd, S)
  329.                 a = 0
  330.                 do t = 1 to datescount
  331.                     if (sdt < edt) & (currdt < sdt) then event.i.date.t = 0    /* This may need reworked! */
  332.                     if (sdt > edt) & (currdt > edt) then event.i.date.t = 0
  333.                 end
  334.                 drop sdt edt currdt tmm tdd tyy
  335.             end
  336.     
  337.     /* ======= Final Routine == */
  338.             do t = 1 to datescount
  339.                 if event.i.date.t = date then do
  340.                     event.i.date = event.i.date.t
  341.                     leave
  342.                 end
  343.             end
  344.             do t = 1 to timescount
  345.                 if event.i.time.t = clock then do
  346.                     event.i.time = event.i.time.t
  347.                     leave
  348.                 end
  349.             end
  350.  
  351.                if event.i.txt = '' & event.i.time = clock & event.i.date = date & event.i.run = '0' then do
  352.                 address command 'run >NIL:' event.i.command event.i.pargs
  353.             end
  354.             else if event.i.time = clock & event.i.date = date & event.i.run = '1' then do
  355.                 address command event.i.command event.i.pargs
  356.             end
  357.             if event.i.time = clock & event.i.date = date & event.i.txt ~= "" then do
  358.                 address command 'run rx <>nil: ezcron:rexx/Reminder.rexx' event.i.txt /*This calls the external rexx proggy for the event display */
  359.                 if showlist(h,SPEAK) then do
  360.                     address command 'echo' '"'event.i.txt'"' '>speak:'
  361.                 end
  362.             end
  363.             if event.i.time = clock & event.i.date = date & event.i.sfx ~= '' then address command 'run >nil:' prefs.6 event.i.sfx
  364.         end       /* end of 'do i = 1 to event.0' */
  365.     end             /* end of do forever */
  366.  
  367.  
  368. /* ======== Arexx Port Message Check == */
  369. aport:
  370.     cmd = getarg(Pkt)
  371.     if cmd = 'STOP' then do
  372.         call reply(Pkt, 0)
  373.         call exiting()
  374.     end
  375.  
  376. parseconfig:
  377.     event.    = 0
  378.     if ~open(cronfile, configfile,'READ') then exit 20
  379.     errors = 0
  380.     do until eof(cronfile)
  381.     /* Grab the line, parse the event and ignore the comments */
  382.         linein = readln(cronfile)
  383.         parse var linein line ';'
  384.         parse var line line '!'
  385.         next = event.0 + 1
  386.  
  387.         parse var line event.next.command','event.next.pargs','event.next.time',',
  388.         event.next.date','event.next.startdate','event.next.enddate',',
  389.         event.next.rng1','event.next.rng2','event.next.sfx','event.next.txt',',
  390.         event.next.run','event.next.desc','
  391.  
  392.         event.next.date = translate(event.next.date, 'abcdefghijklmnopqrstuvwxyz',,
  393.                         'ABCDEFGHIJKLMNOPQRSTUVWXYZ')
  394.                         /* TRANSLATES THE DATE TO LOWERCASE IF NOT NUMERIC */
  395.         event.next.time = translate(event.next.time, 'abcdefghijklmnopqrstuvwxyz',,
  396.                         'ABCDEFGHIJKLMNOPQRSTUVWXYZ')
  397.                         /* TRANSLATES THE TIME TO LOWERCASE IF NOT NUMERIC */
  398.         select
  399.             when event.next.command = "" then iterate
  400.             when event.next.command = "" then do
  401.                 errors =  1
  402.                 iterate
  403.             end
  404.         otherwise event.0 = next
  405.         end
  406.     end
  407.     call close(cronfile)
  408.     return
  409.  
  410. readprefsfiles:
  411.     if exists('EZCron:prefs/EZCron.prefs') then do
  412.         call open('ezconfig','EZCron:Prefs/EZCron.prefs','R')
  413.         do i = 1 to 7
  414.             prefs.i = readln('ezconfig')
  415.         end
  416.         call close('ezconfig')
  417.     end
  418.     else do
  419.         prefs.6    = 'c:play16'
  420.     end
  421.     return
  422.  
  423. syntax:
  424. ioerr:
  425. error:
  426.     errorrc = RC
  427.     if ~show('L','rexxreqtools.library') then call addlib('rexxsupport.library',0,-30)
  428.     call rtezrequest('EZCronD has exited with an error in event'||LF||event.i.command||LF||'Line' SIGL ERRORTEXT(errorrc),'Okay!','**EZCronD Fatal Error**','rt_reqpos = reqpos_centerscr rtez_flags=ezreqf_centertext',)
  429.     call exiting()
  430.  
  431. exiting:
  432.     call closeport('EZCROND')
  433.     if showlist('P', 'EZCRONPREFS') then address 'EZCRONPREFS' refresh
  434.     address command 'avail flush >NIL:'
  435.     exit
  436.