-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Encapsulating time parsing/writing to database. / Fix: did not correc…
…tly update time of last connection.
- Loading branch information
Showing
8 changed files
with
172 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |