2022-07-21 12:39:59 +00:00
|
|
|
from twisted.internet.threads import deferToThread
|
|
|
|
|
2019-08-19 19:12:42 +00:00
|
|
|
import main
|
2022-07-21 12:40:05 +00:00
|
|
|
from utils.logging.log import warn
|
|
|
|
from utils.logging.debug import trace
|
2019-10-17 19:19:35 +00:00
|
|
|
from utils.parsing import parsen
|
2018-02-23 22:05:40 +00:00
|
|
|
|
2022-07-21 12:39:41 +00:00
|
|
|
|
2018-08-27 19:42:49 +00:00
|
|
|
def getWhoSingle(name, query):
|
2022-07-21 12:39:41 +00:00
|
|
|
result = main.r.sscan("live.who." + name, 0, query, count=999999)
|
2018-08-27 19:42:49 +00:00
|
|
|
if result[1] == []:
|
|
|
|
return None
|
2019-10-17 19:19:35 +00:00
|
|
|
return (i.decode() for i in result[1])
|
2018-02-23 22:05:40 +00:00
|
|
|
|
2022-07-21 12:39:41 +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
|
|
|
|
|
2022-07-21 12:39:41 +00:00
|
|
|
|
2018-10-08 19:08:10 +00:00
|
|
|
def getChansSingle(name, nick):
|
2022-07-21 12:39:41 +00:00
|
|
|
nick = ["live.chan." + name + "." + i for i in nick]
|
2018-10-08 19:08:10 +00:00
|
|
|
result = main.r.sinter(*nick)
|
2018-10-07 19:48:39 +00:00
|
|
|
if len(result) == 0:
|
|
|
|
return None
|
2019-10-17 19:19:35 +00:00
|
|
|
return (i.decode() for i in result)
|
2018-10-07 19:48:39 +00:00
|
|
|
|
2022-07-21 12:39:41 +00:00
|
|
|
|
2019-08-15 20:20:49 +00:00
|
|
|
def getChanList(name, nick):
|
2022-07-21 12:39:41 +00:00
|
|
|
chanspace = "live.chan." + name + "." + nick
|
2019-08-15 20:20:49 +00:00
|
|
|
result = main.r.smembers(chanspace)
|
|
|
|
if len(result) == 0:
|
|
|
|
return None
|
2019-10-17 19:19:35 +00:00
|
|
|
return (i.decode() for i in result)
|
2019-08-15 20:20:49 +00:00
|
|
|
|
2022-07-21 12:39:41 +00:00
|
|
|
|
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
|
|
|
|
|
2022-07-21 12:39:41 +00:00
|
|
|
|
2018-10-08 19:08:10 +00:00
|
|
|
def getUsersSingle(name, nick):
|
2022-07-21 12:39:41 +00:00
|
|
|
nick = ("live.who." + name + "." + i for i in nick)
|
2018-10-08 19:08:10 +00:00
|
|
|
result = main.r.sinter(*nick)
|
|
|
|
if len(result) == 0:
|
|
|
|
return None
|
2019-10-17 19:19:35 +00:00
|
|
|
return (i.decode() for i in result)
|
2018-10-08 19:08:10 +00:00
|
|
|
|
2022-07-21 12:39:41 +00:00
|
|
|
|
2018-10-08 19:08:10 +00:00
|
|
|
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
|
|
|
|
|
2022-07-21 12:39:41 +00:00
|
|
|
|
2018-08-27 19:42:49 +00:00
|
|
|
def getNumWhoEntries(name):
|
2022-07-21 12:39:41 +00:00
|
|
|
return main.r.scard("live.who." + name)
|
|
|
|
|
2018-08-27 19:42:49 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2022-07-21 12:39:41 +00:00
|
|
|
|
2018-08-27 19:42:49 +00:00
|
|
|
def getNamespace(name, channel, nick):
|
|
|
|
gnamespace = "live.who.%s" % name
|
|
|
|
namespace = "live.who.%s.%s" % (name, channel)
|
|
|
|
chanspace = "live.chan.%s.%s" % (name, nick)
|
2019-10-17 19:19:35 +00:00
|
|
|
mapspace = "live.map.%s" % name
|
|
|
|
return (gnamespace, namespace, chanspace, mapspace)
|
2018-08-27 19:42:49 +00:00
|
|
|
|
2022-07-21 12:39:41 +00:00
|
|
|
|
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
|
2019-10-17 19:19:35 +00:00
|
|
|
mapspace = "live.map.%s" % name
|
2018-08-27 19:42:49 +00:00
|
|
|
p = main.r.pipeline()
|
|
|
|
for i in users:
|
2022-07-21 12:39:41 +00:00
|
|
|
user = i[0] + "!" + i[1] + "@" + i[2]
|
2019-10-17 19:19:35 +00:00
|
|
|
p.hset(mapspace, i[0], user)
|
|
|
|
p.sadd(gnamespace, user)
|
2018-08-27 19:42:49 +00:00
|
|
|
p.execute()
|
|
|
|
|
2022-07-21 12:39:41 +00:00
|
|
|
|
2019-08-19 19:12:42 +00:00
|
|
|
def initialUsers(name, channel, users):
|
2020-10-31 16:52:00 +00:00
|
|
|
trace("Initialising WHO records for %s on %s" % (channel, name))
|
2022-07-21 12:40:05 +00:00
|
|
|
deferToThread(_initialUsers, name, channel, users)
|
2022-07-21 12:39:41 +00:00
|
|
|
# d.addCallback(testCallback)
|
|
|
|
|
2019-08-19 19:12:42 +00:00
|
|
|
|
|
|
|
def _initialNames(name, channel, names):
|
2018-08-27 19:42:49 +00:00
|
|
|
namespace = "live.who.%s.%s" % (name, channel)
|
|
|
|
p = main.r.pipeline()
|
2020-07-09 18:43:47 +00:00
|
|
|
for mode, nick in names:
|
|
|
|
p.sadd(namespace, nick)
|
2022-07-21 12:39:41 +00:00
|
|
|
p.sadd("live.chan." + name + "." + nick, channel)
|
2020-07-09 18:43:47 +00:00
|
|
|
if mode:
|
2022-07-21 12:39:41 +00:00
|
|
|
p.hset("live.prefix." + name + "." + channel, nick, mode)
|
2018-08-27 19:42:49 +00:00
|
|
|
p.execute()
|
|
|
|
|
2022-07-21 12:39:41 +00:00
|
|
|
|
2019-08-19 19:12:42 +00:00
|
|
|
def initialNames(name, channel, names):
|
2020-10-31 16:52:00 +00:00
|
|
|
trace("Initialising NAMES records for %s on %s" % (channel, name))
|
2022-07-21 12:40:05 +00:00
|
|
|
deferToThread(_initialNames, name, channel, names)
|
2022-07-21 12:39:41 +00:00
|
|
|
# d.addCallback(testCallback)
|
|
|
|
|
2019-08-19 19:12:42 +00:00
|
|
|
|
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
|
2019-10-17 19:19:35 +00:00
|
|
|
mapspace = "live.map.%s" % name
|
|
|
|
parsed = parsen(user)
|
|
|
|
p = main.r.pipeline()
|
|
|
|
p.sadd(gnamespace, user)
|
2022-07-21 12:39:41 +00:00
|
|
|
p.hset(mapspace, parsed[0], user) # add nick -> user mapping
|
2019-10-17 19:19:35 +00:00
|
|
|
p.execute()
|
2018-08-27 19:42:49 +00:00
|
|
|
|
2022-07-21 12:39:41 +00:00
|
|
|
|
2018-08-27 19:42:49 +00:00
|
|
|
def addUser(name, channel, nick, user):
|
2019-10-17 19:19:35 +00:00
|
|
|
gnamespace, namespace, chanspace, mapspace = getNamespace(name, channel, nick)
|
2018-08-27 19:42:49 +00:00
|
|
|
p = main.r.pipeline()
|
|
|
|
p.sadd(gnamespace, user)
|
|
|
|
p.sadd(namespace, nick)
|
|
|
|
p.sadd(chanspace, channel)
|
2019-10-17 19:19:35 +00:00
|
|
|
p.hset(mapspace, nick, user)
|
2018-08-27 19:42:49 +00:00
|
|
|
p.execute()
|
2018-02-23 22:05:40 +00:00
|
|
|
|
2022-07-21 12:39:41 +00:00
|
|
|
|
2019-10-05 17:22:14 +00:00
|
|
|
def delUser(name, channel, nick, user):
|
2019-10-17 19:19:35 +00:00
|
|
|
gnamespace, namespace, chanspace, mapspace = getNamespace(name, channel, nick)
|
2018-08-27 19:42:49 +00:00
|
|
|
p = main.r.pipeline()
|
|
|
|
channels = main.r.smembers(chanspace)
|
|
|
|
p.srem(namespace, nick)
|
2022-07-21 12:39:41 +00:00
|
|
|
if channels == {channel.encode()}: # can we only see them on this channel?
|
|
|
|
p.delete(chanspace) # remove channel tracking entry
|
2022-07-21 12:40:01 +00:00
|
|
|
p.hdel("live.prefix." + name + "." + channel, nick) # remove prefix tracking entry
|
2022-07-21 12:39:41 +00:00
|
|
|
p.hdel(mapspace, nick) # remove nick mapping entry
|
2019-10-05 17:22:14 +00:00
|
|
|
if user:
|
2022-07-21 12:39:41 +00:00
|
|
|
p.srem(gnamespace, user) # remove global userinfo entry
|
2019-10-05 17:22:14 +00:00
|
|
|
else:
|
|
|
|
warn("Attempt to delete nonexistent user: %s" % user)
|
2018-08-27 19:42:49 +00:00
|
|
|
else:
|
2022-07-21 12:39:41 +00:00
|
|
|
p.srem(chanspace, channel) # keep up - remove the channel from their list
|
2018-08-27 19:42:49 +00:00
|
|
|
p.execute()
|
|
|
|
|
2022-07-21 12:39:41 +00:00
|
|
|
|
2018-10-20 23:49:15 +00:00
|
|
|
def escape(text):
|
|
|
|
chars = ["[", "]", "^", "-", "*", "?"]
|
|
|
|
text = text.replace("\\", "\\\\")
|
|
|
|
for i in chars:
|
2022-07-21 12:39:41 +00:00
|
|
|
text = text.replace(i, "\\" + i)
|
2018-10-20 23:49:15 +00:00
|
|
|
return text
|
|
|
|
|
2022-07-21 12:39:41 +00:00
|
|
|
|
2018-08-27 19:42:49 +00:00
|
|
|
def getUserByNick(name, nick):
|
2022-07-21 12:39:41 +00:00
|
|
|
gnamespace = "live.who.%s" % name # "nick": "nick!ident@host"
|
2019-10-17 19:19:35 +00:00
|
|
|
mapspace = "live.map.%s" % name
|
|
|
|
if main.r.hexists(mapspace, nick):
|
|
|
|
return main.r.hget(mapspace, nick)
|
|
|
|
else:
|
2022-07-21 12:40:01 +00:00
|
|
|
warn("Entry doesn't exist: %s on %s - attempting auxiliary lookup" % (nick, mapspace))
|
2022-07-21 12:39:41 +00:00
|
|
|
# return False
|
2019-10-17 19:19:35 +00:00
|
|
|
# legacy code below - remove when map is reliable
|
2022-07-21 12:39:41 +00:00
|
|
|
usermatch = main.r.sscan(gnamespace, match=escape(nick) + "!*", count=999999999)
|
2018-08-27 19:42:49 +00:00
|
|
|
if usermatch[1] == []:
|
2020-05-31 12:44:34 +00:00
|
|
|
warn("No matches found for user query: %s on %s" % (nick, name))
|
2018-08-27 19:42:49 +00:00
|
|
|
return False
|
|
|
|
else:
|
|
|
|
if len(usermatch[1]) == 1:
|
|
|
|
user = usermatch[1][0]
|
|
|
|
return user
|
2018-02-23 22:05:40 +00:00
|
|
|
else:
|
2019-10-17 19:19:35 +00:00
|
|
|
warn("Auxiliary lookup failed: %s on %s" % (nick, gnamespace))
|
2018-08-27 19:42:49 +00:00
|
|
|
return False
|
|
|
|
|
2022-07-21 12:39:41 +00:00
|
|
|
|
2018-08-27 19:42:49 +00:00
|
|
|
def renameUser(name, oldnick, olduser, newnick, newuser):
|
|
|
|
gnamespace = "live.who.%s" % name
|
|
|
|
chanspace = "live.chan.%s.%s" % (name, oldnick)
|
2019-10-17 19:19:35 +00:00
|
|
|
mapspace = "live.map.%s" % name
|
2018-08-27 19:42:49 +00:00
|
|
|
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()
|
2022-07-21 12:39:41 +00:00
|
|
|
p.srem("live.who." + name + "." + i, oldnick)
|
|
|
|
p.sadd("live.who." + name + "." + i, newnick)
|
2019-10-17 19:19:35 +00:00
|
|
|
p.hdel(mapspace, oldnick)
|
|
|
|
p.hset(mapspace, newnick, newuser)
|
2022-07-21 12:40:01 +00:00
|
|
|
if main.r.exists("live.prefix." + name + "." + i): # if there's a prefix entry for the channel
|
|
|
|
if main.r.hexists("live.prefix." + name + "." + i, oldnick): # if the old nick is in it
|
|
|
|
mode = main.r.hget("live.prefix." + name + "." + i, oldnick) # retrieve old modes
|
|
|
|
p.hset("live.prefix." + name + "." + i, newnick, mode) # set old modes to new nickname
|
2018-08-27 19:42:49 +00:00
|
|
|
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
|
|
|
|
2022-07-21 12:39:41 +00:00
|
|
|
|
|
|
|
def delUserByNick(name, channel, nick): # kick
|
2018-08-27 19:42:49 +00:00
|
|
|
user = getUserByNick(name, nick)
|
2020-05-31 12:44:34 +00:00
|
|
|
if not user:
|
|
|
|
return
|
2018-08-27 19:42:49 +00:00
|
|
|
delUser(name, channel, nick, user)
|
|
|
|
|
2022-07-21 12:39:41 +00:00
|
|
|
|
|
|
|
def delUserByNetwork(name, nick, user): # quit
|
2018-08-27 19:42:49 +00:00
|
|
|
gnamespace = "live.who.%s" % name
|
|
|
|
chanspace = "live.chan.%s.%s" % (name, nick)
|
2019-10-17 19:19:35 +00:00
|
|
|
mapspace = "live.chan.%s" % name
|
2018-08-27 19:42:49 +00:00
|
|
|
p = main.r.pipeline()
|
|
|
|
p.srem(gnamespace, user)
|
|
|
|
for i in main.r.smembers(chanspace):
|
2022-07-21 12:39:41 +00:00
|
|
|
p.srem("live.who." + name + "." + i.decode(), nick)
|
|
|
|
p.hdel("live.prefix." + name + "." + i.decode(), nick)
|
2020-07-09 18:43:47 +00:00
|
|
|
|
2018-08-27 19:42:49 +00:00
|
|
|
p.delete(chanspace)
|
2019-10-17 19:19:35 +00:00
|
|
|
p.hdel(mapspace, nick)
|
2018-08-27 19:42:49 +00:00
|
|
|
p.execute()
|
|
|
|
|
2022-07-21 12:39:41 +00:00
|
|
|
|
2019-10-17 19:19:35 +00:00
|
|
|
def _delChannels(net, channels):
|
|
|
|
gnamespace = "live.who.%s" % net
|
|
|
|
mapspace = "live.map.%s" % net
|
2018-08-27 19:42:49 +00:00
|
|
|
p = main.r.pipeline()
|
2019-10-17 19:19:35 +00:00
|
|
|
for channel in channels:
|
|
|
|
namespace = "live.who.%s.%s" % (net, channel)
|
|
|
|
for i in main.r.smembers(namespace):
|
|
|
|
nick = i.decode()
|
2022-07-21 12:39:41 +00:00
|
|
|
# user = getUserByNick(net, nick) -- far too many function calls
|
2019-10-17 19:19:35 +00:00
|
|
|
user = main.r.hget(mapspace, nick)
|
|
|
|
if not user:
|
|
|
|
warn("User lookup failed: %s on %s" % (nick, net))
|
2022-07-21 12:39:41 +00:00
|
|
|
if main.r.smembers("live.chan." + net + "." + nick) == {channel.encode()}:
|
2019-10-17 19:19:35 +00:00
|
|
|
if user:
|
|
|
|
p.srem(gnamespace, user)
|
2020-07-09 18:43:47 +00:00
|
|
|
|
2022-07-21 12:39:41 +00:00
|
|
|
p.delete("live.chan." + net + "." + nick)
|
|
|
|
p.hdel(mapspace, nick) # remove map entry
|
2019-10-17 19:19:35 +00:00
|
|
|
else:
|
2022-07-21 12:39:41 +00:00
|
|
|
p.srem("live.chan." + net + "." + nick, channel)
|
2019-10-17 19:19:35 +00:00
|
|
|
p.delete(namespace)
|
2022-07-21 12:39:41 +00:00
|
|
|
p.delete("live.prefix." + net + "." + channel)
|
2018-08-27 19:42:49 +00:00
|
|
|
p.execute()
|
2019-08-19 19:12:42 +00:00
|
|
|
|
2022-07-21 12:39:41 +00:00
|
|
|
|
|
|
|
def delChannels(net, channels): # we have left a channel
|
2020-10-31 16:52:00 +00:00
|
|
|
trace("Purging channel %s for %s" % (", ".join(channels), net))
|
2022-07-21 12:40:05 +00:00
|
|
|
deferToThread(_delChannels, net, channels)
|
2022-07-21 12:39:41 +00:00
|
|
|
# d.addCallback(testCallback)
|