delete.c 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #include "delete.h"
  2. #include <stdlib.h>
  3. #include <ctype.h>
  4. #include <lib/toolbox/args.h>
  5. #include "../../../list/list.h"
  6. #include "../../../config/config.h"
  7. #include "../../cli_common_helpers.h"
  8. #include "../../../../scenes/scene_director.h"
  9. #define TOTP_CLI_COMMAND_DELETE_ARG_INDEX "INDEX"
  10. #define TOTP_CLI_COMMAND_DELETE_ARG_FORCE_SUFFIX "-f"
  11. void totp_cli_command_delete_print_help() {
  12. TOTP_CLI_PRINTF("\t" TOTP_CLI_COMMAND_DELETE " " TOTP_CLI_ARG(TOTP_CLI_COMMAND_DELETE_ARG_INDEX) " " TOTP_CLI_OPTIONAL_PARAM(TOTP_CLI_COMMAND_DELETE_ARG_FORCE_SUFFIX) " - delete token\r\n");
  13. TOTP_CLI_PRINTF("\t\t" TOTP_CLI_ARG(TOTP_CLI_COMMAND_DELETE_ARG_INDEX) " - token index in the list\r\n");
  14. TOTP_CLI_PRINTF("\t\t" TOTP_CLI_COMMAND_DELETE_ARG_FORCE_SUFFIX " - " TOTP_CLI_OPTIONAL_PARAM_MARK " force command to do not ask user for interactive confirmation\r\n\r\n");
  15. }
  16. void totp_cli_command_delete_handle(PluginState* plugin_state, FuriString* args, Cli* cli) {
  17. int token_number;
  18. if (!args_read_int_and_trim(args, &token_number) || token_number <= 0 || token_number > plugin_state->tokens_count) {
  19. totp_cli_print_invalid_arguments();
  20. return;
  21. }
  22. FuriString* temp_str = furi_string_alloc();
  23. bool confirm_needed = true;
  24. if (args_read_string_and_trim(args, temp_str)) {
  25. if (furi_string_cmpi_str(temp_str, TOTP_CLI_COMMAND_DELETE_ARG_FORCE_SUFFIX) == 0) {
  26. confirm_needed = false;
  27. } else {
  28. TOTP_CLI_PRINTF("Unknown argument \"%s\"\r\n", furi_string_get_cstr(temp_str));
  29. totp_cli_print_invalid_arguments();
  30. furi_string_free(temp_str);
  31. return;
  32. }
  33. }
  34. furi_string_free(temp_str);
  35. ListNode* list_node = list_element_at(plugin_state->tokens_list, token_number - 1);
  36. TokenInfo* token_info = list_node->data;
  37. bool confirmed = !confirm_needed;
  38. if (confirm_needed) {
  39. TOTP_CLI_PRINTF("WARNING!\r\n");
  40. TOTP_CLI_PRINTF("TOKEN \"%s\" WILL BE PERMANENTLY DELETED WITHOUT ABILITY TO RECOVER IT.\r\n", token_info->name);
  41. TOTP_CLI_PRINTF("Confirm? [y/n]\r\n");
  42. fflush(stdout);
  43. char user_pick;
  44. do {
  45. user_pick = tolower(cli_getc(cli));
  46. } while (user_pick != 'y' && user_pick != 'n' && user_pick != CliSymbolAsciiCR);
  47. confirmed = user_pick == 'y' || user_pick == CliSymbolAsciiCR;
  48. }
  49. if (confirmed) {
  50. bool activate_generate_token_scene = false;
  51. if (plugin_state->current_scene != TotpSceneAuthentication) {
  52. totp_scene_director_activate_scene(plugin_state, TotpSceneNone, NULL);
  53. activate_generate_token_scene = true;
  54. }
  55. plugin_state->tokens_list = list_remove(plugin_state->tokens_list, list_node);
  56. plugin_state->tokens_count--;
  57. totp_full_save_config_file(plugin_state);
  58. if (activate_generate_token_scene) {
  59. totp_scene_director_activate_scene(plugin_state, TotpSceneGenerateToken, NULL);
  60. }
  61. TOTP_CLI_PRINTF("Token \"%s\" has been successfully deleted\r\n", token_info->name);
  62. token_info_free(token_info);
  63. } else {
  64. TOTP_CLI_PRINTF("User not confirmed\r\n");
  65. }
  66. }