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

  1. /* Notify 1.01 */
  2. /* Copyright © Michael Tanzer, 1991, 1992 */
  3. /* See additional notices in accompanying documentation */
  4.  
  5. twentyfour = 0                             /* 0 = 12-hour; 1 = 24-hour     */
  6.  
  7. /* Make sure the necessary libraries are available */
  8. if ~show('L','rexxsupport.library') then
  9.   call addlib('rexxsupport.library',0,-30)
  10. if ~show('L','rexxarplib.library') then
  11.   call addlib('rexxarplib.library',0,-30)
  12.  
  13. if twentyfour then do                      /* Adjust for 24-hour clock     */
  14.   colmax = screencols()%8-16               /*   Get max msg length         */
  15.   lprompt1 = '  · Message list ·\'
  16.   lprompt2 = '  Nº hh:mm Message'
  17.   end
  18. else do                                    /* Adjust for 12-hour clock     */
  19.   colmax = screencols()%8-18               /*   Get max msg length         */
  20.   lprompt1 = '  ·  Message list  ·\'
  21.   lprompt2 = '  Nº  Time   Message'
  22.   end
  23. linemax = (screenrows()-94)%9              /* Get maximum lines            */
  24. aprompt  = 'Adding new message.\Enter time and optional message text:'
  25.  
  26. arg request .                              /* Get first argument           */
  27.  
  28. /* Handle list request */
  29. if request='LIST' | request='' then do
  30.   call showlist
  31.   exit
  32.   end
  33.  
  34. /* Handle delete request */
  35. if request='DELETE' then do
  36.   parse arg . dellist                      /* Get list of msgs numbers     */
  37.   call delmsgs                             /* Go delete messages           */
  38.   say response                             /* Type response                */
  39.   exit result                              /* Exit with appropriate code   */
  40.   end
  41.  
  42. /* Handle edit request */
  43. if request='EDIT' then do
  44.   parse arg . thismsg .                    /* Get message number           */
  45.   call edmsg                               /* Go edit the message          */
  46.   say response                             /* Type response                */
  47.   exit result                              /* Exit with appropriate code   */
  48.   end
  49.  
  50. /* Handle help request */
  51. if request='HELP' | request='?' then do
  52.   call help
  53.   exit
  54.   end
  55.  
  56. /* Handle quiet request */
  57. if request='QUIET' then do
  58.   call setenv('Notify.quiet',1)
  59.   say 'Quiet flag set.'
  60.   exit
  61.   end
  62.  
  63. /* Handle noisy request */
  64. if request='NOISY' then do
  65.   call setenv('Notify.quiet')
  66.   say 'Quiet flag deleted.'
  67.   exit
  68.   end
  69.  
  70. /* Handle invalid request */
  71. if request~='ADD' then do
  72.   say 'Invalid request.'
  73.   exit 99
  74.   end
  75.  
  76. /* Add a new message */
  77. parse arg . time message                   /* Get time and message         */
  78. message = strip(message,'B')               /* Remove blanks fore and aft   */
  79. if words(time)=0 then do                   /* If no time specified...      */
  80.   prompt = aprompt
  81.   timemsg = ''
  82.   call getmsg                              /* Go get new time/message      */
  83.   if result=0 then do                      /* Exit if no message           */
  84.     say response
  85.     exit 0
  86.     end
  87.   end
  88. else do                                    /*  Time was specified          */
  89.   if ~vertime(time) then do                /*  If time is invalid...       */
  90.     say 'Invalid time specified.'
  91.     exit 98
  92.     end
  93.   if length(message)>colmax then do        /*  If message is too long...   */
  94.     say 'Maximum message length is' colmax 'characters.'
  95.     exit 97
  96.     end
  97.   end
  98. call addmsg                                /* Go add message to list       */
  99. say response
  100. exit
  101.  
  102. /* Subroutines */
  103.  
  104. addmsg:                                    /* Add message to list          */
  105.   call forbid()                            /* Hold other tasks             */
  106.   lastmsg = getenv('Notify.lastmsg')       /* Get last msg number          */
  107.   if words(lastmsg)=0 then lastmsg = 0     /* Use zero first time          */
  108.   thismsg = lastmsg+1                      /* Bump msg number              */
  109.   if thismsg>9999 then thismsg = 1         /* Limit number to 4 digits     */
  110.   call setenv('Notify.lastmsg',thismsg)    /* Store it                     */
  111.   msglist = getenv('Notify.list') thismsg  /* Get list of msg numbers      */
  112.   call setenv('Notify.list',msglist)       /* Store new list               */
  113.   call setenv('Notify.twentyfour',twentyfour) /* Store for NotifyTimer     */
  114.   call permit()                            /* Free other tasks             */
  115.   timemsg = strip(time,'B') strip(message,'B')
  116.   call setenv('Notify.'thismsg,timemsg)    /* Store time and message       */
  117.   response = 'Message' thismsg 'added.'    /* Set the response             */
  118.   address arexx 'NotifyTimer' time thismsg /* Launch timer task            */
  119.   return
  120.  
  121. delmsg:                                    /* Delete message from list     */
  122.   call forbid()                            /* Hold other tasks             */
  123.   msglist = getenv('Notify.list')          /* Get list of msg numbers      */
  124.   w = find(msglist,thismsg)                /* Get word number of our msg   */
  125.   if w=0 then do                           /* If msg already deleted...    */
  126.     call permit()                          /*   Free other tasks           */
  127.     return 0                               /*   Indicate failure           */
  128.     end
  129.   msglist = strip(delword(msglist,w,1),'B')/* Delete our msg from list     */
  130.   call setenv('Notify.list',msglist)       /* Store new list               */
  131.   call permit()                            /* Free other tasks             */
  132.   call setenv('Notify.'thismsg)            /* Delete the message           */
  133.   return 1                                 /* Indicate success             */
  134.  
  135. getmsg:                                    /* Get new time and/or message  */
  136.   do forever                               /* Prompt for time and msg      */
  137.     timemsg = request(0,0,prompt,timemsg,'OK','Never mind!')
  138.     if words(timemsg)=0 then do            /* Return if no time entered    */
  139.       response = 'Add operation aborted.'
  140.       return 0
  141.       end
  142.     time = word(timemsg,1)                 /* Get the time                 */
  143.     message = subword(timemsg,2)           /* Get the message              */
  144.     if length(message)>colmax then do      /* If message is too long...    */
  145.       prompt = aprompt'\Message truncated to' colmax 'characters.'
  146.       message = substr(message,1,colmax)   /*   Truncate message           */
  147.       timemsg = time message
  148.       iterate
  149.       end
  150.     if ~vertime(time) then do              /* If time is invalid...        */
  151.       prompt = aprompt'\Invalid time specified.'
  152.       iterate
  153.       end
  154.     return 1                               /*  All is well                 */
  155.     end
  156.  
  157. edmsg:                                     /* Edit message                 */
  158.   if words(thismsg)=0 then do              /* Handle no message specified  */
  159.     response = 'No message number specified.'
  160.     return 96
  161.     end
  162.   timemsg = getenv('Notify.'thismsg)       /* Get the time and message     */
  163.   time =  word(timemsg,1)                  /* Get the time                 */
  164.   if words(time)=0 then do                 /* Handle no such message       */
  165.      response = 'Message' thismsg 'not found.'
  166.      return 95
  167.     end
  168.   message = subword(timemsg,2)             /* Get the message              */
  169.   call addsuffix                           /* Adjust for 12-hour clock     */
  170.   eprompt = 'Editing message number' thismsg 'for' strip(showtime,'B')
  171.   prompt = eprompt                         /* Get heading                  */
  172.   do forever                               /* Prompt for time and msg      */
  173.     message = request(0,0,prompt,message,'OK','Never mind!')
  174.     if words(message)=0 then do            /* Return if no time entered    */
  175.       response = 'Edit operation aborted.'
  176.       return 0
  177.       end
  178.     message = strip(message,'B')           /* Drop some blanks             */
  179.     if length(message)>colmax then do      /* If message is too long...    */
  180.       prompt = eprompt'\Message truncated to' colmax 'characters.'
  181.       message = substr(message,1,colmax)   /* Truncate message             */
  182.       iterate
  183.       end
  184.     leave                                  /*  All is well                 */
  185.     end
  186.   timemsg = time message                   /* Get time and message         */
  187.   call forbid()                            /* Hold other tasks             */
  188.   msglist = getenv('Notify.list')          /* Get message list             */
  189.   if find(msglist,thismsg)=0 then do       /* If message has been deleted, */
  190.     call permit()
  191.     response = 'Message' thismsg 'has been deleted.'
  192.     return 94
  193.     end
  194.   call setenv('Notify.'thismsg,timemsg)    /* Store time and message       */
  195.   call permit()                            /* Free other tasks             */
  196.   response = 'Message revised.'
  197.   return 0
  198.  
  199. delmsgs:                                   /* Delete a list of messages    */
  200.   delcnt = words(dellist)                  /* Get number of msg numbers    */
  201.   if upper(dellist)='ALL' then do          /* If ALL specified...          */
  202.     dellist = getenv(Notify.list)          /*   Get list of all messages   */
  203.     delcnt = words(dellist)                /*   Get number of messages     */
  204.     if delcnt=0 then do                    /*   Handle no msg numbers      */
  205.       response = 'No messages found.'
  206.       return 0
  207.       end
  208.     end
  209.   else if delcnt=0 then do                 /* No message numbers specified */
  210.     response = 'No message number specified.'
  211.     return 93
  212.     end
  213.   msglist = getenv('Notify.list')          /* Get message list             */
  214.   do dx = 1 to delcnt                      /* Verify the msg numbers       */
  215.     thismsg = word(dellist,dx)             /*   Get a msg number           */
  216.     if find(msglist,thismsg)=0 then do     /*   If not found...            */
  217.       response = 'Message number' thismsg 'not found.'
  218.       return 92
  219.       end
  220.     end
  221.   do dx = 1 to delcnt                      /* Delete the messages          */
  222.     thismsg = word(dellist,dx)             /*   Get a msg number           */
  223.     call delmsg                            /*   Go delete the message      */
  224.     end
  225.   if delcnt>1 then response = 'Messages deleted.'
  226.   else response = 'Message deleted.'
  227.   return 0
  228.  
  229. vertime:                                   /* Verify time                  */
  230.   arg uctime                               /* Get time in upper case       */
  231.   if uctime='NOW' then return 1            /* 'NOW' is valid               */
  232.   parse var uctime hh':'mm                 /* Get hour and minute          */
  233.   if ~datatype(hh,'W') then return 0       /* HH must be a whole number    */
  234.   if hh<0 then return 0                    /* HH must be positive          */
  235.   if length(mm)<2 then mm = right(mm,2,'0')/* MM must have 2 digits        */
  236.   w = length(mm)                           /* Get length of MM             */
  237.   select                                   /* Handle different formats     */
  238.     when substr(mm,w-1)='AM' then do       /*   AM; 12-hour clock          */
  239.       mm = substr(mm,1,w-2)                /*     Drop 'AM'                */
  240.       if hh<1 | hh>12 then return 0        /*     HH must be between 1 & 12*/
  241.       hh = hh//12                          /*     Change 12 to 0           */
  242.       end
  243.     when substr(mm,w)='A' then do          /*   A; 12-hour clock           */
  244.       mm = substr(mm,1,w-1)                /*     Drop 'A'                 */
  245.       if hh<1 | hh>12 then return 0        /*     HH must be between 1 & 12*/
  246.       hh = hh//12                          /*     Change 12 to 0           */
  247.       end
  248.     when substr(mm,w-1)='PM' then do       /*   PM; 12-hour clock          */
  249.       mm = substr(mm,1,w-2)                /*     Drop 'PM'                */
  250.       if hh<1 | hh>12 then return 0        /*     HH must be between 1 & 12*/
  251.       hh = hh//12+12                       /*     Change 12 to 0, add 12   */
  252.       end
  253.     when substr(mm,w)='P' then do          /*   P; 12-hour clock           */
  254.       mm = substr(mm,1,w-1)                /*     Drop 'P'                 */
  255.       if hh<1 | hh>12 then return 0        /*     HH must be between 1 & 12*/
  256.       hh = hh//12+12                       /*     Change 12 to 0, add 12   */
  257.       end
  258.     when hh=0 | hh>12 then do              /*   24-hour time specified     */
  259.       if hh>23 then return 0               /*     HH must be less than 24  */
  260.       end
  261.     when twentyfour then nop               /*   24-hour time expected      */
  262.     otherwise do                           /*   May be either AM or PM     */
  263.       if ~datatype(mm,'W') then return 0   /*     MM must be a whole number*/
  264.       now = substr(time(),1,5)             /*     Get current time...      */
  265.       decnow = substr(now,1,2)||substr(now,4,2)   /* ... as decimal value  */
  266.       hh = hh//12                          /*     Change 12 to 0           */
  267.       dectime = hh||right(mm,2,'0')        /*     Get time as decimal value*/
  268.       dectimepm = dectime+1200             /*     Get decimal time for PM  */
  269.       select                               /*     Adjust HH as required    */
  270.         when dectime>decnow then nop       /*       Now=AM Time=Later AM   */
  271.         when decnow<1200 then hh = hh+12   /*       Now=AM Time=PM         */
  272.         when dectimepm>decnow then hh = hh+12/*     Now=PM Time=Later PM   */
  273.         otherwise nop                      /*       Now=PM Time=Tomorrow AM*/
  274.         end /* select */
  275.       end /* otherwise do */
  276.     end /* select */
  277.   if ~datatype(mm,'W') then return 0       /* MM must be a whole number    */
  278.   if mm<0 | mm>59 then return 0            /* MM must be between 0 and 59  */
  279.   time = right(hh,2,'0')':'right(mm,2,'0') /* Ensure 2 digits for HH, MM   */
  280.   return 1                                 /* Indicate success             */
  281.  
  282. addsuffix:                                 /* Get time to be displayed     */
  283.   if ~twentyfour then do                   /* Adjust for 12-hour clock     */
  284.     parse var time hh':'mm                 /*   Separate hour and minute   */
  285.     if hh<12 then suffix = 'am'            /*   Set for am...              */
  286.     else suffix = 'pm'                     /*   ...or pm                   */
  287.     hh = hh//12                            /*   Limit hour to 12           */
  288.     if hh=0 then hh = '12'                 /*   Handle 12                  */
  289.     showtime = right(hh':'mm,5)||suffix    /*   Build time for display     */
  290.     end
  291.   else showtime = time                     /* No change for 24-hour clock  */
  292.   return
  293.  
  294. showlist:                                  /* Show list of pending msgs    */
  295.   call screentofront()                     /* Allow for hot keys           */
  296.   response = ''                            /* No response yet              */
  297.   listmax = linemax                        /* Set maximum message lines    */
  298.   do forever
  299.     msglist = getenv('Notify.list')        /* Get list of msg numbers      */
  300.     msgcnt = words(msglist)                /* Get number of messages       */
  301.     linecnt = 0                            /* Clear line count             */
  302.     prompt = lprompt1||lprompt2            /* Build heading                */
  303.     if msgcnt=0 then do                    /* Handle no messages           */
  304.       prompt = lprompt1||'No messages found.'
  305.       linecnt = 1
  306.       end
  307.     else do mx = 1 to msgcnt               /* Get each message...          */
  308.       if linecnt=listmax then do           /*   If max lines done...       */
  309.         linecnt = 0                        /*     Clear line count         */
  310.         if length(response)>0 then prompt = prompt'\'response
  311.         input = request(0,0,prompt,'More...',,'Quit')
  312.         if words(input)=0 then return      /*     Return if cancelled      */
  313.         if input~='More...' then leave     /*     Something was typed in   */
  314.         response = ''                      /*     Clear response           */
  315.         listmax = linemax                  /*     Reset maximum            */
  316.         prompt = lprompt1||lprompt2        /*     Refresh heading          */
  317.         end
  318.       thismsg = word(msglist,mx)           /*   Get next message number    */
  319.       timemsg = getenv('Notify.'thismsg)   /*   Get next message           */
  320.       if words(timemsg)=0 then iterate     /*   Bypass deleted message     */
  321.       time = upper(word(timemsg,1))        /*   Get time                   */
  322.       if time='NOW' then time =  substr(time(),1,5)
  323.       call addsuffix                       /*   Adjust for 12-hour clock   */
  324.       message = subword(timemsg,2)         /*   Get message                */
  325.       w = right(thismsg,4) showtime translate(message,'/','\')
  326.       prompt = prompt'\'w                  /*   Add message on new line    */
  327.       linecnt = linecnt+1                  /*   Bump line count            */
  328.       end
  329.     if linecnt>0 then do                   /* Display remaining messages   */
  330.       if length(response)>0 then prompt = prompt'\'response
  331.       input = request(0,0,prompt,'',,'Quit',)
  332.       end
  333.     if words(input)=0 then return          /* Return if cancelled          */
  334.     response = ''                          /* Clear response               */
  335.     listmax = linemax                      /* Reset maximum                */
  336.     parse upper var input request .        /* Isolate request              */
  337.     if request='LIST' then iterate         /* Back to top for re-display   */
  338.     if request='EDIT' then do              /* Handle edit request          */
  339.       parse var input . thismsg .          /*   Get message number         */
  340.       call edmsg                           /*   Go edit the message        */
  341.       if result~=0 then call listerror     /*   Handle error if any        */
  342.       else listmax = listmax-1             /*   Set to display response    */
  343.       iterate
  344.       end
  345.     if request='DELETE' then do            /* Handle delete request        */
  346.       parse var input . dellist            /*   Get list of msgs numbers   */
  347.       call delmsgs                         /*   Go delete messages         */
  348.       if result~=0 then call listerror     /*   Handle error if any        */
  349.       else listmax = listmax-1             /*   Set to display response    */
  350.       iterate
  351.       end
  352.     if request='HELP' | request='?' then do/* Handle help request          */
  353.       call help
  354.       iterate
  355.       end
  356.     if request='QUIET' then do             /* Handle quiet request         */
  357.       call setenv('Notify.quiet',1)
  358.       response = 'Quiet flag set.'
  359.       listmax = listmax-1
  360.       iterate
  361.       end
  362.     if request='NOISY' then do             /* Handle noisy request         */
  363.       call setenv('Notify.quiet')
  364.       response = 'Noisy flag set.'
  365.       listmax = listmax-1
  366.       iterate
  367.       end
  368.     if request~='ADD' then do              /* Handle invalid request       */
  369.       response = 'Invalid request.'
  370.       call listerror
  371.       iterate
  372.       end
  373.     time = word(input,2)                   /* Get the time                 */
  374.     message = subword(input,3)             /* Get the message              */
  375.     if words(time)=0 then do               /* If no time specified...      */
  376.       prompt = aprompt
  377.       timemsg = ''
  378.       call getmsg                          /*   Go get new time/message    */
  379.       listmax = listmax-1                  /*   Set to display response    */
  380.       if result=0 then iterate             /*   Back to top if no message  */
  381.       end
  382.     else do                                /* Time was specified           */
  383.       if ~vertime(time) then do            /*   If time is invalid...      */
  384.         prompt = aprompt'\Invalid time specified.'
  385.         timemsg = time message
  386.         call getmsg                        /*     Go get message           */
  387.         listmax = listmax-1                /*     Set to display response  */
  388.         if result=0 then iterate           /*     To top if no message     */
  389.         end
  390.       if length(message)>colmax then do    /*   If message is too long...  */
  391.         prompt = aprompt'\Maximum message length is' colmax 'characters.'
  392.         timemsg = time message
  393.         call getmsg                        /*     Go get message           */
  394.         listmax = listmax-1                /*     Set to display response  */
  395.         if result=0 then iterate           /*     To top if no message     */
  396.         end
  397.       end
  398.     call addmsg                            /* Go add message to list       */
  399.     listmax = listmax-1                    /* Set to display response      */
  400.     end
  401.  
  402. listerror:                                 /* Handle list request error    */
  403.   w = request(0,0,response,,'Return to list','Quit')
  404.   response = ''                            /* Clear response               */
  405.   if w='OKAY' then return
  406.   exit
  407.  
  408. help:                                      /* Give help                    */
  409.   if twentyfour then w = '24'
  410.   else w = '12'
  411.   w = 'NOTIFY issues messages at specified times of day.\'              || ,
  412.       'Valid requests are:\'                                            || ,
  413.       'ADD time <message text>         Add a message\'                  || ,
  414.       'DELETE msgno <msgno <msgno...>> Delete one or more messages\'    || ,
  415.       'EDIT msgno                      Edit the text of a message\'     || ,
  416.       'LIST                            List pending messages (default)\'|| ,
  417.       'HELP                            You are here\'                   || ,
  418.       'QUIET                           Keeps messages quiet\'           || ,
  419.       'NOISY                           Makes messages noisy (default)\' || ,
  420.       '· The time may be expressed as ''hh:mm'' ('w'-hour clock)\'      || ,
  421.       '  or as ''NOW'' to cause a message to be issued immediately.\'   || ,
  422.       '· Inserting a backslash into the text of a message will cause\'  || ,
  423.       '  the following characters to be written on the next line.\'     || ,
  424.       '· If the first word of a message is ''CMD'' the remainder of\'   || ,
  425.       '  the message will be passed to Amiga DOS as a command.\'        || ,
  426.       '· Maximum message text length is' colmax 'characters.\'
  427.   call request(0,0,w,,'Oh, thank you!',,)
  428.   return
  429.