<commit_before>import re
from Crypto import Random
from Crypto.Cipher import AES

help_str = """Send messages in the format: sender > receiver : message
Other things here
"""

users = {}

# initialization_vector
iv = b'g\x9e\xecI\x0f\x9b\x81*,\x94\xaa)\x96x$q'

def repl():
    print(help_str)
    while True:
        print("~ ", end="")
        s = input()
        if not re.match("^\w+(\s?)>(\s?)\w+(\s?):(\s?).*", s):
            print(help_str)
            continue
        msg = s[s.find(":")+1:].strip()
        s = s.replace(" ", "")
        sep = s.find(">")
        sender = s[:sep]
        receiver = s[sep+1:s.find(":")]
        if sender in users:
            users[sender].send(receiver, msg)
        else:
            print("%s not found" % sender)

def broadcast(sender, receiver, msg):
    print("(ALL) %s > %s : %s" % (sender, receiver, msg))
    if receiver in users:
        users[receiver].receive(sender, msg)

class User():
    def __init__(self, name):
        self.keys = {}
        self.name = name
        users[self.name] = self

    def add_key(self, name, key):
        self.keys[name] = key

    def remove_key(self, name, key):
        for n, k in self.keys.items():
            if n == name and k == key:
                del key[n]

    def send(self, user, msg):
        if user in self.keys:
            cipher = AES.new(self.keys[user], AES.MODE_CFB, iv)
            ciphertext = cipher.encrypt(str.encode(msg))
            broadcast(self.name, user, ciphertext)
        else:
            print("Error: %s has no shared key with %s" % (self.name, user))

    def receive(self, user, ciphertext):
        if user in self.keys:
            cipher = AES.new(self.keys[user], AES.MODE_CFB, iv)
            msg = cipher.decrypt(ciphertext)
            print("(%s) %s: %s" % (self.name, user, msg.decode("utf-8")))
        else:
            print("Error: %s has no shared key with %s" % (self.name, user))


if __name__ == "__main__":
    shared_key = b'sixteen bit key!'
    alice = User("alice")
    bob = User("bob")
    alice.add_key("bob", shared_key)
    bob.add_key("alice", shared_key)
    repl()
<commit_msg>added help and lskeys commands<commit_after>import re
from Crypto import Random
from Crypto.Cipher import AES

help_str = """Send messages with "sender > receiver : message"
Other available commands are
  help   - show this message
  lskeys - show all shared keys"""

users = {}

# initialization vector
iv = b'g\x9e\xecI\x0f\x9b\x81*,\x94\xaa)\x96x$q'

def repl():
	print(help_str)
	while True:
		print("~ ", end="")
		s = input()
		if s == "help":
			print(help_str)
		elif s == "lskeys":
			for user in users.values():
				print(user.name)
				print("="*len(user.name))
				for name, key in user.keys.items():
					print("{0:<10}{1}".format(name, str(key)))
				print()
		elif not re.match("^\w+(\s?)>(\s?)\w+(\s?):(\s?).*", s):
			print("Invalid command")
		else:
			msg = s[s.find(":")+1:].strip()
			s = s.replace(" ", "")
			sep = s.find(">")
			sender = s[:sep]
			receiver = s[sep+1:s.find(":")]
			if sender in users:
				users[sender].send(receiver, msg)
			else:
				print("%s not found" % sender)

def broadcast(sender, receiver, msg):
	print("(Open) %s > %s : %s" % (sender, receiver, msg))
	if receiver in users:
		users[receiver].receive(sender, msg)

class User():
	def __init__(self, name):
		self.keys = {}
		self.name = name
		users[self.name] = self

	def add_key(self, name, key):
		self.keys[name] = key

	def remove_key(self, name, key):
		for n, k in self.keys.items():
			if n == name and k == key:
				del key[n]

	def send(self, user, msg):
		if user in self.keys:
			cipher = AES.new(self.keys[user], AES.MODE_CFB, iv)
			ciphertext = cipher.encrypt(str.encode(msg))
			broadcast(self.name, user, ciphertext)
		else:
			print("Error: %s has no shared key with %s" % (self.name, user))

	def receive(self, user, ciphertext):
		if user in self.keys:
			cipher = AES.new(self.keys[user], AES.MODE_CFB, iv)
			msg = cipher.decrypt(ciphertext)
			print("(%s) %s: %s" % (self.name, user, msg.decode("utf-8")))
		else:
			print("Error: %s has no shared key with %s" % (self.name, user))


if __name__ == "__main__":
	shared_key = b'sixteen byte key'
	alice = User("alice")
	bob = User("bob")
	alice.add_key("bob", shared_key)
	bob.add_key("alice", shared_key)
	try:
		repl()
	except (EOFError, KeyboardInterrupt):
		print("Interrupt")
{'fraction_non_alphanumeric': 0.17264879600181735, 'fraction_numerical': 0.006360745115856429, 'mean_word_length': 2.7793991416309014, 'pattern_counts': {'":': 8, '<': 4, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4051631', 'n_tokens_mistral': 1721, 'n_tokens_neox': 1590, 'n_words': 414}