delete.c 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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(
  13. "\t" TOTP_CLI_COMMAND_DELETE
  14. " " TOTP_CLI_ARG(TOTP_CLI_COMMAND_DELETE_ARG_INDEX) " " TOTP_CLI_OPTIONAL_PARAM(
  15. TOTP_CLI_COMMAND_DELETE_ARG_FORCE_SUFFIX) " - delete token\r\n");
  16. TOTP_CLI_PRINTF(
  17. "\t\t" TOTP_CLI_ARG(TOTP_CLI_COMMAND_DELETE_ARG_INDEX) " - token index in the list\r\n");
  18. TOTP_CLI_PRINTF("\t\t" TOTP_CLI_COMMAND_DELETE_ARG_FORCE_SUFFIX
  19. " - " TOTP_CLI_OPTIONAL_PARAM_MARK
  20. " force command to do not ask user for interactive confirmation\r\n\r\n");
  21. }
  22. void totp_cli_command_delete_handle(PluginState* plugin_state, FuriString* args, Cli* cli) {
  23. int token_number;
  24. if(!args_read_int_and_trim(args, &token_number) || token_number <= 0 ||
  25. token_number > plugin_state->tokens_count) {
  26. totp_cli_print_invalid_arguments();
  27. return;
  28. }
  29. FuriString* temp_str = furi_string_alloc();
  30. bool confirm_needed = true;
  31. if(args_read_string_and_trim(args, temp_str)) {
  32. if(furi_string_cmpi_str(temp_str, TOTP_CLI_COMMAND_DELETE_ARG_FORCE_SUFFIX) == 0) {
  33. confirm_needed = false;
  34. } else {
  35. TOTP_CLI_PRINTF("Unknown argument \"%s\"\r\n", furi_string_get_cstr(temp_str));
  36. totp_cli_print_invalid_arguments();
  37. furi_string_free(temp_str);
  38. return;
  39. }
  40. }
  41. furi_string_free(temp_str);
  42. if(!totp_cli_ensure_authenticated(plugin_state, cli)) {
  43. return;
  44. }
  45. ListNode* list_node = list_element_at(plugin_state->tokens_list, token_number - 1);
  46. TokenInfo* token_info = list_node->data;
  47. bool confirmed = !confirm_needed;
  48. if(confirm_needed) {
  49. TOTP_CLI_PRINTF("WARNING!\r\n");
  50. TOTP_CLI_PRINTF(
  51. "TOKEN \"%s\" WILL BE PERMANENTLY DELETED WITHOUT ABILITY TO RECOVER IT.\r\n",
  52. token_info->name);
  53. TOTP_CLI_PRINTF("Confirm? [y/n]\r\n");
  54. fflush(stdout);
  55. char user_pick;
  56. do {
  57. user_pick = tolower(cli_getc(cli));
  58. } while(user_pick != 'y' && user_pick != 'n' && user_pick != CliSymbolAsciiCR &&
  59. user_pick != CliSymbolAsciiETX && user_pick != CliSymbolAsciiEsc);
  60. confirmed = user_pick == 'y' || user_pick == CliSymbolAsciiCR;
  61. }
  62. if(confirmed) {
  63. if(!totp_cli_ensure_authenticated(plugin_state, cli)) {
  64. return;
  65. }
  66. bool activate_generate_token_scene = false;
  67. if(plugin_state->current_scene != TotpSceneAuthentication) {
  68. totp_scene_director_activate_scene(plugin_state, TotpSceneNone, NULL);
  69. activate_generate_token_scene = true;
  70. }
  71. plugin_state->tokens_list = list_remove(plugin_state->tokens_list, list_node);
  72. plugin_state->tokens_count--;
  73. totp_full_save_config_file(plugin_state);
  74. if(activate_generate_token_scene) {
  75. totp_scene_director_activate_scene(plugin_state, TotpSceneGenerateToken, NULL);
  76. }
  77. TOTP_CLI_PRINTF("Token \"%s\" has been successfully deleted\r\n", token_info->name);
  78. token_info_free(token_info);
  79. } else {
  80. TOTP_CLI_PRINTF("User not confirmed\r\n");
  81. }
  82. }