2018-07-28 20:33:50 +00:00
|
|
|
from copy import deepcopy
|
2019-03-20 20:22:46 +00:00
|
|
|
from json import dumps
|
2019-08-15 20:20:49 +00:00
|
|
|
from datetime import datetime
|
2018-07-27 21:58:37 +00:00
|
|
|
|
2019-08-05 21:51:16 +00:00
|
|
|
import main
|
|
|
|
from core.relay import sendRelayNotification
|
2019-08-12 20:03:47 +00:00
|
|
|
from modules import userinfo
|
2019-08-15 20:20:49 +00:00
|
|
|
from utils.dedup import dedup
|
2019-08-05 21:51:16 +00:00
|
|
|
|
2018-07-27 21:58:37 +00:00
|
|
|
def testNetTarget(name, target):
|
2018-08-19 15:02:14 +00:00
|
|
|
called = False
|
2018-07-27 21:58:37 +00:00
|
|
|
for i in main.monitor.keys():
|
|
|
|
if "sources" in main.monitor[i].keys():
|
|
|
|
if name in main.monitor[i]["sources"]:
|
|
|
|
if main.monitor[i]["sources"][name] == True:
|
2018-08-19 15:02:14 +00:00
|
|
|
yield i
|
|
|
|
called = True
|
2018-07-27 21:58:37 +00:00
|
|
|
elif target in main.monitor[i]["sources"][name]:
|
2018-08-19 15:02:14 +00:00
|
|
|
yield i
|
|
|
|
called = True
|
2018-07-27 21:58:37 +00:00
|
|
|
else:
|
2018-08-19 15:02:14 +00:00
|
|
|
yield i
|
|
|
|
called = True
|
|
|
|
if not called:
|
|
|
|
return False
|
2018-07-27 21:58:37 +00:00
|
|
|
|
2018-08-31 23:25:51 +00:00
|
|
|
def contained_in(x, y):
|
|
|
|
if x is None or y is None:
|
|
|
|
return False
|
|
|
|
elif x == y:
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return y in x
|
|
|
|
|
|
|
|
def is_in(k, vs, A):
|
|
|
|
return any(contained_in(A.get(k), vp) for vp in vs)
|
|
|
|
|
|
|
|
def matches(A, B):
|
|
|
|
return all(is_in(k, vs, A) for (k, vs) in B.items())
|
|
|
|
|
2018-07-27 21:58:37 +00:00
|
|
|
def magicFunction(A, B):
|
2018-08-31 23:25:51 +00:00
|
|
|
isInside = False
|
2018-07-28 20:33:50 +00:00
|
|
|
if "send" in B.keys():
|
|
|
|
del B["send"]
|
|
|
|
if "sources" in B.keys():
|
|
|
|
del B["sources"]
|
2018-08-31 23:25:51 +00:00
|
|
|
if "inside" in B.keys():
|
|
|
|
if B["inside"] == True:
|
|
|
|
isInside = True
|
|
|
|
del B["inside"]
|
|
|
|
if isInside:
|
|
|
|
return matches(A, B)
|
|
|
|
else:
|
|
|
|
return all(A[k] in B[k] for k in set(A) & set(B)) and set(B) <= set(A)
|
2018-07-27 21:58:37 +00:00
|
|
|
|
2019-08-15 20:20:49 +00:00
|
|
|
def event(numName, c): # yes I'm using a short variable because otherwise it goes off the screen
|
2019-08-12 20:03:47 +00:00
|
|
|
if not "target" in c.keys():
|
|
|
|
c["target"] = None
|
|
|
|
|
2019-08-15 20:20:49 +00:00
|
|
|
if dedup(numName, c):
|
|
|
|
return
|
2019-08-12 20:03:47 +00:00
|
|
|
# metadata scraping
|
|
|
|
# need to check if this was received from a relay
|
|
|
|
# in which case, do not do this
|
|
|
|
if c["type"] in ["msg", "notice", "action", "topic", "mode"]:
|
|
|
|
userinfo.editUser(c["name"], c["muser"])
|
|
|
|
elif c["type"] == "nick":
|
|
|
|
userinfo.renameUser(c["name"], c["nick"], c["muser"], c["user"], c["user"]+"!"+c["ident"]+"@"+c["host"])
|
|
|
|
elif c["type"] == "kick":
|
|
|
|
userinfo.editUser(c["name"], c["muser"])
|
|
|
|
userinfo.delUserByNick(c["name"], c["target"], c["user"])
|
|
|
|
elif c["type"] == "quit":
|
|
|
|
userinfo.delUserByNetwork(c["name"], c["nick"], c["muser"])
|
|
|
|
elif c["type"] == "join":
|
2019-08-15 20:20:49 +00:00
|
|
|
userinfo.addUser(c["name"], c["target"], c["nick"], c["muser"])
|
2019-08-12 20:03:47 +00:00
|
|
|
elif c["type"] == "part":
|
2019-08-15 20:20:49 +00:00
|
|
|
userinfo.delUser(c["name"], c["target"], c["nick"], c["muser"])
|
2019-08-12 20:03:47 +00:00
|
|
|
|
|
|
|
if "mtype" in c.keys():
|
|
|
|
if c["mtype"] == "nick":
|
|
|
|
userinfo.renameUser(c["name"], c["nick"], c["muser"], c["user"], c["user"]+"!"+c["ident"]+"@"+c["host"])
|
2019-08-05 21:51:16 +00:00
|
|
|
|
2019-08-12 20:03:47 +00:00
|
|
|
if "muser" in c.keys():
|
|
|
|
del c["muser"]
|
|
|
|
sendRelayNotification(c)
|
2019-08-15 20:20:49 +00:00
|
|
|
|
|
|
|
# only monitors below
|
2019-08-12 20:03:47 +00:00
|
|
|
monitorGroups = testNetTarget(c["name"], c["target"])
|
2018-08-19 15:02:14 +00:00
|
|
|
if monitorGroups == False:
|
2018-07-27 21:58:37 +00:00
|
|
|
return
|
2018-08-19 15:02:14 +00:00
|
|
|
for monitorGroup in monitorGroups:
|
2019-08-12 20:03:47 +00:00
|
|
|
matcher = magicFunction(deepcopy(c), deepcopy(main.monitor[monitorGroup]))
|
2018-08-19 15:02:14 +00:00
|
|
|
if matcher == True:
|
2019-08-12 20:03:47 +00:00
|
|
|
c["monitor"] = True
|
2018-08-19 15:02:14 +00:00
|
|
|
if "send" in main.monitor[monitorGroup].keys():
|
|
|
|
for i in main.monitor[monitorGroup]["send"].keys():
|
|
|
|
if isinstance(main.monitor[monitorGroup]["send"][i], bool):
|
2019-08-12 20:03:47 +00:00
|
|
|
sendRelayNotification({"type": "err", "name": name, "target": target, "message": c, "reason": "errdeliv"})
|
2018-08-19 15:02:14 +00:00
|
|
|
continue
|
|
|
|
if not i in main.pool.keys():
|
2019-08-12 20:03:47 +00:00
|
|
|
sendRelayNotification({"type": "err", "name": name, "target": target, "message": c, "reason": "noname"})
|
2018-08-19 15:02:14 +00:00
|
|
|
if not i in main.IRCPool.keys():
|
2019-08-12 20:03:47 +00:00
|
|
|
sendRelayNotification({"type": "err", "name": name, "target": target, "message": c, "reason": "noinstance"})
|
2018-08-19 15:02:14 +00:00
|
|
|
for x in main.monitor[monitorGroup]["send"][i]:
|
2019-08-12 20:03:47 +00:00
|
|
|
main.IRCPool[i].msg(x, "monitor [%s] (%s) %s" % (monitorGroup, c["name"], c))
|