move.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #include <flipper_application/flipper_application.h>
  2. #include <lib/toolbox/args.h>
  3. #include "../../cli_helpers.h"
  4. #include "../../cli_shared_methods.h"
  5. #include "../../cli_plugin_interface.h"
  6. #include "../../../types/token_info.h"
  7. #include "../../../services/config/config.h"
  8. #include "../../../ui/scene_director.h"
  9. static void handle(PluginState* plugin_state, FuriString* args, PipeSide* pipe) {
  10. if(!totp_cli_ensure_authenticated(plugin_state, pipe)) {
  11. return;
  12. }
  13. int token_number;
  14. TokenInfoIteratorContext* iterator_context =
  15. totp_config_get_token_iterator_context(plugin_state);
  16. size_t total_count = totp_token_info_iterator_get_total_count(iterator_context);
  17. if(!args_read_int_and_trim(args, &token_number) || token_number < 1 ||
  18. (size_t)token_number > total_count) {
  19. TOTP_CLI_PRINT_INVALID_ARGUMENTS();
  20. return;
  21. }
  22. int new_token_number = 0;
  23. if(!args_read_int_and_trim(args, &new_token_number) || new_token_number < 1 ||
  24. (size_t)new_token_number > total_count) {
  25. TOTP_CLI_PRINT_INVALID_ARGUMENTS();
  26. return;
  27. }
  28. if(token_number == new_token_number) {
  29. TOTP_CLI_PRINTF_ERROR("New token number matches current token number\r\n");
  30. return;
  31. }
  32. TOTP_CLI_LOCK_UI(plugin_state);
  33. size_t token_index = token_number - 1;
  34. size_t new_token_index = new_token_number - 1;
  35. size_t original_token_index =
  36. totp_token_info_iterator_get_current_token_index(iterator_context);
  37. TOTP_CLI_PRINT_PROCESSING();
  38. if(totp_token_info_iterator_go_to(iterator_context, token_index) &&
  39. totp_token_info_iterator_move_current_token_info(iterator_context, new_token_index)) {
  40. TOTP_CLI_DELETE_LAST_LINE();
  41. TOTP_CLI_PRINTF_SUCCESS(
  42. "Token \"%s\" has been successfully updated\r\n",
  43. furi_string_get_cstr(
  44. totp_token_info_iterator_get_current_token(iterator_context)->name));
  45. } else {
  46. TOTP_CLI_DELETE_LAST_LINE();
  47. TOTP_CLI_PRINT_ERROR_UPDATING_CONFIG_FILE();
  48. }
  49. totp_token_info_iterator_go_to(iterator_context, original_token_index);
  50. TOTP_CLI_UNLOCK_UI(plugin_state);
  51. }
  52. static const CliPlugin plugin = {.name = "TOTP CLI Plugin: Move", .handle = &handle};
  53. static const FlipperAppPluginDescriptor plugin_descriptor = {
  54. .appid = PLUGIN_APP_ID,
  55. .ep_api_version = PLUGIN_API_VERSION,
  56. .entry_point = &plugin,
  57. };
  58. const FlipperAppPluginDescriptor* totp_cli_move_plugin_ep() {
  59. return &plugin_descriptor;
  60. }