Example #1
0
    async def _help_welcome(self, _account: Optional["Account"],
                            _cmd: Callback, _params: Tuple) -> str:
        """
        Handle welcome help message event
        """

        welcome = Message.info(f"Welcome to nuqql-matrixd v{VERSION}!")
        welcome += Message.info("Enter \"help\" for a list of available "
                                "commands and their help texts")
        if self.based.config.get_push_accounts():
            welcome += Message.info("Listing your accounts:")
        return welcome
Example #2
0
    async def _help_account_add(_account: Optional["Account"], _cmd: Callback,
                                _params: Tuple) -> str:
        """
        Handle account add help event
        """

        add_help = Message.info("You do not have any accounts configured.")
        add_help += Message.info("You can add a new matrix account with the "
                                 "following command: "
                                 "account add matrix <username>@<homeserver> "
                                 "<password>")
        add_help += Message.info("Example: account add matrix "
                                 "[email protected] MyPassword")
        return add_help
Example #3
0
    async def _handle_account_collect(self, acc_id: int,
                                      params: List[str]) -> str:
        """
        Collect messages for a specific account.

        Expected format:
            account <ID> collect [time]

        params does not include "account <ID> collect"
        """

        # collect all messages since <time>?
        time = 0  # TODO: change it to time of last collect?
        if len(params) >= 1:
            time = int(params[0])

        # log event
        log_msg = f"account {acc_id} collect {time}"
        logging.info(log_msg)

        # collect messages
        accounts = self.account_list.get()
        acc = accounts[acc_id]
        history = acc.get_history()
        # TODO: this expects a list. change to string? document list req?
        history += await self.callbacks.call(Callback.COLLECT_MESSAGES, acc,
                                             ())

        # append info message to notify caller that everything was collected
        history += [Message.info(f"collected messages for account {acc_id}.")]

        # return history as single string
        return "".join(history)
Example #4
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 #5
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
Example #6
0
    async def _handle_version(self) -> Tuple[str, str]:
        """
        Handle the version command received from client
        """

        msg = await self.callbacks.call(Callback.VERSION, None, ())
        if not msg:
            name = self.config.get_name()
            version = self.config.get_version()
            msg = f"version: {name} v{version}"
        return ("msg", Message.info(msg))
Example #7
0
    async def _handle_account_delete(self, acc_id: int) -> str:
        """
        Delete an existing account

        Expected format:
            account <ID> delete
        """

        # delete account
        result = await self.account_list.delete(acc_id)

        # inform caller about result
        return Message.info(result)
Example #8
0
    async def _handle_account_buddies(self, acc_id: int,
                                      params: List[str]) -> str:
        """
        Get buddies for a specific account. If params contains "online", filter
        online buddies.

        Expected format:
            account <ID> buddies [online]

        params does not include "account <ID> buddies"

        Returned messages should look like:
            buddy: <acc_id> status: <Offline/Available> name: <name> alias:
                <alias>
        """

        # get account
        accounts = self.account_list.get()
        acc = accounts[acc_id]

        # filter online buddies?
        online = False
        if len(params) >= 1 and params[0].lower() == "online":
            online = True

        # update buddy list
        result = await self.callbacks.call(Callback.GET_BUDDIES, acc,
                                           (online, ))

        # add info message that all buddies have been received
        info = Message.info(f"got buddies for account {acc_id}.")

        # log event
        log_msg = f"account {acc_id} buddies: {result}"
        logging.info(log_msg)

        # return replies as single string
        return result + info