diff src/context.cpp @ 160:b3ed72ee6564

allow manual updates to auto whitelist files
author carl
date Tue, 10 Jul 2007 11:20:23 -0700
parents a220bfb9211f
children c4bce911c276
line wrap: on
line diff
--- a/src/context.cpp	Sun Jul 08 11:57:51 2007 -0700
+++ b/src/context.cpp	Tue Jul 10 11:20:23 2007 -0700
@@ -8,16 +8,16 @@
 
 #include "includes.h"
 
-// needed for socket io
-#include <unistd.h>
-#include <sys/ioctl.h>
+#include <arpa/inet.h>
 #include <net/if.h>
-#include <arpa/inet.h>
+#include <netdb.h>
 #include <netinet/in.h>
 #include <netinet/tcp.h>
-#include <netdb.h>
+#include <sys/ioctl.h>
 #include <sys/socket.h>
+#include <sys/stat.h>
 #include <sys/un.h>
+#include <unistd.h>
 
 static char* context_version="$Id$";
 
@@ -425,6 +425,13 @@
 	days	 = d;
 	pthread_mutex_init(&mutex, 0);
 	need	 = false;
+	loaded	 = time(NULL);
+	merge();
+}
+
+
+void WHITELISTER::merge() {
+	time_t now = time(NULL);
 	ifstream ifs;
 	ifs.open(fn);
 	if (!ifs.fail()) {
@@ -435,11 +442,19 @@
 			if (p) {
 				*p = '\0';
 				char *who = strdup(buf);
-				int  when = atoi(p+1);
+				time_t when = atoi(p+1);
+				if ((when == 0) || (when > now)) when = now;
+				autowhite_sent::iterator i = rcpts.find(who);
+				if (i != rcpts.end()) {
+					time_t wh = (*i).second;
+					if (when > wh) rcpts[who] = when;
+				}
+				else {
 				rcpts[who] = when;
 			}
 		}
 	}
+	}
 	ifs.close();
 }
 
@@ -447,6 +462,17 @@
 void WHITELISTER::writer() {
 	pthread_mutex_lock(&mutex);
 		time_t limit = time(NULL) - days*86400;
+
+		// check for manually modified autowhitelist file
+		struct stat st;
+		if (stat(fn, &st)) need = true; // file has disappeared
+		else if (st.st_mtime > loaded) {
+			// file has been manually updated, merge new entries
+			merge();
+			need = true;
+		}
+
+		// purge old entries
 		for (autowhite_sent::iterator i=rcpts.begin(); i!=rcpts.end();) {
 			time_t when = (*i).second;
 			if (when < limit) {
@@ -473,6 +499,7 @@
 			}
 			ofs.close();
 			need = false;
+			loaded = time(NULL);	// update load time
 		}
 	pthread_mutex_unlock(&mutex);
 }