def loginCheck(request, connectionSocket): database = lite.connect('user.db') cursor = database.cursor() type = request.split("\t") email = type[1] password = type[2] password = password.strip('\r\n') loginCheck = cursor.execute( "select count(*) from user where email = ? and password = ? ", [email, password]) loginCheck = loginCheck.fetchall() print(loginCheck.__str__()) if len(loginCheck) < 1: status = "CheckLogin\tFailure\r\n" connectionSocket.send(status.encode()) database.close() print("Login failure") return ["Failure"] else: infoValues = email information = database.execute( "select id, firstName, lastName, email from user where email = ?", [infoValues]) info = information.fetchall() status = "CheckLogin\tSuccess\r\n" connectionSocket.send(status.encode()) OverView.userLogin(info[0][0], connectionSocket) now = time.time() update = database.execute( "update user set lastSeen = ? where email = ?", [int(now), email]) database.commit() print("Login Successful") return ["Success"]
def getOfflineMessages(request, connectionSocket): database = lite.connect('user.db') cursor = database.cursor() request = request.strip("\r\n") request = request.split("\t") user = OverView.findUserBySocket(connectionSocket) userID = user.id roomID = request[1] loadmessages = cursor.execute( "select messages.* from messages join user on user.id = messages.userID and messages.roomID = ?", [roomID]) loadmessages = loadmessages.fetchall() messages = [] for i in range(len(loadmessages)): user = OverView.findUserByID(loadmessages[i][1]) messages.append({ 'User': user.name, 'Message': loadmessages[i][2], 'Time': loadmessages[i][3], 'RoomID': loadmessages[i][4] }) print("getOfflineMessages") messages = messages.__str__() connectionSocket.send( ("getOfflineMessages\t" + messages + "\r\n").encode())
def printFriendsList(request, connectionSocket): print("printFriendsList") database = lite.connect('user.db') cursor = database.cursor() user = OverView.findUserBySocket(connectionSocket) if user != None: users = cursor.execute( "select distinct * from friends as f1, friends as f2 where f1.secondFriendID = f2.firstFriendID and f1.firstFriendID = f2.secondFriendID and (f1.firstFriendID = ?)", [user.id]) users = users.fetchall() friends = [] for i in range(len(users)): secondPerson = OverView.findUserByID(users[i][1]) id = users[i][1] friends.append({ "Name": secondPerson.name, "Status": secondPerson.status, "ID": id }) friends = friends.__str__() connectionSocket.send(("FriendsList\t" + friends + "\r\n").encode()) else: connectionSocket.send(("FriendsList").encode())
def leaveRoom(request, connectionSocket): #Removes User froom Room and sends all the other clients telling them that someone has left the room database = lite.connect('user.db') cursor = database.cursor() request = request.strip("\r\n") request = request.split("\t") roomID = request[1] user = OverView.findUserBySocket(connectionSocket) userID = user.id OverView.leaveRoom(roomID, userID) OverView.leaveRoom(roomID, userID) delete = cursor.execute( "delete from roomMembers where roomID = ? and userID = ?", (roomID, userID)) deleteOwner = cursor.execute( "update rooms set ownerID = -1 where roomID = ? and ownerID = ?", (roomID, userID)) database.commit() #Send back all room members and delete the room member from list connectionSocket.send(("LeaveRoom\tSuccess").encode()) print("leaveRoom")
def getMyRooms(request, connectionSocket): user = OverView.findUserBySocket(connectionSocket) print("Get Rooms") if user != None: rooms = OverView.grabRoomsWithUser(user.id) send = "YourRooms\t" + rooms.__str__() connectionSocket.send(send.encode()) else: connectionSocket.send("YourRooms".encode()) return 1
def joinRoom(request, connectionSocket): request = request.strip("\r\n") request = request.split("\t") UserID = int(request[1]) roomID = int(request[2]) OverView.addUser(roomID, UserID) database = lite.connect('user.db') cursor = database.cursor() cursor.execute("insert into roomMembers(roomID, userID) values (?, ?)", [roomID, UserID]) database.commit() connectionSocket.send("JoinRoom\tSuccess\r\n".encode()) print("joinRoom") return 1
def sendPrivateMessage(request, connectionSocket): database = lite.connect('user.db') cursor = database.cursor() print("sendPrivateMessage") request = request.strip("\r\n") request = request.split("\t") sender = OverView.findUserBySocket(connectionSocket) receiverID = int(request[1]) receiver = OverView.findUserByID(receiverID) message = request[2] timeStamp = time.time() receiver = OverView.findUserByID(receiverID) insert = cursor.execute( "insert into privateMessages(toUser, fromUser, message, timeStamp) values(?, ?, ?, ? )", [receiverID, sender.id, message, timeStamp]) database.commit() connectionSocket.send(("SendPrivateMessage\tSuccess").encode())
def getRoomMembers(request, connectionSocket): request = request.strip("\r\n") request = request.split("\t") roomID = int(request[1]) Users = OverView.getRoomMembers(roomID).__str__() connectionSocket.send(("RoomMembers\t" + Users).encode()) return 1
def sendMessage(request, connectionSocket): request = request.strip("\r\n") request = request.split("\t") message = request[1] roomID = int(request[2]) addTime = int(time.time()) user = OverView.findUserBySocket(connectionSocket) database = lite.connect('user.db') cursor = database.cursor() insert = cursor.execute( "insert into messages(userID, message, timeStamp, roomID) values(?, ? ,? ,? )", [user.id, message, addTime, roomID]) database.commit() print(message) OverView.sendMessage(roomID, message, connectionSocket) connectionSocket.send(("SendMessage\tSuccess").encode()) print("sendMessage")
def getFriendStatus(request, connectionSocket): print("getFriendStatus") request = request.strip("\r\n") request = request.split("\t") friendID = request[1] friend = OverView.findUserByID(friendID) connectionSocket.send( ("FriendStatus\tSuccess\t" + friend.status + "\r\n").encode()) return 1
def addFriend(request, connectionSocket): database = lite.connect('user.db') cursor = database.cursor() request = request.strip("\r\n") request = request.split("\t") newFriend = OverView.findUserByEmail(request[1]) if newFriend != None: user = OverView.findUserBySocket(connectionSocket) print(user) cursor.execute("insert into friends values(?, ?)", [newFriend.id, user.id]) cursor.execute("insert into friends values(?, ?)", [user.id, newFriend.id]) database.commit() print("addFriend") connectionSocket.send("AddFriend\tSuccess\r\n".encode()) else: connectionSocket.send("AddFriend\tFailure\r\n".encode())
def createRoom(request, connectionSocket): request = request.strip("\r\n") request = request.split("\t") roomname = request[1] owner = OverView.findUserBySocket(connectionSocket) database = lite.connect('user.db') cursor = database.cursor() cursor.execute("insert into rooms(name, ownerID, deleted) values(?, ?, 0)", [roomname, owner.id]) database.commit() room = cursor.execute( "select * from rooms where name = ? and ownerID = ? ", [roomname, owner.id]) room = room.fetchall() roomID = room[0][0] OverView.createRoom(owner.id, roomname, roomID) connectionSocket.send(("CreateRoom\t" + roomID.__str__() + "\t" + owner.id.__str__() + "\r\n").encode()) print("CreateRoom") return 1
def removeFriend(request, connectionSocket): database = lite.connect('user.db') cursor = database.cursor() request = request.strip("\r\n") request = request.split("\t") secondFriend = int(request[1]) firstFriend = OverView.findUserBySocket(connectionSocket) remove = cursor.execute( "delete from friends where firstFriendID = ? and secondFriendID = ?", [firstFriend.id, secondFriend]) database.commit() connectionSocket.send("RemoveFriend\tSuccess\r\n".encode())
def getPrivateMessages(request, connectionSocket): database = lite.connect('user.db') cursor = database.cursor() request = request.strip("\r\n") request.split("\t") sender = OverView.findUserBySocket(connectionSocket) fromUserID = sender.id toUserId = request[1] messages = cursor.execute( "select * from privateMessages where fromUser = ? and toUser = ? ", [fromUserID, toUserId]) message = [] for i in range(len(messages)): message = messages[i][3] time = message[i][4] message.append({'Message': message, 'Time': time}) message = message.__str__() connectionSocket.send("PrivateMessages\t" + message + "\r\n") return 1
def logout(request, connectionSocket): print("Logout") OverView.userLogout(connectionSocket)
###Chat Bot import sqlite3 as lite from socket import * from _thread import * import time from Utilities import Room, User, OverView serverPort = 12019 serverSocket = socket(AF_INET, SOCK_STREAM) serverSocket.bind(('', serverPort)) serverSocket.listen(10) OverView = OverView() ############################ # Responses: Type \t Failure OR Success \r\n # Ex: SendMessage \t Success \r\n # Requests: Type \t Data \r\n # Ex: RegisterUser \t FirstName \t LastName \t Address \t Email \t password1 \r\n ########################### def initiation(): print("Server started.") createEverything() while 1: print("Server ready to accept connections.") connectionSocket, addr = serverSocket.accept() start_new_thread(main, (connectionSocket, ))