michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "nsUserInfo.h" michael@0: #include "nsCRT.h" michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: #include "nsString.h" michael@0: #include "nsXPIDLString.h" michael@0: #include "nsReadableUtils.h" michael@0: #include "nsNativeCharsetUtils.h" michael@0: michael@0: /* Some UNIXy platforms don't have pw_gecos. In this case we use pw_name */ michael@0: #if defined(NO_PW_GECOS) michael@0: #define PW_GECOS pw_name michael@0: #else michael@0: #define PW_GECOS pw_gecos michael@0: #endif michael@0: michael@0: nsUserInfo::nsUserInfo() michael@0: { michael@0: } michael@0: michael@0: nsUserInfo::~nsUserInfo() michael@0: { michael@0: } michael@0: michael@0: NS_IMPL_ISUPPORTS(nsUserInfo,nsIUserInfo) michael@0: michael@0: NS_IMETHODIMP michael@0: nsUserInfo::GetFullname(char16_t **aFullname) michael@0: { michael@0: struct passwd *pw = nullptr; michael@0: michael@0: pw = getpwuid (geteuid()); michael@0: michael@0: if (!pw || !pw->PW_GECOS) return NS_ERROR_FAILURE; michael@0: michael@0: #ifdef DEBUG_sspitzer michael@0: printf("fullname = %s\n", pw->PW_GECOS); michael@0: #endif michael@0: michael@0: nsAutoCString fullname(pw->PW_GECOS); michael@0: michael@0: // now try to parse the GECOS information, which will be in the form michael@0: // Full Name, - eliminate the ", michael@0: // also, sometimes GECOS uses "&" to mean "the user name" so do michael@0: // the appropriate substitution michael@0: michael@0: // truncate at first comma (field delimiter) michael@0: int32_t index; michael@0: if ((index = fullname.Find(",")) != kNotFound) michael@0: fullname.Truncate(index); michael@0: michael@0: // replace ampersand with username michael@0: if (pw->pw_name) { michael@0: nsAutoCString username(pw->pw_name); michael@0: if (!username.IsEmpty() && nsCRT::IsLower(username.CharAt(0))) michael@0: username.SetCharAt(nsCRT::ToUpper(username.CharAt(0)), 0); michael@0: michael@0: fullname.ReplaceSubstring("&", username.get()); michael@0: } michael@0: michael@0: nsAutoString unicodeFullname; michael@0: NS_CopyNativeToUnicode(fullname, unicodeFullname); michael@0: michael@0: *aFullname = ToNewUnicode(unicodeFullname); michael@0: michael@0: if (*aFullname) michael@0: return NS_OK; michael@0: michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsUserInfo::GetUsername(char * *aUsername) michael@0: { michael@0: struct passwd *pw = nullptr; michael@0: michael@0: // is this portable? those are POSIX compliant calls, but I need to check michael@0: pw = getpwuid(geteuid()); michael@0: michael@0: if (!pw || !pw->pw_name) return NS_ERROR_FAILURE; michael@0: michael@0: #ifdef DEBUG_sspitzer michael@0: printf("username = %s\n", pw->pw_name); michael@0: #endif michael@0: michael@0: *aUsername = strdup(pw->pw_name); michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsUserInfo::GetDomain(char * *aDomain) michael@0: { michael@0: nsresult rv = NS_ERROR_FAILURE; michael@0: michael@0: struct utsname buf; michael@0: char *domainname = nullptr; michael@0: michael@0: // is this portable? that is a POSIX compliant call, but I need to check michael@0: if (uname(&buf)) { michael@0: return rv; michael@0: } michael@0: michael@0: #if defined(__linux__) michael@0: domainname = buf.domainname; michael@0: #endif michael@0: michael@0: if (domainname && domainname[0]) { michael@0: *aDomain = strdup(domainname); michael@0: rv = NS_OK; michael@0: } michael@0: else { michael@0: // try to get the hostname from the nodename michael@0: // on machines that use DHCP, domainname may not be set michael@0: // but the nodename might. michael@0: if (buf.nodename && buf.nodename[0]) { michael@0: // if the nodename is foo.bar.org, use bar.org as the domain michael@0: char *pos = strchr(buf.nodename,'.'); michael@0: if (pos) { michael@0: *aDomain = strdup(pos+1); michael@0: rv = NS_OK; michael@0: } michael@0: } michael@0: } michael@0: michael@0: return rv; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsUserInfo::GetEmailAddress(char * *aEmailAddress) michael@0: { michael@0: // use username + "@" + domain for the email address michael@0: michael@0: nsresult rv; michael@0: michael@0: nsAutoCString emailAddress; michael@0: nsXPIDLCString username; michael@0: nsXPIDLCString domain; michael@0: michael@0: rv = GetUsername(getter_Copies(username)); michael@0: if (NS_FAILED(rv)) return rv; michael@0: michael@0: rv = GetDomain(getter_Copies(domain)); michael@0: if (NS_FAILED(rv)) return rv; michael@0: michael@0: if (!username.IsEmpty() && !domain.IsEmpty()) { michael@0: emailAddress = (const char *)username; michael@0: emailAddress += "@"; michael@0: emailAddress += (const char *)domain; michael@0: } michael@0: else { michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: michael@0: *aEmailAddress = ToNewCString(emailAddress); michael@0: michael@0: return NS_OK; michael@0: } michael@0: