delete.c 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. if (!totp_cli_ensure_authenticated(plugin_state, cli)) {
  36. return;
  37. }
  38. ListNode* list_node = list_element_at(plugin_state->tokens_list, token_number - 1);
  39. TokenInfo* token_info = list_node->data;
  40. bool confirmed = !confirm_needed;
  41. if (confirm_needed) {
  42. TOTP_CLI_PRINTF("WARNING!\r\n");
  43. TOTP_CLI_PRINTF("TOKEN \"%s\" WILL BE PERMANENTLY DELETED WITHOUT ABILITY TO RECOVER IT.\r\n", token_info->name);
  44. TOTP_CLI_PRINTF("Confirm? [y/n]\r\n");
  45. fflush(stdout);
  46. char user_pick;
  47. do {
  48. user_pick = tolower(cli_getc(cli));
  49. } while (user_pick != 'y' &&
  50. user_pick != 'n' &&
  51. user_pick != CliSymbolAsciiCR &&
  52. user_pick != CliSymbolAsciiETX &&
  53. user_pick != CliSymbolAsciiEsc);
  54. confirmed = user_pick == 'y' || user_pick == CliSymbolAsciiCR;
  55. }
  56. if (confirmed) {
  57. if (!totp_cli_ensure_authenticated(plugin_state, cli)) {
  58. return;
  59. }
  60. bool activate_generate_token_scene = false;
  61. if (plugin_state->current_scene != TotpSceneAuthentication) {
  62. totp_scene_director_activate_scene(plugin_state, TotpSceneNone, NULL);
  63. activate_generate_token_scene = true;
  64. }
  65. plugin_state->tokens_list = list_remove(plugin_state->tokens_list, list_node);
  66. plugin_state->tokens_count--;
  67. totp_full_save_config_file(plugin_state);
  68. if (activate_generate_token_scene) {
  69. totp_scene_director_activate_scene(plugin_state, TotpSceneGenerateToken, NULL);
  70. }
  71. TOTP_CLI_PRINTF("Token \"%s\" has been successfully deleted\r\n", token_info->name);
  72. token_info_free(token_info);
  73. } else {
  74. TOTP_CLI_PRINTF("User not confirmed\r\n");
  75. }
  76. }