commit 5b4bd1b9b0a22f9dbacb0addd3aa59b58b6d3788
parent 1a072907420181c28c824fe42699d3d7c44b426c
Author: hhvn <dev@hhvn.uk>
Date: Thu, 21 Apr 2022 18:38:44 +0100
Update server settings with /connect on disconnect server.
Diffstat:
3 files changed, 73 insertions(+), 21 deletions(-)
diff --git a/src/commands.c b/src/commands.c
@@ -186,8 +186,11 @@ struct Command commands[] = {
"but will hide it unless this command is used.", NULL}},
{"connect", command_connect, 0, {
"usage: /connect [-network <name>] [-nick <nick>] [-user <user>]",
- " [-real <comment>] [-tls] [-verify] <host> [port]",
- "Connect to a network/server", NULL}},
+ " [-real <comment>] [-tls] [-verify] [host] [port]",
+ "Connect to a network/server.",
+ "If no host is given, it will attempt to connect to the\n",
+ "selected server if it is disconnected\n",
+ NULL}},
{"disconnect", command_disconnect, 0, {
"usage: /disconnect [network] [msg]",
"Disconnect from a network/server", NULL}},
@@ -791,10 +794,10 @@ command_connect) {
char *network = NULL;
char *host = NULL;
char *port = NULL;
- char *nick = config_gets("def.nick");
- char *username = config_gets("def.user");
- char *realname = config_gets("def.real");
- int tls = 0, tls_verify = 0;
+ char *nick = NULL;
+ char *username = NULL;
+ char *realname = NULL;
+ int tls = -1, tls_verify = -1; /* tell serv_update not to change */
int ret;
struct passwd *user;
enum {
@@ -856,34 +859,51 @@ command_connect) {
host = strtok(str, " ");
port = strtok(NULL, " ");
- if (!host && network && serv_get(&servers, network)) {
- serv_connect(serv_get(&servers, network));
- return;
- } else if (!host && server) {
- serv_connect(server);
- return;
- } else if (!host) {
- ui_error("must specify host", NULL);
+ if (!host) {
+ if (network) {
+ if (!(tserver = serv_get(&servers, network)))
+ ui_error("no such network", NULL);
+ } else {
+ if (!server)
+ ui_error("must specify host", NULL);
+ else
+ tserver = server;
+ }
+ if (server) {
+ serv_update(tserver, nick, username, realname, tls, tls_verify);
+ serv_connect(tserver);
+ }
return;
}
- if (!nick) {
+ if (tls <= 0)
+ tls = 0;
+ if (tls_verify <= 0)
+ tls_verify = 0;
+
+ if (!nick && !(nick = config_gets("def.nick"))) {
user = getpwuid(geteuid());
nick = user ? user->pw_name : "null";
}
-
+ if (!username && !(username = config_gets("def.user")))
+ username = nick;
+ if (!realname && !(realname = config_gets("def.real")))
+ realname = nick;
+ if (!network)
+ network = host;
if (!port) {
- port = tls ? "6697" : "6667";
+ /* no ifdef required, as -tls only works with -DTLS */
+ if (tls)
+ port = "6697";
+ else
+ port = "6667";
}
- username = username ? username : nick;
- realname = realname ? realname : nick;
- network = network ? network : host;
-
tserver = serv_add(&servers, network, host, port, nick, username, realname, tls, tls_verify);
serv_connect(tserver);
if (!nouich)
ui_select(tserver, NULL);
+ return;
}
COMMAND(
diff --git a/src/hirc.h b/src/hirc.h
@@ -118,6 +118,8 @@ void serv_free(struct Server *server);
void serv_connect(struct Server *server);
struct Server * serv_create(char *name, char *host, char *port, char *nick,
char *username, char *realname, int tls, int tls_verify);
+void serv_update(struct Server *sp, char *nick, char *username,
+ char *realname, int tls, int tls_verify);
struct Server * serv_get(struct Server **head, char *name);
struct Server * serv_add(struct Server **head, char *name, char *host,
char *port, char *nick, char *username, char *realname, int tls, int tls_verify);
diff --git a/src/serv.c b/src/serv.c
@@ -121,6 +121,36 @@ serv_create(char *name, char *host, char *port, char *nick,
return server;
}
+void
+serv_update(struct Server *sp, char *nick, char *username,
+ char *realname, int tls, int tls_verify) {
+ if (!sp)
+ return;
+ if (nick) {
+ pfree(&sp->self->nick);
+ sp->self->nick = estrdup(nick);
+ }
+ if (username) {
+ pfree(&sp->username);
+ sp->username = estrdup(nick);
+ }
+ if (realname) {
+ pfree(&sp->realname);
+ sp->username = estrdup(nick);
+ }
+#ifdef TLS
+ if (tls >= 0 && !sp->tls) {
+ sp->tls = tls;
+ if (strcmp(sp->port, "6667") == 0) {
+ pfree(&sp->port);
+ sp->port = estrdup("6697");
+ }
+ }
+ if (tls_verify >= 0)
+ sp->tls_verify = tls_verify;
+#endif /* TLS */
+}
+
struct Server *
serv_add(struct Server **head, char *name, char *host, char *port,
char *nick, char *username, char *realname, int tls, int tls_verify) {