DotaNoobs main site.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

201 lines
9.5 KiB

  1. import os
  2. os.chdir(os.path.abspath(os.path.split(__file__)[0]))
  3. activate_this = os.path.join(os.path.split(__file__)[0], 'venv', 'bin', 'activate_this.py')
  4. execfile(activate_this, dict(__file__=activate_this))
  5. import ts3
  6. from datetime import datetime, timedelta
  7. from app import app, db, models
  8. def idle_mover(server, cfg):
  9. exempt_cids = []
  10. permid_response = server.send_command('permidgetbyname', keys={'permsid': 'i_channel_needed_join_power'})
  11. if permid_response.is_successful:
  12. join_permid = permid_response.data[0]['permid']
  13. def exempt_check(cid):
  14. # check flags
  15. flag_response = server.send_command('channelinfo', keys={'cid': cid})
  16. if flag_response.is_successful:
  17. if flag_response.data[0]['channel_needed_talk_power'] != '0': return True
  18. permid_response = server.send_command('channelpermlist', keys={'cid': cid})
  19. if permid_response.is_successful:
  20. for perm in permid_response.data:
  21. if perm['permid'] == join_permid and perm['permvalue'] != '0': return True
  22. return False
  23. list_response = server.send_command('channellist')
  24. if list_response.is_successful:
  25. for channel in list_response.data:
  26. if exempt_check(channel['cid']):
  27. exempt_cids.append(channel['cid'])
  28. # get destination
  29. response = server.send_command('channelfind', keys={'pattern': 'AFK'})
  30. if response.is_successful:
  31. afk_channel = response.data[0]
  32. # Get list of clients
  33. clientlist = server.send_command('clientlist', opts=['times']).data
  34. for client in clientlist:
  35. clientinfo = server.send_command('clientinfo', {'clid':client['clid']})
  36. if clientinfo.is_successful:
  37. client['client_unique_identifier'] = clientinfo.data[0]['client_unique_identifier']
  38. else:
  39. raise UserWarning('Could not find the clientinfo for %s' % client['clid'])
  40. # move idlers to afk channel
  41. for client in clientlist:
  42. if( int(client['client_idle_time']) > cfg['TS3_MAX_IDLETIME']):
  43. if client['cid'] not in exempt_cids:
  44. # Have TeamSpeak move AFK user to appropriate channel
  45. server.send_command('clientmove', keys={'clid': client['clid'], 'cid': afk_channel['cid']})
  46. def store_active_data(server, cfg):
  47. # Get exempt channels (AFK, passworded, join powers)
  48. exempt_cids = []
  49. permid_response = server.send_command('permidgetbyname', keys={'permsid': 'i_channel_needed_join_power'})
  50. if permid_response.is_successful:
  51. join_permid = permid_response.data[0]['permid']
  52. def exempt_check(cid):
  53. # Check flags
  54. flag_response = server.send_command('channelinfo', keys={'cid': cid})
  55. if flag_response.is_successful:
  56. if flag_response.data[0]['channel_flag_password'] != '0': return True
  57. if flag_response.data[0]['channel_needed_talk_power'] != '0': return True
  58. permid_response = server.send_command('channelpermlist', keys={'cid': cid})
  59. if permid_response.is_successful:
  60. for perm in permid_response.data:
  61. if perm['permid'] == join_permid and perm['permvalue'] != '0': return True
  62. return False
  63. list_response = server.send_command('channellist')
  64. if list_response.is_successful:
  65. for channel in list_response.data:
  66. if exempt_check(channel['cid']):
  67. exempt_cids.append(channel['cid'])
  68. # Get list of clients
  69. clientlist = server.send_command('clientlist', opts=("country",)).data
  70. # Remove the server_query and afk/moderated clients
  71. clientlist = filter(lambda client: client['client_type'] == '0' and client['cid'] not in exempt_cids, clientlist)
  72. # Compile the important information
  73. clients = {}
  74. for client in clientlist:
  75. clientinfo = server.send_command('clientdbinfo', {'cldbid': client['client_database_id']})
  76. if clientinfo.is_successful:
  77. clients[clientinfo.data[0]['client_unique_identifier']] = {'country': client['client_country']}
  78. else:
  79. raise UserWarning('Could not find the clientdbinfo for %s' % client['client_database_id'])
  80. # Update the data
  81. tsdata = models.TeamspeakData(clients)
  82. db.session.add(tsdata)
  83. db.session.commit()
  84. def process_ts3_events(server, cfg):
  85. # Get list of clients
  86. clientlist = server.clientlist()
  87. for clid, client in clientlist.iteritems():
  88. clientinfo = server.send_command('clientinfo', {'clid':clid})
  89. if clientinfo.is_successful:
  90. client['client_unique_identifier'] = clientinfo.data[0]['client_unique_identifier']
  91. else:
  92. raise UserWarning('Could not find clientinfo for %s' % clid)
  93. # Process any active events
  94. for clid, client in clientlist.iteritems():
  95. u = models.User.query.filter_by(teamspeak_id=client['client_unique_identifier']).first()
  96. e = models.Event.query.filter(models.Event.start_time <= datetime.utcnow(), models.Event.end_time > datetime.utcnow()).all()
  97. if u and e:
  98. for event in e:
  99. if client['cid'] in event.cids:
  100. event.add_participant(u)
  101. # Add channels for upcoming events
  102. e = models.Event.query.filter(models.Event.start_time >= datetime.utcnow(), \
  103. models.Event.start_time <= (datetime.utcnow() + timedelta(minutes=60))).all()
  104. for event in e:
  105. if not event.cids:
  106. print("Adding channels for event {}".format(event.name))
  107. event.create_channels()
  108. # Remove channels for expired events
  109. e = models.Event.query.filter(models.Event.start_time > (datetime.utcnow() - timedelta(hours=24)), \
  110. models.Event.end_time < (datetime.utcnow() - timedelta(minutes=60))).all()
  111. for event in e:
  112. current_time = datetime.utcnow()
  113. remove_time = event.end_time + timedelta(minutes=60)
  114. warn_time = event.end_time + timedelta(minutes=30)
  115. time_left = remove_time - current_time
  116. message = "This event channel is temporary and will be removed in {} minutes.".format(divmod(time_left.days * 86400 + time_left.seconds, 60)[0])
  117. if event.cids:
  118. if current_time > remove_time:
  119. print("Removing channels for event: {}".format(event.name))
  120. event.remove_channels()
  121. elif current_time > warn_time:
  122. for cid in event.cids:
  123. clients = [client for client in clientlist.values() if int(client['cid']) == int(cid)]
  124. for client in clients:
  125. print("Warning {} about expired event {}".format(client['client_nickname'], event.name))
  126. server.clientpoke(client['clid'], message)
  127. def award_idle_ts3_points(server, cfg):
  128. # Get exempt channels (AFK, passwords, join power)
  129. exempt_cids = []
  130. permid_response = server.send_command('permidgetbyname', keys={'permsid': 'i_channel_needed_join_power'})
  131. if permid_response.is_successful:
  132. join_permid = permid_response.data[0]['permid']
  133. def exempt_check(cid):
  134. # Check flags
  135. flag_response = server.send_command('channelinfo', keys={'cid':cid})
  136. if flag_response.is_successful:
  137. if flag_response.data[0]['channel_flag_password'] != '0': return True
  138. if flag_response.data[0]['channel_needed_talk_power'] !='0': return True
  139. permid_response = server.send_command('channelpermlist', keys={'cid': cid})
  140. if permid_response.is_successful:
  141. for perm in permid_response.data:
  142. if perm['permid'] == join_permid and perm['permvalue'] != '0': return True
  143. return False
  144. list_response = server.send_command('channellist')
  145. if list_response.is_successful:
  146. for channel in list_response.data:
  147. if exempt_check(channel['cid']):
  148. exempt_cids.append(channel['cid'])
  149. # Get list of clients
  150. clientlist = server.clientlist()
  151. for clid, client in clientlist.iteritems():
  152. clientinfo = server.send_command('clientinfo', {'clid': clid})
  153. if clientinfo.is_successful:
  154. client['client_unique_identifier'] = clientinfo.data[0]['client_unique_identifier']
  155. else:
  156. raise UserWarning('Could not find the clientinfo for %s' % clid)
  157. # Update the data
  158. active_users = set()
  159. for client in clientlist.values():
  160. with open('clientlist.txt', 'ab') as f:
  161. f.write(client['client_nickname']+'\n\r\t'+\
  162. client['client_unique_identifier']+'\n\r')
  163. if client['cid'] not in exempt_cids:
  164. doob = models.User.query.filter_by(teamspeak_id=client['client_unique_identifier']).first()
  165. if doob:
  166. doob.update_connection()
  167. active_users.add(doob)
  168. doobs = set(models.User.query.filter(models.User.ts3_starttime != None).all())
  169. print doobs, active_users
  170. for doob in doobs.intersection(active_users):
  171. print(doob.nickname)
  172. #doob.finalize_connection()
  173. if __name__ == "__main__":
  174. cfg = {}
  175. cfg = app.config
  176. server = ts3.TS3Server(cfg['TS3_HOST'], cfg['TS3_PORT'])
  177. server.login(cfg['TS3_USERNAME'], cfg['TS3_PASSWORD'])
  178. server.use(1)
  179. idle_mover(server, cfg)
  180. store_active_data(server, cfg)
  181. process_ts3_events(server, cfg)
  182. award_idle_ts3_points(server, cfg)