home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / awgt1512.zip / awgets.cmd < prev    next >
OS/2 REXX Batch file  |  1999-07-25  |  16KB  |  555 lines

  1. /* Auto WGet Download Utility $Revision: 1.5.1.2 $
  2.  *
  3.  * Great idea and first release (C) 1998 by Steve Trubachev
  4.  * Final release (C) 1998-1999 by Dmitry A.Steklenev
  5.  *
  6.  */
  7.  
  8. call RxFuncAdd 'SysLoadFuncs', 'RexxUtil', 'SysLoadFuncs'
  9. call SysLoadFuncs
  10.  
  11. parse source os what program tail
  12. dll = substr( program, 1, lastpos( "\", program )) || "awget.dll"
  13.  
  14. call RxFuncAdd 'AwLoadFuncs', dll, 'AwLoadFuncs'
  15. call AwLoadFuncs
  16.  
  17. '@echo off'
  18.  
  19. if translate( value( "REXX_TRACE",, "OS2ENVIRONMENT" )) == "YES" then do
  20.    trace intermediate
  21.    trace results
  22. end
  23.  
  24. CfgFile = value( "ETC",, "OS2ENVIRONMENT" )"\awget.cfg"
  25.  
  26. /*------------------------------------------------------------------
  27.  * Initialization
  28.  *------------------------------------------------------------------*/
  29.  
  30. say "Auto WGet Download Utility $Revision: 1.5.1.2 $"
  31. say "Great idea and first release (C) 1998 by Steve Trubachev"
  32. say "Final release (C) 1998-1999 by Dmitry A.Steklenev"
  33. say ""
  34.  
  35. parse arg urlfile
  36.  
  37. call MsgRead  "awget"
  38. call CfgRead  CfgFile
  39. call CfgShow
  40. say  ""
  41. call DirRead
  42. call DirShow
  43. say  ""
  44.  
  45. if left(urlfile,1) == '"' & right(urlfile,1) == '"' then
  46.    urlfile = substr( urlfile, 2, length(urlfile) - 2 )
  47.  
  48. /*------------------------------------------------------------------
  49.  * Download file
  50.  *------------------------------------------------------------------*/
  51.  
  52. url    = GetURLFromFile(urlfile)
  53. flock  = AwLockSet( filespec( "name", urlfile ))
  54.  
  55. if left( flock, 6 ) == "ERROR:" then
  56.    call LogPut cfg.errorlog, "AwLockSet('"filespec( "name", urlfile )"'), rc="flock
  57.  
  58. logs   = ""
  59. handle = AwPOpen( 'wget.exe', cfg.wget_parameters '--directory-prefix='cfg.download '"'url'"' )
  60.  
  61. do until line == ""
  62.    line = AwPRead( handle )
  63.    logs = logs || line
  64.    call charout , line
  65. end
  66.  
  67. rc = AwPClose( handle )
  68.  
  69. if substr( rc, 6 ) \= "ERROR:" & rc == 0 then do
  70.  
  71.    /* Put URL in EA */
  72.  
  73.    fname = GetFileFromURL(url)
  74.  
  75.    if fname \= "" then
  76.       call WpsPutEA cfg.download"\"fname, ".SUBJECT", url
  77.  
  78.    /* Send message */
  79.  
  80.    if cfg.messages \= 0 then
  81.       call SendMessage 0, cfg.message_done, urlfile, GetURLFromFile(urlfile)
  82.  
  83.    call LogPut cfg.logfile, msg.msg_getdone": "GetURLFromFile(urlfile)
  84.  
  85.    /* Keep URL, if configured */
  86.  
  87.    if cfg.keep_done then do
  88.       rc = WpsMove( urlfile, dir.done"\"filespec( "name", urlfile ))
  89.       rc = WpsRefresh( dir.done )
  90.       end
  91.  
  92.    end
  93. else do
  94.  
  95.    /* Send message */
  96.  
  97.    if cfg.messages \= 0 then
  98.       call SendMessage 1, cfg.message_error, urlfile, GetURLFromFile(urlfile)
  99.  
  100.    call LogPut cfg.errorlog, msg.msg_geterr": "GetURLFromFile(urlfile)
  101.    call LogAdd cfg.errorlog, '0D0A'x || '0D0A'x || logs
  102.  
  103.    /* Keep URL, if configured */
  104.  
  105.    if cfg.keep_failed then do
  106.       rc = WpsMove( urlfile, dir.failed"\"filespec( "name", urlfile ))
  107.       rc = WpsRefresh( dir.failed )
  108.       end
  109. end
  110.  
  111. if stream( urlfile, "c", "query exists" ) \= "" then
  112.    'del "'urlfile'"'
  113.  
  114. rc = WpsRefresh( dir.running )
  115. rc = AwLockRelease( flock )
  116.  
  117. if left( rc, 6 ) == "ERROR:" then
  118.    call LogPut cfg.errorlog, "AwLockRelease('"flock"'), rc="rc
  119.  
  120. exit
  121.  
  122. /*------------------------------------------------------------------
  123.  * Send Message
  124.  *------------------------------------------------------------------*/
  125. SendMessage: procedure expose msg.
  126.  
  127.    parse arg rcCode, msgstr, urlfile, url
  128.  
  129.    if rcCode == 0 then
  130.       msgshow = msg.msg_getdone
  131.    else
  132.       msgshow = msg.msg_geterr
  133.  
  134.    if msgstr == "" then
  135.       msgstr = 'start /n pmpopup2.exe "'msgshow':~~%U" "Auto WGet $Revision: 1.5.1.2 $" /BELL /B1:"OK" /T:3600 /F:"8.Helv"'
  136.  
  137.    i = pos( "%U", msgstr )
  138.    if i > 0 then
  139.       msgstr = substr( msgstr, 1, i-1 ) || url ||,
  140.                substr( msgstr, i+2    )
  141.  
  142.   i = pos( "%F", msgstr )
  143.   if i > 0 then
  144.      msgstr = substr( msgstr, 1, i-1 ) || urlfile ||,
  145.               substr( msgstr, i+2    )
  146.   msgstr
  147.  
  148. return
  149.  
  150. /*------------------------------------------------------------------
  151.  * National Messages Procedures
  152.  *------------------------------------------------------------------*/
  153. MsgRead: procedure expose msg.
  154.  
  155.   parse arg msgfile
  156.   parse source OS2 what msgpath
  157.  
  158.   msgfile = filespec( "disk", msgpath ) ||,
  159.             filespec( "path", msgpath ) || msgfile
  160.  
  161.   country = strip( SysIni( "BOTH", "PM_National", "iCountry" ),, '0'x )
  162.   country = right( country, 3, "0" )
  163.  
  164.   if stream( msgfile"."country, "c", "query exists" ) == "" then
  165.      country = "001"
  166.  
  167.   msgfile = msgfile"."country
  168.   rc = stream( msgfile, "C", "OPEN READ" )
  169.  
  170.   if rc \= "READY:" then do
  171.      say "ERROR: Error open message file: "msgfile
  172.      exit
  173.   end
  174.  
  175.   do while lines(msgfile) > 0
  176.      line = linein(msgfile)
  177.  
  178.      if line \= "" then do
  179.         parse value line with id "=" msg
  180.  
  181.         id  = translate(strip(id))
  182.         msg = strip(msg)
  183.  
  184.         i = pos( "\n", msg )
  185.         do while i > 0
  186.            msg = substr( msg, 1, i-1 ) || '0A0D'x || substr( msg, i+2 )
  187.            i = pos( "\n", msg )
  188.         end
  189.  
  190.         msg.id = msg
  191.      end
  192.   end
  193.  
  194.   rc = stream( msgfile, "C", "CLOSE" )
  195.  
  196. return
  197.  
  198. /*------------------------------------------------------------------
  199.  * Get Yes or No
  200.  *------------------------------------------------------------------*/
  201. MsgYesNo: procedure expose msg.
  202.  
  203.    parse arg prompt
  204.    ok = 0
  205.  
  206.    do until ok
  207.       call charout, prompt"? "
  208.       pull reply
  209.       reply = left(reply,1)
  210.  
  211.       ok = (reply == "Y") |,
  212.            (reply == "N") |,
  213.            (pos( reply, msg.msg_yes ) > 0 ) |,
  214.            (pos( reply, msg.msg_no  ) > 0 )
  215.  
  216.       if \ok then do
  217.          say msg.msg_badyn
  218.       end
  219.    end
  220.  
  221. return (reply = "Y") | (pos( reply, msg.msg_yes ) > 0 )
  222.  
  223. /*------------------------------------------------------------------
  224.  * Get Configuration
  225.  *------------------------------------------------------------------*/
  226. CfgRead: procedure expose cfg. msg.
  227.  
  228.   parse arg CfgFile
  229.  
  230.   cfg.download          = "."
  231.   cfg.maximum_downloads = 3
  232.   cfg.messages          = 1
  233.   cfg.message_done      = ""
  234.   cfg.message_error     = ""
  235.   cfg.wget_parameters   = "-c -t 0 -w 30"
  236.   cfg.scan_interval     = 60
  237.   cfg.check_connection  = 0
  238.   cfg.logfile           = ""
  239.   cfg.errorlog          = ""
  240.   cfg.use_desktop       = 0
  241.   cfg.keep_failed       = 1
  242.   cfg.keep_done         = 0
  243.  
  244.   rc = stream( CfgFile, "C", "OPEN READ" )
  245.  
  246.   do while lines(CfgFile) > 0
  247.      parse value linein(CfgFile) with command "=" argument
  248.  
  249.      command = translate(strip(command))
  250.      argument = strip(argument)
  251.  
  252.      select
  253.         when command == "DOWNLOAD" then
  254.              cfg.download = argument
  255.         when command == "MAXIMUM_DOWNLOADS_SIMULTANEOUSLY" then
  256.              cfg.maximum_downloads = argument
  257.         when command == "MESSAGES" then
  258.              cfg.messages = (argument == "1")
  259.         when command == "WGET_PARAMETERS" then
  260.              cfg.wget_parameters = argument
  261.         when command == "SCAN_INTERVAL" then
  262.              cfg.scan_interval = argument
  263.         when command == "LOG_FILE" then
  264.              cfg.logfile = argument
  265.         when command == "ERROR_LOG" then
  266.              cfg.errorlog = argument
  267.         when command == "MESSAGE_DONE" then
  268.              cfg.message_done = argument
  269.         when command == "MESSAGE_ERROR" then
  270.              cfg.message_error = argument
  271.         when command == "CHECK_CONNECTION" then
  272.              cfg.check_connection = (argument == "1")
  273.         when command == "USE_DESKTOP" then
  274.              cfg.use_desktop = (argument == "1")
  275.         when command == "KEEP_FAILED_URL" then
  276.              cfg.keep_failed = (argument == "1")
  277.         when command == "KEEP_DONE_URL" then
  278.              cfg.keep_done = (argument == "1")
  279.         otherwise
  280.      end
  281.   end
  282.  
  283.   if right( cfg.download, 1 ) = "\" then
  284.      cfg.download = left( cfg.download, length(cfg.download) - 1 )
  285.  
  286.   setlocal
  287.   cfg.download = directory( cfg.download )
  288.   endlocal
  289.  
  290.   if cfg.download == "" then do
  291.      call LogPut cfg.errorlog, msg.msg_invdown
  292.      cfg.download = "."
  293.   end
  294.  
  295.   rc = stream( CfgFile, "C", "CLOSE" )
  296.   cfg.file_date = stream( CfgFile, "C", "QUERY DATETIME" )
  297.  
  298. return
  299.  
  300. /*------------------------------------------------------------------
  301.  * Show Configuration
  302.  *------------------------------------------------------------------*/
  303. CfgShow: procedure expose cfg. msg.
  304.  
  305.   say "*** "msg.msg_downdir  || ": "cfg.download
  306.   say "*** "msg.msg_maxcon   || ": "cfg.maximum_downloads
  307.   say "*** "msg.msg_show     || ": "cfg.messages
  308.  
  309.   if cfg.message_done \= "" then
  310.      say "*** "msg.msg_done  || ": "cfg.message_done
  311.  
  312.   if cfg.message_error \= "" then
  313.      say "*** "msg.msg_error || ": "cfg.message_error
  314.  
  315.   say "*** "msg.msg_interval || ": "cfg.scan_interval
  316.   say "*** "msg.msg_chckconn || ": "cfg.check_connection
  317.   say "*** "msg.msg_parm     || ": "cfg.wget_parameters
  318.   say "*** "msg.msg_usedesk  || ": "cfg.use_desktop
  319.   say "*** "msg.msg_keepfail || ": "cfg.keep_failed
  320.   say "*** "msg.msg_keepdone || ": "cfg.keep_done
  321.  
  322. return
  323.  
  324. /*------------------------------------------------------------------
  325.  * Save Configuration
  326.  *------------------------------------------------------------------*/
  327. CfgSave: procedure expose cfg. msg.
  328.  
  329.   parse arg CfgFile
  330.  
  331.   if stream( CfgFile, "C", "QUERY EXISTS" ) \= "" then
  332.      "del "CfgFile
  333.  
  334.   rc = stream( CfgFile, "C", "OPEN WRITE" )
  335.  
  336.   rc = lineout( CfgFile, "download = "cfg.download )
  337.   rc = lineout( CfgFile, "maximum_downloads_simultaneously = "cfg.maximum_downloads )
  338.   rc = lineout( CfgFile, "messages = "cfg.messages )
  339.  
  340.   if cfg.message_done  \= "" then
  341.      rc = lineout( CfgFile, "message_done  = "cfg.message_done  )
  342.  
  343.   if cfg.message_error \= "" then
  344.      rc = lineout( CfgFile, "message_error = "cfg.message_error )
  345.  
  346.   rc = lineout( CfgFile, "log_file = "cfg.logfile )
  347.   rc = lineout( CfgFile, "error_log = "cfg.errorlog )
  348.   rc = lineout( CfgFile, "wget_parameters = "cfg.wget_parameters )
  349.   rc = lineout( CfgFile, "scan_interval = "cfg.scan_interval )
  350.   rc = lineout( CfgFile, "check_connection = "cfg.check_connection )
  351.   rc = lineout( CfgFile, "use_desktop = "cfg.use_desktop )
  352.   rc = lineout( CfgFile, "keep_failed_url = "cfg.keep_failed )
  353.   rc = lineout( CfgFile, "keep_done_url = "cfg.keep_done )
  354.  
  355.   rc = stream( CfgFile, "C", "CLOSE" )
  356.  
  357. return
  358.  
  359. /*------------------------------------------------------------------
  360.  * Check Configuration
  361.  *------------------------------------------------------------------*/
  362. CfgCheck: procedure expose cfg. msg.
  363.  
  364.   parse arg CfgFile
  365.  
  366.   if cfg.file_date \= stream( CfgFile, "C", "QUERY DATETIME" ) then do
  367.  
  368.      call LogPut cfg.logfile, msg.msg_readcfg
  369.      call CfgRead CfgFile
  370.      say
  371.      call CfgShow
  372.      say
  373.   end
  374.  
  375. return
  376.  
  377. /*------------------------------------------------------------------
  378.  * Get URL from file
  379.  *------------------------------------------------------------------*/
  380. GetURLFromFile: procedure
  381.  
  382.   parse arg filename
  383.  
  384.   rc  = stream(filename, "c", "open read" )
  385.   url = linein(filename)
  386.   rc  = stream(filename, "c", "close" )
  387.  
  388.   do while pos( "%", url ) > 0
  389.  
  390.      i = pos( "%", url )
  391.  
  392.      url = substr( url, 1, i-1      ) ||,
  393.            x2c(substr( url, i+1, 2 )) ||,
  394.            substr( url, i+3         )
  395.   end
  396.  
  397. return url
  398.  
  399. /*------------------------------------------------------------------
  400.  * Get filename from URL
  401.  *------------------------------------------------------------------*/
  402. GetFileFromURL: procedure
  403.  
  404.   parse arg url
  405.  
  406.   pos_parm = pos( "?", url )
  407.   if pos_parm \= 0 then
  408.     url = substr( url, 1, pos_parm-1 )
  409.  
  410.   pos_name = lastpos( "/", url )
  411.   if pos_name \= 0 then
  412.     url = substr( url, pos_name+1 )
  413.  
  414. return url
  415.  
  416. /*------------------------------------------------------------------
  417.  * Check valid URL file
  418.  *------------------------------------------------------------------*/
  419. IsURLFile: procedure
  420.  
  421.   parse arg filename
  422.  
  423.   rc  = stream(filename, "c", "open read" )
  424.   url = translate( strip( charin(filename,1,16)))
  425.   rc  = stream(filename, "c", "close" )
  426.  
  427. return substr( url, 1, 7 ) == "HTTP://" |,
  428.        substr( url, 1, 6 ) == "FTP://"
  429.  
  430. /*------------------------------------------------------------------
  431.  * Write Log Record
  432.  *------------------------------------------------------------------*/
  433. LogPut: procedure expose cfg.
  434.  
  435.   parse arg logfile, message
  436.  
  437.   say "■■■ "message
  438.   if logfile \= "" then do
  439.      call lineout logfile, date()" "time()" -> "message
  440.      rc = stream( logfile, "c", "close" )
  441.   end
  442.  
  443. return
  444.  
  445. /*------------------------------------------------------------------
  446.  * Append Log Record
  447.  *------------------------------------------------------------------*/
  448. LogAdd: procedure expose cfg.
  449.  
  450.   parse arg logfile, message
  451.  
  452.   if logfile \= "" then do
  453.      call lineout logfile, date()" "time()" -> "message
  454.      rc = stream( logfile, "c", "close" )
  455.   end
  456.  
  457. return
  458.  
  459.  
  460. /*------------------------------------------------------------------
  461.  * Move file
  462.  *------------------------------------------------------------------*/
  463. WpsMove: procedure expose msg. cfg.
  464.  
  465.   parse arg file_from, file_to
  466.  
  467.   rc = SysGetEA( file_from, ".LONGNAME", file_long )
  468.  
  469.   if file_long == "" then
  470.      rc = WpsPutEA( file_from, ".LONGNAME", filespec( "name", file_from ))
  471.  
  472.   if stream( file_to, "c", "query exists" ) \= "" then do
  473.      file_to = SysTempFileName( file_to"!???" )
  474.   end
  475.  
  476.   'copy "'file_from'" "'file_to'" 1> nul 2> nul'
  477.  
  478.   if rc == 0 then
  479.      'del "'file_from'" 1> nul 2> nul'
  480.   else do
  481.      call LogPut cfg.error_log msg.msg_notmove": "file_from" -> "file_to
  482.      file_to = ""
  483.   end
  484.  
  485. return file_to
  486.  
  487. /*------------------------------------------------------------------
  488.  * Write a named ascii extended attribute to a file
  489.  *------------------------------------------------------------------*/
  490. WpsPutEA: procedure
  491.  
  492.   parse arg file, name, value
  493.  
  494.   value = 'FDFF'x ||,
  495.            substr( d2c(length(value)), 1, 2, '00'x ) || value
  496.  
  497. return SysPutEA( file, name, value )
  498.  
  499. /*------------------------------------------------------------------
  500.  * Refresh Folder
  501.  *------------------------------------------------------------------*/
  502. WpsRefresh: procedure
  503.  
  504.   parse arg path
  505.  
  506. return SysSetObjectData( path, "MENUITEMSELECTED=503" )
  507.  
  508. /*------------------------------------------------------------------
  509.  * Get Directorys
  510.  *------------------------------------------------------------------*/
  511. DirRead: procedure expose dir. msg. cfg.
  512.  
  513.   dir.todo    = AwGetObjectPath( "<AWG_TODO>"   )
  514.   dir.running = AwGetObjectPath( "<AWG_RUN>"    )
  515.   dir.done    = AwGetObjectPath( "<AWG_DONE>"   )
  516.   dir.failed  = AwGetObjectPath( "<AWG_ERROR>"  )
  517.   dir.desktop = AwGetObjectPath( "<WP_DESKTOP>" )
  518.  
  519.   /* check it */
  520.  
  521.   if dir.todo    == "" then do
  522.      call LogPut cfg.errorlog, msg.msg_notfound': "ToDo"'
  523.      exit 1
  524.   end
  525.   if dir.running == "" then do
  526.      call LogPut cfg.errorlog, msg.msg_notfound': "Running"'
  527.      exit 1
  528.   end
  529.   if dir.done    == "" then do
  530.      call LogPut cfg.errorlog, msg.msg_notfound': "Done"'
  531.      exit 1
  532.   end
  533.   if dir.failed  == "" then do
  534.      call LogPut cfg.errorlog, msg.msg_notfound': "Failed"'
  535.      exit 1
  536.   end
  537.   if dir.desktop == "" then do
  538.      call LogPut cfg.errorlog, msg.msg_notfound': "Desktop"'
  539.      exit 1
  540.   end
  541.  
  542. return
  543.  
  544. /*------------------------------------------------------------------
  545.  * Show Directorys
  546.  *------------------------------------------------------------------*/
  547. DirShow: procedure expose dir.
  548.  
  549.   say "*** ToDo:    "dir.todo
  550.   say "*** Running: "dir.running
  551.   say "*** Done:    "dir.done
  552.   say "*** Failed:  "dir.failed
  553.   say "*** Desktop: "dir.desktop
  554. return
  555.