-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
149 lines (124 loc) · 6.16 KB
/
main.py
File metadata and controls
149 lines (124 loc) · 6.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# Application chat v4.0
# Import module
import pwinput
import atexit
from tabulate import tabulate
import os
from prompt_toolkit import prompt
from prompt_toolkit.completion import NestedCompleter
from prompt_toolkit.validation import Validator, ValidationError
# Setting the working directory to the current directory
os.chdir(os.path.dirname(os.path.abspath(__file__)))
# Import custom module
from libs.config import *
from libs.functions import *
from libs.config import *
from libs.colors import *
from libs.database import *
from libs.security import *
from libs.chat import *
debugger_log("info", "[MAIN] Current Working Directory " + os.getcwd()) # Debug
os.system('') #enable VT100 Escape Sequence for WINDOWS 10 Ver. 1607
colors.reset_color()
message_stream = getMessageStream()
# Message validator
class TextEmptyValidator(Validator):
def validate(self, document):
text = document.text
if text == "":
raise ValidationError(message='Message is empty!')
def main():
debugger_log("debug", "[FUNCTION] Run main function") # Debug
colors.print("Welcome to " + variable.get("config_appName"), colors.BRIGHT_GREEN)
# Ask for username
username = colors.input("Please enter your username: ", colors.MAGENTA, colors.BRIGHT_GREEN)
# Check if username is in database
if(username in database.getUsernames()):
debugger_log("info", "[FUNCTION] User " + username + " logged into system. Waiting for the passcode") # Debug
variable.set("username_logged", username)
# Ask for passcode to unlock program
passcode = pwinput.pwinput(colors.MAGENTA + "Please enter passcode to unlock program: " + colors.BRIGHT_GREEN)
colors.reset_color()
if(security.verifyPasscode(passcode, database.getPasscode())):
debugger_log("success", "[FUNCTION] User " + username + " logged into system") # Debug
colors.print("Welcome " + username, colors.BRIGHT_GREEN)
# Set online status to true
database.setOnlineStatus(username, True)
# Get users connected to chat and filter only if online
# Get all status of users and last seen of users. If user is online, add to list and add last seen to list
users = database.getUsernames()
users_online = database.getOnlineStatus()
users_last_seen = database.getLastSeen()
users_username_filtered = [] # Store only online users username
users_online_filtered = []
users_last_seen_filtered = []
for user in users:
# Get user i
i = users.index(user)
if(users_online[i]):
users_online_filtered.append(user)
users_last_seen_filtered.append(users_last_seen[i])
users_username_filtered.append(user)
# Create nested completer for message input
completer = NestedCompleter.from_nested_dict(chat_commands.initCommand(users_username_filtered))
print("Users connected:")
# Concatenate username and last seen
users_online = list(zip(users_username_filtered, users_last_seen_filtered))
print(tabulate(users_online, headers=['Users', 'Last Seen'], tablefmt='fancy_grid'))
print("Getting messages...\n")
debugger_log("debug", "[FUNCTION] Getting messages...") # Debug
def stream_handler(message):
debugger_log("debug", "[FUNCTION] Run stream_handler function")
try:
print(colors.CURSOR_UP_ONE + colors.ERASE_LINE)
for mess in message["data"].items():
print(security.decrypt(toBytes(mess[1])))
variable.set("message_stream_loading", True) # Messages are loaded
debugger_log("debug", "[FUNCTION] Messages loaded")
except:
print(colors.CURSOR_UP_ONE + colors.ERASE_LINE)
print(security.decrypt(toBytes(message["data"])))
global message_stream
message_stream = db.child("messages").stream(stream_handler)
setMessageStream(message_stream)
message = ''
function.wait_until(lambda: variable.get("message_stream_loading") == True, 20)
while message != 'END':
# Ask for message
message = prompt(chat.messageStyle(username), completer=completer, bottom_toolbar=chat.bottom_toolbar(username), style=chat.style, validator=TextEmptyValidator())
# Check if message is a command
if(chat_commands.checkCommand(message)):
# Remove "/" from message
message = message.replace("/", "")
# Execute command
chat_commands.executeCommand(message)
else:
if message != "":
# Add date and time to message
message = "[" + function.getCurrentDateTime() + "] " + username + ": " + message
# Set last seen to current datetime
database.setLastSeen(username, function.getCurrentDateTime())
# Push message to database
# Encrypt message and push to database
database.pushMessage(security.encrypt(message))
else:
print("Message is empty")
else:
colors.print("Passcode not valid", colors.BRIGHT_RED)
debugger_log("error", "[LOGIN] User " + username + " has falied login") # Debug
else:
debugger_log("error", "[DATABASE] User " + username + " not found in database") # Debug
colors.print("User not found!", colors.BRIGHT_RED)
if __name__ == "__main__":
try:
colors.reset_color()
main()
except KeyboardInterrupt:
colors.reset_color()
# Set online status to false and close the message stream
database.exitDB()
def exit_handler():
colors.reset_color()
# Set online status to false and close the message stream
database.exitDB()
atexit.register(exit_handler)