summaryrefslogtreecommitdiff
path: root/blabouncer.c
diff options
context:
space:
mode:
Diffstat (limited to 'blabouncer.c')
-rw-r--r--blabouncer.c67
1 files changed, 65 insertions, 2 deletions
diff --git a/blabouncer.c b/blabouncer.c
index 5fa2e5d..fe8cd1e 100644
--- a/blabouncer.c
+++ b/blabouncer.c
@@ -17,6 +17,7 @@
#include <sys/select.h>
#include <time.h>
#include <sys/stat.h>
+#include <limits.h>
#include <openssl/ssl.h>
#include <openssl/err.h>
#include <openssl/bio.h>
@@ -52,9 +53,11 @@
#define MAXPORTLEN 6 // Up to 65535, so 5 characters + 1 for null
#define MAXAUTOCHANLEN 1024 // Randomly picked maximum length of the auto channel list
#define SERVERTIMEOUT 300 // How many seconds to wait without hearing from the server before assuming a timeout
+#define DEBUGFILESKEEP 5 // How many debug files to keep around
// Global debug control
int debug = 0;
+char debugpath[PATH_MAX];
struct channel {
char name[MAXCHANLENGTH];
@@ -2548,6 +2551,10 @@ int main(int argc, char *argv[]) {
// Is debugging enabled?
debug = getconfint("debug", settings.conffile);
+ if (!snprintf(debugpath, PATH_MAX, "%s/debug.txt", settings.basedir)) {
+ fprintf(stderr, "Error while preparing debug path location!\n");
+ exit(1);
+ }
// Make sure the base directory exists
struct stat st = {0};
@@ -2566,12 +2573,68 @@ int main(int argc, char *argv[]) {
// Is replay logging enabled?
settings.replaylogging = getconfint("replaylogging", settings.conffile);
+ // Prepare the debug file
+ // (Keep DEBUGFILESKEEP number of debug files around for past debugging)
+ if (DEBUGFILESKEEP > 0) {
+ // Check each possible debug file and rename it to one higher
+ char tmppath[PATH_MAX];
+ char tmppathnew[PATH_MAX];
+ // Delete or rename numbered debug files first
+ for (int i = DEBUGFILESKEEP; i > 0; i--) {
+ if (!snprintf(tmppath, PATH_MAX - 1, "%s.%d", debugpath, i)) {
+ fprintf(stderr, "Error while preparing to check old debug files!\n");
+ exit(1);
+ }
+ if (!access(tmppath, F_OK)) {
+ if (i == DEBUGFILESKEEP) {
+ if (remove(tmppath)) {
+ printf("error deleting old debug file '%s'.\n", tmppath);
+ exit(1);
+ }
+ } else {
+ if (!snprintf(tmppathnew, PATH_MAX - 1, "%s.%d", debugpath, i + 1)) {
+ fprintf(stderr, "Error while preparing to rename old debug file!\n");
+ exit(1);
+ }
+ if (rename(tmppath, tmppathnew)) {
+ printf("error renaming old debug file '%s' to '%s'.\n", tmppath, tmppathnew);
+ exit(1);
+ }
+ }
+ }
+ }
+ // Rename the previous debug file next
+ if (!access(debugpath, F_OK)) {
+ if (rename(debugpath, tmppath)) {
+ printf("error renaming old debug file '%s' to '%s'.\n", debugpath, tmppath);
+ exit(1);
+ }
+ }
+ }
+
+ // Write a friendly debug message to file (even if debug to file is disabled)
+ // Prepare a friendly time string
+ time_t rawtime;
+ struct tm * timeinfo;
+ time(&rawtime);
+ timeinfo = localtime(&rawtime);
+ // Strip the trailing newline
+ char timestr[MAXCHAR];
+ snprintf(timestr, MAXCHAR, "%s", asctime(timeinfo));
+ timestr[strlen(timestr) - 1] = '\0';
+ // Note the old debug setting
+ int debugold = debug;
+ // Temporarily enable debugging to file
+ debug = 1;
+ // Print it
+ debugprint("blabouncer started at %s, debugging is set to %d.\n", timestr, debugold);
+ // Set debugging back to whatever it was
+ debug = debugold;
+
// TODO: see if any of this can be shared (i.e. 1. avoid code duplication, and 2. see if variables can be shared between client/server sockets)
// I will try to keep to the notation of "server" meaning the real IRCd, "bouncer" meaning the bouncer, and "client" meaning the real IRC client
- // BOUNCER-TO-SERVER socket things
-
// Create server socket
int serversockfd = createserversocket(settings.ircserver, settings.ircserverport);