delete.c 4.0 KB

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