Adding more comunication skills
[mnenc] / mnencd.hpp
1 //      mnencd.hpp
2 //      
3 //      Copyright 2010 Micke Nordin <mickewiki@gmail.com>
4 //      
5 //      This program is free software; you can redistribute it and/or modify
6 //      it under the terms of the GNU General Public License as published by
7 //      the Free Software Foundation; either version 3 of the License, or
8 //      (at your option) any later version.
9 //      
10 //      This program is distributed in the hope that it will be useful,
11 //      but WITHOUT ANY WARRANTY; without even the implied warranty of
12 //      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 //      GNU General Public License for more details.
14 //      
15 //      You should have received a copy of the GNU General Public License
16 //      along with this program; if not, write to the Free Software
17 //      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
18 //      MA 02110-1301, USA.
19 #ifndef _mnencd_h_included_
20 #define _mnencd_h_included_
21 #include <string>
22 #include <iostream>
23 #include <fstream>
24 #include "php.hpp"
25 #include <vector>
26 std::string masterpasswd = "";
27
28 std::string remove_char(std::string str, char c) {
29         std::string::size_type k = 0;
30         while((k=str.find(c,k))!=str.npos) {
31                 str.erase(k, 1);
32         }
33         return str;
34 }
35
36 std::string remove_chars(std::string str) {
37         std::string chars = " \t\n\b\a-?+\\{[]}'*'";
38         for(int i = 0; i < (signed) chars.size(); i++) {
39                 str = remove_char(str, chars[i]);
40         }
41         
42         return str;     
43 }
44
45 std::string make_filename(std::string user, std::string app) {
46         return remove_chars(app + user);
47 }
48 std::string get_password(std::string masterpasswd, std::string user, std::string app) {
49         mnenc menc = mnenc();
50         menc.genkey(masterpasswd);
51         std::string key = menc.get_key();
52         password pw = password("", "", key);
53         pw.from_file(make_filename(user, app));
54         return menc.decrypt(key, pw.get_enc());
55 }
56
57 void put_password(std::string masterpasswd, std::string passwd, std::string user, std::string app) {
58         mnenc menc = mnenc();
59         menc.genkey(masterpasswd);
60         std::string key = menc.get_key();
61         password pw = password(menc.encrypt(key, passwd ), "", key);
62         pw.to_file(make_filename(user, app));
63 }
64
65 std::string do_something(std::string str) {
66         std::vector<std::string> request;
67         request = explode(str, "|");
68         if(request[0][0] == '0') {
69                 if(masterpasswd == "")  {
70                         masterpasswd = request[1];
71                         return "201_Created\n";
72                 } else if(masterpasswd!= "") {
73                         return "403 Forbidden\n";
74                 } else {
75                         return "400_Bad_Request " + str + '\n';
76                 }
77         } else {
78                 return "400_Bad_Request " + str + '\n';
79         }
80 }
81
82 std::string m_read() {
83         std::string str;
84         std::ifstream is("fife");
85         getline(is, str);
86         is.close();
87         return str;
88 }
89
90 void m_send(std::string message) {
91         std::ofstream os("fife");
92         os << message;
93         os.close();
94 }
95 #endif