delete.c 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #include <flipper_application/flipper_application.h>
  2. #include <lib/toolbox/args.h>
  3. #include "../../cli_helpers.h"
  4. #include "../../cli_shared_methods.h"
  5. #include "../../cli_plugin_interface.h"
  6. #include "../../../services/config/config.h"
  7. #include "../../../ui/scene_director.h"
  8. #define TOTP_CLI_COMMAND_DELETE_ARG_FORCE_PREFIX "-f"
  9. static void handle(PluginState* plugin_state, FuriString* args, Cli* cli) {
  10. if(!totp_cli_ensure_authenticated(plugin_state, cli)) {
  11. return;
  12. }
  13. TokenInfoIteratorContext* iterator_context =
  14. totp_config_get_token_iterator_context(plugin_state);
  15. int token_number;
  16. if(!args_read_int_and_trim(args, &token_number) || token_number <= 0 ||
  17. (size_t)token_number > totp_token_info_iterator_get_total_count(iterator_context)) {
  18. TOTP_CLI_PRINT_INVALID_ARGUMENTS();
  19. return;
  20. }
  21. FuriString* temp_str = furi_string_alloc();
  22. bool confirm_needed = true;
  23. if(args_read_string_and_trim(args, temp_str)) {
  24. if(furi_string_cmpi_str(temp_str, TOTP_CLI_COMMAND_DELETE_ARG_FORCE_PREFIX) == 0) {
  25. confirm_needed = false;
  26. } else {
  27. totp_cli_printf_unknown_argument(temp_str);
  28. TOTP_CLI_PRINT_INVALID_ARGUMENTS();
  29. furi_string_free(temp_str);
  30. return;
  31. }
  32. }
  33. furi_string_free(temp_str);
  34. TOTP_CLI_LOCK_UI(plugin_state);
  35. size_t original_token_index =
  36. totp_token_info_iterator_get_current_token_index(iterator_context);
  37. totp_token_info_iterator_go_to(iterator_context, token_number - 1);
  38. const TokenInfo* token_info = totp_token_info_iterator_get_current_token(iterator_context);
  39. const char* token_info_name = furi_string_get_cstr(token_info->name);
  40. bool confirmed = !confirm_needed;
  41. if(confirm_needed) {
  42. TOTP_CLI_PRINTF_WARNING("WARNING!\r\n");
  43. TOTP_CLI_PRINTF_WARNING(
  44. "TOKEN \"%s\" WILL BE PERMANENTLY DELETED WITHOUT ABILITY TO RECOVER IT.\r\n",
  45. token_info_name);
  46. TOTP_CLI_PRINTF_WARNING("Confirm? [y/n]\r\n");
  47. fflush(stdout);
  48. char user_pick;
  49. do {
  50. user_pick = tolower(cli_getc(cli));
  51. } while(user_pick != 'y' && user_pick != 'n' && user_pick != CliSymbolAsciiCR &&
  52. user_pick != CliSymbolAsciiETX && user_pick != CliSymbolAsciiEsc);
  53. confirmed = user_pick == 'y' || user_pick == CliSymbolAsciiCR;
  54. }
  55. if(confirmed) {
  56. TOTP_CLI_PRINT_PROCESSING();
  57. if(totp_token_info_iterator_remove_current_token_info(iterator_context)) {
  58. TOTP_CLI_DELETE_LAST_LINE();
  59. TOTP_CLI_PRINTF_SUCCESS(
  60. "Token \"%s\" has been successfully deleted\r\n", token_info_name);
  61. totp_token_info_iterator_go_to(iterator_context, 0);
  62. } else {
  63. TOTP_CLI_DELETE_LAST_LINE();
  64. TOTP_CLI_PRINT_ERROR_UPDATING_CONFIG_FILE();
  65. totp_token_info_iterator_go_to(iterator_context, original_token_index);
  66. }
  67. } else {
  68. TOTP_CLI_PRINTF_INFO("User has not confirmed\r\n");
  69. totp_token_info_iterator_go_to(iterator_context, original_token_index);
  70. }
  71. TOTP_CLI_UNLOCK_UI(plugin_state);
  72. }
  73. static const CliPlugin plugin = {.name = "TOTP CLI Plugin: Delete", .handle = &handle};
  74. static const FlipperAppPluginDescriptor plugin_descriptor = {
  75. .appid = PLUGIN_APP_ID,
  76. .ep_api_version = PLUGIN_API_VERSION,
  77. .entry_point = &plugin,
  78. };
  79. const FlipperAppPluginDescriptor* totp_cli_delete_plugin_ep() {
  80. return &plugin_descriptor;
  81. }