Exemplo n.º 1
0
	def irc_MODE(self, prefix, params):
		"""
		Parse a server mode change message.
		"""
		channel, modes, args = params[0], params[1], params[2:]

		if modes[0] not in '-+':
			modes = '+' + modes

		if channel == self.nickname:
			# This is a mode change to our individual user, not a channel mode
			# that involves us.
			paramModes = self.getUserModeParams()
			# take note of our prefix! (for message length calculation
			self.prefixlen = len(prefix)
		else:
			paramModes = self.getChannelModeParams()

		try:
			added, removed = parseModes(modes, args, paramModes)
		except IRCBadModes:
			log.err(None, 'An error occured while parsing the following '
						  'MODE message: MODE %s' % (' '.join(params),))
		else:
			nick, ident, host = processHostmask(prefix)
			if self.state and (channel != self.nickname):
				self.state._modechange(channel, nick, added, removed)
			self.dispatch(self, "modeChanged", prefix=prefix, params=params, hostmask=prefix, target=channel,
				added=added, removed=removed, modes=modes, args=args, nick=nick, ident=ident, host=host)
Exemplo n.º 2
0
	def irc_MODE(self, prefix, params):
		"""
		Parse a server mode change message.
		"""
		channel, modes, args = params[0], params[1], params[2:]

		if modes[0] not in '-+':
			modes = '+' + modes

		if channel == self.nickname:
			# This is a mode change to our individual user, not a channel mode
			# that involves us.
			paramModes = self.getUserModeParams()
			# take note of our prefix! (for message length calculation
			self.prefixlen = len(prefix)
		else:
			paramModes = self.getChannelModeParams()

		try:
			added, removed = parseModes(modes, args, paramModes)
		except IRCBadModes:
			log.err(None, 'An error occured while parsing the following '
						  'MODE message: MODE %s' % (' '.join(params),))
		else:
			nick, ident, host = processHostmask(prefix)
			if self.state and (channel != self.nickname):
				self.state._modechange(channel, nick, added, removed)
			self.dispatch(self, "modeChanged", prefix=prefix, params=params, hostmask=prefix, target=channel,
				added=added, removed=removed, modes=modes, args=args, nick=nick, ident=ident, host=host)
Exemplo n.º 3
0
    def got_sjoin(self, lp, suffix):
        src = self.findsrc(lp[0][1:])
        (ts, name) = (int(lp[2]), lp[3])

        modes = lp[4]  ### modes surely aren't in the proper format here
        if len(lp) > 5:
            args = lp[5:]
        else:
            args = []
        uids = suffix.split()

        h = self.state.chans.get(name.lower(), None)

        if h:
            if (ts < h.ts):
                # Oops. One of our clients joined a preexisting but split channel
                # and now the split's being healed. Time to do the TS change dance!
                h.tschange(ts, modes)

            elif (ts == h.ts):
                # Merge both sets of modes, since this is 'the same' channel.
                paramModes = h.getModeParams(self.factory.supports)
                try:
                    added, removed = parseModes(modes, args, paramModes)
                except IRCBadModes, msg:
                    print 'An error occured (%s) while parsing the following TMODE message: MODE %s' % (
                        msg, ' '.join(lp))
                else:
                    h._modeChanged(src, h, added, removed)

            elif (ts > h.ts):
                # Disregard incoming modes altogether; just use their client list.
                # The far side will take care of kicking remote splitriders if need
                # be.
                pass
Exemplo n.º 4
0
    def got_sjoin(self, lp, suffix):
        src = self.findsrc(lp[0][1:])
        (ts, name) = (int(lp[2]), lp[3])

        modes = lp[4]  ### modes surely aren't in the proper format here
        if len(lp) > 5:
            args = lp[5:]
        else:
            args = []
        uids = suffix.split(' ')

        h = self.state.chans.get(name.lower(), None)

        if h:
            if (ts < h.ts):
                # Oops. One of our clients joined a preexisting but split channel
                # and now the split's being healed. Time to do the TS change dance!
                h.tschange(ts, modes)

            elif (ts == h.ts):
                # Merge both sets of modes, since this is 'the same' channel.
                paramModes = h.getModeParams(self.factory.supports)
                try:
                    added, removed = parseModes(modes, args, paramModes)
                except IRCBadModes, msg:
                    print 'An error occured (%s) while parsing the following TMODE message: MODE %s' % (msg, ' '.join(lp))
                else:
                    h._modeChanged(src, h, added, removed)

            elif (ts > h.ts):
                # Disregard incoming modes altogether; just use their client list.
                # The far side will take care of kicking remote splitriders if need
                # be.
                pass
Exemplo n.º 5
0
    def irc_RPL_CHANNELMODEIS(self, prefix, params):
        """
		Parse a RPL_CHANNELMODEIS message.
		"""
        channel, modes, args = params[1], params[2], params[3:]

        if modes[0] not in '-+':
            modes = '+' + modes
        try:
            added, _ = parseModes(modes, args, self.getChannelModeParams())
        except IRCBadModes:
            log.err(
                None, 'An error occured while parsing the following '
                'MODE message: MODE %s' % (' '.join(params), ))
        else:
            if self.state:
                self.state._modechange(channel, None, added, [])
            self.dispatch(self,
                          "channelModeIs",
                          prefix=prefix,
                          params=params,
                          hostmask=prefix,
                          target=channel,
                          added=added,
                          modes=modes,
                          args=args)
Exemplo n.º 6
0
    def set_modes(self, channel, modes, args):
        try:
            chan = self._channels[channel]
        except KeyError:
            self.logger.error(f"Can't set mode for unknown channel: {channel}")
            return

        # parse mode changes out into added and removed
        try:
            added, removed = irc.parseModes(modes, args,
                                            self._twisted_param_modes)
        except KeyError:
            self.logger.error(
                "Error parsing modes for {channel}: {modes}".format(
                    channel=channel, modes=modes + " " + ' '.join(args)))
            return

        # set modes
        for mode, param in added:
            if self.chanmodes.get(mode) == "addressParam":
                chan.modes[mode] = chan.modes.get(mode, []).append(param)

            elif self.chanmodes.get(mode) in ("param", "setParam"):
                chan.modes[mode] = param

            else:  # noParam
                if param is not None:
                    self.logger.error(
                        "Mode '{mode}' should not have a param".format(
                            mode=mode))
                    continue

                chan.modes[mode] = param

        # unset modes
        for mode, param in removed:
            # ignore unset modes
            if mode not in chan.modes:
                self.logger.error(
                    "Cannot set unset mode '{mode}'".format(mode=mode))
                continue

            if self.chanmodes.get(mode) == "addressParam":
                chan.modes[mode] = chan.modes[mode].remove(param)

            elif self.chanmodes.get(mode) == "param":
                if chan.modes[mode] != param:
                    self.logger.error(
                        "Mode '{mode}' param ({param}) does not match "
                        "set param ({set_param})".format(
                            mode=mode,
                            param=param,
                            set_param=chan.modes[param]))
                    continue
                del chan.modes[mode]

            else:  # setParam or noParam
                del chan.modes[mode]
Exemplo n.º 7
0
 def irc_RPL_CHANNELMODEIS(self, prefix, params):
     channel = params[1]
     modes = params[2]
     modeparams = params[3:]
     added, removed = irc.parseModes(modes, modeparams, self.getChannelModeParams())
     for mode, arg in added:
         self.modeChanged(None, channel, True, mode, (arg,))
     for mode, arg in removed:
         self.modeChanged(None, channel, False, mode, (arg,))
Exemplo n.º 8
0
 def irc_RPL_CHANNELMODEIS(self, prefix, params):
     channel = params[1]
     modes = params[2]
     modeparams = params[3:]
     added, removed = irc.parseModes(modes, modeparams,
                                     self.getChannelModeParams())
     for mode, arg in added:
         self.modeChanged(None, channel, True, mode, (arg, ))
     for mode, arg in removed:
         self.modeChanged(None, channel, False, mode, (arg, ))
Exemplo n.º 9
0
 def got_mode(self, lp, suffix):
     src = self.findsrc(lp[0][1:])
     params = suffix.split(' ')
     modes, args = params[0], params[1:]
     if lp[2][0] == '#':
         dest = self.state.chans[lp[2]]
     else:
         dest = self.state.Client(lp[2])
     paramModes = dest.getModeParams(self.factory.supports)
     try:
         added, removed = parseModes(modes, args, paramModes)
     except IRCBadModes:
         print 'An error occured while parsing the following MODE message: MODE %s :%s' % (lp[3], modes)
     else:
         dest._modeChanged(src, dest, added, removed)
Exemplo n.º 10
0
 def got_mode(self, lp, suffix):
     src = self.findsrc(lp[0][1:])
     params = suffix.split(' ')
     modes, args = params[0], params[1:]
     if lp[2][0] == '#':
         dest = self.state.chans[lp[2]]
     else:
         dest = self.state.Client(lp[2])
     paramModes = dest.getModeParams(self.factory.supports)
     try:
         added, removed = parseModes(modes, args, paramModes)
     except IRCBadModes:
         print 'An error occured while parsing the following MODE message: MODE %s :%s' % (
             lp[3], modes)
     else:
         dest._modeChanged(src, dest, added, removed)
Exemplo n.º 11
0
 def got_tmode(self, lp, suffix):
     modes, al = lp[4], lp[5:]
     args = [a for a in al if a]
     src = self.findsrc(lp[0][1:])
     ts = int(lp[2])
     dest = self.state.Channel(lp[3])
     # We have to discard higher-TS TMODEs because they come from a newer
     # version of the channel.
     if ts > dest.ts:
         print 'TMODE: ignoring higher TS mode %s to %s from %s (%d > %d)' % (
               modes, dest, src, ts, dest.ts)
         return
     paramModes = dest.getModeParams(self.factory.supports)
     try:
         added, removed = parseModes(modes, args, paramModes)
     except IRCBadModes, msg:
         print 'An error occured (%s) while parsing the following TMODE message: MODE %s' % (msg, ' '.join(lp))
Exemplo n.º 12
0
	def irc_RPL_CHANNELMODEIS(self, prefix, params):
		"""
		Parse a RPL_CHANNELMODEIS message.
		"""
		channel, modes, args = params[1], params[2], params[3:]

		if modes[0] not in '-+':
			modes = '+' + modes
		try:
			added, _ = parseModes(modes, args, self.getChannelModeParams())
		except IRCBadModes:
			log.err(None, 'An error occured while parsing the following '
						  'MODE message: MODE %s' % (' '.join(params),))
		else:
			if self.state:
				self.state._modechange(channel, None, added, [])
			self.dispatch(self, "channelModeIs", prefix=prefix, params=params, hostmask=prefix, target=channel,
				added=added, modes=modes, args=args)
Exemplo n.º 13
0
 def got_tmode(self, lp, suffix):
     modes, al = lp[4], lp[5:]
     args = [a for a in al if a]
     src = self.findsrc(lp[0][1:])
     ts = int(lp[2])
     dest = self.state.Channel(lp[3])
     # We have to discard higher-TS TMODEs because they come from a newer
     # version of the channel.
     if ts > dest.ts:
         print 'TMODE: ignoring higher TS mode %s to %s from %s (%d > %d)' % (
             modes, dest, src, ts, dest.ts)
         return
     paramModes = dest.getModeParams(self.factory.supports)
     try:
         added, removed = parseModes(modes, args, paramModes)
     except IRCBadModes, msg:
         print 'An error occured (%s) while parsing the following TMODE message: MODE %s' % (
             msg, ' '.join(lp))
         print 'modes: %s  args: %s - paramModes: %s' % (modes, args,
                                                         paramModes)
Exemplo n.º 14
0
    def irc_RPL_CHANNELMODEIS(self, prefix, params):
        channel, modes, args = params[1], params[2], params[3:]

        if modes[0] not in '-+':
            modes = '+' + modes

        if len(modes) < 2:
            # Avoid trying to set channels without any modes set,
            # easy way to get an unnecessary exception.
            return

        paramModes = self.getChannelModeParams()

        try:
            added, _ = irc.parseModes(modes, args, paramModes)
        except irc.IRCBadModes:
            log.err(
                None,
                'An error occurred while parsing the following MODE message: MODE {}'
                .format(' '.join(params)))
        else:
            modes, params = zip(*added)
            self.modeChanged(None, channel, True, ''.join(modes), params)
Exemplo n.º 15
0
    def on_mode(self, bot, command, prefix, params):
        channel, modes, args = params[0], params[1], params[2:]

        if modes[0] not in '-+':
            modes = '+' + modes

        if channel == bot.connection.nickname:
            # This is a mode change to our individual user, not a channel mode
            # that involves us.
            paramModes = bot.connection.getUserModeParams()
        else:
            paramModes = bot.connection.getChannelModeParams()

        added, removed = parseModes(modes, args, paramModes)

        if added:
            for mode, nick in added:
                if nick:
                    try:
                        user = bot.users.getUser(nick)

                        if not mode in user.channels[channel].modes:
                            user.channels[channel].modes += mode
                    except KeyError:
                        pass

        if removed:
            for mode, nick in removed:
                if nick:
                    try:
                        user = bot.users.getUser(nick)

                        if mode in user.channels[channel].modes:
                            newmodes = user.channels[channel].modes.replace(mode, "")
                            user.channels[channel].modes = newmodes
                    except KeyError:
                        pass