commit dc7bd3224ed299c3d7c83faca877e8d5d8fb1051
parent ab466944611643d7ad715ae52507e2963dd65cd2
Author: hhvn <dev@hhvn.uk>
Date: Fri, 12 Nov 2021 23:38:09 +0000
commands.c handle.c hirc.h struct.h: only select new window if event expected by command
Also added /part and /join
Diffstat:
4 files changed, 70 insertions(+), 4 deletions(-)
diff --git a/commands.c b/commands.c
@@ -21,6 +21,12 @@ static struct Command commands[] = {
{"quote", command_quote, {
"usage: /quote <message>",
"Send raw message to server", NULL}},
+ {"join", command_join, {
+ "usage: /join <channel>",
+ "Join channel", NULL}},
+ {"part", command_part, {
+ "usage: /part <channel>",
+ "Part channel", NULL}},
{"connect", command_connect, {
"usage: /connect [-network <name>] [-nick <nick>] [-user <user>]",
" [-real <comment>] [-tls] [-verify] <host> [port]",
@@ -50,6 +56,24 @@ command_quit(struct Server *server, char *str) {
}
void
+command_join(struct Server *server, char *str) {
+ if (strchr(config_gets("def.chantypes"), *str))
+ ircprintf(server, "JOIN %s\r\n", str);
+ else
+ ircprintf(server, "JOIN #%s\r\n", str);
+ handle_expect("JOIN", str);
+}
+
+void
+command_part(struct Server *server, char *str) {
+ if (strchr(config_gets("def.chantypes"), *str))
+ ircprintf(server, "PART %s\r\n", str);
+ else
+ ircprintf(server, "PART #%s\r\n", str);
+ handle_expect("PART", str);
+}
+
+void
command_quote(struct Server *server, char *str) {
if (!str) {
ui_error("/quote requires argument", NULL);
diff --git a/handle.c b/handle.c
@@ -4,6 +4,12 @@
#include <stdlib.h>
#include "hirc.h"
+struct Expect expect[] = {
+ { "JOIN", NULL },
+ { "PART", NULL },
+ { NULL, NULL },
+};
+
struct Handler handlers[] = {
{ "PING", handle_PING },
{ "JOIN", handle_JOIN },
@@ -51,10 +57,15 @@ handle_JOIN(char *msg, char **params, struct Server *server, time_t timestamp) {
hist_add(server->history, nick, msg, params, Activity_status, timestamp, HIST_LOG);
hist_add(chan->history, nick, msg, params, Activity_status, timestamp, HIST_SHOW);
- if (nick_isself(nick))
- ui_select(server, chan);
- else if (selected.channel == chan)
+ if (nick_isself(nick)) {
+ if (strcmp_n(target, handle_expect_get("JOIN")) == 0)
+ ui_select(server, chan);
+ else
+ windows[Win_buflist].refresh = 1;
+ handle_expect("JOIN", NULL);
+ } else if (selected.channel == chan) {
windows[Win_nicklist].refresh = 1;
+ }
nick_free(nick);
}
@@ -76,8 +87,11 @@ handle_PART(char *msg, char **params, struct Server *server, time_t timestamp) {
if (nick_isself(nick)) {
chan_setold(chan, 1);
nick_free_list(&chan->nicks);
- if (chan == selected.channel)
+ if (chan == selected.channel && strcmp_n(target, handle_expect_get("PART"))) {
ui_select(selected.server, NULL);
+ handle_expect("PART", NULL);
+ }
+ windows[Win_buflist].refresh = 1;
} else {
nick_remove(&chan->nicks, nick->nick);
if (chan == selected.channel)
@@ -283,6 +297,27 @@ handle_NICK(char *msg, char **params, struct Server *server, time_t timestamp) {
}
void
+handle_expect(char *cmd, char *about) {
+ int i;
+
+ for (i=0; expect[i].cmd; i++) {
+ if (strcmp(expect[i].cmd, cmd) == 0) {
+ free(expect[i].about);
+ expect[i].about = about ? strdup(about) : NULL;
+ }
+ }
+}
+
+char *
+handle_expect_get(char *cmd) {
+ int i;
+
+ for (i=0; expect[i].cmd; i++)
+ if (strcmp(expect[i].cmd, cmd) == 0)
+ return expect[i].about;
+}
+
+void
handle(int rfd, struct Server *server) {
time_t timestamp;
char **params;
diff --git a/hirc.h b/hirc.h
@@ -130,6 +130,8 @@ void ui_tls_error_(char *file, int line, struct tls *ctx, char *str);
void command_eval(char *str);
int command_getopt(char **str, struct CommandOpts *opts);
void command_quit(struct Server *server, char *str);
+void command_join(struct Server *server, char *str);
+void command_part(struct Server *server, char *str);
void command_quote(struct Server *server, char *str);
void command_connect(struct Server *server, char *str);
void command_select(struct Server *server, char *str);
diff --git a/struct.h b/struct.h
@@ -118,6 +118,11 @@ struct Handler {
void (*func)(char *msg, char **params, struct Server *server, time_t timestamp);
};
+struct Expect {
+ char *cmd;
+ char *about;
+};
+
/* commands received from user */
struct Command {
char *name;