Compare commits
No commits in common. "a78f4536b2dd1012a30e2292ee0a11cf5c337b87" and "575101780d9a563a92c5b91ff89a659743e4509e" have entirely different histories.
a78f4536b2
...
575101780d
197
src/komens.cpp
197
src/komens.cpp
@ -2,31 +2,24 @@
|
|||||||
#include "helper_funcs.h"
|
#include "helper_funcs.h"
|
||||||
#include "memory.h"
|
#include "memory.h"
|
||||||
#include "net.h"
|
#include "net.h"
|
||||||
#include "types.h"
|
|
||||||
#include <cmath>
|
|
||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <curses.h>
|
#include <curses.h>
|
||||||
#include <cwchar>
|
#include <cwchar>
|
||||||
#include <menu.h>
|
#include <menu.h>
|
||||||
#include <ncurses.h>
|
|
||||||
#include <nlohmann/json.hpp>
|
#include <nlohmann/json.hpp>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#define MAIN_WIN_PROPORTION 0.714f
|
#define WIN_HIGHT 40
|
||||||
|
|
||||||
#define MAIN_WIN_HIGHT (roundf(LINES * MAIN_WIN_PROPORTION))
|
|
||||||
|
|
||||||
#define DEFAULT_OFSET 4
|
#define DEFAULT_OFSET 4
|
||||||
|
|
||||||
using nlohmann::json;
|
using nlohmann::json;
|
||||||
|
|
||||||
std::vector<allocation> komens_allocated;
|
std::vector<allocation> komens_allocated;
|
||||||
|
|
||||||
void insert_content(WINDOW *content_win, WINDOW *attachment_win,
|
void insert_content(WINDOW *content_window, size_t i, json &resp_from_api);
|
||||||
json &resp_from_api);
|
|
||||||
|
|
||||||
void komens_page(koment_type type) {
|
void komens_page(koment_type type) {
|
||||||
current_allocated = &komens_allocated;
|
current_allocated = &komens_allocated;
|
||||||
@ -55,13 +48,9 @@ void komens_page(koment_type type) {
|
|||||||
for (uint8_t i = 0; i < 8; i++) {
|
for (uint8_t i = 0; i < 8; i++) {
|
||||||
init_pair(i, i, COLOR_BLACK);
|
init_pair(i, i, COLOR_BLACK);
|
||||||
}
|
}
|
||||||
|
|
||||||
complete_menu komens_choise_menu;
|
|
||||||
|
|
||||||
size_t num_of_komens = resp_from_api["Messages"].size();
|
size_t num_of_komens = resp_from_api["Messages"].size();
|
||||||
komens_choise_menu.items = new ITEM *[num_of_komens + 1];
|
ITEM **komens_items = new ITEM *[num_of_komens + 1];
|
||||||
komens_allocated.push_back(
|
komens_allocated.push_back({ITEM_ARRAY, komens_items, num_of_komens});
|
||||||
{ITEM_ARRAY, komens_choise_menu.items, num_of_komens});
|
|
||||||
|
|
||||||
char **title_bufs = new char *[num_of_komens];
|
char **title_bufs = new char *[num_of_komens];
|
||||||
char **name_bufs = new char *[num_of_komens];
|
char **name_bufs = new char *[num_of_komens];
|
||||||
@ -105,48 +94,46 @@ void komens_page(koment_type type) {
|
|||||||
name_bufs[i] = new char[strlen(tmp_buf) + 1];
|
name_bufs[i] = new char[strlen(tmp_buf) + 1];
|
||||||
strlcpy(name_bufs[i], tmp_buf, strlen(tmp_buf) + 1);
|
strlcpy(name_bufs[i], tmp_buf, strlen(tmp_buf) + 1);
|
||||||
|
|
||||||
komens_choise_menu.items[i] = new_item(title_bufs[i], name_bufs[i]);
|
komens_items[i] = new_item(title_bufs[i], name_bufs[i]);
|
||||||
}
|
}
|
||||||
max_item_lenght = 3 + max_title_lenght + 1 + max_name_lenght;
|
max_item_lenght = 3 + max_title_lenght + 1 + max_name_lenght;
|
||||||
}
|
}
|
||||||
komens_choise_menu.items[num_of_komens] = nullptr;
|
komens_items[num_of_komens] = nullptr;
|
||||||
|
|
||||||
komens_choise_menu.menu = new_menu(komens_choise_menu.items);
|
MENU *komens_choise_menu = new_menu(komens_items);
|
||||||
komens_allocated.push_back({MENU_TYPE, komens_choise_menu.menu, 1});
|
komens_allocated.push_back({MENU_TYPE, komens_choise_menu, 1});
|
||||||
|
|
||||||
komens_choise_menu.win =
|
WINDOW *komens_choise_menu_win =
|
||||||
newwin(MAIN_WIN_HIGHT, max_item_lenght + 1, DEFAULT_OFSET, DEFAULT_OFSET);
|
newwin(WIN_HIGHT, max_item_lenght + 1, DEFAULT_OFSET, DEFAULT_OFSET);
|
||||||
komens_allocated.push_back({WINDOW_TYPE, komens_choise_menu.win, 1});
|
komens_allocated.push_back({WINDOW_TYPE, komens_choise_menu_win, 1});
|
||||||
|
|
||||||
set_menu_win(komens_choise_menu.menu, komens_choise_menu.win);
|
set_menu_win(komens_choise_menu, komens_choise_menu_win);
|
||||||
set_menu_sub(komens_choise_menu.menu,
|
set_menu_sub(komens_choise_menu,
|
||||||
derwin(komens_choise_menu.win, MAIN_WIN_HIGHT - 10,
|
derwin(komens_choise_menu_win, WIN_HIGHT - 10, max_item_lenght,
|
||||||
max_item_lenght, DEFAULT_OFSET - 1, DEFAULT_OFSET - 3));
|
DEFAULT_OFSET - 1, DEFAULT_OFSET - 3));
|
||||||
set_menu_format(komens_choise_menu.menu, MAIN_WIN_HIGHT - 5, 1);
|
set_menu_format(komens_choise_menu, WIN_HIGHT - 5, 1);
|
||||||
|
|
||||||
set_menu_mark(komens_choise_menu.menu, " * ");
|
set_menu_mark(komens_choise_menu, " * ");
|
||||||
|
|
||||||
box(komens_choise_menu.win, 0, 0);
|
box(komens_choise_menu_win, 0, 0);
|
||||||
|
|
||||||
wprint_in_middle(komens_choise_menu.win, 1, 0, max_item_lenght, L"Komens",
|
wprint_in_middle(komens_choise_menu_win, 1, 0, max_item_lenght, L"Komens",
|
||||||
COLOR_PAIR(1));
|
COLOR_PAIR(1));
|
||||||
mvwaddch(komens_choise_menu.win, 2, 0, ACS_LTEE);
|
mvwaddch(komens_choise_menu_win, 2, 0, ACS_LTEE);
|
||||||
mvwhline(komens_choise_menu.win, 2, 1, ACS_HLINE, max_item_lenght - 1);
|
mvwhline(komens_choise_menu_win, 2, 1, ACS_HLINE, max_item_lenght - 1);
|
||||||
mvwaddch(komens_choise_menu.win, 2, max_item_lenght, ACS_RTEE);
|
mvwaddch(komens_choise_menu_win, 2, max_item_lenght, ACS_RTEE);
|
||||||
|
|
||||||
post_menu(komens_choise_menu.menu);
|
post_menu(komens_choise_menu);
|
||||||
wrefresh(komens_choise_menu.win);
|
wrefresh(komens_choise_menu_win);
|
||||||
|
|
||||||
WINDOW *content_win =
|
WINDOW *content_window =
|
||||||
newwin(MAIN_WIN_HIGHT, COLS - max_item_lenght - DEFAULT_OFSET - 1,
|
newwin(WIN_HIGHT, COLS - max_item_lenght - DEFAULT_OFSET - 1,
|
||||||
DEFAULT_OFSET, DEFAULT_OFSET + max_item_lenght + 1);
|
DEFAULT_OFSET, DEFAULT_OFSET + max_item_lenght + 1);
|
||||||
komens_allocated.push_back({WINDOW_TYPE, content_win, 1});
|
komens_allocated.push_back({WINDOW_TYPE, content_window, 1});
|
||||||
|
box(content_window, 0, 0);
|
||||||
WINDOW *attachment_win = newwin(1, 1, LINES, COLS);
|
insert_content(content_window, item_index(current_item(komens_choise_menu)),
|
||||||
|
resp_from_api);
|
||||||
insert_content(content_win, attachment_win,
|
wrefresh(content_window);
|
||||||
resp_from_api["Messages"][item_index(
|
|
||||||
current_item(komens_choise_menu.menu))]);
|
|
||||||
|
|
||||||
attron(COLOR_PAIR(COLOR_BLUE));
|
attron(COLOR_PAIR(COLOR_BLUE));
|
||||||
mvprintw(LINES - 2, 0,
|
mvprintw(LINES - 2, 0,
|
||||||
@ -161,125 +148,25 @@ void komens_page(koment_type type) {
|
|||||||
case KEY_DOWN:
|
case KEY_DOWN:
|
||||||
case KEY_NPAGE:
|
case KEY_NPAGE:
|
||||||
case 'j':
|
case 'j':
|
||||||
menu_driver(komens_choise_menu.menu, REQ_DOWN_ITEM);
|
menu_driver(komens_choise_menu, REQ_DOWN_ITEM);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case KEY_UP:
|
case KEY_UP:
|
||||||
case KEY_PPAGE:
|
case KEY_PPAGE:
|
||||||
case 'k':
|
case 'k':
|
||||||
menu_driver(komens_choise_menu.menu, REQ_UP_ITEM);
|
menu_driver(komens_choise_menu, REQ_UP_ITEM);
|
||||||
break;
|
|
||||||
default:
|
|
||||||
if (c >= '1' && c <= '9') {
|
|
||||||
size_t index = c - '0' - 1;
|
|
||||||
if (index < resp_from_api["Messages"][item_index(
|
|
||||||
current_item(komens_choise_menu.menu))]["Attachments"]
|
|
||||||
.size()) {
|
|
||||||
|
|
||||||
std::string default_path =
|
|
||||||
"~/Downloads/" +
|
|
||||||
resp_from_api["Messages"][item_index(current_item(
|
|
||||||
komens_choise_menu.menu))]["Attachments"][index]["Name"]
|
|
||||||
.get<std::string>();
|
|
||||||
char path[256];
|
|
||||||
|
|
||||||
// Create input prompt at bottom of screen
|
|
||||||
move(LINES - 1, 0);
|
|
||||||
clrtoeol();
|
|
||||||
printw("Save path [%s]: ", default_path.c_str());
|
|
||||||
echo();
|
|
||||||
curs_set(1);
|
|
||||||
getnstr(path, sizeof(path) - 1);
|
|
||||||
if (strlen(path) == 0)
|
|
||||||
strcpy(path, default_path.c_str());
|
|
||||||
noecho();
|
|
||||||
curs_set(0);
|
|
||||||
move(LINES - 1, 0);
|
|
||||||
clrtoeol();
|
|
||||||
refresh();
|
|
||||||
|
|
||||||
// Download the attachment
|
|
||||||
bakaapi::download_attachment(
|
|
||||||
resp_from_api["Messages"][item_index(current_item(
|
|
||||||
komens_choise_menu.menu))]["Attachments"][index]["Id"]
|
|
||||||
.get<std::string>(),
|
|
||||||
path);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
insert_content(content_window, item_index(current_item(komens_choise_menu)),
|
||||||
insert_content(content_win, attachment_win,
|
resp_from_api);
|
||||||
resp_from_api["Messages"][item_index(
|
wrefresh(content_window);
|
||||||
current_item(komens_choise_menu.menu))]);
|
wrefresh(komens_choise_menu_win);
|
||||||
wrefresh(komens_choise_menu.win);
|
|
||||||
}
|
}
|
||||||
delete_all(&komens_allocated);
|
delete_all(&komens_allocated);
|
||||||
}
|
}
|
||||||
|
|
||||||
void insert_content(WINDOW *content_win, WINDOW *attachment_win,
|
void insert_content(WINDOW *content_window, size_t i, json &resp_from_api) {
|
||||||
json &message) {
|
wclear(content_window);
|
||||||
wclear(content_win);
|
mvwprintw(content_window, 0, 0, "%s",
|
||||||
mvwprintw(content_win, 0, 0, "%s",
|
html_to_string(resp_from_api.at("Messages")[i]["Text"]).c_str());
|
||||||
html_to_string(message.at("Text")).c_str());
|
}
|
||||||
wrefresh(content_win);
|
|
||||||
if (!message.at("Attachments").empty()) {
|
|
||||||
|
|
||||||
size_t max_item_lenght = 0;
|
|
||||||
{
|
|
||||||
size_t max_name_lenght = 0;
|
|
||||||
size_t max_size_lenght = 0;
|
|
||||||
size_t tmp_lenght;
|
|
||||||
|
|
||||||
for (size_t j = 0; j < message.at("Attachments").size(); j++) {
|
|
||||||
tmp_lenght =
|
|
||||||
message.at("Attachments")[j]["Name"].get<std::string>().length();
|
|
||||||
if (tmp_lenght > max_name_lenght) {
|
|
||||||
max_name_lenght = tmp_lenght;
|
|
||||||
}
|
|
||||||
|
|
||||||
tmp_lenght =
|
|
||||||
std::to_string(message.at("Attachments")[j]["Size"].get<size_t>())
|
|
||||||
.length();
|
|
||||||
if (tmp_lenght > max_size_lenght) {
|
|
||||||
max_size_lenght = tmp_lenght;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
max_item_lenght = 3 + max_name_lenght + 1 + max_size_lenght;
|
|
||||||
}
|
|
||||||
|
|
||||||
mvwin(attachment_win, MAIN_WIN_HIGHT + DEFAULT_OFSET + 1,
|
|
||||||
COLS - max_item_lenght - 2);
|
|
||||||
wresize(attachment_win, LINES - (MAIN_WIN_HIGHT + DEFAULT_OFSET + 1),
|
|
||||||
max_item_lenght + 2);
|
|
||||||
|
|
||||||
wborder(attachment_win, 0, ' ', 0, 0, 0, ACS_HLINE, 0, ACS_HLINE);
|
|
||||||
print_in_middle(attachment_win, 0, 0, max_item_lenght + 2, "Attachments",
|
|
||||||
COLOR_PAIR(COLOR_RED));
|
|
||||||
for (size_t j = 0; j < message.at("Attachments").size(); j++) {
|
|
||||||
|
|
||||||
mvwprintw(
|
|
||||||
attachment_win, j + 1, 2, "%s %s",
|
|
||||||
message.at("Attachments")[j]["Name"].get<std::string>().c_str(),
|
|
||||||
std::to_string(message.at("Attachments")[j]["Size"].get<size_t>())
|
|
||||||
.c_str());
|
|
||||||
|
|
||||||
wattron(attachment_win, COLOR_PAIR(COLOR_MAGENTA));
|
|
||||||
mvwprintw(attachment_win, j + 1, 0, "%zu>", j + 1);
|
|
||||||
wattroff(attachment_win, COLOR_PAIR(COLOR_MAGENTA));
|
|
||||||
}
|
|
||||||
{ // remove duplicating spaces
|
|
||||||
unsigned short attachment_win_top, attachment_win_left,
|
|
||||||
attachment_win_height, attachment_win_width;
|
|
||||||
getbegyx(attachment_win, attachment_win_top, attachment_win_left);
|
|
||||||
getmaxyx(attachment_win, attachment_win_height, attachment_win_width);
|
|
||||||
|
|
||||||
mvvline(attachment_win_top, attachment_win_left - 1, ' ',
|
|
||||||
attachment_win_height);
|
|
||||||
}
|
|
||||||
refresh();
|
|
||||||
|
|
||||||
wrefresh(attachment_win);
|
|
||||||
}
|
|
||||||
}
|
|
@ -2,7 +2,6 @@
|
|||||||
#include "helper_funcs.h"
|
#include "helper_funcs.h"
|
||||||
#include "komens.h"
|
#include "komens.h"
|
||||||
#include "net.h"
|
#include "net.h"
|
||||||
#include "types.h"
|
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <curses.h>
|
#include <curses.h>
|
||||||
@ -13,9 +12,10 @@ void komens_menu() {
|
|||||||
L"received", L"sent", L"noticeboard", L"Exit", nullptr,
|
L"received", L"sent", L"noticeboard", L"Exit", nullptr,
|
||||||
};
|
};
|
||||||
|
|
||||||
complete_menu komens_menu;
|
ITEM **my_items;
|
||||||
|
|
||||||
int c;
|
int c;
|
||||||
|
MENU *my_menu;
|
||||||
|
WINDOW *my_menu_win;
|
||||||
int n_choices, i;
|
int n_choices, i;
|
||||||
|
|
||||||
/* Initialize curses */
|
/* Initialize curses */
|
||||||
@ -30,37 +30,37 @@ void komens_menu() {
|
|||||||
|
|
||||||
/* Create items */
|
/* Create items */
|
||||||
n_choices = ARRAY_SIZE(choices);
|
n_choices = ARRAY_SIZE(choices);
|
||||||
komens_menu.items = (ITEM **)calloc(n_choices, sizeof(ITEM *));
|
my_items = (ITEM **)calloc(n_choices, sizeof(ITEM *));
|
||||||
for (i = 0; i < n_choices; ++i)
|
for (i = 0; i < n_choices; ++i)
|
||||||
komens_menu.items[i] =
|
my_items[i] =
|
||||||
new_item(wchar_to_char(choices[i]), wchar_to_char(choices[i]));
|
new_item(wchar_to_char(choices[i]), wchar_to_char(choices[i]));
|
||||||
|
|
||||||
/* Crate menu */
|
/* Crate menu */
|
||||||
komens_menu.menu = new_menu(komens_menu.items);
|
my_menu = new_menu((ITEM **)my_items);
|
||||||
|
|
||||||
/* Create the window to be associated with the menu */
|
/* Create the window to be associated with the menu */
|
||||||
komens_menu.win = newwin(12, 40, 4, 4);
|
my_menu_win = newwin(12, 40, 4, 4);
|
||||||
keypad(komens_menu.win, TRUE);
|
keypad(my_menu_win, TRUE);
|
||||||
|
|
||||||
/* Set main window and sub window */
|
/* Set main window and sub window */
|
||||||
set_menu_win(komens_menu.menu, komens_menu.win);
|
set_menu_win(my_menu, my_menu_win);
|
||||||
set_menu_sub(komens_menu.menu, derwin(komens_menu.win, 8, 38, 3, 1));
|
set_menu_sub(my_menu, derwin(my_menu_win, 8, 38, 3, 1));
|
||||||
set_menu_format(komens_menu.menu, 7, 1);
|
set_menu_format(my_menu, 7, 1);
|
||||||
|
|
||||||
/* Set menu mark to the string " * " */
|
/* Set menu mark to the string " * " */
|
||||||
set_menu_mark(komens_menu.menu, " * ");
|
set_menu_mark(my_menu, " * ");
|
||||||
|
|
||||||
/* Print a border around the main window and print a title */
|
/* Print a border around the main window and print a title */
|
||||||
box(komens_menu.win, 0, 0);
|
box(my_menu_win, 0, 0);
|
||||||
|
|
||||||
wprint_in_middle(komens_menu.win, 1, 0, 40, L"Komens Menu", COLOR_PAIR(1));
|
wprint_in_middle(my_menu_win, 1, 0, 40, L"Komens Menu", COLOR_PAIR(1));
|
||||||
mvwaddch(komens_menu.win, 2, 0, ACS_LTEE);
|
mvwaddch(my_menu_win, 2, 0, ACS_LTEE);
|
||||||
mvwhline(komens_menu.win, 2, 1, ACS_HLINE, 38);
|
mvwhline(my_menu_win, 2, 1, ACS_HLINE, 38);
|
||||||
mvwaddch(komens_menu.win, 2, 39, ACS_RTEE);
|
mvwaddch(my_menu_win, 2, 39, ACS_RTEE);
|
||||||
|
|
||||||
/* Post the menu */
|
/* Post the menu */
|
||||||
post_menu(komens_menu.menu);
|
post_menu(my_menu);
|
||||||
wrefresh(komens_menu.win);
|
wrefresh(my_menu_win);
|
||||||
|
|
||||||
attron(COLOR_PAIR(2));
|
attron(COLOR_PAIR(2));
|
||||||
mvprintw(LINES - 2, 0,
|
mvprintw(LINES - 2, 0,
|
||||||
@ -74,34 +74,33 @@ void komens_menu() {
|
|||||||
case KEY_DOWN:
|
case KEY_DOWN:
|
||||||
case KEY_NPAGE:
|
case KEY_NPAGE:
|
||||||
case 'j':
|
case 'j':
|
||||||
menu_driver(komens_menu.menu, REQ_DOWN_ITEM);
|
menu_driver(my_menu, REQ_DOWN_ITEM);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case KEY_UP:
|
case KEY_UP:
|
||||||
case KEY_PPAGE:
|
case KEY_PPAGE:
|
||||||
case 'k':
|
case 'k':
|
||||||
menu_driver(komens_menu.menu, REQ_UP_ITEM);
|
menu_driver(my_menu, REQ_UP_ITEM);
|
||||||
break;
|
break;
|
||||||
case 10: // ENTER
|
case 10: // ENTER
|
||||||
clear();
|
clear();
|
||||||
if (item_index(current_item(komens_menu.menu)) == n_choices - 1) {
|
if (item_index(current_item(my_menu)) == n_choices - 1) {
|
||||||
goto close_menu;
|
goto close_menu;
|
||||||
}
|
}
|
||||||
komens_page(
|
komens_page(static_cast<koment_type>(item_index(current_item(my_menu))));
|
||||||
static_cast<koment_type>(item_index(current_item(komens_menu.menu))));
|
pos_menu_cursor(my_menu);
|
||||||
pos_menu_cursor(komens_menu.menu);
|
|
||||||
refresh();
|
refresh();
|
||||||
wrefresh(komens_menu.win);
|
wrefresh(my_menu_win);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
wrefresh(komens_menu.win);
|
wrefresh(my_menu_win);
|
||||||
}
|
}
|
||||||
close_menu:
|
close_menu:
|
||||||
|
|
||||||
/* Unpost and free all the memory taken up */
|
/* Unpost and free all the memory taken up */
|
||||||
unpost_menu(komens_menu.menu);
|
unpost_menu(my_menu);
|
||||||
free_menu(komens_menu.menu);
|
free_menu(my_menu);
|
||||||
for (i = 0; i < n_choices; ++i)
|
for (i = 0; i < n_choices; ++i)
|
||||||
free_item(komens_menu.items[i]);
|
free_item(my_items[i]);
|
||||||
endwin();
|
endwin();
|
||||||
}
|
}
|
||||||
|
35
src/main.cpp
35
src/main.cpp
@ -1,10 +1,8 @@
|
|||||||
#include "main.h"
|
#include "main.h"
|
||||||
#include "color.h"
|
#include "color.h"
|
||||||
#include "flags.h"
|
|
||||||
#include "helper_funcs.h"
|
#include "helper_funcs.h"
|
||||||
#include "main_menu.h"
|
#include "main_menu.h"
|
||||||
#include "net.h"
|
#include "net.h"
|
||||||
#include "types.h"
|
|
||||||
#include <csignal>
|
#include <csignal>
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
#include <curl/curl.h>
|
#include <curl/curl.h>
|
||||||
@ -14,6 +12,8 @@
|
|||||||
#include <regex>
|
#include <regex>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
#include "flags.h"
|
||||||
|
#include "types.h"
|
||||||
|
|
||||||
std::string baka_api_url;
|
std::string baka_api_url;
|
||||||
|
|
||||||
@ -36,27 +36,14 @@ int main(int argc, char **argv) {
|
|||||||
|
|
||||||
int opt;
|
int opt;
|
||||||
while ((opt = getopt(argc, argv, "hVvLS:")) != -1) {
|
while ((opt = getopt(argc, argv, "hVvLS:")) != -1) {
|
||||||
switch (opt) {
|
switch (opt) {
|
||||||
case 'h':
|
case 'h': PrintHelp(); break;
|
||||||
PrintHelp();
|
case 'V': PrintVersion(); break;
|
||||||
break;
|
case 'v': config.verbose = true; break;
|
||||||
case 'V':
|
case 'L': DeleteLogin(savedir_path); break;
|
||||||
PrintVersion();
|
case 'S': config.ignoressl = true; break;
|
||||||
break;
|
default: std::cerr << RED"[ERROR]" << RESET" invalid option: " << (char)optopt << "\ntry: -h\n"; safe_exit(EINVAL);
|
||||||
case 'v':
|
}
|
||||||
config.verbose = true;
|
|
||||||
break;
|
|
||||||
case 'L':
|
|
||||||
DeleteLogin(savedir_path);
|
|
||||||
break;
|
|
||||||
case 'S':
|
|
||||||
config.ignoressl = true;
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
std::cerr << RED "[ERROR]" << RESET " invalid option: " << (char)optopt
|
|
||||||
<< "\ntry: -h\n";
|
|
||||||
safe_exit(EINVAL);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string urlfile_path = std::string(savedir_path) + "/url";
|
std::string urlfile_path = std::string(savedir_path) + "/url";
|
||||||
@ -92,6 +79,6 @@ int main(int argc, char **argv) {
|
|||||||
get_input_and_login();
|
get_input_and_login();
|
||||||
}
|
}
|
||||||
main_menu();
|
main_menu();
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
@ -3,7 +3,6 @@
|
|||||||
#include "marks.h"
|
#include "marks.h"
|
||||||
#include "net.h"
|
#include "net.h"
|
||||||
#include "timetable.h"
|
#include "timetable.h"
|
||||||
#include "types.h"
|
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <curses.h>
|
#include <curses.h>
|
||||||
@ -16,10 +15,10 @@ void main_menu() {
|
|||||||
};
|
};
|
||||||
void (*choicesFuncs[])() = {nullptr, marks_page, timetable_page, komens_menu,
|
void (*choicesFuncs[])() = {nullptr, marks_page, timetable_page, komens_menu,
|
||||||
nullptr, nullptr, nullptr, nullptr};
|
nullptr, nullptr, nullptr, nullptr};
|
||||||
|
ITEM **my_items;
|
||||||
complete_menu main_menu;
|
|
||||||
|
|
||||||
int c;
|
int c;
|
||||||
|
MENU *my_menu;
|
||||||
|
WINDOW *my_menu_win;
|
||||||
int n_choices, i;
|
int n_choices, i;
|
||||||
|
|
||||||
/* Initialize curses */
|
/* Initialize curses */
|
||||||
@ -34,37 +33,37 @@ void main_menu() {
|
|||||||
|
|
||||||
/* Create items */
|
/* Create items */
|
||||||
n_choices = ARRAY_SIZE(choices);
|
n_choices = ARRAY_SIZE(choices);
|
||||||
main_menu.items = (ITEM **)calloc(n_choices, sizeof(ITEM *));
|
my_items = (ITEM **)calloc(n_choices, sizeof(ITEM *));
|
||||||
for (i = 0; i < n_choices; ++i)
|
for (i = 0; i < n_choices; ++i)
|
||||||
main_menu.items[i] =
|
my_items[i] =
|
||||||
new_item(wchar_to_char(choices[i]), wchar_to_char(choices[i]));
|
new_item(wchar_to_char(choices[i]), wchar_to_char(choices[i]));
|
||||||
|
|
||||||
/* Crate menu */
|
/* Crate menu */
|
||||||
main_menu.menu = new_menu((ITEM **)main_menu.items);
|
my_menu = new_menu((ITEM **)my_items);
|
||||||
|
|
||||||
/* Create the window to be associated with the menu */
|
/* Create the window to be associated with the menu */
|
||||||
main_menu.win = newwin(12, 40, 4, 4);
|
my_menu_win = newwin(12, 40, 4, 4);
|
||||||
keypad(main_menu.win, TRUE);
|
keypad(my_menu_win, TRUE);
|
||||||
|
|
||||||
/* Set main window and sub window */
|
/* Set main window and sub window */
|
||||||
set_menu_win(main_menu.menu, main_menu.win);
|
set_menu_win(my_menu, my_menu_win);
|
||||||
set_menu_sub(main_menu.menu, derwin(main_menu.win, 8, 38, 3, 1));
|
set_menu_sub(my_menu, derwin(my_menu_win, 8, 38, 3, 1));
|
||||||
set_menu_format(main_menu.menu, 7, 1);
|
set_menu_format(my_menu, 7, 1);
|
||||||
|
|
||||||
/* Set menu mark to the string " * " */
|
/* Set menu mark to the string " * " */
|
||||||
set_menu_mark(main_menu.menu, " * ");
|
set_menu_mark(my_menu, " * ");
|
||||||
|
|
||||||
/* Print a border around the main window and print a title */
|
/* Print a border around the main window and print a title */
|
||||||
box(main_menu.win, 0, 0);
|
box(my_menu_win, 0, 0);
|
||||||
|
|
||||||
wprint_in_middle(main_menu.win, 1, 0, 40, L"Main Menu", COLOR_PAIR(1));
|
wprint_in_middle(my_menu_win, 1, 0, 40, L"Main Menu", COLOR_PAIR(1));
|
||||||
mvwaddch(main_menu.win, 2, 0, ACS_LTEE);
|
mvwaddch(my_menu_win, 2, 0, ACS_LTEE);
|
||||||
mvwhline(main_menu.win, 2, 1, ACS_HLINE, 38);
|
mvwhline(my_menu_win, 2, 1, ACS_HLINE, 38);
|
||||||
mvwaddch(main_menu.win, 2, 39, ACS_RTEE);
|
mvwaddch(my_menu_win, 2, 39, ACS_RTEE);
|
||||||
|
|
||||||
/* Post the menu */
|
/* Post the menu */
|
||||||
post_menu(main_menu.menu);
|
post_menu(my_menu);
|
||||||
wrefresh(main_menu.win);
|
wrefresh(my_menu_win);
|
||||||
|
|
||||||
attron(COLOR_PAIR(2));
|
attron(COLOR_PAIR(2));
|
||||||
mvprintw(LINES - 2, 0,
|
mvprintw(LINES - 2, 0,
|
||||||
@ -78,33 +77,33 @@ void main_menu() {
|
|||||||
case KEY_DOWN:
|
case KEY_DOWN:
|
||||||
case KEY_NPAGE:
|
case KEY_NPAGE:
|
||||||
case 'j':
|
case 'j':
|
||||||
menu_driver(main_menu.menu, REQ_DOWN_ITEM);
|
menu_driver(my_menu, REQ_DOWN_ITEM);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case KEY_UP:
|
case KEY_UP:
|
||||||
case KEY_PPAGE:
|
case KEY_PPAGE:
|
||||||
case 'k':
|
case 'k':
|
||||||
menu_driver(main_menu.menu, REQ_UP_ITEM);
|
menu_driver(my_menu, REQ_UP_ITEM);
|
||||||
break;
|
break;
|
||||||
case 10: // ENTER
|
case 10: // ENTER
|
||||||
clear();
|
clear();
|
||||||
if (item_index(current_item(main_menu.menu)) == n_choices - 1) {
|
if (item_index(current_item(my_menu)) == n_choices - 1) {
|
||||||
goto close_menu;
|
goto close_menu;
|
||||||
}
|
}
|
||||||
choicesFuncs[item_index(current_item(main_menu.menu))]();
|
choicesFuncs[item_index(current_item(my_menu))]();
|
||||||
pos_menu_cursor(main_menu.menu);
|
pos_menu_cursor(my_menu);
|
||||||
refresh();
|
refresh();
|
||||||
wrefresh(main_menu.win);
|
wrefresh(my_menu_win);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
wrefresh(main_menu.win);
|
wrefresh(my_menu_win);
|
||||||
}
|
}
|
||||||
close_menu:
|
close_menu:
|
||||||
|
|
||||||
/* Unpost and free all the memory taken up */
|
/* Unpost and free all the memory taken up */
|
||||||
unpost_menu(main_menu.menu);
|
unpost_menu(my_menu);
|
||||||
free_menu(main_menu.menu);
|
free_menu(my_menu);
|
||||||
for (i = 0; i < n_choices; ++i)
|
for (i = 0; i < n_choices; ++i)
|
||||||
free_item(main_menu.items[i]);
|
free_item(my_items[i]);
|
||||||
endwin();
|
endwin();
|
||||||
}
|
}
|
||||||
|
107
src/net.cpp
107
src/net.cpp
@ -11,17 +11,12 @@
|
|||||||
#include <curl/curl.h>
|
#include <curl/curl.h>
|
||||||
#include <curses.h>
|
#include <curses.h>
|
||||||
#include <dirent.h>
|
#include <dirent.h>
|
||||||
#include <fcntl.h>
|
|
||||||
#include <format>
|
#include <format>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <nlohmann/json.hpp>
|
#include <nlohmann/json.hpp>
|
||||||
#include <nlohmann/json_fwd.hpp>
|
#include <nlohmann/json_fwd.hpp>
|
||||||
#include <stdlib.h>
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <unistd.h>
|
|
||||||
|
|
||||||
#define ROUND_UP(x, y) ((((x) + (y) - 1)) & ~((y) - 1))
|
|
||||||
|
|
||||||
using nlohmann::json;
|
using nlohmann::json;
|
||||||
|
|
||||||
@ -30,44 +25,15 @@ std::string access_token;
|
|||||||
CURL *curl = curl_easy_init();
|
CURL *curl = curl_easy_init();
|
||||||
|
|
||||||
// Callback function to write data into a std::string
|
// Callback function to write data into a std::string
|
||||||
size_t WriteCallback_to_string(void *contents, size_t size, size_t nmemb,
|
size_t WriteCallback(void *contents, size_t size, size_t nmemb,
|
||||||
void *userp) {
|
std::string *userp) {
|
||||||
size_t totalSize = size * nmemb;
|
size_t totalSize = size * nmemb;
|
||||||
static_cast<std::string *>(userp)->append((char *)contents, totalSize);
|
userp->append((char *)contents, totalSize);
|
||||||
return totalSize;
|
return totalSize;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t WriteCallback_to_file(void *contents, size_t size, size_t nmemb,
|
std::tuple<std::string, int> send_curl_request(std::string endpoint, metod type,
|
||||||
void *userp) {
|
std::string req_data) {
|
||||||
const size_t sector_size = 4096;
|
|
||||||
size_t total = size * nmemb;
|
|
||||||
void *aligned_buf = aligned_alloc(sector_size, ROUND_UP(total, sector_size));
|
|
||||||
|
|
||||||
// Zero out the buffer before copying
|
|
||||||
memset(aligned_buf, 0, ROUND_UP(total, sector_size));
|
|
||||||
memcpy(aligned_buf, contents, total);
|
|
||||||
|
|
||||||
// Cast userp to fstream*
|
|
||||||
std::fstream *fileStream = static_cast<std::fstream *>(userp);
|
|
||||||
|
|
||||||
// Write to the file using fstream
|
|
||||||
fileStream->write(static_cast<char *>(aligned_buf), total);
|
|
||||||
|
|
||||||
// Check if write was successful
|
|
||||||
if (fileStream->fail()) {
|
|
||||||
free(aligned_buf);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
free(aligned_buf);
|
|
||||||
return total;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::tuple<std::string, int> send_curl_request(
|
|
||||||
std::string endpoint, metod type, std::string req_data,
|
|
||||||
size_t (*WriteCallback_function)(void *, size_t, size_t,
|
|
||||||
void *) = WriteCallback_to_string,
|
|
||||||
std::fstream *fileStream = nullptr) {
|
|
||||||
std::string response;
|
std::string response;
|
||||||
std::string url = baka_api_url + endpoint;
|
std::string url = baka_api_url + endpoint;
|
||||||
if (type == GET) {
|
if (type == GET) {
|
||||||
@ -76,19 +42,15 @@ std::tuple<std::string, int> send_curl_request(
|
|||||||
|
|
||||||
if (curl) {
|
if (curl) {
|
||||||
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
|
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
|
||||||
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback_function);
|
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
|
||||||
if (fileStream) {
|
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);
|
||||||
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fileStream);
|
|
||||||
} else {
|
|
||||||
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);
|
|
||||||
}
|
|
||||||
if (config.ignoressl) {
|
if (config.ignoressl) {
|
||||||
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
|
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
|
||||||
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
|
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
|
||||||
}
|
}
|
||||||
|
|
||||||
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
|
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
|
||||||
curl_easy_setopt(curl, CURLOPT_ACCEPT_ENCODING, "gzip, deflate");
|
|
||||||
|
|
||||||
struct curl_slist *headers = NULL;
|
struct curl_slist *headers = NULL;
|
||||||
headers = curl_slist_append(
|
headers = curl_slist_append(
|
||||||
@ -115,15 +77,7 @@ std::tuple<std::string, int> send_curl_request(
|
|||||||
std::cerr << RED "[ERROR] " << RESET "curl not initialised\n";
|
std::cerr << RED "[ERROR] " << RESET "curl not initialised\n";
|
||||||
safe_exit(20);
|
safe_exit(20);
|
||||||
}
|
}
|
||||||
CURLcode curl_return_code = curl_easy_perform(curl);
|
curl_easy_perform(curl); // Perform the request
|
||||||
if (curl_return_code != CURLE_OK) {
|
|
||||||
std::cerr << RED "[ERROR] " << RESET << "curl_easy_perform() failed: "
|
|
||||||
<< curl_easy_strerror(curl_return_code) << "\n";
|
|
||||||
safe_exit(21);
|
|
||||||
}
|
|
||||||
if (fileStream) {
|
|
||||||
fileStream->close();
|
|
||||||
}
|
|
||||||
|
|
||||||
int http_code = 0;
|
int http_code = 0;
|
||||||
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code);
|
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code);
|
||||||
@ -218,47 +172,4 @@ access_token_refreshed:
|
|||||||
|
|
||||||
return json::parse(response);
|
return json::parse(response);
|
||||||
}
|
}
|
||||||
|
|
||||||
int download_attachment(std::string id, std::string path) {
|
|
||||||
if (config.verbose) {
|
|
||||||
std::clog << "downloading attachment please wait...\n";
|
|
||||||
}
|
|
||||||
if (path[0] == '~') {
|
|
||||||
path = std::string(std::getenv("HOME")) + path.substr(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string savedir_path = std::filesystem::path(path).parent_path().string();
|
|
||||||
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::fstream fileStream(path, std::ios::out | std::ios::binary);
|
|
||||||
if (!fileStream.is_open()) {
|
|
||||||
std::cerr << RED "[ERROR] " RESET << "Failed to open file for writing\n";
|
|
||||||
safe_exit(EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
|
|
||||||
is_access_token_empty();
|
|
||||||
access_token_refreshed:
|
|
||||||
std::string req_data =
|
|
||||||
std::format("Authorization=Bearer&access_token={}", access_token);
|
|
||||||
|
|
||||||
auto [response, http_code] =
|
|
||||||
send_curl_request(std::format("/api/3/komens/attachment/{}", id), GET,
|
|
||||||
req_data, WriteCallback_to_file, &fileStream);
|
|
||||||
if (http_code != 200) {
|
|
||||||
if (config.verbose) {
|
|
||||||
std::clog << BLUE "[LOG] " RESET << "download failed " << http_code
|
|
||||||
<< " is non 200 response\n";
|
|
||||||
}
|
|
||||||
refresh_access_token();
|
|
||||||
goto access_token_refreshed;
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace bakaapi
|
} // namespace bakaapi
|
@ -19,6 +19,5 @@ void login(std::string username, std::string password);
|
|||||||
void refresh_access_token();
|
void refresh_access_token();
|
||||||
json get_data_from_endpoint(const std::string &endpoint, metod metod,
|
json get_data_from_endpoint(const std::string &endpoint, metod metod,
|
||||||
std::string additional_data = "");
|
std::string additional_data = "");
|
||||||
int download_attachment(std::string id, std::string path);
|
|
||||||
} // namespace bakaapi
|
} // namespace bakaapi
|
||||||
#endif
|
#endif
|
@ -3,7 +3,6 @@
|
|||||||
#define _ba_ty_hg_
|
#define _ba_ty_hg_
|
||||||
|
|
||||||
#include <istream>
|
#include <istream>
|
||||||
#include <menu.h>
|
|
||||||
struct Config {
|
struct Config {
|
||||||
bool verbose = false;
|
bool verbose = false;
|
||||||
bool ignoressl = false;
|
bool ignoressl = false;
|
||||||
@ -145,10 +144,4 @@ struct SelectorType {
|
|||||||
: x(xArg, min_limit_x, max_limit_x), y(yArg, min_limit_y, max_limit_y) {}
|
: x(xArg, min_limit_x, max_limit_x), y(yArg, min_limit_y, max_limit_y) {}
|
||||||
};
|
};
|
||||||
|
|
||||||
struct complete_menu {
|
|
||||||
WINDOW *win;
|
|
||||||
ITEM **items;
|
|
||||||
MENU *menu;
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif
|
#endif
|
Loading…
x
Reference in New Issue
Block a user