From 93e530320e024e9119fb8717459ef618086c5839 Mon Sep 17 00:00:00 2001 From: Luke Bratch Date: Thu, 16 May 2019 23:40:43 +0100 Subject: Correctly handle nicks changing and actually track users PARTing channels. Also change nickuserhost to store the leading colon (:) since it's always needed (so far). --- functions.c | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) (limited to 'functions.c') diff --git a/functions.c b/functions.c index e43f31c..6034250 100644 --- a/functions.c +++ b/functions.c @@ -150,3 +150,35 @@ void extractnickfromprefix(char *string) { printf("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); + + // Position of bang + int bangpos = -1; + + // Find the bang + for (size_t i = 0; i < strlen(nickuserhost); i++) { + if (nickuserhost[i] == '!') { + printf("Found bang at position %ld!\n", i); + bangpos = i; + break; + } + } + + if (bangpos == -1) { + printf("No bang found in existing nickuserhost, quitting!\n"); + return; + } + + // Make a new string combining the nick nick and the old nickuserhost + the offset of the bang + char newstr[MAXDATASIZE]; + snprintf(newstr, MAXDATASIZE, "%s%s", nick, nickuserhost + bangpos); + newstr[strlen(nickuserhost) - bangpos + strlen(nick)] = '\0'; + + // Copy back to source string + strcpy(nickuserhost, newstr); + + printf("updatenickuserhost(): new nickuserhost '%s', length '%ld'.\n", nickuserhost, strlen(nickuserhost)); +} -- cgit v1.2.3