home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / awget167.zip / awgadd.cmd next >
OS/2 REXX Batch file  |  2001-06-11  |  14KB  |  476 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.11 2001/05/16 17:15:10 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.7"
  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 left(url,1) == '"' & right(url,1) == '"' then
  86.    url = substr( url, 2, length(url) - 2 )
  87.  
  88. if url == "" then do
  89.    say "Usage: awgadd url_or_list_name [todo_folder]"
  90.    exit 1
  91. end
  92.  
  93. if stream( url, "c", "query exists" ) \= "" then do
  94.    do while lines(url) > 0
  95.       next = strip(linein(url))
  96.       if next \= "" then
  97.          call CreateURL next, home
  98.    end
  99.    call stream url, "c", "close"
  100.    end
  101. else
  102.    call CreateURL url, home
  103.  
  104. call lineout sys_pushing, "It is demand."
  105. call stream  sys_pushing, 'c', 'close'
  106. exit 0
  107.  
  108. syntax:
  109.  
  110.   parse source . . program .
  111.  
  112.   if sourceline() >= SIGL then
  113.      sourceline = strip(sourceline(SIGL))
  114.   else
  115.      sourceline = ""
  116.  
  117.   call LogPut err, condition('C') rc "running" filespec( "name", program )", line " ||,
  118.                    SIGL errortext(rc) condition('D'),, sourceline
  119.   exit 256
  120.  
  121. /*------------------------------------------------------------------
  122.  * Creates URL object
  123.  *------------------------------------------------------------------*/
  124. CreateURL: procedure expose (globals)
  125.  
  126.   parse arg url, home
  127.  
  128.   if left(url,1) == '"' & right(url,1) == '"' then
  129.      url = substr( url, 2, length(url) - 2 )
  130.  
  131.   if SysOs2Ver() > "2.30" then
  132.      rc = SysCreateObject( "WPUrl", GetFileFromURL(url), home, "URL="url, "R" )
  133.   else do
  134.      object = SysTempFileName( home"\add!!???" )
  135.  
  136.      call lineout  object, url
  137.      call stream   object, "c", "close"
  138.      call WpsPutEA object, ".LONGNAME", GetFileFromURL(url)
  139.      call WpsRefresh home
  140.   end
  141. return
  142.  
  143. /* $Id: init.cms,v 1.26 2001/05/11 09:45:01 glass Exp $ */
  144.  
  145. /*------------------------------------------------------------------
  146.  * Initialization
  147.  *------------------------------------------------------------------*/
  148. AwInit: procedure expose (globals)
  149.  
  150.   if RxFuncQuery('SysLoadFuncs') then do
  151.      call RxFuncAdd 'SysLoadFuncs', 'RexxUtil', 'SysLoadFuncs'
  152.      call SysLoadFuncs
  153.   end
  154.  
  155.   parse source os what program tail
  156.   lpath = substr( program, 1, lastpos( "\", program ))
  157.   dll   = lpath || "awget.dll"
  158.  
  159.   if RxFuncQuery('AwLoadFuncs') then do
  160.      call RxFuncAdd  'AwLoadFuncs', dll, 'AwLoadFuncs'
  161.      call AwLoadFuncs
  162.   end
  163.  
  164.   '@echo off'
  165.  
  166.   sys.config_file = value( "ETC",, "OS2ENVIRONMENT" )"\awget.cfg"
  167.   sys.connected   = 1
  168.   sys.active_time = 0
  169.  
  170.   /* enable ANSI extended screen and keyboard control */
  171.   '@ansi on > nul'
  172.  
  173.   color.brown   = "1B"x"[0;33m"
  174.   color.red     = "1B"x"[1;31m"
  175.   color.green   = "1B"x"[1;32m"
  176.   color.yellow  = "1B"x"[1;33m"
  177.   color.blue    = "1B"x"[1;34m"
  178.   color.magenta = "1B"x"[1;35m"
  179.   color.cyan    = "1B"x"[1;36m"
  180.   color.white   = "1B"x"[1;37m"
  181.   color.gray    = "1B"x"[0m"
  182.  
  183.   color.usual   = color.gray
  184.   color.bold    = color.white
  185.   color.error   = color.red
  186.   color.info    = color.green
  187.   color.debug   = color.brown
  188.  
  189.   /* known download utilites */
  190.  
  191.   sys.utils.0      = 2
  192.   sys.utils.1.prog = wget.exe
  193.   sys.utils.1.name = "GNU WGet"
  194.   sys.utils.1.parm = '-c -t 10 -w 30 -P "%p" "%u"'
  195.   sys.utils.2.prog = curl.exe
  196.   sys.utils.2.name = "cURL"
  197.   sys.utils.2.parm = '-y 300 -Y 1 -v -C - -o "%p/%f" "%u"'
  198.  
  199.   sys.utils.default.prog = sys.utils.1.prog
  200.   sys.utils.default.name = sys.utils.1.name
  201.   sys.utils.default.parm = sys.utils.1.parm
  202.  
  203.   /* service semaphores */
  204.  
  205.   sys.running = "$live$"
  206.   sys.killing = "$stop$"
  207.   sys.pushing = "$push$"
  208.   sys.tqueue  = "$term$"
  209.  
  210.   /* stream's open modes */
  211.  
  212.   parse version version .
  213.   if version = "OBJREXX" then do
  214.      sys.open_read  = "OPEN READ  SHAREREAD"
  215.      sys.open_write = "OPEN WRITE SHAREREAD"
  216.      end
  217.   else do
  218.      sys.open_read  = "OPEN READ"
  219.      sys.open_write = "OPEN WRITE"
  220.   end
  221. return
  222.  
  223. /* $Id: nls.cms,v 1.13 2001/05/11 08:54:34 glass Exp $ */
  224.  
  225. /*------------------------------------------------------------------
  226.  * Read messages
  227.  *------------------------------------------------------------------*/
  228. MsgRead: procedure expose (globals)
  229.  
  230.   parse arg msgfile
  231.   parse source OS2 what msgpath
  232.  
  233.   msgfile = filespec( "disk", msgpath ) ||,
  234.             filespec( "path", msgpath ) || "NLS\" || msgfile
  235.  
  236.   country = MsgCountryID()
  237.  
  238.   if stream( msgfile"."country, "c", "query exists" ) == "" then
  239.      country = "001"
  240.  
  241.   msgfile = msgfile"."country
  242.   rc = stream( msgfile, "C", sys.open_read )
  243.  
  244.   if rc \= "READY:" then do
  245.      say  color.error || "■■■ Error on open NLS file: "msgfile
  246.      exit 1
  247.   end
  248.  
  249.   do while lines(msgfile) > 0
  250.      line = strip(linein(msgfile))
  251.  
  252.      do while right(line,1) == "\"
  253.         line = left( line, length(line)-1 )
  254.         line = line || strip(linein(msgfile))
  255.      end
  256.  
  257.      if line \= "" & left(line,1) \= "#" then do
  258.         parse value line with id "=" msg
  259.  
  260.         id  = strip(id )
  261.         msg = strip(msg)
  262.  
  263.         i = pos( "\n", msg )
  264.         do while i > 0
  265.            msg = substr( msg, 1, i-1 ) || '0D0A'x || substr( msg, i+2 )
  266.            i = pos( "\n", msg )
  267.         end
  268.  
  269.         msg.id = msg
  270.      end
  271.   end
  272.  
  273.   rc = stream( msgfile, "C", "CLOSE" )
  274. return
  275.  
  276. /*------------------------------------------------------------------
  277.  * Returns Country Identifier
  278.  *------------------------------------------------------------------*/
  279. MsgCountryID: procedure expose (globals)
  280.  
  281.   country = strip( SysIni( "BOTH", "PM_National", "iCountry" ),, '0'x )
  282.  
  283.   if country == "ERROR:" then
  284.      country =  "001"
  285.   else
  286.      country =  right( country, 3, "0" )
  287.  
  288. return country
  289.  
  290.  
  291. /* $Id: config.cms,v 1.29 2001/05/11 08:54:34 glass Exp $ */
  292.  
  293. /*------------------------------------------------------------------
  294.  * Get Configuration
  295.  *------------------------------------------------------------------*/
  296. CfgRead: procedure expose (globals)
  297.  
  298.   cfg.home                     = "."
  299.   cfg.download                 = "."
  300.   cfg.downloads_simultaneously = 3
  301.   cfg.downloads_attempts       = 15
  302.   cfg.downloads_utility        = sys.utils.default.prog
  303.   cfg.downloads_parameters     = sys.utils.default.parm
  304.   cfg.scan_interval            = 30
  305.   cfg.log_file                 = "nul"
  306.   cfg.error_log                = "nul"
  307.   cfg.log_keep                 = 15
  308.   cfg.message_done             = 'start /n pmpopup2.exe "%m:~~%u" "Auto WGet Daemon" /BELL /B1:"OK" /T:900 /F:"8.Helv"'
  309.   cfg.message_error            = 'start /n pmpopup2.exe "%m:~~%u~%i" "Auto WGet Daemon" /BELL /B1:"OK" /T:900 /F:"8.Helv"'
  310.   cfg.messages                 = 1
  311.   cfg.check_connection         = 0
  312.   cfg.use_desktop              = 0
  313.   cfg.keep_failed_url          = 1
  314.   cfg.keep_done_url            = 0
  315.  
  316.   rc = stream( sys.config_file, "C", sys.open_read )
  317.  
  318.   do while lines(sys.config_file) > 0
  319.      parse value linein(sys.config_file) with command "=" argument
  320.  
  321.      command  = translate(strip(command))
  322.      argument = strip(argument)
  323.  
  324.      select
  325.         when command == "HOME",
  326.            | command == "DOWNLOAD",
  327.            | command == "DOWNLOADS_SIMULTANEOUSLY",
  328.            | command == "DOWNLOADS_ATTEMPTS",
  329.            | command == "DOWNLOADS_UTILITY",
  330.            | command == "DOWNLOADS_PARAMETERS",
  331.            | command == "SCAN_INTERVAL",
  332.            | command == "LOG_FILE",
  333.            | command == "ERROR_LOG",
  334.            | command == "LOG_KEEP",
  335.            | command == "MESSAGE_DONE",
  336.            | command == "MESSAGE_ERROR" then
  337.  
  338.              cfg.command = argument
  339.  
  340.         when command == "MESSAGES",
  341.            | command == "CHECK_CONNECTION",
  342.            | command == "USE_DESKTOP",
  343.            | command == "KEEP_FAILED_URL",
  344.            | command == "KEEP_DONE_URL" then
  345.  
  346.              cfg.command = (argument == "1")
  347.         otherwise
  348.      end
  349.   end
  350.  
  351.   rc = stream( sys.config_file, "C", "CLOSE" )
  352.   cfg.file_date = stream( sys.config_file, "C", "QUERY DATETIME" )
  353. return
  354.  
  355.  
  356. /* $Id: dirs.cms,v 1.24 2001/05/11 08:54:34 glass Exp $ */
  357.  
  358. /*------------------------------------------------------------------
  359.  * Get Directorys
  360.  *------------------------------------------------------------------*/
  361. DirRead: procedure expose (globals)
  362.  
  363.   dir.todo    = AwGetObjectPath( "<AWG2_TODO>"    )
  364.   dir.running = AwGetObjectPath( "<AWG2_RUNNING>" )
  365.   dir.done    = AwGetObjectPath( "<AWG2_DONE>"    )
  366.   dir.failed  = AwGetObjectPath( "<AWG2_FAILED>"  )
  367.   dir.jobs    = AwGetObjectPath( "<AWG2_JOBS>"    )
  368.   dir.desktop = AwGetObjectPath( "<WP_DESKTOP>"   )
  369.  
  370.   /* check it */
  371.   dir_list = "ToDo Running Done Failed Jobs Desktop"
  372.   do i = 1 to words(dir_list)
  373.      name = translate( word( dir_list,i ))
  374.      if dir.name == "" then do
  375.         call LogPut err, msg.not_found, word( dir_list,i )
  376.         exit 1
  377.      end
  378.   end
  379. return
  380.  
  381.  
  382. /* $Id: wps.cms,v 1.19 2001/05/11 08:54:34 glass Exp $ */
  383.  
  384. /*------------------------------------------------------------------
  385.  * Refresh Folder
  386.  *------------------------------------------------------------------*/
  387. WpsRefresh: procedure expose (globals)
  388.  
  389.   parse arg path
  390.  
  391. return SysSetObjectData( path, "MENUITEMSELECTED=503" )
  392.  
  393. /*------------------------------------------------------------------
  394.  * Write a named ascii extended attribute to a file
  395.  *------------------------------------------------------------------*/
  396. WpsPutEA: procedure expose (globals)
  397.  
  398.   parse arg file, name, ea_string
  399.   ea = ""
  400.  
  401.   if pos( '00'x, ea_string ) > 0 then do
  402.      do ea_count = 0 while length( ea_string ) > 0
  403.        parse value ea_string with string '00'x ea_string
  404.        ea = ea || 'FDFF'x ||,
  405.             substr( reverse( d2c(length(string))), 1, 2, '00'x ) ||,
  406.             string
  407.      end
  408.      ea = 'DFFF'x || '0000'x ||,
  409.           substr( reverse( d2c(ea_count)), 1, 2, '00'x ) || ea
  410.      end
  411.   else
  412.      ea = 'FDFF'x ||,
  413.           substr( reverse( d2c(length(ea_string))), 1, 2, '00'x ) ||,
  414.           ea_string
  415.  
  416. return SysPutEA( file, name, ea )
  417.  
  418.  
  419. /* $Id: url.cms,v 1.15 2001/05/11 08:54:34 glass Exp $ */
  420.  
  421. /*------------------------------------------------------------------
  422.  * Decode URL
  423.  *------------------------------------------------------------------*/
  424. DecodeURL: procedure expose (globals)
  425.  
  426.   parse arg url
  427.  
  428.   do while pos( "%", url ) > 0
  429.  
  430.      i = pos( "%", url )
  431.  
  432.      url = substr( url, 1, i-1      ) ||,
  433.            x2c(substr( url, i+1, 2 )) ||,
  434.            substr( url, i+3         )
  435.   end
  436.  
  437. return url
  438.  
  439. /*------------------------------------------------------------------
  440.  * Get filename from URL
  441.  *------------------------------------------------------------------*/
  442. GetFileFromURL: procedure expose (globals)
  443.  
  444.   /* generic-RL syntax consists of six components:           */
  445.   /* <scheme>://<net_loc>/<path>;<params>?<query>#<fragment> */
  446.  
  447.   parse arg url
  448.   url = strip(url)
  449.  
  450.   i = lastpos( "#", url )
  451.   if i > 0 then url = substr( url, 1, i-1 )
  452.  
  453.   i = pos( ":", url )
  454.   if i > 0 then url = substr( url, i+1 )
  455.  
  456.   if left(url,2) == "//" then do
  457.      i = pos( "/", url, 3 )
  458.      if i > 0 then
  459.         url = substr( url, i )
  460.      else
  461.         url = ""
  462.   end
  463.  
  464.   i = lastpos( "?", url )
  465.   if i > 0 then url = substr( url, 1, i-1 )
  466.  
  467.   i = lastpos( ";", url )
  468.   if i > 0 then url = substr( url, 1, i-1 )
  469.  
  470.   i = lastpos( "/", url )
  471.   if i > 0 then url = substr( url, i+1 )
  472.  
  473.   if url == "" then url = "index.html"
  474. return DecodeURL(url)
  475.  
  476.