Example #1
0
    def idle_email(self, master, user_email):
        user_password = mydb.get_email_dict(master)[user_email]["password"]
        current_folder_list, idle_list = ([] for i in range(2))
        current_thread = threading.currentThread()

        while current_thread.do_run:
            email_dict = mydb.get_email_dict(master)[user_email]
            is_email_box = email_dict["is_email_box"]
            prev_folder_list = []

            if is_email_box:
                length = len(is_email_box)
                for i in range(length):
                    if is_email_box[i]:
                        prev_folder_list.append(email_dict["folder"][i])

                if current_folder_list:
                    add_folder_list = [
                        folder for folder in prev_folder_list
                        if not folder in current_folder_list
                    ]
                    delete_folder_list = [
                        folder for folder in current_folder_list
                        if not folder in prev_folder_list
                    ]

                    if add_folder_list:
                        for folder in add_folder_list:
                            server = self._server_login(
                                user_email, user_password)
                            thread_func = threading.Thread(
                                target=self._thread_idle_email,
                                args=(master, server, user_email, folder))
                            idle_list.append((folder, thread_func))
                            idle_list[-1][1].start()

                    if delete_folder_list:
                        for folder in delete_folder_list:
                            for idle_func in idle_list:
                                if idle_func[0] == folder:
                                    idle_func[1].do_run = False
                                    del idle_list[idle_func]
                                    break

                else:
                    current_folder_list = prev_folder_list[:]
                    for folder in current_folder_list:
                        server = self._server_login(user_email, user_password)
                        thread_func = threading.Thread(
                            target=self._thread_idle_email,
                            args=(master, server, user_email, folder))
                        thread_func.do_run = True
                        idle_list.append((folder, thread_func))

                    for (folder, thread_func) in idle_list:
                        thread_func.start()

        for (folder, idle_func) in idle_list:
            idle_func.do_run = False
Example #2
0
def schedule_idle_email(master):
    current_thread = threading.currentThread()
    email = Email()
    idle_email = getattr(email, 'idle_email')
    prev_email_list, thread_list = ([] for i in range(2))

    while current_thread.do_run:
        email_dict = mydb.get_email_dict(master)
        current_email_list = [
            email for email in email_dict
            if email_dict[email]["is_IDLE"] == True
        ]

        if prev_email_list:
            add_email_list = [
                email for email in current_email_list
                if not email in prev_email_list
            ]
            delete_email_list = [
                email for email in prev_email_list
                if not email in current_email_list
            ]

            if add_email_list:
                for email in add_email_list:
                    thread_func = threading.Thread(target=idle_email,
                                                   args=(master, email))
                    thread_func.do_run = True
                    thread_list.append((email, thread_func))
                    thread_func.start()

            if delete_email_list:
                for email in delete_email_list:
                    for index in range(len(thread_list)):
                        if thread_list[index][0] == email:
                            thread_list[index][1].do_run = False
                            del thread_list[index]
                            break

        else:
            prev_email_list = current_email_list[:]

            for email in prev_email_list:
                thread_func = threading.Thread(target=idle_email,
                                               args=(master, email))
                thread_func.do_run = True
                thread_list.append((email, thread_func))

            for thread_func in thread_list:
                thread_func[1].start()
    for (email, thread_func) in thread_list:
        thread_func.do_run = False
Example #3
0
    def polling_email(self, master, init):
        print("Polling email - {0}".format(master))

        email_list = mydb.get_email_dict(master)

        if init:
            email_account_list = [email for email in email_list]
        else:
            email_account_list = [
                email for email in email_list if not 'gmail' in email
            ]

        responses, responses["data"] = ({} for i in range(2))
        responses["master"] = master
        responses["method"] = "update_email"

        try:
            for user_email in email_account_list:
                email_details = email_list[user_email]
                user_password = email_details['password']
                folder_list = email_details['folder']
                is_email_box_list = email_details['is_email_box']
                if folder_list:
                    length = len(folder_list)
                    responses["data"][user_email] = {}

                    for i in range(0, length):
                        if is_email_box_list[i]:
                            mailbox, mailbox_list, unseen_num, do_update = self.update_email_box(
                                user_email, user_password, folder_list[i])
                            folder = {
                                "mailbox": mailbox,
                                "mailbox_list": mailbox_list,
                                "unseen_num": unseen_num
                            }

                            if do_update:
                                mydb.update_email_box(user_email, folder,
                                                      folder_list[i])

                            responses["data"][user_email][
                                folder_list[i]] = folder

            responses["success"] = True

        except Exception as e:
            print(e)
            responses["success"] = False

        self.io.emit("to-master", responses)
Example #4
0
    def get_email_folder(self, master, email):
        user_email = email
        user_password = mydb.get_email_dict(master)[user_email]["password"]

        try:
            server = self._server_login(user_email, user_password)
            result = server.list_folders()
            folder_list = []
            for folder in result:
                folder_list.append(folder[2])
            return folder_list

        except Exception as e:
            print(e)
Example #5
0
    def get_email_content(self, master, data):
        print("Start 'get_email_content'")

        user_email = data["email"]
        user_password = mydb.get_email_dict(master)[user_email]["password"]
        folder_name = data["folder"]
        email_id = data["id"]
        index = data["index"]
        state = data["state"]
        responses = {}
        responses["master"] = master
        responses["method"] = "responses_get_email_content"
        responses["data"] = {
            "folder": folder_name,
            "state": state,
            "id": email_id,
            "email": user_email
        }

        try:
            server = self._server_login(user_email, user_password)
            server.select_folder(folder_name)

            message = server.fetch([email_id], ('RFC822'))
            msg = email.message_from_bytes(message[email_id][b'RFC822'])

            for part in msg.walk():
                charset = part.get_content_charset()
                if part.get_content_type() == 'text/html':
                    email_content = part.get_payload(
                        decode=True).decode(charset)
                    responses["data"]["email_content"] = email_content

            responses["success"] = True

        except Exception as e:
            print(e)
            responses["success"] = False

        self.io.emit("to-master", responses)

        if state == "unseen":
            mydb.change_email_flag(user_email, folder_name, index, None, state)
            self._change_flag(server, email_id, state)

        print("End 'get_email_content'")
Example #6
0
    def click_email(self, master, data):
        user_email = data["email"]
        email_details = mydb.get_email_dict(master)[user_email]
        added_folder_list = email_details["folder"]
        responses, responses["data"] = ({} for i in range(2))
        responses["master"] = master
        responses["method"] = "responses_click_email"
        responses["data"] = {
            "email": user_email,
            "folder": mydb.email_folder(user_email),
            "added_folder_list": added_folder_list,
            "total_folder_list": self.get_email_folder(master, user_email)
        }
        responses["success"] = True

        print("click email - {0}".format(user_email))

        self.io.emit("to-master", responses)
Example #7
0
    def _thread_idle_email(self, master, server, user_email, folder_name):
        user_password = mydb.get_email_dict(master)[user_email]["password"]

        server.select_folder(folder_name)
        current_thread = threading.currentThread()

        responses, responses["data"] = ({} for i in range(2))
        responses["master"] = master
        responses["method"] = "update_email"
        responses["data"][user_email] = {}

        try:
            while current_thread.do_run:
                server.idle()
                result = server.idle_check(600)
                if result:
                    mailbox, mailbox_list, unseen_num, do_update = self.update_email_box(
                        user_email, user_password, folder_name)
                    folder = {
                        "mailbox": mailbox,
                        "mailbox_list": mailbox_list,
                        "unseen_num": unseen_num
                    }

                    if do_update:
                        mydb.update_email_box(user_email, folder, folder_name)

                    responses["data"][user_email][folder_name] = folder
                    responses["success"] = True

                    self.io.emit("to-master", responses)

                server.idle_done()

        except Exception as e:
            print(e)
            responses["success"] = False
            self.io.emit("to-master", responses)
Example #8
0
    def get_email_box(self, master, data):
        print("Start 'get_email_box'")

        user_email = data["email"]
        user_password = mydb.get_email_dict(master)[user_email]["password"]
        folder_name = data["folder"]

        is_email_box = mydb.is_email_box(master, user_email, folder_name)

        folder, responses, responses["data"] = ({} for i in range(3))
        responses["master"] = master
        responses["method"] = "responses_get_email_box"
        responses["data"]["email"] = user_email

        try:
            if is_email_box:
                mailbox, mailbox_list, unseen_num, do_update = self.update_email_box(
                    user_email, user_password, folder_name)

                if do_update:
                    folder = {
                        "mailbox": mailbox,
                        "mailbox_list": mailbox_list,
                        "unseen_num": unseen_num
                    }

                    mydb.update_email_box(user_email, folder, folder_name)

            else:
                server = self._server_login(user_email, user_password)
                server.select_folder(folder_name)

                not_delete_list = server.search("NOT DELETED")
                not_delete_list.sort(reverse=True)
                mailbox_list = not_delete_list[:]
                unseen_list = server.search("UNSEEN")
                unseen_num = len(unseen_list)

                if unseen_list:
                    for item in unseen_list:
                        index = not_delete_list.index(item)
                        not_delete_list[index] = (item, "unseen")

                mailbox = self._thread_email_fetch(user_email, user_password,
                                                   folder_name,
                                                   not_delete_list)

                if mailbox:
                    mailbox = sorted(mailbox,
                                     key=lambda k: k['id'],
                                     reverse=True)

                    folder[folder_name] = {
                        "mailbox": mailbox,
                        "mailbox_list": mailbox_list,
                        "unseen_num": unseen_num
                    }
                    mydb.add_email_box(master, user_email, folder, folder_name)

            responses["data"]["folder"] = {
                folder_name: {
                    "mailbox": mailbox,
                    "mailbox_list": mailbox_list,
                    "unseen_num": unseen_num
                }
            }
            responses["success"] = True

        except Exception as e:
            print(e)
            responses["data"] = None
            responses["success"] = False
            mailbox = None

        self.io.emit("to-master", responses)

        print("End 'get_email_box'")