-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathidleTime.py
198 lines (145 loc) · 5.76 KB
/
idleTime.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import teamspeak3
import time
import re
hostname = '192.168.1.10'
portnum = '10011'
username = 'IdleTime'
password = 'nUpFfgav'
serverID = '1'
nickname = 'IdleTime'
reconnect = True # if True it will try to reconnect if the connection is lost. False will terminate the program after connection lost.
servergroupSortID = '91115'
minIdleMinutes = 25
ts3 = teamspeak3.TeamSpeak3()
recon = 1 # this is set to reconnect's value after the first loop starts
# main program loop. Continously run until exited
def checkIdleClients(clientsList, serverGroupList):
'''
:type clientsList: Teamspeak3IdleClient[]
:type serverGroupList: Teamspeak3IdleServerGroup[]
'''
for client in clientsList:
# ignore self
if( client.name == username):
continue
# client is not idle
if(client.idle < minIdleMinutes * 60 * 1000):
# client is in idle group
for serverGroupID in client.returnServerGroupsClientIsIdleIn(serverGroupList):
ts3.executeCommand('servergroupdelclient sgid='+serverGroupID.id+' cldbid='+client.databaseid)
serverGroupID.clients.remove(client.databaseid)
# client is idle
else:
timeIdleObj = IdleTime(client.idle)
clientsServerGroups = client.returnServerGroupsClientIsIdleIn(serverGroupList)
# client is in idle group
if(len(clientsServerGroups) > 0):
# rename the server group he is in if it needs to be renamed. Do not reduce the time.
idlename = timeIdleObj.toString()
if(timeIdleObj.compareToString(clientsServerGroups[0].name) > 0):
ts3.executeCommand('servergrouprename sgid='+clientsServerGroups[0].id+' name=' + ts3.escapeString(idlename))
clientsServerGroups[0].name = idlename
#client is not in idle group
else:
# create new group and add client to it
ts3.executeCommand('servergroupadd name=' + ts3.escapeString(timeIdleObj.toString()))
errorCode = ts3.getErrorCode()
# Server group add was successful
if (errorCode == '0'):
serverGroupID = ts3.parseLastMsg()[0].get('sgid')
# if same server group name exists, search through the groups to find the same named one
elif(errorCode == '1282'):
for serverGroupToAddTo in serverGroupList:
if(timeIdleObj.toString() == serverGroupToAddTo.name):
serverGroupID = serverGroupToAddTo.id
ts3.executeCommand('servergroupaddperm sgid='+serverGroupID+' permsid=i_group_sort_id permvalue='+servergroupSortID+' permnegated=0 permskip=0|permsid=i_group_show_name_in_tree permvalue=2 permnegated=0 permskip=0')
ts3.executeCommand('servergroupaddclient sgid='+serverGroupID+' cldbid=' + client.databaseid)
return
def removeOfflineClients(clients, idleServerGroups):
'''
:type clientsList: list[Teamspeak3IdleClient]
:type serverGroupList: list[Teamspeak3IdleServerGroup]
'''
for serverGroup in idleServerGroups:
for sgClient in serverGroup.clients:
clientIsOnline = False
for onlineClient in clients:
if(onlineClient.databaseid == sgClient and onlineClient.name != nickname):
clientIsOnline = True
if(clientIsOnline == False):
ts3.executeCommand('servergroupdelclient sgid='+serverGroup.id+' cldbid='+sgClient)
class IdleTime:
seconds = 0
minutes = 0
hours = 0
def __init__(self, ms):
ms = int(ms)
self.seconds = int(ms / 1000)
self.minutes = int(self.seconds / 60)
self.hours = int(self.minutes / 60)
self.seconds = self.seconds % 60
self.minutes = self.minutes % 60
self.hours = self.hours % 60
def toString(self):
returnTime = []
if(self.hours > 0):
returnTime.append(str(self.hours) + ' Hr' + ('' if self.hours == 1 else 's'))
returnTime.append(str(self.minutes) + ' Min' + ('' if self.minutes == 1 else 's'))
return ' '.join(returnTime) + ' Idle'
def compareMinAccuracy(self, otherIdleTime):
'''
Compares to another IdleTime object
:param otherIdleTime: IdleTime object to compare to.
:type otherIdleTime: IdleTime
:return: Positive if self is greater, zero if equal, and negative if self is less than.
'''
return (self.minutes - otherIdleTime.minutes) + ( 60 * (self.hours - otherIdleTime.hours) )
def compareToString(self, otherIdleString):
'''
Compares to another IdleTime object that is in string format
:param otherIdleString: Idletime that was converted toString()
:type otherIdleString: string
:return: Positive if self is greater, zero if equal, and negative if self is less than.
'''
hrSearch = re.search('(\d+) Hr', otherIdleString)
if(hrSearch != None):
otherHours = int(hrSearch.group(1))
else:
otherHours = 0
minSearch = re.search('(\d+) Min', otherIdleString)
if (minSearch != None):
otherMinutes = int(minSearch.group(1))
else:
otherMinutes = 0
return (self.minutes - otherMinutes) + (60 * (self.hours - otherHours))
while(recon):
recon = reconnect
ts3.verbose(False)
try:
print("Connecting to server.")
ts3.connect(hostname, portnum)
ts3.login(username, password, serverID, nickname)
print("Connection successful! IdleTime is now running...")
while(ts3.connected()):
try:
# Grab list of clients
clients = ts3.getClientIdleList()
# Grab list of idle server groups and delete empty ones
idleServerGroups = ts3.getServerGroupbySortID(servergroupSortID)
# Check each client to see if they're idle, then add them to a sever group if they are, or rename a server group they are already in. If they aren't idle, remove them from the group.
checkIdleClients(clients, idleServerGroups)
# Remove offine clients from server groups
removeOfflineClients(clients, idleServerGroups)
time.sleep(2)
except ValueError as err:
print("ValueError," , err.args)
except ValueError as err:
print("ValueError," , err.args)
ts3.disconnect()
except:
print('Unidentified error. Disconnecting.')
ts3.disconnect()
if(recon):
print('Reconnecting in 30 seconds')
time.sleep(30)
ts3.disconnect()