move.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #include "move.h"
  2. #include <stdlib.h>
  3. #include <lib/toolbox/args.h>
  4. #include "../../../lib/list/list.h"
  5. #include "../../../types/token_info.h"
  6. #include "../../../services/config/config.h"
  7. #include "../../cli_helpers.h"
  8. #include "../../../ui/scene_director.h"
  9. #include "../../common_command_arguments.h"
  10. #define TOTP_CLI_COMMAND_MOVE_ARG_NEW_INDEX "index"
  11. #define TOTP_CLI_COMMAND_MOVE_ARG_NEW_INDEX_PREFIX "-i"
  12. void totp_cli_command_move_docopt_commands() {
  13. TOTP_CLI_PRINTF(" " TOTP_CLI_COMMAND_MOVE ", " TOTP_CLI_COMMAND_MOVE_ALT
  14. " Move token\r\n");
  15. }
  16. void totp_cli_command_move_docopt_usage() {
  17. TOTP_CLI_PRINTF(
  18. " " TOTP_CLI_COMMAND_NAME
  19. " " DOCOPT_REQUIRED(TOTP_CLI_COMMAND_MOVE " | " TOTP_CLI_COMMAND_MOVE_ALT) " " DOCOPT_ARGUMENT(TOTP_CLI_COMMAND_ARG_INDEX) " " DOCOPT_OPTIONAL(
  20. DOCOPT_OPTION(
  21. TOTP_CLI_COMMAND_MOVE_ARG_NEW_INDEX_PREFIX,
  22. DOCOPT_ARGUMENT(TOTP_CLI_COMMAND_MOVE_ARG_NEW_INDEX))) "\r\n");
  23. }
  24. void totp_cli_command_move_docopt_options() {
  25. TOTP_CLI_PRINTF(" " DOCOPT_OPTION(
  26. TOTP_CLI_COMMAND_MOVE_ARG_NEW_INDEX_PREFIX,
  27. DOCOPT_ARGUMENT(TOTP_CLI_COMMAND_MOVE_ARG_NEW_INDEX)) " New token index\r\n");
  28. }
  29. void totp_cli_command_move_handle(PluginState* plugin_state, FuriString* args, Cli* cli) {
  30. if(!totp_cli_ensure_authenticated(plugin_state, cli)) {
  31. return;
  32. }
  33. int token_index;
  34. if(!args_read_int_and_trim(args, &token_index) || token_index < 1 ||
  35. token_index > plugin_state->tokens_count) {
  36. TOTP_CLI_PRINT_INVALID_ARGUMENTS();
  37. return;
  38. }
  39. FuriString* temp_str = furi_string_alloc();
  40. int new_token_index = 0;
  41. while(args_read_string_and_trim(args, temp_str)) {
  42. bool parsed = false;
  43. if(furi_string_cmpi_str(temp_str, TOTP_CLI_COMMAND_MOVE_ARG_NEW_INDEX_PREFIX) == 0) {
  44. if(!args_read_int_and_trim(args, &new_token_index)) {
  45. TOTP_CLI_PRINTF_ERROR(
  46. "Missed value for argument \"" TOTP_CLI_COMMAND_MOVE_ARG_NEW_INDEX_PREFIX
  47. "\"\r\n");
  48. } else if(new_token_index < 1 || new_token_index > plugin_state->tokens_count) {
  49. TOTP_CLI_PRINTF_ERROR(
  50. "\"%" PRId16
  51. "\" is incorrect value for argument \"" TOTP_CLI_COMMAND_MOVE_ARG_NEW_INDEX_PREFIX
  52. "\"\r\n",
  53. new_token_index);
  54. } else {
  55. parsed = true;
  56. }
  57. } else {
  58. TOTP_CLI_PRINTF_ERROR("Unknown argument \"%s\"\r\n", furi_string_get_cstr(temp_str));
  59. }
  60. if(!parsed) {
  61. TOTP_CLI_PRINT_INVALID_ARGUMENTS();
  62. furi_string_free(temp_str);
  63. return;
  64. }
  65. }
  66. if(!totp_cli_ensure_authenticated(plugin_state, cli)) {
  67. furi_string_free(temp_str);
  68. return;
  69. }
  70. bool activate_generate_token_scene = false;
  71. if(plugin_state->current_scene != TotpSceneAuthentication) {
  72. totp_scene_director_activate_scene(plugin_state, TotpSceneNone, NULL);
  73. activate_generate_token_scene = true;
  74. }
  75. bool token_updated = false;
  76. TokenInfo* token_info = NULL;
  77. if(new_token_index > 0) {
  78. plugin_state->tokens_list =
  79. list_remove_at(plugin_state->tokens_list, token_index - 1, (void**)&token_info);
  80. furi_check(token_info != NULL);
  81. plugin_state->tokens_list =
  82. list_insert_at(plugin_state->tokens_list, new_token_index - 1, token_info);
  83. token_updated = true;
  84. } else {
  85. token_info = list_element_at(plugin_state->tokens_list, token_index - 1)->data;
  86. }
  87. if(token_updated) {
  88. if(totp_full_save_config_file(plugin_state) == TotpConfigFileUpdateSuccess) {
  89. TOTP_CLI_PRINTF_SUCCESS(
  90. "Token \"%s\" has been successfully updated\r\n", token_info->name);
  91. } else {
  92. TOTP_CLI_PRINT_ERROR_UPDATING_CONFIG_FILE();
  93. }
  94. } else {
  95. TOTP_CLI_PRINT_INVALID_ARGUMENTS();
  96. }
  97. if(activate_generate_token_scene) {
  98. totp_scene_director_activate_scene(plugin_state, TotpSceneGenerateToken, NULL);
  99. }
  100. furi_string_free(temp_str);
  101. }