main.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // xpg - the xkcd password generator
  2. // Main ('Runner') implementation file
  3. // author: lotus
  4. // license: 3-Clause BSD License
  5. #include "xpg.hpp"
  6. // --- Non-member functions ---
  7. int main(int argc, char* argv[]) {
  8. if (sodium_init() < 0) {
  9. // panic! the library couldn't be initialized, it is not safe to use
  10. return 1;
  11. }
  12. const std::string VERSION = "0.1";
  13. Params p{ "pascal", 6, 1, "_", false, VERSION, (std::string(getenv("HOME"))+="/.xpg/dict.txt") };
  14. bool ran_ok = false;
  15. // parse the user input flags & options
  16. CLI::App app{ "Generates a xkcd 'CorrectHorseBatteryStaple' style password." };
  17. app.ignore_case();
  18. app.add_flag("-v,--version", "Display the version of xpg");
  19. app.add_option("-c,--case", p.casing_desired, "Casing styles: pascal, camel, snake, lisp");
  20. app.add_option("-w,--words", p.num_words, "Number of words to use - defaults to 4");
  21. app.add_option("-n,--numpass", p.num_pass, "Number of passwords to create - defaults to 1");
  22. app.add_option("-p,--path", p.dict_path, "Manually specify the path of a dictionary to use.");
  23. CLI11_PARSE(app, argc, argv);
  24. bool args_ok = false;
  25. try {
  26. args_ok = check_args(p, app);
  27. }
  28. catch(...){
  29. std::cout << "Problem parsing arguments! (Bad input values used?)";
  30. }
  31. std::unique_ptr<Xpg> sp_xpg_obj;
  32. if (args_ok) {
  33. // good input, now construct object
  34. sp_xpg_obj = std::make_unique<Xpg>(p);
  35. // now try to read in the dictionary.
  36. bool read_in = false;
  37. try {
  38. if (sp_xpg_obj->file_exists(p.dict_path)) {
  39. read_in = sp_xpg_obj->read_dict(p.dict_path);
  40. }
  41. else {
  42. throw "File not found!";
  43. }
  44. }
  45. catch (...) {
  46. std::cout << "Exception trying to read dictionary file! (Does that file exist?)" << '\n';
  47. std::cout << "Put a dictionary at ~/.xpg/dict.txt" << '\n';
  48. }
  49. if (read_in) {
  50. // good read, now build password
  51. sp_xpg_obj->create_password();
  52. ran_ok = true;
  53. }
  54. }
  55. // exit, returning an int
  56. if (ran_ok) {
  57. return 0;
  58. }
  59. else {
  60. return 1;
  61. }
  62. }