This commit is contained in:
Wolfgang Hottgenroth 2013-03-27 20:07:19 +01:00
commit 52fc172950
4 changed files with 120 additions and 0 deletions

69
ChildProt.cpp Normal file
View File

@ -0,0 +1,69 @@
#include <iostream>
#include <string>
#include <sqlite3.h>
#include "Database.hpp"
using namespace std;
class InvalidAddressTuple {};
class AddressTuple {
public :
AddressTuple();
const string& getSender() const { return sender; }
const string& getRecipient() const { return recipient; }
private:
string sender;
string recipient;
};
AddressTuple::AddressTuple() {
sender = "";
recipient = "";
while (1) {
string buffer;
getline(cin, buffer);
if (0 == buffer.length())
break;
string::size_type equalsIdx = buffer.find('=');
if (string::npos != equalsIdx) {
string key = buffer.substr(0, equalsIdx);
string value = buffer.substr(equalsIdx+1);
if ("sender" == key)
sender = value;
if ("recipient" ==key)
recipient = value;
}
}
if ((0 == sender.length()) || (0 == recipient.length()))
throw InvalidAddressTuple();
}
int main() {
string result;
try {
AddressTuple addressTuple;
cerr << addressTuple.getSender() << endl;
cerr << addressTuple.getRecipient() << endl;
DatabaseHandle databaseHandle("/etc/postfix/children.sqlite");
// SELECT id, delegate FROM child_v WHERE address = '%s'
} catch (InvalidAddressTuple& iat) {
cerr << "InvalidAddressTuple" << endl;
result = "OK";
} catch (DatabaseError &de) {
cerr << "DatabaseError" << endl;
result = "OK";
}
cout << "action=" << result << endl << endl;
}

15
Database.cpp Normal file
View File

@ -0,0 +1,15 @@
#include "Database.hpp"
using namespace std;
DatabaseHandle::DatabaseHandle(const string& filename) {
if (SQLITE_OK != sqlite3_open(filename.c_str(), &dbh))
throw DatabaseError();
}
DatabaseHandle::~DatabaseHandle() {
sqlite3_close(dbh);
}

21
Database.hpp Normal file
View File

@ -0,0 +1,21 @@
#ifndef DATABASE_HPP_
#define DATABASE_HPP_
#include <string>
#include <sqlite3.h>
class DatabaseError {};
// resource management class
class DatabaseHandle {
public:
DatabaseHandle(const std::string& filename);
~DatabaseHandle();
sqlite3 *getHandle() { return dbh; }
private:
sqlite3 *dbh;
};
#endif /* DATABASE_HPP_ */

15
Makefile Normal file
View File

@ -0,0 +1,15 @@
CXXFLAGS = -O2 -g -Wall -fmessage-length=0
OBJS = Database.o ChildProt.o
LIBS = -lsqlite3
TARGET = ChildProt
$(TARGET): $(OBJS)
$(CXX) -o $(TARGET) $(OBJS) $(LIBS)
all: $(TARGET)
clean:
rm -f $(OBJS) $(TARGET)