Skip to content

Instantly share code, notes, and snippets.

@bigntallmike
Created March 10, 2020 18:45
Show Gist options
  • Save bigntallmike/5179bad579d36b66fa55ec98a0edc1dc to your computer and use it in GitHub Desktop.
Save bigntallmike/5179bad579d36b66fa55ec98a0edc1dc to your computer and use it in GitHub Desktop.
Delay calculation for E-mail sending under a daily limit with nullmailer
diff -ur nullmailer-2.2/src/send.cc nullmailer-2.2-delay/src/send.cc
--- nullmailer-2.2/src/send.cc 2018-10-12 16:49:30.000000000 -0400
+++ nullmailer-2.2-delay/src/send.cc 2020-03-10 13:58:17.803978134 -0400
@@ -31,6 +31,7 @@
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
+#include <ctime>
#include "ac/time.h"
#include "argparse.h"
#include "autoclose.h"
@@ -45,6 +46,11 @@
#include "selfpipe.h"
#include "setenv.h"
+const long LIMIT_DAILY = 550;
+
+const static long DELAY_FAILURES = 15 * 60 * 1000 * 1000;
+const static char *DELAY_FAILURES_STR = "Resting for 15 minutes ...";
+
const char* cli_program = "nullmailer-send";
selfpipe selfpipe;
@@ -375,6 +381,32 @@
return true;
}
+int get_dayofmonth()
+{
+ time_t t = time(0);
+ tm* now = localtime(&t);
+ return now->tm_mday;
+}
+
+long delay_recalc()
+{
+ static int lastday = 0;
+ static long messagecount = 1;
+
+ int today = get_dayofmonth();
+ if (lastday != today) {
+ messagecount = 1;
+ lastday = today;
+ }
+
+ /* Can be refactored:
+ * float messagedelay = (86400.0 / LIMIT_DAILY) / (LIMIT_DAILY / 2);
+ * float multiplier = (86400.0 * 2.0) / (LIMIT_DAILY * LIMIT_DAILY) */
+ float delay = (86400.0 * 2.0) / (LIMIT_DAILY * LIMIT_DAILY) * messagecount++;
+ fout << "Delaying message #" << messagecount << " for " << (int)delay << " seconds" << endl;
+ return (long)(delay * 1000.0 * 1000.0);
+}
+
void send_all()
{
if(!load_config()) {
@@ -402,12 +434,16 @@
}
}
msg++;
+ fout << DELAY_FAILURES_STR << endl;
+ usleep(DELAY_FAILURES);
break;
case permfail:
if (bounce_msg(*msg, *remote, output))
messages.remove(msg);
else
msg++;
+ fout << DELAY_FAILURES_STR << endl;
+ usleep(DELAY_FAILURES);
break;
default:
if(unlink((*msg).filename.c_str()) == -1) {
@@ -416,10 +452,11 @@
}
else
messages.remove(msg);
+ usleep(delay_recalc());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment