12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160 |
- #include "door.h"
- #include <algorithm>
- #include <chrono>
- #include <ctype.h>
- #include <string.h>
- #include <string>
- #include <thread>
- #include <libgen.h> // basename
- #include <ctime>
- #include <iomanip>
- #include <signal.h>
- #include <unistd.h>
- #include <iconv.h>
- #include <algorithm>
- #include <iostream>
- namespace door {
- void to_lower(std::string &text) {
- transform(text.begin(), text.end(), text.begin(), ::tolower);
- }
- bool replace(std::string &str, const std::string &from, const std::string &to) {
- size_t start_pos = str.find(from);
- if (start_pos == std::string::npos)
- return false;
- str.replace(start_pos, from.length(), to);
- return true;
- }
- static bool hangup = false;
- void sig_handler(int signal) {
- hangup = true;
-
-
- }
- class IConv {
- iconv_t ic;
- public:
- IConv(const char *to, const char *from);
- ~IConv();
- int convert(char *input, char *output, size_t outbufsize);
- };
- IConv::IConv(const char *to, const char *from) : ic(iconv_open(to, from)) {}
- IConv::~IConv() { iconv_close(ic); }
- int IConv::convert(char *input, char *output, size_t outbufsize) {
- size_t inbufsize = strlen(input);
-
-
-
- int r = iconv(ic, &input, &inbufsize, &output, &outbufsize);
- *output = 0;
- return r;
- }
- static IConv converter("UTF-8", "CP437");
- void cp437toUnicode(std::string input, std::string &out) {
- char buffer[10240];
- char output[16384];
- strcpy(buffer, input.c_str());
- converter.convert(buffer, output, sizeof(output));
- out.assign(output);
- }
- void cp437toUnicode(const char *input, std::string &out) {
- char buffer[10240];
- char output[16384];
- strcpy(buffer, input);
- converter.convert(buffer, output, sizeof(output));
- out.assign(output);
- }
- bool unicode = false;
- bool debug_capture = false;
- Door::Door(std::string dname, int argc, char *argv[])
- : std::ostream(this), doorname{dname},
- has_dropfile{false}, debugging{false}, seconds_elapsed{0},
- previous(COLOR::WHITE), track{true}, cx{1}, cy{1},
- inactivity{120}, node{1} {
-
- opt.addUsage("Door++ library by BUGZ (C) 2021");
- opt.addUsage("");
- opt.addUsage(" -h --help Displays this help");
- opt.addUsage(" -l --local Local Mode");
- opt.addUsage(" -d --dropfile [FILENAME] Load Dropfile");
- opt.addUsage(" -n --node N Set node number");
-
- opt.addUsage(" -u --username NAME Set Username");
- opt.addUsage(" -t --timeleft N Set time left");
- opt.addUsage(" --maxtime N Set max time");
- opt.addUsage("");
- opt.setFlag("help", 'h');
- opt.setFlag("local", 'l');
- opt.setFlag("debuggering");
- opt.setOption("dropfile", 'd');
-
- opt.setOption("username", 'u');
- opt.setOption("timeleft", 't');
- opt.setOption("maxtime");
- opt.processCommandArgs(argc, argv);
- if (!opt.hasOptions()) {
- opt.printUsage();
- exit(1);
- }
- if (opt.getFlag("help") || opt.getFlag('h')) {
- opt.printUsage();
- exit(1);
- }
- if (opt.getValue("username") != nullptr) {
- username = opt.getValue("username");
- }
- if (opt.getFlag("debuggering")) {
- debugging = true;
- }
- if (opt.getValue("node") != nullptr) {
- node = atoi(opt.getValue("node"));
- }
- if (opt.getValue("timeleft") != nullptr) {
- time_left = atoi(opt.getValue("timeleft"));
- } else {
-
- time_left = 25;
- }
-
- parse_dropfile(opt.getValue("dropfile"));
-
- if (opt.getValue("maxtime") != nullptr) {
- int maxtime = atoi(opt.getValue("maxtime"));
- if (time_left > maxtime) {
- logf << "Adjusting time from " << time_left << " to " << maxtime
- << std::endl;
- time_left = maxtime;
- }
- }
- if (opt.getFlag("local")) {
- if (username.empty()) {
- std::cout << "Local mode requires username to be set." << std::endl;
- opt.printUsage();
- exit(1);
- }
- } else {
-
- if (!has_dropfile) {
- std::cout << "I require a dropfile. And a shrubbery." << std::endl;
- opt.printUsage();
- exit(1);
- }
- }
-
- std::string logFileName = dname + ".log";
- logf.open(logFileName.c_str(), std::ofstream::out | std::ofstream::app);
- log("Door init");
- init();
-
- if (!debugging) {
- detect_unicode_and_screen();
- logf << "Screen " << width << " X " << height << std::endl;
- }
- }
- Door::~Door() {
-
-
- log("dtor");
- tcsetattr(STDIN_FILENO, TCOFLUSH, &tio_default);
- signal(SIGHUP, SIG_DFL);
- signal(SIGPIPE, SIG_DFL);
-
- stop_thread.set_value();
- time_thread.join();
- log("done");
- logf.close();
- }
- void Door::time_thread_run(std::future<void> future) {
- while (future.wait_for(std::chrono::milliseconds(1)) ==
- std::future_status::timeout) {
-
- std::this_thread::sleep_for(std::chrono::seconds(1));
- seconds_elapsed++;
-
-
- if (seconds_elapsed % 60 == 0) {
- if (time_left > 0)
- time_left--;
- }
- }
- }
- void Door::init(void) {
-
-
-
-
- struct termios tio_raw;
- tcgetattr(STDIN_FILENO, &tio_default);
- tio_raw = tio_default;
- cfmakeraw(&tio_raw);
-
- tio_raw.c_cc[VMIN] = 0;
- tio_raw.c_cc[VTIME] = 1;
- bpos = 0;
- tcsetattr(STDIN_FILENO, TCSANOW, &tio_raw);
- startup = std::time(nullptr);
- signal(SIGHUP, sig_handler);
- signal(SIGPIPE, sig_handler);
-
- std::future<void> stop_future = stop_thread.get_future();
- time_thread =
- std::thread(&Door::time_thread_run, this, std::move(stop_future));
- }
- void Door::detect_unicode_and_screen(void) {
- unicode = false;
- width = 0;
- height = 0;
- if (!isatty(STDIN_FILENO)) {
-
- *this << "\377\375\042\377\373\001";
- }
-
-
- *this << "\x1b[0;30;40m\x1b[2J\x1b[H";
-
- *this << "\x03\x04"
- << "\x1b[6n";
- *this << "\x1b[999C\x1b[999B\x1b[6n";
- *this << "\x1b[H";
- this->flush();
- usleep(1000 * 1000);
- if (haskey()) {
- char buffer[101];
- int len;
- len = read(STDIN_FILENO, &buffer, sizeof(buffer) - 1);
-
- if (len > 0) {
- buffer[len] = 0;
- int x;
-
- for (x = 0; x < len; x++) {
- if (buffer[x] == 0)
- buffer[x] = ' ';
- }
-
- if (1) {
- std::string cleanbuffer = buffer;
- std::string esc = "\x1b";
- std::string esc_text = "^[";
- while (replace(cleanbuffer, esc, esc_text)) {
- };
- logf << "BUFFER [" << cleanbuffer << "]" << std::endl;
- }
-
-
-
-
- if ((strstr(buffer, "1;1R") != nullptr)) {
-
-
- unicode = true;
- log("unicode enabled \u2615");
- }
-
-
-
- char *cp;
- cp = strchr(buffer, '\x1b');
- if (cp != nullptr) {
- cp = strchr(cp + 1, '\x1b');
- } else {
- log("Failed terminal size detection. See buffer:");
- log(buffer);
- return;
- }
- if (cp != nullptr) {
- cp++;
- if (*cp == '[') {
- cp++;
- height = atoi(cp);
- cp = strchr(cp, ';');
- if (cp != nullptr) {
- cp++;
- width = atoi(cp);
- } else {
- height = 0;
- }
- }
- }
- }
- } else {
- logf << "FAIL-WHALE, no response to terminal getposition." << std::endl;
- }
- }
- void Door::parse_dropfile(const char *filepath) {
- if (filepath == nullptr)
- return;
-
- std::ifstream file(filepath);
- std::string line;
- while (std::getline(file, line)) {
-
- if (!line.empty() && line[line.size() - 1] == '\r')
- line.erase(line.size() - 1);
- dropfilelines.push_back(line);
- }
- file.close();
- std::string filename;
- {
-
- char *temp = strdup(filepath);
- filename = basename(temp);
- free(temp);
- }
- to_lower(filename);
-
- if (filename == "door.sys") {
-
- node = atoi(dropfilelines[3].c_str());
- username = dropfilelines[9];
- location = dropfilelines[10];
- time_left = atoi(dropfilelines[18].c_str());
- sysop = dropfilelines[34];
- handle = dropfilelines[35];
- } else {
- std::string msg = "Unknown dropfile: ";
- msg += filename;
- log(msg);
- *this << msg << std::endl;
- exit(2);
- }
- has_dropfile = true;
- }
- void Door::log(std::string output) {
-
- std::time_t t = std::time(nullptr);
- std::tm tm = *std::localtime(&t);
- logf << std::put_time(&tm, "%c ") << output << std::endl;
- }
- bool Door::haskey(void) {
- fd_set socket_set;
- struct timeval tv;
- int select_ret = -1;
- if (hangup)
- return -2;
- if (time_left < 2)
- return -3;
- while (select_ret == -1) {
- FD_ZERO(&socket_set);
- FD_SET(STDIN_FILENO, &socket_set);
- tv.tv_sec = 0;
- tv.tv_usec = 1;
- select_ret = select(STDIN_FILENO + 1, &socket_set, NULL, NULL, &tv);
- if (select_ret == -1) {
- if (errno == EINTR)
- continue;
- log("hangup detected");
- hangup = true;
- return (-2);
- }
- if (select_ret == 0)
- return false;
- }
- return true;
- }
- signed int Door::getch(void) {
- fd_set socket_set;
- struct timeval tv;
- int select_ret = -1;
- int recv_ret;
- char key;
- if (door::hangup)
- return -2;
- if (time_left < 2)
- return -3;
- while (select_ret == -1) {
- FD_ZERO(&socket_set);
- FD_SET(STDIN_FILENO, &socket_set);
- tv.tv_sec = 0;
- tv.tv_usec = 100;
- select_ret = select(STDIN_FILENO + 1, &socket_set, NULL, NULL, &tv);
-
- if (select_ret == -1) {
- if (errno == EINTR)
- continue;
- log("hangup detected");
- door::hangup = true;
- return (-2);
- }
- if (select_ret == 0)
- return (-1);
- }
- recv_ret = read(STDIN_FILENO, &key, 1);
- if (recv_ret != 1) {
-
- log("hangup");
- hangup = true;
- return -2;
- }
- return key;
- }
- void Door::unget(char c) {
- if (bpos < sizeof(buffer) - 1) {
- buffer[bpos] = c;
- bpos++;
- }
- }
- char Door::get(void) {
- if (bpos == 0)
- return 0;
- bpos--;
- return buffer[bpos];
- }
- signed int Door::getkey(void) {
- signed int c, c2;
- if (bpos != 0) {
- c = get();
- } else {
- c = getch();
- }
- if (c < 0)
- return c;
-
- if (c == 0x0d) {
- c2 = getch();
- if ((c2 != 0) and (c2 >= 0))
- unget(c2);
- return c;
- }
- if (c == 0x1b) {
-
- c2 = getch();
- if (c2 < 0) {
-
- return c;
- }
-
- char extended[16];
- unsigned int pos = 0;
- extended[pos] = (char)c2;
- extended[pos + 1] = 0;
- pos++;
- while ((pos < sizeof(extended) - 1) and ((c2 = getch()) >= 0)) {
-
-
- if (c2 == 0x1b) {
- unget(c2);
- break;
- }
- extended[pos] = (char)c2;
- extended[pos + 1] = 0;
- pos++;
- }
-
- if (extended[0] == '[') {
- switch (extended[1]) {
- case 'A':
- return XKEY_UP_ARROW;
- case 'B':
- return XKEY_DOWN_ARROW;
- case 'C':
- return XKEY_RIGHT_ARROW;
- case 'D':
- return XKEY_LEFT_ARROW;
- case 'H':
- return XKEY_HOME;
- case 'F':
- return XKEY_END;
- case 'K':
- return XKEY_END;
- case 'U':
- return XKEY_PGUP;
- case 'V':
- return XKEY_PGDN;
- case '@':
- return XKEY_INSERT;
- }
- if (extended[pos - 1] == '~') {
-
- int number = atoi(extended + 1);
- switch (number) {
- case 2:
- return XKEY_INSERT;
- case 3:
- return XKEY_DELETE;
- case 5:
- return XKEY_PGUP;
- case 6:
- return XKEY_PGDN;
- case 15:
- return XKEY_F5;
- case 17:
- return XKEY_F6;
- case 18:
- return XKEY_F7;
- case 19:
- return XKEY_F8;
- case 20:
- return XKEY_F9;
- case 21:
- return XKEY_F10;
- case 23:
- return XKEY_F11;
- case 24:
- return XKEY_F12;
- }
- }
- }
- if (extended[0] == 'O') {
- switch (extended[1]) {
- case 'P':
- return XKEY_F1;
- case 'Q':
- return XKEY_F2;
- case 'R':
- return XKEY_F3;
- case 'S':
- return XKEY_F4;
- case 't':
- return XKEY_F5;
- }
- }
-
- logf << "\r\nDEBUG:\r\nESC + ";
- for (unsigned int x = 0; x < pos; x++) {
- char z = extended[x];
- if (iscntrl(z)) {
- logf << (int)z << " ";
- } else {
- logf << "'" << (char)z << "'"
- << " ";
- };
- }
- logf << "\r\n";
- logf.flush();
- return XKEY_UNKNOWN;
- }
- return c;
- }
- int Door::get_input(void) {
- signed int c;
- c = getkey();
- if (c < 0)
- return 0;
- return c;
-
- }
- signed int Door::sleep_key(int secs) {
- fd_set socket_set;
- struct timeval tv;
- int select_ret = -1;
-
- if (hangup)
- return -2;
- if (time_left < 2)
- return -3;
- while (select_ret == -1) {
- FD_ZERO(&socket_set);
- FD_SET(STDIN_FILENO, &socket_set);
- tv.tv_sec = secs;
- tv.tv_usec = 0;
- select_ret = select(STDIN_FILENO + 1, &socket_set, NULL, NULL, &tv);
-
- if (select_ret == -1) {
- if (errno == EINTR)
- continue;
- hangup = true;
- log("hangup detected");
- return (-2);
- }
- if (select_ret == 0)
- return (-1);
- }
- return getkey();
- }
- std::string Door::input_string(int max) {
- std::string input;
-
- *this << std::string(max, ' ');
- *this << std::string(max, '\x08');
- int c;
- while (true) {
- c = sleep_key(inactivity);
- if (c < 0) {
- input.clear();
- return input;
- }
- if (c > 0x1000)
- continue;
- if (isprint(c)) {
- if (int(input.length()) < max) {
- *this << char(c);
- input.append(1, c);
- } else {
-
- *this << '\x07';
- }
- } else {
- switch (c) {
- case 0x08:
- case 0x7f:
- if (input.length() > 0) {
- *this << "\x08 \x08";
-
- input.erase(input.length() - 1);
- };
- break;
- case 0x0d:
- return input;
- }
- }
- }
- }
- std::streamsize Door::xsputn(const char *s, std::streamsize n) {
- if (debug_capture) {
- debug_buffer.append(s, n);
- } else {
- static std::string buffer;
- buffer.append(s, n);
-
- if (!hangup) {
- std::cout << buffer;
- std::cout.flush();
- }
-
-
- if (track)
- cx += n;
- buffer.clear();
- }
- return n;
- }
- int Door::overflow(char c) {
-
-
-
- if (debug_capture) {
- debug_buffer.append(1, c);
- } else {
- if (!hangup) {
- std::cout << c;
- std::cout.flush();
- }
- }
- if (track)
- cx++;
-
- return c;
- }
- ColorOutput::ColorOutput() : c(COLOR::BLACK, COLOR::BLACK) {
- pos = 0;
- len = 0;
- }
- void ColorOutput::reset(void) {
- pos = 0;
- len = 0;
- }
- Render::Render(const std::string txt) : text{txt} {}
- void Render::output(std::ostream &os) {
- for (auto out : outputs) {
- std::string fragment = text.substr(out.pos, out.len);
- os << out.c << fragment;
- }
- }
- Clrscr::Clrscr() {}
- std::ostream &operator<<(std::ostream &os, const Clrscr &clr) {
- Door *d = dynamic_cast<Door *>(&os);
- if (d != nullptr) {
- d->track = false;
- *d << "\x1b[2J"
- "\x1b[H";
- d->cx = 1;
- d->cy = 1;
- d->track = true;
- } else {
- os << "\x1b[2J"
- "\x1b[H";
- }
- return os;
- }
- Clrscr cls;
- NewLine::NewLine() {}
- std::ostream &operator<<(std::ostream &os, const NewLine &nl) {
- Door *d = dynamic_cast<Door *>(&os);
- if (d != nullptr) {
- d->track = false;
- *d << "\r\n";
- d->cx = 1;
- d->cy++;
- d->track = true;
- } else {
- os << "\r\n";
- };
- return os;
- }
- NewLine nl;
- Goto::Goto(int xpos, int ypos) {
- x = xpos;
- y = ypos;
- }
- void Goto::set(int xpos, int ypos) {
- x = xpos;
- y = ypos;
- }
- std::ostream &operator<<(std::ostream &os, const Goto &g) {
- Door *d = dynamic_cast<Door *>(&os);
- if (d != nullptr) {
- d->track = false;
- *d << "\x1b[";
- if (g.y > 1)
- *d << std::to_string(g.y);
- if (g.x > 1) {
- os << ";";
- *d << std::to_string(g.x);
- }
- *d << "H";
- d->cx = g.x;
- d->cy = g.y;
- d->track = true;
- } else {
- os << "\x1b[" << std::to_string(g.y) << ";" << std::to_string(g.x) << "H";
- };
- return os;
- }
- renderFunction rBlueYellow = [](const std::string &txt) -> Render {
- Render r(txt);
- ColorOutput co;
- bool uc = true;
- ANSIColor blue(COLOR::BLUE, ATTR::BOLD);
- ANSIColor cyan(COLOR::YELLOW, ATTR::BOLD);
- co.pos = 0;
- co.len = 0;
- co.c = blue;
-
- int tpos = 0;
- for (char const &c : txt) {
- if (uc) {
- if (!isupper(c)) {
-
- if (co.len != 0) {
- r.outputs.push_back(co);
- co.reset();
- co.pos = tpos;
- }
- co.c = cyan;
-
- uc = false;
- }
- } else {
- if (isupper(c)) {
- if (co.len != 0) {
- r.outputs.push_back(co);
- co.reset();
- co.pos = tpos;
- }
- co.c = blue;
-
- uc = true;
- }
- }
- co.len++;
- tpos++;
-
- }
- if (co.len != 0) {
- r.outputs.push_back(co);
- }
- return r;
- };
- door::renderFunction renderStatusValue(door::ANSIColor status,
- door::ANSIColor value) {
- door::renderFunction rf = [status,
- value](const std::string &txt) -> door::Render {
- door::Render r(txt);
- door::ColorOutput co;
- co.pos = 0;
- co.len = 0;
- co.c = status;
- size_t pos = txt.find(':');
- if (pos == std::string::npos) {
-
- co.len = txt.length();
- r.outputs.push_back(co);
- } else {
- pos++;
- co.len = pos;
- r.outputs.push_back(co);
- co.reset();
- co.pos = pos;
- co.c = value;
- co.len = txt.length() - pos;
- r.outputs.push_back(co);
- }
- return r;
- };
- return rf;
- }
- door::renderFunction rStatusValue = [](const std::string &txt) -> door::Render {
- door::Render r(txt);
- door::ColorOutput co;
-
- door::ANSIColor status(door::COLOR::BLUE, door::ATTR::BOLD);
- door::ANSIColor value(door::COLOR::YELLOW, door::ATTR::BOLD);
- co.pos = 0;
- co.len = 0;
- co.c = status;
- size_t pos = txt.find(':');
- if (pos == std::string::npos) {
-
- co.len = txt.length();
- r.outputs.push_back(co);
- } else {
- pos++;
- co.len = pos;
- r.outputs.push_back(co);
- co.reset();
- co.pos = pos;
- co.c = value;
- co.len = txt.length() - pos;
- r.outputs.push_back(co);
- }
- return r;
- };
- }
|