home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / gostools.zip / www_mirr.cmd < prev   
OS/2 REXX Batch file  |  1995-06-24  |  13KB  |  396 lines

  1. /* Creates automatically HTML-mirror pages of hobbes archives.      */
  2. /* ================================================================ */
  3. /* Written by Michael Warmuth for OS/2 Forum Austria.               */
  4. /* (E-mail: Michael.Warmuth@wu-wien.ac.at).                         */
  5. /* For using and copiing see the file copyright.doc.                */
  6. /* ================================================================ */
  7. /* Version: 0.96 (06/04/95)                                         */
  8.  
  9. parse arg conf_file
  10.  
  11. /* Load REXX utility functions */
  12. if RxFuncQuery('SysLoadFuncs') then do
  13.    CALL RxFuncAdd 'SysLoadFuncs', 'RexxUtil', 'SysLoadFuncs'
  14.    CALL SysLoadFuncs
  15. end  /* Do */
  16.  
  17. /* Load REXX TCP-SOCKET functions */
  18. if RxFuncQuery("SockLoadFuncs") then do
  19.    call RxFuncAdd "SockLoadFuncs","rxSock","SockLoadFuncs"
  20.    call SockLoadFuncs
  21. end  /* Do */
  22.  
  23.  
  24. /* Read configuration file into stem */
  25. do i=1 to 9999999 until \lines(conf_file)
  26.    do until next_line\='' | \lines(conf_file)
  27.       parse value linein(conf_file) with next_line '#' .
  28.       next_line = strip(next_line)
  29.    end /* do */
  30.    conf_line.i = next_line
  31. end /* do */
  32. conf_line.0 = i
  33.  
  34. /* Process settings */
  35. do i=1 to conf_line.0 while translate(conf_line.i)\='[SETTINGS]'
  36. end /* do */
  37. do j=i+1 to conf_line.0 while translate(conf_line.j)\='[URLS]'
  38.    interpret conf_line.j
  39. end /* do */
  40. j = j+1
  41.  
  42. /* Do with all urls */
  43. do i=j to conf_line.0
  44.    /* Set url specific vars */
  45.    g.ftp_url = word(conf_line.i,1)
  46.    g.html_dir = word(conf_line.i,2)
  47.    g.url_head = left(g.ftp_url,lastpos('/',g.ftp_url))
  48.    g.list_file = substr(g.ftp_url,lastpos('/',g.ftp_url)+1)
  49.    g.tmp_file = g.html_dir||g.list_file
  50.  
  51.    g.log_file = g.html_dir||g.ftp_log
  52.    parse var g.url_head 'ftp://' ftp_host '/' ftp_dir
  53.    ftp_dir = '/'left(ftp_dir,length(ftp_dir)-1)
  54.  
  55.    /* Try as often as specified if ftp fails */
  56.    do g.retry_numbe until ftp_ok\=0
  57.  
  58.       /* look if log file exists */
  59.       if stream(g.log_file,'c','query exists')='' then do
  60.          g.old_date = 'Jan 00'
  61.          g.old_time = '00:00'
  62.          call stream g.log_file, 'c', 'open'
  63.          call lineout g.log_file, '      Local             Index File     Ping                   Info or'
  64.          call lineout g.log_file, '  Date       Time      Date    Time    msecs   ftp secs       Errors    '
  65.          call lineout g.log_file, '========================================================================'
  66.       end  /* Do */
  67.       else do
  68.          call stream g.log_file, 'c', 'open'      /* Open stream */
  69.          log_size = stream(g.log_file,'c','query size') /* Get size */
  70.          if log_siez>200 then do                /* Long stream: get last 200 chars */
  71.             call stream g.log_file, 'c', 'seek <200'
  72.             log_tail = charin(g.log_file,,198)
  73.          end  /* Do */
  74.          else do                                /* Short stream: get whole stream */
  75.             log_tail = charin(g.log_file,1,log_size-2)
  76.          end  /* Do */
  77.    
  78.          /* Get old index file date and time */
  79.          parse value substr(log_tail,lastpos('0d0a'x,log_tail)+2) with,
  80.                . '|' . '| ' g.old_date ' | ' g.old_time ' |' .
  81.    
  82.          /* Go to the end of the stream */
  83.          call stream g.log_file, 'c', 'seek <0'
  84.          
  85.       end  /* Do */
  86.       g.sys_date = date('u')
  87.       g.sys_time = time()
  88.       g.ping_time = left('',5)
  89.       g.ftp_time = left('',9)
  90.       g.ftp_info = left('UNDEFINED ERR',13)
  91.    
  92.       /* Do the whole ftp stuff */
  93.       ftp_ok = do_ftp()
  94.    
  95.       /* Logoff */
  96.       call FtpLogoff
  97.    
  98.       /* Ftp was ok, so ... */
  99.       if ftp_ok=1 then do
  100.          /* Build the html pages */
  101.          call makeurl
  102.    
  103.          g.old_date = g.new_date
  104.          g.old_time = g.new_time
  105.          g.ftp_info = left('update done',13)
  106.       end  /* Do */
  107.    
  108.       /* Write log file */
  109.       call writelog
  110.    end /* do */
  111.  
  112. end /* do */
  113.  
  114. exit
  115.  
  116.  
  117. /* ---------------------------------------------------------------- */
  118. /* Ping the remote host and do the rest                             */
  119. do_ftp:
  120.  
  121. /* Ping host */
  122. retc = FtpPing(ftp_host,'64')
  123. if pos(left(retc,1),'-'xrange('A','Z'))\=0 | retc=-1 then do
  124.    g.ftp_info = left(retc,13)
  125.    return 0
  126. end  /* Do */
  127. /* Correct bug of FtpPing */
  128. if right(retc,4)='0000' then do
  129.    g.ping_time = right(left(retc,length(retc)-2),5)
  130. end  /* Do */
  131. else do
  132.    g.ping_time = right(retc,5)
  133. end  /* Do */
  134.  
  135. return do_setuser()
  136.  
  137.  
  138. /* ---------------------------------------------------------------- */
  139. /* Logon to the remote host and do the rest                         */
  140. do_setuser:
  141.  
  142. /* Host login */
  143. if \FtpSetUser(ftp_host,'anonymous',g.webma_email,) then do
  144.    g.ftp_info = left('LOGIN ERROR',13)
  145.    return 0
  146. end  /* Do */
  147.  
  148. return do_chdir()
  149.  
  150.  
  151. /* ---------------------------------------------------------------- */
  152. /* Change remote directory and do the rest                          */
  153. do_chdir:
  154.  
  155. /* Change directory */
  156. retc = FtpChDir(ftp_dir)
  157. if retc\=0 then do
  158.    g.ftp_info = left(retc,13)
  159.    return 0
  160. end  /* Do */
  161.  
  162. return do_dir()
  163.  
  164.  
  165. /* ---------------------------------------------------------------- */
  166. /* Check remote index file and do the rest                          */
  167. do_dir:
  168.  
  169. /* Get directory listing of index file */
  170. /* '-r--r--r--   1 os2-adm  archive   256809 Apr 11 19:14 00indexd.txt' */
  171. retc = FtpDir(g.list_file,'rem_files.')
  172. if retc\=0 then do
  173.    g.ftp_info = left(retc,13)
  174.    return 0
  175. end  /* Do */
  176. if rem_files.0\=1 then do
  177.    g.ftp_info = left('FILENO ERROR',13)
  178.    return 0
  179. end  /* Do */
  180. g.new_date = subword(rem_files.1,6,2)
  181. g.new_time = word(rem_files.1,8)
  182.  
  183. /* Check if an update is neccessary */
  184. if g.old_date=g.new_date & g.old_time=g.new_time then do
  185.    g.ftp_info = left('not needed',13)
  186.    return 2
  187. end  /* Do */
  188.  
  189. return do_get()
  190.  
  191.  
  192. do_get:
  193.  
  194. /* Get remote file */
  195. call time 'r'                             /* Reset timer */
  196. retc = FtpGet(g.tmp_file,g.list_file,'Ascii')
  197. g.ftp_time = time('e')                    /* Get elapsed time */
  198. parse var g.ftp_time g.ftp_time '.' .
  199. g.ftp_time = right(g.ftp_time,9)
  200. if retc\=0 then do
  201.    g.ftp_info = left(retc,13)
  202.    return 0
  203. end  /* Do */
  204.  
  205. return 1
  206.  
  207.  
  208.  
  209. /* ================================================================ */
  210. /* Global procedures                                                */
  211. /* ================================================================ */
  212.  
  213.  
  214.  
  215. /* ---------------------------------------------------------------- */
  216. /* Write to the log file                                            */
  217. writelog: procedure expose g.
  218.  
  219. call lineout g.log_file, g.sys_date '|' g.sys_time '|' g.old_date '|' g.old_time '|',
  220.       g.ping_time '|' g.ftp_time '|' g.ftp_info
  221. call stream g.log_file, 'c', 'close'
  222.  
  223. return 0
  224.  
  225.  
  226. /* ---------------------------------------------------------------- */
  227. /* Create the all html files for the current url                    */
  228. makeurl: procedure expose g.
  229.  
  230. /* Delete entries of session queue */
  231. do queued()
  232.    pull .
  233. end /* do */
  234.  
  235. /* Delete archive attribute of all existing files (except log and index file) */
  236. call SysFileTree g.html_dir||'*', 'dummy.', 'F', , '-****'
  237. call SysFileTree g.log_file, 'dummy.', 'F', , '+****'
  238. call SysFileTree g.tmp_file, 'dummy.', 'F', , '+****'
  239.  
  240. /* Initial file name */
  241. g.act_dir = ''
  242. g.html_file = g.html_dir || g.ind_file       /* file for save */
  243.  
  244. /* Do with all lines of the file */
  245. do while lines(g.tmp_file)
  246.    /* Make one directory */
  247.    do until right(file,1)=':' | \lines(g.tmp_file)
  248.       do until line\='' | \lines(g.tmp_file)
  249.          line = strip(linein(g.tmp_file))    /* Get next line */
  250.       end /* do */
  251.       /* Get filename */
  252.       file = word(line,1)
  253.  
  254.       /* Not the start of a new directory */
  255.       if right(file,1)\=':' then do
  256.          /* Break line */
  257.          size = word(line,2)
  258.          if size='-' | size='=' then size = ''
  259.          if words(line)<6 then da_ti = ''
  260.          else da_ti = subword(line,3,3)
  261.          if words(line)<7 then info = ''
  262.          else info = subword(line,6)
  263.  
  264.          /* Build URL */
  265.          if right(file,1)='/' then do        /* Directory */
  266.             url = translate(g.act_dir,'_','/') || left(file,length(file)-1)||'.html'
  267.          end  /* Do */
  268.          else do                             /* File */
  269.             url = g.url_head||g.act_dir||file
  270.          end  /* Do */
  271.  
  272.          /* Queue all parts */
  273.          queue url
  274.          queue file
  275.          queue size
  276.          queue da_ti
  277.          queue info
  278.       end  /* Do */
  279.       else do
  280.          if left(file,2)='./' then file = substr(file,3) /* Correction for hobbes */
  281.          call makefile g.html_file g.url_head||g.act_dir /* Make file for this directory */
  282.          g.act_dir = left(file,length(file)-1)  /* remote dir */
  283.          g.html_file = g.html_dir || translate(g.act_dir,'_','/') || '.html'  /* file for save */
  284.          g.act_dir = g.act_dir'/'
  285.       end  /* Do */
  286.    end /* do */
  287. end /* do */
  288.  
  289. if queued()\=0 then do
  290.    call makefile g.html_file g.url_head||g.act_dir /* Make file for this directory */
  291. end  /* Do */
  292.  
  293. /* Delete all files without archive attribute - they are obsolete */
  294. call SysFileTree g.html_dir||'*', 'files.', 'FO', '-****'
  295. do i=1 to files.0
  296.    call SysFileDelete files.i
  297. end /* do */
  298.  
  299. return 0
  300.  
  301.  
  302.  
  303. /* ---------------------------------------------------------------- */
  304. /* Create one directory file                                        */
  305. makefile: procedure expose g.
  306.  
  307. parse arg file sel .
  308.  
  309.  
  310. /* Delete old directory index */
  311. call SysFileDelete file
  312.  
  313. /* Create header of directory index */
  314. call lineout file, '<HEAD><TITLE>Index of 'sel'</TITLE></HEAD>'
  315. call lineout file, '<BODY>'
  316. call lineout file, '<CENTER><H5>Index of 'sel'</H5></CENTER>'
  317. call lineout file, '<PRE>'
  318. call lineout file, '<IMG SRC="'g.icons_direc||'blk.gif" ALT="     " ALIGN=MIDDLE',
  319.       'WIDTH='g.direc_width' HEIGHT='g.direc_heigh'>',
  320.       center('Name',g.direc_name) center('Date',g.direc_date),
  321.       center('Size',g.direc_size) left('Info',g.direc_info)
  322. call lineout file, '<HR>'
  323.  
  324. select
  325.    when filespec('name',file)=g.ind_file then url = g.home_url
  326.    when pos('_',filespec('name',file))=0 then url = './'
  327.    otherwise url = left(filespec('name',file),lastpos('_',filespec('name',file))-1)'.html'
  328. end  /* select */
  329. act_file = '..'
  330. size = ''
  331. file_fill = copies(' ',g.direc_name-length(strip(left(act_file,g.direc_name))))
  332. call lineout file, '<IMG SRC="'g.icons_direc||icontype('.!!!')||,
  333.       '" ALT="'alttype('.!!!')'" ALIGN=MIDDLE WIDTH='g.direc_width,
  334.       'HEIGHT='g.direc_heigh'> <A HREF="'url'">'||,
  335.       strip(right(act_file,g.direc_name))'</A>'file_fill
  336.  
  337. /* Create entries for files */
  338. do while queued()>=5
  339.    /* Get values from queue */
  340.    parse pull url
  341.    parse pull act_file
  342.    parse pull size
  343.    parse pull da_ti
  344.    parse pull info
  345.  
  346.    /* Only files without leading dot (.) */
  347.       if right(act_file,1)='/' then do       /* This is a directory */
  348.          icon = icontype('.!!!')             /* Get icon to use */
  349.          type = alttype('.!!!')              /* Get ALT to use */
  350.       end  /* Do */
  351.       else do
  352.          icon = icontype(act_file)           /* Get icon to use */
  353.          type = alttype(act_file)            /* Get ALT to use */
  354.       end  /* Do */
  355.       file_fill = copies(' ',g.direc_name-length(strip(right(act_file,g.direc_name))))
  356.       call lineout file, '<IMG SRC="'g.icons_direc||icon'" ALT="'type,
  357.             '"ALIGN=MIDDLE WIDTH='g.direc_width' HEIGHT='g.direc_heigh'>',
  358.             '<A NAME="'act_file'" HREF="'url'">'||,
  359.             strip(right(act_file,g.direc_name))'</A>'file_fill,
  360.             left(da_ti,g.direc_date),
  361.             right(size,g.direc_size) left(info,g.direc_info)
  362. end /* do */
  363.  
  364. /* Create tail */
  365. call lineout file, '</PRE>'
  366. call lineout file, '</BODY>'
  367. call stream file, 'c', 'close'
  368.  
  369. return 0
  370.  
  371.  
  372.  
  373. /* ----------------------------------------------------------------------- */
  374. /* ICONTYPE: Return the name of the icon for directory indexing.           */
  375. /* ----------------------------------------------------------------------- */
  376. icontype: procedure expose g.
  377.  
  378. /* First get the extension; this assumes filenames have at least one '.' */
  379. ???=translate(substr(arg(1), lastpos('.',arg(1))+1))
  380. if symbol('g.direc_.???')='LIT' then ???=''
  381.  
  382. return word(g.direc_.???,1)
  383.  
  384.  
  385. /* ----------------------------------------------------------------------- */
  386. /* ALTTYPE: Return the alternate for icon for directory indexing.          */
  387. /* ----------------------------------------------------------------------- */
  388. alttype: procedure expose g.
  389.  
  390. /* First get the extension; this assumes filenames have at least one '.' */
  391. ???=translate(substr(arg(1), lastpos('.',arg(1))+1))
  392. if symbol('g.direc_.???')='LIT' then ???=''
  393.  
  394. return word(g.direc_.???,2)
  395.  
  396.