Deal with $hostname and $shorthost
[apps/madmutt.git] / lib-lua / madmutt.cpkg
index daa101f..d083405 100644 (file)
  */
 
 #include <lib-lib/lib-lib.h>
  */
 
 #include <lib-lib/lib-lib.h>
-#include <lib-lua/lib-lua.h>
 
 
+#include <sys/utsname.h>
 #include <sys/types.h>
 #include <pwd.h>
 
 #include <sys/types.h>
 #include <pwd.h>
 
+#include <lib-lua/lib-lua.h>
+#include <lib-sys/unix.h>
+
 #include "../mutt.h"
 @import "base.cpkg"
 
 #include "../mutt.h"
 @import "base.cpkg"
 
@@ -44,6 +47,34 @@ static char *madmutt_init_homedir(void)
     return m_strdup(pw ? pw->pw_dir : (getenv("HOME") ?: "/"));
 }
 
     return m_strdup(pw ? pw->pw_dir : (getenv("HOME") ?: "/"));
 }
 
+static char *madmutt_init_shorthost(void)
+{
+    struct utsname utsname;
+    const char *p;
+
+    /* some systems report the FQDN instead of just the hostname */
+    uname(&utsname);
+    p = strchrnul(utsname.nodename, '.');
+    return p_dupstr(utsname.nodename, p - utsname.nodename);
+}
+
+static char *madmutt_init_hostname(void)
+{
+    char buffer[STRING];
+
+    if (getdnsdomainname(buffer, sizeof(buffer)) < 0)
+        return m_strdup("@");
+
+    if (*buffer != '@') {
+        int len   = m_strlen(buffer) + m_strlen(MCore.shorthost) + 2;
+        char *res = p_new(char, len);
+        snprintf(res, len, "%s.%s", NONULL(MCore.shorthost), buffer);
+        return res;
+    }
+
+    return m_strdup(NONULL(MCore.shorthost));
+}
+
 #if defined(HAVE_QDBM)
 #  define HCACHE_BACKEND  "qdbm"
 #elif defined(HAVE_GDBM)
 #if defined(HAVE_QDBM)
 #  define HCACHE_BACKEND  "qdbm"
 #elif defined(HAVE_GDBM)
@@ -106,12 +137,20 @@ static char *madmutt_init_homedir(void)
      */
     path_t shell          = madmutt_init_shell();
 
      */
     path_t shell          = madmutt_init_shell();
 
-    /* TODO: document */
     path_t username       = madmutt_init_username();
     path_t username       = madmutt_init_username();
-
-    /* TODO: document */
     path_t homedir        = madmutt_init_homedir();
 
     path_t homedir        = madmutt_init_homedir();
 
+    /*
+     ** .pp
+     ** Specifies the hostname to use after the ``\fT@\fP'' in local e-mail
+     ** addresses and during generation of \fTMessage-ID:\fP headers.
+     ** .pp
+     ** Please be sure to really know what you are doing when changing this variable
+     ** to configure a custom domain part of Message-IDs.
+     */
+    string_t hostname     = madmutt_init_hostname();
+    string_t shorthost    = madmutt_init_shorthost();
+
     /*
      ** .pp
      ** This variable allows you to specify where Madmutt will place its
     /*
      ** .pp
      ** This variable allows you to specify where Madmutt will place its