list.c 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #include "list.h"
  2. #include <stdlib.h>
  3. #include <linked_list.h>
  4. #include "../../../types/token_info.h"
  5. #include "../../../services/config/constants.h"
  6. #include "../../cli_helpers.h"
  7. void totp_cli_command_list_docopt_commands() {
  8. TOTP_CLI_PRINTF(" " TOTP_CLI_COMMAND_LIST ", " TOTP_CLI_COMMAND_LIST_ALT
  9. " List all available tokens\r\n");
  10. }
  11. void totp_cli_command_list_docopt_usage() {
  12. TOTP_CLI_PRINTF(" " TOTP_CLI_COMMAND_NAME " " DOCOPT_REQUIRED(
  13. TOTP_CLI_COMMAND_LIST " | " TOTP_CLI_COMMAND_LIST_ALT) "\r\n");
  14. }
  15. void totp_cli_command_list_handle(PluginState* plugin_state, Cli* cli) {
  16. if(!totp_cli_ensure_authenticated(plugin_state, cli)) {
  17. return;
  18. }
  19. if(plugin_state->tokens_list == NULL) {
  20. TOTP_CLI_PRINTF("There are no tokens");
  21. return;
  22. }
  23. TOTP_CLI_PRINTF("+-----+---------------------------+--------+----+-----+\r\n");
  24. TOTP_CLI_PRINTF("| %-3s | %-25s | %-6s | %-s | %-s |\r\n", "#", "Name", "Algo", "Ln", "Dur");
  25. TOTP_CLI_PRINTF("+-----+---------------------------+--------+----+-----+\r\n");
  26. uint16_t index = 1;
  27. TOTP_LIST_FOREACH(plugin_state->tokens_list, node, {
  28. TokenInfo* token_info = (TokenInfo*)node->data;
  29. TOTP_CLI_PRINTF(
  30. "| %-3" PRIu16 " | %-25.25s | %-6s | %-2" PRIu8 " | %-3" PRIu8 " |\r\n",
  31. index,
  32. token_info->name,
  33. token_info_get_algo_as_cstr(token_info),
  34. token_info->digits,
  35. token_info->duration);
  36. index++;
  37. });
  38. TOTP_CLI_PRINTF("+-----+---------------------------+--------+----+-----+\r\n");
  39. }