delete.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #include "delete.h"
  2. #include <stdlib.h>
  3. #include <ctype.h>
  4. #include <lib/toolbox/args.h>
  5. #include "../../../services/config/config.h"
  6. #include "../../cli_helpers.h"
  7. #include "../../../ui/scene_director.h"
  8. #include "../../common_command_arguments.h"
  9. #define TOTP_CLI_COMMAND_DELETE_ARG_FORCE_PREFIX "-f"
  10. #ifdef TOTP_CLI_RICH_HELP_ENABLED
  11. void totp_cli_command_delete_docopt_commands() {
  12. TOTP_CLI_PRINTF(" " TOTP_CLI_COMMAND_DELETE ", " TOTP_CLI_COMMAND_DELETE_ALT
  13. " Delete existing token\r\n");
  14. }
  15. void totp_cli_command_delete_docopt_usage() {
  16. TOTP_CLI_PRINTF(
  17. " " TOTP_CLI_COMMAND_NAME
  18. " " DOCOPT_REQUIRED(TOTP_CLI_COMMAND_DELETE " | " TOTP_CLI_COMMAND_DELETE_ALT) " " DOCOPT_ARGUMENT(
  19. TOTP_CLI_COMMAND_ARG_INDEX) " " DOCOPT_OPTIONAL(DOCOPT_SWITCH(TOTP_CLI_COMMAND_DELETE_ARG_FORCE_PREFIX)) "\r\n");
  20. }
  21. void totp_cli_command_delete_docopt_arguments() {
  22. TOTP_CLI_PRINTF(" " TOTP_CLI_COMMAND_ARG_INDEX " Token index in the list\r\n");
  23. }
  24. void totp_cli_command_delete_docopt_options() {
  25. TOTP_CLI_PRINTF(" " DOCOPT_SWITCH(
  26. TOTP_CLI_COMMAND_DELETE_ARG_FORCE_PREFIX) " Force command to do not ask user for interactive confirmation\r\n");
  27. }
  28. #endif
  29. void totp_cli_command_delete_handle(PluginState* plugin_state, FuriString* args, Cli* cli) {
  30. if(!totp_cli_ensure_authenticated(plugin_state, cli)) {
  31. return;
  32. }
  33. TokenInfoIteratorContext* iterator_context =
  34. totp_config_get_token_iterator_context(plugin_state);
  35. int token_number;
  36. if(!args_read_int_and_trim(args, &token_number) || token_number <= 0 ||
  37. (size_t)token_number > totp_token_info_iterator_get_total_count(iterator_context)) {
  38. totp_cli_print_invalid_arguments();
  39. return;
  40. }
  41. FuriString* temp_str = furi_string_alloc();
  42. bool confirm_needed = true;
  43. if(args_read_string_and_trim(args, temp_str)) {
  44. if(furi_string_cmpi_str(temp_str, TOTP_CLI_COMMAND_DELETE_ARG_FORCE_PREFIX) == 0) {
  45. confirm_needed = false;
  46. } else {
  47. totp_cli_printf_unknown_argument(temp_str);
  48. totp_cli_print_invalid_arguments();
  49. furi_string_free(temp_str);
  50. return;
  51. }
  52. }
  53. furi_string_free(temp_str);
  54. TOTP_CLI_LOCK_UI(plugin_state);
  55. size_t original_token_index =
  56. totp_token_info_iterator_get_current_token_index(iterator_context);
  57. totp_token_info_iterator_go_to(iterator_context, token_number - 1);
  58. const TokenInfo* token_info = totp_token_info_iterator_get_current_token(iterator_context);
  59. const char* token_info_name = furi_string_get_cstr(token_info->name);
  60. bool confirmed = !confirm_needed;
  61. if(confirm_needed) {
  62. TOTP_CLI_PRINTF_WARNING("WARNING!\r\n");
  63. TOTP_CLI_PRINTF_WARNING(
  64. "TOKEN \"%s\" WILL BE PERMANENTLY DELETED WITHOUT ABILITY TO RECOVER IT.\r\n",
  65. token_info_name);
  66. TOTP_CLI_PRINTF_WARNING("Confirm? [y/n]\r\n");
  67. fflush(stdout);
  68. char user_pick;
  69. do {
  70. user_pick = tolower(cli_getc(cli));
  71. } while(user_pick != 'y' && user_pick != 'n' && user_pick != CliSymbolAsciiCR &&
  72. user_pick != CliSymbolAsciiETX && user_pick != CliSymbolAsciiEsc);
  73. confirmed = user_pick == 'y' || user_pick == CliSymbolAsciiCR;
  74. }
  75. if(confirmed) {
  76. totp_cli_print_processing();
  77. if(totp_token_info_iterator_remove_current_token_info(iterator_context)) {
  78. totp_cli_delete_last_line();
  79. TOTP_CLI_PRINTF_SUCCESS(
  80. "Token \"%s\" has been successfully deleted\r\n", token_info_name);
  81. totp_token_info_iterator_go_to(iterator_context, 0);
  82. } else {
  83. totp_cli_delete_last_line();
  84. totp_cli_print_error_updating_config_file();
  85. totp_token_info_iterator_go_to(iterator_context, original_token_index);
  86. }
  87. } else {
  88. TOTP_CLI_PRINTF_INFO("User has not confirmed\r\n");
  89. totp_token_info_iterator_go_to(iterator_context, original_token_index);
  90. }
  91. TOTP_CLI_UNLOCK_UI(plugin_state);
  92. }