# Path: allchat/filestore/encrypt.py
# class Encryptor(object):
#     def __init__(self, key, IV):
#         self.key = key
#         self.IV = IV
#         self.blocksize = 16
# 
#     def EncryptStr(self,content):
#         if sys.version_info.major == 2:
#             content = content.encode("utf-8")
#         elif sys.version_info.major == 3:
#             content = bytes(content,"utf-8")
#         while len(content)%self.blocksize != 0:
#             content += b"\0"
#         obj = AES.new(self.key,AES.MODE_CBC,self.IV)
#         return obj.encrypt(content)
# 
#     def DecryptStr(self,content):
#         obj = AES.new(self.key,AES.MODE_CBC,self.IV)
#         content = obj.decrypt(content).strip(b"\0")
#         return content.decode("utf-8")
# 
# Path: allchat/path.py
# def get_single_msg_dir():
# 	return os.path.join(get_project_root(),"Data","single_messages")
# 
# def get_group_msg_dir():
# 	return os.path.join(get_project_root(),"Data","group_messages")
# 
# def get_picture_dir():
# 	return os.path.join(get_project_root(),"Data","picture")
# 
# def get_project_root():
# 	return os.path.dirname(os.path.dirname(__file__))


import os
import time
import hashlib
from multiprocessing import Process, Queue
from .encrypt import Encryptor
from allchat.path import get_single_msg_dir, get_group_msg_dir, get_picture_dir, get_project_root
# -*- coding:utf-8 -*-

class MessageSaver(object):
    def __init__(self):
        self._single_message_queue = Queue()
        self._group_message_queue = Queue()
        self._get_config()
        self._terminator = "\nquit\n"
        self._single_message_proc = None
        self._group_message_proc = None

    def _init_config(self, conf_path):
        if not os.path.exists(os.path.dirname(conf_path)):
            os.makedirs(os.path.dirname(conf_path))
        with open(conf_path,"wb") as conf:
            encrypt_key = b"=".join((b"encrypt_key",os.urandom(16)))
            IV = b"=".join((b"IV",os.urandom(16)))
            conf.write(b"\n".join((encrypt_key,IV)))

    def _get_config(self):
        root = get_project_root()
        conf_path = os.path.join(root, "conf", "blizzard.conf")
        if not os.path.exists(conf_path):
            self._init_config(conf_path)
        with open(conf_path,"rb") as conf:
            configs = conf.read()

# Path: allchat/filestore/encrypt.py
# class Encryptor(object):
#     def __init__(self, key, IV):
#         self.key = key
#         self.IV = IV
#         self.blocksize = 16
# 
#     def EncryptStr(self,content):
#         if sys.version_info.major == 2:
#             content = content.encode("utf-8")
#         elif sys.version_info.major == 3:
#             content = bytes(content,"utf-8")
#         while len(content)%self.blocksize != 0:
#             content += b"\0"
#         obj = AES.new(self.key,AES.MODE_CBC,self.IV)
#         return obj.encrypt(content)
# 
#     def DecryptStr(self,content):
#         obj = AES.new(self.key,AES.MODE_CBC,self.IV)
#         content = obj.decrypt(content).strip(b"\0")
#         return content.decode("utf-8")
# 
# Path: allchat/path.py
# def get_single_msg_dir():
# 	return os.path.join(get_project_root(),"Data","single_messages")
# 
# def get_group_msg_dir():
# 	return os.path.join(get_project_root(),"Data","group_messages")
# 
# def get_picture_dir():
# 	return os.path.join(get_project_root(),"Data","picture")
# 
# def get_project_root():
# 	return os.path.dirname(os.path.dirname(__file__))


# Path: allchat/filestore/storage.py
import os
import time
import hashlib
from multiprocessing import Process, Queue
from .encrypt import Encryptor
from allchat.path import get_single_msg_dir, get_group_msg_dir, get_picture_dir, get_project_root

# -*- coding:utf-8 -*-

class MessageSaver(object):
    def __init__(self):
        self._single_message_queue = Queue()
        self._group_message_queue = Queue()
        self._get_config()
        self._terminator = "\nquit\n"
        self._single_message_proc = None
        self._group_message_proc = None

    def _init_config(self, conf_path):
        if not os.path.exists(os.path.dirname(conf_path)):
            os.makedirs(os.path.dirname(conf_path))
        with open(conf_path,"wb") as conf:
            encrypt_key = b"=".join((b"encrypt_key",os.urandom(16)))
            IV = b"=".join((b"IV",os.urandom(16)))
            conf.write(b"\n".join((encrypt_key,IV)))

    def _get_config(self):
        root = get_project_root()
        conf_path = os.path.join(root, "conf", "blizzard.conf")
        if not os.path.exists(conf_path):
            self._init_config(conf_path)
        with open(conf_path,"rb") as conf:
            configs = conf.read()

            config_dict = dict(tuple(line.split(b"="))