fixed 1 segfault
Some checks failed
/ sync-to-origin (push) Has been cancelled

This commit is contained in:
PoliEcho 2025-01-29 14:55:22 +01:00
parent 4ba8710a71
commit 6985b21d37
4 changed files with 48 additions and 34 deletions

3
.gitignore vendored
View File

@ -3,4 +3,5 @@
compile_commands.json
core*
build
test-data
test-data
log

View File

@ -44,41 +44,45 @@ std::string bool_to_string(bool bool_in) { return bool_in ? "true" : "false"; }
std::string SoRAuthFile(bool save, std::string data) {
std::string savedir_path = std::getenv("HOME");
savedir_path.append("/.local/share/bakatui");
std::string home = std::getenv("HOME");
if (home.empty()) {
std::cerr << RED "[ERROR] " RESET << "HOME environment variable not set.\n";
safe_exit(EXIT_FAILURE);
}
DIR *savedir = opendir(savedir_path.c_str());
if (savedir) {
/* Directory exists. */
closedir(savedir);
} else if (ENOENT == errno) {
/* Directory does not exist. */
std::filesystem::create_directories(savedir_path);
} else {
/* opendir() failed for some other reason. */
std::cerr << "cannot access ~/.local/share/bakatui\n";
safe_exit(100);
}
std::string savedir_path = home;
savedir_path.append("/.local/share/bakatui");
std::string authfile_path = std::string(savedir_path) + "/auth";
if (save) {
std::ofstream authfile(authfile_path);
authfile << data;
authfile.close();
return "";
} else {
std::ifstream authfile(authfile_path);
if (authfile.is_open()) {
if (!std::filesystem::exists(savedir_path)) {
if (!std::filesystem::create_directories(savedir_path)) {
std::cerr << RED "[ERROR] " RESET << "Failed to create directory: " << savedir_path << "\n";
safe_exit(EXIT_FAILURE);
}
}
std::string authfile_path = savedir_path + "/auth";
if (save) {
std::ofstream authfile(authfile_path);
if (!authfile.is_open()) {
std::cerr << RED "[ERROR] " RESET << "Failed to open auth file for writing.\n";
safe_exit(EXIT_FAILURE);
}
authfile << data;
authfile.close();
return "";
} else {
std::ifstream authfile(authfile_path);
if (!authfile.is_open()) {
std::cerr << RED "[ERROR] " RESET << "Failed to open auth file for reading.\n";
safe_exit(EXIT_FAILURE);
}
data.assign((std::istreambuf_iterator<char>(authfile)),
std::istreambuf_iterator<char>());
authfile.close();
} else {
std::cerr << RED "[ERROR] " << RESET << "Failed to open auth file\n";
safe_exit(-2);
return data;
}
return data;
}
}
void get_input_and_login() {

View File

@ -90,6 +90,7 @@ void main_menu() {
menu_driver(my_menu, REQ_UP_ITEM);
break;
case 10: // ENTER
endwin();
choicesFuncs[item_index(current_item(my_menu))]();
pos_menu_cursor(my_menu);
break;

View File

@ -20,8 +20,12 @@
using nlohmann::json;
// metods
#define GET 0
#define POST 1
enum {
GET,
POST,
};
std::string access_token;
CURL *curl = curl_easy_init();
@ -76,14 +80,12 @@ std::tuple<std::string, int> send_curl_request(std::string endpoint,
int http_code = 0;
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code);
curl_easy_cleanup(curl); // Cleanup
return {response, http_code};
}
namespace bakaapi {
std::string access_token;
void login(std::string username, std::string password) {
std::string req_data =
@ -119,6 +121,8 @@ void login(std::string username, std::string password) {
}
void refresh_access_token() {
// DEBUG
std::clog << "refreshing access token please wait...\n";
json authfile_parsed = json::parse(SoRAuthFile(false, ""));
@ -129,6 +133,8 @@ void refresh_access_token() {
"token&refresh_token={}",
refresh_token);
// DEBUG
std::clog << "calling send_curl_request() with folowing req_data\n" << req_data << std::endl;
auto [response, http_code] = send_curl_request("api/login", POST, req_data);
if (http_code != 200) {
std::cerr << RED "[ERROR] " << RESET << http_code
@ -155,6 +161,8 @@ json get_grades() {
auto [response, http_code] = send_curl_request("api/3/marks", GET, req_data);
if(http_code != 200) {
// DEBUG
std::clog << "Failed geting marks code: " << http_code << "\nrequest: " << req_data <<"\nresponse: " << response << std::endl;
refresh_access_token();
}