details.c 4.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #include "details.h"
  2. #include <stdlib.h>
  3. #include <lib/toolbox/args.h>
  4. #include "../../../types/token_info.h"
  5. #include "../../../services/config/constants.h"
  6. #include "../../../services/config/config.h"
  7. #include "../../../ui/scene_director.h"
  8. #include "../../cli_helpers.h"
  9. #include "../../common_command_arguments.h"
  10. #define TOTP_CLI_PRINTF_AUTOMATION_FEATURE(description, header_printed) \
  11. do { \
  12. TOTP_CLI_PRINTF( \
  13. "| %-20s | %-28.28s |\r\n", \
  14. header_printed ? "" : "Automation features", \
  15. description); \
  16. header_printed = true; \
  17. } while(false)
  18. static void print_automation_features(const TokenInfo* token_info) {
  19. if(token_info->automation_features == TokenAutomationFeatureNone) {
  20. TOTP_CLI_PRINTF("| %-20s | %-28.28s |\r\n", "Automation features", "None");
  21. return;
  22. }
  23. bool header_printed = false;
  24. if(token_info->automation_features & TokenAutomationFeatureEnterAtTheEnd) {
  25. TOTP_CLI_PRINTF_AUTOMATION_FEATURE("Type <Enter> key at the end", header_printed);
  26. }
  27. if(token_info->automation_features & TokenAutomationFeatureTabAtTheEnd) {
  28. TOTP_CLI_PRINTF_AUTOMATION_FEATURE("Type <Tab> key at the end", header_printed);
  29. }
  30. if(token_info->automation_features & TokenAutomationFeatureTypeSlower) {
  31. TOTP_CLI_PRINTF_AUTOMATION_FEATURE("Type slower", header_printed);
  32. }
  33. }
  34. #ifdef TOTP_CLI_RICH_HELP_ENABLED
  35. void totp_cli_command_details_docopt_commands() {
  36. TOTP_CLI_PRINTF(" " TOTP_CLI_COMMAND_DETAILS ", " TOTP_CLI_COMMAND_DETAILS_ALT
  37. " Displays token details\r\n");
  38. }
  39. void totp_cli_command_details_docopt_usage() {
  40. TOTP_CLI_PRINTF(" " TOTP_CLI_COMMAND_NAME " " DOCOPT_REQUIRED(
  41. TOTP_CLI_COMMAND_DETAILS
  42. " | " TOTP_CLI_COMMAND_DETAILS_ALT) " " DOCOPT_ARGUMENT(TOTP_CLI_COMMAND_ARG_INDEX) "\r\n");
  43. }
  44. #endif
  45. void totp_cli_command_details_handle(PluginState* plugin_state, FuriString* args, Cli* cli) {
  46. if(!totp_cli_ensure_authenticated(plugin_state, cli)) {
  47. return;
  48. }
  49. int token_number;
  50. TokenInfoIteratorContext* iterator_context =
  51. totp_config_get_token_iterator_context(plugin_state);
  52. if(!args_read_int_and_trim(args, &token_number) || token_number <= 0 ||
  53. (size_t)token_number > totp_token_info_iterator_get_total_count(iterator_context)) {
  54. totp_cli_print_invalid_arguments();
  55. return;
  56. }
  57. TOTP_CLI_LOCK_UI(plugin_state);
  58. size_t original_token_index =
  59. totp_token_info_iterator_get_current_token_index(iterator_context);
  60. if(totp_token_info_iterator_go_to(iterator_context, token_number - 1)) {
  61. const TokenInfo* token_info = totp_token_info_iterator_get_current_token(iterator_context);
  62. TOTP_CLI_PRINTF("+----------------------+------------------------------+\r\n");
  63. TOTP_CLI_PRINTF("| %-20s | %-28s |\r\n", "Property", "Value");
  64. TOTP_CLI_PRINTF("+----------------------+------------------------------+\r\n");
  65. TOTP_CLI_PRINTF("| %-20s | %-28d |\r\n", "Index", token_number);
  66. TOTP_CLI_PRINTF(
  67. "| %-20s | %-28.28s |\r\n", "Name", furi_string_get_cstr(token_info->name));
  68. TOTP_CLI_PRINTF(
  69. "| %-20s | %-28s |\r\n", "Hashing algorithm", token_info_get_algo_as_cstr(token_info));
  70. TOTP_CLI_PRINTF("| %-20s | %-28" PRIu8 " |\r\n", "Number of digits", token_info->digits);
  71. TOTP_CLI_PRINTF(
  72. "| %-20s | %" PRIu8 " sec.%-21s |\r\n", "Token lifetime", token_info->duration, " ");
  73. print_automation_features(token_info);
  74. TOTP_CLI_PRINTF("+----------------------+------------------------------+\r\n");
  75. } else {
  76. totp_cli_print_error_loading_token_info();
  77. }
  78. totp_token_info_iterator_go_to(iterator_context, original_token_index);
  79. TOTP_CLI_UNLOCK_UI(plugin_state);
  80. }