diff --git a/horsygui.py b/horsygui.py index db6e469..82de014 100644 --- a/horsygui.py +++ b/horsygui.py @@ -165,6 +165,11 @@ def change_password_gui(): change(ui.oldpass_box.toPlainText(), ui.newpass_box.toPlainText()) +def change_email_gui(): + from modules.change_email import change + change(ui.email_box.toPlainText()) + + # Run functions on startup if __name__ == "__main__": # Checking version @@ -206,6 +211,7 @@ if __name__ == "__main__": ui.regmessage_button.clicked.connect(lambda: webbrowser.open(f"{horsy_vars.protocol}{horsy_vars.server_url}" f"/registration")) ui.changepass_button.clicked.connect(change_password_gui) + ui.changeemail_button.clicked.connect(change_email_gui) # Handle GUI exiting to exit whole program sys.exit(app.exec_()) diff --git a/modules/auth.py b/modules/auth.py index 95d71b5..31a1797 100644 --- a/modules/auth.py +++ b/modules/auth.py @@ -24,6 +24,7 @@ def get_auth(is_gui=False, login_ui=None, Ui_LoginWindow=None): print('[OK] Auth created') return config['auth'] else: + login_ui.setupUi(Ui_LoginWindow) Ui_LoginWindow.show() login_ui.login_button.clicked.connect(lambda: get_gui_auth(login_ui=login_ui, Ui_LoginWindow=Ui_LoginWindow)) diff --git a/modules/change_email.py b/modules/change_email.py index e69de29..3386f4c 100644 --- a/modules/change_email.py +++ b/modules/change_email.py @@ -0,0 +1,57 @@ +import requests +import modules.gui as gui +from modules.auth import del_auth, get_auth +import modules.vars as horsy_vars +import json +import threading +from PyQt5 import QtWidgets + + +def change(email): + UiLoginWindow = QtWidgets.QMainWindow() + login_ui = gui.Ui_LoginWindow() + login_ui.setupUi(UiLoginWindow) + + if email == "": + gui.popup('Error', 'Please enter new email address') + return + + auth = get_auth(True, login_ui, QtWidgets.QMainWindow()) + + try: + def change_in_new_thread(): + try: + r = requests.put(horsy_vars.protocol + horsy_vars.server_url + '/users', + json={'auth': auth, 'email': email}) + try: + r = r.json() + except: + if r.text == '': + gui.cpopup('Success', 'Success, your email has been changed') + with open(horsy_vars.horsypath + 'config.cfg') as f: + config = json.load(f) + config['auth'] = {'email': email, 'password': config['auth']['password']} + with open(horsy_vars.horsypath + 'config.cfg', 'w') as f: + json.dump(config, f) + try: + if r['message'] == 'Unauthorized': + gui.cpopup('Error', 'Invalid credentials \nDeleting auth from config') + del_auth() + + elif r['message'] == 'Internal server error': + gui.cpopup('Error', 'Internal server error') + return 'Internal server error' + + else: + print('Unknown error, please try again') + print('Server response:') + print(r.text) + return 'Unknown error, please try again, \n Server response: \n' + str(r.text) + except: + pass + except: + gui.cpopup('Error', 'Unexpected error.') + threading.Thread(target=change_in_new_thread).start() + gui.popup('Started', 'Check your email for confirmation') + except: + gui.popup('Error', 'Unexpected error.') diff --git a/modules/change_password.py b/modules/change_password.py index a103bb1..22fdb0b 100644 --- a/modules/change_password.py +++ b/modules/change_password.py @@ -1,6 +1,5 @@ import requests import modules.gui as gui -import threading from modules.auth import del_auth, get_auth import modules.vars as horsy_vars import json @@ -8,27 +7,38 @@ from PyQt5 import QtWidgets def change(oldpass, newpass): - ui = gui.Ui_MainWindow() - ui.setupUi(QtWidgets.QMainWindow()) + UiLoginWindow = QtWidgets.QMainWindow() + login_ui = gui.Ui_LoginWindow() + login_ui.setupUi(UiLoginWindow) if oldpass == "" or newpass == "": gui.popup('Error', 'Please enter both old and new passwords.') return - with open(horsy_vars.horsypath + 'config.cfg') as f: - config = json.load(f) + config = get_auth(True, gui.Ui_LoginWindow(), QtWidgets.QMainWindow()) try: - if oldpass != config['auth']['password']: + if oldpass != config['password']: gui.popup('Error', 'Old password does not match with password in config.cfg') return - except KeyError: + except: gui.popup('Error', 'You don\'t have a password set in config.cfg') return try: r = requests.put(horsy_vars.protocol + horsy_vars.server_url + '/users', - json={'auth': config['auth'], 'password': newpass}).text + json={'auth': get_auth(True, login_ui, QtWidgets.QMainWindow()), + 'password': newpass}) + try: + r = r.json() + except: + if r.text == '': + gui.popup('Success', 'Success, your password has been changed') + with open(horsy_vars.horsypath + 'config.cfg') as f: + config = json.load(f) + config['auth'] = {'email': config['auth']['email'], 'password': newpass} + with open(horsy_vars.horsypath + 'config.cfg', 'w') as f: + json.dump(config, f) try: if r['message'] == 'Unauthorized': gui.popup('Error', 'Invalid credentials \nDeleting auth from config') @@ -38,14 +48,8 @@ def change(oldpass, newpass): gui.popup('Error', 'Internal server error') return 'Internal server error' - elif r == '': - gui.popup('Success', 'Success, your password has been changed') - config['auth'] = {'email': config['auth']['email'], 'password': newpass} - with open(horsy_vars.horsypath + 'config.cfg', 'w') as f: - json.dump(config, f) - else: - print('[red]Unknown error, please try again[/red]') + print('Unknown error, please try again') print('Server response:') print(r) return 'Unknown error, please try again, \n Server response: \n' + str(r) diff --git a/modules/gui.py b/modules/gui.py index 522e7f1..06ded8f 100644 --- a/modules/gui.py +++ b/modules/gui.py @@ -1,6 +1,6 @@ from PyQt5 import QtCore, QtGui, QtWidgets -import modules.images -import requests +import modules.images # import images from binaries +import ctypes class Ui_MainWindow(object): @@ -801,9 +801,25 @@ class Ui_DownloadWindow(object): "


")) self.logs_box.setPlaceholderText(_translate("MainWindow", "Logs")) + def popup(title, text): QtWidgets.QMessageBox.information(None, title, text) + +def cpopup(title, text, style=0): + """ + Styles: + 0 : OK + 1 : OK | Cancel + 2 : Abort | Retry | Ignore + 3 : Yes | No | Cancel + 4 : Yes | No + 5 : Retry | Cancel + 6 : Cancel | Try Again | Continue + """ + return ctypes.windll.user32.MessageBoxW(0, text, title, style) + + if __name__ == "__main__": import sys