-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
acc.py
278 lines (256 loc) · 9.79 KB
/
acc.py
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
import mysql.connector
import hashlib
import json
import getpass
import toml
import os
import dotenv
import random
dotenv.load_dotenv()
mydb = mysql.connector.connect(
host = os.getenv("host"),
user = os.getenv("user"),
password = os.getenv("password"),
port = os.getenv("port"),
)
cursor = mydb.cursor(buffered=True)
try:
dic = json.load(open('stats.json', 'r'))
except:
dic = {}
try:
sett = toml.load(open(os.getcwd()+'/config.toml'))
except:
sett = {}
try:
acct = toml.load(open(os.getcwd()+'/.account.toml'))
except:
acct = {}
def gensalt(uid, pwd):
alpha = "qweryuiopasdfghjklzxcvbnm"
count = 0
for x in uid:
try:
count += alpha.index(x) + 1
except:
count += ord(x)
for x in pwd:
try:
count += alpha.index(x) + 1
except:
count += ord(x)
return hashlib.sha3_512(hex(count)[::-1].encode()).hexdigest()
def sync(user, dic, edic, con, econ):
print("syncing database...")
edic = json.dumps(edic).replace(r"\\", "").strip(r"'\\").strip('"').replace("'", "")
econ = toml.loads(econ.replace(r"\\", "").strip(r"'\\").strip('"'))
blanks = [{}, "null", None, ""]
if (edic in blanks and econ in blanks) and (dic in blanks and con in blanks):
print("you have no data to import or export. happy habit tracking!")
elif (edic not in blanks or econ not in blanks) and (dic in blanks and con in blanks):
prompt = input("you have no data saved locally but you have data on the cloud. would you like to import from the cloud? (Y/n) ")
if prompt == "n":
print("import aborted.")
else:
dic = edic
con = econ
elif (edic in blanks and econ in [{},"null", None]) and (dic not in blanks or con not in blanks):
prompt = input("you have no data on the cloud but you have data saved locally. would you like to export to the cloud? (Y/n) ")
if prompt == "n":
print("export aborted.")
else:
edic = dic
econ = con
else:
if not(edic == dic and econ == con):
while 1:
prompt = input("you have data on the cloud and saved locally. please pick one data file to keep. (cloud/local) ")
if prompt == "cloud":
dic = edic
con = econ
break
elif prompt == "local":
edic = dic
econ = con
break
else:
print("invalid. try again")
else:
print("your data on the cloud and your data saved locally are identical. happy habit tracking!")
edic = json.dumps(edic).replace(r"\\", "")
econ = toml.dumps(econ).replace(r"\\", "")
dic = json.loads(json.dumps(dic).replace(r"\\", ""))
con = toml.loads(toml.dumps(con).replace(r"\\", ""))
json.dump(dic, open('stats.json', 'w'), default=str)
toml.dump(con, open('config.toml', 'w'))
if edic == None:
edic = ""
if econ == None:
econ = ""
cursor.execute(f"""
UPDATE
accounts
SET
data = '{edic}',
config = '{econ}'
WHERE
username = '{user}';
""")
mydb.commit()
print("data synced successfully.")
def logout(acct, dic, sett):
if acct["loggedin"]:
prompt = input("are you sure you would like to log out? (y/N) ")
if prompt == "y":
print("logging out...")
acct["loggedin"] = False
acct["username"] = ""
toml.dump(acct, open('.account.toml', 'w'))
json.dump(dic, open('stats.json', 'w'), default=str)
toml.dump(sett, open('config.toml', 'w'))
else:
print("logout abort.")
else:
print("you are already logged out.")
def login(acct, dic, sett):
print("logging in...")
if acct["loggedin"]:
prompt = input("you are already logged in. would you like to log out? (y/N) ")
if prompt == "y":
logout(acct, dic, sett)
login(acct, dic, sett)
else:
print("logout abort.")
else:
username = input("enter username: ")
acct["username"] = username
password = getpass.getpass("enter password: ")
maindb = os.getenv("database")
cursor.execute(f"use {maindb};")
cursor.execute("select * from accounts;")
result = cursor.fetchall()
fuser = False
for row in result:
if row[0] == username:
account = row
fuser = True
if hashlib.sha3_512((password + row[1]).encode()).hexdigest() == row[2]:
acct["loggedin"] = True
print("login successful!")
else:
print("invalid credentials. please try again.")
exit()
if not fuser:
prompt = input("this username is not registered. would you like to create a new account? (Y/n) ")
if prompt == "n":
print("signup aborted.")
exit()
else:
print("creating account...")
salt = gensalt(username, password)
hpwd = hashlib.sha3_512((password + salt).encode()).hexdigest()
id = hashlib.sha3_512(str(random.random()).encode()).hexdigest()
cursor.execute(f"""
insert into
`accounts` (`username`, `salt`, `password`, `data`, `config`, `id`, `discord`)
values
('{username}', '{salt}', '{hpwd}', '""', '""', '{id}', '');
""")
mydb.commit()
acct["loggedin"] = True
try:
edic = account[3]
except:
edic = ""
try:
esett = account[4]
except:
esett = ""
sync(username, dic, edic, sett, esett)
mydb.commit()
toml.dump(acct, open('.account.toml', 'w'))
if dic in ["none", None]:
dic = {}
return dic
def editacct(acct, det, new):
if acct["loggedin"]:
maindb = os.getenv("database")
cursor.execute(f"use {maindb};")
cursor.execute("select * from accounts;")
result = cursor.fetchall()
fuser = False
for row in result:
if row[0] == acct["username"]:
fuser = True
account = row
break
if fuser:
if det in ["username", "password"]:
prompt = input(f"are you sure you would like to change your {det}? (y/N) ")
if prompt == "y":
verify = getpass.getpass(f"enter your current {det} to verify: ")
if det == "username":
if verify == acct["username"]:
print("username verified.")
acct["username"] = new
cursor.execute(f"""
UPDATE
accounts
SET
username = '{new}'
WHERE
id = '{account[5]}';
""")
toml.dump(acct, open('.account.toml', 'w'))
mydb.commit()
print(f"your username is now {new}.")
else:
print("invalid username.")
elif det == "password":
if hashlib.sha3_512((verify + account[1]).encode()).hexdigest() == account[2]:
print("password verified.")
cursor.execute(f"""
UPDATE
accounts
SET
salt = '{gensalt(account[0], new)}',
password = '{hashlib.sha3_512((new + gensalt(account[0], new)).encode()).hexdigest()}'
WHERE
username = '{acct["username"]}';
""")
mydb.commit()
print(f"your password has been changed.")
else:
print("invalid password.")
else:
print("invalid credential.")
else:
print("please login first.")
def removeacct(acct, dic, sett):
if acct["loggedin"]:
prompt = input("are you sure you would like to remove your account? (y/N) ")
if prompt == "y":
maindb = os.getenv("database")
cursor.execute(f"use {maindb};")
cursor.execute("select * from accounts;")
uname = acct["username"]
cursor.execute(f"delete from accounts where username = '{uname}'")
mydb.commit()
print("account successfully deleted.")
prompt = ("would you like to keep your data? (Y/n)")
if prompt != "n":
print("logging out...")
acct["loggedin"] = False
acct["username"] = ""
toml.dump(acct, open('.account.toml', 'w'))
json.dump(dic, open('stats.json', 'w'), default=str)
toml.dump(sett, open('config.toml', 'w'))
else:
print("deleting local data...")
acct["loggedin"] = False
acct["username"] = ""
toml.dump({}, open('.account.toml', 'w'))
json.dump({}, open('stats.json', 'w'), default=str)
toml.dump({}, open('config.toml', 'w'))
else:
print("you are not logged in.")