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('ascii'))
			rules[toadd['id']] = toadd['value']
		while (todel := r.lpop('todel')) is not None:
			try:
				del rules[todel.decode('ascii')]
			except:
				pass


	domain = r.blpop('certstream')[1].decode('ascii')

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


	count += 1


	    
