From d50c1a6b2004c3cc616f3db842b4c2d209dca38b Mon Sep 17 00:00:00 2001 From: Luke Bratch Date: Sat, 20 Apr 2019 23:50:04 +0200 Subject: We are bouncing! You can now: - connect the bouncer to a server - connect to the bouncer with a real client - join channels - have new clients connect and have the bouncer join those new clients to the channels - relay PRIVMSGs between all clients Some random current big bugs: - Joining channels whilst multiple clients are already connected doesn't join all clients properly - Parting channels doesn't work - No idea what will happen if the nick is in use, etc. Features missing: - Almost everything :) --- functions.c | 77 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) (limited to 'functions.c') diff --git a/functions.c b/functions.c index 97f8629..86a1f03 100644 --- a/functions.c +++ b/functions.c @@ -50,3 +50,80 @@ void appendcrlf(char *string) { string[startlen + 1] = '\n'; // Add LF string[startlen + 2] = '\0'; // Finish with null terminator } + +// Remove leading colon ':' which is the starting character of a prefix in an IRC message +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)); + + // Don't bother if this isn't a prefix with a leading ':' + if (string[0] != ':') { + printf("stripprefix(): no leading ':', returning.\n"); + return; + } + + // Copy the old string into a new one, but... + for (size_t i = 1; i < strlen(string); i++) { + printf("i: %zd. strlen: %zd.\n", i, strlen(string)); + string2[i - 1] = string[i]; + } + + // Copy result back to original string + strncpy(string, string2, strlen(string) - 1); + + // Finish with null terminator + string[strlen(string) - 1] = '\0'; + + printf("stripprefix(): finishing with '%s', strlen: %zd.\n", string, strlen(string)); +} + +// Extract final parameter from IRC message, removing the leading colon ':' +// e.g. given this string: +// ":irc.tghost.co.uk 332 blabounce #test :This is a test topic!" +// We want to end up with: +// "This is a test topic!" +void extractfinalparameter(char *string) { + // Make a copy to work with + char string2[strlen(string)]; + + // Position of colon + int colonpos = -1; + + printf("extractfinalparameter(): starting with '%s', strlen: %zd.\n", string, strlen(string)); + + // Strip the colon at position 0 if there is one + stripprefix(string); + + // Find the colon + for (size_t i = 0; i < strlen(string); i++) { + printf("i: %zd. strlen: %zd.\n", i, strlen(string)); + if (string[i] == ':') { + printf("Found colon at position %zd!\n", i); + colonpos = i; + break; + } + } + + if (colonpos == -1) { + printf("no colon found, returning\n"); + return; + } + + // Build a new string starting from the next position after the colon + int counter = 0; + for (size_t i = colonpos + 1; i < strlen(string); i++) { + printf("i: %zd. strlen: %zd.\n", i, strlen(string)); + string2[counter] = string[i]; + counter++; + } + + // Copy result back to original string + strncpy(string, string2, counter); + + // Finish with null terminator + string[counter] = '\0'; + + printf("extractfinalparameter(): finishing with '%s', strlen: %zd.\n", string, strlen(string)); +} -- cgit v1.2.3