async def chat(message, ws): log = route_log.getChild('Chat') message = ChatMessage(message) log.debug(message) if send(message.to_user, message.to_json()) < 1: message = ServerMessage({'content': '{0}已离线。'.format(message.to_user)}) await ws.send(message.to_json()) return None
async def updateFriend(message, ws): log = route_log.getChild('FriendUpdate') message = FriendUpdateMessage(message) temp_list = db.fetchFriend(message.source) friend_list = [] for row in temp_list: friend_list.append(row[0]) log.debug(friend_list) send(message.source, FriendMessage({'friend_list': friend_list}).to_json()) for friend in friend_list: friend_cert = db.fetchCert(friend) install_message = CertificateInstallMessage({ 'cert_user': friend, 'cert': friend_cert }) send(message.source, install_message.to_json())
async def requestFriend(message, ws): log = route_log.getChild('FriendRequest') message = FriendRequestMessage(message) log.debug(message) if send(message.friend_name, message.to_json()) < 1: message = ServerMessage( {'content': '{0}已离线。'.format(message.friend_name)}) await ws.send(message.to_json()) return None
async def acceptFriend(message, ws): log = route_log.getChild('FriendAccept') message = FriendAcceptMessage(message) log.debug(message) if message.accept: db.addFriend(message.friend_name, message.source) db.addFriend(message.source, message.friend_name) await updateFriend( FriendUpdateMessage({ 'source': message.friend_name }).to_json(), ws) await updateFriend( FriendUpdateMessage({ 'source': message.source }).to_json(), ws) else: server_message = ServerMessage( {'content': '{0}拒绝成为您的好友。'.format(message.source)}) send(message.friend_name, server_message.to_json())
async def certficateSigning(message, ws): log = route_log.getChild('CertificateSigning') server_key = crypto.loadPrivateFromUser('Server', None) server_cert = crypto.loadCertFromUser('Server') message = CertificateSigningRequestMessage(message) cert = crypto.signCSR(server_cert, server_key, crypto.loadCSR(message.csr), 90) log.debug('Sign CSR for {0}.'.format(message.source)) install_message = CertificateInstallMessage({ 'cert': cert, 'cert_user': message.source }) db.addCert(install_message.cert_user, install_message.cert) send(message.source, install_message.to_json()) friend_list = db.fetchFriend(message.source) log.debug('Send new cert to friends: {0}'.format(friend_list)) for friend in friend_list: send(friend[0], install_message.to_json())