move.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 "new_index"
  11. void totp_cli_command_move_docopt_commands() {
  12. TOTP_CLI_PRINTF(" " TOTP_CLI_COMMAND_MOVE ", " TOTP_CLI_COMMAND_MOVE_ALT
  13. " Move token\r\n");
  14. }
  15. void totp_cli_command_move_docopt_usage() {
  16. TOTP_CLI_PRINTF(
  17. " " TOTP_CLI_COMMAND_NAME
  18. " " DOCOPT_REQUIRED(TOTP_CLI_COMMAND_MOVE " | " TOTP_CLI_COMMAND_MOVE_ALT) " " DOCOPT_ARGUMENT(
  19. TOTP_CLI_COMMAND_ARG_INDEX) " " DOCOPT_ARGUMENT(TOTP_CLI_COMMAND_MOVE_ARG_NEW_INDEX) "\r\n");
  20. }
  21. void totp_cli_command_move_docopt_arguments() {
  22. TOTP_CLI_PRINTF(" " TOTP_CLI_COMMAND_MOVE_ARG_NEW_INDEX
  23. " New token index in the list\r\n");
  24. }
  25. void totp_cli_command_move_handle(PluginState* plugin_state, FuriString* args, Cli* cli) {
  26. if(!totp_cli_ensure_authenticated(plugin_state, cli)) {
  27. return;
  28. }
  29. int token_index;
  30. if(!args_read_int_and_trim(args, &token_index) || token_index < 1 ||
  31. token_index > plugin_state->tokens_count) {
  32. TOTP_CLI_PRINT_INVALID_ARGUMENTS();
  33. return;
  34. }
  35. int new_token_index = 0;
  36. if(!args_read_int_and_trim(args, &new_token_index) || new_token_index < 1 ||
  37. new_token_index > plugin_state->tokens_count) {
  38. TOTP_CLI_PRINT_INVALID_ARGUMENTS();
  39. return;
  40. }
  41. bool activate_generate_token_scene = false;
  42. if(plugin_state->current_scene != TotpSceneAuthentication) {
  43. totp_scene_director_activate_scene(plugin_state, TotpSceneNone, NULL);
  44. activate_generate_token_scene = true;
  45. }
  46. bool token_updated = false;
  47. TokenInfo* token_info = NULL;
  48. if(new_token_index > 0) {
  49. plugin_state->tokens_list =
  50. list_remove_at(plugin_state->tokens_list, token_index - 1, (void**)&token_info);
  51. furi_check(token_info != NULL);
  52. plugin_state->tokens_list =
  53. list_insert_at(plugin_state->tokens_list, new_token_index - 1, token_info);
  54. token_updated = true;
  55. } else {
  56. token_info = list_element_at(plugin_state->tokens_list, token_index - 1)->data;
  57. }
  58. if(token_updated) {
  59. if(totp_full_save_config_file(plugin_state) == TotpConfigFileUpdateSuccess) {
  60. TOTP_CLI_PRINTF_SUCCESS(
  61. "Token \"%s\" has been successfully updated\r\n", token_info->name);
  62. } else {
  63. TOTP_CLI_PRINT_ERROR_UPDATING_CONFIG_FILE();
  64. }
  65. } else {
  66. TOTP_CLI_PRINT_INVALID_ARGUMENTS();
  67. }
  68. if(activate_generate_token_scene) {
  69. totp_scene_director_activate_scene(plugin_state, TotpSceneGenerateToken, NULL);
  70. }
  71. }