AmendHub

Download:

jcs

/

wallops

/

amendments

/

10

irc: Handle /nick command and handle others changing nicks


jcs made amendment 10 over 2 years ago
--- irc.c Sun Feb 6 19:30:53 2022 +++ irc.c Sun Feb 6 20:17:09 2022 @@ -36,6 +36,8 @@ void irc_parse_names(struct chatter *chatter, char *li void irc_add_user_to_channel(struct chatter *chatter, struct irc_user *user); void irc_remove_nick_from_channel(struct chatter *chatter, char *nick); +void irc_change_user_nick(struct chatter *chatter, struct irc_user *user, + char *nick); void irc_parse_channel_mode_change(struct chatter *chatter, char *mode, char *args); @@ -472,13 +474,23 @@ done_parsing: irc_remove_nick_from_channel(chatter, user->nick); return; } + if (strcmp(msg.cmd, "KILL") == 0) { + user = irc_parse_user(msg.source); + chatter_printf(chatter, "$B*** %s ($0%s@%s$B)$0 has been " + "killed:$/ %s", user->nick, user->username, user->hostname, + msg.msg); + if (strcmp(user->nick, chatter->irc_nick) == 0) + irc_set_active_channel(chatter, NULL); + else + irc_remove_nick_from_channel(chatter, user->nick); + return; + } + if (strcmp(msg.cmd, "NICK") == 0) { user = irc_parse_user(msg.source); chatter_printf(chatter, "$B*** %s$0 is now known as $B%s$0", user->nick, msg.msg); - irc_remove_nick_from_channel(chatter, user->nick); - strlcpy(user->nick, msg.msg, sizeof(user->nick)); - irc_add_user_to_channel(chatter, user); + irc_change_user_nick(chatter, user, msg.msg); return; } if (strcmp(msg.cmd, "MODE") == 0) { @@ -630,24 +642,44 @@ irc_process_input(struct chatter *chatter, char *str) goto not_enough_params; chatter_printf(chatter, "$B[$0msg$B($0%s$B)]$0$/ %s", arg1, str); irc_printf(chatter, "PRIVMSG %s :%s\r\n", arg1, str); - } else if (strcmp(arg0, "join") == 0) { + return; + } + + if (strcmp(arg0, "join") == 0) { if (str == NULL) goto not_enough_params; irc_printf(chatter, "JOIN %s\r\n", str); - } else if (strcmp(arg0, "part") == 0) { + return; + } + + if (strcmp(arg0, "part") == 0) { if (str == NULL && chatter->irc_channel) irc_printf(chatter, "PART %s\r\n", chatter->irc_channel->name); else irc_printf(chatter, "PART %s\r\n", str); - } else if (strcmp(arg0, "quit") == 0) { + return; + } + + if (strcmp(arg0, "quit") == 0) { irc_printf(chatter, "QUIT :%s\r\n", str == NULL ? "&" : str); - } else if (strcmp(arg0, "quote") == 0) { + return; + } + + if (strcmp(arg0, "nick") == 0) { if (str == NULL) goto not_enough_params; + irc_printf(chatter, "NICK %s\r\n", str); + return; + } + + if (strcmp(arg0, "quote") == 0) { + if (str == NULL) + goto not_enough_params; irc_printf(chatter, "%s\r\n", str); - } else - chatter_printf(chatter, "unrecognized command \"$B%s$0\"", arg0); - + return; + } + + chatter_printf(chatter, "unrecognized command \"$B%s$0\"", arg0); return; not_enough_params: @@ -759,6 +791,29 @@ irc_remove_nick_from_channel(struct chatter *chatter, chatter->irc_channel->users = xreallocarray(chatter->irc_channel->users, sizeof(struct irc_channel_nick), chatter->irc_channel->nusers); +} + +void +irc_change_user_nick(struct chatter *chatter, struct irc_user *user, + char *nick) +{ + short n; + + for (n = 0; n < chatter->irc_channel->nusers; n++) { + if (strcmp(chatter->irc_channel->users[n].nick, user->nick) == 0) { + strlcpy(chatter->irc_channel->users[n].nick, nick, + sizeof(chatter->irc_channel->users[n].nick)); + break; + } + } + + chatter_sync_nick_list(chatter, false); + + if (strcmp(chatter->irc_nick, user->nick) == 0) { + free(chatter->irc_nick); + chatter->irc_nick = xstrdup(nick); + chatter_update_titlebar(chatter); + } } void --- main.c Sun Feb 6 19:39:13 2022 +++ main.c Sun Feb 6 20:21:15 2022 @@ -104,8 +104,10 @@ main(void) event_in = FindWindow(event.where, &event_win); found_focusable = NULL; for (n = 0; n < nfocusables; n++) { - if (focusables[n]->win == event_win) + if (focusables[n]->win == event_win) { found_focusable = focusables[n]; + break; + } } }