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.
    """

    # get the admin plugin so we can issue kicks etc.
    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

    self.gi = GeoIP.open(self.cf_geoipdat_path, GeoIP.GEOIP_STANDARD)
    self.registerEvent(b3.events.EVT_CLIENT_AUTH)
    self.debug('Started')
    
        # 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.debug('Started')
 def onPlayerConnect(self, client):
   """\
   Examine players country and allow/deny to connect.
   """
   self.debug('Connecting slot: %s, %s, %s' % (client.cid, client.name, client.ip))
   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):
     if 0 < len(self.cf_allow_message) and (not self.isMessageExcludeFrom(countryCode)):
       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(': Your Country was REJECTED by B3 - CountryFilter')
   self.debug('Connecting done.')
  def onStartup(self):
    """\
    Create a new GeoIP object and register callback functions.
    """
    self.verbose('Loading config')
    self.cf_country_print_mode = self.config.get('settings', 'cf_country_print_mode')
    self.cf_allow_message = self.config.get('messages', 'cf_allow_message')
    self.cf_deny_message = self.config.get('messages', 'cf_deny_message')
    self.cf_message_exclude_from = self.config.get('settings', 'cf_message_exclude_from')
    self.cf_order = self.config.get('settings', 'cf_order')
    self.cf_deny_from = self.config.get('settings', 'cf_deny_from')
    self.cf_allow_from = self.config.get('settings', 'cf_allow_from')
    self.cf_geoipdat_path = self.config.get('settings', 'cf_geoipdat_path')
    
    # get the admin plugin so we can issue kicks etc.
    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
    # correction for pathing errors on win32
    self.debug('sys.platform = %s and os.cwd = %s' % (sys.platform, os.getcwd()))
    # if sys.platform == 'win32':
    self.gi = GeoIP.open(self.cf_geoipdat_path, GeoIP.GEOIP_STANDARD)
    self.registerEvent(b3.events.EVT_CLIENT_AUTH)
    self.debug('Started')
    
        # 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.debug('Started')
  def cmd_playerinfo(self, data, client, cmd=None):
    """\
    <player> - Find a player info
    """

    input = self._adminPlugin.parseUserCmd(data)
    if input[0] == '':
      cmd.sayLoudOrPM(client,'Incorrect player searched')
      return True
    elif input[0]:
      # 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:
      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 (%s) ^7Guid: ^1%s ^9Country: ^1%s ^7ip: ^1%s ^7Level: ^1%s' % (sclient.exactName, str(sclient.id), str(sclient.guid), str(country), str(sclient.ip), str(sclient.maxLevel)))