def __init__(self, u: str, user_list): self.name = u channel = grpc.insecure_channel(address + ':' + str(port)) self.conn = rpc.ChatServerStub(channel) nUsr = chat.User() nUsr.name = u nRetUsr = chat.User() nRetUsr = self.conn.CheckMessageList(nUsr) if(nRetUsr.name != "NA"): askToChat = input(nRetUsr.name + " is requesting to chat with you. Enter 'yes' to accept or different user: "******"yes"): o = nRetUsr.name else: o = askToChat else: o = input("[Spartan] Enter a user whom you want to chat with: ") if o not in user_list: print(o + " is not allowed to use the messenger.") print("Try again with one of the following users: " + ','.join(str(x) for x in user_list)) return print("[Spartan] You are now ready to chat with " + o + ".") n = chat.User() n.name = u n.othername = o self.othername = o if(not(self.conn.CreateConnection(n).successful)): print("[Spartan] Connection failed!!") threading.Thread(target=self.__listen_for_messages, daemon=True).start() self.send_message()
def CreateConnection(self, request: chat.User, context): users = [request.name, request.othername] self.requestFromUser[request.othername] = chat.User(name=request.name) users.sort() new_key = users[0] + "-" + users[1] for online_user in self.registeredUsers: if (online_user.name == request.othername): if new_key not in self.messages: self.messages[new_key] = lrucache.LRUCache(max_num_messages) return chat.Connection(successful=True) return chat.Connection(successful=False)
def __init__(self): self.cache = {} self.messages = {} self.unreadMessages = {} self.registeredUsers = [] self.sent_num_messages = {} self.requestFromUser = {} for usr in yaml_file['users']: self.registeredUsers.append(chat.User(name=usr)) self.unreadMessages[usr] = {} self.sent_num_messages[usr] = []
def __listen_for_messages(self): nl = chat.User() nl.name = self.name nl.othername = self.othername def findWholeWord(w, s): return re.compile(r'\b({0})\b'.format(w), flags=re.IGNORECASE).search(s) while True: for note in self.conn.ChatStream(nl): message = self.decrypt_message(note.nonce, note.ciphertext, note.tag) if(findWholeWord(nl.name, message) == None): print(message)
def CheckMessageList(self, request, context): if request.name in self.requestFromUser: return self.requestFromUser[request.name] else: return chat.User(name="NA")