def handle_part(data, match, client, channels): """ Someone is leaving a channel. Let's tell everyone about it and remove them from the channel's user listing (and the channel from the client's). '^PART (?P<channel>[^\s]+)( :(?P<message>.*))' :type data: str :type match: dict :type client: Client :type channels: list """ channame = match['channel'] logger.debug("{nick} leaves channel {channame}", nick=client.nick, channame=channame) if not channame in channels: logger.warn("no channel named {channame}", channame=channame) return channel = channels[channame] client.channels.remove(channel) channels[channame].clients.remove(client) announce = Protocol.part(client, channel, match['message'] or 'leaving') channel.send(announce)
def connectionLost(self, reason): logger.info('Lost client "{nick}"', nick=self._client.nick) announce = Protocol.quit(self._client, "Connection lost") for channel in self._client.channels: channel.clients.remove(self._client) channel.send(announce) self.factory.clients.remove(self)
def say(self, client, message): """ Sends message as a PRIVMSG to everyone in the channel except the person who sent it. :type client: Client :type message: str :rtype: None """ message = Protocol.privmsg(client, self, message) for cl in self._clients: if cl is not client: cl.send(message)
def handle_ping(data, match, client, channels): """ Ping! Send pong back. '^PING.*' :type data: str :type match: dict :type client: Client :type channels: list """ # logger.debug("Got ping from {nick}", nick=client.nick) return [Protocol.pong()]
def handle_join(data, match, client, channels): """ Someone joined a channel! A few things can happen now. Either they joined a channel that doesn't exist, and in that case, we create it and set them as the owner. If it already exists, let's announce their arrival to the participants. '^JOIN (?P<channels>.*)' :type data: str :type match: dict :type client: Client :type channels: list """ templates = [ ":{identity} JOIN {channel}", # join ":{server} 332 {nick} {channel} :{topic}", # topic ":{server} 353 {nick} = {channel} :{nicks}", # names in channel ":{server} 366 {nick} {channel} :End of /NAMES list." ] # end of names for channame in match['channels'].split(','): channame = channame.strip() logger.debug("{nick} joins channel {channame}", nick=client.nick, channame=channame) if channame not in channels: channels[channame] = Channel(name=channame, owner=client) channel = channels[channame] channel.clients.append(client) client.channels.append(channel) nicks = ' '.join(client.nick for client in channel.clients) client.send([ template.format(channel=channel.name, identity=client.identity, nick=client.nick, nicks=nicks, server="localhost", topic=channel.topic) for template in templates ]) announce = Protocol.join(client, channel) channel.send(announce)
def handle_join(data, match, client, channels): """ Someone joined a channel! A few things can happen now. Either they joined a channel that doesn't exist, and in that case, we create it and set them as the owner. If it already exists, let's announce their arrival to the participants. '^JOIN (?P<channels>.*)' :type data: str :type match: dict :type client: Client :type channels: list """ templates = [":{identity} JOIN {channel}", # join ":{server} 332 {nick} {channel} :{topic}", # topic ":{server} 353 {nick} = {channel} :{nicks}", # names in channel ":{server} 366 {nick} {channel} :End of /NAMES list."] # end of names for channame in match['channels'].split(','): channame = channame.strip() logger.debug("{nick} joins channel {channame}", nick=client.nick, channame=channame) if channame not in channels: channels[channame] = Channel(name=channame, owner=client) channel = channels[channame] channel.clients.append(client) client.channels.append(channel) nicks = ' '.join(client.nick for client in channel.clients) client.send([template.format(channel=channel.name, identity=client.identity, nick=client.nick, nicks=nicks, server="localhost", topic=channel.topic) for template in templates]) announce = Protocol.join(client, channel) channel.send(announce)
def handle_user(data, match, client, channels): """ This is a handshake. We'll blatantly disregard the whole pinging thing, because I don't really care. Clients still ping the server as it is. We'll see how it works out. '^USER (?P<user>[^\s]+) (?P<mode>[^\s]+) (?P<junk>[^\s]+) :(?P<name>.*)' :type data: str :type match: dict :type client: Client :type channels: list """ client.name = match['name'] client.host = match['host'] logger.info("Set new user's name to '{name}'", name=client.name) logger.info("Set new user's host to '{host}'", host=client.host) response = Protocol.handshake(client) return [line.format(nick=client.nick) for line in response]