summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--blabouncer.c328
-rw-r--r--blabouncer.conf.example6
-rw-r--r--config.c20
-rw-r--r--config.h2
-rw-r--r--functions.c54
-rw-r--r--functions.h4
-rw-r--r--logging.c12
-rw-r--r--replay.c2
-rw-r--r--sockets.c10
-rw-r--r--sockets.h2
10 files changed, 238 insertions, 202 deletions
diff --git a/blabouncer.c b/blabouncer.c
index 85305c6..e67c083 100644
--- a/blabouncer.c
+++ b/blabouncer.c
@@ -53,6 +53,9 @@
#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
+// Global debug control
+int debug = 0;
+
struct channel {
char name[MAXCHANLENGTH];
char topic[MAXDATASIZE]; // TODO - Is there a particular maximum topic length?
@@ -152,7 +155,7 @@ int sendtoclient(int fd, char *strsrc, struct client *clients, struct settings *
if (clients[i].fd == fd) {
// Found client in array, check authentication status
if (!clients[i].authed && !bypass) {
- printf("sendtoclient(): skipping unauthenticated client with fd %d.\n", clients[i].fd);
+ debugprint("sendtoclient(): skipping unauthenticated client with fd %d.\n", clients[i].fd);
return 0;
}
// Break when we get to the correct fd, "i" is now the position in the clients array of our client
@@ -160,7 +163,7 @@ int sendtoclient(int fd, char *strsrc, struct client *clients, struct settings *
}
}
- printf("sendtoclient(): sending \"%s\" (length %zd) to client with fd %d.\n", str, strlen(str), fd);
+ debugprint("sendtoclient(): sending \"%s\" (length %zd) to client with fd %d.\n", str, strlen(str), fd);
if (socksend(clients[i].ssl, str, strlen(str), settings->clienttls) == -1) {
perror("error: sendtoclient() send()\n");
return 0;
@@ -196,7 +199,7 @@ int sendtoallclients(struct client *clients, char *strsrc, int except, struct se
for (int i = 0; i < MAXCLIENTS; i++) {
// Trust clientfd of 0, only we can set that ourselves
if (!except) {
- printf("sendtoallclients(): trusting clientfd of 0.\n");
+ debugprint("sendtoallclients(): trusting clientfd of 0.\n");
break;
}
if (clients[i].fd == except) {
@@ -218,10 +221,10 @@ int sendtoallclients(struct client *clients, char *strsrc, int except, struct se
if (clients[i].fd > 0) {
// ...and authenticated
if (!clients[i].authed) {
- printf("sendtoallclients(): skipping unauthenticated client with fd %d.\n", clients[i].fd);
+ debugprint("sendtoallclients(): skipping unauthenticated client with fd %d.\n", clients[i].fd);
continue;
}
- printf("sendtoallclients(): %s: sending '%s' to client with fd %d.\n", sendertype, str, clients[i].fd);
+ debugprint("sendtoallclients(): %s: sending '%s' to client with fd %d.\n", sendertype, str, clients[i].fd);
if (socksend(clients[i].ssl, str, strlen(str), settings->clienttls) == -1) {
printf("error: sendtoallclients() send() with fd '%d'.\n", clients[i].fd);
}
@@ -247,7 +250,7 @@ int sendtoserver(SSL *server_ssl, char *strsrc, int str_len, int clientfd, struc
for (int i = 0; i < MAXCLIENTS; i++) {
// Trust clientfd of 0, only we can set that ourselves
if (!clientfd) {
- printf("sendtoserver(): trusting clientfd of 0.\n");
+ debugprint("sendtoserver(): trusting clientfd of 0.\n");
break;
}
if (clients[i].fd == clientfd) {
@@ -259,7 +262,7 @@ int sendtoserver(SSL *server_ssl, char *strsrc, int str_len, int clientfd, struc
}
}
- printf("sendtoserver(): sending %s to IRC server (length %d).\n", str, str_len);
+ debugprint("sendtoserver(): sending %s to IRC server (length %d).\n", str, str_len);
if (socksend(server_ssl, str, str_len, settings->servertls) == -1) {
printf("error: sendtoserver() send()\n");
return 0;
@@ -273,7 +276,7 @@ int sendtoserver(SSL *server_ssl, char *strsrc, int str_len, int clientfd, struc
// Also set its authentication and registration statuses to 0.
// Also set the pending statuses to 0
int disconnectclient(int fd, struct client *clients, struct ircdstrings *ircdstrings, struct settings *settings) {
- printf("disconnectclient(): disconnecting client fd '%d'\n", fd);
+ debugprint("disconnectclient(): disconnecting client fd '%d'\n", fd);
// Alert other clients about the disconnection (don't send yet, we haven't removed from the clients array yet)
char alertmsg[MAXDATASIZE];
@@ -284,7 +287,7 @@ int disconnectclient(int fd, struct client *clients, struct ircdstrings *ircdstr
// Remove the client from the clients array
for (int i = 0; i < MAXCLIENTS; i++) {
if (clients[i].fd == fd) {
- printf("found and clearing fd %d from clients[%d]\n", fd, i);
+ debugprint("found and clearing fd %d from clients[%d]\n", fd, i);
clients[i].fd = 0;
clients[i].authed = 0;
clients[i].registered = 0;
@@ -314,7 +317,7 @@ int disconnectclient(int fd, struct client *clients, struct ircdstrings *ircdstr
}
int createchannel(struct channel *channels, char *name, char *topic, char *topicwho, char *topicwhen) {
- printf("createchannel(): given \"%s\", \"%s\", \"%s\", and \"%s\".\n", name, topic, topicwho, topicwhen);
+ debugprint("createchannel(): given \"%s\", \"%s\", \"%s\", and \"%s\".\n", name, topic, topicwho, topicwhen);
// Make sure the channel doesn't already exist
for (int i = 0; i < MAXCHANNELS; i++) {
@@ -331,12 +334,12 @@ int createchannel(struct channel *channels, char *name, char *topic, char *topic
// ...and set the name and topic
strncpy(channels[i].name, name, strlen(name));
channels[i].name[strlen(name)] = '\0';
- printf("createchannel(): name given was '%s', length '%ld'.\n", name, strlen(name));
- printf("createchannel(): name set to '%s', length '%ld'.\n", channels[i].name, strlen(channels[i].name));
+ debugprint("createchannel(): name given was '%s', length '%ld'.\n", name, strlen(name));
+ debugprint("createchannel(): name set to '%s', length '%ld'.\n", channels[i].name, strlen(channels[i].name));
strncpy(channels[i].topic, topic, strlen(topic));
channels[i].topic[strlen(topic)] = '\0';
- printf("createchannel(): topic given was '%s', length '%ld'.\n", topic, strlen(topic));
- printf("createchannel(): topic set to '%s', length '%ld'.\n", channels[i].topic, strlen(channels[i].topic));
+ debugprint("createchannel(): topic given was '%s', length '%ld'.\n", topic, strlen(topic));
+ debugprint("createchannel(): topic set to '%s', length '%ld'.\n", channels[i].topic, strlen(channels[i].topic));
strncpy(channels[i].topicwho, topicwho, strlen(topicwho));
channels[i].topicwho[strlen(topicwho)] = '\0';
strncpy(channels[i].topicwhen, topicwhen, strlen(topicwhen));
@@ -354,10 +357,10 @@ int createchannel(struct channel *channels, char *name, char *topic, char *topic
}
int setchanneltopicwhotime(struct channel *channels, char *channelname, char *who, char *when) {
- printf("setchanneltopicwhotime(): given \"%s\", \"%s\", and \"%s\".\n", channelname, who, when);
+ debugprint("setchanneltopicwhotime(): given \"%s\", \"%s\", and \"%s\".\n", channelname, who, when);
- printf("setchanneltopicwhotime(): who: '%s' with length '%ld'.\n", who, strlen(who));
- printf("setchanneltopicwhotime(): when: '%s' with length '%ld'.\n", when, strlen(when));
+ debugprint("setchanneltopicwhotime(): who: '%s' with length '%ld'.\n", who, strlen(who));
+ debugprint("setchanneltopicwhotime(): when: '%s' with length '%ld'.\n", when, strlen(when));
for (int i = 0; i < MAXCHANNELS; i++) {
if (strncmp(channels[i].name, channelname, strlen(channelname)) == 0) {
@@ -374,7 +377,7 @@ int setchanneltopicwhotime(struct channel *channels, char *channelname, char *wh
}
int setchanneltopic(struct channel *channels, char *channelname, char *topic) {
- printf("setchanneltopic(): given \"%s\" and \"%s\".\n", channelname, topic);
+ debugprint("setchanneltopic(): given \"%s\" and \"%s\".\n", channelname, topic);
for (int i = 0; i < MAXCHANNELS; i++) {
if (strncmp(channels[i].name, channelname, strlen(channelname)) == 0) {
@@ -397,13 +400,13 @@ int getchannelcount(struct channel *channels) {
}
}
- printf("getchannelcount(): counted %d channels.\n", count);
+ debugprint("getchannelcount(): counted %d channels.\n", count);
return count;
}
int removechannel(struct channel *channels, char *name) {
- printf("removechannel(): given \"%s\".\n", name);
+ debugprint("removechannel(): given \"%s\".\n", name);
// Clear its topic setter and timestamp...
setchanneltopicwhotime(channels, name, "", "0");
@@ -413,7 +416,7 @@ int removechannel(struct channel *channels, char *name) {
if (strncmp(channels[i].name, name, strlen(name)) == 0) {
// ..and NULL its name (0th character = '\0')
channels[i].name[0] = '\0';
- printf("removechannel(): channel '%s' removed and topicwhen set to '%s'.\n", name, channels[i].topicwhen);
+ debugprint("removechannel(): channel '%s' removed and topicwhen set to '%s'.\n", name, channels[i].topicwhen);
return 1;
}
}
@@ -427,14 +430,14 @@ int removechannel(struct channel *channels, char *name) {
// Check if we have the NAMES for the channel 'name' already.
// Return the 1 if we do, 0 if we don't, or -1 if there's an error.
int channelgotnames(struct channel *channels, char *name) {
- printf("channelgotnames(): given '%s'.\n", name);
+ debugprint("channelgotnames(): given '%s'.\n", name);
for (int i = 0; i < MAXCHANNELS; i++) {
if (strncmp(channels[i].name, name, strlen(name)) == 0) {
if (channels[i].gotnames) {
- printf("channelgotnames(): channel '%s' gotnames was set, returning '%d'.\n", channels[i].name, channels[i].gotnames);
+ debugprint("channelgotnames(): channel '%s' gotnames was set, returning '%d'.\n", channels[i].name, channels[i].gotnames);
return 1;
} else {
- printf("channelgotnames(): channel '%s' gotnames was not set, returning '%d'.\n", channels[i].name, channels[i].gotnames);
+ debugprint("channelgotnames(): channel '%s' gotnames was not set, returning '%d'.\n", channels[i].name, channels[i].gotnames);
return 0;
}
}
@@ -458,13 +461,13 @@ int inchannel(struct channel *channels, char *name) {
for (int i = 0; i < MAXCHANNELS; i++) {
if (strncmp(channels[i].name, name, strlen(name)) == 0) {
- printf("inchannel(): in channel '%s'.\n", name);
+ debugprint("inchannel(): in channel '%s'.\n", name);
return 1;
}
}
// We're not in the channel
- printf("inchannel(): NOT in channel '%s'.\n", name);
+ debugprint("inchannel(): NOT in channel '%s'.\n", name);
return 0;
}
@@ -472,7 +475,7 @@ int inchannel(struct channel *channels, char *name) {
// named 'channel'.
// Returns -1 if there was an error.
int channelindex(struct channel *channels, char *name) {
- printf("channelindex(): given '%s'.\n", name);
+ debugprint("channelindex(): given '%s'.\n", name);
for (int i = 0; i < MAXCHANNELS; i++) {
if (strncmp(channels[i].name, name, strlen(name)) == 0) {
return i;
@@ -493,7 +496,7 @@ int doreplay(int sourcefd, int replayseconds, struct client *clients, struct set
// Figure out how many lines to replay
int numlines = replaylines(replayseconds, settings->basedir);
- printf("Replay log lines: '%d'.\n", numlines);
+ debugprint("Replay log lines: '%d'.\n", numlines);
if (numlines < 0) {
printf("Error getting number of replay lines.\n");
@@ -553,7 +556,7 @@ int doreplay(int sourcefd, int replayseconds, struct client *clients, struct set
// Check if we're currently in this channel or if the log line is from us
if (!inchannel(channels, tokens[2] + offset) || strncmp(tokens[0], ircdstrings->ircnick, strlen(tokens[0])) == 0) {
- printf("Not sending '%s' replay line '%s'.\n", tokens[1], outgoingmsg);
+ debugprint("Not sending '%s' replay line '%s'.\n", tokens[1], outgoingmsg);
free(strcopyPtr);
continue;
}
@@ -561,7 +564,7 @@ int doreplay(int sourcefd, int replayseconds, struct client *clients, struct set
free(strcopyPtr);
- printf("Sending replay line: '%s'.\n", outgoingmsg);
+ debugprint("Sending replay line: '%s'.\n", outgoingmsg);
sendtoclient(sourcefd, outgoingmsg, clients, settings, 0);
}
@@ -582,7 +585,7 @@ int numclients(struct client *clients) {
}
}
- printf("numclients(): '%d' clients connected.\n", count);
+ debugprint("numclients(): '%d' clients connected.\n", count);
return count;
}
@@ -592,7 +595,7 @@ int numclients(struct client *clients) {
int joinautochannels(SSL *server_ssl, struct client *clients, struct settings *settings) {
if (strlen(settings->autochannels) == 0) {
// None configured
- printf("joinautochannels(): none configured.\n");
+ debugprint("joinautochannels(): none configured.\n");
return 1;
}
@@ -611,7 +614,7 @@ int joinautochannels(SSL *server_ssl, struct client *clients, struct settings *s
while ((token = strsep(&strcopy, ",")) != NULL) {
if (*token == '\0') continue; // Skip consecutive matches
if (counter >= MAXAUTOCHANLEN) break; // Too many tokens
- printf(" >> Auto channel: '%s', length '%ld'.\n", token, strlen(token));
+ debugprint(" >> Auto channel: '%s', length '%ld'.\n", token, strlen(token));
// Copy into the token array (strlen + 1 to get the NULL terminator)
strncpy(tokens[counter], token, strlen(token) + 1);
if (strlen(tokens[counter]) > MAXCHANLENGTH) {
@@ -623,7 +626,7 @@ int joinautochannels(SSL *server_ssl, struct client *clients, struct settings *s
// Join all the channels
for (int i = 0; i < counter; i++) {
- printf("joinautochannels(): Joining '%s'.\n", tokens[i]);
+ debugprint("joinautochannels(): Joining '%s'.\n", tokens[i]);
char joinmsg[MAXDATASIZE];
snprintf(joinmsg, MAXDATASIZE, "JOIN %s", tokens[i]);
sendtoserver(server_ssl, joinmsg, strlen(joinmsg), 0, clients, settings);
@@ -651,34 +654,34 @@ void tryautonick(struct ircdstrings *ircdstrings) {
// If we've already started trying autonick, just replace the last character a the new number
if (ircdstrings->autonicknum > 1) {
- printf("tryautonick(): already started autonick, starting with '%s' length '%ld'.\n", ircdstrings->ircnick, strlen(ircdstrings->ircnick));
+ debugprint("tryautonick(): already started autonick, starting with '%s' length '%ld'.\n", ircdstrings->ircnick, strlen(ircdstrings->ircnick));
ircdstrings->ircnick[oldlen - 1] = ircdstrings->autonicknum + '0';
// And null terminate
ircdstrings->ircnick[oldlen] = '\0';
// If the nick is longer than or equal to the RFC 1459 max nick
// length then try sticking the number at the end
} else if (oldlen >= MAXRFCNICKLEN) {
- printf("tryautonick(): long old nick, starting with '%s' length '%ld'.\n", ircdstrings->ircnick, strlen(ircdstrings->ircnick));
+ debugprint("tryautonick(): long old nick, starting with '%s' length '%ld'.\n", ircdstrings->ircnick, strlen(ircdstrings->ircnick));
// (+ '0' to make char from int)
ircdstrings->ircnick[MAXRFCNICKLEN] = ircdstrings->autonicknum + '0';
// And null terminate
ircdstrings->ircnick[MAXRFCNICKLEN + 1] = '\0';
// Otherwise, just stick it on the end (+ '0' to make char from int)
} else {
- printf("tryautonick(): short old nick, starting with '%s' length '%ld'.\n", ircdstrings->ircnick, strlen(ircdstrings->ircnick));
+ debugprint("tryautonick(): short old nick, starting with '%s' length '%ld'.\n", ircdstrings->ircnick, strlen(ircdstrings->ircnick));
ircdstrings->ircnick[oldlen] = ircdstrings->autonicknum + '0';
// And null terminate
ircdstrings->ircnick[oldlen + 1] = '\0';
}
- printf("tryautonick(): set irdstrings->ircnick to '%s'.\n", ircdstrings->ircnick);
+ debugprint("tryautonick(): set irdstrings->ircnick to '%s'.\n", ircdstrings->ircnick);
}
int connecttoircserver(SSL_CTX **serverctx, SSL **server_ssl, int *serversockfd, struct ircdstrings *ircdstrings, struct settings *settings, struct client *clients) {
char outgoingmsg[MAXDATASIZE]; // String to send to server
if (settings->servertls) {
- printf("server openssl start.\n");
+ debugprint("server openssl start.\n");
*serverctx = create_openssl_context(SOURCE_SERVER);
configure_openssl_context(*serverctx, NULL, NULL);
*server_ssl = SSL_new(*serverctx);
@@ -686,9 +689,9 @@ int connecttoircserver(SSL_CTX **serverctx, SSL **server_ssl, int *serversockfd,
if (SSL_connect(*server_ssl) == -1) {
ERR_print_errors_fp(stderr);
} else {
- printf("SSL_connect() success.\n");
+ debugprint("SSL_connect() success.\n");
}
- printf("server openssl complete.\n");
+ debugprint("server openssl complete.\n");
} else {
// If not using TLS then just slap the serversockfd into server_ssl by casting it
*server_ssl = (SSL*)(long int)*serversockfd;
@@ -766,7 +769,7 @@ int processircmessage(SSL *server_ssl, char *str, int source, struct client *cli
// Build array of each space-separated token (TODO - Use counter to stop splitting once we reach some reasonable value - i.e. once we're definitely past commands and into just free text)
char tokens[MAXTOKENS][MAXDATASIZE];
- printf(" >> processircmessage(): Processing source %d message \"%s\"...\n", source, str);
+ debugprint(" >> processircmessage(): Processing source %d message \"%s\"...\n", source, str);
// Copy to a temporary string so we still have the original in case it's not processed
char *strcopy = strdup(str);
@@ -778,7 +781,7 @@ int processircmessage(SSL *server_ssl, char *str, int source, struct client *cli
while ((token = strsep(&strcopy, " ")) != NULL) {
if (*token == '\0') continue; // Skip consecutive matches
if (counter >= MAXTOKENS) break; // Too many tokens
- printf(" >> Message Token: \"%s\", length %zd.\n", token, strlen(token));
+ debugprint(" >> Message Token: \"%s\", length %zd.\n", token, strlen(token));
// Copy into the token array (strlen + 1 to get the NULL terminator)
strncpy(tokens[counter], token, strlen(token) + 1);
counter++;
@@ -794,7 +797,7 @@ int processircmessage(SSL *server_ssl, char *str, int source, struct client *cli
// Server PING received? If so, send a PONG back with the next element as the argument.
if (strncmp(tokens[0], "PING", strlen(tokens[0])) == 0) {
- printf("Server PING found and it is: %s with length %zd! Sending response...\n", tokens[0], strlen(tokens[0]));
+ debugprint("Server PING found and it is: %s with length %zd! Sending response...\n", tokens[0], strlen(tokens[0]));
char outgoingmsg[MAXDATASIZE]; // String to send to server
if (!snprintf(outgoingmsg, MAXDATASIZE, "PONG %s", tokens[1])) { // TODO - Make sure tokens[1] actually has a token
@@ -811,16 +814,16 @@ int processircmessage(SSL *server_ssl, char *str, int source, struct client *cli
// Prefix received? TODO - Care about what the prefix is - what if it's a different server/network/whatever?
if (tokens[0][0] == ':') {
- printf("Prefix found: '%s'! Next token is '%s', length %zd.\n", tokens[0], tokens[1], strlen(tokens[1]));
+ debugprint("Prefix found: '%s'! Next token is '%s', length %zd.\n", tokens[0], tokens[1], strlen(tokens[1]));
// Greetings 001 through to 005, store in ircdstrings array for resending when clients connect
// Also store our nick!user@host from greeting 001
// Also store the real IRCd's name from greeting 004
if (strncmp(tokens[1], "001", strlen(tokens[1])) == 0) {
- printf("Found greeting 001 (%s) (length %zd), storing in ircdstrings struct.\n", str, strlen(str));
+ debugprint("Found greeting 001 (%s) (length %zd), storing in ircdstrings struct.\n", str, strlen(str));
strncpy(ircdstrings->greeting001, str, strlen(str));
// Null the end of the string
ircdstrings->greeting001[strlen(str)] = '\0';
- printf("Storing our nick!user@host (:%s) from greeting 001 in ircdstrings struct.\n", tokens[counter - 1]);
+ debugprint("Storing our nick!user@host (:%s) from greeting 001 in ircdstrings struct.\n", tokens[counter - 1]);
// Prepend a colon (:) first since everything (so far) needs one
if (!snprintf(ircdstrings->nickuserhost, MAXDATASIZE, ":%s", tokens[counter - 1])) {
fprintf(stderr, "Error while preparing nickuserhost for storage!\n");
@@ -828,29 +831,29 @@ int processircmessage(SSL *server_ssl, char *str, int source, struct client *cli
}
// Null the end of the new string
ircdstrings->nickuserhost[strlen(tokens[counter - 1]) + 1] = '\0'; // +1 for the inserted colon
- printf("nickuserhost '%s' stored.\n", ircdstrings->nickuserhost);
+ debugprint("nickuserhost '%s' stored.\n", ircdstrings->nickuserhost);
free(strcopyPtr);
return 1;
} else if (strncmp(tokens[1], "002", strlen(tokens[1])) == 0) {
- printf("Found greeting 002 (%s), storing in ircdstrings struct.\n", str);
+ debugprint("Found greeting 002 (%s), storing in ircdstrings struct.\n", str);
strncpy(ircdstrings->greeting002, str, strlen(str));
// Null the end of the string
ircdstrings->greeting002[strlen(str)] = '\0';
free(strcopyPtr);
return 1;
} else if (strncmp(tokens[1], "003", strlen(tokens[1])) == 0) {
- printf("Found greeting 003 (%s), storing in ircdstrings struct.\n", str);
+ debugprint("Found greeting 003 (%s), storing in ircdstrings struct.\n", str);
strncpy(ircdstrings->greeting003, str, strlen(str));
// Null the end of the string
ircdstrings->greeting003[strlen(str)] = '\0';
free(strcopyPtr);
return 1;
} else if (strncmp(tokens[1], "004", strlen(tokens[1])) == 0) {
- printf("Found greeting 004 (%s), storing in ircdstrings struct.\n", str);
+ debugprint("Found greeting 004 (%s), storing in ircdstrings struct.\n", str);
strncpy(ircdstrings->greeting004, str, strlen(str));
// Null the end of the string
ircdstrings->greeting004[strlen(str)] = '\0';
- printf("Storing the real IRCd's name (%s) from greeting 004 in ircdstrings struct.\n", tokens[3]);
+ debugprint("Storing the real IRCd's name (%s) from greeting 004 in ircdstrings struct.\n", tokens[3]);
strncpy(ircdstrings->ircdname, tokens[3], strlen(tokens[3]));
// Null the end of the string
ircdstrings->ircdname[strlen(tokens[3])] = '\0';
@@ -861,7 +864,7 @@ int processircmessage(SSL *server_ssl, char *str, int source, struct client *cli
if (ircdstrings->reconnecting) {
// First tell clients if our nick changed
if (!strcmp(ircdstrings->ircnick, ircdstrings->oldnick) == 0) {
- printf("Telling clients about nick change.\n");
+ debugprint("Telling clients about nick change.\n");
char nickmsg[MAXDATASIZE];
snprintf(nickmsg, MAXDATASIZE, ":%s NICK :%s", ircdstrings->oldnick, ircdstrings->ircnick);
sendtoallclients(clients, nickmsg, sourcefd, settings);
@@ -874,12 +877,12 @@ int processircmessage(SSL *server_ssl, char *str, int source, struct client *cli
for (int i = 0; i < channelcount; i++) {
// Skip this one and increment channelcount if it's a blank channel
if (!channels[i].name[0]) {
- printf("Reconnection: Skipping channel[%d], incrementing channelcount.\n", i);
+ debugprint("Reconnection: Skipping channel[%d], incrementing channelcount.\n", i);
channelcount++;
continue;
}
- printf("Reconnection: Re-joining '%s'.\n", channels[i].name);
+ debugprint("Reconnection: Re-joining '%s'.\n", channels[i].name);
char joinmsg[MAXDATASIZE];
snprintf(joinmsg, MAXDATASIZE, "JOIN %s", channels[i].name);
@@ -907,7 +910,7 @@ int processircmessage(SSL *server_ssl, char *str, int source, struct client *cli
free(strcopyPtr);
return 1;
} else if (strncmp(tokens[1], "005", strlen(tokens[1])) == 0) {
- printf("Found greeting 005 (%s), storing in ircdstrings struct.\n", str);
+ debugprint("Found greeting 005 (%s), storing in ircdstrings struct.\n", str);
// Find an empty greeting005 string in ircdstrings and store in there...
if (!ircdstrings->greeting005a[0]) {
strncpy(ircdstrings->greeting005a, str, strlen(str));
@@ -920,7 +923,7 @@ int processircmessage(SSL *server_ssl, char *str, int source, struct client *cli
ircdstrings->greeting005c[strlen(str)] = '\0';
} else {
// ...or if they are all fill, discard - TODO - Support more than three greeting005 strings!
- printf("Already stored three greeting 005 strings, discarding this one.\n");
+ debugprint("Already stored three greeting 005 strings, discarding this one.\n");
}
free(strcopyPtr);
return 1;
@@ -928,11 +931,11 @@ int processircmessage(SSL *server_ssl, char *str, int source, struct client *cli
// Server JOIN received? Add to our local channel array if it's us, or record the user in the channel if it's not us.
if (strncmp(tokens[1], "JOIN", strlen(tokens[1])) == 0) {
- printf("Server JOIN found and it is: %s with length %zd! Next token is '%s'. Adding to local channel list if it's us.\n", tokens[0], strlen(tokens[0]), tokens[2]);
+ debugprint("Server JOIN found and it is: %s with length %zd! Next token is '%s'. Adding to local channel list if it's us.\n", tokens[0], strlen(tokens[0]), tokens[2]);
// Next token should be the channel name but it probably needs the leading ':' stripping
- printf("processircmessage(): Channel name was '%s'\n", tokens[2]);
+ debugprint("processircmessage(): Channel name was '%s'\n", tokens[2]);
stripprefix(tokens[2]);
- printf("processircmessage(): Channel name now '%s'\n", tokens[2]);
+ debugprint("processircmessage(): Channel name now '%s'\n", tokens[2]);
// If the user JOINing is us, then we must have joined a channel, so add to our local channel array.
// Copy to a temporary string so we still have the original in case we need it
@@ -940,12 +943,12 @@ int processircmessage(SSL *server_ssl, char *str, int source, struct client *cli
// Just get the nick for comparison
extractnickfromprefix(prefixcopy);
if (strncmp(prefixcopy, ircdstrings->ircnick, strlen(tokens[0])) == 0) {
- printf("Server JOIN: nick is ours ('%s' vs '%s').\n", prefixcopy, ircdstrings->ircnick);
+ debugprint("Server JOIN: nick is ours ('%s' vs '%s').\n", prefixcopy, ircdstrings->ircnick);
// TODO - Saner way to initialise this since we don't have the variables yet?
// TODO - Defaulting to type '=' which is "public" since I don't know what else to guess.
createchannel(channels, tokens[2], "TOPIC", "TOPICWHO", "0");
} else {
- printf("Server JOIN: nick is NOT ours ('%s' vs '%s').\n", prefixcopy, ircdstrings->ircnick);
+ debugprint("Server JOIN: nick is NOT ours ('%s' vs '%s').\n", prefixcopy, ircdstrings->ircnick);
}
// And then send to all clients
@@ -968,7 +971,7 @@ int processircmessage(SSL *server_ssl, char *str, int source, struct client *cli
// Server PART received? Remove from our local channel list if it's us.
if (strncmp(tokens[1], "PART", strlen(tokens[1])) == 0) {
- printf("Server PART found and it is: %s with length %zd! Next token is '%s'. Removing from local channel list if it's us.\n", tokens[0], strlen(tokens[0]), tokens[2]);
+ debugprint("Server PART found and it is: %s with length %zd! Next token is '%s'. Removing from local channel list if it's us.\n", tokens[0], strlen(tokens[0]), tokens[2]);
// If the user PARTing is us, then we must have left a channel, so remove it from our local channel array.
// (If it's not us, then it's another user PARTing a channel, so just pass straight through to letting all our clients know.)
@@ -977,10 +980,10 @@ int processircmessage(SSL *server_ssl, char *str, int source, struct client *cli
// Just get the nick for comparison
extractnickfromprefix(prefixcopy);
if (strncmp(prefixcopy, ircdstrings->ircnick, strlen(tokens[0])) == 0) {
- printf("Server PART: nick is ours ('%s' vs '%s').\n", prefixcopy, ircdstrings->ircnick);
+ debugprint("Server PART: nick is ours ('%s' vs '%s').\n", prefixcopy, ircdstrings->ircnick);
removechannel(channels, tokens[2]);
} else {
- printf("Server PART: nick is NOT ours ('%s' vs '%s').\n", prefixcopy, ircdstrings->ircnick);
+ debugprint("Server PART: nick is NOT ours ('%s' vs '%s').\n", prefixcopy, ircdstrings->ircnick);
}
// And then send to all clients
@@ -1010,7 +1013,7 @@ int processircmessage(SSL *server_ssl, char *str, int source, struct client *cli
setchanneltopicwhotime(channels, tokens[3], "", "0");
// Server 332 (RPL_TOPIC) set the channel topic
} else if (strncmp(tokens[1], "332", strlen(tokens[1])) == 0) {
- printf("Server 332 (RPL_TOPIC) found, extracting topic and storing in channel struct.\n");
+ debugprint("Server 332 (RPL_TOPIC) found, extracting topic and storing in channel struct.\n");
// Need to extract the final parameter as topics can have spaces
// Copy to a temporary string so we still have the original in case we need it
char *topiccopy = strdup(str);
@@ -1019,24 +1022,24 @@ int processircmessage(SSL *server_ssl, char *str, int source, struct client *cli
free(topiccopy);
// Server 333 (RPL_TOPICWHOTIME) set the channel topic setter and the time it was set
} else if (strncmp(tokens[1], "333", strlen(tokens[1])) == 0) {
- printf("Server 333 (RPL_TOPICWHOTIME) found, extracting who and when, and storing in channel struct.\n");
+ debugprint("Server 333 (RPL_TOPICWHOTIME) found, extracting who and when, and storing in channel struct.\n");
setchanneltopicwhotime(channels, tokens[3], tokens[4], tokens[5]);
// Server 353 (RPL_NAMREPLY), relay to all clients if we've just JOINed the channel, or relay to any clients
// who were pending RPL_NAMREPLYs if it's an existing channel.
} else if (strncmp(tokens[1], "353", strlen(tokens[1])) == 0) {
// It must be a new channel and we don't have the NAMES
if (!channelgotnames(channels, tokens[4])) {
- printf("Server 353 received for a new channel, sending to all clients.\n");
+ debugprint("Server 353 received for a new channel, sending to all clients.\n");
// Relay to all clients
sendtoallclients(clients, str, sourcefd, settings);
} else {
// We were already in the channel and have the NAMES
- printf("Server 353 received for an existing channel, sending to all clients who were pending RPL_NAMREPLYs.\n");
+ debugprint("Server 353 received for an existing channel, sending to all clients who were pending RPL_NAMREPLYs.\n");
// If any clients were pending RPL_NAMREPLYs, send this on to them
// TODO - Make sure clients only get the ones they were waiting on in case there are multiple conflicting requests going on at once
for (int i = 0; i < MAXCLIENTS; i++) {
if (clients[i].pendingnames > 0) {
- printf("Sending 353 RPL_NAMREPLY for channel '%s' to client with fd '%d' who was pending %d RPL_NAMREPLYs.\n", tokens[4], clients[i].fd, clients[i].pendingnames);
+ debugprint("Sending 353 RPL_NAMREPLY for channel '%s' to client with fd '%d' who was pending %d RPL_NAMREPLYs.\n", tokens[4], clients[i].fd, clients[i].pendingnames);
sendtoclient(clients[i].fd, str, clients, settings, 0);
}
}
@@ -1049,7 +1052,7 @@ int processircmessage(SSL *server_ssl, char *str, int source, struct client *cli
int channelelement;
// It must be a new channel and we don't have the NAMES
if (!(channelelement = channelgotnames(channels, tokens[3]))) {
- printf("Server 366 received for a new channel, sending to all clients and set as got names.\n");
+ debugprint("Server 366 received for a new channel, sending to all clients and set as got names.\n");
// We have the names now!
channels[channelindex(channels, tokens[3])].gotnames = 1;
// Relay to all clients
@@ -1057,14 +1060,14 @@ int processircmessage(SSL *server_ssl, char *str, int source, struct client *cli
} else {
// We were already in the channel and have the NAMES
// TODO - Make sure clients only get the ones they were waiting on in case there are multiple conflicting requests going on at once
- printf("Server 366 received for an existing channel, sending to all clients who were pending RPL_NAMREPLYs and decrementing their pendingnames count.\n");
+ debugprint("Server 366 received for an existing channel, sending to all clients who were pending RPL_NAMREPLYs and decrementing their pendingnames count.\n");
for (int i = 0; i < MAXCLIENTS; i++) {
if (clients[i].pendingnames > 0) {
sendtoclient(clients[i].fd, str, clients, settings, 0);
// And decrement their pendingnames count
- printf("Decrementing pendingnames due 366 RPL_ENDOFNAMES to for channel '%s' to client with fd '%d' who was pending %d RPL_NAMREPLYs.\n", tokens[3], clients[i].fd, clients[i].pendingnames);
+ debugprint("Decrementing pendingnames due 366 RPL_ENDOFNAMES to for channel '%s' to client with fd '%d' who was pending %d RPL_NAMREPLYs.\n", tokens[3], clients[i].fd, clients[i].pendingnames);
clients[i].pendingnames--;
- printf("Client with fd '%d' has '%d' pendingnames left.\n", clients[i].fd, clients[i].pendingnames);
+ debugprint("Client with fd '%d' has '%d' pendingnames left.\n", clients[i].fd, clients[i].pendingnames);
}
}
}
@@ -1074,7 +1077,7 @@ int processircmessage(SSL *server_ssl, char *str, int source, struct client *cli
// Server TOPIC received? Update our local channel topic info then relay to clients.
if (strncmp(tokens[1], "TOPIC", strlen(tokens[1])) == 0) {
- printf("Server TOPIC found and it is: %s with length %zd! Next token is '%s'. Updating our local channel topic info.\n", tokens[0], strlen(tokens[0]), tokens[2]);
+ debugprint("Server TOPIC found and it is: %s with length %zd! Next token is '%s'. Updating our local channel topic info.\n", tokens[0], strlen(tokens[0]), tokens[2]);
// Set the topic itself
@@ -1121,7 +1124,7 @@ int processircmessage(SSL *server_ssl, char *str, int source, struct client *cli
// Server PRIVMSG received? Relay to all clients and write to replay log.
if (strncmp(tokens[1], "PRIVMSG", strlen(tokens[1])) == 0) {
- printf("Server PRIVMSG found and it is: %s with length %zd! Next token is '%s'. Relaying to all clients.\n", tokens[0], strlen(tokens[0]), tokens[2]);
+ debugprint("Server PRIVMSG found and it is: %s with length %zd! Next token is '%s'. Relaying to all clients.\n", tokens[0], strlen(tokens[0]), tokens[2]);
sendtoallclients(clients, str, sourcefd, settings);
@@ -1143,7 +1146,7 @@ int processircmessage(SSL *server_ssl, char *str, int source, struct client *cli
// 1. Find out if it was us and change ircnick and nickuserhost if so
// 2. Either way, relay to all clients
if (strncmp(tokens[1], "NICK", strlen(tokens[1])) == 0) {
- printf("Server NICK found and it is: %s with length %zd! Next token is '%s'. Updating records and relaying to all clients.\n", tokens[0], strlen(tokens[0]), tokens[2]);
+ debugprint("Server NICK found and it is: %s with length %zd! Next token is '%s'. Updating records and relaying to all clients.\n", tokens[0], strlen(tokens[0]), tokens[2]);
// Was it us?
// Copy to a temporary string so we still have the original in case we need it
@@ -1151,12 +1154,12 @@ int processircmessage(SSL *server_ssl, char *str, int source, struct client *cli
// Just get the nick for comparison
extractnickfromprefix(svrprefixcopy);
if (strncmp(ircdstrings->ircnick, svrprefixcopy, strlen(ircdstrings->ircnick)) == 0) {
- printf("Server NICK: nick is ours ('%s' vs '%s').\n", svrprefixcopy, ircdstrings->ircnick);
+ debugprint("Server NICK: nick is ours ('%s' vs '%s').\n", svrprefixcopy, ircdstrings->ircnick);
// Make a copy of the old nickuserhost for updategreetings() below
char *nickuserhostcpy = strdup(ircdstrings->nickuserhost);
// Update nickuserhost with the new :nick!user@host
updatenickuserhost(ircdstrings->nickuserhost, tokens[2]);
- printf("Updated nickuserhost to '%s'.\n", ircdstrings->nickuserhost);
+ debugprint("Updated nickuserhost to '%s'.\n", ircdstrings->nickuserhost);
// Prepare to update ircnick and greetings strings
// Temporary copy of new nickuserhost
char *prefixcopy = strdup(ircdstrings->nickuserhost);
@@ -1166,7 +1169,7 @@ int processircmessage(SSL *server_ssl, char *str, int source, struct client *cli
updategreetings(ircdstrings->greeting001, ircdstrings->greeting002, ircdstrings->greeting003, ircdstrings->greeting004, ircdstrings->greeting005a, ircdstrings->greeting005b, ircdstrings->greeting005c, ircdstrings->nickuserhost, nickuserhostcpy, tokens[2], ircdstrings->ircnick);
// Update our nick
strcpy(ircdstrings->ircnick, prefixcopy);
- printf("Updated ircnick to '%s'.\n", ircdstrings->ircnick);
+ debugprint("Updated ircnick to '%s'.\n", ircdstrings->ircnick);
free(nickuserhostcpy);
free(prefixcopy);
}
@@ -1181,7 +1184,7 @@ int processircmessage(SSL *server_ssl, char *str, int source, struct client *cli
// Server MODE received? See what sort it is and act accordingly.
if (strncmp(tokens[1], "MODE", strlen(tokens[1])) == 0) {
- printf("Server MODE found and it is: %s with length %zd! Next token is '%s'. Analysing...\n", tokens[1], strlen(tokens[1]), tokens[2]);
+ debugprint("Server MODE found and it is: %s with length %zd! Next token is '%s'. Analysing...\n", tokens[1], strlen(tokens[1]), tokens[2]);
// 4 tokens could be either our initial mode or a channel mode (or something else - TODO - what else?)
if (counter == 4) {
@@ -1190,7 +1193,7 @@ int processircmessage(SSL *server_ssl, char *str, int source, struct client *cli
snprintf(comparison, MAXDATASIZE, ":%s MODE %s :", ircdstrings->ircnick, ircdstrings->ircnick);
if (strncmp(str, comparison, strlen(comparison)) == 0) {
// Looks like it!
- printf("Our initial MODE found (%s), storing for later.\n", tokens[3]);
+ debugprint("Our initial MODE found (%s), storing for later.\n", tokens[3]);
// Store in ircdstrings for when clients connect and relay to current clients.
strcpy(ircdstrings->mode, tokens[3]);
@@ -1204,7 +1207,7 @@ int processircmessage(SSL *server_ssl, char *str, int source, struct client *cli
// Might be a channel mode (e.g. ":nick!user@host MODE #channel +s")
if (tokens[2][0] == '#') {
// Looks like it! Tell all clients.
- printf("Channel MODE found (%s %s), telling all clients.\n", tokens[2], tokens[3]);
+ debugprint("Channel MODE found (%s %s), telling all clients.\n", tokens[2], tokens[3]);
sendtoallclients(clients, str, sourcefd, settings);
free(strcopyPtr);
@@ -1221,7 +1224,7 @@ int processircmessage(SSL *server_ssl, char *str, int source, struct client *cli
// Server 324 (RPL_CHANNELMODEIS) received? Send to any clients who requested a channel MODE.
if (strncmp(tokens[1], "324", strlen(tokens[1])) == 0) {
- printf("Server 324 (RPL_CHANNELMODEIS) found and it is: %s with length %zd! Sending to clients who are pending this.\n", tokens[1], strlen(tokens[1]));
+ debugprint("Server 324 (RPL_CHANNELMODEIS) found and it is: %s with length %zd! Sending to clients who are pending this.\n", tokens[1], strlen(tokens[1]));
// Relay to all pending clients
for (int i = 0; i < MAXCLIENTS; i++) {
@@ -1238,7 +1241,7 @@ int processircmessage(SSL *server_ssl, char *str, int source, struct client *cli
// Server 368 (RPL_ENDOFBANLIST) received? Send to any clients who requested a ban MODE query. - TODO - Identify and handle start/middle of ban responses.
if (strncmp(tokens[1], "368", strlen(tokens[1])) == 0) {
- printf("Server 368 (RPL_ENDOFBANLIST) found and it is: %s with length %zd! Sending to clients who are pending this.\n", tokens[1], strlen(tokens[1]));
+ debugprint("Server 368 (RPL_ENDOFBANLIST) found and it is: %s with length %zd! Sending to clients who are pending this.\n", tokens[1], strlen(tokens[1]));
// Relay to all pending clients
for (int i = 0; i < MAXCLIENTS; i++) {
@@ -1255,7 +1258,7 @@ int processircmessage(SSL *server_ssl, char *str, int source, struct client *cli
// Server 329 (RPL_CREATIONTIME), 352 (RPL_WHOREPLY), or 315 (RPL_ENDOFWHO) received? Send to any clients who requested a WHO.
if (strncmp(tokens[1], "329", strlen(tokens[1])) == 0 || strncmp(tokens[1], "352", strlen(tokens[1])) == 0 || strncmp(tokens[1], "315", strlen(tokens[1])) == 0) {
- printf("Server 329 (RPL_CREATIONTIME), 352 (RPL_WHOREPLY), or 315 (RPL_ENDOFWHO) found and it is: %s with length %zd! Sending to clients who are pending one of these.\n", tokens[1], strlen(tokens[1]));
+ debugprint("Server 329 (RPL_CREATIONTIME), 352 (RPL_WHOREPLY), or 315 (RPL_ENDOFWHO) found and it is: %s with length %zd! Sending to clients who are pending one of these.\n", tokens[1], strlen(tokens[1]));
// Relay to all pending clients
for (int i = 0; i < MAXCLIENTS; i++) {
@@ -1274,7 +1277,7 @@ int processircmessage(SSL *server_ssl, char *str, int source, struct client *cli
// Server 321 (RPL_LISTSTART), 322 (RPL_LIST), or 323 (RPL_LISTEND) received? Send to any clients who requested a WHO.
if (strncmp(tokens[1], "321", strlen(tokens[1])) == 0 || strncmp(tokens[1], "322", strlen(tokens[1])) == 0 || strncmp(tokens[1], "323", strlen(tokens[1])) == 0) {
- printf("Server 321 (RPL_LISTSTART), 322 (RPL_LIST), or 323 (RPL_LISTEND) found and it is: %s with length %zd! Sending to clients who are pending one of these.\n", tokens[1], strlen(tokens[1]));
+ debugprint("Server 321 (RPL_LISTSTART), 322 (RPL_LIST), or 323 (RPL_LISTEND) found and it is: %s with length %zd! Sending to clients who are pending one of these.\n", tokens[1], strlen(tokens[1]));
// Relay to all pending clients
for (int i = 0; i < MAXCLIENTS; i++) {
@@ -1299,7 +1302,7 @@ int processircmessage(SSL *server_ssl, char *str, int source, struct client *cli
strncmp(tokens[1], "317", strlen(tokens[1])) == 0 || strncmp(tokens[1], "319", strlen(tokens[1])) == 0 ||
strncmp(tokens[1], "320", strlen(tokens[1])) == 0 || strncmp(tokens[1], "671", strlen(tokens[1])) == 0 ||
strncmp(tokens[1], "318", strlen(tokens[1])) == 0) {
- printf("Server 307 RPL_SUSERHOST, 311 RPL_WHOISUSER, 312 RPL_WHOISSERVER, 313 (RPL_WHOISOPERATOR), 317 RPL_WHOISIDLE, "
+ debugprint("Server 307 RPL_SUSERHOST, 311 RPL_WHOISUSER, 312 RPL_WHOISSERVER, 313 (RPL_WHOISOPERATOR), 317 RPL_WHOISIDLE, "
"319 RPL_WHOISCHANNELS, 320 (RPL_WHOISSPECIAL), 671 (RPL_WHOISSECURE), or 318 RPL_ENDOFWHOIS found and it is: "
"%s with length %zd! Sending to clients who are pending one of these.\n", tokens[1], strlen(tokens[1]));
@@ -1321,7 +1324,7 @@ int processircmessage(SSL *server_ssl, char *str, int source, struct client *cli
// Server 314 (RPL_WHOWASUSER), 406 (ERR_WASNOSUCHNICK), or 369 (RPL_ENDOFWHOWAS) received?
// Send to any clients who requested a WHOWAS.
if (strncmp(tokens[1], "314", strlen(tokens[1])) == 0 || strncmp(tokens[1], "406", strlen(tokens[1])) == 0 || strncmp(tokens[1], "369", strlen(tokens[1])) == 0) {
- printf("314 (RPL_WHOWASUSER), 406 (ERR_WASNOSUCHNICK), or 369 (RPL_ENDOFWHOWAS) "
+ debugprint("314 (RPL_WHOWASUSER), 406 (ERR_WASNOSUCHNICK), or 369 (RPL_ENDOFWHOWAS) "
"found and it is: %s with length %zd! Sending to clients who are pending one of these.\n", tokens[1], strlen(tokens[1]));
// Relay to all pending clients
@@ -1341,7 +1344,7 @@ int processircmessage(SSL *server_ssl, char *str, int source, struct client *cli
// Server 312 (RPL_WHOISSERVER) received? Check to see if anyone was pending a WHOIS or a WHOWAS and send to them, if not send to everyone.
if (strncmp(tokens[1], "312", strlen(tokens[1])) == 0) {
- printf("Server 312 (RPL_WHOISSERVER) found and it is: %s with length %zd! Sending to clients who are pending this or to everyone if nobody is.\n", tokens[1], strlen(tokens[1]));
+ debugprint("Server 312 (RPL_WHOISSERVER) found and it is: %s with length %zd! Sending to clients who are pending this or to everyone if nobody is.\n", tokens[1], strlen(tokens[1]));
int waspending = 0;
@@ -1367,7 +1370,7 @@ int processircmessage(SSL *server_ssl, char *str, int source, struct client *cli
// Server 401 (ERR_NOSUCHNICK) received? Check to see if anyone was pending a WHOIS and send to them,
// if not send to everyone (401 was probably in reply to something else like a PRIVMSG).
if (strncmp(tokens[1], "401", strlen(tokens[1])) == 0) {
- printf("Server 401 (ERR_NOSUCHNICK) found and it is: %s with length %zd! Sending to clients who are pending this or to everyone if nobody is.\n", tokens[1], strlen(tokens[1]));
+ debugprint("Server 401 (ERR_NOSUCHNICK) found and it is: %s with length %zd! Sending to clients who are pending this or to everyone if nobody is.\n", tokens[1], strlen(tokens[1]));
int waspending = 0;
@@ -1392,7 +1395,7 @@ int processircmessage(SSL *server_ssl, char *str, int source, struct client *cli
// Server 432 (ERR_ERRONEUSNICKNAME) or 433 (ERR_NICKNAMEINUSE) received? See which nick we're on and try another.
// (But only if we're not already registered with the real IRC server.)
if ((strncmp(tokens[1], "432", strlen(tokens[1])) == 0 || strncmp(tokens[1], "433", strlen(tokens[1])) == 0) && !strlen(ircdstrings->greeting004)) {
- printf("Server 432 (ERR_ERRONEUSNICKNAME) or 433 (ERR_NICKNAMEINUSE) found and it is: %s with length %zd! Trying another nick...\n", tokens[1], strlen(tokens[1]));
+ debugprint("Server 432 (ERR_ERRONEUSNICKNAME) or 433 (ERR_NICKNAMEINUSE) found and it is: %s with length %zd! Trying another nick...\n", tokens[1], strlen(tokens[1]));
// Try to get nick2 and nick3 from the configuration file
char nick2[MAXNICKLENGTH];
@@ -1413,12 +1416,12 @@ int processircmessage(SSL *server_ssl, char *str, int source, struct client *cli
// Has nick2 already been tried?
} else if (strncmp(ircdstrings->ircnick, nick2, strlen(settings->ircnick)) == 0) {
// Then try nick3
- printf("Trying nick3, nick2 was already tried.\n");
+ debugprint("Trying nick3, nick2 was already tried.\n");
strcpy(ircdstrings->ircnick, nick3);
// Have neither been tried?
} else {
// Then try nick2
- printf("Trying nick2, nick3 is also configured.\n");
+ debugprint("Trying nick2, nick3 is also configured.\n");
strcpy(ircdstrings->ircnick, nick2);
}
// Do we only have a nick2? (And not tried autonick yet.)
@@ -1429,7 +1432,7 @@ int processircmessage(SSL *server_ssl, char *str, int source, struct client *cli
tryautonick(ircdstrings);
} else {
// Then try it
- printf("Trying nick2, nick3 is not configured.\n");
+ debugprint("Trying nick2, nick3 is not configured.\n");
strcpy(ircdstrings->ircnick, nick2);
}
// Do we have neither? (Or have already started autonick.)
@@ -1453,7 +1456,7 @@ int processircmessage(SSL *server_ssl, char *str, int source, struct client *cli
// Server CAP received?
if (strncmp(tokens[1], "CAP", strlen(tokens[1])) == 0) {
- printf("Server CAP found and it is: %s with length %zd! Analysing...\n", tokens[1], strlen(tokens[1]));
+ debugprint("Server CAP found and it is: %s with length %zd! Analysing...\n", tokens[1], strlen(tokens[1]));
// If the server said "CAP <ournick> ACK :multi-prefix" then it must have approved our CAP multi-prefix request
if (counter == 5) {
if (strncmp(tokens[2], ircdstrings->ircnick, strlen(tokens[2])) == 0 &&
@@ -1463,7 +1466,7 @@ int processircmessage(SSL *server_ssl, char *str, int source, struct client *cli
}
}
// We didn't handle it
- printf("Unhandled server CAP response.\n");
+ debugprint("Unhandled server CAP response.\n");
}
}
@@ -1474,13 +1477,13 @@ int processircmessage(SSL *server_ssl, char *str, int source, struct client *cli
// PASS received? User is trying to log in, check their password.
if (strncasecmp(tokens[0], "PASS", strlen(tokens[0])) == 0) {
if (checkpassword(tokens[1], settings->conffile)) {
- printf("Password accepted! Setting fd %d to authenticated.\n", sourcefd);
+ debugprint("Password accepted! Setting fd %d to authenticated.\n", sourcefd);
// Find the client in the clients array and set them as authenticated
for (int i = 0; i < MAXCLIENTS; i++) {
if (clients[i].fd == sourcefd) {
// Found client in array, set to authenticated
clients[i].authed = 1;
- printf("Found and authenticated fd in arr_authed.\n");
+ debugprint("Found and authenticated fd in arr_authed.\n");
// Alert other clients about the successful authentication
char alertmsg[MAXDATASIZE];
if (!snprintf(alertmsg, MAXDATASIZE, "NOTICE %s :blabouncer: new client with fd %d has successfully authenticated.", ircdstrings->ircnick, sourcefd)) {
@@ -1492,7 +1495,7 @@ int processircmessage(SSL *server_ssl, char *str, int source, struct client *cli
}
}
} else {
- printf("Password rejected, disconnecting fd %d.\n", sourcefd);
+ debugprint("Password rejected, disconnecting fd %d.\n", sourcefd);
disconnectclient(sourcefd, clients, ircdstrings, settings);
// Alert other clients about the failed authentication
char alertmsg[MAXDATASIZE];
@@ -1512,9 +1515,9 @@ int processircmessage(SSL *server_ssl, char *str, int source, struct client *cli
if (strncasecmp(tokens[0], "CAP", strlen(tokens[0])) == 0) {
// But only do something if the real server told us it had a CAP (only multi-prefix for now)
if (ircdstrings->capmultiprefix == 1) {
- printf("Client CAP received and the server supports CAPs, continuing.\n");
+ debugprint("Client CAP received and the server supports CAPs, continuing.\n");
} else {
- printf("Client CAP received but the server doesn't support CAPs, returning.\n");
+ debugprint("Client CAP received but the server doesn't support CAPs, returning.\n");
free(strcopyPtr);
return 1;
}
@@ -1554,7 +1557,7 @@ int processircmessage(SSL *server_ssl, char *str, int source, struct client *cli
// We're past PASS in the list of possible commands, so ignore
// anything else the client says if they are not authenticated yet.
if (!clients[arrindex(clients, sourcefd)].authed) {
- printf("Ignoring client command '%s' from sourcefd '%d' as not authenticated yet.\n", tokens[0], sourcefd);
+ debugprint("Ignoring client command '%s' from sourcefd '%d' as not authenticated yet.\n", tokens[0], sourcefd);
free(strcopyPtr);
return 1;
}
@@ -1613,15 +1616,15 @@ int processircmessage(SSL *server_ssl, char *str, int source, struct client *cli
// Storing separately so we can skip over blank channels.
int channelcount = getchannelcount(channels);
// Set the client as pending RPL_NAMREPLYs for 'channelcount' channels
- printf("Setting pendingnames to '%d' for client with fd '%d'.\n", channelcount, sourcefd);
+ debugprint("Setting pendingnames to '%d' for client with fd '%d'.\n", channelcount, sourcefd);
clients[arrindex(clients, sourcefd)].pendingnames = channelcount;
// Get client to join channels, and tell client about those channels
for (int i = 0; i < channelcount; i++) {
- printf("JOINing channel[%d] out of %d.\n", i, channelcount);
+ debugprint("JOINing channel[%d] out of %d.\n", i, channelcount);
// Skip this one and increment channelcount if it's a blank channel
if (!channels[i].name[0]) {
- printf("Skipping channel[%d], incrementing channelcount.\n", i);
+ debugprint("Skipping channel[%d], incrementing channelcount.\n", i);
channelcount++;
continue;
}
@@ -1690,14 +1693,14 @@ int processircmessage(SSL *server_ssl, char *str, int source, struct client *cli
// Pretty much ignore anything else the client says if it's not registered yet,
// as the first thing we want to hear is either PASS or USER
if (!clients[arrindex(clients, sourcefd)].registered) {
- printf("Ignoring client command '%s' from sourcefd '%d' as not registered yet.\n", tokens[0], sourcefd);
+ debugprint("Ignoring client command '%s' from sourcefd '%d' as not registered yet.\n", tokens[0], sourcefd);
free(strcopyPtr);
return 1;
}
// Client PING received? If so, send a PONG back with the next element as the argument.
if (strncasecmp(tokens[0], "PING", strlen(tokens[0])) == 0) {
- printf("Client PING found and it is: %s with length %zd! Sending response...\n", tokens[0], strlen(tokens[0]));
+ debugprint("Client PING found and it is: %s with length %zd! Sending response...\n", tokens[0], strlen(tokens[0]));
char outgoingmsg[MAXDATASIZE]; // String to send to client
if (!snprintf(outgoingmsg, MAXDATASIZE, "PONG %s", tokens[1])) { // TODO - Make sure tokens[1] actually has a token
@@ -1713,14 +1716,14 @@ int processircmessage(SSL *server_ssl, char *str, int source, struct client *cli
// TODO - Ignoring CAP for now so as not to confuse other clients, but we should probably query the server then relay the response to the client
if (strncasecmp(tokens[0], "CAP", strlen(tokens[0])) == 0) {
- printf("Client CAP found and it is: %s with length %zd! Ignoring completely for now - TODO - do something useful!\n", tokens[0], strlen(tokens[0]));
+ debugprint("Client CAP found and it is: %s with length %zd! Ignoring completely for now - TODO - do something useful!\n", tokens[0], strlen(tokens[0]));
free(strcopyPtr);
return 1;
}
// Just send NICK to server and let it change all clients' nicks if needed
if (strncasecmp(tokens[0], "NICK", strlen(tokens[0])) == 0) {
- printf("Client NICK found and it is: %s with length %zd! Sending to server...\n", tokens[0], strlen(tokens[0]));
+ debugprint("Client NICK found and it is: %s with length %zd! Sending to server...\n", tokens[0], strlen(tokens[0]));
sendtoserver(server_ssl, str, strlen(str), sourcefd, clients, settings);
free(strcopyPtr);
return 1;
@@ -1728,13 +1731,13 @@ int processircmessage(SSL *server_ssl, char *str, int source, struct client *cli
// If PRIVMSG received, send to server, but also reformat and send to all other clients and log to replay file.
if (strncasecmp(tokens[0], "PRIVMSG", strlen(tokens[0])) == 0) {
- printf("Client PRIVMSG found and it is: %s with length %zd! Sending to server then back to other clients...\n", tokens[0], strlen(tokens[0]));
+ debugprint("Client PRIVMSG found and it is: %s with length %zd! Sending to server then back to other clients...\n", tokens[0], strlen(tokens[0]));
// Send original request straight to server
sendtoserver(server_ssl, str, strlen(str), sourcefd, clients, settings);
// If it seems to be a CTCP VERSION request, just send to the server (CTCP requests are delimited with \1)
if (counter == 3 && strncmp(tokens[2], ":\1VERSION\1", strlen(tokens[2])) == 0) {
- printf("Client PRIVMSG looked like a CTCP VERSION request, so not sending to other clients.\n");
+ debugprint("Client PRIVMSG looked like a CTCP VERSION request, so not sending to other clients.\n");
free(strcopyPtr);
return 1;
}
@@ -1765,7 +1768,7 @@ int processircmessage(SSL *server_ssl, char *str, int source, struct client *cli
// Just send JOIN to server and let it talk back to clients as required
if (strncasecmp(tokens[0], "JOIN", strlen(tokens[0])) == 0) {
- printf("Client JOIN found and it is: %s with length %zd! Sending to server...\n", tokens[0], strlen(tokens[0]));
+ debugprint("Client JOIN found and it is: %s with length %zd! Sending to server...\n", tokens[0], strlen(tokens[0]));
sendtoserver(server_ssl, str, strlen(str), sourcefd, clients, settings);
free(strcopyPtr);
return 1;
@@ -1774,7 +1777,7 @@ int processircmessage(SSL *server_ssl, char *str, int source, struct client *cli
// Don't do anything with QUIT, just let the client go - TODO: Let another clients know with a NOTICE or something
// A client has QUIT, so disconnect (close) them and don't do anything else for now - TODO: Let another clients know with a NOTICE or something
if (strncasecmp(tokens[0], "QUIT", strlen(tokens[0])) == 0) {
- printf("Client QUIT found from fd %d and it is: %s with length %zd! Disconnecting that fd.\n", sourcefd, tokens[0], strlen(tokens[0]));
+ debugprint("Client QUIT found from fd %d and it is: %s with length %zd! Disconnecting that fd.\n", sourcefd, tokens[0], strlen(tokens[0]));
disconnectclient(sourcefd, clients, ircdstrings, settings);
free(strcopyPtr);
return 1;
@@ -1782,7 +1785,7 @@ int processircmessage(SSL *server_ssl, char *str, int source, struct client *cli
// Just send PART to server and let it talk back to clients as required
if (strncasecmp(tokens[0], "PART", strlen(tokens[0])) == 0) {
- printf("Client PART found and it is: %s with length %zd! Sending to server...\n", tokens[0], strlen(tokens[0]));
+ debugprint("Client PART found and it is: %s with length %zd! Sending to server...\n", tokens[0], strlen(tokens[0]));
sendtoserver(server_ssl, str, strlen(str), sourcefd, clients, settings);
free(strcopyPtr);
return 1;
@@ -1790,7 +1793,7 @@ int processircmessage(SSL *server_ssl, char *str, int source, struct client *cli
// Just send TOPIC to server and let it talk back to clients as required
if (strncasecmp(tokens[0], "TOPIC", strlen(tokens[0])) == 0) {
- printf("Client TOPIC found and it is: %s with length %zd! Sending to server...\n", tokens[0], strlen(tokens[0]));
+ debugprint("Client TOPIC found and it is: %s with length %zd! Sending to server...\n", tokens[0], strlen(tokens[0]));
sendtoserver(server_ssl, str, strlen(str), sourcefd, clients, settings);
free(strcopyPtr);
return 1;
@@ -1799,14 +1802,14 @@ int processircmessage(SSL *server_ssl, char *str, int source, struct client *cli
// Take note of what sort of MODE was requested, mark the client as waiting for the reply (so not all clients get it)
// Send it to the server either way and let it talk back to the requesting client as required
if (strncasecmp(tokens[0], "MODE", strlen(tokens[0])) == 0) {
- printf("Client MODE found and it is: %s with length %zd! Analysing...\n", tokens[0], strlen(tokens[0]));
+ debugprint("Client MODE found and it is: %s with length %zd! Analysing...\n", tokens[0], strlen(tokens[0]));
// Is it a ban MODE request (MODE #channel b)?
if (counter >= 3 && strncmp(tokens[2], "b", strlen("b")) == 0) {
- printf("Ban MODE request received, marking as pending.\n");
+ debugprint("Ban MODE request received, marking as pending.\n");
clients[arrindex(clients, sourcefd)].pendingban = 1;
} else if (counter == 2) {
// Assume a normal channel mode request (MODE #channel) - TODO - What if it isn't!?
- printf("Assuming channel MODE request received, marking as pending.\n");
+ debugprint("Assuming channel MODE request received, marking as pending.\n");
clients[arrindex(clients, sourcefd)].pendingchannelmode = 1;
}
@@ -1818,7 +1821,7 @@ int processircmessage(SSL *server_ssl, char *str, int source, struct client *cli
// WHO requested, mark the client as waiting for the reply (so not all clients get it) and send it on the server
if (strncasecmp(tokens[0], "WHO", strlen(tokens[0])) == 0) {
- printf("Client WHO found and it is: %s with length %zd! Marking as pending.\n", tokens[0], strlen(tokens[0]));
+ debugprint("Client WHO found and it is: %s with length %zd! Marking as pending.\n", tokens[0], strlen(tokens[0]));
clients[arrindex(clients, sourcefd)].pendingwho = 1;
// Either way, send it on the server
@@ -1829,7 +1832,7 @@ int processircmessage(SSL *server_ssl, char *str, int source, struct client *cli
// LIST requested, mark the client as waiting for the reply (so not all clients get it) and send it on the server
if (strncasecmp(tokens[0], "LIST", strlen(tokens[0])) == 0) {
- printf("Client LIST found and it is: %s with length %zd! Marking as pending.\n", tokens[0], strlen(tokens[0]));
+ debugprint("Client LIST found and it is: %s with length %zd! Marking as pending.\n", tokens[0], strlen(tokens[0]));
clients[arrindex(clients, sourcefd)].pendinglist = 1;
// Either way, send it on the server
@@ -1840,7 +1843,7 @@ int processircmessage(SSL *server_ssl, char *str, int source, struct client *cli
// Client WHOIS received, send straight on to server and mark the client as pending the response
if (strncasecmp(tokens[0], "WHOIS", strlen(tokens[0])) == 0) {
- printf("Client WHOIS found and it is: %s with length %zd! Sending to server and setting client as pending.\n", tokens[0], strlen(tokens[0]));
+ debugprint("Client WHOIS found and it is: %s with length %zd! Sending to server and setting client as pending.\n", tokens[0], strlen(tokens[0]));
clients[arrindex(clients, sourcefd)].pendingwhois = 1;
sendtoserver(server_ssl, str, strlen(str), sourcefd, clients, settings);
free(strcopyPtr);
@@ -1849,7 +1852,7 @@ int processircmessage(SSL *server_ssl, char *str, int source, struct client *cli
// Client WHOWAS received, send straight on to server and mark the client as pending the response
if (strncasecmp(tokens[0], "WHOWAS", strlen(tokens[0])) == 0) {
- printf("Client WHOWAS found and it is: %s with length %zd! Sending to server and setting client as pending.\n", tokens[0], strlen(tokens[0]));
+ debugprint("Client WHOWAS found and it is: %s with length %zd! Sending to server and setting client as pending.\n", tokens[0], strlen(tokens[0]));
clients[arrindex(clients, sourcefd)].pendingwhowas = 1;
sendtoserver(server_ssl, str, strlen(str), sourcefd, clients, settings);
free(strcopyPtr);
@@ -1860,7 +1863,7 @@ int processircmessage(SSL *server_ssl, char *str, int source, struct client *cli
if (strncasecmp(tokens[0], "NOTICE", strlen(tokens[0])) == 0) {
// If it's a CTCP VERSION response then only send to the server (CTCP requests are delimited with \1)
if (counter >= 3 && strncmp(tokens[2], ":\1VERSION", strlen(tokens[2])) == 0) {
- printf("Client NOTICE looked like a CTCP VERSION response, so just sending to the server.\n");
+ debugprint("Client NOTICE looked like a CTCP VERSION response, so just sending to the server.\n");
sendtoserver(server_ssl, str, strlen(str), sourcefd, clients, settings);
free(strcopyPtr);
return 1;
@@ -1873,10 +1876,10 @@ int processircmessage(SSL *server_ssl, char *str, int source, struct client *cli
// Case insensitive comparisons here since clients won't be recognising and uppercasing these commands
if (strncasecmp(tokens[0], "BLABOUNCER", strlen(tokens[0])) == 0) {
char outgoingmsg[MAXDATASIZE];
- printf("Client BLABOUNCER found and it is: %s with length %zd!\n", tokens[1], strlen(tokens[1]));
+ debugprint("Client BLABOUNCER found and it is: %s with length %zd!\n", tokens[1], strlen(tokens[1]));
// REPLAY received, send the requested length of replay time to the client
if (strncasecmp(tokens[1], "REPLAY", strlen("REPLAY")) == 0) {
- printf("Client BLABOUNCER REPLAY (custom blabouncer command) found and it is: %s with length %zd!\n", tokens[1], strlen(tokens[1]));
+ debugprint("Client BLABOUNCER REPLAY (custom blabouncer command) found and it is: %s with length %zd!\n", tokens[1], strlen(tokens[1]));
// Split the request into days:hours:minutes:seconds
@@ -1894,7 +1897,7 @@ int processircmessage(SSL *server_ssl, char *str, int source, struct client *cli
while ((timetoken = strsep(&timestrcopy, ":")) != NULL) {
if (*timetoken == '\0') continue; // Skip consecutive matches
if (timecounter >= MAXTOKENS) break; // Too many tokens
- printf("Time token: \"%s\", length %zd.\n", timetoken, strlen(timetoken));
+ debugprint("Time token: \"%s\", length %zd.\n", timetoken, strlen(timetoken));
// Copy into the token array (strlen + 1 to get the NULL terminator)
strncpy(timetokens[timecounter], timetoken, strlen(timetoken) + 1);
timecounter++;
@@ -1902,7 +1905,7 @@ int processircmessage(SSL *server_ssl, char *str, int source, struct client *cli
// Make sure we don't have more than four (d:h:m:s) components
if (timecounter > 4) {
- printf("Too many time components requested by REPLAY command. Telling client.\n");
+ debugprint("Too many time components requested by REPLAY command. Telling client.\n");
snprintf(outgoingmsg, MAXDATASIZE, "NOTICE %s :Too many time components requestd by REPLAY command. Expected up to four (days:hours:minutes:seconds).", ircdstrings->ircnick);
sendtoclient(sourcefd, outgoingmsg, clients, settings, 0);
free(timestrcopyPtr);
@@ -1914,7 +1917,7 @@ int processircmessage(SSL *server_ssl, char *str, int source, struct client *cli
for (int i = 0; i < timecounter; i++) {
int check;
if ((check = strtol(timetokens[i], NULL, 10)) == 0) {
- printf("Invalid number '%s' requested by REPLAY command. Telling client.\n", timetokens[i]);
+ debugprint("Invalid number '%s' requested by REPLAY command. Telling client.\n", timetokens[i]);
if (!snprintf(outgoingmsg, MAXDATASIZE, "NOTICE %s :Invalid number '%s' requested by REPLAY command.", ircdstrings->ircnick, timetokens[i])) {
fprintf(stderr, "Error while preparing REPLAY invalid number response!\n");
exit(1);
@@ -1952,7 +1955,7 @@ int processircmessage(SSL *server_ssl, char *str, int source, struct client *cli
replayseconds += strtol(timetokens[0], NULL, 10);
}
- printf("Replaying '%s' which is '%d' seconds.\n", tokens[2], replayseconds);
+ debugprint("Replaying '%s' which is '%d' seconds.\n", tokens[2], replayseconds);
doreplay(sourcefd, replayseconds, clients, settings, ircdstrings, channels);
free(timestrcopyPtr);
@@ -1960,7 +1963,7 @@ int processircmessage(SSL *server_ssl, char *str, int source, struct client *cli
return 1;
// Unrecognised BLABOUNCER command received, send some help instructions
} else {
- printf("Client BLABOUNCER unrecognised command found and it is: %s with length %zd! Sending a help message.\n", tokens[1], strlen(tokens[1]));
+ debugprint("Client BLABOUNCER unrecognised command found and it is: %s with length %zd! Sending a help message.\n", tokens[1], strlen(tokens[1]));
snprintf(outgoingmsg, MAXDATASIZE, "NOTICE %s :Unrecognised BLABOUNCER command received. Valid commands are:", ircdstrings->ircnick);
sendtoclient(sourcefd, outgoingmsg, clients, settings, 0);
snprintf(outgoingmsg, MAXDATASIZE, "NOTICE %s :\"BLABOUNCER REPLAY [[[[days:]hours:]minutes:]seconds]\" (To replay a given length of time of replay log.)", ircdstrings->ircnick);
@@ -1978,7 +1981,7 @@ int processircmessage(SSL *server_ssl, char *str, int source, struct client *cli
// =============================================>
- printf("Done with processircmessage()\n");
+ debugprint("Done with processircmessage()\n");
// If we got here then we didn't process anything
free(strcopyPtr);
@@ -2006,7 +2009,7 @@ int processrawstring(SSL *server_ssl, char *str, int source, struct client *clie
// Keep track of initial pointer for free()ing later
char *strcopyPtr = strcopy;
- printf("processrawstring(): Source type %d sent: \"%s\". Processing it...\n", source, strcopy);
+ debugprint("processrawstring(): Source type %d sent: \"%s\". Processing it...\n", source, strcopy);
// Track which CLRF-separated message within this string we're on
int messagecount = 0;
@@ -2020,7 +2023,7 @@ int processrawstring(SSL *server_ssl, char *str, int source, struct client *clie
while ((token = strsep(&strcopy, "\r\n")) != NULL) {
if (*token == '\0') continue; // Skip consecutive matches
if (messagecount >= MAXTOKENS) break; // Too many tokens
- printf("String Token: \"%s\", length %zd.\n", token, strlen(token));
+ debugprint("String Token: \"%s\", length %zd.\n", token, strlen(token));
// Copy into the token array (strlen + 1 to get the NULL terminator)
strncpy(messages[messagecount], token, strlen(token) + 1);
messagecount++;
@@ -2033,10 +2036,10 @@ int processrawstring(SSL *server_ssl, char *str, int source, struct client *clie
if (ircdstrings->currentmsg[0] && source == SOURCE_SERVER) {
// Make a copy since we can't have source and destination the same with snprintf
char *strtmp = strdup(messages[0]);
- printf("processrawstring(): Previous truncated message detected, combining old '%s' with new '%s'...\n", ircdstrings->currentmsg, strtmp);
+ debugprint("processrawstring(): Previous truncated message detected, combining old '%s' with new '%s'...\n", ircdstrings->currentmsg, strtmp);
snprintf(messages[0], strlen(ircdstrings->currentmsg) + strlen(strtmp) + 1, "%s%s", ircdstrings->currentmsg, strtmp);
messages[0][strlen(ircdstrings->currentmsg) + strlen(strtmp)] = '\0'; // Make sure it's null terminated
- printf("...into new string '%s' and clearing currentmsg holding area.\n", messages[0]);
+ debugprint("...into new string '%s' and clearing currentmsg holding area.\n", messages[0]);
ircdstrings->currentmsg[0] = '\0';
free(strtmp);
}
@@ -2045,7 +2048,7 @@ int processrawstring(SSL *server_ssl, char *str, int source, struct client *clie
// Copy to a holding area for continuation next time
// (Only if source was the server since we always strip \r\n from client messages when recving - TODO - Should we be doing that?
if ((str[strlen(str)-2] != 13 || str[strlen(str)-1] != 10) && source == SOURCE_SERVER) {
- printf("processrawstring(): Truncated message detected, storing final token '%s' for later.\n", messages[messagecount - 1]);
+ debugprint("processrawstring(): Truncated message detected, storing final token '%s' for later.\n", messages[messagecount - 1]);
strncpy(ircdstrings->currentmsg, messages[messagecount - 1], strlen(messages[messagecount - 1]));
ircdstrings->currentmsg[strlen(messages[messagecount - 1])] = '\0';
// Remove it from the message count so it's not processed time time
@@ -2060,7 +2063,7 @@ int processrawstring(SSL *server_ssl, char *str, int source, struct client *clie
// Copy to a temporary string so we still have the original in case it's not processed
char *messagecopy = strdup(messages[i]);
if (processircmessage(server_ssl, messagecopy, source, clients, sourcefd, ircdstrings, channels, settings)) {
- printf("Message processed: \"%s\", NULLing...\n", messages[i]);
+ debugprint("Message processed: \"%s\", NULLing...\n", messages[i]);
messages[i][0] = '\0';
}
free(messagecopy);
@@ -2069,20 +2072,20 @@ int processrawstring(SSL *server_ssl, char *str, int source, struct client *clie
// Deal with any unprocessed messages (send on to server/client(s) since we haven't dealt with them)
for (int i = 0; i < messagecount; i++) {
if (messages[i][0] != '\0') {
- printf("Message %d (\"%s\") remains unprocessed...\n", i, messages[i]);
+ debugprint("Message %d (\"%s\") remains unprocessed...\n", i, messages[i]);
switch(source) {
case SOURCE_SERVER: // If message(s) were from the real IRC server
// Relay/send to all clients ("except" = 0 because this should send to all clients)
// TODO - Is this really going to send the original string if we have messed it with it in processrawstring() and friends!?
- printf("bouncer-server: sending unprocessed server message \"%s\" to all clients, length %zd.\n", messages[i], strlen(messages[i]));
+ debugprint("bouncer-server: sending unprocessed server message \"%s\" to all clients, length %zd.\n", messages[i], strlen(messages[i]));
sendtoallclients(clients, messages[i], EXCEPT_NONE, settings);
break;
case SOURCE_CLIENT: // If message(s) were from a real IRC client
// Send to server
- printf("bouncer-client: sending unprocessed client message \"%s\" to the server, length %zd.\n", messages[i], strlen(messages[i]));
+ debugprint("bouncer-client: sending unprocessed client message \"%s\" to the server, length %zd.\n", messages[i], strlen(messages[i]));
sendtoserver(server_ssl, messages[i], strlen(messages[i]), sourcefd, clients, settings);
- printf("bouncer-client: sending unprocessed client message \"%s\" to all other clients, length %zd.\n", messages[i], strlen(messages[i]));
+ debugprint("bouncer-client: sending unprocessed client message \"%s\" to all other clients, length %zd.\n", messages[i], strlen(messages[i]));
// send the same thing to all *other* clients (all except for source fd)
sendtoallclients(clients, messages[i], sourcefd, settings);
break;
@@ -2093,7 +2096,7 @@ int processrawstring(SSL *server_ssl, char *str, int source, struct client *clie
}
}
- printf("Done with processrawstring()\n");
+ debugprint("Done with processrawstring()\n");
return 1;
}
@@ -2169,7 +2172,7 @@ void dochat(int *serversockfd, int *clientsockfd, struct settings *settings) {
}
while (1) {
- printf("top of loop, fdmax %d.\n", fdmax);
+ debugprint("top of loop, fdmax %d.\n", fdmax);
FD_ZERO(&rfds); // clear entries from fd set
FD_SET(STDIN, &rfds); // add STDIN (fd 0) to read fds to monitor
@@ -2181,7 +2184,7 @@ void dochat(int *serversockfd, int *clientsockfd, struct settings *settings) {
// TODO - now that only connected clients are monitored, perhaps tracking using both fdmax and num_client loops is unnecessary?
for (int i = 0; i < MAXCLIENTS; i++) {
if (clients[i].fd > 0) {
- printf("monitoring fd %d.\n", clients[i].fd);
+ debugprint("monitoring fd %d.\n", clients[i].fd);
FD_SET(clients[i].fd, &rfds);
}
}
@@ -2213,7 +2216,7 @@ void dochat(int *serversockfd, int *clientsockfd, struct settings *settings) {
continue;
}
- printf("select()ing...\n");
+ debugprint("select()ing...\n");
// check to see if anything in the fd_set is waiting - waits here until one of the fds in the set does something
if (select(fdmax + 1, &rfds, NULL, NULL, NULL) < 0) { // network socket + 1, rfds, no writes, no exceptions/errors, no timeout
printf("receive error, exiting!?\n");
@@ -2225,7 +2228,7 @@ void dochat(int *serversockfd, int *clientsockfd, struct settings *settings) {
// See if there's anything to read from the server side (the real IRCd)
if (FD_ISSET(*serversockfd, &rfds)) {
- printf("reading server socket!\n");
+ debugprint("reading server socket!\n");
if ((servernumbytes = sockread(server_ssl, serverbuf, MAXRCVSIZE - 1, settings->servertls)) == -1) {
printf("receive error (-1), exiting...\n");
@@ -2264,7 +2267,7 @@ void dochat(int *serversockfd, int *clientsockfd, struct settings *settings) {
serverbuf[servernumbytes] = '\0';
- printf("BOUNCER-SERVER RECEIVED: '%s', length '%d'.\n", serverbuf, servernumbytes);
+ debugprint("BOUNCER-SERVER RECEIVED: '%s', length '%d'.\n", serverbuf, servernumbytes);
// Try to process received string (which should contain one or more server responses/commands)
// TODO - What if there were two server respones/commands and only one didn't need relaying?
@@ -2276,7 +2279,7 @@ void dochat(int *serversockfd, int *clientsockfd, struct settings *settings) {
// see if there's anything from stdin
if (FD_ISSET(STDIN, &rfds)) {
- printf("reading stdin!\n");
+ debugprint("reading stdin!\n");
char outgoingmsg[MAXDATASIZE]; // String to send to server
int outgoingmsgrc; // Return code from getstdin() for outgoing message
@@ -2299,7 +2302,7 @@ void dochat(int *serversockfd, int *clientsockfd, struct settings *settings) {
printf("Checking channel[%d] out of %d.\n", i, channelcount);
// Skip this one and increment channelcount if it's a blank channel
if (!channels[i].name[0]) {
- printf("Skipping channel[%d], incrementing channelcount.\n", i);
+ debugprint("Skipping channel[%d], incrementing channelcount.\n", i);
channelcount++;
continue;
}
@@ -2323,12 +2326,12 @@ void dochat(int *serversockfd, int *clientsockfd, struct settings *settings) {
if (i == newfd) {
continue;
}
- printf("checking client socket %d out of %d.\n", i, fdmax);
+ debugprint("checking client socket %d out of %d.\n", i, fdmax);
if (FD_ISSET(i, &rfds)) {
- printf("fd %d is FD_ISSET and it is a...\n", i);
+ debugprint("fd %d is FD_ISSET and it is a...\n", i);
// if value of clientsockfd then must be a new connection, if greater must be an existing connection
if (i == *clientsockfd) {
- printf("...new connection!\n");
+ debugprint("...new connection!\n");
// handle new connections
if (numclients(clients) >= MAXCLIENTS) {
fprintf(stderr, "too many clients!\n");
@@ -2359,7 +2362,7 @@ void dochat(int *serversockfd, int *clientsockfd, struct settings *settings) {
printf("SSL_accept failed for fd %d.\n", clients[j].fd);
ERR_print_errors_fp(stderr);
} else {
- printf("SSL_accept succeeded for fd %d.\n", clients[j].fd);
+ debugprint("SSL_accept succeeded for fd %d.\n", clients[j].fd);
}
} else {
// If not using TLS then cast newfd to SSL* even though it will just be the original newfd int really
@@ -2369,7 +2372,7 @@ void dochat(int *serversockfd, int *clientsockfd, struct settings *settings) {
}
}
// TODO - Handle the "find a free element" loop not finding a free element
- printf("bouncer-client: new connection from %s on socket %d\n", inet_ntop(remoteaddr.ss_family, get_in_addr((struct sockaddr*)&remoteaddr), remoteIP, INET6_ADDRSTRLEN), newfd);
+ debugprint("bouncer-client: new connection from %s on socket %d\n", inet_ntop(remoteaddr.ss_family, get_in_addr((struct sockaddr*)&remoteaddr), remoteIP, INET6_ADDRSTRLEN), newfd);
// Alert other clients about the new connection
char alertmsg[MAXDATASIZE];
if (!snprintf(alertmsg, MAXDATASIZE, "NOTICE %s :blabouncer: new client connected from %s with fd %d.", ircdstrings.ircnick,
@@ -2379,16 +2382,16 @@ void dochat(int *serversockfd, int *clientsockfd, struct settings *settings) {
}
// "except" 0 since we trust this message
sendtoallclients(clients, alertmsg, 0, settings);
- printf("bouncer-client: total client connections: %d\n", numclients(clients));
+ debugprint("bouncer-client: total client connections: %d\n", numclients(clients));
}
} else {
- printf("...previous connection!\n");
+ debugprint("...previous connection!\n");
// handle data from a client
if ((clientnumbytes = sockread(clients[arrindex(clients, i)].ssl, clientbuf, sizeof clientbuf, settings->clienttls)) <= 0) {
// got error or connection closed by client
if (clientnumbytes == 0) {
// connection closed
- printf("bouncer-client: socket %d hung up\n", i);
+ debugprint("bouncer-client: socket %d hung up\n", i);
} else {
perror("recv");
}
@@ -2396,7 +2399,7 @@ void dochat(int *serversockfd, int *clientsockfd, struct settings *settings) {
disconnectclient(i, clients, &ircdstrings, settings);
FD_CLR(i, &rfds); // remove from master set - TODO is this needed at the moment since we just add everything from *clientsockfd to fdmax to rfds
// TODO - Handle the "remove the client" loop not finding the old fd
- printf("bouncer-client: total client connections: %d\n", numclients(clients));
+ debugprint("bouncer-client: total client connections: %d\n", numclients(clients));
} else {
// we got some data from a client
// null terminate that baby
@@ -2405,7 +2408,7 @@ void dochat(int *serversockfd, int *clientsockfd, struct settings *settings) {
while (clientbuf[strlen(clientbuf) - 1] == '\n' || clientbuf[strlen(clientbuf) - 1] == '\r') {
clientbuf[strlen(clientbuf) - 1] = '\0';
}
- printf("BOUNCER-CLIENT RECEIVED: '%s'\n", clientbuf);
+ debugprint("BOUNCER-CLIENT RECEIVED: '%s'\n", clientbuf);
// Try to process received string (which should contain one or more client responses/commands)
// TODO - What if there were two server respones/commands and only one didn't need relaying?
@@ -2449,7 +2452,7 @@ int main(int argc, char *argv[]) {
}
}
- printf("Using configuration file '%s'.\n", settings.conffile);
+ debugprint("Using configuration file '%s'.\n", settings.conffile);
// Populate said settings from configuration file
@@ -2539,6 +2542,9 @@ int main(int argc, char *argv[]) {
}
}
+ // Is debugging enabled?
+ debug = getconfint("debug", settings.conffile);
+
// Make sure the base directory exists
struct stat st = {0};
if (stat(settings.basedir, &st) == -1) {
diff --git a/blabouncer.conf.example b/blabouncer.conf.example
index 1395f97..8e0b263 100644
--- a/blabouncer.conf.example
+++ b/blabouncer.conf.example
@@ -6,7 +6,7 @@
# realname = "Mr Bla Bouncer"
#
# Shell expansion is not supported, so do not try and specify e.g.
-# "~/.blabouncer/" or "%HOME/.blabouncer/", instead use "/home/foo/.blabouncer"
+# "~/.blabouncer/" or "$HOME/.blabouncer/", instead use "/home/foo/.blabouncer"
nick = "blabounce"
nick2 = "bbounce2"
@@ -62,3 +62,7 @@ logging = "1"
# Enable replay logging ("1" for yes or "0" for no)
# Replay log goes to basedir/replay.log
replaylogging = "1"
+
+# Debug output control ("2" for print to screen, "1" for print to file, or "0" for disabled)
+# (Note: print to file not yet implemented)
+debug = "0"
diff --git a/config.c b/config.c
index 9716d8f..9b69682 100644
--- a/config.c
+++ b/config.c
@@ -48,7 +48,7 @@ int getconfstr(char *confname, char *filename, char* dest) {
// If we got here, then either we found the configuration option or we ran out of file
if (!found) {
- printf("Error reading configuration option '%s', did not find it in configuration file '%s'.\n", confname, filename);
+ debugprint("Error reading configuration option '%s', did not find it in configuration file '%s'.\n", confname, filename);
fclose(fp);
return 0;
}
@@ -80,7 +80,7 @@ int getconfstr(char *confname, char *filename, char* dest) {
strncpy(dest, conf + 1, strlen(conf) - 2); // Copy result to destination string without quotes
dest[strlen(conf) - 2] = '\0'; // Null terminate
- printf("getconfstr(): returning '%s'.\n", dest);
+ debugprint("getconfstr(): returning '%s'.\n", dest);
// Close fine and return success
fclose(fp);
@@ -107,21 +107,21 @@ int checkpassword(char *password, char *filename) {
char confpassword[MAXCHAR];
if (!getconfstr("password", filename, confpassword)) {
- printf("checkpassword(): error getting configuration option 'password' from configuration file '%s'.\n", filename);
+ debugprint("checkpassword(): error getting configuration option 'password' from configuration file '%s'.\n", filename);
return 0;
}
// Ensure passwords are the same length
if (strlen(password) != strlen(confpassword)) {
- printf("Password length mismatch!\n");
+ debugprint("Password length mismatch!\n");
return 0;
}
// Ensure passwords match
if (strncmp(password, confpassword, strlen(password)) == 0) {
- printf("confpassword matches password.\n");
+ debugprint("confpassword matches password.\n");
return 1;
} else {
- printf("confpassword does NOT match password!\n");
+ debugprint("confpassword does NOT match password!\n");
return 0;
}
@@ -167,7 +167,7 @@ int createconfigfile(char *filename) {
"# realname = \"Mr Bla Bouncer\"\n"
"#\n"
"# Shell expansion is not supported, so do not try and specify e.g.\n"
- "# \"~/.blabouncer/\" or \"%HOME/.blabouncer/\", instead use \"/home/foo/.blabouncer\"\n"
+ "# \"~/.blabouncer/\" or \"$HOME/.blabouncer/\", instead use \"/home/foo/.blabouncer\"\n"
"\n"
"nick = \"blabounce\"\n"
"nick2 = \"bbounce2\"\n"
@@ -222,7 +222,11 @@ int createconfigfile(char *filename) {
"\n"
"# Enable replay logging (\"1\" for yes or \"0\" for no)\n"
"# Replay log goes to basedir/replay.log\n"
- "replaylogging = \"1\"\n";
+ "replaylogging = \"1\"\n"
+ "\n"
+ "# Debug output control (\"2\" for print to screen, \"1\" for print to file, or \"0\" for disabled)\n"
+ "# (Note: print to file not yet implemented)\n"
+ "debug = \"0\"\n";
// Write complete string to file
if ((fprintf(fp, string)) < 0) {
diff --git a/config.h b/config.h
index 4043f36..630d011 100644
--- a/config.h
+++ b/config.h
@@ -8,6 +8,8 @@
#include <sys/stat.h>
#include <limits.h>
+#include "functions.h"
+
#define MAXCHAR 1000
// Sets 'dest' to the value of the configuration option with name
diff --git a/functions.c b/functions.c
index 3ca7b06..0d4380b 100644
--- a/functions.c
+++ b/functions.c
@@ -1,9 +1,12 @@
#include "functions.h"
+// Global debug control
+extern int debug;
+
// Internal function just to replace nick in server greeting strings
// (as in ":servername 00x oldnick :Blablabla" -> ":servername 00x newnick :Blablabla")
void updategreetingnick(char *greeting, char *greetingnum, char *newnick, char *oldnick) {
- printf("updategreetingnick(): '%s' '%s' '%s' '%s'.\n", greeting, greetingnum, newnick, oldnick);
+ debugprint("updategreetingnick(): '%s' '%s' '%s' '%s'.\n", greeting, greetingnum, newnick, oldnick);
// Find the position of the old nick in the current greeting
char searchstr[MAXDATASIZE];
@@ -22,7 +25,7 @@ void updategreetingnick(char *greeting, char *greetingnum, char *newnick, char *
snprintf(searchstr, MAXDATASIZE, " %s %s ", greetingnum, newnick);
ret = strstr(greeting, searchstr);
if (ret != NULL) {
- printf("updategreetingnick(): newnick is already present, returning.\n");
+ debugprint("updategreetingnick(): newnick is already present, returning.\n");
return;
}
}
@@ -52,7 +55,18 @@ void updategreetingnick(char *greeting, char *greetingnum, char *newnick, char *
// And finally copy back to source string
strcpy(greeting, greetingtmp2);
- printf("updategreetingnick(): Built new greeting '%s' '%s', length '%ld'.\n", greetingnum, greeting, strlen(greeting));
+ debugprint("updategreetingnick(): Built new greeting '%s' '%s', length '%ld'.\n", greetingnum, greeting, strlen(greeting));
+}
+
+// Print debugging output if enabled
+void debugprint(char *format, ...) {
+ if (!debug) return;
+
+ va_list args;
+
+ va_start(args, format);
+ vprintf(format, args);
+ va_end(args);
}
// Get stdin line with buffer overrun protection
@@ -106,11 +120,11 @@ void stripprefix(char *string) {
// Make a copy to work with
char string2[strlen(string)];
- printf("stripprefix(): starting with '%s', strlen: %zd.\n", string, strlen(string));
+ debugprint("stripprefix(): starting with '%s', strlen: %zd.\n", string, strlen(string));
// Don't bother if this isn't a prefix with a leading ':'
if (string[0] != ':') {
- printf("stripprefix(): no leading ':', returning.\n");
+ debugprint("stripprefix(): no leading ':', returning.\n");
return;
}
@@ -125,7 +139,7 @@ void stripprefix(char *string) {
// Finish with null terminator
string[strlen(string) - 1] = '\0';
- printf("stripprefix(): finishing with '%s', strlen: %zd.\n", string, strlen(string));
+ debugprint("stripprefix(): finishing with '%s', strlen: %zd.\n", string, strlen(string));
}
// Extract final parameter from IRC message, removing the leading colon ':'
@@ -140,7 +154,7 @@ void extractfinalparameter(char *string) {
// Position of colon
int colonpos = -1;
- printf("extractfinalparameter(): starting with '%s', strlen: %zd.\n", string, strlen(string));
+ debugprint("extractfinalparameter(): starting with '%s', strlen: %zd.\n", string, strlen(string));
// Strip the colon at position 0 if there is one
stripprefix(string);
@@ -148,14 +162,14 @@ void extractfinalparameter(char *string) {
// Find the colon
for (size_t i = 0; i < strlen(string); i++) {
if (string[i] == ':') {
- printf("Found colon at position %zd!\n", i);
+ debugprint("Found colon at position %zd!\n", i);
colonpos = i;
break;
}
}
if (colonpos == -1) {
- printf("no colon found, returning\n");
+ debugprint("no colon found, returning\n");
return;
}
@@ -172,7 +186,7 @@ void extractfinalparameter(char *string) {
// Finish with null terminator
string[counter] = '\0';
- printf("extractfinalparameter(): finishing with '%s', strlen: %zd.\n", string, strlen(string));
+ debugprint("extractfinalparameter(): finishing with '%s', strlen: %zd.\n", string, strlen(string));
}
// Extract the IRC nick from a prefix
@@ -184,7 +198,7 @@ void extractnickfromprefix(char *string) {
// Position of bang
int bangpos = -1;
- printf("extractnickfromprefix(): starting with '%s', strlen: %zd.\n", string, strlen(string));
+ debugprint("extractnickfromprefix(): starting with '%s', strlen: %zd.\n", string, strlen(string));
// Strip the colon at position 0 if there is one
stripprefix(string);
@@ -192,26 +206,26 @@ void extractnickfromprefix(char *string) {
// Find the bang
for (size_t i = 0; i < strlen(string); i++) {
if (string[i] == '!') {
- printf("Found bang at position %zd!\n", i);
+ debugprint("Found bang at position %zd!\n", i);
bangpos = i;
break;
}
}
if (bangpos == -1) {
- printf("no bang found, returning\n");
+ debugprint("no bang found, returning\n");
return;
}
// Terminate the string at whatever position we found the bang
string[bangpos] = '\0';
- printf("extractnickfromprefix(): finishing with '%s', strlen: %zd.\n", string, strlen(string));
+ debugprint("extractnickfromprefix(): finishing with '%s', strlen: %zd.\n", string, strlen(string));
}
// Update an existing nickuserhost string with a new nick
void updatenickuserhost(char *nickuserhost, char *nick) {
- printf("updatenickuserhost(): updating '%s' with '%s'.\n", nickuserhost, nick);
+ debugprint("updatenickuserhost(): updating '%s' with '%s'.\n", nickuserhost, nick);
// Position of bang
int bangpos = -1;
@@ -219,14 +233,14 @@ void updatenickuserhost(char *nickuserhost, char *nick) {
// Find the bang
for (size_t i = 0; i < strlen(nickuserhost); i++) {
if (nickuserhost[i] == '!') {
- printf("Found bang at position %ld!\n", i);
+ debugprint("Found bang at position %ld!\n", i);
bangpos = i;
break;
}
}
if (bangpos == -1) {
- printf("No bang found in existing nickuserhost, quitting!\n");
+ debugprint("No bang found in existing nickuserhost, quitting!\n");
return;
}
@@ -238,12 +252,12 @@ void updatenickuserhost(char *nickuserhost, char *nick) {
// Copy back to source string
strcpy(nickuserhost, newstr);
- printf("updatenickuserhost(): new nickuserhost '%s', length '%ld'.\n", nickuserhost, strlen(nickuserhost));
+ debugprint("updatenickuserhost(): new nickuserhost '%s', length '%ld'.\n", nickuserhost, strlen(nickuserhost));
}
// Update existing greeting strings with a new nickuserhost and new nick
void updategreetings(char *greeting001, char *greeting002, char *greeting003, char *greeting004, char *greeting005a, char *greeting005b, char *greeting005c, char *newnickuserhost, char *oldnickuserhost, char *newnick, char *oldnick) {
- printf("updategreetings(): updating greetings with new nickuserhost '%s' and nick '%s'.\n", newnickuserhost, newnick);
+ debugprint("updategreetings(): updating greetings with new nickuserhost '%s' and nick '%s'.\n", newnickuserhost, newnick);
// nickuserhost and greeting001's final component first
// (final component as in ":servername 001 nick :Blablabla final!com@ponent"
@@ -274,7 +288,7 @@ void updategreetings(char *greeting001, char *greeting002, char *greeting003, ch
// Copy back to source greeting 001
strcpy(greeting001, greetingtmp);
- printf("updategreetings(): new greeting 001 '%s', length '%ld'.\n", greeting001, strlen(greeting001));
+ debugprint("updategreetings(): new greeting 001 '%s', length '%ld'.\n", greeting001, strlen(greeting001));
// Get the new nick
diff --git a/functions.h b/functions.h
index 8fb5145..29518aa 100644
--- a/functions.h
+++ b/functions.h
@@ -12,6 +12,7 @@
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/select.h>
+#include <stdarg.h>
#define MAXDATASIZE 513 // max number of bytes we can get at once (RFC2812 says 512, plus one for null terminator)
@@ -20,6 +21,9 @@
#define NO_INPUT 1
#define TOO_LONG 2
+// Print debugging output if enabled
+void debugprint(char *format, ...);
+
// Get stdin line with buffer overrun protection
int getstdin(char *prompt, char *buff, size_t sz);
diff --git a/logging.c b/logging.c
index a4698c6..dcadb26 100644
--- a/logging.c
+++ b/logging.c
@@ -55,7 +55,7 @@ int logline(char *str, char *ournick, char *basedir, int type) {
}
// Copy into the token array (strlen + 1 to get the NULL terminator)
strncpy(tokens[i], token, strlen(token) + 1);
- printf("logline(): extracted '%s'.\n", tokens[i]);
+ debugprint("logline(): extracted '%s'.\n", tokens[i]);
}
switch(type) {
@@ -75,7 +75,7 @@ int logline(char *str, char *ournick, char *basedir, int type) {
snprintf(filename, MAXCHAR, "%s/logs/%s.log", basedir, tokens[2]);
}
- printf("logline(): Logging PRIVMSG from '%s' to '%s' message '%s' in filename '%s'.\n", tokens[0], tokens[2], str, filename);
+ debugprint("logline(): Logging PRIVMSG from '%s' to '%s' message '%s' in filename '%s'.\n", tokens[0], tokens[2], str, filename);
break;
@@ -95,7 +95,7 @@ int logline(char *str, char *ournick, char *basedir, int type) {
snprintf(filename, MAXCHAR, "%s/logs/%s.log", basedir, tokens[2] + pos);
- printf("logline(): Logging JOIN/PART to/from '%s' in filename '%s'.\n", tokens[2] + pos, filename);
+ debugprint("logline(): Logging JOIN/PART to/from '%s' in filename '%s'.\n", tokens[2] + pos, filename);
// Build a friendly message (e.g. ":nick!user@host JOIN #channel" -> "nick (user@host) has joined #channel")
@@ -134,7 +134,7 @@ int logline(char *str, char *ournick, char *basedir, int type) {
snprintf(filename, MAXCHAR, "%s/logs/%s.log", basedir, tokens[2]);
- printf("logline(): Logging TOPIC for '%s' in filename '%s'.\n", tokens[2], filename);
+ debugprint("logline(): Logging TOPIC for '%s' in filename '%s'.\n", tokens[2], filename);
// Build a friendly message (e.g. ":nick!user@host TOPIC #channel :blah blah" -> "nick has changed the topic to: blah blah")
snprintf(line, MAXCHAR, "%s has changed the topic to: %s", tokens[0], str);
@@ -155,7 +155,7 @@ int logline(char *str, char *ournick, char *basedir, int type) {
printf("Error creating log directory '%s'.\n", logdir);
exit(1);
} else {
- printf("logline(): log directory '%s'.\n", logdir);
+ debugprint("logline(): log directory '%s'.\n", logdir);
}
}
@@ -200,7 +200,7 @@ int logline(char *str, char *ournick, char *basedir, int type) {
// Ensure the line finishes with CRLF
appendcrlf(line);
- printf("logline(): Complete log string to write: '%s', length '%ld'.\n", line, strlen(line));
+ debugprint("logline(): Complete log string to write: '%s', length '%ld'.\n", line, strlen(line));
// Write complete line to file
if ((bytes = fprintf(fp, line)) < 0) {
diff --git a/replay.c b/replay.c
index 8926c9e..b0b4b26 100644
--- a/replay.c
+++ b/replay.c
@@ -270,7 +270,7 @@ int writereplayline(char *str, char *basedir) {
// Ensure the line finishes with CRLF
appendcrlf(line);
- printf("Complete replay log string to write: '%s', length '%ld'.\n", line, strlen(line));
+ debugprint("Complete replay log string to write: '%s', length '%ld'.\n", line, strlen(line));
// Write complete line to file
if ((bytes = fprintf(fp, line)) < 0) {
diff --git a/sockets.c b/sockets.c
index 562e5c0..7581779 100644
--- a/sockets.c
+++ b/sockets.c
@@ -22,7 +22,7 @@ int createserversocket(char *host, char *port) {
if ((rv = getaddrinfo(host, port, &hints, &servinfo)) != 0) {
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
- return 1;
+ return 1; // TODO - Should this perhaps return 0 or exit?
}
// loop through all the results and connect to the first we can
@@ -43,11 +43,11 @@ int createserversocket(char *host, char *port) {
if (p == NULL) {
fprintf(stderr, "bouncer-server: failed to connect\n");
- return 2;
+ return 2; // TODO - Should this perhaps return 0 or exit?
}
inet_ntop(p->ai_family, get_in_addr((struct sockaddr *)p->ai_addr), s, sizeof s);
- printf("bouncer-server: connecting to %s\n", s);
+ debugprint("bouncer-server: connecting to %s\n", s);
freeaddrinfo(servinfo); // all done with this structure
@@ -78,7 +78,7 @@ int createclientsocket(char *listenport) {
listener = socket(p->ai_family, p->ai_socktype, p->ai_protocol);
if (listener != -1) {
// success, got IPv6!
- printf("success, got IPv6! ai_family: %d\n", p->ai_family);
+ debugprint("success, got IPv6! ai_family: %d\n", p->ai_family);
break;
}
}
@@ -91,7 +91,7 @@ int createclientsocket(char *listenport) {
listener = socket(p->ai_family, p->ai_socktype, p->ai_protocol);
if (listener != -1) {
// moderate success, got IPv4!
- printf("moderate success, got IPv4! ai_family: %d\n", p->ai_family);
+ debugprint("moderate success, got IPv4! ai_family: %d\n", p->ai_family);
break;
}
}
diff --git a/sockets.h b/sockets.h
index 64cbd44..3ad5406 100644
--- a/sockets.h
+++ b/sockets.h
@@ -15,6 +15,8 @@
#include <openssl/ssl.h>
#include <openssl/err.h>
+#include "functions.h"
+
#define BACKLOG 10 // maximum length to which the queue of pending connections for sockfd may grow
// get sockaddr, IPv4 or IPv6: