From 8e71d6ec739ec74fbb5c712e9341d4319a17d8f5 Mon Sep 17 00:00:00 2001 From: kyak Date: Sun, 19 Jul 2015 09:45:50 +0300 Subject: [PATCH 1/6] Add expandos for hostname See http://bugs.irssi.org/index.php?do=details&task_id=829 --- src/irc/core/irc-expandos.c | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/irc/core/irc-expandos.c b/src/irc/core/irc-expandos.c index 5d2de503..cc46ece5 100644 --- a/src/irc/core/irc-expandos.c +++ b/src/irc/core/irc-expandos.c @@ -76,6 +76,29 @@ static char *expando_userhost(SERVER_REC *server, void *item, int *free_ret) return g_strconcat(username, "@", hostname, NULL);; } +/* your hostname address (host) */ +static char *expando_hostname(SERVER_REC *server, void *item, int *free_ret) +{ + IRC_SERVER_REC *ircserver; + char hostname[100]; + char **list; + + ircserver = IRC_SERVER(server); + + /* prefer the _real_ /userhost reply */ + if (ircserver != NULL && ircserver->userhost != NULL) { + list = g_strsplit(ircserver->userhost, "@", -1); + return list[1]; + } + + /* haven't received userhost reply yet. guess something */ + *free_ret = TRUE; + + if (gethostname(hostname, sizeof(hostname)) != 0 || *hostname == '\0') + strcpy(hostname, "??"); + return g_strconcat(hostname, NULL); +} + /* user mode in active server */ static char *expando_usermode(SERVER_REC *server, void *item, int *free_ret) { @@ -136,6 +159,9 @@ void irc_expandos_init(void) expando_create("X", expando_userhost, "window changed", EXPANDO_ARG_NONE, "window server changed", EXPANDO_ARG_WINDOW, NULL); + expando_create("x", expando_hostname, + "window changed", EXPANDO_ARG_NONE, + "window server changed", EXPANDO_ARG_WINDOW, NULL); expando_create("usermode", expando_usermode, "window changed", EXPANDO_ARG_NONE, "window server changed", EXPANDO_ARG_WINDOW, @@ -164,6 +190,7 @@ void irc_expandos_deinit(void) expando_destroy("H", expando_server_numeric); expando_destroy("S", expando_servername); expando_destroy("X", expando_userhost); + expando_destroy("x", expando_hostname); expando_destroy("usermode", expando_usermode); expando_destroy("cumode", expando_cumode); From 0435331912f5d975c4cbef9b018bb89c07e610eb Mon Sep 17 00:00:00 2001 From: kyak Date: Wed, 26 Aug 2015 09:34:48 +0300 Subject: [PATCH 2/6] Clean up in hostname expando before return Clean up the vector resulting from g_strsplit before returning from expando_hostname(). Also, use g_strdup() instead of g_strconcat() to return the pointer to hostname. --- src/irc/core/irc-expandos.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/irc/core/irc-expandos.c b/src/irc/core/irc-expandos.c index cc46ece5..f26c9699 100644 --- a/src/irc/core/irc-expandos.c +++ b/src/irc/core/irc-expandos.c @@ -82,13 +82,16 @@ static char *expando_hostname(SERVER_REC *server, void *item, int *free_ret) IRC_SERVER_REC *ircserver; char hostname[100]; char **list; + char *hostname_split; ircserver = IRC_SERVER(server); /* prefer the _real_ /userhost reply */ if (ircserver != NULL && ircserver->userhost != NULL) { list = g_strsplit(ircserver->userhost, "@", -1); - return list[1]; + hostname_split = g_strdup(list[1]); + g_strfreev(list); + return hostname_split; } /* haven't received userhost reply yet. guess something */ @@ -96,7 +99,7 @@ static char *expando_hostname(SERVER_REC *server, void *item, int *free_ret) if (gethostname(hostname, sizeof(hostname)) != 0 || *hostname == '\0') strcpy(hostname, "??"); - return g_strconcat(hostname, NULL); + return g_strdup(hostname); } /* user mode in active server */ From 19760679eb141baf3bff2110ee03aec90c653a2a Mon Sep 17 00:00:00 2001 From: kyak Date: Sat, 29 Aug 2015 15:29:57 +0300 Subject: [PATCH 3/6] In expando_hostname, set *free_ret to TRUE *free_ret must be set to TRUE in both cases, since we return some newly initialised memory --- src/irc/core/irc-expandos.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/irc/core/irc-expandos.c b/src/irc/core/irc-expandos.c index f26c9699..4aa50c52 100644 --- a/src/irc/core/irc-expandos.c +++ b/src/irc/core/irc-expandos.c @@ -86,6 +86,8 @@ static char *expando_hostname(SERVER_REC *server, void *item, int *free_ret) ircserver = IRC_SERVER(server); + *free_ret = TRUE; + /* prefer the _real_ /userhost reply */ if (ircserver != NULL && ircserver->userhost != NULL) { list = g_strsplit(ircserver->userhost, "@", -1); @@ -95,8 +97,6 @@ static char *expando_hostname(SERVER_REC *server, void *item, int *free_ret) } /* haven't received userhost reply yet. guess something */ - *free_ret = TRUE; - if (gethostname(hostname, sizeof(hostname)) != 0 || *hostname == '\0') strcpy(hostname, "??"); return g_strdup(hostname); From b7d82ecdcee3224ca5280977d0ffd7b5e0fb07d2 Mon Sep 17 00:00:00 2001 From: kyak Date: Sat, 29 Aug 2015 15:32:22 +0300 Subject: [PATCH 4/6] Use "(none)" as fallback for hostname and userhost expandos Use "(none)" instead of "??" for hostname and userhost expandos when these can't be reliably detected. --- src/irc/core/irc-expandos.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/irc/core/irc-expandos.c b/src/irc/core/irc-expandos.c index 4aa50c52..df0326a2 100644 --- a/src/irc/core/irc-expandos.c +++ b/src/irc/core/irc-expandos.c @@ -72,7 +72,7 @@ static char *expando_userhost(SERVER_REC *server, void *item, int *free_ret) username = ircserver->connrec->username; if (gethostname(hostname, sizeof(hostname)) != 0 || *hostname == '\0') - strcpy(hostname, "??"); + strcpy(hostname, "(none)"); return g_strconcat(username, "@", hostname, NULL);; } @@ -98,7 +98,7 @@ static char *expando_hostname(SERVER_REC *server, void *item, int *free_ret) /* haven't received userhost reply yet. guess something */ if (gethostname(hostname, sizeof(hostname)) != 0 || *hostname == '\0') - strcpy(hostname, "??"); + strcpy(hostname, "(none)"); return g_strdup(hostname); } From b065f2a0112fe733878c17375d16cf12a3457e09 Mon Sep 17 00:00:00 2001 From: kyak Date: Sat, 29 Aug 2015 15:36:05 +0300 Subject: [PATCH 5/6] Use HOST_NAME_MAX for userhost and hostname expandos Use HOST_NAME_MAX instead of hardcoded 100 for userhost and hostname expandos. --- src/irc/core/irc-expandos.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/irc/core/irc-expandos.c b/src/irc/core/irc-expandos.c index df0326a2..9604c625 100644 --- a/src/irc/core/irc-expandos.c +++ b/src/irc/core/irc-expandos.c @@ -56,7 +56,8 @@ static char *expando_userhost(SERVER_REC *server, void *item, int *free_ret) { IRC_SERVER_REC *ircserver; const char *username; - char hostname[100]; + char hostname[HOST_NAME_MAX]; + ircserver = IRC_SERVER(server); @@ -80,7 +81,7 @@ static char *expando_userhost(SERVER_REC *server, void *item, int *free_ret) static char *expando_hostname(SERVER_REC *server, void *item, int *free_ret) { IRC_SERVER_REC *ircserver; - char hostname[100]; + char hostname[HOST_NAME_MAX]; char **list; char *hostname_split; From 2f9cca8ec6e491759583c1de3bd74fbef0d6e2a1 Mon Sep 17 00:00:00 2001 From: kyak Date: Sat, 29 Aug 2015 15:40:17 +0300 Subject: [PATCH 6/6] Remove extra line break How did this extra break cripple in here? --- src/irc/core/irc-expandos.c | 1 - 1 file changed, 1 deletion(-) diff --git a/src/irc/core/irc-expandos.c b/src/irc/core/irc-expandos.c index 9604c625..5d8150f0 100644 --- a/src/irc/core/irc-expandos.c +++ b/src/irc/core/irc-expandos.c @@ -58,7 +58,6 @@ static char *expando_userhost(SERVER_REC *server, void *item, int *free_ret) const char *username; char hostname[HOST_NAME_MAX]; - ircserver = IRC_SERVER(server); /* prefer the _real_ /userhost reply */