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()
Showing
2 changed files
with
63 additions
and
19 deletions
... | @@ -70,6 +70,12 @@ extern int mu_virtual_domain; | ... | @@ -70,6 +70,12 @@ extern int mu_virtual_domain; |
70 | 70 | ||
71 | extern struct passwd * getpwnam_virtual __P((const char *u)); | 71 | extern struct passwd * getpwnam_virtual __P((const char *u)); |
72 | 72 | ||
73 | /* Get the host name, doing a gethostbyname() if possible. | ||
74 | * | ||
75 | * It is the caller's responsibility to free host. | ||
76 | */ | ||
77 | extern int mu_get_host_name __P((char **host)); | ||
78 | |||
73 | /* Set the default user email address. | 79 | /* Set the default user email address. |
74 | * | 80 | * |
75 | * Subsequent calls to mu_get_user_email() with a NULL name will return this | 81 | * 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)); | ... | @@ -88,7 +94,7 @@ extern int mu_set_user_email __P ((const char *email)); |
88 | extern int mu_set_user_email_domain __P ((const char *domain)); | 94 | extern int mu_set_user_email_domain __P ((const char *domain)); |
89 | 95 | ||
90 | /* Return the currently set user email domain, or NULL if not set. */ | 96 | /* Return the currently set user email domain, or NULL if not set. */ |
91 | extern const char *mu_get_user_email_domain __P ((void)); | 97 | extern int mu_get_user_email_domain __P ((const char** domain)); |
92 | 98 | ||
93 | /* | 99 | /* |
94 | * Get the default email address for user name. A NULL name is taken | 100 | * Get the default email address for user name. A NULL name is taken | ... | ... |
... | @@ -31,6 +31,7 @@ | ... | @@ -31,6 +31,7 @@ |
31 | #include <time.h> | 31 | #include <time.h> |
32 | #include <unistd.h> | 32 | #include <unistd.h> |
33 | 33 | ||
34 | #include <sys/param.h> | ||
34 | #include <sys/stat.h> | 35 | #include <sys/stat.h> |
35 | #include <sys/types.h> | 36 | #include <sys/types.h> |
36 | #include <sys/wait.h> | 37 | #include <sys/wait.h> |
... | @@ -43,6 +44,7 @@ | ... | @@ -43,6 +44,7 @@ |
43 | #include <mailutils/error.h> | 44 | #include <mailutils/error.h> |
44 | #include <mailutils/iterator.h> | 45 | #include <mailutils/iterator.h> |
45 | #include <mailutils/mutil.h> | 46 | #include <mailutils/mutil.h> |
47 | #include <mailutils/parse822.h> | ||
46 | 48 | ||
47 | #include "mu_asprintf.h" | 49 | #include "mu_asprintf.h" |
48 | 50 | ||
... | @@ -519,6 +521,31 @@ getpwnam_virtual (const char *u) | ... | @@ -519,6 +521,31 @@ getpwnam_virtual (const char *u) |
519 | 521 | ||
520 | #endif | 522 | #endif |
521 | 523 | ||
524 | int | ||
525 | mu_get_host_name (char **host) | ||
526 | { | ||
527 | char hostname[MAXHOSTNAMELEN + 1]; | ||
528 | struct hostent *hp = NULL; | ||
529 | char *domain = NULL; | ||
530 | |||
531 | gethostname (hostname, sizeof hostname); | ||
532 | hostname[sizeof (hostname) - 1] = 0; | ||
533 | |||
534 | if ((hp = gethostbyname (hostname))) | ||
535 | domain = hp->h_name; | ||
536 | else | ||
537 | domain = hostname; | ||
538 | |||
539 | domain = strdup (domain); | ||
540 | |||
541 | if (!domain) | ||
542 | return ENOMEM; | ||
543 | |||
544 | *host = domain; | ||
545 | |||
546 | return 0; | ||
547 | } | ||
548 | |||
522 | /* | 549 | /* |
523 | * Functions used to convert unix mailbox/user names into RFC822 addr-specs. | 550 | * Functions used to convert unix mailbox/user names into RFC822 addr-specs. |
524 | */ | 551 | */ |
... | @@ -583,19 +610,29 @@ mu_set_user_email_domain (const char *domain) | ... | @@ -583,19 +610,29 @@ mu_set_user_email_domain (const char *domain) |
583 | return 0; | 610 | return 0; |
584 | } | 611 | } |
585 | 612 | ||
586 | const char * | 613 | int |
587 | mu_get_user_email_domain (void) | 614 | mu_get_user_email_domain (const char** domain) |
588 | { | 615 | { |
589 | return mu_user_email_domain; | 616 | int err = 0; |
617 | |||
618 | if (!mu_user_email_domain) | ||
619 | { | ||
620 | if((err = mu_get_host_name(&mu_user_email_domain))) | ||
621 | return err; | ||
622 | } | ||
623 | |||
624 | *domain = mu_user_email_domain; | ||
625 | |||
626 | return 0; | ||
590 | } | 627 | } |
591 | 628 | ||
592 | char * | 629 | char * |
593 | mu_get_user_email (const char *name) | 630 | mu_get_user_email (const char *name) |
594 | { | 631 | { |
595 | char hostname[256]; | 632 | int status = 0; |
596 | struct hostent *hp; | 633 | char *localpart = NULL; |
597 | char *domainpart; | 634 | const char *domainpart = NULL; |
598 | char *email; | 635 | char *email = NULL; |
599 | 636 | ||
600 | if (!name && mu_user_email) | 637 | if (!name && mu_user_email) |
601 | { | 638 | { |
... | @@ -616,22 +653,23 @@ mu_get_user_email (const char *name) | ... | @@ -616,22 +653,23 @@ mu_get_user_email (const char *name) |
616 | name = pw->pw_name; | 653 | name = pw->pw_name; |
617 | } | 654 | } |
618 | 655 | ||
619 | if (mu_user_email_domain) | 656 | status = mu_get_user_email_domain (&domainpart); |
657 | |||
658 | if (status) | ||
620 | { | 659 | { |
621 | domainpart = mu_user_email_domain; | 660 | errno = status; |
661 | return NULL; | ||
622 | } | 662 | } |
623 | else | ||
624 | { | ||
625 | gethostname (hostname, sizeof hostname); | ||
626 | hostname[sizeof (hostname) - 1] = 0; | ||
627 | 663 | ||
628 | if ((hp = gethostbyname (hostname))) | 664 | if ((status = parse822_quote_local_part (&localpart, name))) |
629 | domainpart = hp->h_name; | 665 | { |
630 | else | 666 | errno = status; |
631 | domainpart = hostname; | 667 | return NULL; |
632 | } | 668 | } |
633 | 669 | ||
634 | mu_asprintf (&email, "%s@%s", name, domainpart); | 670 | mu_asprintf (&email, "%s@%s", localpart, domainpart); |
671 | |||
672 | free (localpart); | ||
635 | 673 | ||
636 | if (!email) | 674 | if (!email) |
637 | errno = ENOMEM; | 675 | errno = ENOMEM; | ... | ... |
-
Please register or sign in to post a comment