Advertisement
MRtecno98

SRMP Client V.1.0.0

May 20th, 2017
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.05 KB | None | 0 0
  1. #SRMP(Send/Receive Mail Protocol) è un "Protocollo" creato da me(MRtecno98)
  2. #per inviare "email" da un dispositivo all'altro
  3. #Per settare il programma prima di tutto dovete avere una struttura di cartelle creata dallo script di setup server
  4. #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
  5. #e in SERVER_PSW la password dell'ftp
  6. #Poi inserite in SERVER_DIR il nome della cartella dei dati del server, se non avete cambiato alcuna impostazione nel setup
  7. #si chiamerà SRMP
  8.  
  9. from ftplib import FTP
  10. import techcore , requests , json , datetime , os , time , sys
  11.  
  12. SERVER = "*URLSERVER*"
  13. SERVER_USR = "*USERFTP*"
  14. SERVER_PSW = "*PSWFTP*"
  15. SERVER_DIR = "SRMP"
  16.  
  17. ACCOUNTS_FILE = "/".join(["http:/" , SERVER , SERVER_DIR , "accounts.json"])
  18.  
  19. def login() :
  20.     global usr
  21.     global psw
  22.     #global accrequest
  23.     global accounts
  24.     accrequest = requests.get(ACCOUNTS_FILE)
  25.     accounts = json.loads(accrequest.text)
  26.     while True :
  27.         usr = techcore.semplifyText(input("login as: "))
  28.         if usr in accounts :
  29.             psw = input("password for " + usr + ": ")
  30.             if accounts[usr] == psw :
  31.                 print("Access Garanted.")
  32.                 break
  33.             else :
  34.                 print("Errated Password")
  35.                 continue
  36.         else :
  37.             print("User Inesistent")
  38.             continue
  39.     print()
  40.  
  41. def updateMails() :
  42.     print("Downloading new mails...")
  43.     ftp.cwd("/" + SERVER_DIR + "/" + usr)
  44.     if os.path.basename(os.getcwd()) != "Mails" :
  45.         os.chdir(r".\Mails")
  46.     mails = ftp.nlst()
  47.     time.sleep(1)
  48.     if len(mails) != 0 :
  49.         print("Finded " + str(len(mails)) + " mails, downloading...")
  50.         for mail in mails :
  51.             print("Downloading " + mail + "...")
  52.             mailPath = "/".join(["/" + SERVER_DIR + "" , usr , mail])
  53.             file = open("." + os.path.sep + mail , "wb")
  54.             techcore.ftp.downloadFile(ftp , file , mailPath , 1024)
  55.             file.close()
  56.             techcore.ftp.deleteFile(ftp , mailPath)
  57.             time.sleep(0.3)
  58.         print("Mail download finished!\n")
  59.     else :
  60.         print("Finded 0 Mails, all mails have been updated!\n")
  61.     time.sleep(1.3)
  62.  
  63. if not os.path.isdir("." + os.path.sep + Mails") :
  64.    os.mkdir("." + os.path.sep + Mails")
  65.  
  66. try :
  67.  
  68.     ftp = FTP(SERVER)
  69.     ftp.login(user=SERVER_USR , passwd=SERVER_PSW)
  70.    
  71.     login()
  72.     updateMails()
  73.  
  74.     print("SRMP Client".center(37,"="))
  75.     print("send: Send an E-mail")
  76.     print("update: Download Received E-mails")
  77.     print("show: View Memorized E-mails")
  78.     print("clear: Clear Client\Server E-mails")
  79.     print("logout: Logout")
  80.     print("=====================================")
  81.  
  82.     while True :
  83.         opp = input("srmp> ")
  84.  
  85.         if opp == "send" :
  86.             while True :
  87.                 dest = input("Select destinatary: ")
  88.                 if dest in accounts :
  89.                     obj = input("Insert Object: ")
  90.                     att = input("Attachments?(y/n) ") == "y"
  91.                     if att :
  92.                         print("Insert Attachment Path: ")
  93.                         attc = input()
  94.                     else :
  95.                         attc = None
  96.                     print("Write Text of the Message, use |back| to remove a line and |end| to finish: \n")
  97.                     text = techcore.editText("","|end|","|back|")
  98.                     print("Building Message...")
  99.                
  100.                     msg = {"dest" : dest,
  101.                            "object" : obj,
  102.                            "mitt" : usr,
  103.                            "text" : text,
  104.                            "info" : {
  105.                                       "datasend" : str(datetime.datetime.now()),
  106.                                       "attachment" : attc
  107.                                     }
  108.                           }
  109.            
  110.                     json_msg = json.dumps(msg)
  111.                     file = open("." + os.path.sep + obj + ".json" , "w")
  112.                     file.write(json_msg)
  113.                     file.close()
  114.                     del file
  115.                     file = open("." + os.path.sep + obj + ".json" , "rb")
  116.                     techcore.ftp.uploadFile(ftp , file , "/" + SERVER_DIR + "/" + dest)
  117.                     file.close()
  118.                     del file
  119.                     os.unlink("." + os.path.sep + obj + ".json")
  120.                     print("Message Sended!\n")
  121.                     break
  122.                 else :
  123.                     print("User Inesistent")
  124.                     continue
  125.         elif opp == "update" :
  126.             updateMails()
  127.         elif opp == "show" :
  128.             mails = os.listdir(".")
  129.             i = 0
  130.             print("Finded " + str(len(mails)) + " mails:")
  131.             for mail in mails :
  132.                 print("Mail #" + str(i) + ": " + mail)
  133.                 i+=1
  134.         elif opp == "clear" :
  135.             ftp.cwd("/" + SERVER_DIR + "/" + usr)
  136.             mails = ftp.nlst()
  137.             if len(mails) > 0 :
  138.                 print("Deleting server mails...")
  139.                 for mail in mails :
  140.                     if mail.endswhith(".json") :
  141.                         print("Deleting " + mail + "...")
  142.                         techcore.ftp.deleteFile(ftp , "/".join(["/" + SERVER_DIR + "",mail]))
  143.             else :
  144.                 print("Server mails not found.")
  145.             mails = os.listdir(".")
  146.             if len(mails) > 0 :
  147.                 print("\nDeleting client Mails...")
  148.                 for mail in mails :
  149.                     if mail.endswith(".json") :
  150.                         print("Deleting " + mail + "...")
  151.                         os.unlink(mail)
  152.             else :
  153.                 print("Client mails not found.")
  154.         elif opp == "logout" :
  155.             print("Logout succesfully\n")
  156.             time.sleep(2)
  157.             sys.exit()
  158.         elif opp.replace("\n","").replace(" ","") == "" :
  159.             continue
  160.         else :
  161.             print("Inesistend Command")
  162.  
  163. finally :
  164.     ftp.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement