-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathterminalChat.py
418 lines (324 loc) · 14.6 KB
/
terminalChat.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
from flask import Flask, render_template, request, redirect, url_for, session, jsonify
from pusher import Pusher
from dotenv import load_dotenv
from datetime import datetime
from db import db, Message, Company, User, ChatRoom
from flask import flash
import hashlib
import random
import string
import uuid
from werkzeug.security import check_password_hash
from werkzeug.security import generate_password_hash
from flask_login import LoginManager
from flask_login import login_required
from flask_login import current_user
from werkzeug.security import check_password_hash
from flask_login import LoginManager, login_required, current_user, login_user, logout_user
from flask import send_from_directory
from cryptography.fernet import Fernet
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
import base64
import os
import random
import string
from cryptography.fernet import Fernet, InvalidToken
app = Flask(__name__)
load_dotenv(dotenv_path='.env')
app.secret_key = 'some_secret_key' # Set a secret key for session management
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///chat.db' # Add your database URI
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
login_manager = LoginManager()
login_manager.init_app(app)
login_manager = LoginManager(app)
login_manager.login_view = 'login'
db.init_app(app) # Initialize the db with the Flask app
pusher = Pusher(
app_id='1701887',
key='d65c1eee46cdcd199bc6',
secret='f7d4323687a227294a2a',
cluster='eu',
ssl=True
)
class TerminalChat():
users = {
"pacifictrout": "App123",
"dingleberry": "App123",
}
@staticmethod
def login(username, password):
if TerminalChat.users.get(username) == password:
session['user'] = username
return True
return False
@app.route('/', methods=['GET', 'POST'])
def index():
if(current_user.is_authenticated):
logout_user()
if request.method == 'POST':
username = request.form.get('username')
password = request.form.get('password')
user = User.query.filter_by(username=username).first()
if user and user.check_password(password):
user_primary_key = User.query.filter_by(username=username).first().id
company_id = User.query.filter_by(username=username).first().company_id
company_password = User.query.filter_by(username=username).first().id
session['user'] = username
session['company_id'] = company_id
session['company_password'] = company_password
login_user(user)
print(user.is_authenticated)
return redirect(url_for('select_chatroom', username=current_user.username))
else:
flash('Invalid username or password')
return render_template('login.html')
@app.route('/user/<username>/select-chatroom', methods=['GET', 'POST'])
@login_required
def select_chatroom(username):
# No need to fetch the user again if using Flask-Login, current_user should be used directly
company_id = current_user.company_id
if request.method == 'POST':
chatroom_name = request.form.get('chatroom_name')
print(chatroom_name)
action = request.form.get('action')
if action == "Join Chatroom": # Joining existing chatroom logic
chatroom = ChatRoom.query.filter_by(name=chatroom_name, company_id=company_id).first()
if chatroom:
print("aosulute retard")
# Your logic when the chatroom exists (like using Pusher)
session['current_chatroom'] = chatroom.name
session['chatroom_id'] = chatroom.id
pusher.trigger(chatroom_name, 'user_joined', {'message': f"{session['user']} joined the chat"})
# Triggering Pusher event or any other action
return redirect(url_for('chat', chatroom_id=chatroom.id)) # Redirect to chat view
else:
flash('Chatroom does not exist.', 'danger')
elif action == "Create Room":
print("djlsnfvousdvidnsivndsundsfcdsnisdfdsijdnijncsdncidsnivdsjvdnn")
chatroom_name = request.form.get('new_chatroom_name')
print(chatroom_name)
new_chatroom = ChatRoom(name=chatroom_name, company_id=company_id)
db.session.add(new_chatroom)
try:
db.session.commit()
flash('New chat room created!', 'success')
session['current_chatroom'] = new_chatroom.name
# Redirect to the chat view or refresh the page to show the new chatroom
return redirect(url_for('select_chatroom'))
except Exception as e:
db.session.rollback()
flash('Error adding chat room: ' + str(e), 'danger')
else:
flash('Chatroom name is required.', 'warning')
chatrooms = ChatRoom.query.filter_by(company_id=company_id).all()
return render_template('chatroom.html', chatrooms=chatrooms)
@app.route('/chat')
def chat():
print()
new_room = session.get('current_chatroom')
# Fetch the last N messages from the database
# Adjust the number of messages as needed
num_messages_to_load = 1000 # For example, load the last 50 messages
messages = Message.query \
.filter_by(chatroom_id=session['chatroom_id']) \
.order_by(Message.timestamp.asc()) \
.limit(num_messages_to_load) \
.all()
print(messages)
# Format messages for display
formatted_messages = [
{
'user': message.username,
'message': message.message,
'timestamp': message.timestamp.strftime("%A, %H:%M")
}
for message in messages
]
# Pass the messages to the template
return render_template('chat.html', username=session['user'], messages=formatted_messages)
@app.route('/send_message', methods=['POST'])
def send_message():
username = session.get('user', 'Anonymous')
print(username)
message_content = f"{username}: {request.form.get('message')}"
timestamp = datetime.utcnow()
chatroom_id = session.get('chatroom_id')
chatroom_name = session.get('current_chatroom')
new_message = Message(username=username, room=chatroom_name, message=message_content, timestamp=timestamp, company_id=current_user.company_id, chatroom_id=chatroom_id)
db.session.add(new_message) # Add the message to the database session
db.session.commit() # Commit the session to store the message in the database
# Use pusher to broadcast this message to all the clients
pusher.trigger('chatroom', 'new_message', {
'user': username,
'message': message_content,
'timestamp': timestamp.strftime("%A, %H:%M")
})
return jsonify(success=True)
@app.route('/register_organization', methods=['GET', 'POST'])
def register_organization():
if request.method == 'POST':
company_name = request.form['company_name']
# Add your logic to create the organization here
# For example, save to database
return redirect(url_for('some_route')) # Redirect to a confirmation page or dashboard
return render_template('create-organization.html')
from flask import session, request, flash, render_template, redirect, url_for
from cryptography.fernet import Fernet
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
from cryptography.hazmat.backends import default_backend
import base64
import os
import random
import string
SALT = b'my_pseudo_random'
@app.route('/create-organization', methods=['GET', 'POST'])
def create_organization():
session.clear()
if request.method == 'POST':
company_name = request.form['company_name']
if company_name:
existing_company = Company.query.filter_by(name=company_name).first()
if existing_company:
flash('Company already exists.')
return render_template('create-organization.html')
# Generate a random password
random_password = ''.join(random.choices(string.ascii_letters + string.digits, k=10))
new_company = Company(name=company_name)
new_company.set_password(random_password)
try:
db.session.add(new_company)
db.session.commit()
session['company_unique_id'] = new_company.unique_id
session['company_password'] = random_password
flash('Company created successfully.')
return redirect(url_for('organization_start', company_name=company_name, unique_id=new_company.unique_id, password=random_password))
except Exception as e:
db.session.rollback()
flash(f'An error occurred while creating the company. Error: {e}')
return render_template('create-organization.html')
else:
flash('Company name cannot be empty.')
return render_template('create-organization.html')
return render_template('create-organization.html')
@app.route('/organization-start/<company_name>/<unique_id>/<password>')
def organization_start(company_name, unique_id, password):
# Retrieve the unique ID and password from the session
unique_id = session.get('company_unique_id', None)
password = session.get('company_password', None)
session['company_id'] = unique_id
session['company_password'] = password
if unique_id and password:
# Render a template that shows the unique ID and password securely
return render_template('organization-start.html', company_name=company_name, unique_id=unique_id, password=password)
else:
# If there is no unique ID or password in the session, redirect to home
flash('No company information found.')
return redirect(url_for('index'))
@app.route('/join-organization', methods=['GET'])
def redirect_organization():
organization_id = request.args.get('company_id')
organization_password = request.args.get('company_password')
# Simply render the join-organization.html template on GET request
return render_template('join-organization.html',
company_id=organization_id,
company_password=organization_password)
@app.route('/join-organization', methods=['POST'])
def join_organization():
# Extract form data
print(request.form)
company_id = request.form.get('company_id')
company_password = request.form.get('company_password')
username = request.form.get('username')
user_password = request.form.get('user_password')
# Verify company
company = Company.query.filter_by(unique_id=company_id).first()
if company and company.password == company_password:
# Company verified, now handle user
# Check if username already exists within this company
existing_user = User.query.filter_by(username=username, company_id=company.id).first()
if existing_user:
flash('Username already exists within this company.')
return render_template('join-organization.html')
# Generate a unique user ID using UUID
new_user_id = str(uuid.uuid4())
# Create a new user with the generated ID
new_user = User(
id=new_user_id,
username=username,
password_hash=generate_password_hash(user_password), # Hash the user's password
company_id=company.unique_id # Associate user with the company
)
db.session.add(new_user)
try:
print("hello")
db.session.commit()
flash('Successfully joined the organization.')
# Redirect to the user's dashboard or another appropriate page
return redirect(url_for('index'))
except Exception as e:
print(e)
db.session.rollback()
flash('An error occurred while creating your account. Please try again.')
return render_template('login.html')
else:
flash('Invalid company ID or password.')
print("jigaboo")
return render_template('join-organization.html')
@app.route('/logout')
def logout():
session.clear()
return render_template('login.html')
@app.route('/admin/dashboard')
def admin_dashboard():
organization_id = session.get('company_id')
password = session.get('company_password')
users = User.query.filter_by(company_id=organization_id).all()
new_company = Company.query.filter_by(unique_id=organization_id).first()
messages = Message.query.filter_by(company_id=organization_id).all()
chatrooms = ChatRoom.query.filter_by(company_id=organization_id).all()
# Render the admin dashboard template
return render_template('administrator-dashboard',
organization_id=organization_id,
organization_password=password,
users=users,
messages=messages,
chatrooms=chatrooms, company=new_company, acc_user = current_user)
@app.route('/delete-user', methods=['POST'])
def delete_user():
user_id = request.form.get('user_id')
user = User.query.get(user_id)
if user:
db.session.delete(user)
db.session.commit()
# Returning a JSON response here, not redirecting.
return jsonify({'status': 'success', 'message': 'User deleted successfully.'})
else:
# Returning a JSON response for failure case.
return jsonify({'status': 'failure', 'message': 'User not found.'})
@app.route('/delete-chatroom', methods=['POST'])
def delete_chatroom():
chat_id = request.form.get('chatroom_id')
chat = ChatRoom.query.get(chat_id)
if chat:
db.session.delete(chat)
db.session.commit()
# Returning a JSON response here, not redirecting.
return jsonify({'status': 'success', 'message': 'Chatroom deleted successfully.'})
else:
# Returning a JSON response for failure case.
return jsonify({'status': 'failure', 'message': 'Chatroom not found.'})
@app.route('/service-worker.js')
def serve_service_worker():
return send_from_directory('static', 'service-worker.js')
@login_manager.user_loader
def load_user(user_id):
# Assuming your user model's ID is an integer.
# Adjust the query if your user identifier is of a different type
return User.query.get(user_id)
with app.app_context():
db.create_all()
if __name__ == '__main__':
app.run(debug=True)