def import_file(sess, fname, fo, recv_from, bulk): if fo: raise Exception("unable to use file object with rarfile") if fo is None: fo = open(fname, "rb") doclose = True else: doclose = False try: if ismsg(fname): print("msg") import_msg(sess, ftn.msg.MSG(fo), recv_from, bulk) elif ispkt(fname): print("pkt") # pktformat = ..get from database.. import_pkt(sess, fo, recv_from, bulk) elif isbundle(fname): sample = fo.read(8192) fo.seek(-len(sample), io.SEEK_CUR) mime = magic.whatis(sample) print("bundle (%s)" % mime) if mime == "application/zip": print("zip file") z = zipfile.ZipFile(fo) for zf in z.namelist(): if not ispkt(zf): raise Exception("non-PKT file in bundle %s" % fname) for zf in z.namelist(): print(zf) zfo = z.open(zf) import_pkt(sess, zfo, recv_from, bulk) zfo.close() z.close() elif mime == "application/x-rar": print("rar file: reopening fname") z = rarfile.RarFile(fname) for zf in z.namelist(): if not ispkt(zf): raise Exception("non-PKT file in bundle %s" % fname) for zf in z.namelist(): print(zf) zfo = z.open(zf) import_pkt(sess, zfo, recv_from, bulk) zfo.close() z.close() else: raise Exception("dont know how to unpack bundle %s" % fname) else: raise Exception("file %s is not FIDO mail file") # elif istic(f): # pass #print("tic - ignore") # # #fo=file(f, "rb") # #import_file(fo, f, "tic", recv_from) # #fo.close() # #os.unlink(f) finally: if doclose: fo.close()
def import_file(sess, fname, fo, recv_from, bulk): if fo: raise Exception("unable to use file object with rarfile") if fo is None: fo = open(fname, "rb") doclose = True else: doclose = False try: if ismsg(fname): print("msg") import_msg(sess, ftn.msg.MSG(fo), recv_from, bulk) elif ispkt(fname): print("pkt") # pktformat = ..get from database.. import_pkt(sess, fo, recv_from, bulk) elif isbundle(fname): sample = fo.read(8192) fo.seek(-len(sample), io.SEEK_CUR) mime=magic.whatis(sample) print("bundle (%s)"%mime) if mime=="application/zip": print("zip file") z=zipfile.ZipFile(fo) for zf in z.namelist(): if not ispkt(zf): raise Exception("non-PKT file in bundle %s"%fname) for zf in z.namelist(): print(zf) zfo=z.open(zf) import_pkt(sess, zfo, recv_from, bulk) zfo.close() z.close() elif mime=="application/x-rar": print("rar file: reopening fname") z=rarfile.RarFile(fname) for zf in z.namelist(): if not ispkt(zf): raise Exception("non-PKT file in bundle %s"%fname) for zf in z.namelist(): print(zf) zfo = z.open(zf) import_pkt(sess, zfo, recv_from, bulk) zfo.close() z.close() else: raise Exception("dont know how to unpack bundle %s"%fname) else: raise Exception("file %s is not FIDO mail file") # elif istic(f): # pass #print("tic - ignore") # # #fo=file(f, "rb") # #import_file(fo, f, "tic", recv_from) # #fo.close() # #os.unlink(f) finally: if doclose: fo.close()
if __name__ == "__main__": db = connectdb() #destinations = set() print(time.asctime(), "start ftnpush") for pnode_dir in glob.glob(INBOUND + "/*"): node = ftn.addr.addr2str( map(int, pnode_dir[len(INBOUND) + 1:].split("."))) #print("source: "+node) for f in find_all(pnode_dir + "/pwd-in"): #skip non-mail if not ismsg(f) and not ispkt(f) and not isbundle(f): continue print("file:", f) try: with ftnimport.session(db) as sess: #fo = open(f, "rb") import_file(sess, f, None, node, False) #fo.close() #os.unlink(f) os.rename(f, f + "-" + str(time.time())) #destinations.update(sess.touched_addresses()) except Exception as e: print("error on file %s" % repr(f)) fx = open(f + ".status", "w")
if __name__ == "__main__": db=connectdb() #destinations = set() print(time.asctime(), "start ftnpush") for pnode_dir in glob.glob(INBOUND+"/*"): node = ftn.addr.addr2str(map(int, pnode_dir[len(INBOUND)+1:].split("."))) #print("source: "+node) for f in find_all(pnode_dir+"/pwd-in"): #skip non-mail if not ismsg(f) and not ispkt(f) and not isbundle(f): continue print("file:", f) try: with ftnimport.session(db) as sess: #fo = open(f, "rb") import_file(sess, f, None, node, False) #fo.close() #os.unlink(f) os.rename(f, f+"-"+str(time.time())) #destinations.update(sess.touched_addresses()) except Exception as e: print("error on file %s"%repr(f)) fx=open(f+".status", "w")