add files back

This commit is contained in:
PoliEcho 2024-11-10 18:41:01 +01:00
parent b36003431d
commit c504d91b1a
9 changed files with 280 additions and 0 deletions

23
Makefile Normal file
View File

@ -0,0 +1,23 @@
CC = g++
CC_FLAGS = -std=c++23 -s -O3 -lncurses -lcurl -Wall -Wextra
#debug flags:
#CC_FLAGS = -ggdb -std=c++23 -lncurses -lcurl -Wall -Wextra
all: build/bin/bakatui
build/bin/bakatui: build/obj/main.o build/obj/net.o build/obj/helper_funcs.o
$(CC) $(CC_FLAGS) build/obj/main.o build/obj/net.o build/obj/helper_funcs.o -o build/bin/bakatui
build/obj/main.o: src/main.cpp
mkdir -p build/obj
mkdir -p build/bin
$(CC) $(CC_FLAGS) -c src/main.cpp -o build/obj/main.o
build/obj/net.o: src/net.cpp
$(CC) $(CC_FLAGS) -c src/net.cpp -o build/obj/net.o
build/obj/helper_funcs.o: src/helper_funcs.cpp
$(CC) $(CC_FLAGS) -c src/helper_funcs.cpp -o build/obj/helper_funcs.o
clean:
rm -fr build

5
README.md Normal file
View File

@ -0,0 +1,5 @@
# Bakatui
## bakalari for your terminal
> ### Dependencies:
> [nlohmann-json](https://github.com/nlohmann/json)

17
src/color.h Normal file
View File

@ -0,0 +1,17 @@
#define RESET "\033[0m"
#define BLACK "\033[30m" /* Black */
#define RED "\033[31m" /* Red */
#define GREEN "\033[32m" /* Green */
#define YELLOW "\033[33m" /* Yellow */
#define BLUE "\033[34m" /* Blue */
#define MAGENTA "\033[35m" /* Magenta */
#define CYAN "\033[36m" /* Cyan */
#define WHITE "\033[37m" /* White */
#define BOLDBLACK "\033[1m\033[30m" /* Bold Black */
#define BOLDRED "\033[1m\033[31m" /* Bold Red */
#define BOLDGREEN "\033[1m\033[32m" /* Bold Green */
#define BOLDYELLOW "\033[1m\033[33m" /* Bold Yellow */
#define BOLDBLUE "\033[1m\033[34m" /* Bold Blue */
#define BOLDMAGENTA "\033[1m\033[35m" /* Bold Magenta */
#define BOLDCYAN "\033[1m\033[36m" /* Bold Cyan */
#define BOLDWHITE "\033[1m\033[37m" /* Bold White */

34
src/helper_funcs.cpp Normal file
View File

@ -0,0 +1,34 @@
#include <csignal>
#include <curses.h>
#include <iostream>
#include <stdio.h>
#include <string>
#include <termios.h>
#include <unistd.h>
void safe_exit(int code) {
switch (code) {
case SIGTERM:
std::cerr << "\nreceived SIGTERM exiting...\n";
break;
case SIGINT:
std::cerr << "\nreceived SIGINT exiting...\n";
break;
case SIGQUIT:
std::cerr << "\nreceived SIGQUIT exiting...\n";
break;
case SIGHUP:
std::cerr << "\nreceived SIGHUP exiting...\n";
break;
case SIGSEGV:
std::cerr << "\nreceived SIGSEGV(segmentaiton fault) exiting...\nIf this "
"repeats please report it as bug\n";
break;
default:
break;
}
exit(code);
}

2
src/helper_funcs.h Normal file
View File

@ -0,0 +1,2 @@
#include <string>
void safe_exit(int code);

80
src/main.cpp Normal file
View File

@ -0,0 +1,80 @@
#include "main.h"
#include "color.h"
#include "helper_funcs.h"
#include "net.h"
#include <csignal>
#include <curl/curl.h>
#include <curses.h>
#include <iostream>
#include <regex>
#include <string>
#include <unistd.h>
std::string baka_api_url;
int main(int argc, char **argv) {
// signal handlers
signal(SIGTERM, safe_exit);
signal(SIGINT, safe_exit);
signal(SIGQUIT, safe_exit);
signal(SIGHUP, safe_exit);
// error signal handlers
signal(SIGSEGV, safe_exit);
/*initscr();
// creating a window;
// with height = 15 and width = 10
// also with start x axis 10 and start y axis = 20
WINDOW *win = newwin(15, 17, 2, 10);
refresh();
// making box border with default border styles
box(win, 0, 0);
// move and print in window
mvwprintw(win, 0, 1, "Greeter");
mvwprintw(win, 1, 1, "Hello");
// refreshing the window
wrefresh(win);
getch();
endwin(); */
std::cout << "enter school bakalari url:\n";
while (true) {
std::cout << "(or q to quit )";
std::cin >> baka_api_url;
const std::regex url_regex_pattern(
R"((http|https)://(www\.)?[a-zA-Z0-9@:%._\+~#?&//=]{2,256}\.[a-z]{2,6}(/\S*)?)");
if (std::regex_match(baka_api_url, url_regex_pattern)) {
break;
} else if (baka_api_url == "q") {
std::cerr << GREEN "[NOTICE] " << RESET "user quit\n";
return 255;
}
std::cerr << "enter valid url using following pattern "
"[(http|https)://school.bakalari.url]\n";
}
if (baka_api_url.back() != '/') {
baka_api_url.append("/");
}
{
std::string username;
std::cout << "enter username: ";
std::cin >> username;
std::string password;
password = getpass("enter password: ");
// DEBUG
// std::cout << "\nenter password: ";
// std::cin >> password;
bakaapi::login(username, password);
}
return 0;
}

4
src/main.h Normal file
View File

@ -0,0 +1,4 @@
#include <curl/curl.h>
#include <string>
extern std::string baka_api_url;

110
src/net.cpp Normal file
View File

@ -0,0 +1,110 @@
#include "net.h"
#include "color.h"
#include "helper_funcs.h"
#include "main.h"
#include <cmath>
#include <cstddef>
#include <cstdlib>
#include <cstring>
#include <curl/curl.h>
#include <dirent.h>
#include <errno.h>
#include <filesystem>
#include <format>
#include <fstream>
#include <iostream>
#include <nlohmann/json.hpp>
#include <string>
using nlohmann::json;
CURL *curl = curl_easy_init();
// Callback function to write data into a std::string
size_t WriteCallback(void *contents, size_t size, size_t nmemb,
std::string *userp) {
size_t totalSize = size * nmemb;
userp->append((char *)contents, totalSize);
return totalSize;
}
std::string send_curl_request(std::string endpoint, std::string type,
std::string req_data) {
std::string response;
std::string url = baka_api_url + endpoint;
if (curl) {
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, req_data.c_str());
struct curl_slist *headers = NULL;
headers = curl_slist_append(
headers, "Content-Type: application/x-www-form-urlencoded");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
if (type == "POST") {
curl_easy_setopt(curl, CURLOPT_POST, 1L);
} else {
std::cerr << RED "[ERROR] " << RESET "invalid method\n";
safe_exit(-5);
}
} else {
std::cerr << RED "[ERROR] " << RESET "curl not initialised\n";
safe_exit(20);
}
curl_easy_perform(curl); // Perform the request
curl_easy_cleanup(curl); // Cleanup
return response;
}
namespace bakaapi {
std::string access_token;
void login(std::string username, std::string password) {
std::string req_data =
std::format("client_id=ANDR&grant_type=password&username={}&password={}",
username, password);
std::string response = send_curl_request("api/login", "POST", req_data);
std::string savedir_path = std::getenv("HOME");
savedir_path.append("/.local/share/bakatui");
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 authfile_path = std::string(savedir_path) + "/auth";
{
std::ofstream authfile;
authfile.open(authfile_path);
authfile << response;
authfile.close();
}
json resp_parsed = json::parse(response);
access_token = resp_parsed["access_token"];
std::cout << "access token: " << access_token << std::endl;
}
} // namespace bakaapi

5
src/net.h Normal file
View File

@ -0,0 +1,5 @@
#include <curl/curl.h>
#include <string>
namespace bakaapi {
void login(std::string username, std::string password);
}