irssi-import / bugs.irssi.org

bugs.irssi.org archive
https://github.com/irssi/irssi/issues
0 stars 0 forks source link

Special variable for a hostname [patch] #829

Open irssibot opened 12 years ago

irssibot commented 12 years ago

Hi,

By looking into the list of irssi special variables here: http://irssi.org/documentation/special_vars i couldn't help noticing that there is no expandos for a hostname. There is $X, but it expands to user@host, while i need just a host.

The motivation is to be able add a host to the right of a nickname in the statusbar. Currently it is not possible to achieve this by editing config/theme file. It should be possible to add a statusbar item via perl script, but i guess it's not a good day for perl scripting today...

Anyway, attached is the patch that adds $x expandos, which expands to a hostname and works pretty similar to $X (i.e. first try the result of /userhost, then fallback to gethostbyname).

Thanks!

irssibot commented 12 years ago

irssi-hostname-expandos.patch

Index: src/irc/core/irc-expandos.c
===================================================================
--- src/irc/core/irc-expandos.c (revision 5210)
+++ src/irc/core/irc-expandos.c (working copy)
@@ -76,6 +76,29 @@
    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 @@
    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 @@
    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);