delete.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 =
  32. totp_config_get_token_iterator_context(plugin_state);
  33. int token_number;
  34. if(!args_read_int_and_trim(args, &token_number) || token_number <= 0 ||
  35. (size_t)token_number > 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 =
  54. totp_token_info_iterator_get_current_token_index(iterator_context);
  55. totp_token_info_iterator_go_to(iterator_context, token_number - 1);
  56. const TokenInfo* token_info = totp_token_info_iterator_get_current_token(iterator_context);
  57. const char* token_info_name = furi_string_get_cstr(token_info->name);
  58. bool confirmed = !confirm_needed;
  59. if(confirm_needed) {
  60. TOTP_CLI_PRINTF_WARNING("WARNING!\r\n");
  61. TOTP_CLI_PRINTF_WARNING(
  62. "TOKEN \"%s\" WILL BE PERMANENTLY DELETED WITHOUT ABILITY TO RECOVER IT.\r\n",
  63. token_info_name);
  64. TOTP_CLI_PRINTF_WARNING("Confirm? [y/n]\r\n");
  65. fflush(stdout);
  66. char user_pick;
  67. do {
  68. user_pick = tolower(cli_getc(cli));
  69. } while(user_pick != 'y' && user_pick != 'n' && user_pick != CliSymbolAsciiCR &&
  70. user_pick != CliSymbolAsciiETX && user_pick != CliSymbolAsciiEsc);
  71. confirmed = user_pick == 'y' || user_pick == CliSymbolAsciiCR;
  72. }
  73. if(confirmed) {
  74. totp_cli_print_processing();
  75. if(totp_token_info_iterator_remove_current_token_info(iterator_context)) {
  76. totp_cli_delete_last_line();
  77. TOTP_CLI_PRINTF_SUCCESS(
  78. "Token \"%s\" has been successfully deleted\r\n", token_info_name);
  79. totp_token_info_iterator_go_to(iterator_context, 0);
  80. } else {
  81. totp_cli_delete_last_line();
  82. totp_cli_print_error_updating_config_file();
  83. totp_token_info_iterator_go_to(iterator_context, original_token_index);
  84. }
  85. } else {
  86. TOTP_CLI_PRINTF_INFO("User has not confirmed\r\n");
  87. totp_token_info_iterator_go_to(iterator_context, original_token_index);
  88. }
  89. TOTP_CLI_UNLOCK_UI(plugin_state);
  90. }