/* * Created by yuuta on 4/1/22. */ #include "botd.h" #include "log.h" #include #include #include #include #include #include static inline void parse_api_id(const char *str) { char *endptr; intmax_t num = strtoimax(str, &endptr, 10); if (strcmp(endptr, "") != 0 || (num == INTMAX_MAX && errno == ERANGE) || num > INT_MAX || num < INT_MIN) { LOGFEV("Invalid API Hash: %s.", 64, str); } cmd.api_id = (int) num; } void parse_cmdline(int argc, char **argv) { for (int i = 1; i < argc; i++) { const char *arg = argv[i]; if (arg[0] != '-' || arg[1] != '-') LOGFEV("Unexpected argument: %s.", 64, arg); const char *a = &arg[2]; if (!strcmp(a, "test")) { LOGD("Test DC is in use."); cmd.test_dc = true; } else if (!strcmp(a, "logout")) { cmd.logout = true; } else if (!strcmp(a, "api_id")) { if (i == (argc - 1)) LOGFEV("%s expects an argument.", 64, arg); parse_api_id(argv[++i]); } else if (!strcmp(a, "api_hash")) { if (i == (argc - 1)) LOGFEV("%s expects an argument.", 64, arg); cmd.api_hash = argv[++i]; } else if (!strcmp(a, "bot_token")) { if (i == (argc - 1)) LOGFEV("%s expects an argument.", 64, arg); cmd.bot_token = argv[++i]; } else if (!strcmp(a, "td_path")) { if (i == (argc - 1)) LOGFEV("%s expects an argument.", 64, arg); cmd.td_path = argv[++i]; } else if (!strcmp(a, "db_path")) { if (i == (argc - 1)) LOGFEV("%s expects an argument.", 64, arg); cmd.db_path = argv[++i]; } else if (!strcmp(a, "help")) { printf("Usage: " "[TD_API_ID=] " "[TD_API_HASH=] " "[TD_PATH=] " "[BOT_TOKEN=] " "[DB_PATH=] " "%s " "[--help] " "[--logout] " "[--test] " "[--api_id TD_API_ID] " "[--api_hash TD_API_HASH] " "[--td_path TD_PATH] " "[--bot_token BOT_TOKEN] " "[--db_path DB_PATH] ", argv[0]); exit(0); } else { LOGFEV("Invalid command: %s. Use %s --help to view usage.", 64, arg, argv[0]); } } if (!cmd.api_hash && !(cmd.api_hash = getenv("TD_API_HASH"))) LOGFEV("Required environment variable %s is missing.", 64, "TD_API_HASH"); if (!cmd.bot_token && !(cmd.bot_token = getenv("BOT_TOKEN"))) LOGFEV("Required environment variable %s is missing.", 64, "BOT_TOKEN"); if (!cmd.td_path && !(cmd.td_path = getenv("TD_PATH"))) LOGFEV("Required environment variable %s is missing.", 64, "TD_PATH"); if (!cmd.db_path && !(cmd.db_path = getenv("DB_PATH"))) LOGFEV("Required environment variable %s is missing.", 64, "DB_PATH"); if (!cmd.api_id) { const char *a; if (!(a = getenv("TD_API_ID"))) LOGFEV("Required environment variable %s is missing.", 64, "TD_API_ID"); parse_api_id(a); } }