summaryrefslogtreecommitdiff
path: root/functions.c
diff options
context:
space:
mode:
authorLuke Bratch <luke@bratch.co.uk>2019-04-20 23:50:04 +0200
committerLuke Bratch <luke@bratch.co.uk>2019-04-20 23:50:04 +0200
commitd50c1a6b2004c3cc616f3db842b4c2d209dca38b (patch)
tree346ef9701d2e4488f7d6ee65fd0aa6969241e765 /functions.c
parentf4070050375a621d4297e0629c0072519cdf5d2d (diff)
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 :)
Diffstat (limited to 'functions.c')
-rw-r--r--functions.c77
1 files changed, 77 insertions, 0 deletions
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));
+}