def idToCountry(self, countryId):
     """\
     Convert country id to country representation.
     """
     if 'code3' == self.cf_country_print_mode:
         return GeoIP.id_to_country_code3(countryId)
     elif 'name' == self.cf_country_print_mode:
         return GeoIP.id_to_country_name(countryId)
     else:  # 'code' (default)
         return GeoIP.id_to_country_code(countryId)
    def onStartup(self):
        """\
        Create a new GeoIP object and register callback functions.
        :rtype : object
        """

        # get the admin plugin so we can register commands, issue kicks and such
        self._adminPlugin = self.console.getPlugin('admin')
        if not self._adminPlugin:
            # something is wrong, can't start without admin plugin
            self.error('Could not find admin plugin')
            return False

        # register our commands
        if 'commands' in self.config.sections():
            for cmd in self.config.options('commands'):
                level = self.config.get('commands', cmd)
                sp = cmd.split('-')
                alias = None
                if len(sp) == 2:
                    cmd, alias = sp

                func = self.getCmd(cmd)
                if func:
                    self._adminPlugin.registerCommand(self, cmd, level, func, alias)

        self.gi = GeoIP.open(self.cf_geoipdat_path, GeoIP.GEOIP_STANDARD)
        if self.console.gameName in self._frostBiteGameNames:
            self.registerEvent(b3.events.EVT_PUNKBUSTER_NEW_CONNECTION)
        else:
            self.registerEvent(b3.events.EVT_CLIENT_AUTH)
        self.debug('Started')
 def onPlayerConnect(self, client):
     """\
     Examine players country and allow/deny to connect.
     """
     self.debug(
         'Checking player: %s, name: %s, ip: %s, level: %s' % (client.cid, client.name, client.ip, client.maxLevel))
     countryId = self.gi.id_by_addr(str(client.ip))
     countryCode = GeoIP.id_to_country_code(countryId)
     country = self.idToCountry(countryId)
     self.debug('Country: %s' % country)
     if self.isAllowConnect(countryCode, client):
         if (0 < len(self.cf_allow_message) and (
                 not self.isMessageExcludeFrom(countryCode))) and client.guid and self.console.upTime() > 300:
             message = self.getMessage('cf_allow_message', {'name': client.name, 'country': country})
             self.console.say(message)
         pass  # do nothing
     else:
         if 0 < len(self.cf_deny_message) and (not self.isMessageExcludeFrom(countryCode)):
             message = self.getMessage('cf_deny_message', {'name': client.name, 'country': country})
             self.console.say(message)
         client.kick(silent=True)
     self.debug('Checking done.')
    def cmd_cfcountry(self, data, client, cmd=None):
        """\
        <player> - What country is this player from?
        """
        # this will split the player name and the message (oops, no message...)
        _input = self._adminPlugin.parseUserCmd(data)
        if _input:
            # input[0] is the player id
            sclient = self._adminPlugin.findClientPrompt(_input[0], client)
            if not sclient:
                # a player matchin the name was not found, a list of closest matches will be displayed
                # we can exit here and the user will retry with a more specific player
                return False
        else:
            client.message('^7Invalid or missing data, try !help cfcountry')
            return False

        # are we still here? Let's get the country
        countryId = self.gi.id_by_addr(str(sclient.ip))
        countryCode = GeoIP.id_to_country_code(countryId)
        country = self.idToCountry(countryId)
        cmd.sayLoudOrPM(client, '^1%s^7 is connecting from ^1%s (%s)^7' % (sclient.name, country, countryCode))

        return True