1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* ***** BEGIN LICENSE BLOCK *****
3 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License. You may obtain a copy of the License at
8 : * http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * The Original Code is Mozilla Communicator client code.
16 : *
17 : * The Initial Developer of the Original Code is
18 : * Netscape Communications Corporation.
19 : * Portions created by the Initial Developer are Copyright (C) 1998
20 : * the Initial Developer. All Rights Reserved.
21 : *
22 : * Contributor(s):
23 : * Seth Spitzer <sspitzer@netscape.com>
24 : *
25 : * Alternatively, the contents of this file may be used under the terms of
26 : * either the GNU General Public License Version 2 or later (the "GPL"), or
27 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 : * in which case the provisions of the GPL or the LGPL are applicable instead
29 : * of those above. If you wish to allow use of your version of this file only
30 : * under the terms of either the GPL or the LGPL, and not to allow others to
31 : * use your version of this file under the terms of the MPL, indicate your
32 : * decision by deleting the provisions above and replace them with the notice
33 : * and other provisions required by the GPL or the LGPL. If you do not delete
34 : * the provisions above, a recipient may use your version of this file under
35 : * the terms of any one of the MPL, the GPL or the LGPL.
36 : *
37 : * ***** END LICENSE BLOCK ***** */
38 :
39 : #include "nsUserInfo.h"
40 : #include "nsCRT.h"
41 :
42 : #include <pwd.h>
43 : #include <sys/types.h>
44 : #include <unistd.h>
45 : #include <sys/utsname.h>
46 :
47 : #include "nsString.h"
48 : #include "nsXPIDLString.h"
49 : #include "nsReadableUtils.h"
50 : #include "nsNativeCharsetUtils.h"
51 :
52 : /* Some UNIXy platforms don't have pw_gecos. In this case we use pw_name */
53 : #if defined(NO_PW_GECOS)
54 : #define PW_GECOS pw_name
55 : #else
56 : #define PW_GECOS pw_gecos
57 : #endif
58 :
59 0 : nsUserInfo::nsUserInfo()
60 : {
61 0 : }
62 :
63 0 : nsUserInfo::~nsUserInfo()
64 : {
65 0 : }
66 :
67 0 : NS_IMPL_ISUPPORTS1(nsUserInfo,nsIUserInfo)
68 :
69 : NS_IMETHODIMP
70 0 : nsUserInfo::GetFullname(PRUnichar **aFullname)
71 : {
72 0 : struct passwd *pw = nsnull;
73 :
74 0 : pw = getpwuid (geteuid());
75 :
76 0 : if (!pw || !pw->PW_GECOS) return NS_ERROR_FAILURE;
77 :
78 : #ifdef DEBUG_sspitzer
79 : printf("fullname = %s\n", pw->PW_GECOS);
80 : #endif
81 :
82 0 : nsCAutoString fullname(pw->PW_GECOS);
83 :
84 : // now try to parse the GECOS information, which will be in the form
85 : // Full Name, <other stuff> - eliminate the ", <other stuff>
86 : // also, sometimes GECOS uses "&" to mean "the user name" so do
87 : // the appropriate substitution
88 :
89 : // truncate at first comma (field delimiter)
90 : PRInt32 index;
91 0 : if ((index = fullname.Find(",")) != kNotFound)
92 0 : fullname.Truncate(index);
93 :
94 : // replace ampersand with username
95 0 : if (pw->pw_name) {
96 0 : nsCAutoString username(pw->pw_name);
97 0 : if (!username.IsEmpty() && nsCRT::IsLower(username.CharAt(0)))
98 0 : username.SetCharAt(nsCRT::ToUpper(username.CharAt(0)), 0);
99 :
100 0 : fullname.ReplaceSubstring("&", username.get());
101 : }
102 :
103 0 : nsAutoString unicodeFullname;
104 0 : NS_CopyNativeToUnicode(fullname, unicodeFullname);
105 :
106 0 : *aFullname = ToNewUnicode(unicodeFullname);
107 :
108 0 : if (*aFullname)
109 0 : return NS_OK;
110 :
111 0 : return NS_ERROR_FAILURE;
112 : }
113 :
114 : NS_IMETHODIMP
115 0 : nsUserInfo::GetUsername(char * *aUsername)
116 : {
117 0 : struct passwd *pw = nsnull;
118 :
119 : // is this portable? those are POSIX compliant calls, but I need to check
120 0 : pw = getpwuid(geteuid());
121 :
122 0 : if (!pw || !pw->pw_name) return NS_ERROR_FAILURE;
123 :
124 : #ifdef DEBUG_sspitzer
125 : printf("username = %s\n", pw->pw_name);
126 : #endif
127 :
128 0 : *aUsername = nsCRT::strdup(pw->pw_name);
129 :
130 0 : return NS_OK;
131 : }
132 :
133 : NS_IMETHODIMP
134 0 : nsUserInfo::GetDomain(char * *aDomain)
135 : {
136 0 : nsresult rv = NS_ERROR_FAILURE;
137 :
138 : struct utsname buf;
139 0 : char *domainname = nsnull;
140 :
141 : // is this portable? that is a POSIX compliant call, but I need to check
142 0 : if (uname(&buf)) {
143 0 : return rv;
144 : }
145 :
146 : #if defined(HAVE_UNAME_DOMAINNAME_FIELD)
147 0 : domainname = buf.domainname;
148 : #elif defined(HAVE_UNAME_US_DOMAINNAME_FIELD)
149 : domainname = buf.__domainname;
150 : #endif
151 :
152 0 : if (domainname && domainname[0]) {
153 0 : *aDomain = nsCRT::strdup(domainname);
154 0 : rv = NS_OK;
155 : }
156 : else {
157 : // try to get the hostname from the nodename
158 : // on machines that use DHCP, domainname may not be set
159 : // but the nodename might.
160 0 : if (buf.nodename && buf.nodename[0]) {
161 : // if the nodename is foo.bar.org, use bar.org as the domain
162 0 : char *pos = strchr(buf.nodename,'.');
163 0 : if (pos) {
164 0 : *aDomain = nsCRT::strdup(pos+1);
165 0 : rv = NS_OK;
166 : }
167 : }
168 : }
169 :
170 0 : return rv;
171 : }
172 :
173 : NS_IMETHODIMP
174 0 : nsUserInfo::GetEmailAddress(char * *aEmailAddress)
175 : {
176 : // use username + "@" + domain for the email address
177 :
178 : nsresult rv;
179 :
180 0 : nsCAutoString emailAddress;
181 0 : nsXPIDLCString username;
182 0 : nsXPIDLCString domain;
183 :
184 0 : rv = GetUsername(getter_Copies(username));
185 0 : if (NS_FAILED(rv)) return rv;
186 :
187 0 : rv = GetDomain(getter_Copies(domain));
188 0 : if (NS_FAILED(rv)) return rv;
189 :
190 0 : if (!username.IsEmpty() && !domain.IsEmpty()) {
191 0 : emailAddress = (const char *)username;
192 0 : emailAddress += "@";
193 0 : emailAddress += (const char *)domain;
194 : }
195 : else {
196 0 : return NS_ERROR_FAILURE;
197 : }
198 :
199 0 : *aEmailAddress = ToNewCString(emailAddress);
200 :
201 0 : return NS_OK;
202 : }
203 :
|