def _join_room(self, room): # check if we are not already in a room if self.room is not None: self.transport.write('You are already in a room\n') return # check if room exists room = room.strip() if room not in self.factory.rooms: self.transport.write('Room %s does not exist\n' % room) return # join room self.room = room self.factory.rooms[room].append(self.login) self.factory.clients[self.login]["room"] = room self.transport.write('entering room: %s\n' % room) if self.room is not None: for person in self.factory.rooms[self.room]: if self.login != person: # search for this client client = self.factory.clients[person] protocol = client['protocol'] protocol.sendLine('* new user joined chat: %s' % self.login)
def handle_CMD(self, cmd): cmdline = "<%s>, %s" % (self.name, cmd) for name, protocol in self.users.iteritems(): #if protocol == self: if True: protocol.sendLine(cmdline) print "sending %s command %s" % (self.name, cmdline)
def received_data(self, data): if not self.login: self.login = data print("[{time}] [{user}] joined".format(time=timestamp(), user=self.login)) self.factory.clients[self.login] = self for login, protocol in self.factory.clients.items(): protocol.sendLine( "[color=f1c40f]{user} joined[/color]{stop}".format( user=self.login, stop=self.stop_str)) elif data == 'exit': self.transport.write('Bye!') self.transport.loseConnection() else: for login, protocol in self.factory.clients.items(): if self.login == login: # print("Self: MSG NOT Sent to {}/{}".format(login, # self.login)) # Communicate back to the user that sent the data # protocol.sendLine("Data successfully reached server for " # "distribution.".format(data=data)) pass else: # print("MSG Sent to {}/{}".format(login, self.login)) protocol.sendLine("{data}{stop}".format(data=data, stop=self.stop_str))
def DoodleQueueWorkload(reactor, protocol): while True: if not Communicator.doodle_queue.empty(): while not Communicator.doodle_queue.empty(): line = Communicator.doodle_queue.get() protocol.sendLine(line) yield deferLater(reactor, 0.1, lambda: None)
def connectionLost(self, reason): print("[{time}] {user} Disconnected".format(time=timestamp(), user=self.login)) self.factory.clients.pop(self.login) for login, protocol in self.factory.clients.items(): protocol.sendLine( "[color=f1c40f]{user} quit[/color]{stop}".format( user=self.login, stop=self.stop_str))
def connectionLost(self, reason): client = self.factory.clients[self.login] room = client['room'] protocol = client['protocol'] if room is not None: for people in self.factory.rooms[room]: if people != self.login: current = self.factory.clients[people] protocol = current['protocol'] protocol.sendLine("%s has quit the chat room" % (self.login,))
def releaseTriage(self): for user, triage in self.users.iteritems(): if triage.messageTriage != None: for name,protocol in self.servers.iteritems(): if protocol.state == "cleared" and protocol.contribution_score > 50: protocol.state = "server" protocol.sendLine("From: " + triage.messageTriage.f + " To:" + triage.messageTriage.t + " ->" + triage.messageTriage.message) if(str(triage.messageTriage.t) in self.users): proto = self.users[str(triage.messageTriage.t)] proto.sendLine("[" + triage.messageTriage.f + "] " + triage.messageTriage.message) triage.messageTriage = None # clear the triage
def _leave_room(self): # check if we are in a room if self.room is None: self.transport.write('You are not in a room\n') return # leave room self.factory.rooms[self.room].remove(self.login) self.factory.clients[self.login]["room"] = None if self.room is not None: for person in self.factory.rooms[self.room]: if person != self.login: client = self.factory.clients[person] protocol = client['protocol'] protocol.sendLine('* user has left chat: %s' % self.login) self.room = None
def lineReceived(self, line): if len(line) == 0: return if not self.login: self._login(line) elif line == '/quit': self._quit() elif line == '/rooms': self._list_rooms() elif line.startswith('/join'): self._join_room(line[5:]) elif line == '/leave': self._leave_room() else: if self.room is not None: for person in self.factory.rooms[self.room]: if person != self.login: client = self.factory.clients[person] protocol = client['protocol'] protocol.sendLine("%s: %s" % (self.login, line))
def handle_privMsg(self, msg): msg = msg.split(":") if msg[0] in self.myChannels: #send the message to that channel for name in self.channelNames[msg[0]]: proto = self.users[name] proto.sendLine(msg[0] + ":" + self.name + ":" + msg[1]) elif (msg[0] in self.users) or (len(self.servers) > 0): logger.debug("Trying to triage message from:" + self.name + " to:" + msg[0] + " msg:" + msg[1]) self.messageTriage = TriagedMessage(self.name, msg[0], msg[1]) if len(self.servers) > 0: # Ask the servers search:kdawkins for name, protocol in self.servers.iteritems(): protocol.sendLine("search:"+msg[0]) protocol.state = "SEARCH" elif msg[0] in self.users: self.releaseTriage() else: self.sendLine("Error, user not found.") self.messageTriage = None else: self.sendLine("Target of private message not found.")
def broadcastMessage(self,message): for name,protocol in self.factory.users.iteritems(): if protocol is not self: protocol.sendLine(message)
def _write_usual_message(self, protocol, data): resp = ChatResponse(data=data) protocol.sendLine(to_bytes(json.dumps(resp.to_dict())))
def broadcastMessage(self, message): for name, protocol in self.factory.users.iteritems(): if protocol != self: protocol.sendLine(message)
def broadcastLine(self, line): for name, protocol in self.factory.users.iteritems(): protocol.sendLine(line)
def sendMessage(cls, protocol, msg): assert isinstance(protocol, ChatProtocal) #protocol.transport.write(msg) protocol.sendLine(msg)
def loseAllConnections(self): for protocol in self.clients.values(): protocol.sendLine('error in chat') protocol.transport.loseConnection()
def announce(self, message): for name, protocol in self.users.iteritems(): if protocol != self: protocol.sendLine(message)
def distrubute(self, message): toSend = "%s <%s> %s" % (strftime("%H:%M:%S", localtime()), self.name, message) for name, protocol in self.users.iteritems(): if protocol != self: protocol.sendLine(toSend)
def sendToAll(self, line): """Send a line to every connected player.""" for protocol in self.allProtocols.values(): protocol.sendLine(line)