monolith/modules/monitor.py

71 lines
2.5 KiB
Python

import main
import modules.keyword as keyword
from copy import deepcopy
def testNetTarget(name, target):
called = False
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:
yield i
called = True
elif target in main.monitor[i]["sources"][name]:
yield i
called = True
else:
yield i
called = True
if not called:
return False
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())
def magicFunction(A, B):
isInside = False
if "send" in B.keys():
del B["send"]
if "sources" in B.keys():
del B["sources"]
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)
def event(name, target, cast):
monitorGroups = testNetTarget(name, target)
if monitorGroups == False:
return
for monitorGroup in monitorGroups:
matcher = magicFunction(deepcopy(cast), deepcopy(main.monitor[monitorGroup]))
if matcher == True:
if "send" in main.monitor[monitorGroup].keys():
for i in main.monitor[monitorGroup]["send"].keys():
if isinstance(main.monitor[monitorGroup]["send"][i], bool):
keyword.sendMaster("ERRDELIV MONITOR [%s] (%s/%s) %s " % (monitorGroup, name, target, cast))
continue
if not i in main.pool.keys():
keyword.sendMaster("ERROR on monitor %s: No such name: %s" % (monitorGroup, i))
if not i in main.IRCPool.keys():
keyword.sendMaster("ERROR on monitor %s: No such instance: %s" % (monitorGroup, i))
for x in main.monitor[monitorGroup]["send"][i]:
main.IRCPool[i].msg(x, "MONITOR [%s] (%s/%s) %s" % (monitorGroup, name, target, cast))
else:
keyword.sendMaster("MONITOR [%s] (%s/%s) %s " % (monitorGroup, name, target, cast))