Example #1
0
    async def handle_account_list(self) -> str:
        """
        List all accounts
        """

        replies = []
        accounts = self.account_list.get()
        for acc in accounts.values():
            reply = Message.account(acc)
            replies.append(reply)

        # inform caller that all accounts have been received
        replies.append(Message.info("listed accounts."))

        # add account add help if there are no accounts
        if not accounts:
            replies.append(await self.callbacks.call(Callback.HELP_ACCOUNT_ADD,
                                                     None, ()))

        # log event
        log_msg = f"account list: {replies}"
        logging.info(log_msg)

        # return a single string
        return "".join(replies)
Example #2
0
    async def add(self,
                  acc_type: str,
                  acc_user: str,
                  acc_pass: str,
                  acc_id: int = None) -> str:
        """
        Add a new account
        """

        # make sure the account does not exist
        for acc in self.accounts.values():
            if acc.type == acc_type and acc.user == acc_user:
                return Message.info("account already exists.")

        # get a free account id if none is given
        if acc_id is None:
            acc_id = self._get_free_account_id()

        # create account and add it to list
        new_acc = Account(config=self.config,
                          callbacks=self.callbacks,
                          queue=self.queue,
                          aid=acc_id)
        new_acc.type = acc_type
        new_acc.user = acc_user
        new_acc.password = acc_pass
        self.accounts[new_acc.aid] = new_acc

        # store updated accounts in file
        self.store()

        # log event
        log_msg = (f"account new: id {new_acc.aid} type {new_acc.type} "
                   f"user {new_acc.user}")
        logging.info(log_msg)

        # notify callback (if present) about new account
        await self.callbacks.call(Callback.ADD_ACCOUNT, new_acc, ())

        # return result
        result = Message.info(f"added account {new_acc.aid}.")
        if self.config.get_push_accounts():
            result += Message.account(new_acc)
        return result