Commit 6488c734 6488c73413387febaca267dd0f26c6c947388833 by Sam Roberts

mu_get_host_name(): returns this host name, looking gethostname() up

with gethostbyname() if possible.
mu_get_user_email_domain() and family now use mu_get_host_name()
1 parent 15e24c25
......@@ -70,6 +70,12 @@ extern int mu_virtual_domain;
extern struct passwd * getpwnam_virtual __P((const char *u));
/* Get the host name, doing a gethostbyname() if possible.
*
* It is the caller's responsibility to free host.
*/
extern int mu_get_host_name __P((char **host));
/* Set the default user email address.
*
* Subsequent calls to mu_get_user_email() with a NULL name will return this
......@@ -88,7 +94,7 @@ extern int mu_set_user_email __P ((const char *email));
extern int mu_set_user_email_domain __P ((const char *domain));
/* Return the currently set user email domain, or NULL if not set. */
extern const char *mu_get_user_email_domain __P ((void));
extern int mu_get_user_email_domain __P ((const char** domain));
/*
* Get the default email address for user name. A NULL name is taken
......
......@@ -31,6 +31,7 @@
#include <time.h>
#include <unistd.h>
#include <sys/param.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
......@@ -43,6 +44,7 @@
#include <mailutils/error.h>
#include <mailutils/iterator.h>
#include <mailutils/mutil.h>
#include <mailutils/parse822.h>
#include "mu_asprintf.h"
......@@ -519,6 +521,31 @@ getpwnam_virtual (const char *u)
#endif
int
mu_get_host_name (char **host)
{
char hostname[MAXHOSTNAMELEN + 1];
struct hostent *hp = NULL;
char *domain = NULL;
gethostname (hostname, sizeof hostname);
hostname[sizeof (hostname) - 1] = 0;
if ((hp = gethostbyname (hostname)))
domain = hp->h_name;
else
domain = hostname;
domain = strdup (domain);
if (!domain)
return ENOMEM;
*host = domain;
return 0;
}
/*
* Functions used to convert unix mailbox/user names into RFC822 addr-specs.
*/
......@@ -583,19 +610,29 @@ mu_set_user_email_domain (const char *domain)
return 0;
}
const char *
mu_get_user_email_domain (void)
int
mu_get_user_email_domain (const char** domain)
{
return mu_user_email_domain;
int err = 0;
if (!mu_user_email_domain)
{
if((err = mu_get_host_name(&mu_user_email_domain)))
return err;
}
*domain = mu_user_email_domain;
return 0;
}
char *
mu_get_user_email (const char *name)
{
char hostname[256];
struct hostent *hp;
char *domainpart;
char *email;
int status = 0;
char *localpart = NULL;
const char *domainpart = NULL;
char *email = NULL;
if (!name && mu_user_email)
{
......@@ -616,22 +653,23 @@ mu_get_user_email (const char *name)
name = pw->pw_name;
}
if (mu_user_email_domain)
status = mu_get_user_email_domain (&domainpart);
if (status)
{
domainpart = mu_user_email_domain;
errno = status;
return NULL;
}
else
{
gethostname (hostname, sizeof hostname);
hostname[sizeof (hostname) - 1] = 0;
if ((hp = gethostbyname (hostname)))
domainpart = hp->h_name;
else
domainpart = hostname;
if ((status = parse822_quote_local_part (&localpart, name)))
{
errno = status;
return NULL;
}
mu_asprintf (&email, "%s@%s", name, domainpart);
mu_asprintf (&email, "%s@%s", localpart, domainpart);
free (localpart);
if (!email)
errno = ENOMEM;
......