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

  1. /* Auto WGet Daemon $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 Daemon $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. pppOKey = 1
  36.  
  37. call MsgRead  "awget"
  38. call CfgRead  CfgFile
  39. call CfgShow
  40. say  ""
  41. call DirRead
  42. call DirShow
  43. say  ""
  44.  
  45. /*------------------------------------------------------------------
  46.  * Start Daemon
  47.  *------------------------------------------------------------------*/
  48.  
  49. do forever
  50.  
  51.    /* Check configuration file */
  52.  
  53.    call CfgCheck CfgFile
  54.  
  55.    rc = SysFileTree( dir.todo"\*"   ,'todo'   , 'FO' )
  56.    rc = SysFileTree( dir.running"\*",'running', 'FO' )
  57.  
  58.    /* Recover interrupted files */
  59.  
  60.    refresh = 0
  61.  
  62.    do i = 1 to running.0
  63.  
  64.       fname = filespec( "name", running.i )
  65.  
  66.       if AwLockIs( fname ) == 0 then do
  67.  
  68.          call LogPut cfg.logfile, msg.msg_recover": "fname
  69.          fname = WpsMove( dir.running"\"fname, dir.todo"\"fname )
  70.  
  71.          if  fname \= "" then do
  72.              todolen = todo.0 + 1
  73.              todo.todolen = fname
  74.              todo.0  = todolen
  75.              refresh = 1
  76.          end
  77.       end
  78.    end
  79.  
  80.    if refresh then do
  81.       rc = WpsRefresh( dir.todo    )
  82.       rc = WpsRefresh( dir.running )
  83.    end
  84.  
  85.    /* Scan DeskTop */
  86.  
  87.    if cfg.use_desktop then do
  88.  
  89.       refresh = 0
  90.       rc = SysFileTree( dir.desktop"\*" ,'desktop','FO' )
  91.  
  92.       do i = 1 to desktop.0
  93.  
  94.          fname = filespec( "name", desktop.i )
  95.  
  96.          if IsURLFile( desktop.i ) then do
  97.             fname = WpsMove( dir.desktop"\"fname, dir.todo"\"fname )
  98.  
  99.             if fname \= "" then do
  100.                todolen = todo.0 + 1
  101.                todo.todolen = fname
  102.                todo.0  = todolen
  103.                refresh = 1
  104.             end
  105.          end
  106.       end
  107.    end
  108.  
  109.    if refresh then do
  110.       rc = WpsRefresh( dir.desktop )
  111.       rc = WpsRefresh( dir.todo    )
  112.    end
  113.  
  114.    /* Check PPP connection */
  115.  
  116.    if cfg.check_connection & todo.0 > 0 then do
  117.  
  118.       pppState = CheckPPP()
  119.       if pppOKey \= pppState then do
  120.          pppOkey  = pppState
  121.  
  122.          if pppOkey then
  123.             call LogPut cfg.logfile, msg.msg_pppokey
  124.          else
  125.             call LogPut cfg.logfile, msg.msg_pppbad
  126.       end
  127.  
  128.       if pppOKey == 0 then do
  129.          call SysSleep cfg.scan_interval
  130.          iterate
  131.       end
  132.    end
  133.  
  134.    /* Shedule */
  135.  
  136.    do i = 1 to todo.0
  137.  
  138.       rc = SysFileTree( dir.running"\*", 'running', 'FO' )
  139.  
  140.       if running.0 >= cfg.maximum_downloads then
  141.          leave
  142.  
  143.       if \IsURLFile(todo.i) then do
  144.          call logPut cfg.errorlog, msg.msg_invurl": "todo.i
  145.          call WpsMove todo.i, dir.failed"\"filespec( "name", todo.i )
  146.          rc = WpsRefresh( dir.failed )
  147.          rc = WpsRefresh( dir.todo   )
  148.          iterate
  149.       end
  150.  
  151.       if cfg.messages then
  152.          call beep 1000, 50
  153.  
  154.       url = GetURLFromFile(todo.i)
  155.       run = WpsMove( todo.i, dir.running"\"filespec( "name", todo.i ))
  156.  
  157.       if run \= "" then do
  158.  
  159.          call LogPut cfg.logfile, msg.msg_getstart": "url
  160.  
  161.          if length(url) > 49 then
  162.             url = "..." || right( url, 49 )
  163.  
  164.          rc = WpsRefresh( dir.running )
  165.          rc = WpsRefresh( dir.todo    )
  166.  
  167.         'set REXX_TRACE='
  168.         'start "Auto WGet: 'url'" /min /c /pgm "'directory()'\awgets.cmd" "'run'"'
  169.  
  170.          do wait_start = 1 to 30
  171.  
  172.             if AwLockIs( filespec( "name", run )) |,
  173.                stream( run, "c", "query exists" ) == "" then
  174.                leave
  175.  
  176.             call SysSleep(1)
  177.          end
  178.        end
  179.    end
  180.    call SysSleep cfg.scan_interval
  181. end
  182.  
  183. /*------------------------------------------------------------------
  184.  * National Messages Procedures
  185.  *------------------------------------------------------------------*/
  186. MsgRead: procedure expose msg.
  187.  
  188.   parse arg msgfile
  189.   parse source OS2 what msgpath
  190.  
  191.   msgfile = filespec( "disk", msgpath ) ||,
  192.             filespec( "path", msgpath ) || msgfile
  193.  
  194.   country = strip( SysIni( "BOTH", "PM_National", "iCountry" ),, '0'x )
  195.   country = right( country, 3, "0" )
  196.  
  197.   if stream( msgfile"."country, "c", "query exists" ) == "" then
  198.      country = "001"
  199.  
  200.   msgfile = msgfile"."country
  201.   rc = stream( msgfile, "C", "OPEN READ" )
  202.  
  203.   if rc \= "READY:" then do
  204.      say "ERROR: Error open message file: "msgfile
  205.      exit
  206.   end
  207.  
  208.   do while lines(msgfile) > 0
  209.      line = linein(msgfile)
  210.  
  211.      if line \= "" then do
  212.         parse value line with id "=" msg
  213.  
  214.         id  = translate(strip(id))
  215.         msg = strip(msg)
  216.  
  217.         i = pos( "\n", msg )
  218.         do while i > 0
  219.            msg = substr( msg, 1, i-1 ) || '0A0D'x || substr( msg, i+2 )
  220.            i = pos( "\n", msg )
  221.         end
  222.  
  223.         msg.id = msg
  224.      end
  225.   end
  226.  
  227.   rc = stream( msgfile, "C", "CLOSE" )
  228.  
  229. return
  230.  
  231. /*------------------------------------------------------------------
  232.  * Get Yes or No
  233.  *------------------------------------------------------------------*/
  234. MsgYesNo: procedure expose msg.
  235.  
  236.    parse arg prompt
  237.    ok = 0
  238.  
  239.    do until ok
  240.       call charout, prompt"? "
  241.       pull reply
  242.       reply = left(reply,1)
  243.  
  244.       ok = (reply == "Y") |,
  245.            (reply == "N") |,
  246.            (pos( reply, msg.msg_yes ) > 0 ) |,
  247.            (pos( reply, msg.msg_no  ) > 0 )
  248.  
  249.       if \ok then do
  250.          say msg.msg_badyn
  251.       end
  252.    end
  253.  
  254. return (reply = "Y") | (pos( reply, msg.msg_yes ) > 0 )
  255.  
  256. /*------------------------------------------------------------------
  257.  * Get Configuration
  258.  *------------------------------------------------------------------*/
  259. CfgRead: procedure expose cfg. msg.
  260.  
  261.   parse arg CfgFile
  262.  
  263.   cfg.download          = "."
  264.   cfg.maximum_downloads = 3
  265.   cfg.messages          = 1
  266.   cfg.message_done      = ""
  267.   cfg.message_error     = ""
  268.   cfg.wget_parameters   = "-c -t 0 -w 30"
  269.   cfg.scan_interval     = 60
  270.   cfg.check_connection  = 0
  271.   cfg.logfile           = ""
  272.   cfg.errorlog          = ""
  273.   cfg.use_desktop       = 0
  274.   cfg.keep_failed       = 1
  275.   cfg.keep_done         = 0
  276.  
  277.   rc = stream( CfgFile, "C", "OPEN READ" )
  278.  
  279.   do while lines(CfgFile) > 0
  280.      parse value linein(CfgFile) with command "=" argument
  281.  
  282.      command = translate(strip(command))
  283.      argument = strip(argument)
  284.  
  285.      select
  286.         when command == "DOWNLOAD" then
  287.              cfg.download = argument
  288.         when command == "MAXIMUM_DOWNLOADS_SIMULTANEOUSLY" then
  289.              cfg.maximum_downloads = argument
  290.         when command == "MESSAGES" then
  291.              cfg.messages = (argument == "1")
  292.         when command == "WGET_PARAMETERS" then
  293.              cfg.wget_parameters = argument
  294.         when command == "SCAN_INTERVAL" then
  295.              cfg.scan_interval = argument
  296.         when command == "LOG_FILE" then
  297.              cfg.logfile = argument
  298.         when command == "ERROR_LOG" then
  299.              cfg.errorlog = argument
  300.         when command == "MESSAGE_DONE" then
  301.              cfg.message_done = argument
  302.         when command == "MESSAGE_ERROR" then
  303.              cfg.message_error = argument
  304.         when command == "CHECK_CONNECTION" then
  305.              cfg.check_connection = (argument == "1")
  306.         when command == "USE_DESKTOP" then
  307.              cfg.use_desktop = (argument == "1")
  308.         when command == "KEEP_FAILED_URL" then
  309.              cfg.keep_failed = (argument == "1")
  310.         when command == "KEEP_DONE_URL" then
  311.              cfg.keep_done = (argument == "1")
  312.         otherwise
  313.      end
  314.   end
  315.  
  316.   if right( cfg.download, 1 ) = "\" then
  317.      cfg.download = left( cfg.download, length(cfg.download) - 1 )
  318.  
  319.   setlocal
  320.   cfg.download = directory( cfg.download )
  321.   endlocal
  322.  
  323.   if cfg.download == "" then do
  324.      call LogPut cfg.errorlog, msg.msg_invdown
  325.      cfg.download = "."
  326.   end
  327.  
  328.   rc = stream( CfgFile, "C", "CLOSE" )
  329.   cfg.file_date = stream( CfgFile, "C", "QUERY DATETIME" )
  330.  
  331. return
  332.  
  333. /*------------------------------------------------------------------
  334.  * Show Configuration
  335.  *------------------------------------------------------------------*/
  336. CfgShow: procedure expose cfg. msg.
  337.  
  338.   say "*** "msg.msg_downdir  || ": "cfg.download
  339.   say "*** "msg.msg_maxcon   || ": "cfg.maximum_downloads
  340.   say "*** "msg.msg_show     || ": "cfg.messages
  341.  
  342.   if cfg.message_done \= "" then
  343.      say "*** "msg.msg_done  || ": "cfg.message_done
  344.  
  345.   if cfg.message_error \= "" then
  346.      say "*** "msg.msg_error || ": "cfg.message_error
  347.  
  348.   say "*** "msg.msg_interval || ": "cfg.scan_interval
  349.   say "*** "msg.msg_chckconn || ": "cfg.check_connection
  350.   say "*** "msg.msg_parm     || ": "cfg.wget_parameters
  351.   say "*** "msg.msg_usedesk  || ": "cfg.use_desktop
  352.   say "*** "msg.msg_keepfail || ": "cfg.keep_failed
  353.   say "*** "msg.msg_keepdone || ": "cfg.keep_done
  354.  
  355. return
  356.  
  357. /*------------------------------------------------------------------
  358.  * Save Configuration
  359.  *------------------------------------------------------------------*/
  360. CfgSave: procedure expose cfg. msg.
  361.  
  362.   parse arg CfgFile
  363.  
  364.   if stream( CfgFile, "C", "QUERY EXISTS" ) \= "" then
  365.      "del "CfgFile
  366.  
  367.   rc = stream( CfgFile, "C", "OPEN WRITE" )
  368.  
  369.   rc = lineout( CfgFile, "download = "cfg.download )
  370.   rc = lineout( CfgFile, "maximum_downloads_simultaneously = "cfg.maximum_downloads )
  371.   rc = lineout( CfgFile, "messages = "cfg.messages )
  372.  
  373.   if cfg.message_done  \= "" then
  374.      rc = lineout( CfgFile, "message_done  = "cfg.message_done  )
  375.  
  376.   if cfg.message_error \= "" then
  377.      rc = lineout( CfgFile, "message_error = "cfg.message_error )
  378.  
  379.   rc = lineout( CfgFile, "log_file = "cfg.logfile )
  380.   rc = lineout( CfgFile, "error_log = "cfg.errorlog )
  381.   rc = lineout( CfgFile, "wget_parameters = "cfg.wget_parameters )
  382.   rc = lineout( CfgFile, "scan_interval = "cfg.scan_interval )
  383.   rc = lineout( CfgFile, "check_connection = "cfg.check_connection )
  384.   rc = lineout( CfgFile, "use_desktop = "cfg.use_desktop )
  385.   rc = lineout( CfgFile, "keep_failed_url = "cfg.keep_failed )
  386.   rc = lineout( CfgFile, "keep_done_url = "cfg.keep_done )
  387.  
  388.   rc = stream( CfgFile, "C", "CLOSE" )
  389.  
  390. return
  391.  
  392. /*------------------------------------------------------------------
  393.  * Check Configuration
  394.  *------------------------------------------------------------------*/
  395. CfgCheck: procedure expose cfg. msg.
  396.  
  397.   parse arg CfgFile
  398.  
  399.   if cfg.file_date \= stream( CfgFile, "C", "QUERY DATETIME" ) then do
  400.  
  401.      call LogPut cfg.logfile, msg.msg_readcfg
  402.      call CfgRead CfgFile
  403.      say
  404.      call CfgShow
  405.      say
  406.   end
  407.  
  408. return
  409.  
  410. /*------------------------------------------------------------------
  411.  * Check PPP connection
  412.  *------------------------------------------------------------------*/
  413. CheckPPP: procedure
  414.  
  415.   NewQueue = RxQueue( 'create' )
  416.   OldQueue = RxQueue( 'set', NewQueue )
  417.  
  418.   Connect  = 0
  419.  
  420.   '@netstat -a | RXQUEUE' NewQueue
  421.  
  422.   do while queued() > 0
  423.      parse pull . IP1 . Interface . . . IP2
  424.      if Interface >= '10' then do
  425.         Connect = 1
  426.      end
  427.   end
  428.  
  429.   call RxQueue 'delete', NewQueue
  430.   call RxQueue 'set'   , OldQueue
  431.  
  432. return Connect
  433.  
  434. /*------------------------------------------------------------------
  435.  * Get URL from file
  436.  *------------------------------------------------------------------*/
  437. GetURLFromFile: procedure
  438.  
  439.   parse arg filename
  440.  
  441.   rc  = stream(filename, "c", "open read" )
  442.   url = linein(filename)
  443.   rc  = stream(filename, "c", "close" )
  444.  
  445.   do while pos( "%", url ) > 0
  446.  
  447.      i = pos( "%", url )
  448.  
  449.      url = substr( url, 1, i-1      ) ||,
  450.            x2c(substr( url, i+1, 2 )) ||,
  451.            substr( url, i+3         )
  452.   end
  453.  
  454. return url
  455.  
  456. /*------------------------------------------------------------------
  457.  * Get filename from URL
  458.  *------------------------------------------------------------------*/
  459. GetFileFromURL: procedure
  460.  
  461.   parse arg url
  462.  
  463.   pos_parm = pos( "?", url )
  464.   if pos_parm \= 0 then
  465.     url = substr( url, 1, pos_parm-1 )
  466.  
  467.   pos_name = lastpos( "/", url )
  468.   if pos_name \= 0 then
  469.     url = substr( url, pos_name+1 )
  470.  
  471. return url
  472.  
  473. /*------------------------------------------------------------------
  474.  * Check valid URL file
  475.  *------------------------------------------------------------------*/
  476. IsURLFile: procedure
  477.  
  478.   parse arg filename
  479.  
  480.   rc  = stream(filename, "c", "open read" )
  481.   url = translate( strip( charin(filename,1,16)))
  482.   rc  = stream(filename, "c", "close" )
  483.  
  484. return substr( url, 1, 7 ) == "HTTP://" |,
  485.        substr( url, 1, 6 ) == "FTP://"
  486.  
  487. /*------------------------------------------------------------------
  488.  * Write Log Record
  489.  *------------------------------------------------------------------*/
  490. LogPut: procedure expose cfg.
  491.  
  492.   parse arg logfile, message
  493.  
  494.   say "■■■ "message
  495.   if logfile \= "" then do
  496.      call lineout logfile, date()" "time()" -> "message
  497.      rc = stream( logfile, "c", "close" )
  498.   end
  499.  
  500. return
  501.  
  502. /*------------------------------------------------------------------
  503.  * Append Log Record
  504.  *------------------------------------------------------------------*/
  505. LogAdd: procedure expose cfg.
  506.  
  507.   parse arg logfile, message
  508.  
  509.   if logfile \= "" then do
  510.      call lineout logfile, date()" "time()" -> "message
  511.      rc = stream( logfile, "c", "close" )
  512.   end
  513.  
  514. return
  515.  
  516.  
  517. /*------------------------------------------------------------------
  518.  * Move file
  519.  *------------------------------------------------------------------*/
  520. WpsMove: procedure expose msg. cfg.
  521.  
  522.   parse arg file_from, file_to
  523.  
  524.   rc = SysGetEA( file_from, ".LONGNAME", file_long )
  525.  
  526.   if file_long == "" then
  527.      rc = WpsPutEA( file_from, ".LONGNAME", filespec( "name", file_from ))
  528.  
  529.   if stream( file_to, "c", "query exists" ) \= "" then do
  530.      file_to = SysTempFileName( file_to"!???" )
  531.   end
  532.  
  533.   'copy "'file_from'" "'file_to'" 1> nul 2> nul'
  534.  
  535.   if rc == 0 then
  536.      'del "'file_from'" 1> nul 2> nul'
  537.   else do
  538.      call LogPut cfg.error_log msg.msg_notmove": "file_from" -> "file_to
  539.      file_to = ""
  540.   end
  541.  
  542. return file_to
  543.  
  544. /*------------------------------------------------------------------
  545.  * Write a named ascii extended attribute to a file
  546.  *------------------------------------------------------------------*/
  547. WpsPutEA: procedure
  548.  
  549.   parse arg file, name, value
  550.  
  551.   value = 'FDFF'x ||,
  552.            substr( d2c(length(value)), 1, 2, '00'x ) || value
  553.  
  554. return SysPutEA( file, name, value )
  555.  
  556. /*------------------------------------------------------------------
  557.  * Refresh Folder
  558.  *------------------------------------------------------------------*/
  559. WpsRefresh: procedure
  560.  
  561.   parse arg path
  562.  
  563. return SysSetObjectData( path, "MENUITEMSELECTED=503" )
  564.  
  565. /*------------------------------------------------------------------
  566.  * Get Directorys
  567.  *------------------------------------------------------------------*/
  568. DirRead: procedure expose dir. msg. cfg.
  569.  
  570.   dir.todo    = AwGetObjectPath( "<AWG_TODO>"   )
  571.   dir.running = AwGetObjectPath( "<AWG_RUN>"    )
  572.   dir.done    = AwGetObjectPath( "<AWG_DONE>"   )
  573.   dir.failed  = AwGetObjectPath( "<AWG_ERROR>"  )
  574.   dir.desktop = AwGetObjectPath( "<WP_DESKTOP>" )
  575.  
  576.   /* check it */
  577.  
  578.   if dir.todo    == "" then do
  579.      call LogPut cfg.errorlog, msg.msg_notfound': "ToDo"'
  580.      exit 1
  581.   end
  582.   if dir.running == "" then do
  583.      call LogPut cfg.errorlog, msg.msg_notfound': "Running"'
  584.      exit 1
  585.   end
  586.   if dir.done    == "" then do
  587.      call LogPut cfg.errorlog, msg.msg_notfound': "Done"'
  588.      exit 1
  589.   end
  590.   if dir.failed  == "" then do
  591.      call LogPut cfg.errorlog, msg.msg_notfound': "Failed"'
  592.      exit 1
  593.   end
  594.   if dir.desktop == "" then do
  595.      call LogPut cfg.errorlog, msg.msg_notfound': "Desktop"'
  596.      exit 1
  597.   end
  598.  
  599. return
  600.  
  601. /*------------------------------------------------------------------
  602.  * Show Directorys
  603.  *------------------------------------------------------------------*/
  604. DirShow: procedure expose dir.
  605.  
  606.   say "*** ToDo:    "dir.todo
  607.   say "*** Running: "dir.running
  608.   say "*** Done:    "dir.done
  609.   say "*** Failed:  "dir.failed
  610.   say "*** Desktop: "dir.desktop
  611. return
  612.