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

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