def createChatRoom(self, roomName): # Avoid duplicates for room in self.chatRoomList: if room.name == roomName: return tempRoom = ChatRoom(roomName) self.chatRoomList.append(tempRoom) return
def __init__(self): self.host = utils.socket.get('HOST', '') self.host = "" self.port = utils.socket.get('PORT', 9000) self.buf_size = utils.BUFF_SIZE self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) self.sock.bind((self.host, self.port)) self.ChatroomDict['General'] = ChatRoom(utils.GEN_ROOM)
def load(self, room_id_alias): room_name = self.parse_room_name_or_id(room_id_alias) if room_name in self.room_table: if room_id_alias in self.loaded_rooms.keys(): self.current_room = self.loaded_rooms[room_id_alias] # print("CURRENT ROOM: {}".format(self.current_room.get_room_name())) for msg in self.all_rooms_messages[room_name]: print(msg) else: self.current_room = ChatRoom(self.join_room(room_id_alias), self.init_msg_hist_for_room) self.loaded_rooms[room_id_alias] = self.current_room self.room_table[room_name]["loaded"] = True else: room_name = self.parse_room_name_or_id(room_id_alias) room = self.m_client.create_room(room_name) room.set_room_name(room_name) self.room_table[self.room_handle] = { "room_id": room.room_id, "loaded": False } self.current_room = ChatRoom(room, self.init_msg_hist_for_room) self.loaded_rooms[room.room_id] = self.current_room self.initialized = True
def create_room(self): if len(self.ChatroomDict) < utils.MAX_ROOMS: try: print("Please type the name for the new room and press Enter.") room_name = raw_input() if utils.validate_str( room_name) and room_name not in self.ChatroomDict.keys( ): self.ChatroomDict[room_name] = ChatRoom(room_name) print("The room %s has been created" % (room_name)) else: print("Invalid room name. Command has been aborted.") except Exception as e: logging.exception(e) self.sock.close() os._exit(1) else: print( "Sorry, the maximum room limit of %s has already been reached." % (utils.MAX_ROOMS))
def create_chat_room(user, command): """ Método para crear un cuarto de chat :param user: El objeto tipo user que será el dueño del chatroom :param command: El nombre del chatroom """ name = command[1] original_name = False for chat_room in chat_rooms: if(getattr(chat_room, 'name') == name): original_name = True message = '111_...NOMBRE DEL CUARTO EN USO\n' getattr(user, 'socket').sendall(message.encode()) break if(not original_name): room = ChatRoom.create_room(name, user) chat_rooms.append(room) message = '112_...CUARTO DE CHAT CREADO\n' getattr(user, 'socket').sendall(message.encode())
def sendMessage(self, message): ChatRoom.showMessage(self, message)
def recibe(self, cliente, ip): eventos = Eventos() while True: try: r = cliente.recv(1024).encode('UTF-8') m = r.rstrip() e = m.split(" ", 1) evento = eventos.get_evento(e.pop(0)) print(evento) if evento == eventos.IDENTIFY: if len(e) != 1: raise ValueError for i in self.clientes: if ip == i.get_ip(): if not e[0] in self.nombres: i.set_nombre(e.pop(0)) self.nombres.append(i.get_nombre()) print(str(self.nombres)) try: if len(self.nombres) != 0: if e[0] in self.nombres: self.envia("Nombre ocupdado", cliente) except: continue if evento == eventos.STATUS: if len(e) != 1: raise ValueError for i in clientes: if ip == i.get_ip(): i.set_estado(e.pop(0)) if evento == eventos.MESSAGE: z = e.pop(0).split(" ", 1) usuario = z.pop(0) mensaje = z.pop(0) nombre = self.obtiene_nombre(ip) self.envia_privado(usuario, mensaje, nombre) if evento == eventos.USERS: self.envia(str(self.nombres), cliente) if evento == eventos.PUBLICMESSAGE: self.envia_publico(e.pop(0), ip) if evento == eventos.CREATEROOM: for i in self.clientes: if ip == i.get_ip(): nombre = i.get_nombre() cuarto = ChatRoom(e.pop(0), nombre, cliente) self.cuartos.append(cuarto) if evento == eventos.INVITE: new_int = e.pop(0).split(" ", 1) cuarto = new_int.pop(0) usuarios = new_int.pop(0).split() for r in self.clientes: if ip == r.get_ip(): nombre = r.get_nombre() for i in self.cuartos: if i.get_nombre() == cuarto: if nombre == i.get_due(): for usuario in usuarios: print("listo") i.invita(usuario) self.envia_privado( usuario, "Te he invitado al cuarto " + cuarto, nombre) print(i.get_invitaciones()) if evento == eventos.JOINROOM: try: cuarto = e.pop(0) for i in self.cuartos: if cuarto == i.get_nombre(): for r in self.clientes: if ip == r.get_ip(): nombre = r.get_nombre() i.joinr(nombre, cliente) i.envia("Te has unido al cuarto " + nombre, cliente) except ValueError: self.envia("No estás invitado", cliente) if evento == eventos.ROOMESSAGE: q = e.pop(0).rstrip().split(" ", 1) for i in self.clientes: if ip == i.get_ip(): nombre = i.get_nombre() for r in self.cuartos: if q.pop(0) == r.get_nombre(): m = r.get_miembros() for a in m: self.envia_cuarto(q[0], a, nombre) q.pop(0) if evento == eventos.DISCONNECT: for i in self.clientes: if i.get_ip() == ip: for n in self.nombres: if i.get_nombre() == n: d = self.nombres.index(n) self.nombres.pop(d) t = self.clientes.index(i) self.clientes.pop(t) i.get_socket().close() raise ValueError except Exception as e: if e != KeyboardInterrupt: continue else: self.desconecta()