forked from ChannelX/ChannelX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
312 lines (274 loc) · 12.5 KB
/
main.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
from flask import Flask, session, redirect, url_for, render_template, request
from flask_socketio import SocketIO, send,emit, join_room, leave_room
from flask_sqlalchemy import SQLAlchemy
from flask_mail import Mail, Message
from flask_login import LoginManager, UserMixin, login_user, login_required, logout_user
import os
import datetime
basedir = os.path.abspath(os.path.dirname(__file__)) # Relative path for SQLAlchemy database file.
app = Flask(__name__)
app.config.from_object(__name__)
app.config['SECRET_KEY'] = 'ChannelX!^+%&/(()=?798465312-_*' # Random complex key for CSRF security.
app.config['SQLALCHEMY_TRACK_MODIFICATIONS']= False # Eliminates SQLAlchemy deprecation warning.
app.config['SQLALCHEMY_DATABASE_URI']= 'sqlite:///' + os.path.join(basedir, 'database.db')
app.config['MAIL_SERVER']='smtp.gmail.com'
app.config['MAIL_PORT'] = 465
app.config['MAIL_USERNAME'] = '[email protected]'
app.config['MAIL_PASSWORD'] = 'CProje2..'
app.config['MAIL_USE_TLS'] = False
app.config['MAIL_USE_SSL'] = True
mail = Mail(app)
db = SQLAlchemy(app)
login_manager = LoginManager()
login_manager.init_app(app)
class User(UserMixin, db.Model):
id=db.Column(db.Integer,primary_key=True)
Username=db.Column(db.String(20),unique=True)
Password=db.Column(db.String(20))
Name=db.Column(db.String(50))
Email=db.Column(db.String(20))
class Channel(db.Model):
id=db.Column(db.Integer,primary_key=True)
Channel_Name=db.Column(db.String(20))
Channel_Password=db.Column(db.String(20))
Chat_Admin=db.Column(db.String(20))
Start_Time=db.Column(db.String(20))
End_Time=db.Column(db.String(20))
days=db.Column(db.String(100))
class Nickname(db.Model):
id=db.Column(db.Integer,primary_key=True)
nickname=db.Column(db.String(20))
username=db.Column(db.String(20))
channel_name=db.Column(db.String(20))
class Message(db.Model):
id=db.Column(db.Integer,primary_key=True)
sender=db.Column(db.String(20))
channel_name=db.Column(db.String(20))
content=db.Column(db.String(200))
date=db.Column(db.DateTime, default=datetime.datetime.utcnow)
socketio = SocketIO(app, async_mode='gevent') # Working with gevent mode provides keyboard interrupt with CTRL+C.
db.create_all() # Creates DB.
@login_manager.user_loader
def load_user(user_id):
return User.query.get(int(user_id))
@app.route('/', methods=['GET'])
def home():
return render_template('index.html')
@app.route('/terms_and_conditions', methods=['GET'])
def terms_and_conditions():
return render_template('terms_and_conditions.html')
@app.route('/sign_up', methods = ['GET','POST'])
def sign_up():
if request.method == 'POST':
Name = request.form['Name']
Username = request.form['Username']
Password = request.form['Password']
Email = request.form['Email']
Confirm = request.form['Confirm']
user = User.query.filter_by(Username = Username).first()
if user:
username_failure = "Username already exists choose another one."
return render_template('sign_up.html', username_failure = username_failure)
else:
if Password == Confirm:
new_user = User(Name = Name, Username = Username, Password = Password, Email = Email)
db.session.add(new_user)
db.session.commit()
login_user(new_user)
session['Name'] = Name
session['Username'] = Username
return redirect('/user_panel')
else:
password_match = "Passwords are not same!"
return render_template('/sign_up.html', password_match = password_match)
else:
return render_template('sign_up.html')
@app.route('/login',methods=['GET','POST'])
def login():
if request.method == 'GET':
return render_template('login.html')
else:
Username = request.form['Username']
Password = request.form['Password']
user = User.query.filter_by(Username = Username).first()
if user:
if user.Password == Password:
login_user(user)
session['Name'] = user.Name
session['Username'] = user.Username
return redirect('/user_panel')
else:
password_failure = "Password failure!"
return render_template('/login.html', password_failure = password_failure)
else:
name_failure = "Username not found!"
return render_template('/login.html', name_failure = name_failure)
@app.route('/join',methods=['GET','POST'])
def join():
if request.method == 'GET':
return render_template('join.html')
else:
Channel_Name = request.form['Channel_Name']
Channel_Password = request.form['Channel_Password']
nick = request.form['Nickname']
session['Channel_Name'] = Channel_Name
session['Nickname'] = nick
try:
username=session['Username']
except:
username=session['Username']="guest"
chnn = Channel.query.filter_by(Channel_Name=Channel_Name).first()
if not chnn:
ChannelName_failure = "Sorry, channel name is not found!"
return render_template('join.html', ChannelName_failure = ChannelName_failure)
if chnn.Channel_Password != Channel_Password:
ChannelPassword_failure = "Channel name and password could not match!"
return render_template('join.html', ChannelPassword_failure = ChannelPassword_failure)
weekday=["Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"][datetime.datetime.today().weekday()]
if weekday not in chnn.days.split(','):
ChannelName_failure = "Channel is not available today!"
return render_template('join.html', ChannelName_failure = ChannelName_failure)
now = datetime.datetime.now()
now=int(now.hour)*60+int(now.minute)
start_hour,start_min=chnn.Start_Time.split(':')
end_hour,end_min=chnn.End_Time.split(':')
if int(start_hour)*60+int(start_min) <= now < int(end_hour)*60+int(end_min):
pass
else:
ChannelName_failure = "Channel is not available at this time!"
return render_template('join.html', ChannelName_failure = ChannelName_failure)
nicknames=Nickname.query.filter_by(channel_name=Channel_Name).all()
nickname_matched=False
for nickname in nicknames:
if nickname.nickname == nick:
if nickname.username == session['Username']:
nickname_matched=True
break
else:
Nickname_failure = "This nickname is already being used by another user!"
return render_template('join.html', Nickname_failure = Nickname_failure)
elif (nickname.username == session['Username']) and (session['Username'] != "guest"):
Nickname_failure = "You already have another nickname for this channel!"
return render_template('join.html', Nickname_failure = Nickname_failure)
if not nickname_matched:
new_nickname = Nickname(nickname = nick, username = session['Username'], channel_name = Channel_Name)
db.session.add(new_nickname)
db.session.commit()
return redirect('/channel')
@app.route('/log_out')
@login_required
def log_out():
logout_user()
print(session.get('Name') + " has logged out.")
return 'Logged out.'
@app.route('/delete_channel/<channel_id>',methods=['GET','POST'])
@login_required
def delete_channel(channel_id):
Channel.query.filter_by(id=channel_id).delete()
session['Nickname'] = Nickname.query.filter_by(channel_name=session['Channel_Name'],username=session['Username']).first().nickname
db.session.commit()
return redirect('/user_panel')
@app.route('/enter_channel/<channel_id>',methods=['GET','POST'])
@login_required
def enter_channel(channel_id):
chn = Channel.query.filter_by(id=channel_id).first()
session['Channel_Name'] = chn.Channel_Name
session['Nickname'] = Nickname.query.filter_by(channel_name=session['Channel_Name'],username=session['Username']).first().nickname
db.session.commit()
return redirect('/channel')
@app.route('/user_panel',methods=['GET','POST'])
@login_required
def user_panel():
if request.method == 'GET':
uname=session['Username']
channels=Channel.query.filter_by(Chat_Admin=uname).all()
print(channels)
return render_template('user_panel.html', channels=channels)
Name = session['Name']
User = session['Username']
Channel_Name = request.form['Channel_Name']
nickname = request.form['Nickname']
Channel_Password = request.form['Channel_Password']
Start_Time=request.form['Start_Time']
End_Time=request.form['End_Time']
days=request.form.getlist('days')
chn = Channel.query.filter_by(Channel_Name=Channel_Name).first()
if chn:
ChannelName_failure="Channel is already exists please choose another."
return render_template('user_panel.html', ChannelName_failure = ChannelName_failure)
new_channel = Channel(Channel_Name = Channel_Name, Channel_Password = Channel_Password, Chat_Admin=User,Start_Time=Start_Time,End_Time=End_Time,days=",".join(days))
new_nickname = Nickname(nickname = nickname, username = User, channel_name = Channel_Name)
db.session.add(new_nickname)
db.session.commit()
session['Nickname'] = nickname
session['Channel_Name'] = Channel_Name
db.session.add(new_channel)
db.session.commit()
return redirect('/channel')
@app.route('/channel',methods=['GET','POST'])
def channel():
Nickname = session.get('Nickname')
Channel_Name = session.get('Channel_Name')
chn = Channel.query.filter_by(Channel_Name=Channel_Name).first()
days_channel_available=chn.days.split(',')
start_hour,start_min=chn.Start_Time.split(':')
start_hour,start_min=int(start_hour),int(start_min)
current_date=datetime.datetime.now()
days=["Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"]
start_date=datetime.datetime(current_date.year,current_date.month,current_date.day-(datetime.datetime.today().weekday()-days.index(days_channel_available[0])),start_hour,start_min)
messages=Message.query.filter_by(channel_name=Channel_Name).all()
messages=[str(message.date)+" "+message.sender+": "+message.content for message in messages if (start_date <= message.date) or (chn.Chat_Admin == session['Username'])]
return render_template('channel.html', Nickname=Nickname, Channel_Name=Channel_Name, messages="\n".join(messages)+"\n")
@app.route("/email",methods=['GET','POST'])
def index():
MsgBody = "This mail is send automatically."
fromMsg = ('ChannelX','[email protected]')
subject = 'ChannelX - New Messages'
emails = User.query.all()
for email in emails:
toMsg = "['" + email + "']"
msg = Message(subject, sender = fromMsg, recipients = toMsg)
msg.body = MsgBody
mail.send(msg)
return render_template('channel.html')
@socketio.on('joined',namespace='/channel')
def joined(message):
Channel_Name = session.get('Channel_Name')
Nickname=session.get('Nickname')
new_message=Message(sender='Server',channel_name=Channel_Name,content=Nickname + ' has entered the channel.')
db.session.add(new_message)
db.session.commit()
print(new_message.content +" "+ Channel_Name)
join_room(Channel_Name)
emit('status', {'msg': str(new_message.date)+" "+new_message.sender+": "+new_message.content}, room=Channel_Name)
@socketio.on('text',namespace='/channel')
def text(message):
Channel_Name=session['Channel_Name']
chnn = Channel.query.filter_by(Channel_Name=Channel_Name).first()
now = datetime.datetime.now()
now=int(now.hour)*60+int(now.minute)
start_hour,start_min=chnn.Start_Time.split(':')
end_hour,end_min=chnn.End_Time.split(':')
if int(start_hour)*60+int(start_min) <= now < int(end_hour)*60+int(end_min):
print("Sent message by " + session.get('Nickname') + ": " + message['msg'])
else:
print("Channel is closed")
return
Channel_Name = session.get('Channel_Name')
Nickname=session.get('Nickname')
new_message=Message(sender=Nickname,channel_name=Channel_Name,content=message['msg'])
db.session.add(new_message)
db.session.commit()
emit('message', {'msg': str(new_message.date)+" "+new_message.sender+": "+new_message.content}, room=Channel_Name)
@socketio.on('left',namespace='/channel')
def left(message):
Channel_Name = session.get('Channel_Name')
Nickname=session.get('Nickname')
new_message=Message(sender='Server',channel_name=Channel_Name,content=Nickname + ' has left the channel.')
db.session.add(new_message)
db.session.commit()
print(new_message.content+ " " + Channel_Name)
leave_room(Channel_Name)
emit('status', {'msg': str(new_message.date)+" "+new_message.sender+": "+new_message.content}, room=Channel_Name)
if __name__ == '__main__':
socketio.run(app)