def main(): """ Register a new User or Authenticates the already registered User to send message """ try: if auth.check_existed(CURRENT_USER): password = getpass(f"User {CURRENT_USER} found\nEnter password: "******"Welcome {CURRENT_USER} to tmessage!\nPlease register...") displayed_name = input("Enter your name used for display: ") password = getpass("Enter password: "******"Re-enter password: "******"Passwords do not match, please try again...") password = getpass("Enter password: "******"Re-enter password: "******"User Authorized") user_name = payload["user_name"] displayed_name = payload["displayed_name"] MQTT_CLIENT.on_message = on_message MQTT_CLIENT.connect(BROKER_ENDPOINT, BROKER_PORT) MQTT_CLIENT.subscribe(MQTT_TOPIC) MQTT_CLIENT.loop_start() while True: raw_msg = MESSAGE_INPUT.get_input() formatted_msg = get_formatted_message(raw_msg) pub_msg = f"[{user_name}] {displayed_name}: {formatted_msg}" if raw_msg != "": MQTT_CLIENT.publish(MQTT_TOPIC, pub_msg) if IS_STORE: store_messages(CURRENT_USER, formatted_msg) else: print(Back.WHITE + Fore.RED + "Can't send empty message", end="\n") except KeyboardInterrupt: # pylint: disable=pointless-statement MQTT_CLIENT.disconnect() Style.RESET_ALL deinit() print("\nGoodbye!") except ConnectionRefusedError: # pylint: disable=pointless-statement Style.RESET_ALL deinit() print("\nCan't connect please check your network connection") except Exception as err: # pylint: disable=broad-except # pylint: disable=pointless-statement Style.RESET_ALL deinit() print(f"\n{err}")
def on_message(client, userdata, message): # pylint: disable=unused-argument """ callback functions to Process any Messages""" current_msg = message.payload.decode("utf-8") # to get the username between [] user = current_msg.partition('[')[-1].rpartition(']')[0] if user != CURRENT_USER: print(Back.GREEN + Fore.BLACK + current_msg + Back.RESET + Fore.RESET + "") _, _, message = current_msg.partition('] ') if IS_STORE: store_messages(user, message)
def on_message(client, userdata, message): # pylint: disable=unused-argument """ callback functions to Process any Messages""" current_msg = message.payload.decode("utf-8") # to get the username between [] user = current_msg.partition("[")[-1].rpartition("]")[0] user_name_separator_index = current_msg.find(":") + 1 user_details = chalk.bgGreen(current_msg[:user_name_separator_index]) msg = current_msg[user_name_separator_index + 1:] if user != CURRENT_USER: print(user_details, msg) _, _, message = current_msg.partition("] ") if IS_STORE: store_messages(user, message)