diff --git a/Makefile b/Makefile index 84bd570..9dd5be7 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ # make rebuild clean and remake # compiler -CC=g++ +CC = g++ # compile arguments CFLAGS = -Wall -g -fexceptions -std=c++11 -D_REENTRANT -pthread @@ -18,7 +18,7 @@ LDFLAGS = -g -std=c++11 LIBS = -lcrypto -lssl #our source files -SOURCES=$(wildcard *.cpp) +SOURCES = $(wildcard *.cpp) # a macro to define the objects from sources BUILD_DIR := build @@ -32,8 +32,9 @@ EXECUTABLE=jce all: $(EXECUTABLE) $(EXECUTABLE): $(OBJC) - @echo "Building target" $@ "..." + @echo "Building target" $@ @$(CC) $(LDFLAGS) -o $@ $(OBJC) $(LIBS) + @echo "Done." # a rule for generating object files given their c files #.cpp.o: /$(OBJCDIR) @@ -42,13 +43,15 @@ ${BUILD_DIR}/%.o: %.cpp @$(CC) $(CFLAGS) -c $< -o $@ clean: - @echo "Ceaning up *.o Files..." + @echo "Ceaning up *.o Files" @rm -rf ${BUILD_DIR} + @echo "Done." bclean: @echo "Ceaning all" @rm -rf $(EXECUTABLE) ${BUILD_DIR} + @echo "Done." -rebuild: clean all +rebuild: bclean all diff --git a/jce.cpp b/jce.cpp index b196a74..844af76 100644 --- a/jce.cpp +++ b/jce.cpp @@ -1,12 +1,51 @@ #include "jce.h" -jce::jce() : userAcc() +jce::jce() : userAcc(),jDates() { - handler = new jceHandler(&userAcc); + handler = new jceHandler(&userAcc,&jDates); } void jce::start() { - handler->start(); -} \ No newline at end of file + if (handler->start()) + { + int ch; + do + { + system("clear"); + printf("\n----------------jce API alpha----------------\n"); + printf("\tPlease choose as following:\n"); + printf("\t\tShow grades: 1\n"); + printf("\t\tShow scheduale: 2\n"); + printf("\t\tChange dates: 3\n"); + printf("\t\ttype q to quit "); + switch(ch) + { + case '1': + handler->requestMenu(jceHandler::GET_GRADES_BY_DATES); + printf("\npress enter to continue..."); + getchar(); + break; + case '2': + handler->requestMenu(jceHandler::GET_SCHEDULE); + printf("\npress enter to continue..."); + getchar(); + break; + case '3': + jDates.setDate(); + break; + + } + }while ((ch = getchar()) != 'q'); + + } + else + { + printf("\ncouldnt visit"); + exit(1); + } + +} + + diff --git a/jce.h b/jce.h index cd0d17f..45dde16 100644 --- a/jce.h +++ b/jce.h @@ -3,23 +3,21 @@ #include "jceHandler.h" -enum jceOPTIONS -{ - GET_GRADES_BY_DATES, - GET_SCHEDULE -}; + class jce { public: jce(); - ~jce() {} + ~jce() { free(handler); } void start(); private: - jceHandler* handler; user userAcc; + jceDates jDates; + jceHandler* handler; + }; #endif \ No newline at end of file diff --git a/jceHandler.cpp b/jceHandler.cpp index f93b41f..4fb8e01 100644 --- a/jceHandler.cpp +++ b/jceHandler.cpp @@ -3,34 +3,57 @@ template std::string to_string(T value); //simple method to convert types into string. -jceHandler::jceHandler(user* const a) : jceUser(a) +jceHandler::jceHandler(user* const us,jceDates* const jd) : jceUser(us), jDates(jd) { recieverPage = new std::string(""); JceConnector = new sslsocket(dst_host, dst_port); //open a new ssl connection to jce } -void jceHandler::start() +bool jceHandler::start() { - if (!JceConnector->isCon()) - printErrorANDabort(ERROR_ON_OPEN_SOCKET); + checkConnection(); makeFirstVisit(); + + makeSecondVisit(); + + //if succeed on all visiting, that means everything is clear. + // + //what we need to do is make the tests more clean + return true; } +bool jceHandler::requestMenu(jceOptions t) +{ + checkConnection(); + switch (t) + { + case GET_GRADES_BY_DATES: + getGraders(); + break; + case GET_SCHEDULE: + getSchedule(); + break; + default: + return true; + } + return false; + +} void jceHandler::makeFirstVisit() { if (JceConnector->send(makeRequest(getFirstValidationStep()))) { puts ("First login validation step"); if (!JceConnector->recieve(*recieverPage)) - printErrorANDabort(ERROR_ON_GETTING_INFO); + printErrorANDabort(jceHandler::ERROR_ON_GETTING_INFO); if (!checkValidation(*recieverPage)) - printErrorANDabort(ERROR_ON_VALIDATION); + printErrorANDabort(jceHandler::ERROR_ON_VALIDATION); - makeSecondVisit(); + return; } else - printErrorANDabort(ERROR_ON_SEND_REQUEST); + printErrorANDabort(jceHandler::ERROR_ON_SEND_REQUEST); } void jceHandler::makeSecondVisit() @@ -39,49 +62,44 @@ void jceHandler::makeSecondVisit() if ((JceConnector->send(makeRequest(getSecondValidationStep())))) { if (!(JceConnector->recieve(*recieverPage))) - printErrorANDabort(ERROR_ON_GETTING_INFO); + printErrorANDabort(jceHandler::ERROR_ON_GETTING_INFO); - makeFurtherRequests(); + return; } else - printErrorANDabort(ERROR_ON_SEND_REQUEST); + printErrorANDabort(jceHandler::ERROR_ON_SEND_REQUEST); } -bool jceHandler::checkValidation(std::string &html) + +void jceHandler::checkConnection() { - //finds the hashed password - std::size_t hasspass_position1 = recieverPage->find("-A,-N"); - hasspass_position1 += 5; - std::size_t hasspass_position2 = recieverPage->find(",-A,-A", hasspass_position1); - if ((hasspass_position2 != std::string::npos) && (hasspass_position1 != std::string::npos)) - { - std::string hasspass = recieverPage->substr(hasspass_position1,hasspass_position2-hasspass_position1); - jceUser->setHashedPassword(hasspass); - } - //finds the user id - std::size_t id_position1 = recieverPage->find("e=\"-N", 0); - id_position1 += 5; - std::size_t id_position2 = recieverPage->find(",-A", id_position1); - if ((id_position2 != std::string::npos) && (id_position1 != std::string::npos)) + if (!JceConnector->isCon()) + printErrorANDabort(jceHandler::ERROR_ON_OPEN_SOCKET); +} +void jceHandler::getSchedule() +{ + + puts("getting schedule"); + if (!(JceConnector->send(makeRequest(getSchedulePath())))) + printErrorANDabort(jceHandler::ERROR_ON_SEND_REQUEST); + if (!(JceConnector->recieve(*recieverPage))) { - std::string hassid = recieverPage->substr(id_position1,id_position2-id_position1); - jceUser->setUserID(hassid); + printErrorANDabort(jceHandler::ERROR_ON_GETTING_GRADES); } - if (((jceUser->getUserID()).empty()) || ((jceUser->getHashedPassword()).empty())) - return false; - return true; -} -void jceHandler::makeFurtherRequests() -{ + std::cout << *recieverPage; + +} +void jceHandler::getGraders() +{ puts ("getting rates!"); - if (!(JceConnector->send(makeRequest(getGradesPath("2013","0","2014","3"))))) //change it in GUI (select years, semesters) - printErrorANDabort(ERROR_ON_SEND_REQUEST); + if (!(JceConnector->send(makeRequest(getGradesPath())))) + printErrorANDabort(jceHandler::ERROR_ON_SEND_REQUEST); if (!(JceConnector->recieve(*recieverPage))) { - printErrorANDabort(ERROR_ON_GETTING_GRADES); + printErrorANDabort(jceHandler::ERROR_ON_GETTING_GRADES); } GradePage* gp = GradePage::createGradeClass(*recieverPage); @@ -91,33 +109,27 @@ void jceHandler::makeFurtherRequests() cout << "Your GPA is currantly: " << gp->getAvg() << endl; } -std::string jceHandler::getFirstValidationStep() -{ - std::string parameters = "?appname=BSHITA&prgname=LoginValidation&arguments=-N"; - parameters += jceUser->getUsername(); - parameters += ",-N"; - parameters += jceUser->getPassword(); - return parameters; -} -std::string jceHandler::getSecondValidationStep() + +std::string jceHandler::getSchedulePath() { - std::string parameters = "prgname=LoginValidtion1&Arguments=-N"; - parameters += jceUser->getUserID(); - parameters += ",-A,-N"; - parameters += jceUser->getHashedPassword(); - parameters += ",-A,-A"; - return parameters; + std::string string = "PRGNAME=Bitsua_maarechet_shaot&ARGUMENTS=TZ,UNIQ,MisparSheilta,R1C1,R1C2&"; + string += "TZ=" + jceUser->getUserID()+ "&"; + string += "UNIQ=" + jceUser->getHashedPassword() + "&"; + string += "MisparSheilta=3&"; + string += "R1C1=" + jDates->getTYear() + "&"; + string += "R1C2=" + jDates->getTSemester(); + return string; + } -std::string jceHandler::getGradesPath(std::string fromyear, std::string fromsemester, - std::string toyear,std::string tosemester) +std::string jceHandler::getGradesPath() { std::string string = "PRGNAME=HADPASAT_MISMAHIM_LETALMID&ARGUMENTS=TZ,-N4,R1C2,R1C4,R1C1,R1C3,-A,-A,R1C5,-A,UNIQ&"; string += "TZ=" + jceUser->getUserID() + "&"; string += "UNIQ=" + jceUser->getHashedPassword() + "&"; - string += "R1C2=" + fromyear + "&"; - string += "R1C1=" + toyear + "&"; - string += "R1C3=" + tosemester + "&"; - string += "R1C4=" + fromsemester + "&"; + string += "R1C2=" + jDates->getFYear() + "&"; + string += "R1C1=" + jDates->getTYear() + "&"; + string += "R1C3=" + jDates->getTSemester() + "&"; + string += "R1C4=" + jDates->getFSemester() + "&"; string += "R1C5=0"; return string; } @@ -136,6 +148,48 @@ std::string jceHandler::makeRequest(std::string parameters) msg += parameters; return msg; } +std::string jceHandler::getFirstValidationStep() +{ + std::string parameters = "?appname=BSHITA&prgname=LoginValidation&arguments=-N"; + parameters += jceUser->getUsername(); + parameters += ",-N"; + parameters += jceUser->getPassword(); + return parameters; +} +std::string jceHandler::getSecondValidationStep() +{ + std::string parameters = "prgname=LoginValidtion1&Arguments=-N"; + parameters += jceUser->getUserID(); + parameters += ",-A,-N"; + parameters += jceUser->getHashedPassword(); + parameters += ",-A,-A"; + return parameters; +} +bool jceHandler::checkValidation(std::string &html) +{ + //finds the hashed password + std::size_t hasspass_position1 = recieverPage->find("-A,-N"); + hasspass_position1 += 5; + std::size_t hasspass_position2 = recieverPage->find(",-A,-A", hasspass_position1); + if ((hasspass_position2 != std::string::npos) && (hasspass_position1 != std::string::npos)) + { + std::string hasspass = recieverPage->substr(hasspass_position1,hasspass_position2-hasspass_position1); + jceUser->setHashedPassword(hasspass); + } + //finds the user id + std::size_t id_position1 = recieverPage->find("e=\"-N", 0); + id_position1 += 5; + std::size_t id_position2 = recieverPage->find(",-A", id_position1); + if ((id_position2 != std::string::npos) && (id_position1 != std::string::npos)) + { + std::string hassid = recieverPage->substr(id_position1,id_position2-id_position1); + jceUser->setUserID(hassid); + } + if (((jceUser->getUserID()).empty()) || ((jceUser->getHashedPassword()).empty())) + return false; + + return true; +} void jceHandler::printErrorANDabort(jceErrors t) { switch(t) diff --git a/jceHandler.h b/jceHandler.h index 6fcf43f..1a0bddf 100644 --- a/jceHandler.h +++ b/jceHandler.h @@ -8,36 +8,44 @@ #include "sslsocket.h" #include "user.h" - +#include "jceDates.h" #include "GradePage.h" #define dst_host "yedion.jce.ac.il" #define dst_port 443 -enum jceErrors { - ERROR_ON_VALIDATION, - ERROR_ON_INPUT, - ERROR_ON_CONNECTING, - ERROR_ON_OPEN_SOCKET, - ERROR_ON_GETTING_INFO, - ERROR_ON_GETTING_GRADES, - ERROR_ON_SEND_REQUEST -}; class jceHandler { public: - - jceHandler(user* const a); + enum jceOptions + { + GET_GRADES_BY_DATES, + GET_SCHEDULE + }; + jceHandler(user* const,jceDates* const); ~jceHandler() { delete recieverPage; delete JceConnector; } - void start(); + bool start(); + bool requestMenu(jceOptions); private: + + enum jceErrors { + ERROR_ON_VALIDATION, + ERROR_ON_INPUT, + ERROR_ON_CONNECTING, + ERROR_ON_OPEN_SOCKET, + ERROR_ON_GETTING_INFO, + ERROR_ON_GETTING_GRADES, + ERROR_ON_SEND_REQUEST + }; + void makeFirstVisit(); void makeSecondVisit(); - void makeFurtherRequests(); + void getGraders(); + void getSchedule(); //validation steps @@ -45,22 +53,22 @@ class jceHandler std::string getSecondValidationStep(); //getting html - std::string getGradesPath(std::string fromyear, std::string fromsemester, - std::string toyear,std::string tosemester); + std::string getSchedulePath(); + std::string getGradesPath(); //making server requests std::string makeRequest(std::string); //check if html file contains valid id and hash bool checkValidation(std::string &html); - + void checkConnection(); void printErrorANDabort(jceErrors t); std::string * recieverPage; sslsocket * JceConnector; user * const jceUser; //there is only one user. therefor we need only 1 user in time. we wont change the pointer to other user. and we wont make new user. - + jceDates * const jDates; };