import redis
import json

r = redis.Redis()

count = 0
rules = {}
while True:
	if count % 1000 == 0:
		count = 0
		todel = 1
		while (toadd := r.lpop('toadd')) is not None:
			toadd = json.loads(toadd.decode('utf-8'))
			rules[toadd['id']] = toadd['value']
			print("Added rule " + str(toadd['id']))
		while (todel := r.lpop('todel')) is not None:
			try:
				del rules[int(todel.decode('utf-8'))]
				print("Delete rule " + str(todel.decode('utf-8')))
			except Exception as e:
				print(e)
				pass


	domain = r.blpop('certstream')[1].decode('utf-8')

	for rule in rules.values():
		notify = False
		v = str(rule['v'])
		if rule['t'] == 0:
			if v in domain:
				notify = True
		elif rule['t'] == 1:
			if domain.startswith(v):
				notify = True
		elif rule['t'] == 2:
			if domain.endswith(v):
				notify = True
		if notify:
			print(domain)
			r.rpush('notifications', json.dumps({"domain": domain, "chat": rule['c']}))


	count += 1


	    
