commit 9fc9d7481f16ceaa2e1314429820cdde9e2f4825
parent 1ba28625784be33b4110f20fc50ef080efec9aa1
Author: hhvn <dev@hhvn.uk>
Date: Sat, 18 Dec 2021 16:43:33 +0000
s/commands.c s/handle.c s/serv.c s/hirc.h: serv_ischannel() for CHANTYPE checks
Diffstat:
4 files changed, 27 insertions(+), 13 deletions(-)
diff --git a/src/commands.c b/src/commands.c
@@ -173,7 +173,7 @@ command_query(struct Server *server, char *str) {
return;
}
- if (strchr(support_get(server, "CHANTYPES"), *str)) {
+ if (serv_ischannel(server, str)) {
ui_error("can't query a channel", NULL);
return;
}
@@ -193,18 +193,17 @@ command_quit(struct Server *server, char *str) {
static void
command_join(struct Server *server, char *str) {
- char *chantypes, msg[512];
+ char msg[512];
if (!str) {
ui_error("/join requires argument", NULL);
return;
}
- chantypes = support_get(server, "CHANTYPES");
- if (strchr(chantypes, *str))
+ if (serv_ischannel(server, str))
snprintf(msg, sizeof(msg), "JOIN %s\r\n", str);
else
- snprintf(msg, sizeof(msg), "JOIN %c%s\r\n", chantypes ? *chantypes : '#', str);
+ snprintf(msg, sizeof(msg), "JOIN %c%s\r\n", '#', str);
if (server->status == ConnStatus_connected)
ircprintf(server, "%s", msg);
@@ -220,12 +219,10 @@ command_join(struct Server *server, char *str) {
static void
command_part(struct Server *server, char *str) {
char *channel = NULL, *reason = NULL;
- char *chantypes, msg[512];
-
- chantypes = support_get(server, "CHANTYPES");
+ char msg[512];
if (str) {
- if (strchr(chantypes, *str))
+ if (serv_ischannel(server, str))
channel = strtok_r(str, " ", &reason);
else
reason = str;
@@ -258,7 +255,7 @@ command_kick(struct Server *server, char *str) {
s = strtok_r(str, " ", &reason);
- if (s && strchr(support_get(server, "CHANTYPES"), *s)) {
+ if (serv_ischannel(server, s)) {
channel = s;
nick = strtok_r(NULL, " ", &reason);
} else {
@@ -285,7 +282,7 @@ command_mode(struct Server *server, char *str) {
if (str)
s = strtok_r(str, " ", &modes);
- if (s && strchr(support_get(server, "CHANTYPES"), *s)) {
+ if (serv_ischannel(server, s)) {
channel = s;
} else {
if (selected.channel == NULL) {
@@ -690,7 +687,7 @@ command_topic(struct Server *server, char *str) {
else
channel = topic = NULL;
- if (channel && !strchr(support_get(server, "CHANTYPES"), *channel)) {
+ if (channel && !serv_ischannel(server, channel)) {
topic = channel;
channel = NULL;
}
diff --git a/src/handle.c b/src/handle.c
@@ -242,7 +242,7 @@ handle_MODE(char *msg, char **params, struct Server *server, time_t timestamp) {
if (**params != ':' || param_len(params) < 4)
return;
- if (strchr(support_get(server, "CHANTYPES"), **(params+2))) {
+ if (serv_ischannel(server, *(params+2))) {
if ((chan = chan_get(&server->channels, *(params+2), -1)) == NULL)
chan = chan_add(server, &server->channels, *(params+2), 0);
diff --git a/src/hirc.h b/src/hirc.h
@@ -108,6 +108,7 @@ char * support_get(struct Server *server, char *key);
void support_set(struct Server *server, char *key, char *value);
void schedule_push(struct Server *server, char *tmsg, char *msg);
char * schedule_pull(struct Server *server, char *tmsg);
+int serv_ischannel(struct Server *server, char *str);
/* handle.c */
void handle(struct Server *server);
diff --git a/src/serv.c b/src/serv.c
@@ -20,6 +20,7 @@
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
+#include <assert.h>
#include <errno.h>
#include <netdb.h>
#include <sys/types.h>
@@ -410,6 +411,21 @@ support_set(struct Server *server, char *key, char *value) {
p->next->value = value ? strdup(value) : NULL;
}
+int
+serv_ischannel(struct Server *server, char *str) {
+ char *chantypes;
+
+ if (!str || !server)
+ return 0;
+
+ chantypes = support_get(server, "CHANTYPES");
+ if (!chantypes)
+ chantypes = config_gets("def.chantypes");
+ assert(chantypes != NULL);
+
+ return strchr(chantypes, *str) != NULL;
+}
+
void
schedule_push(struct Server *server, char *tmsg, char *msg) {
struct Schedule *p;