Adding more comunication skills
authorMicke Nordin <mickewiki@gmail.com>
Sun, 10 Jan 2010 11:13:22 +0000 (12:13 +0100)
committerMicke Nordin <mickewiki@gmail.com>
Sun, 10 Jan 2010 11:13:22 +0000 (12:13 +0100)
Makefile
mnencd.hpp
php.cpp [new file with mode: 0755]
php.hpp [new file with mode: 0755]
protocol_v_0.txt

index 3a26f35..1ecd94f 100755 (executable)
--- 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
index 379f7f9..50065c8 100644 (file)
@@ -21,6 +21,9 @@
 #include <string>
 #include <iostream>
 #include <fstream>
+#include "php.hpp"
+#include <vector>
+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<std::string> 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 (executable)
index 0000000..f86878b
--- /dev/null
+++ b/php.cpp
@@ -0,0 +1,64 @@
+//      php.cpp
+//      
+//      Copyright 2010 Micke Nordin <mickewiki@gmail.com>
+//      
+//      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<string> explode(string str, string separator) {
+       vector<string> 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 (executable)
index 0000000..fe91764
--- /dev/null
+++ b/php.hpp
@@ -0,0 +1,28 @@
+//      php.hpp
+//      
+//      Copyright 2010 Micke Nordin <mickewiki@gmail.com>
+//      
+//      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 <vector>
+#include <string>
+using namespace std;
+vector<string> explode(string str, string separator); //Make a vector<string> 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
index 4057a6d..1339010 100644 (file)
@@ -5,6 +5,7 @@ Protocol:
 ==Reply==
 201_Created
 400_Bad_Request 
+403_Forbidden
 500_Internal_Server_Error
 503_Service_Unavailable