include sort.e include file.e include datetime.e include get.e include eunet.e constant REMOTE_OPEN = "10.201.32.105" constant REMOTE_USER = "ftpuser" constant REMOTE_PW = "ftppw" constant REMOTE_ROOT = "/home" constant LOCAL_ROOT = "C:\\" -------------------------------------------------------------------------------- --=== FTP stub routines ===-- function get_passive(atom socketT) object status, data, temp atom socketR, RXPort, cpos, spos, epos status = eunet_send(socketT,"PASV"&13&10,0) if eunet_delay(10) then end if data = eunet_recv(socketT,0) -- 227 response if length(data)=0 then -- Wait a little longer if eunet_delay(100) then end if data = eunet_recv(socketT,0) -- 227 response end if RXPort = 0 cpos = find(',',data) if cpos > 0 then data = data[cpos+1..length(data)] for ctr = 1 to 3 do cpos = find(',',data) if cpos > 0 then data = data[cpos+1..length(data)] end if end for spos = 1 epos = find(',',data) if epos > 0 then temp = value(data[spos..epos-1]) if temp[1] = GET_SUCCESS then RXPort = temp[2] * 256 end if spos = epos + 1 epos = find(')',data) if epos > 0 then temp = value(data[spos..epos-1]) if temp[1] = GET_SUCCESS then RXPort += temp[2] end if end if end if end if socketR = eunet_new_socket(AF_INET,SOCK_STREAM,0) status = eunet_connect(socketR,REMOTE_OPEN&":"&sprintf("%d",RXPort)) if status < 0 then data = eunet_close_socket(socketR) return status else return socketR end if end function -------------------------------------------------------------------------------- function stream_to_lines(sequence stream) atom cpos, yy, hh, mm, offset sequence lines, fields, temp, MONTHS, now now = date() MONTHS = {"JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC"} lines = {} -- Make lines cpos = match({13,10},stream) while cpos > 0 do lines = append(lines,stream[1..cpos-1]) if length(stream)>cpos+1 then stream = stream[cpos+2..length(stream)] else stream = "" end if cpos = match({13,10},stream) end while if length(stream) > 0 then lines = append(lines,stream) end if if length(lines) < 4 then return {} end if -- Make fields for ctr = 1 to length(lines) do fields = {"",0,"","",0,"",0,"",""} offset = 0 if length(lines[ctr]) >= 56 then --drwxrwxrwx 15 root root 4096 Jun 4 08:17 Lectra fields[1] = lines[ctr][1..10] fields[2] = lines[ctr][11..14] fields[3] = lines[ctr][16..24] fields[4] = lines[ctr][25..33] fields[5] = lines[ctr][34..41] if lines[ctr][42]!=32 then -- If the size is too wide, the rest of -- the fields are getting pushed over. cpos = find(' ',lines[ctr][42..length(lines[ctr])]) if cpos > 0 then offset = cpos - 1 fields[5] = fields[5] & lines[ctr][42..41+offset] end if end if fields[6] = lines[ctr][43+offset..45+offset] fields[7] = lines[ctr][47+offset..48+offset] fields[8] = lines[ctr][50+offset..54+offset] fields[9] = lines[ctr][56+offset..length(lines[ctr])] temp = value(fields[2]) if temp[1] = GET_SUCCESS then fields[2] = temp[2] else fields[2] = 0 end if temp = value(fields[5]) if temp[1] = GET_SUCCESS then fields[5] = temp[2] else fields[5] = 0 end if temp = value(fields[7]) if temp[1] = GET_SUCCESS then fields[7] = temp[2] else fields[7] = 0 end if cpos = find(upper(fields[6]),MONTHS) if cpos > 0 then fields[6] = cpos else fields[6] = 0 end if if find(':',fields[8]) then hh = 0 mm = 0 temp = value(fields[8][1..2]) if temp[1] = GET_SUCCESS then hh = temp[2] end if temp = value(fields[8][4..5]) if temp[1] = GET_SUCCESS then mm = temp[2] end if yy = now[1] + 1900 else hh = 0 mm = 0 temp = value(fields[8]) if temp[1] = GET_SUCCESS then yy = temp[2] else yy = 0 end if end if -- lines[ctr] = fields[1..5] & {{yy,fields[6],fields[7],hh,mm,0}} & -- {fields[9]} lines[ctr] = {fields[1],fields[9],fields[5],{yy,fields[6],fields[7],hh,mm,0}} if find('d',lines[ctr][1]) then lines[ctr][1] = 0 else lines[ctr][1] = 1 end if end if end for return lines end function -------------------------------------------------------------------------------- function connect_to_remote_dir() atom socketT sequence inetaddr object addrinfo, status, data socketT = eunet_new_socket(AF_INET,SOCK_STREAM,0) addrinfo = eunet_getaddrinfo(REMOTE_OPEN,"ftp",0) if sequence(addrinfo) and length(addrinfo)>= 5 and length(addrinfo[5])>0 then inetaddr = addrinfo[5] else inetaddr = REMOTE_OPEN&":21" end if status = eunet_connect(socketT,inetaddr) if status < 0 then status = eunet_close_socket(socketT) return -1 end if -- Send username if eunet_delay(10) then end if data = eunet_recv(socketT,0) status = eunet_send(socketT,"USER "&REMOTE_USER&13&10,0) -- Send password if eunet_delay(10) then end if data = eunet_recv(socketT,0) status = eunet_send(socketT,"PASS "&REMOTE_PW&13&10,0) if eunet_delay(10) then end if data = eunet_recv(socketT,0) return socketT end function -------------------------------------------------------------------------------- function ch_remote_dir(atom socketT, sequence dirname) object status, data status = eunet_send(socketT,"CWD "&dirname&13&10,0) if eunet_delay(10) then end if data = eunet_recv(socketT,0) if sequence(data) and match("250 ",data)>0 then return 0 elsif atom(data) or length(data)=0 then -- Try one more time if eunet_delay(100) then end if data = eunet_recv(socketT,0) if sequence(data) and match("250 ",data)>0 then return 0 else return -1 end if else return -1 end if end function -------------------------------------------------------------------------------- function get_remote_dir(atom socketT) -- Each raw dlist entry follows the form: -- {seq attr, int ?, seq owner, seq group, atom dirsize, seq Month, int date, -- seq "HH:mm", seq name} -- Month, date, and time will be translated to {YYYY,MM,DD,HH,mm,ss}, and -- all numerical fields will be value'd before returning. atom socketR sequence dlist object status,data,temp socketR = get_passive(socketT) status = eunet_send(socketT,"LIST"&13&10,0) if eunet_delay(10) then end if status = eunet_recv(socketT,0) -- 150 response data = "" temp = eunet_recv(socketR,0) -- Dir LIST while sequence(temp) and length(temp) > 2 and atom(temp[2]) do data &= temp temp = eunet_recv(socketR,0) end while if sequence(temp) and length(temp) > 2 and atom (temp[2]) then data &= temp end if status = eunet_close_socket(socketR) if eunet_delay(10) then end if status = eunet_recv(socketT,0) -- 226 response if sequence(data) then dlist = stream_to_lines(data) return dlist else return -1 end if end function -------------------------------------------------------------------------------- -------------------------------------------------------------------------------- function create_remote_folder(atom socketT, sequence dirname) object status, data status = eunet_send(socketT,"MKD "&dirname&13&10,0) if eunet_delay(10) then end if data = eunet_recv(socketT,0) if sequence(data) and match("200 ",data)>0 then return 0 else return -1 end if end function -------------------------------------------------------------------------------- function copy_file(atom socketT, sequence filename) atom socketR, fp, bs object status,data,temp bs = eunet_get_blocksize() fp = open(filename,"rb") if fp < 3 then return -1 end if socketR = get_passive(socketT) if socketR < 0 then -- puts(1,"Could not open passive mode connection for copy\n") close(fp) return -1 end if status = eunet_send(socketT,"STOR "&filename&13&10,0) if eunet_delay(10) then end if data = "" temp = eunet_recv(socketT,0) if sequence(temp) and match("150 ",temp)>0 then data = get_bytes(fp,bs) while length(data) = bs do status = eunet_send(socketR,data,0) data = get_bytes(fp,bs) end while status = eunet_send(socketR,data,0) status = eunet_close_socket(socketR) if eunet_delay(10) then end if temp = eunet_recv(socketT,0) close(fp) files_copied += 1 -- Defined at the top of the file return 0 else status = eunet_close_socket(socketR) close(fp) return -1 end if end function --------------------------------------------------------------------------------