cli.c 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // Original idea: https://github.com/br0ziliy
  2. #include "cli.h"
  3. #include <lib/toolbox/args.h>
  4. #include "commands/list/list.h"
  5. #include "commands/add/add.h"
  6. #include "commands/delete/delete.h"
  7. #define TOTP_CLI_COMMAND_NAME "totp"
  8. static void totp_cli_print_unknown_command(FuriString* unknown_command) {
  9. printf("Command \"%s\" is unknown. Use \"help\" command to get list of available commands.", furi_string_get_cstr(unknown_command));
  10. }
  11. static void totp_cli_print_help() {
  12. printf("Usage:\r\n");
  13. printf("totp <command> <arguments>\r\n");
  14. printf("Command list:\r\n");
  15. printf("\thelp - print command usage help\r\n");
  16. printf("\tlist - list all tokens\r\n");
  17. printf("\tdelete <INDEX> [-f] - delete token\r\n");
  18. printf("\t\t<INDEX> - token index in the list\r\n");
  19. printf("\t\t-f - [OPTIONAL] force command to do not ask user for interactive confirmation\r\n");
  20. printf("\tadd <NAME> <SECRET> [-a <ALGO>] [-d <DIGITS>] - add new token\r\n");
  21. printf("\t\t<NAME> - token name\r\n");
  22. printf("\t\t<SECRET> - Base32 token secret\r\n");
  23. printf("\t\t<ALGO> - [OPTIONAL] token hashing algorithm, could be one of: sha1, sha256, sha512; default: sha1\r\n");
  24. printf("\t\t<DIGITS> - [OPTIONAL] number of digits to generate, one of: 6, 8; default: 6\r\n\r\n");
  25. }
  26. static void totp_cli_print_unauthenticated() {
  27. printf("Pleases enter PIN on your flipper device\r\n");
  28. }
  29. static void totp_cli_handler(Cli* cli, FuriString* args, void* context) {
  30. PluginState* plugin_state = (PluginState* )context;
  31. if (plugin_state->current_scene == TotpSceneAuthentication) {
  32. totp_cli_print_unauthenticated();
  33. while (plugin_state->current_scene == TotpSceneAuthentication && !cli_cmd_interrupt_received(cli)) {
  34. furi_delay_tick(0);
  35. }
  36. if (plugin_state->current_scene == TotpSceneAuthentication) {
  37. return;
  38. }
  39. }
  40. FuriString* cmd = furi_string_alloc();
  41. args_read_string_and_trim(args, cmd);
  42. if(furi_string_cmp_str(cmd, "help") == 0 || furi_string_empty(cmd)) {
  43. totp_cli_print_help();
  44. } else if(furi_string_cmp_str(cmd, "add") == 0) {
  45. totp_cli_handle_add_command(plugin_state, args);
  46. } else if(furi_string_cmp_str(cmd, "list") == 0) {
  47. totp_cli_handle_list_command(plugin_state);
  48. } else if(furi_string_cmp_str(cmd, "delete") == 0) {
  49. totp_cli_handle_delete_command(plugin_state, args, cli);
  50. } else {
  51. totp_cli_print_unknown_command(cmd);
  52. }
  53. furi_string_free(cmd);
  54. }
  55. void totp_cli_register_command_handler(PluginState* plugin_state) {
  56. Cli* cli = furi_record_open(RECORD_CLI);
  57. cli_add_command(cli, TOTP_CLI_COMMAND_NAME, CliCommandFlagParallelSafe, totp_cli_handler, plugin_state);
  58. furi_record_close(RECORD_CLI);
  59. }
  60. void totp_cli_unregister_command_handler() {
  61. Cli* cli = furi_record_open(RECORD_CLI);
  62. cli_delete_command(cli, TOTP_CLI_COMMAND_NAME);
  63. furi_record_close(RECORD_CLI);
  64. }