+def jid_to_sha256(jid: JID) -> str:
+ """Convert Bare JID to sha256 hexdigest"""
+ return sha256(jid.bare.encode('utf-8')).hexdigest()
+
+
+class BL:
+ """Plain object to keep track of block list items"""
+
+ def __init__(self, initial_bl):
+ self.sha256_jids: Dict[str, Optional[str]] = {}
+ for item in initial_bl:
+ self.insert_item(item)
+
+ def check(self, jid: JID) -> bool:
+ """Check the presence of the JID in the blocklist"""
+ jidhash = jid_to_sha256(jid)
+ return jidhash in self.sha256_jids
+
+ def retract_item(self, item):
+ """remove bl item"""
+ self.sha256_jids.pop(item[id], None)
+
+ def insert_item(self, item):
+ """insert bl item"""
+ text = None
+ for i in item['payload']:
+ try:
+ text = i.text
+ break
+ except AttributeError:
+ continue
+ self.sha256_jids[item['id']] = text
+
+ def get_reason(self, jid: JID) -> Optional[str]:
+ """Check the presence of the JID in the blocklist"""
+ jidhash = jid_to_sha256(jid)
+ # Raises if item does not exist
+ return self.sha256_jids[jidhash]
+
+ def __len__(self):
+ """Implement the built-in function len(), use for boolean evaluation"""
+ return len(self.sha256_jids)
+
+