Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #SRMP(Send/Receive Mail Protocol) è un "Protocollo" creato da me(MRtecno98)
- #per inviare "email" da un dispositivo all'altro
- #Per settare il programma prima di tutto dovete avere una struttura di cartelle creata dallo script di setup server
- #Poi inserite in SERVER l'url del server in cui si trova l'albero di cartelle, in SERVER_USR l'usr dell'ftp del server
- #e in SERVER_PSW la password dell'ftp
- #Poi inserite in SERVER_DIR il nome della cartella dei dati del server, se non avete cambiato alcuna impostazione nel setup
- #si chiamerà SRMP
- from ftplib import FTP
- import techcore , requests , json , datetime , os , time , sys
- SERVER = "*URLSERVER*"
- SERVER_USR = "*USERFTP*"
- SERVER_PSW = "*PSWFTP*"
- SERVER_DIR = "SRMP"
- ACCOUNTS_FILE = "/".join(["http:/" , SERVER , SERVER_DIR , "accounts.json"])
- def login() :
- global usr
- global psw
- #global accrequest
- global accounts
- accrequest = requests.get(ACCOUNTS_FILE)
- accounts = json.loads(accrequest.text)
- while True :
- usr = techcore.semplifyText(input("login as: "))
- if usr in accounts :
- psw = input("password for " + usr + ": ")
- if accounts[usr] == psw :
- print("Access Garanted.")
- break
- else :
- print("Errated Password")
- continue
- else :
- print("User Inesistent")
- continue
- print()
- def updateMails() :
- print("Downloading new mails...")
- ftp.cwd("/" + SERVER_DIR + "/" + usr)
- if os.path.basename(os.getcwd()) != "Mails" :
- os.chdir(r".\Mails")
- mails = ftp.nlst()
- time.sleep(1)
- if len(mails) != 0 :
- print("Finded " + str(len(mails)) + " mails, downloading...")
- for mail in mails :
- print("Downloading " + mail + "...")
- mailPath = "/".join(["/" + SERVER_DIR + "" , usr , mail])
- file = open("." + os.path.sep + mail , "wb")
- techcore.ftp.downloadFile(ftp , file , mailPath , 1024)
- file.close()
- techcore.ftp.deleteFile(ftp , mailPath)
- time.sleep(0.3)
- print("Mail download finished!\n")
- else :
- print("Finded 0 Mails, all mails have been updated!\n")
- time.sleep(1.3)
- if not os.path.isdir("." + os.path.sep + Mails") :
- os.mkdir("." + os.path.sep + Mails")
- try :
- ftp = FTP(SERVER)
- ftp.login(user=SERVER_USR , passwd=SERVER_PSW)
- login()
- updateMails()
- print("SRMP Client".center(37,"="))
- print("send: Send an E-mail")
- print("update: Download Received E-mails")
- print("show: View Memorized E-mails")
- print("clear: Clear Client\Server E-mails")
- print("logout: Logout")
- print("=====================================")
- while True :
- opp = input("srmp> ")
- if opp == "send" :
- while True :
- dest = input("Select destinatary: ")
- if dest in accounts :
- obj = input("Insert Object: ")
- att = input("Attachments?(y/n) ") == "y"
- if att :
- print("Insert Attachment Path: ")
- attc = input()
- else :
- attc = None
- print("Write Text of the Message, use |back| to remove a line and |end| to finish: \n")
- text = techcore.editText("","|end|","|back|")
- print("Building Message...")
- msg = {"dest" : dest,
- "object" : obj,
- "mitt" : usr,
- "text" : text,
- "info" : {
- "datasend" : str(datetime.datetime.now()),
- "attachment" : attc
- }
- }
- json_msg = json.dumps(msg)
- file = open("." + os.path.sep + obj + ".json" , "w")
- file.write(json_msg)
- file.close()
- del file
- file = open("." + os.path.sep + obj + ".json" , "rb")
- techcore.ftp.uploadFile(ftp , file , "/" + SERVER_DIR + "/" + dest)
- file.close()
- del file
- os.unlink("." + os.path.sep + obj + ".json")
- print("Message Sended!\n")
- break
- else :
- print("User Inesistent")
- continue
- elif opp == "update" :
- updateMails()
- elif opp == "show" :
- mails = os.listdir(".")
- i = 0
- print("Finded " + str(len(mails)) + " mails:")
- for mail in mails :
- print("Mail #" + str(i) + ": " + mail)
- i+=1
- elif opp == "clear" :
- ftp.cwd("/" + SERVER_DIR + "/" + usr)
- mails = ftp.nlst()
- if len(mails) > 0 :
- print("Deleting server mails...")
- for mail in mails :
- if mail.endswhith(".json") :
- print("Deleting " + mail + "...")
- techcore.ftp.deleteFile(ftp , "/".join(["/" + SERVER_DIR + "",mail]))
- else :
- print("Server mails not found.")
- mails = os.listdir(".")
- if len(mails) > 0 :
- print("\nDeleting client Mails...")
- for mail in mails :
- if mail.endswith(".json") :
- print("Deleting " + mail + "...")
- os.unlink(mail)
- else :
- print("Client mails not found.")
- elif opp == "logout" :
- print("Logout succesfully\n")
- time.sleep(2)
- sys.exit()
- elif opp.replace("\n","").replace(" ","") == "" :
- continue
- else :
- print("Inesistend Command")
- finally :
- ftp.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement