home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / awget169.zip / awgadd.cmd next >
OS/2 REXX Batch file  |  2001-07-17  |  18KB  |  604 lines

  1. /* Auto WGet URL Add Utility
  2.  * Copyright (C) 1999-2001 Dmitry A.Steklenev
  3.  *
  4.  * Redistribution and use in source and binary forms, with or without
  5.  * modification, are permitted provided that the following conditions
  6.  * are met:
  7.  *
  8.  * 1. Redistributions of source code must retain the above copyright
  9.  *    notice, this list of conditions and the following disclaimer.
  10.  *
  11.  * 2. Redistributions in binary form must reproduce the above copyright
  12.  *    notice, this list of conditions and the following disclaimer in
  13.  *    the documentation and/or other materials provided with the
  14.  *    distribution.
  15.  *
  16.  * 3. All advertising materials mentioning features or use of this
  17.  *    software must display the following acknowledgment:
  18.  *    "This product includes software developed by Dmitry A.Steklenev".
  19.  *
  20.  * 4. Redistributions of any form whatsoever must retain the following
  21.  *    acknowledgment:
  22.  *    "This product includes software developed by Dmitry A.Steklenev".
  23.  *
  24.  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR OR CONTRIBUTORS "AS IS"
  25.  * AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  26.  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  27.  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  28.  * AUTHOR OR THE CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  29.  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  30.  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  31.  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  32.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  33.  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  34.  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  35.  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  36.  *
  37.  * $Id: awgadd.cms,v 1.16 2001/07/16 09:27:23 glass Exp $
  38.  */
  39.  
  40. globals = "cfg. local. msg. sys. color. dir. jobs. job. plugins."
  41.  
  42. if translate( value( "AWGET_TRACE",, "OS2ENVIRONMENT" )) == "YES" then do
  43.    call  value "AWGET_TRACE", "", "OS2ENVIRONMENT"
  44.    trace intermediate
  45.    trace results
  46. end
  47.  
  48. call AwInit
  49.  
  50. say color.bold  || "Auto WGet URL Add Utility " || color.usual || "Version 1.6.9"
  51. say color.usual || "Copyright (C) 1998-2001 Dmitry A.Steklenev"
  52. say color.usual || ""
  53.  
  54. call MsgRead "awgmsg"
  55. call CfgRead
  56. call DirRead
  57.  
  58. signal on syntax
  59. sys_pushing = cfg.home'\'sys.pushing
  60.  
  61. parse source . what .
  62.  
  63. if what == 'COMMAND' then
  64.    do
  65.       "SET AWGADDURL=%1"
  66.       "SET AWGFOLDER=%2"
  67.    end /* do */
  68. else
  69.    do
  70.       "SET AWGADDURL="arg(1)
  71.       "SET AWGFOLDER="arg(2)
  72.    end /* do */
  73.  
  74. url  = strip( value( "AWGADDURL",, "OS2ENVIRONMENT" ))
  75. home = strip( value( "AWGFOLDER",, "OS2ENVIRONMENT" ))
  76.  
  77. if home == "" then do
  78.    home = directory()
  79.    if translate( dir.todo ) \= ,
  80.       translate( left( home, length( dir.todo ))) then do
  81.       home = dir.todo
  82.    end
  83. end
  84.  
  85. if url == "" then
  86.    url = AwQueryURL( msg.enter_url, msg.enter_add, msg.enter_cancel )
  87.  
  88. if left(url,1) == '"' & right(url,1) == '"' then
  89.    url = substr( url, 2, length(url) - 2 )
  90.  
  91. if url == "" then do
  92.    say "Usage: awgadd {<url>|<list_filename>} [<todo_folder>]"
  93.    exit 1
  94. end
  95.  
  96. if stream( url, "c", "query exists" ) \= "" then do
  97.    do while lines(url) > 0
  98.       next = strip(linein(url))
  99.       if next \= "" then
  100.          call CreateURL next, home
  101.    end
  102.    call stream url, "c", "close"
  103.    end
  104. else
  105.    call CreateURL url, home
  106.  
  107. call lineout sys_pushing, "It is demand."
  108. call stream  sys_pushing, 'c', 'close'
  109. exit 0
  110.  
  111. syntax:
  112.  
  113.   parse source . . program .
  114.  
  115.   if sourceline() >= SIGL then
  116.      sourceline = strip(sourceline(SIGL))
  117.   else
  118.      sourceline = ""
  119.  
  120.   call LogPut err, condition('C') rc "running" filespec( "name", program )", line " ||,
  121.                    SIGL errortext(rc) condition('D'),, sourceline
  122.   exit 256
  123.  
  124. /*------------------------------------------------------------------
  125.  * Creates URL object
  126.  *------------------------------------------------------------------*/
  127. CreateURL: procedure expose (globals)
  128.  
  129.   parse arg url, home
  130.  
  131.   if left(url,1) == '"' & right(url,1) == '"' then
  132.      url = substr( url, 2, length(url) - 2 )
  133.  
  134.   object = SysTempFileName( home"\add!????" )
  135.   title  = GetFileFromURL(url)
  136.   url    = replace( url, ";", "%3B" )
  137.  
  138.   if SysOs2Ver() > "2.30" then
  139.      rc = SysCreateObject( "WPUrl", object, home, 'TITLE='title';URL='url, "R" )
  140.   else do
  141.      call lineout  object, url
  142.      call stream   object, "c", "close"
  143.      call WpsPutEA object, ".LONGNAME", title
  144.      call SysSaveObject object, 0
  145.   end
  146. return
  147.  
  148. /* $Id: init.cms,v 1.26 2001/05/11 09:45:01 glass Exp $ */
  149.  
  150. /*------------------------------------------------------------------
  151.  * Initialization
  152.  *------------------------------------------------------------------*/
  153. AwInit: procedure expose (globals)
  154.  
  155.   if RxFuncQuery('SysLoadFuncs') then do
  156.      call RxFuncAdd 'SysLoadFuncs', 'RexxUtil', 'SysLoadFuncs'
  157.      call SysLoadFuncs
  158.   end
  159.  
  160.   parse source os what program tail
  161.   lpath = substr( program, 1, lastpos( "\", program ))
  162.   dll   = lpath || "awget.dll"
  163.  
  164.   if RxFuncQuery('AwLoadFuncs') then do
  165.      call RxFuncAdd  'AwLoadFuncs', dll, 'AwLoadFuncs'
  166.      call AwLoadFuncs
  167.   end
  168.  
  169.   '@echo off'
  170.  
  171.   sys.config_file = value( "ETC",, "OS2ENVIRONMENT" )"\awget.cfg"
  172.   sys.connected   = 1
  173.   sys.active_time = 0
  174.  
  175.   /* enable ANSI extended screen and keyboard control */
  176.   '@ansi on > nul'
  177.  
  178.   color.brown   = "1B"x"[0;33m"
  179.   color.red     = "1B"x"[1;31m"
  180.   color.green   = "1B"x"[1;32m"
  181.   color.yellow  = "1B"x"[1;33m"
  182.   color.blue    = "1B"x"[1;34m"
  183.   color.magenta = "1B"x"[1;35m"
  184.   color.cyan    = "1B"x"[1;36m"
  185.   color.white   = "1B"x"[1;37m"
  186.   color.gray    = "1B"x"[0m"
  187.  
  188.   color.usual   = color.gray
  189.   color.bold    = color.white
  190.   color.error   = color.red
  191.   color.info    = color.green
  192.   color.debug   = color.brown
  193.  
  194.   /* known download utilites */
  195.  
  196.   sys.utils.0      = 2
  197.   sys.utils.1.prog = wget.exe
  198.   sys.utils.1.name = "GNU WGet"
  199.   sys.utils.1.parm = '-c -t 10 -w 30 -P "%p" "%u"'
  200.   sys.utils.2.prog = curl.exe
  201.   sys.utils.2.name = "cURL"
  202.   sys.utils.2.parm = '-y 300 -Y 1 -v -C - -o "%p/%f" "%u"'
  203.  
  204.   sys.utils.default.prog = sys.utils.1.prog
  205.   sys.utils.default.name = sys.utils.1.name
  206.   sys.utils.default.parm = sys.utils.1.parm
  207.  
  208.   /* service semaphores */
  209.  
  210.   sys.running = "$live$"
  211.   sys.killing = "$stop$"
  212.   sys.pushing = "$push$"
  213.   sys.tqueue  = "$term$"
  214.  
  215.   /* stream's open modes */
  216.  
  217.   parse version version .
  218.   if version = "OBJREXX" then do
  219.      sys.open_read  = "OPEN READ  SHAREREAD"
  220.      sys.open_write = "OPEN WRITE SHAREREAD"
  221.      end
  222.   else do
  223.      sys.open_read  = "OPEN READ"
  224.      sys.open_write = "OPEN WRITE"
  225.   end
  226. return
  227.  
  228. /* $Id: nls.cms,v 1.13 2001/05/11 08:54:34 glass Exp $ */
  229.  
  230. /*------------------------------------------------------------------
  231.  * Read messages
  232.  *------------------------------------------------------------------*/
  233. MsgRead: procedure expose (globals)
  234.  
  235.   parse arg msgfile
  236.   parse source OS2 what msgpath
  237.  
  238.   msgfile = filespec( "disk", msgpath ) ||,
  239.             filespec( "path", msgpath ) || "NLS\" || msgfile
  240.  
  241.   country = MsgCountryID()
  242.  
  243.   if stream( msgfile"."country, "c", "query exists" ) == "" then
  244.      country = "001"
  245.  
  246.   msgfile = msgfile"."country
  247.   rc = stream( msgfile, "C", sys.open_read )
  248.  
  249.   if rc \= "READY:" then do
  250.      say  color.error || "■■■ Error on open NLS file: "msgfile
  251.      exit 1
  252.   end
  253.  
  254.   do while lines(msgfile) > 0
  255.      line = strip(linein(msgfile))
  256.  
  257.      do while right(line,1) == "\"
  258.         line = left( line, length(line)-1 )
  259.         line = line || strip(linein(msgfile))
  260.      end
  261.  
  262.      if line \= "" & left(line,1) \= "#" then do
  263.         parse value line with id "=" msg
  264.  
  265.         id  = strip(id )
  266.         msg = strip(msg)
  267.  
  268.         i = pos( "\n", msg )
  269.         do while i > 0
  270.            msg = substr( msg, 1, i-1 ) || '0D0A'x || substr( msg, i+2 )
  271.            i = pos( "\n", msg )
  272.         end
  273.  
  274.         msg.id = msg
  275.      end
  276.   end
  277.  
  278.   rc = stream( msgfile, "C", "CLOSE" )
  279. return
  280.  
  281. /*------------------------------------------------------------------
  282.  * Returns Country Identifier
  283.  *------------------------------------------------------------------*/
  284. MsgCountryID: procedure expose (globals)
  285.  
  286.   country = strip( SysIni( "BOTH", "PM_National", "iCountry" ),, '0'x )
  287.  
  288.   if country == "ERROR:" then
  289.      country =  "001"
  290.   else
  291.      country =  right( country, 3, "0" )
  292.  
  293. return country
  294.  
  295.  
  296. /* $Id: config.cms,v 1.29 2001/05/11 08:54:34 glass Exp $ */
  297.  
  298. /*------------------------------------------------------------------
  299.  * Get Configuration
  300.  *------------------------------------------------------------------*/
  301. CfgRead: procedure expose (globals)
  302.  
  303.   cfg.home                     = "."
  304.   cfg.download                 = "."
  305.   cfg.downloads_simultaneously = 3
  306.   cfg.downloads_attempts       = 15
  307.   cfg.downloads_utility        = sys.utils.default.prog
  308.   cfg.downloads_parameters     = sys.utils.default.parm
  309.   cfg.scan_interval            = 30
  310.   cfg.log_file                 = "nul"
  311.   cfg.error_log                = "nul"
  312.   cfg.log_keep                 = 15
  313.   cfg.message_done             = 'start /n pmpopup2.exe "%m:~~%u" "Auto WGet Daemon" /BELL /B1:"OK" /T:900 /F:"8.Helv"'
  314.   cfg.message_error            = 'start /n pmpopup2.exe "%m:~~%u~%i" "Auto WGet Daemon" /BELL /B1:"OK" /T:900 /F:"8.Helv"'
  315.   cfg.messages                 = 1
  316.   cfg.check_connection         = 0
  317.   cfg.use_desktop              = 0
  318.   cfg.keep_failed_url          = 1
  319.   cfg.keep_done_url            = 0
  320.  
  321.   rc = stream( sys.config_file, "C", sys.open_read )
  322.  
  323.   do while lines(sys.config_file) > 0
  324.      parse value linein(sys.config_file) with command "=" argument
  325.  
  326.      command  = translate(strip(command))
  327.      argument = strip(argument)
  328.  
  329.      select
  330.         when command == "HOME",
  331.            | command == "DOWNLOAD",
  332.            | command == "DOWNLOADS_SIMULTANEOUSLY",
  333.            | command == "DOWNLOADS_ATTEMPTS",
  334.            | command == "DOWNLOADS_UTILITY",
  335.            | command == "DOWNLOADS_PARAMETERS",
  336.            | command == "SCAN_INTERVAL",
  337.            | command == "LOG_FILE",
  338.            | command == "ERROR_LOG",
  339.            | command == "LOG_KEEP",
  340.            | command == "MESSAGE_DONE",
  341.            | command == "MESSAGE_ERROR" then
  342.  
  343.              cfg.command = argument
  344.  
  345.         when command == "MESSAGES",
  346.            | command == "CHECK_CONNECTION",
  347.            | command == "USE_DESKTOP",
  348.            | command == "KEEP_FAILED_URL",
  349.            | command == "KEEP_DONE_URL" then
  350.  
  351.              cfg.command = (argument == "1")
  352.         otherwise
  353.      end
  354.   end
  355.  
  356.   rc = stream( sys.config_file, "C", "CLOSE" )
  357.   cfg.file_date = stream( sys.config_file, "C", "QUERY DATETIME" )
  358. return
  359.  
  360.  
  361. /* $Id: dirs.cms,v 1.24 2001/05/11 08:54:34 glass Exp $ */
  362.  
  363. /*------------------------------------------------------------------
  364.  * Get Directorys
  365.  *------------------------------------------------------------------*/
  366. DirRead: procedure expose (globals)
  367.  
  368.   dir.todo    = AwGetObjectPath( "<AWG2_TODO>"    )
  369.   dir.running = AwGetObjectPath( "<AWG2_RUNNING>" )
  370.   dir.done    = AwGetObjectPath( "<AWG2_DONE>"    )
  371.   dir.failed  = AwGetObjectPath( "<AWG2_FAILED>"  )
  372.   dir.jobs    = AwGetObjectPath( "<AWG2_JOBS>"    )
  373.   dir.desktop = AwGetObjectPath( "<WP_DESKTOP>"   )
  374.  
  375.   /* check it */
  376.   dir_list = "ToDo Running Done Failed Jobs Desktop"
  377.   do i = 1 to words(dir_list)
  378.      name = translate( word( dir_list,i ))
  379.      if dir.name == "" then do
  380.         call LogPut err, msg.not_found, word( dir_list,i )
  381.         exit 1
  382.      end
  383.   end
  384. return
  385.  
  386.  
  387. /* $Id: wps.cms,v 1.22 2001/07/16 09:27:23 glass Exp $ */
  388.  
  389. /*------------------------------------------------------------------
  390.  * Write a named ascii extended attribute to a file
  391.  *------------------------------------------------------------------*/
  392. WpsPutEA: procedure expose (globals)
  393.  
  394.   parse arg file, name, ea_string
  395.   ea = ""
  396.  
  397.   if pos( '00'x, ea_string ) > 0 then do
  398.      do ea_count = 0 while length( ea_string ) > 0
  399.        parse value ea_string with string '00'x ea_string
  400.        ea = ea || 'FDFF'x ||,
  401.             substr( reverse( d2c(length(string))), 1, 2, '00'x ) ||,
  402.             string
  403.      end
  404.      ea = 'DFFF'x || '0000'x ||,
  405.           substr( reverse( d2c(ea_count)), 1, 2, '00'x ) || ea
  406.      end
  407.   else
  408.      ea = 'FDFF'x ||,
  409.           substr( reverse( d2c(length(ea_string))), 1, 2, '00'x ) ||,
  410.           ea_string
  411.  
  412. return SysPutEA( file, name, ea )
  413.  
  414.  
  415. /* $Id: url.cms,v 1.15 2001/05/11 08:54:34 glass Exp $ */
  416.  
  417. /*------------------------------------------------------------------
  418.  * Decode URL
  419.  *------------------------------------------------------------------*/
  420. DecodeURL: procedure expose (globals)
  421.  
  422.   parse arg url
  423.  
  424.   do while pos( "%", url ) > 0
  425.  
  426.      i = pos( "%", url )
  427.  
  428.      url = substr( url, 1, i-1      ) ||,
  429.            x2c(substr( url, i+1, 2 )) ||,
  430.            substr( url, i+3         )
  431.   end
  432.  
  433. return url
  434.  
  435. /*------------------------------------------------------------------
  436.  * Get filename from URL
  437.  *------------------------------------------------------------------*/
  438. GetFileFromURL: procedure expose (globals)
  439.  
  440.   /* generic-RL syntax consists of six components:           */
  441.   /* <scheme>://<net_loc>/<path>;<params>?<query>#<fragment> */
  442.  
  443.   parse arg url
  444.   url = strip(url)
  445.  
  446.   i = lastpos( "#", url )
  447.   if i > 0 then url = substr( url, 1, i-1 )
  448.  
  449.   i = pos( ":", url )
  450.   if i > 0 then url = substr( url, i+1 )
  451.  
  452.   if left(url,2) == "//" then do
  453.      i = pos( "/", url, 3 )
  454.      if i > 0 then
  455.         url = substr( url, i )
  456.      else
  457.         url = ""
  458.   end
  459.  
  460.   i = lastpos( "?", url )
  461.   if i > 0 then url = substr( url, 1, i-1 )
  462.  
  463.   i = lastpos( ";", url )
  464.   if i > 0 then url = substr( url, 1, i-1 )
  465.  
  466.   i = lastpos( "/", url )
  467.   if i > 0 then url = substr( url, i+1 )
  468.  
  469.   if url == "" then url = "index.html"
  470. return DecodeURL(url)
  471.  
  472.  
  473. /* $Id: logs.cms,v 1.21 2001/05/11 08:54:34 glass Exp $ */
  474.  
  475. /*------------------------------------------------------------------
  476.  * Open log file
  477.  *------------------------------------------------------------------*/
  478. LogOpen: procedure expose (globals)
  479.  
  480.   parse arg logfile
  481.  
  482.   if logfile == "" | translate(logfile) == "NUL" then
  483.      return 0
  484.  
  485.   do 5
  486.      rc = stream( logfile, "c", sys.open_write )
  487.      if rc == "READY:" then leave
  488.      call SysSleep 1
  489.   end
  490.  
  491. return (rc == "READY:")
  492.  
  493. /*------------------------------------------------------------------
  494.  * Write Log Record
  495.  *------------------------------------------------------------------*/
  496. LogPut: procedure expose (globals)
  497.  
  498.   parse arg type, message, file_or_url, information
  499.   type = translate(type)
  500.  
  501.   if type == "INF" then type = "INFO"
  502.   if type == "ERR" then type = "ERROR"
  503.   if type == "BLD" then type = "BOLD"
  504.  
  505.   select
  506.     when type == "BOLD" | type == "INFO" then do
  507.       logfile  = cfg.log_file
  508.       end
  509.  
  510.     when type == "ERROR" then do
  511.       logfile  = cfg.error_log
  512.       end
  513.  
  514.     otherwise do
  515.       type     = "USUAL"
  516.       logfile  = cfg.log_file
  517.       end
  518.   end
  519.  
  520.   /* Output to file */
  521.   if LogOpen(logfile) then do
  522.  
  523.      if arg( 3, "exists" ) then
  524.         call lineout logfile, date('e')" "time()" "message": "file_or_url
  525.      else
  526.         call lineout logfile, date('e')" "time()" "message
  527.  
  528.      if arg( 4, "exists" ) & information \= "" then
  529.         call lineout logfile, copies( ' ', 18 ) || information
  530.  
  531.      rc = stream( logfile, "c", "close" )
  532.   end
  533.  
  534.   /* Sound's alert */
  535.   if type == "ERROR" then
  536.      call beep 1000, 100
  537.  
  538.   /* Display message */
  539.   if arg( 3, "exists" ) then do
  540.      if length(file_or_url) + length(message) < 73 then
  541.         say color.type"■■■ "message": "file_or_url
  542.      else do
  543.         say color.type"■■■ "message": "
  544.         say color.type"    "ShortenURL( file_or_url, 75 )
  545.      end
  546.      end
  547.   else
  548.      say color.type"■■■ "message
  549.  
  550.   if arg( 4, "exist" ) & information \= "" then
  551.      say color.type"    "information
  552.  
  553.   call charout, color.usual
  554. return
  555.  
  556. /* $Id: shorten.cms,v 1.3 2001/05/11 08:54:34 glass Exp $ */
  557.  
  558. /*------------------------------------------------------------------
  559.  * Reduces URL length
  560.  *------------------------------------------------------------------*/
  561. ShortenURL: procedure expose (globals)
  562.  
  563.   parse arg url, max
  564.  
  565.   if length(url) > max then
  566.      do
  567.        if translate( left( url, 6 )) == "FTP://" then
  568.           shorten = left( url, 6 )
  569.        else if translate( left( url, 7 )) == "HTTP://" then
  570.           shorten = left( url, 7 )
  571.        else if substr( url, 3, 1 ) == "\" then
  572.           shorten = left( url, 3 )
  573.        else
  574.           shorten = ""
  575.  
  576.        shorten = shorten || "..." ||,
  577.                  right( url, max - length(shorten) - 3 )
  578.      end
  579.   else
  580.      shorten = url
  581.  
  582. return shorten
  583.  
  584. /* $Id: replace.cms,v 1.5 2001/05/11 08:54:34 glass Exp $ */
  585.  
  586. /*------------------------------------------------------------------
  587.  * Search and replace string
  588.  *------------------------------------------------------------------*/
  589. replace: procedure expose (globals)
  590.  
  591.   parse arg source, string, substitute
  592.   string = translate(string)
  593.  
  594.   i = pos( string, translate(source))
  595.  
  596.   do while i \= 0
  597.      source = substr( source, 1, i-1 ) || substitute ||,
  598.               substr( source, i+length(string))
  599.  
  600.      i = pos( string, translate(source), i + length(substitute))
  601.   end
  602.  
  603. return source
  604.