Advertisement
Guest User

wideban

a guest
Oct 18th, 2014
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.48 KB | None | 0 0
  1. #
  2. # BigBrotherBot(B3) (www.bigbrotherbot.com)
  3. # Plugin for extra authentication of privileged users dynamically
  4. # Copyright (C) 2010 Stephen LARROQUE (lrq3000@gmail.com) aka GrosBedo
  5. #
  6. # Description :
  7. # This plugin basically fills the login and password fields in the database with !regaccount
  8. # and when you issue a !loginaccount, it gives you the privileges associated with your old account, by either duplicating them, or either by merging your new account with the old one ( by replacing your old IP and GUID with your current one, and place your current name as an alias, so you get your privileges back while permitting the tracking of the alias).
  9. #
  10. # This program is free software; you can redistribute it and/or modify
  11. # it under the terms of the GNU General Public License as published by
  12. # the Free Software Foundation; either version 2 of the License, or
  13. # (at your option) any later version.
  14. #
  15. # This program is distributed in the hope that it will be useful,
  16. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. # GNU General Public License for more details.
  19. #
  20. # You should have received a copy of the GNU General Public License
  21. # along with this program; if not, write to the Free Software
  22. # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  23. #
  24. # Changelog:
  25. #
  26. # 2010-08-21 - v0.1 - GrosBedo
  27. # - first public release.
  28. # 2010-08-21 - v0.1.1 - GrosBedo
  29. # - fixed a possible bug with empty guid or pbid.
  30. # 2010-08-21 - v0.1.2 - GrosBedo
  31. # - optimized the algorithm, no more action queue list overflow
  32. # - attempts are now saved in the db as new penalty (with the new client id, ip, guid and pbid)
  33. # 2010-09-14 - v0.1.3 - GrosBedo
  34. # - fixed a bug with empty fields
  35. # - register on authentication rather than on client connection, should avoid some unrecognized player bugs
  36. #
  37.  
  38. __author__    = 'GrosBedo'
  39. __version__ = '0.1.3'
  40. __date__ = '2010-09-14'
  41.  
  42. # Version = major.minor.patches
  43.  
  44. import b3
  45. import b3.events
  46. import b3.plugin
  47. from b3 import clients
  48. from b3.querybuilder import QueryBuilder
  49.  
  50. class WidebanPlugin(b3.plugin.Plugin):
  51.  
  52.     def onLoadConfig(self):
  53.         try:
  54.             self.banbyip = self.config.getboolean('settings', 'banbyip')
  55.         except:
  56.             self.banbyip = 0
  57.             self.debug('Using default value (%i) for settings::banbyip', self.banbyip)
  58.         try:
  59.             self.banbyguid = self.config.getboolean('settings', 'banbyguid')
  60.         except:
  61.             self.banbyguid = 0
  62.             self.debug('Using default value (%i) for settings::banbyguid', self.banbyguid)
  63.         try:
  64.             self.banbypbid = self.config.getboolean('settings', 'banbypbid')
  65.         except:
  66.             self.banbypbid = 0
  67.             self.debug('Using default value (%i) for settings::banbypbid', self.banbypbid)
  68.         try:
  69.             self.publicmsg = self.config.get('settings', 'publicmsg')
  70.         except:
  71.             self.publicmsg = 0
  72.             self.debug('Using default value (%i) for settings::publicmsg', self.publicmsg)
  73.         return
  74.  
  75.  
  76.     def onStartup(self):
  77.         # Hook to each client's connection
  78.         self.registerEvent(b3.events.EVT_CLIENT_AUTH)
  79.  
  80.     def onEvent(self, event):
  81.         if (event.type == b3.events.EVT_CLIENT_AUTH):
  82.             self.debug('Checking for a wide ban for user %s named %s ...' % (event.client.id, event.client.name))
  83.             # Fetching all BAN penalties or penalties for an undetermined amount of time (time_expire = -1) and join the client infos, and the infos of the admin who banned the client
  84.             whereclient = []
  85.             if self.banbyip:
  86.                 if len(event.client.ip) > 0 and event.client.ip is not None and event.client.ip != 'None':
  87.                     whereclient.append('c.ip = \'' + event.client.ip + '\'')
  88.  
  89.             if self.banbyguid:
  90.                 if len(event.client.guid) > 0 and event.client.guid is not None and event.client.guid != 'None':
  91.                     whereclient.append('c.guid = \'' + event.client.guid + '\'')
  92.             if self.banbypbid:
  93.                 if len(event.client.pbid) > 0 and event.client.pbid is not None and event.client.pbid != 'None':
  94.                     whereclient.append('c.pbid = \'' + event.client.pbid + '\'')
  95.                
  96.                 if len(whereclient) > 0:
  97.                     whereclient = ' or '.join(whereclient)
  98.                     whereclient = ' and (' + whereclient + ')'
  99.             else:
  100.                 whereclient = ''
  101.  
  102.             q = "SELECT p.type as type, p.time_expire as time_expire, c.id as id, c.name as name, c.ip as ip, c.guid as guid, c.pbid as pbid, p.admin_id as admin_id, a.name as admin_name, p.reason as reason FROM penalties as p JOIN clients as c ON c.id = p.client_id LEFT JOIN clients as a ON a.id = p.admin_id WHERE (p.type = 'Ban' or p.time_expire = -1)" + whereclient
  103.             self.debug(q)
  104.             cursor = self.console.storage.query(q)
  105.  
  106.             if cursor and not cursor.EOF:
  107.                 r = cursor.getOneRow()
  108.                 # We compare each row to the current user : if the ip, or guid, or pbid match, then this means that this user is probably faking his identity, and we then reban him with his new one
  109.                 self.info('Player %s id %i with ip %s and guid %s tried to connect to the server while he is banned for reason: %s' % (event.client.name, event.client.id, event.client.ip, event.client.guid, r['reason']))
  110.                 # Try to kick/ban user
  111.                 try:
  112.                     # Try with the standard way, with the client methods, so we can save the penalties in the database
  113.                     event.client.kick(r['reason'], None, event.client)
  114.                     event.client.ban(r['reason'], None, event.client)
  115.                     event.client.save()
  116.                 except:
  117.                     # If we can't, then we use the console wide methods, but new penalty won't be saved
  118.                     self.console.kick(event.client, r['reason'], None, True)
  119.                     self.console.ban(event.client, r['reason'], None, True)
  120.                 # Disconnect the user from the current memory of B3
  121.                 self.console.queueEvent(b3.events.Event(b3.events.EVT_CLIENT_BAN, r['reason'], event.client))
  122.                 event.client.disconnect()
  123.  
  124.                 # If you want to show a public message, to show that you don't ban for nothing...
  125.                 if len(self.publicmsg)  > 0:
  126.                     if r['admin_id'] == 0 or r['admin_name'] == None:
  127.                         admin_name = 'SYSTEM'
  128.                     else:
  129.                         admin_name = r['admin_name']
  130.  
  131.                     variables = { # converting them into variables to later include them in user's custom talk_msg
  132.                         'name' : event.client.name,
  133.                         'alias' : r['name'],
  134.                         'ip' : event.client.ip,
  135.                         'id' : event.client.id,
  136.                         'guid' : event.client.guid,
  137.                         'pbid' : event.client.pbid,
  138.                         'adminname' : admin_name,
  139.                         'reason' : r['reason']
  140.                     }
  141.                     message = b3.functions.vars2printf( self.publicmsg ) # preparing publicmsg to include variables
  142.                     self.console.say(message % variables) # saying the ban message with the format
  143.  
  144.                 # Since we re-banned the client, now we can break the database check
  145.                 return
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement