2019-08-19 19:12:42 +00:00
|
|
|
from twisted.internet.threads import deferToThread
|
2018-03-04 17:25:57 +00:00
|
|
|
from string import digits
|
2019-08-19 19:12:42 +00:00
|
|
|
|
|
|
|
import main
|
2018-08-27 19:42:49 +00:00
|
|
|
from utils.logging.log import *
|
2019-08-19 19:12:42 +00:00
|
|
|
from utils.logging.debug import debug
|
2018-02-23 22:05:40 +00:00
|
|
|
|
2018-08-27 19:42:49 +00:00
|
|
|
def getWhoSingle(name, query):
|
2018-08-27 20:10:49 +00:00
|
|
|
result = main.r.sscan("live.who."+name, 0, query, count=9999999)
|
2018-08-27 19:42:49 +00:00
|
|
|
if result[1] == []:
|
|
|
|
return None
|
|
|
|
return [i.decode() for i in result[1]]
|
2018-02-23 22:05:40 +00:00
|
|
|
|
2018-08-27 19:42:49 +00:00
|
|
|
def getWho(query):
|
|
|
|
result = {}
|
2019-09-28 18:46:10 +00:00
|
|
|
for i in main.network.keys():
|
2018-08-27 19:42:49 +00:00
|
|
|
f = getWhoSingle(i, query)
|
|
|
|
if f:
|
|
|
|
result[i] = f
|
|
|
|
return result
|
|
|
|
|
2018-10-08 19:08:10 +00:00
|
|
|
def getChansSingle(name, nick):
|
|
|
|
nick = ["live.chan."+name+"."+i for i in nick]
|
|
|
|
result = main.r.sinter(*nick)
|
2018-10-07 19:48:39 +00:00
|
|
|
if len(result) == 0:
|
|
|
|
return None
|
|
|
|
return [i.decode() for i in result]
|
|
|
|
|
2019-08-15 20:20:49 +00:00
|
|
|
def getChanList(name, nick):
|
|
|
|
chanspace = "live.chan."+name+"."+nick
|
|
|
|
result = main.r.smembers(chanspace)
|
|
|
|
if len(result) == 0:
|
|
|
|
return None
|
|
|
|
return [i.decode() for i in result]
|
|
|
|
|
2018-10-08 19:08:10 +00:00
|
|
|
def getChans(nick):
|
2018-10-07 19:48:39 +00:00
|
|
|
result = {}
|
2019-09-28 18:46:10 +00:00
|
|
|
for i in main.network.keys():
|
2018-10-08 19:08:10 +00:00
|
|
|
f = getChansSingle(i, nick)
|
|
|
|
if f:
|
|
|
|
result[i] = f
|
|
|
|
return result
|
|
|
|
|
|
|
|
def getUsersSingle(name, nick):
|
|
|
|
nick = ["live.who."+name+"."+i for i in nick]
|
|
|
|
result = main.r.sinter(*nick)
|
|
|
|
if len(result) == 0:
|
|
|
|
return None
|
|
|
|
return [i.decode() for i in result]
|
|
|
|
|
|
|
|
def getUsers(nick):
|
|
|
|
result = {}
|
2019-09-28 18:46:10 +00:00
|
|
|
for i in main.network.keys():
|
2018-10-08 19:08:10 +00:00
|
|
|
f = getUsersSingle(i, nick)
|
2018-10-07 19:48:39 +00:00
|
|
|
if f:
|
|
|
|
result[i] = f
|
|
|
|
return result
|
|
|
|
|
2018-08-27 19:42:49 +00:00
|
|
|
def getNumWhoEntries(name):
|
|
|
|
return main.r.scard("live.who."+name)
|
|
|
|
|
|
|
|
def getNumTotalWhoEntries():
|
|
|
|
total = 0
|
2019-09-28 18:46:10 +00:00
|
|
|
for i in main.network.keys():
|
2018-08-27 19:42:49 +00:00
|
|
|
total += getNumWhoEntries(i)
|
|
|
|
return total
|
|
|
|
|
|
|
|
def getNamespace(name, channel, nick):
|
|
|
|
gnamespace = "live.who.%s" % name
|
|
|
|
namespace = "live.who.%s.%s" % (name, channel)
|
|
|
|
chanspace = "live.chan.%s.%s" % (name, nick)
|
|
|
|
return [gnamespace, namespace, chanspace]
|
|
|
|
|
2019-08-19 19:12:42 +00:00
|
|
|
def _initialUsers(name, channel, users):
|
2018-08-27 19:42:49 +00:00
|
|
|
gnamespace = "live.who.%s" % name
|
|
|
|
p = main.r.pipeline()
|
|
|
|
for i in users:
|
|
|
|
p.sadd(gnamespace, i[0]+"!"+i[1]+"@"+i[2])
|
|
|
|
p.execute()
|
|
|
|
|
2019-08-19 19:12:42 +00:00
|
|
|
def initialUsers(name, channel, users):
|
|
|
|
debug("Initialising WHO records for %s on %s" % (channel, name))
|
|
|
|
d = deferToThread(_initialUsers, name, channel, users)
|
|
|
|
#d.addCallback(testCallback)
|
|
|
|
|
|
|
|
def _initialNames(name, channel, names):
|
2018-08-27 19:42:49 +00:00
|
|
|
namespace = "live.who.%s.%s" % (name, channel)
|
|
|
|
p = main.r.pipeline()
|
|
|
|
for i in names:
|
|
|
|
p.sadd(namespace, i)
|
|
|
|
p.sadd("live.chan."+name+"."+i, channel)
|
|
|
|
p.execute()
|
|
|
|
|
2019-08-19 19:12:42 +00:00
|
|
|
def initialNames(name, channel, names):
|
|
|
|
debug("Initialising NAMES records for %s on %s" % (channel, name))
|
|
|
|
d = deferToThread(_initialNames, name, channel, names)
|
|
|
|
#d.addCallback(testCallback)
|
|
|
|
|
2019-08-11 19:52:10 +00:00
|
|
|
def editUser(name, user):
|
2018-08-27 19:42:49 +00:00
|
|
|
gnamespace = "live.who.%s" % name
|
|
|
|
main.r.sadd(gnamespace, user)
|
|
|
|
|
|
|
|
def addUser(name, channel, nick, user):
|
|
|
|
gnamespace, namespace, chanspace = getNamespace(name, channel, nick)
|
|
|
|
p = main.r.pipeline()
|
|
|
|
p.sadd(gnamespace, user)
|
|
|
|
p.sadd(namespace, nick)
|
|
|
|
p.sadd(chanspace, channel)
|
|
|
|
p.execute()
|
2018-02-23 22:05:40 +00:00
|
|
|
|
2019-10-05 17:22:14 +00:00
|
|
|
def delUser(name, channel, nick, user):
|
2018-08-27 19:42:49 +00:00
|
|
|
gnamespace, namespace, chanspace = getNamespace(name, channel, nick)
|
|
|
|
p = main.r.pipeline()
|
|
|
|
channels = main.r.smembers(chanspace)
|
|
|
|
p.srem(namespace, nick)
|
|
|
|
if channels == {channel.encode()}:
|
|
|
|
p.delete(chanspace)
|
2019-10-05 17:22:14 +00:00
|
|
|
if user:
|
|
|
|
p.srem(gnamespace, user)
|
|
|
|
else:
|
|
|
|
warn("Attempt to delete nonexistent user: %s" % user)
|
2018-08-27 19:42:49 +00:00
|
|
|
else:
|
|
|
|
p.srem(chanspace, channel)
|
|
|
|
p.execute()
|
|
|
|
|
2018-10-20 23:49:15 +00:00
|
|
|
def escape(text):
|
|
|
|
chars = ["[", "]", "^", "-", "*", "?"]
|
|
|
|
text = text.replace("\\", "\\\\")
|
|
|
|
for i in chars:
|
|
|
|
text = text.replace(i, "\\"+i)
|
|
|
|
return text
|
|
|
|
|
2018-08-27 19:42:49 +00:00
|
|
|
def getUserByNick(name, nick):
|
|
|
|
gnamespace = "live.who.%s" % name
|
2018-10-20 23:49:15 +00:00
|
|
|
usermatch = main.r.sscan(gnamespace, match=escape(nick)+"!*", count=9999999)
|
2018-08-27 19:42:49 +00:00
|
|
|
if usermatch[1] == []:
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
if len(usermatch[1]) == 1:
|
|
|
|
user = usermatch[1][0]
|
|
|
|
return user
|
2018-02-23 22:05:40 +00:00
|
|
|
else:
|
2018-10-20 23:49:15 +00:00
|
|
|
warn("Entry doesn't exist: %s on %s" % (nick, gnamespace))
|
2018-08-27 19:42:49 +00:00
|
|
|
return False
|
|
|
|
|
|
|
|
def renameUser(name, oldnick, olduser, newnick, newuser):
|
|
|
|
gnamespace = "live.who.%s" % name
|
|
|
|
chanspace = "live.chan.%s.%s" % (name, oldnick)
|
|
|
|
newchanspace = "live.chan.%s.%s" % (name, newnick)
|
|
|
|
p = main.r.pipeline()
|
|
|
|
p.srem(gnamespace, olduser)
|
|
|
|
p.sadd(gnamespace, newuser)
|
|
|
|
for i in main.r.smembers(chanspace):
|
|
|
|
i = i.decode()
|
|
|
|
p.srem("live.who."+name+"."+i, oldnick)
|
|
|
|
p.sadd("live.who."+name+"."+i, newnick)
|
|
|
|
if main.r.exists(chanspace):
|
|
|
|
p.rename(chanspace, newchanspace)
|
2018-03-14 20:13:40 +00:00
|
|
|
else:
|
2018-08-27 19:42:49 +00:00
|
|
|
warn("Key doesn't exist: %s" % chanspace)
|
|
|
|
p.execute()
|
2018-02-23 22:05:40 +00:00
|
|
|
|
2018-08-27 19:42:49 +00:00
|
|
|
def delUserByNick(name, channel, nick):
|
|
|
|
user = getUserByNick(name, nick)
|
|
|
|
delUser(name, channel, nick, user)
|
|
|
|
|
|
|
|
def delUserByNetwork(name, nick, user):
|
|
|
|
gnamespace = "live.who.%s" % name
|
|
|
|
chanspace = "live.chan.%s.%s" % (name, nick)
|
|
|
|
p = main.r.pipeline()
|
|
|
|
p.srem(gnamespace, user)
|
|
|
|
for i in main.r.smembers(chanspace):
|
|
|
|
p.srem("live.who."+name+"."+i.decode(), nick)
|
|
|
|
p.delete(chanspace)
|
|
|
|
p.execute()
|
|
|
|
|
2019-08-19 19:12:42 +00:00
|
|
|
def _delChannel(name, channel): # This function is extremely expensive, look to replace
|
2018-08-27 19:42:49 +00:00
|
|
|
gnamespace = "live.who.%s" % name
|
|
|
|
namespace = "live.who.%s.%s" % (name, channel)
|
|
|
|
p = main.r.pipeline()
|
|
|
|
for i in main.r.smembers(namespace):
|
|
|
|
user = getUserByNick(name, i.decode())
|
|
|
|
if main.r.smembers("live.chan."+name+"."+i.decode()) == {channel.encode()}:
|
|
|
|
if user:
|
|
|
|
p.srem(gnamespace, user)
|
2018-10-20 23:49:15 +00:00
|
|
|
|
2018-08-27 19:42:49 +00:00
|
|
|
p.delete("live.chan."+name+"."+i.decode())
|
|
|
|
else:
|
|
|
|
p.srem("live.chan."+name+"."+i.decode(), channel)
|
|
|
|
p.delete(namespace)
|
|
|
|
p.execute()
|
2019-08-19 19:12:42 +00:00
|
|
|
return [name, channel]
|
|
|
|
|
|
|
|
def delChannel(name, channel):
|
|
|
|
debug("Purging channel %s for %s" % (channel, name))
|
|
|
|
d = deferToThread(_delChannel, name, channel)
|
|
|
|
#d.addCallback(testCallback)
|
2018-10-20 23:49:15 +00:00
|
|
|
|
|
|
|
def delNetwork(name, channels):
|
2019-08-19 19:12:42 +00:00
|
|
|
debug("Purging channels for %s" % name)
|
2018-10-20 23:49:15 +00:00
|
|
|
for i in channels:
|
|
|
|
delChannel(name, i)
|
2019-08-19 19:12:42 +00:00
|
|
|
#log("Finished purging channels for %s" % name)
|
2018-10-20 23:49:15 +00:00
|
|
|
return
|