2010-08-21 15:32:22 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
# Copyright 2010 Maurizio Porrato <maurizio.porrato@gmail.com>
|
|
|
|
# See LICENSE.txt for copyright info
|
|
|
|
|
2010-08-21 18:32:56 +00:00
|
|
|
import shelve
|
2010-08-21 15:32:22 +00:00
|
|
|
from twisted.python import log
|
|
|
|
|
|
|
|
|
|
|
|
class ClientTracker(object):
|
|
|
|
|
2010-08-21 18:32:56 +00:00
|
|
|
def __init__(self, filename, cbClient, cbNet, cbMute, cbBlock, cbAdmin):
|
2010-08-21 15:32:22 +00:00
|
|
|
self._clientData = {}
|
|
|
|
self._net = {}
|
|
|
|
self._mute = {}
|
|
|
|
self._block = {}
|
|
|
|
self._admin = {}
|
|
|
|
self.cbClient = cbClient
|
|
|
|
self.cbNet = cbNet
|
|
|
|
self.cbMute = cbMute
|
|
|
|
self.cbBlock = cbBlock
|
|
|
|
self.cbAdmin = cbAdmin
|
2010-08-21 18:32:56 +00:00
|
|
|
self.filename = filename
|
|
|
|
self.load()
|
|
|
|
|
|
|
|
def load(self, filename=None):
|
|
|
|
s = shelve.open(filename or self.filename)
|
|
|
|
try:
|
|
|
|
self._mute = s['mute']
|
|
|
|
self._block = s['block']
|
|
|
|
self._admin = s['admin']
|
|
|
|
except KeyError: pass
|
|
|
|
s.close()
|
|
|
|
|
|
|
|
def save(self, filename=None):
|
|
|
|
s = shelve.open(filename or self.filename)
|
|
|
|
s['mute'] = self._mute
|
|
|
|
s['block'] = self._block
|
|
|
|
s['admin'] = self._admin
|
|
|
|
s.close()
|
2010-08-21 15:32:22 +00:00
|
|
|
|
2010-08-25 08:10:43 +00:00
|
|
|
def isLoggedIn(self, clientId):
|
|
|
|
return clientId in self._clientData
|
|
|
|
|
2010-08-21 15:32:22 +00:00
|
|
|
def getClient(self, clientId):
|
|
|
|
return self._clientData[clientId].user
|
|
|
|
|
|
|
|
def getClientProtocol(self, clientId):
|
|
|
|
return self._clientData[clientId]
|
|
|
|
|
|
|
|
def getClientIndex(self, clientId):
|
|
|
|
net = self.getClient(clientId).NT
|
|
|
|
l = [x.ID for x in self.getClientList(net)]
|
|
|
|
return l.index(clientId)+1
|
|
|
|
|
|
|
|
def getNetworkList(self):
|
|
|
|
return self._net.keys()
|
|
|
|
|
|
|
|
def getClientList(self, network=[]):
|
|
|
|
if network:
|
|
|
|
allClients = self._net.get(network, [])
|
|
|
|
else:
|
|
|
|
allClients = self._clientData.keys()
|
|
|
|
return [self._clientData[x].user
|
|
|
|
for x in allClients]
|
|
|
|
|
|
|
|
def getMuteList(self, network=[]):
|
|
|
|
if network:
|
|
|
|
return [x for x in self._mute.values()
|
|
|
|
if self._mute[x.ID].NT in network]
|
|
|
|
else:
|
|
|
|
return [x for x in self._mute.values()]
|
|
|
|
|
|
|
|
def getBlockList(self):
|
|
|
|
return self._block.values()
|
|
|
|
|
|
|
|
def getAdminList(self):
|
|
|
|
return self._admin.values()
|
|
|
|
|
|
|
|
def isMute(self, clientId):
|
|
|
|
return clientId in self._mute
|
|
|
|
|
|
|
|
def isBlocked(self, clientId):
|
|
|
|
return clientId in self._block
|
|
|
|
|
2010-08-28 17:54:25 +00:00
|
|
|
def isAdmin(self, clientId):
|
|
|
|
return clientId in self._admin
|
2010-08-21 15:32:22 +00:00
|
|
|
|
|
|
|
def login(self, client, status=0):
|
|
|
|
clientId = client.user.ID
|
|
|
|
if clientId not in self._clientData:
|
|
|
|
client.user.S = status
|
|
|
|
self._clientData[clientId] = client
|
|
|
|
net = client.user.NT
|
|
|
|
nc = self._net.get(net, [])
|
|
|
|
nc.append(clientId)
|
|
|
|
self._net[net] = nc
|
|
|
|
if clientId in self._mute:
|
|
|
|
client.user.M = 1
|
|
|
|
a = self._mute[clientId].AI
|
2010-08-21 18:32:56 +00:00
|
|
|
self._mute[clientId].update(**client.user.dict())
|
2010-08-21 15:32:22 +00:00
|
|
|
self._mute[clientId].AI = a
|
|
|
|
else:
|
|
|
|
client.user.M = 0
|
|
|
|
if clientId in self._block:
|
|
|
|
client.role = "BLOCK"
|
|
|
|
if client.user.EA in self._admin:
|
|
|
|
client.role = "ADMIN"
|
|
|
|
self._admin[client.user.EA].update(client.user.dict())
|
2010-08-21 18:32:56 +00:00
|
|
|
self.save()
|
2010-08-21 15:32:22 +00:00
|
|
|
if len(nc) == 1:
|
|
|
|
self.cbNet(self.getNetworkList())
|
|
|
|
self.cbClient(net, self.getClientList(net))
|
|
|
|
|
|
|
|
def logout(self, client):
|
|
|
|
if client.user is None:
|
|
|
|
return
|
|
|
|
clientId = client.user.ID
|
|
|
|
log.msg("Client logout: %s" % (clientId,))
|
|
|
|
if clientId in self._clientData:
|
|
|
|
del self._clientData[clientId]
|
|
|
|
net = client.user.NT
|
|
|
|
self._net[net].remove(clientId)
|
|
|
|
if not self._net[net]:
|
|
|
|
del self._net[net]
|
|
|
|
self.cbNet(self.getNetworkList())
|
2010-08-21 18:32:56 +00:00
|
|
|
self.save()
|
2010-08-21 15:32:22 +00:00
|
|
|
self.cbClient(net, self.getClientList(net))
|
|
|
|
|
|
|
|
def setStatus(self, clientId, status):
|
|
|
|
oldStatus = self._clientData[clientId].user.S
|
|
|
|
if oldStatus != str(status):
|
|
|
|
net = self._clientData[clientId].user.NT
|
|
|
|
self._clientData[clientId].user.S = status
|
|
|
|
self.cbClient(net, self.getClientList(net))
|
|
|
|
|
|
|
|
def mute(self, admin, clientId):
|
|
|
|
if clientId not in self._mute:
|
|
|
|
self._mute[clientId] = self.getClient(clientId).copy(AI=admin.ON)
|
|
|
|
self._clientData[clientId].user.M = 1
|
2010-08-21 18:32:56 +00:00
|
|
|
self.save()
|
2010-08-21 15:32:22 +00:00
|
|
|
net = self.getClient(clientId).NT
|
|
|
|
self.cbClient(net, self.getClientList(net))
|
|
|
|
self.cbMute(self.getMuteList())
|
|
|
|
|
|
|
|
def unMute(self, clientId):
|
|
|
|
if clientId in self._mute:
|
|
|
|
del self._mute[clientId]
|
|
|
|
if clientId in self._clientData:
|
|
|
|
self._clientData[clientId].user.M = 0
|
|
|
|
net = self._clientData[clientId].user.NT
|
|
|
|
self.cbClient(net, self.getClientList(net))
|
2010-08-21 18:32:56 +00:00
|
|
|
self.save()
|
2010-08-21 15:32:22 +00:00
|
|
|
self.cbMute(self.getMuteList())
|
|
|
|
|
|
|
|
def block(self, admin, clientId):
|
|
|
|
if clientId not in self._block:
|
|
|
|
self._block[clientId] = self.getClient(clientId).copy(AI=admin.ON)
|
|
|
|
if clientId in self._clientData:
|
|
|
|
net = self._clientData[clientId].user.NT
|
2010-08-21 18:32:56 +00:00
|
|
|
self.save()
|
2010-08-21 15:32:22 +00:00
|
|
|
self.cbBlock(self.getBlockList())
|
|
|
|
|
|
|
|
def unBlock(self, clientId):
|
|
|
|
if clientId in self._block:
|
|
|
|
del self._block[clientId]
|
2010-08-21 18:32:56 +00:00
|
|
|
self.save()
|
2010-08-21 15:32:22 +00:00
|
|
|
self.cbBlock(self.getBlockList())
|
|
|
|
|
|
|
|
def admin(self, clientId):
|
|
|
|
if clientId not in self._admin:
|
|
|
|
self._admin[clientId] = self.getClient(clientId).copy()
|
2010-08-21 18:32:56 +00:00
|
|
|
self.save()
|
2010-08-21 15:32:22 +00:00
|
|
|
self.cbAdmin(self.getAdminList())
|
|
|
|
|
|
|
|
def unAdmin(self, clientId):
|
|
|
|
if clientId in self._admin:
|
|
|
|
del self._admin[clientId]
|
2010-08-21 18:32:56 +00:00
|
|
|
self.save()
|
2010-08-21 15:32:22 +00:00
|
|
|
self.cbAdmin(self.getAdminList())
|
|
|
|
|
|
|
|
|
|
|
|
# vim: set et ai sw=4 ts=4 sts=4:
|