From: Micke Nordin Date: Sun, 10 Jan 2010 11:13:22 +0000 (+0100) Subject: Adding more comunication skills X-Git-Url: http://git.maemo.org/git/?p=mnenc;a=commitdiff_plain;h=6a648ab17c5def963cedae232102f72e3a2f70ee Adding more comunication skills --- diff --git a/Makefile b/Makefile index 3a26f35..1ecd94f 100755 --- a/Makefile +++ b/Makefile @@ -1,14 +1,17 @@ -mnencd : mnencd.o password.o mnenc.o - g++ -o mnencd -g -Wall mnencd.o password.o mnenc.o +mnencd : mnencd.o password.o mnenc.o php.o + g++ -o mnencd -g -Wall mnencd.o password.o mnenc.o php.o -mnencd.o: mnencd.cpp password.hpp mnenc.hpp +mnencd.o: mnencd.cpp password.hpp mnenc.hpp php.hpp g++ -c -g -Wall mnencd.cpp -password.o: password.cpp password.hpp +password.o: password.cpp password.hpp g++ -c -g -Wall password.cpp mnenc.o: mnenc.cpp mnenc.hpp g++ -c -g -Wall mnenc.cpp +php.o: php.cpp php.hpp + g++ -c -g -Wall php.cpp + clean: rm *.o mnencd diff --git a/mnencd.hpp b/mnencd.hpp index 379f7f9..50065c8 100644 --- a/mnencd.hpp +++ b/mnencd.hpp @@ -21,6 +21,9 @@ #include #include #include +#include "php.hpp" +#include +std::string masterpasswd = ""; std::string remove_char(std::string str, char c) { std::string::size_type k = 0; @@ -60,8 +63,17 @@ void put_password(std::string masterpasswd, std::string passwd, std::string user } std::string do_something(std::string str) { - if(str[0] == '0') { - return "201_Created\n"; + std::vector request; + request = explode(str, "|"); + if(request[0][0] == '0') { + if(masterpasswd == "") { + masterpasswd = request[1]; + return "201_Created\n"; + } else if(masterpasswd!= "") { + return "403 Forbidden\n"; + } else { + return "400_Bad_Request " + str + '\n'; + } } else { return "400_Bad_Request " + str + '\n'; } diff --git a/php.cpp b/php.cpp new file mode 100755 index 0000000..f86878b --- /dev/null +++ b/php.cpp @@ -0,0 +1,64 @@ +// php.cpp +// +// Copyright 2010 Micke Nordin +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, +// MA 02110-1301, USA. +#include "php.hpp" + +vector explode(string str, string separator) { + vector results; //To hold the reslut + string::size_type found; //Varible to compare with + found = str.find_first_of(separator); //See if/where we have the separator + while(found != string::npos) { //While we have separators + if(found > 0) { //If we have a match + results.push_back(str.substr(0,found)); //Put it in the vector + } + str = str.substr(found+1); //Remove the part that has allready been used + found = str.find_first_of(separator);//Find next + } + if(str.length() > 0) { //Special case for the last bit + results.push_back(str); + } + + return results; +} + +string reverse(string &str) { + char c[str.size() + 1]; + int j = 0; + + for(int i = str.size(); i > 0; i--) { + c[j] = str[i]; + j++; + } + c[str.size()] = '\0'; + string rstr = c; + return rstr; + +} + +string trim(string &str, char const* sepSet) { + string::size_type const first = str.find_first_not_of(sepSet); //Find first char that is not the ones we want to remove + + if(first==string::npos ) { + return string(); + } else { + str = str.substr(first, str.find_last_not_of(sepSet)-first+1); //If we have chars we want to keep return those + str = reverse(str); + str.substr(first, str.find_last_not_of(sepSet)-first+1); + return reverse(str); + } +} diff --git a/php.hpp b/php.hpp new file mode 100755 index 0000000..fe91764 --- /dev/null +++ b/php.hpp @@ -0,0 +1,28 @@ +// php.hpp +// +// Copyright 2010 Micke Nordin +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, +// MA 02110-1301, USA. +#ifndef _php_h_included_ +#define _php_h_included_ +#include +#include +using namespace std; +vector explode(string str, string separator); //Make a vector from a string by slitting by a delimiter +string reverse(string &str); //Revers a string +string trim(string &str, char const* sepSet); //Remove some characters from a string + +#endif diff --git a/protocol_v_0.txt b/protocol_v_0.txt index 4057a6d..1339010 100644 --- a/protocol_v_0.txt +++ b/protocol_v_0.txt @@ -5,6 +5,7 @@ Protocol: ==Reply== 201_Created 400_Bad_Request +403_Forbidden 500_Internal_Server_Error 503_Service_Unavailable