Skip to content
This repository has been archived by the owner on Oct 14, 2023. It is now read-only.

Commit

Permalink
final commit for email command
Browse files Browse the repository at this point in the history
  • Loading branch information
Philotheephilix committed Oct 3, 2023
1 parent 2b4843b commit b169bf0
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 12 deletions.
Empty file added crypt_cred.key
Empty file.
53 changes: 41 additions & 12 deletions multi_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,34 @@
BASE_URL = "http://api.openweathermap.org/data/2.5/weather?"
path=os.getcwd()
#Code for handling credential encryption and decryption
def encryptcred(msgid):
key = Fernet.generate_key()
with open('./credentials/'+msgid+'/crypt_cred.key', 'wb') as filekey:
filekey.write(key)
print("new key gen")
with open('./credentials/'+msgid+'/crypt_cred.key', 'rb') as filekey:
key = filekey.read()
fernet = Fernet(key)
with open("./credentials/"+msgid+'/credential.env', 'rb') as file:
original = file.read()
encrypted = fernet.encrypt(original)
with open("./credentials/"+msgid+'/credential.env', 'wb+') as encrypted_file:
encrypted_file.write(encrypted)
enc=encrypted_file.read()
print(enc)
def decryptcred(msgid):
try:
with open('./credentials/'+msgid+'/crypt_cred.key', 'rb') as filekey:
key = filekey.read()
fernet = Fernet(key)
with open('./credentials/'+msgid+'/credential.env', 'rb') as file:
original = file.read()
decrypted = fernet.decrypt(original)
with open('./credentials/'+msgid+'/credential.env', 'wb+') as decrypted_file:
decrypted_file.write(decrypted)
print("decrypted")
except:
print("Invalid Key or Already decrypted")
def decrypt():
a=1
if a==1:
Expand Down Expand Up @@ -171,10 +199,16 @@ def init_email(message):
global emailinit_active
emailinit_active=1
bot.reply_to(message,"Enter your email address")
try:
os.mkdir(path+"\\credentials\\"+msgid)
except:
print("Using previous directory")
shutil.copy("./credential.env","./credentials/"+msgid+"/credential.env")
shutil.copy("./crypt_cred.key","./credentials/"+msgid+"/crypt_cred.key")
@bot.message_handler(commands=["check_email"])
def check_email(message):
msgid = str(message.chat.id)
decryptcred(msgid)
credential_path = os.path.join(tempdir, "credentials", msgid, "credential.env")

# Read the email and password directly from the credential file
Expand All @@ -187,12 +221,7 @@ def check_email(message):
email = line.strip("email=").strip()
elif line.startswith("pass="):
password = line.strip("pass=").strip()

if email and password:
# Use the loaded email and password
print(f"Loaded email: {email}")
print(f"Loaded password: {password}")

mail = imaplib.IMAP4_SSL("imap.gmail.com")
mail.login(email, password)
mail.select("inbox")
Expand All @@ -202,7 +231,7 @@ def check_email(message):
mail.logout()
else:
bot.reply_to(message, "Email and/or password not found in credentials.")

encryptcred(msgid)
def kelvin_to_celsius_fahrenheit(kelvin):
celsius = kelvin - 273.15
fahrenheit = celsius * (9/5) + 32
Expand Down Expand Up @@ -316,10 +345,8 @@ def city(message):
elif emailinit_active ==1 or emailinit_active==2:
emailid=str(message.text)
msgid=str(message.chat.id)
try:
os.mkdir(path+"\\credentials\\"+msgid)
except:
print("Using previous directory")
decryptcred(msgid)


newline="\n"
newline=newline.encode()
Expand All @@ -338,8 +365,10 @@ def city(message):
bot.send_message(message.chat.id,"Enter your email password")
else:
emailinit_active=0
return 0

Credential=open("./credentials/"+msgid+"/credential.env","r")
cred=Credential.read()
bot.send_message(message.chat.id,"verify ur credentials\n"+cred)
encryptcred(msgid)


else:
Expand Down

0 comments on commit b169bf0

Please sign in to comment.