home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / awget167.zip / awgexec.cmd < prev    next >
OS/2 REXX Batch file  |  2001-06-11  |  26KB  |  889 lines

  1. /* Auto WGet Download Utility
  2.  *
  3.  * Great idea and first release (C) 1998 Steve Trubachev
  4.  * Final release (C) 1998-2001 Dmitry A.Steklenev
  5.  *
  6.  * Redistribution and use in source and binary forms, with or without
  7.  * modification, are permitted provided that the following conditions
  8.  * are met:
  9.  *
  10.  * 1. Redistributions of source code must retain the above copyright
  11.  *    notice, this list of conditions and the following disclaimer.
  12.  *
  13.  * 2. Redistributions in binary form must reproduce the above copyright
  14.  *    notice, this list of conditions and the following disclaimer in
  15.  *    the documentation and/or other materials provided with the
  16.  *    distribution.
  17.  *
  18.  * 3. All advertising materials mentioning features or use of this
  19.  *    software must display the following acknowledgment:
  20.  *    "This product includes software developed by Dmitry A.Steklenev".
  21.  *
  22.  * 4. Redistributions of any form whatsoever must retain the following
  23.  *    acknowledgment:
  24.  *    "This product includes software developed by Dmitry A.Steklenev".
  25.  *
  26.  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR OR CONTRIBUTORS "AS IS"
  27.  * AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  28.  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  29.  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  30.  * AUTHOR OR THE CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  31.  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  32.  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  33.  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  34.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  35.  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  36.  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  37.  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38.  *
  39.  * $Id: awgexec.cms,v 1.33 2001/05/11 08:54:33 glass Exp $
  40.  */
  41.  
  42. globals = "cfg. local. msg. sys. color. dir. jobs. job. plugins."
  43.  
  44. if translate( value( "AWGET_TRACE",, "OS2ENVIRONMENT" )) == "YES" then do
  45.    call  value "AWGET_TRACE", "", "OS2ENVIRONMENT"
  46.    trace intermediate
  47.    trace results
  48. end
  49.  
  50. call AwInit
  51.  
  52. say color.bold  || "Auto WGet Download Utility " || color.usual || "Version 1.6.7"
  53. say color.usual || "Copyright (C) 1998-2001 Dmitry A.Steklenev"
  54. say color.usual || ""
  55.  
  56. call MsgRead "awgmsg"
  57. call CfgRead
  58. call PlgRead "private"
  59.  
  60. /* activate plugins before opening any file */
  61. call PlgStart
  62.  
  63. signal on syntax
  64. parse arg job_file
  65.  
  66. if job_file == "" then
  67.    exit 257
  68.  
  69. if \PlgBroadcast( "INIT "job_file ) then
  70.    exit 257
  71.  
  72. if \JobRead( job_file ) then
  73.    exit 257
  74.  
  75. parameters = job.downloads_parameters
  76. url        = job.url
  77. file       = GetFileFromURL(url)
  78. parameters = replace( parameters, "%u", EncodeURL(url))
  79. parameters = replace( parameters, "%d", job.download )
  80. parameters = replace( parameters, "%p", translate(job.download, "/", "\" ))
  81. parameters = replace( parameters, "%f", file )
  82. logs       = ""
  83.  
  84. rc = AwPOpen( job.downloads_utility, parameters, "phandle" )
  85.  
  86. if rc == 0 then do
  87.    call PlgBroadcast "DATA"
  88.    do until line == ""
  89.       line = AwPRead( phandle )
  90.       call charout , line
  91.       logs = logs || line
  92.  
  93.       if length(logs) > 4096 then
  94.          logs = right( logs, 2048 )
  95.  
  96.       call PlgWrite translate( line, ' ', '00'x )
  97.    end
  98.    rc = AwPClose( phandle )
  99.    call PlgWrite '00'x
  100.  
  101.    job.downloads_rc   = rc
  102.    job.downloads_info = AwExtract( logs )
  103.    end
  104. else do
  105.    job.downloads_rc   = rc
  106.    job.downloads_info = SysGetMessage(rc)
  107. end
  108.  
  109. if job.downloads_rc == 0 then do
  110.    call WpsPutEA job.download"\"file, ".SUBJECT" , url
  111.    object_title   = space( WpsGetEA( job.object, ".LONGNAME" ))
  112.    object_comment = object_title || '00'x || date()" "time() || '00'x
  113.    call WpsPutEA job.download"\"file, ".COMMENTS", object_comment
  114. end
  115.  
  116. call JobSave job_file
  117. call PlgBroadcast "STOP" job_file
  118. exit job.downloads_rc
  119.  
  120. syntax:
  121.  
  122.   parse source . . program .
  123.  
  124.   if sourceline() >= SIGL then
  125.      sourceline = strip(sourceline(SIGL))
  126.   else
  127.      sourceline = ""
  128.  
  129.   call LogPut err, condition('C') rc "running" filespec( "name", program )", line " ||,
  130.                    SIGL errortext(rc) condition('D'),, sourceline
  131.  
  132.   exit 256
  133.  
  134. /*------------------------------------------------------------------
  135.  * Extracts the last not empty message of the download utility
  136.  *------------------------------------------------------------------*/
  137. AwExtract: procedure
  138.  
  139.   parse arg logs
  140.  
  141.   logs = strip(logs)
  142.   do i = length(logs) to 1 by -1
  143.      c = substr(logs,i,1)
  144.      if c \= '0A'x & c \='0D'x then
  145.         leave
  146.   end
  147.  
  148.   logs = substr(logs,1,i)
  149.   do i = length(logs) to 1 by -1
  150.      c = substr(logs,i,1)
  151.      if c == '0A'x | c =='0D'x then
  152.         leave
  153.   end
  154.  
  155. return substr(logs,i+1)
  156.  
  157. /* $Id: init.cms,v 1.26 2001/05/11 09:45:01 glass Exp $ */
  158.  
  159. /*------------------------------------------------------------------
  160.  * Initialization
  161.  *------------------------------------------------------------------*/
  162. AwInit: procedure expose (globals)
  163.  
  164.   if RxFuncQuery('SysLoadFuncs') then do
  165.      call RxFuncAdd 'SysLoadFuncs', 'RexxUtil', 'SysLoadFuncs'
  166.      call SysLoadFuncs
  167.   end
  168.  
  169.   parse source os what program tail
  170.   lpath = substr( program, 1, lastpos( "\", program ))
  171.   dll   = lpath || "awget.dll"
  172.  
  173.   if RxFuncQuery('AwLoadFuncs') then do
  174.      call RxFuncAdd  'AwLoadFuncs', dll, 'AwLoadFuncs'
  175.      call AwLoadFuncs
  176.   end
  177.  
  178.   '@echo off'
  179.  
  180.   sys.config_file = value( "ETC",, "OS2ENVIRONMENT" )"\awget.cfg"
  181.   sys.connected   = 1
  182.   sys.active_time = 0
  183.  
  184.   /* enable ANSI extended screen and keyboard control */
  185.   '@ansi on > nul'
  186.  
  187.   color.brown   = "1B"x"[0;33m"
  188.   color.red     = "1B"x"[1;31m"
  189.   color.green   = "1B"x"[1;32m"
  190.   color.yellow  = "1B"x"[1;33m"
  191.   color.blue    = "1B"x"[1;34m"
  192.   color.magenta = "1B"x"[1;35m"
  193.   color.cyan    = "1B"x"[1;36m"
  194.   color.white   = "1B"x"[1;37m"
  195.   color.gray    = "1B"x"[0m"
  196.  
  197.   color.usual   = color.gray
  198.   color.bold    = color.white
  199.   color.error   = color.red
  200.   color.info    = color.green
  201.   color.debug   = color.brown
  202.  
  203.   /* known download utilites */
  204.  
  205.   sys.utils.0      = 2
  206.   sys.utils.1.prog = wget.exe
  207.   sys.utils.1.name = "GNU WGet"
  208.   sys.utils.1.parm = '-c -t 10 -w 30 -P "%p" "%u"'
  209.   sys.utils.2.prog = curl.exe
  210.   sys.utils.2.name = "cURL"
  211.   sys.utils.2.parm = '-y 300 -Y 1 -v -C - -o "%p/%f" "%u"'
  212.  
  213.   sys.utils.default.prog = sys.utils.1.prog
  214.   sys.utils.default.name = sys.utils.1.name
  215.   sys.utils.default.parm = sys.utils.1.parm
  216.  
  217.   /* service semaphores */
  218.  
  219.   sys.running = "$live$"
  220.   sys.killing = "$stop$"
  221.   sys.pushing = "$push$"
  222.   sys.tqueue  = "$term$"
  223.  
  224.   /* stream's open modes */
  225.  
  226.   parse version version .
  227.   if version = "OBJREXX" then do
  228.      sys.open_read  = "OPEN READ  SHAREREAD"
  229.      sys.open_write = "OPEN WRITE SHAREREAD"
  230.      end
  231.   else do
  232.      sys.open_read  = "OPEN READ"
  233.      sys.open_write = "OPEN WRITE"
  234.   end
  235. return
  236.  
  237. /* $Id: nls.cms,v 1.13 2001/05/11 08:54:34 glass Exp $ */
  238.  
  239. /*------------------------------------------------------------------
  240.  * Read messages
  241.  *------------------------------------------------------------------*/
  242. MsgRead: procedure expose (globals)
  243.  
  244.   parse arg msgfile
  245.   parse source OS2 what msgpath
  246.  
  247.   msgfile = filespec( "disk", msgpath ) ||,
  248.             filespec( "path", msgpath ) || "NLS\" || msgfile
  249.  
  250.   country = MsgCountryID()
  251.  
  252.   if stream( msgfile"."country, "c", "query exists" ) == "" then
  253.      country = "001"
  254.  
  255.   msgfile = msgfile"."country
  256.   rc = stream( msgfile, "C", sys.open_read )
  257.  
  258.   if rc \= "READY:" then do
  259.      say  color.error || "■■■ Error on open NLS file: "msgfile
  260.      exit 1
  261.   end
  262.  
  263.   do while lines(msgfile) > 0
  264.      line = strip(linein(msgfile))
  265.  
  266.      do while right(line,1) == "\"
  267.         line = left( line, length(line)-1 )
  268.         line = line || strip(linein(msgfile))
  269.      end
  270.  
  271.      if line \= "" & left(line,1) \= "#" then do
  272.         parse value line with id "=" msg
  273.  
  274.         id  = strip(id )
  275.         msg = strip(msg)
  276.  
  277.         i = pos( "\n", msg )
  278.         do while i > 0
  279.            msg = substr( msg, 1, i-1 ) || '0D0A'x || substr( msg, i+2 )
  280.            i = pos( "\n", msg )
  281.         end
  282.  
  283.         msg.id = msg
  284.      end
  285.   end
  286.  
  287.   rc = stream( msgfile, "C", "CLOSE" )
  288. return
  289.  
  290. /*------------------------------------------------------------------
  291.  * Returns Country Identifier
  292.  *------------------------------------------------------------------*/
  293. MsgCountryID: procedure expose (globals)
  294.  
  295.   country = strip( SysIni( "BOTH", "PM_National", "iCountry" ),, '0'x )
  296.  
  297.   if country == "ERROR:" then
  298.      country =  "001"
  299.   else
  300.      country =  right( country, 3, "0" )
  301.  
  302. return country
  303.  
  304.  
  305. /* $Id: config.cms,v 1.29 2001/05/11 08:54:34 glass Exp $ */
  306.  
  307. /*------------------------------------------------------------------
  308.  * Get Configuration
  309.  *------------------------------------------------------------------*/
  310. CfgRead: procedure expose (globals)
  311.  
  312.   cfg.home                     = "."
  313.   cfg.download                 = "."
  314.   cfg.downloads_simultaneously = 3
  315.   cfg.downloads_attempts       = 15
  316.   cfg.downloads_utility        = sys.utils.default.prog
  317.   cfg.downloads_parameters     = sys.utils.default.parm
  318.   cfg.scan_interval            = 30
  319.   cfg.log_file                 = "nul"
  320.   cfg.error_log                = "nul"
  321.   cfg.log_keep                 = 15
  322.   cfg.message_done             = 'start /n pmpopup2.exe "%m:~~%u" "Auto WGet Daemon" /BELL /B1:"OK" /T:900 /F:"8.Helv"'
  323.   cfg.message_error            = 'start /n pmpopup2.exe "%m:~~%u~%i" "Auto WGet Daemon" /BELL /B1:"OK" /T:900 /F:"8.Helv"'
  324.   cfg.messages                 = 1
  325.   cfg.check_connection         = 0
  326.   cfg.use_desktop              = 0
  327.   cfg.keep_failed_url          = 1
  328.   cfg.keep_done_url            = 0
  329.  
  330.   rc = stream( sys.config_file, "C", sys.open_read )
  331.  
  332.   do while lines(sys.config_file) > 0
  333.      parse value linein(sys.config_file) with command "=" argument
  334.  
  335.      command  = translate(strip(command))
  336.      argument = strip(argument)
  337.  
  338.      select
  339.         when command == "HOME",
  340.            | command == "DOWNLOAD",
  341.            | command == "DOWNLOADS_SIMULTANEOUSLY",
  342.            | command == "DOWNLOADS_ATTEMPTS",
  343.            | command == "DOWNLOADS_UTILITY",
  344.            | command == "DOWNLOADS_PARAMETERS",
  345.            | command == "SCAN_INTERVAL",
  346.            | command == "LOG_FILE",
  347.            | command == "ERROR_LOG",
  348.            | command == "LOG_KEEP",
  349.            | command == "MESSAGE_DONE",
  350.            | command == "MESSAGE_ERROR" then
  351.  
  352.              cfg.command = argument
  353.  
  354.         when command == "MESSAGES",
  355.            | command == "CHECK_CONNECTION",
  356.            | command == "USE_DESKTOP",
  357.            | command == "KEEP_FAILED_URL",
  358.            | command == "KEEP_DONE_URL" then
  359.  
  360.              cfg.command = (argument == "1")
  361.         otherwise
  362.      end
  363.   end
  364.  
  365.   rc = stream( sys.config_file, "C", "CLOSE" )
  366.   cfg.file_date = stream( sys.config_file, "C", "QUERY DATETIME" )
  367. return
  368.  
  369.  
  370. /* $Id: logs.cms,v 1.21 2001/05/11 08:54:34 glass Exp $ */
  371.  
  372. /*------------------------------------------------------------------
  373.  * Open log file
  374.  *------------------------------------------------------------------*/
  375. LogOpen: procedure expose (globals)
  376.  
  377.   parse arg logfile
  378.  
  379.   if logfile == "" | translate(logfile) == "NUL" then
  380.      return 0
  381.  
  382.   do 5
  383.      rc = stream( logfile, "c", sys.open_write )
  384.      if rc == "READY:" then leave
  385.      call SysSleep 1
  386.   end
  387.  
  388. return (rc == "READY:")
  389.  
  390. /*------------------------------------------------------------------
  391.  * Write Log Record
  392.  *------------------------------------------------------------------*/
  393. LogPut: procedure expose (globals)
  394.  
  395.   parse arg type, message, file_or_url, information
  396.   type = translate(type)
  397.  
  398.   if type == "INF" then type = "INFO"
  399.   if type == "ERR" then type = "ERROR"
  400.   if type == "BLD" then type = "BOLD"
  401.  
  402.   select
  403.     when type == "BOLD" | type == "INFO" then do
  404.       logfile  = cfg.log_file
  405.       end
  406.  
  407.     when type == "ERROR" then do
  408.       logfile  = cfg.error_log
  409.       end
  410.  
  411.     otherwise do
  412.       type     = "USUAL"
  413.       logfile  = cfg.log_file
  414.       end
  415.   end
  416.  
  417.   /* Output to file */
  418.   if LogOpen(logfile) then do
  419.  
  420.      if arg( 3, "exists" ) then
  421.         call lineout logfile, date('e')" "time()" "message": "file_or_url
  422.      else
  423.         call lineout logfile, date('e')" "time()" "message
  424.  
  425.      if arg( 4, "exists" ) & information \= "" then
  426.         call lineout logfile, copies( ' ', 18 ) || information
  427.  
  428.      rc = stream( logfile, "c", "close" )
  429.   end
  430.  
  431.   /* Sound's alert */
  432.   if type == "ERROR" then
  433.      call beep 1000, 100
  434.  
  435.   /* Display message */
  436.   if arg( 3, "exists" ) then do
  437.      if length(file_or_url) + length(message) < 73 then
  438.         say color.type"■■■ "message": "file_or_url
  439.      else do
  440.         say color.type"■■■ "message": "
  441.         say color.type"    "ShortenURL( file_or_url, 75 )
  442.      end
  443.      end
  444.   else
  445.      say color.type"■■■ "message
  446.  
  447.   if arg( 4, "exist" ) & information \= "" then
  448.      say color.type"    "information
  449.  
  450.   call charout, color.usual
  451. return
  452.  
  453. /* $Id: shorten.cms,v 1.3 2001/05/11 08:54:34 glass Exp $ */
  454.  
  455. /*------------------------------------------------------------------
  456.  * Reduces URL length
  457.  *------------------------------------------------------------------*/
  458. ShortenURL: procedure expose (globals)
  459.  
  460.   parse arg url, max
  461.  
  462.   if length(url) > max then
  463.      do
  464.        if translate( left( url, 6 )) == "FTP://" then
  465.           shorten = left( url, 6 )
  466.        else if translate( left( url, 7 )) == "HTTP://" then
  467.           shorten = left( url, 7 )
  468.        else if substr( url, 3, 1 ) == "\" then
  469.           shorten = left( url, 3 )
  470.        else
  471.           shorten = ""
  472.  
  473.        shorten = shorten || "..." ||,
  474.                  right( url, max - length(shorten) - 3 )
  475.      end
  476.   else
  477.      shorten = url
  478.  
  479. return shorten
  480.  
  481. /* $Id: wps.cms,v 1.19 2001/05/11 08:54:34 glass Exp $ */
  482.  
  483. /*------------------------------------------------------------------
  484.  * Write a named ascii extended attribute to a file
  485.  *------------------------------------------------------------------*/
  486. WpsPutEA: procedure expose (globals)
  487.  
  488.   parse arg file, name, ea_string
  489.   ea = ""
  490.  
  491.   if pos( '00'x, ea_string ) > 0 then do
  492.      do ea_count = 0 while length( ea_string ) > 0
  493.        parse value ea_string with string '00'x ea_string
  494.        ea = ea || 'FDFF'x ||,
  495.             substr( reverse( d2c(length(string))), 1, 2, '00'x ) ||,
  496.             string
  497.      end
  498.      ea = 'DFFF'x || '0000'x ||,
  499.           substr( reverse( d2c(ea_count)), 1, 2, '00'x ) || ea
  500.      end
  501.   else
  502.      ea = 'FDFF'x ||,
  503.           substr( reverse( d2c(length(ea_string))), 1, 2, '00'x ) ||,
  504.           ea_string
  505.  
  506. return SysPutEA( file, name, ea )
  507.  
  508. /*------------------------------------------------------------------
  509.  * Read a named ascii extended attribute from a file
  510.  *------------------------------------------------------------------*/
  511. WpsGetEA: procedure expose (globals)
  512.  
  513.   parse arg file, name
  514.   if file == "" then return ""
  515.  
  516.   if SysGetEA( file, name, "ea" ) \= 0 then
  517.      return ""
  518.  
  519.   ea_type   = substr( ea, 1, 2 )
  520.   ea_string = ""
  521.  
  522.   select
  523.     when ea_type == 'FDFF'x then
  524.       ea_string = substr( ea, 5 )
  525.  
  526.     when ea_type == 'DFFF'x then do
  527.       ea_count = c2d( reverse( substr( ea, 5, 2 )))
  528.       say "count: "ea_count
  529.       ea_pos   = 7
  530.       do ea_count while substr( ea, ea_pos, 2 ) == 'FDFF'x
  531.          ea_length = c2d( reverse( substr( ea, ea_pos+2, 2 )))
  532.          ea_string = ea_string || substr( ea, ea_pos+4, ea_length ) || '00'x
  533.          ea_pos    = ea_pos + 4 + ea_length
  534.       end
  535.       end
  536.  
  537.     otherwise
  538.   end
  539.  
  540. return ea_string
  541.  
  542. /* $Id: url.cms,v 1.15 2001/05/11 08:54:34 glass Exp $ */
  543.  
  544. /*------------------------------------------------------------------
  545.  * Encode URL
  546.  *------------------------------------------------------------------*/
  547. EncodeURL: procedure expose (globals)
  548.  
  549.   parse arg url
  550. return replace( url, " ", "%20" )
  551.  
  552. /*------------------------------------------------------------------
  553.  * Decode URL
  554.  *------------------------------------------------------------------*/
  555. DecodeURL: procedure expose (globals)
  556.  
  557.   parse arg url
  558.  
  559.   do while pos( "%", url ) > 0
  560.  
  561.      i = pos( "%", url )
  562.  
  563.      url = substr( url, 1, i-1      ) ||,
  564.            x2c(substr( url, i+1, 2 )) ||,
  565.            substr( url, i+3         )
  566.   end
  567.  
  568. return url
  569.  
  570. /*------------------------------------------------------------------
  571.  * Get filename from URL
  572.  *------------------------------------------------------------------*/
  573. GetFileFromURL: procedure expose (globals)
  574.  
  575.   /* generic-RL syntax consists of six components:           */
  576.   /* <scheme>://<net_loc>/<path>;<params>?<query>#<fragment> */
  577.  
  578.   parse arg url
  579.   url = strip(url)
  580.  
  581.   i = lastpos( "#", url )
  582.   if i > 0 then url = substr( url, 1, i-1 )
  583.  
  584.   i = pos( ":", url )
  585.   if i > 0 then url = substr( url, i+1 )
  586.  
  587.   if left(url,2) == "//" then do
  588.      i = pos( "/", url, 3 )
  589.      if i > 0 then
  590.         url = substr( url, i )
  591.      else
  592.         url = ""
  593.   end
  594.  
  595.   i = lastpos( "?", url )
  596.   if i > 0 then url = substr( url, 1, i-1 )
  597.  
  598.   i = lastpos( ";", url )
  599.   if i > 0 then url = substr( url, 1, i-1 )
  600.  
  601.   i = lastpos( "/", url )
  602.   if i > 0 then url = substr( url, i+1 )
  603.  
  604.   if url == "" then url = "index.html"
  605. return DecodeURL(url)
  606.  
  607.  
  608. /* $Id: replace.cms,v 1.5 2001/05/11 08:54:34 glass Exp $ */
  609.  
  610. /*------------------------------------------------------------------
  611.  * Search and replace string
  612.  *------------------------------------------------------------------*/
  613. replace: procedure expose (globals)
  614.  
  615.   parse arg source, string, substitute
  616.   string = translate(string)
  617.  
  618.   i = pos( string, translate(source))
  619.  
  620.   do while i \= 0
  621.      source = substr( source, 1, i-1 ) || substitute ||,
  622.               substr( source, i+length(string))
  623.  
  624.      i = pos( string, translate(source), i + length(substitute))
  625.   end
  626.  
  627. return source
  628.  
  629. /* $Id: jobs.cms,v 1.11 2001/05/11 08:54:34 glass Exp $ */
  630.  
  631. /*------------------------------------------------------------------
  632.  * Read Job from file
  633.  *------------------------------------------------------------------*/
  634. JobRead: procedure expose (globals)
  635.  
  636.   parse arg pathname
  637.  
  638.   job.object               = ""
  639.   job.url                  = ""
  640.   job.download             = ""
  641.   job.message_done         = ""
  642.   job.message_error        = ""
  643.   job.downloads_utility    = ""
  644.   job.downloads_parameters = ""
  645.   job.downloads_rc         = 0
  646.   job.downloads_info       = ""
  647.  
  648.   rc = stream( pathname, "C", sys.open_read )
  649.  
  650.   if rc \= "READY:" then do
  651.      call LogPut err, msg.read_error, pathname
  652.      return 0
  653.   end
  654.  
  655.   do while lines(pathname) > 0
  656.      parse value linein(pathname) with command "=" argument
  657.  
  658.      command  = translate(strip(command))
  659.      argument = strip(argument)
  660.  
  661.      select
  662.         when command == "OBJECT",
  663.            | command == "URL",
  664.            | command == "DOWNLOAD",
  665.            | command == "DOWNLOADS_UTILITY",
  666.            | command == "DOWNLOADS_PARAMETERS",
  667.            | command == "DOWNLOADS_SIMULTANEOUSLY",
  668.            | command == "DOWNLOADS_RC",
  669.            | command == "DOWNLOADS_INFO",
  670.            | command == "MESSAGE_DONE",
  671.            | command == "MESSAGE_ERROR" then
  672.  
  673.              job.command = argument
  674.         otherwise
  675.      end
  676.   end
  677.  
  678.   rc = stream( pathname, "C", "CLOSE" )
  679. return 1
  680.  
  681. /*------------------------------------------------------------------
  682.  * Save Job to file
  683.  *------------------------------------------------------------------*/
  684. JobSave: procedure expose (globals)
  685.  
  686.   parse arg pathname
  687.  
  688.   if arg( 1, "omitted" ) | pathname == "" then do
  689.      pathname = SysTempFileName( dir.jobs"\?????.job" )
  690.      body.0   = 0
  691.      end
  692.   else do
  693.      rc = stream( pathname, "C", sys.open_read )
  694.  
  695.      do i = 1 while lines(pathname) > 0
  696.         body.i = linein(pathname)
  697.      end
  698.      body.0 = i - 1
  699.      rc = stream( pathname, "C", "CLOSE" )
  700.   end
  701.  
  702.   key_list = "OBJECT "               ||,
  703.              "URL "                  ||,
  704.              "DOWNLOAD "             ||,
  705.              "MESSAGE_DONE "         ||,
  706.              "MESSAGE_ERROR "        ||,
  707.              "DOWNLOADS_UTILITY "    ||,
  708.              "DOWNLOADS_PARAMETERS " ||,
  709.              "DOWNLOADS_RC "         ||,
  710.              "DOWNLOADS_INFO "
  711.  
  712.   do i = 1 to words(key_list)
  713.      key = word(key_list,i)
  714.  
  715.      do j = 1 to body.0
  716.         if left( strip( body.j ), 1 ) == "#" then
  717.            iterate
  718.  
  719.         parse value body.j with command "="
  720.         command = translate(strip(command))
  721.  
  722.         if key == command then
  723.            leave
  724.      end j
  725.  
  726.      body.j = key "=" job.key
  727.  
  728.      if j > body.0 then
  729.         body.0 = j
  730.   end i
  731.  
  732.   if stream( pathname, "C", "QUERY EXISTS" ) \= "" then
  733.      '@del "'pathname'" /F'
  734.  
  735.   rc = stream( pathname, "C", sys.open_write )
  736.  
  737.   if rc \= "READY:" then do
  738.      call LogPut err, msg.write_error, pathname
  739.      return ""
  740.   end
  741.  
  742.   do j = 1 to body.0
  743.      call lineout pathname, body.j
  744.   end
  745.  
  746.   rc = stream( pathname, "C", "CLOSE" )
  747. return pathname
  748.  
  749. /* $Id: plugins.cms,v 1.14 2001/05/11 08:54:34 glass Exp $ */
  750.  
  751. /*------------------------------------------------------------------
  752.  * Get Plugins List
  753.  *------------------------------------------------------------------*/
  754. PlgRead: procedure expose (globals)
  755.  
  756.   parse arg type
  757.  
  758.   type  = translate(type"_plugin")
  759.   rc    = stream( sys.config_file, "C", sys.open_read )
  760.   count = 0
  761.  
  762.   do while lines(sys.config_file) > 0
  763.      parse value linein(sys.config_file) with command "=" argument
  764.  
  765.      if translate(strip(command)) == type then do
  766.         count = count + 1
  767.         plugins.count.module = strip(argument)
  768.         plugins.count.handle = ""
  769.         plugins.count.buffer = ""
  770.      end
  771.   end
  772.  
  773.   rc = stream( sys.config_file, "C", "CLOSE" )
  774.   plugins.0 = count
  775. return
  776.  
  777. /*------------------------------------------------------------------
  778.  * Activate Plugins
  779.  *------------------------------------------------------------------*/
  780. PlgStart: procedure expose (globals)
  781.  
  782.   do i = 1 to plugins.0
  783.      rc = AwPOpen( "cmd.exe", "/c "plugins.i.module,,
  784.                               "plugins."i".handle", "detach" )
  785.      if rc \= 0 then
  786.         call LogPut err, msg.error_in "AwPOpen, rc="rc,, SysGetMessage(rc)
  787.   end
  788. return
  789.  
  790. /*------------------------------------------------------------------
  791.  * Deactivate Plugins
  792.  *------------------------------------------------------------------*/
  793. PlgStop: procedure expose (globals)
  794.  
  795.   do i = 1 to plugins.0
  796.      if plugins.i.handle \= "" then
  797.         AwPWrite( plugins.id.handle, "STOP" || "0D0A"x )
  798.   end
  799. return
  800.  
  801. /*------------------------------------------------------------------
  802.  * Send Event
  803.  *------------------------------------------------------------------*/
  804. PlgSend: procedure expose (globals)
  805.  
  806.   parse arg id, event
  807.   event = event || "0D0A"x
  808.  
  809.   if AwPWrite( plugins.id.handle, event ) != length(event) then do
  810.      call LogPut err, msg.error_in "AwPWrite",, plugins.id.module
  811.      return 0
  812.   end
  813.  
  814.   do forever
  815.      readed = AwPRead( plugins.id.handle )
  816.      if readed == "" then do
  817.         call LogPut err, msg.plugin_dead, plugins.id.module
  818.         plugins.id.handle = ""
  819.         return 1
  820.      end
  821.  
  822.      plugins.id.buffer = plugins.id.buffer || readed
  823.  
  824.      i = pos( '0D0A'x,  plugins.id.buffer )
  825.      do while i > 0
  826.         event = substr( plugins.id.buffer, 1, i - 1 )
  827.         plugins.id.buffer = substr( plugins.id.buffer, i + 2 )
  828.  
  829.         message = substr( event, 6    )
  830.         event   = substr( event, 1, 4 )
  831.  
  832.         select
  833.           when event == "INFO" then
  834.                say color.info || "*** "message || color.usual
  835.           when event == "EVNT" then
  836.                call LogPut bld, message
  837.           when event == "ALRM" then
  838.                call LogPut err, message
  839.  
  840.           when event == "DONE" then do
  841.                if message \= "" then
  842.                   say color.info || "*** "message || color.usual
  843.  
  844.                plugins.id.buffer = ""
  845.                return 1
  846.                end
  847.  
  848.           when event == "FAIL" then do
  849.                if message \= "" then
  850.                   call LogPut err, message
  851.  
  852.                plugins.id.buffer = ""
  853.                return 0
  854.                end
  855.  
  856.           otherwise
  857.         end
  858.         i = pos( '0D0A'x,  plugins.id.buffer )
  859.      end
  860.   end
  861. return 0
  862.  
  863. /*------------------------------------------------------------------
  864.  * Broadcast Event
  865.  *------------------------------------------------------------------*/
  866. PlgBroadcast: procedure expose (globals)
  867.  
  868.   parse arg event
  869.   done = 1
  870.  
  871.   do i = 1 to plugins.0
  872.      if plugins.i.handle \= "" then
  873.         done = PlgSend( i, event ) & done
  874.   end
  875. return done
  876.  
  877. /*------------------------------------------------------------------
  878.  * Write Byte Stream
  879.  *------------------------------------------------------------------*/
  880. PlgWrite: procedure expose (globals)
  881.  
  882.   parse arg stream
  883.  
  884.   do i = 1 to plugins.0
  885.      if plugins.i.handle \= "" then
  886.         call AwPWrite plugins.i.handle, stream
  887.   end
  888. return
  889.