-
Notifications
You must be signed in to change notification settings - Fork 2
/
ftnftp.py
executable file
·120 lines (95 loc) · 3.64 KB
/
ftnftp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#!/usr/local/bin/python3 -bb
import ftnconfig
import ftplib
import ast
import io
import ftn.pkt
import os
import ftnimport
from ftn.ftn import ismail
from ftnpush import import_file
from ftnexport import file_export
class FTPFile():
def __init__(self, b):
self.b = b
def feed(self, d):
self.b.write(d)
print (len(d), "bytes added")
db = ftnconfig.connectdb()
for lid, addr, conn in db.prepare("select id, address, connection from links"):
if conn is not None:
ftp = conn.find("FTP")
if ftp is not None:
#print (addr, ftp.keys(), ftp.get("host"))
domain, address = ftnconfig.get_addr(db, addr)
link_pkt_format = ftnconfig.get_link_pkt_format(db, address)
password = ftnconfig.get_link_password(db, address)
print (ftp.get("username"), ftp.get("password"), ftp.get("host"), ftp.get("port", 0))
with ftplib.FTP() as ftpc:
ftpc.connect(host=ftp.get("host"), port=int(ftp.get("port", 0)))
ftpc.login(user=ftp.get("username"), passwd=ftp.get("password"))
#print (ftpc.getwelcome())
ftpc.set_pasv(ast.literal_eval(ftp.get("passive", "False")))
path = ftp.get("getfrom")
print (path)
if path:
ftpc.cwd(path)
print (ftpc.nlst())
for rfile in ftpc.nlst():
print ("Remote file", rfile, "size", ftpc.size(rfile))
if "/" in rfile:
raise Exception("Slash in remote file name")
if False: # ismail(rfile):
# in-memory import is disabled until rarfile will start working with file objects
print ("ismail")
# download
data = io.BytesIO()
recipient = FTPFile(data)
ftpc.retrbinary('RETR '+rfile, recipient.feed)
data.seek(0)
with ftnimport.session(db) as sess:
import_file(sess, rfile, data, address, False)
#ftpc.delete(rfile)
else:
savedir = ftnconfig.addrdir(ftnconfig.INBOUND, address)
print("save", savedir)
if not os.path.exists(savedir):
os.makedirs(savedir)
fd = os.open(os.path.join(savedir, "pwd-in", rfile), os.O_CREAT|os.O_WRONLY)
fo = os.fdopen(fd, "wb")
recipient = FTPFile(fo)
ftpc.retrbinary('RETR '+rfile, recipient.feed)
fo.close()
ftpc.delete(rfile)
ftpc.cwd("~")
path = ftp.get("putto")
if path:
ftpc.cwd(path)
for outbfile, committer in file_export(db, address, password, ["netmail", "echomail"]):
if outbfile:
print("outbound file "+outbfile.filename)
existing = ftpc.nlst()
while outbfile.filename in existing:
outbfile.filename = "_" + outbfile.filename
# ff=open("/tmp/test.zip", "wb")
# while True:
# x=outbfile.data.read(1000)
# if not x:
# break
# ff.write(x)
# ff.close()
ftpc.storbinary("STOR "+outbfile.filename, outbfile.data)
committer.commit()
outdir = ftnconfig.addrdir(ftnconfig.OUTBOUND, address)
print("send", outdir)
if os.path.isdir(outdir):
for outbfile in os.listdir(outdir):
fullname = os.path.join(outdir, outbfile)
data=open(fullname, "rb")
print("outbound file "+fullname)
existing = ftpc.nlst()
while outbfile in existing:
outbfile = "_" + outbfile
ftpc.storbinary("STOR "+outbfile, data)
data.close()
os.unlink(fullname)