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 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 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 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 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 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 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 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