Skip to content

Commit

Permalink
Encapsulating time parsing/writing to database. / Fix: did not correc…
Browse files Browse the repository at this point in the history
…tly update time of last connection.
  • Loading branch information
mawis committed Oct 29, 2011
1 parent bfa29bc commit 23f5c7f
Show file tree
Hide file tree
Showing 8 changed files with 172 additions and 23 deletions.
5 changes: 5 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
2011-10-30 Matthias Wimmer <[email protected]>

* timestore.cc: encapsulate parsing of database values
* timestore.h: same

2011-10-28 Matthias Wimmer <[email protected]>

* couriergrey.cc: separate database access to its own class
Expand Down
4 changes: 2 additions & 2 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ SUBDIRS = intl m4 man po

bin_PROGRAMS = couriergrey

noinst_HEADERS = couriergrey.h database.h mail_processor.h message_processor.h whitelist.h
noinst_HEADERS = couriergrey.h database.h mail_processor.h message_processor.h timestore.h whitelist.h

sysconf_DATA = whitelist_ip.dist

couriergrey_SOURCES = couriergrey.cc database.cc mail_processor.cc message_processor.cc whitelist.cc
couriergrey_SOURCES = couriergrey.cc database.cc mail_processor.cc message_processor.cc timestore.cc whitelist.cc

couriergrey_LDFLAGS = @LDFLAGS@

Expand Down
20 changes: 18 additions & 2 deletions couriergrey.cc
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,30 @@ int main(int argc, char const** argv) {
// dump database if requested
if (dump_database) {
try {
couriergrey::database db;
couriergrey::timestore db;

std::cout << N_("Content of the greylist database:") << std::endl;

std::list<std::string> keys = db.get_keys();
for (std::list<std::string>::const_iterator p = keys.begin(); p != keys.end(); ++p) {
std::cout << *p << std::endl;
std::cout << "\t \\ " << db.fetch(*p) << std::endl;
std::pair<std::time_t, std::time_t> times = db.fetch(*p);
struct std::tm first_time_tm;
gmtime_r(&times.first, &first_time_tm);
struct std::tm last_time_tm;
gmtime_r(&times.second, &last_time_tm);
char first_time[128];
char last_time[128];
std::size_t first_time_size = strftime(first_time, sizeof(first_time), "%Y-%m-%dT%H:%M:%SZ", &first_time_tm);
std::size_t last_time_size = strftime(last_time, sizeof(last_time), "%Y-%m-%dT%H:%M:%SZ", &last_time_tm);
std::cout << "\t";
if (first_time_size > 0) {
std::cout << first_time << " ";
}
if (last_time_size > 0) {
std::cout << last_time;
}
std::cout << std::endl;
}
return 0;
} catch (Glib::ustring msg) {
Expand Down
1 change: 1 addition & 0 deletions couriergrey.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#endif

#include <database.h>
#include <timestore.h>
#include <whitelist.h>
#include <mail_processor.h>
#include <message_processor.h>
Expand Down
4 changes: 1 addition & 3 deletions database.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@
#include <cstdio>
#include <glibmm.h>

#define SOCKET_BACKLOG_SIZE 10

namespace couriergrey {
database::database() : db(NULL) {
for (int retry = 0; db == NULL && retry < 10; retry++) {
Expand Down Expand Up @@ -87,7 +85,7 @@ namespace couriergrey {
::datum value_datum;
value_datum.dptr = const_cast<char*>(value.c_str());
value_datum.dsize = value.length();
::gdbm_store(db, key_datum, value_datum, GDBM_INSERT);
::gdbm_store(db, key_datum, value_datum, GDBM_REPLACE);
}

std::list<std::string> database::get_keys() {
Expand Down
21 changes: 5 additions & 16 deletions message_processor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#endif

#include "message_processor.h"
#include "database.h"
#include "timestore.h"
#include "mail_processor.h"
#include <iostream>
#include <cstring>
Expand Down Expand Up @@ -183,27 +183,16 @@ namespace couriergrey {

// open the database
try {
database db;
timestore db;

std::string mail_identifier_string = mail_identifier.str();

std::string value = db.fetch(mail_identifier_string);

// check when there has been the first delivery attempt for this mail
std::time_t first_delivery = 0;
if (value.empty()) {
// new mail, first attempt is now ...
first_delivery = std::time(NULL);
} else {
// mail relation is already in the database, read first attempt from there
std::istringstream value_stream(value);
value_stream >> first_delivery;
}
std::pair<std::time_t, std::time_t> value = db.fetch(mail_identifier_string);
std::time_t first_delivery = value.first;

// update the content (first attempt + last access for cleanup) in the database
std::ostringstream value_stream;
value_stream << first_delivery << " " << std::time(NULL);
db.store(mail_identifier_string, value_stream.str());
db.store(mail_identifier_string, first_delivery, std::time(NULL));

// check if the first attempt for this mail is old enought so that we can accept the mail
std::time_t seconds_to_wait = (first_delivery + 120) - std::time(NULL);
Expand Down
64 changes: 64 additions & 0 deletions timestore.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/* ---------------------------------------------------------------------------
* couriergrey - Greylisting filter for Courier
* Copyright (C) 2007 Matthias Wimmer <[email protected]>
*
* 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 2 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
* ---------------------------------------------------------------------------
*/

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include "timestore.h"
#include <sstream>

namespace couriergrey {

timestore::timestore() {
}

timestore::~timestore() {
}

std::pair<std::time_t, std::time_t> timestore::fetch(std::string const& key) const {
std::string database_value = db.fetch(key);

if (database_value.empty()) {
std::time_t now = std::time(NULL);
return std::pair<std::time_t, std::time_t>(now, now);
}

std::istringstream value_stream(database_value);

std::time_t first_connect;
std::time_t last_connect;

value_stream >> first_connect;
value_stream >> last_connect;

return std::pair<std::time_t, std::time_t>(first_connect, last_connect);
}

void timestore::store(std::string const& key, std::time_t first_connect, std::time_t last_connect) {
std::ostringstream value_stream;
value_stream << first_connect << ' ' << last_connect;
db.store(key, value_stream.str());
}

std::list<std::string> timestore::get_keys() {
return db.get_keys();
}
}
76 changes: 76 additions & 0 deletions timestore.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/* ---------------------------------------------------------------------------
* couriergrey - Greylisting filter for Courier
* Copyright (C) 2007-2011 Matthias Wimmer <[email protected]>
*
* 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 2 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
* ---------------------------------------------------------------------------
*/

#ifndef TIMESTORE_H
#define TIMESTORE_H

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <string>
#include <list>
#include <ctime>

#include <database.h>

#ifndef N_
# define N_(n) (n)
#endif

namespace couriergrey {
/**
* class storing the learned data
*/
class timestore {
public:
/**
* create a timestore instance
*/
timestore();

/**
* destruct a timestore instance
*/
~timestore();

/**
* fetch a value from a key
*/
std::pair<std::time_t, std::time_t> fetch(std::string const& key) const;

/**
* store a value to a key
*/
void store(std::string const& key, std::time_t first_connect, std::time_t last_connect);

/**
* get all the keys in the timestore
*/
std::list<std::string> get_keys();
private:
/**
* The database we use
*/
database db;
};
}

#endif // TIMESTORE_H

0 comments on commit 23f5c7f

Please sign in to comment.