summaryrefslogtreecommitdiff
path: root/functions.c
diff options
context:
space:
mode:
Diffstat (limited to 'functions.c')
-rw-r--r--functions.c54
1 files changed, 34 insertions, 20 deletions
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