summaryrefslogtreecommitdiff
path: root/functions.c
diff options
context:
space:
mode:
authorLuke Bratch <luke@bratch.co.uk>2019-05-17 22:18:28 +0100
committerLuke Bratch <luke@bratch.co.uk>2019-05-17 22:18:28 +0100
commit6c4c42c68c53091a0ca04ae6885ed3cc24216348 (patch)
tree8223b896b5da3f8f40cffe4970edf7e85c379cff /functions.c
parent751bee2c2ceb7e9426bf00a669da5679c20ee41a (diff)
Properly support changing our nick while connecting by updating it everywhere including initial server welcome strings.
Make sure channels are cleared when PARTing them. Ignore most commands from clients until they are registered with us. Send the correct current nick when sending NOTICEs.
Diffstat (limited to 'functions.c')
-rw-r--r--functions.c94
1 files changed, 94 insertions, 0 deletions
diff --git a/functions.c b/functions.c
index 6034250..ffd94f1 100644
--- a/functions.c
+++ b/functions.c
@@ -1,5 +1,50 @@
#include "functions.h"
+// 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);
+
+ // Find the position of the old nick in the current greeting
+ char searchstr[MAXDATASIZE];
+ snprintf(searchstr, MAXDATASIZE, " %s %s :", greetingnum, oldnick);
+ char *ret;
+ ret = strstr(greeting, searchstr);
+
+ // If ret not found, try again without the colon (e.g. for greeting 004)
+ if (ret == NULL) {
+ snprintf(searchstr, MAXDATASIZE, " %s %s ", greetingnum, oldnick);
+ ret = strstr(greeting, searchstr);
+ }
+
+ // If ret *still* not found, abandon ship
+ if (ret == NULL) {
+ printf("Error updating greeting string, substring not found. Exiting!\n");
+ exit(1);
+ }
+
+ int pos = ret - greeting + 5; // +5 for " 001 "
+
+ // Copy the start of the old greeting into a new string
+ char greetingtmp[MAXDATASIZE];
+ strncpy(greetingtmp, greeting, pos);
+ // Terminate it
+ greetingtmp[pos] = '\0';
+
+ // Now smash everything (start of old greeting + new nick + remainder of old greeting)
+ // together into the new greeting, put in a new temporary string
+ char greetingtmp2[MAXDATASIZE];
+ if (!snprintf(greetingtmp2, MAXDATASIZE, "%s%s %s", greetingtmp, newnick, greeting + pos + strlen(oldnick) + 1)) {
+ fprintf(stderr, "Error while preparing new greeting string!\n");
+ exit(1);
+ }
+
+ // And finally copy back to source string
+ strcpy(greeting, greetingtmp2);
+
+ printf("updategreetingnick(): Built new greeting '%s' '%s', length '%ld'.\n", greetingnum, greeting, strlen(greeting));
+}
+
// Get stdin line with buffer overrun protection
int getstdin(char *prompt, char *buff, size_t sz) {
int ch, extra;
@@ -182,3 +227,52 @@ void updatenickuserhost(char *nickuserhost, char *nick) {
printf("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 *newnickuserhost, char *oldnickuserhost, char *newnick, char *oldnick) {
+ printf("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"
+
+ // Make copies of the nickuserhosts to work with
+ char newnickuserhostcpy[MAXDATASIZE];
+ strcpy(newnickuserhostcpy, newnickuserhost);
+ stripprefix(newnickuserhostcpy);
+ char oldnickuserhostcpy[MAXDATASIZE];
+ strcpy(oldnickuserhostcpy, oldnickuserhost);
+ stripprefix(oldnickuserhostcpy);
+
+ // Find the position of the old nickuserhost in the current greeting 001
+ char *ret;
+ ret = strstr(greeting001, oldnickuserhostcpy);
+ int pos = ret - greeting001;
+
+ // Terminate greeting001 where the nickuserhost begins
+ greeting001[pos] = '\0';
+
+ // Stick the new nickuserhost in place in a temporary greeting 001 string
+ char greetingtmp[MAXDATASIZE];
+ snprintf(greetingtmp, MAXDATASIZE, "%s%s", greeting001, newnickuserhostcpy);
+
+ // Terminate it
+ greetingtmp[pos + strlen(newnickuserhostcpy)] = '\0';
+
+ // Copy back to source greeting 001
+ strcpy(greeting001, greetingtmp);
+
+ printf("updategreetings(): new greeting 001 '%s', length '%ld'.\n", greeting001, strlen(greeting001));
+
+ // Get the new nick
+
+ char newnickcpy[MAXDATASIZE];
+ strcpy(newnickcpy, newnick);
+ extractnickfromprefix(newnickcpy);
+
+ // greeting nicks next
+ // (as in ":servername 00x oldnick :Blablabla" -> ":servername 00x newnick :Blablabla")
+ updategreetingnick(greeting001, "001", newnickcpy, oldnick);
+ updategreetingnick(greeting002, "002", newnickcpy, oldnick);
+ updategreetingnick(greeting003, "003", newnickcpy, oldnick);
+ updategreetingnick(greeting004, "004", newnickcpy, oldnick);
+}