1 |
< |
// Douglas Thrift |
1 |
> |
// Douglas Thrift |
2 |
|
// |
3 |
|
// CCS Computer Science |
4 |
|
// |
19 |
|
{ |
20 |
|
private class Login |
21 |
|
{ |
22 |
< |
private Net.USER_INFO_11 info = null; |
22 |
> |
private Net.USER_INFO_11 info; |
23 |
|
private uint id; |
24 |
|
private string session; |
25 |
+ |
private string client; |
26 |
+ |
private Wts.WTS_PROTOCOL_TYPE protocol; |
27 |
+ |
private Wts.WTS_CONNECTSTATE_CLASS status; |
28 |
+ |
private DateTime logon = DateTime.MinValue; |
29 |
|
|
30 |
< |
public Login(string login, uint id, string session) |
30 |
> |
public Login(string login) |
31 |
|
{ |
32 |
< |
Net.UserGetInfo(null, login, 11, info); |
32 |
> |
Net.UserGetInfo(null, login, 11, out info); |
33 |
> |
|
34 |
> |
this.id = uint.MaxValue; |
35 |
> |
} |
36 |
> |
|
37 |
> |
public Login(string login, uint id, string session, string client, Wts.WTS_PROTOCOL_TYPE protocol, Wts.WTS_CONNECTSTATE_CLASS status) |
38 |
> |
{ |
39 |
> |
Net.UserGetInfo(null, login, 11, out info); |
40 |
|
|
41 |
|
this.id = id; |
42 |
|
this.session = session; |
43 |
+ |
this.client = client; |
44 |
+ |
this.protocol = protocol; |
45 |
+ |
this.status = status; |
46 |
|
} |
47 |
|
|
48 |
< |
~Login() |
48 |
> |
public string Client |
49 |
|
{ |
50 |
< |
Net.ApiBufferFree(info); |
50 |
> |
get { return protocol == Wts.WTS_PROTOCOL_TYPE.WTS_PROTOCOL_TYPE_CONSOLE ? "the Console" : client; } |
51 |
|
} |
52 |
|
|
53 |
|
public string Directory |
60 |
|
get { return id; } |
61 |
|
} |
62 |
|
|
63 |
+ |
public DateTime LastLogon |
64 |
+ |
{ |
65 |
+ |
get |
66 |
+ |
{ |
67 |
+ |
if (logon == DateTime.MinValue) |
68 |
+ |
logon = new DateTime(1970, 1, 1) + new TimeSpan((long)info.usri11_last_logon * 10000000); |
69 |
+ |
|
70 |
+ |
return logon; |
71 |
+ |
} |
72 |
+ |
} |
73 |
+ |
|
74 |
|
public string Name |
75 |
|
{ |
76 |
|
get { return info.usri11_full_name; } |
80 |
|
{ |
81 |
|
get { return session; } |
82 |
|
} |
83 |
+ |
|
84 |
+ |
public string Status |
85 |
+ |
{ |
86 |
+ |
get { return status.ToString().Substring(3); } |
87 |
+ |
} |
88 |
|
} |
89 |
|
|
90 |
|
private StringWriter writer = new StringWriter(); |
91 |
|
private SortedDictionary<string, List<Login>> logins = new SortedDictionary<string, List<Login>>(); |
92 |
+ |
private List<string> nots = new List<string>(); |
93 |
|
|
94 |
|
private void Full() |
95 |
|
{ |
96 |
+ |
foreach (string not in nots) |
97 |
+ |
writer.Write("finger: {0}: no such user\r\n", not); |
98 |
+ |
|
99 |
|
bool first = true; |
100 |
|
|
101 |
|
foreach (KeyValuePair<string, List<Login>> login in logins) |
108 |
|
writer.Write("Login: {0,-32} Name: {1}\r\nDirectory: {2}\r\n", login.Key, login.Value[0].Name, login.Value[0].Directory); |
109 |
|
|
110 |
|
foreach (Login login_ in login.Value) |
111 |
< |
writer.Write("{} {} {}\r\n"); |
111 |
> |
if (login_.Id != uint.MaxValue) |
112 |
> |
writer.Write("{0} on {1}, from {2}\r\n", login_.Status, login_.Id, login_.Client); |
113 |
> |
|
114 |
> |
writer.Write("Last login {0:ddd MMM dd HH:mm} ({1})\r\n", login.Value[0].LastLogon.ToLocalTime(), login.Value[0].LastLogon.ToLocalTime().IsDaylightSavingTime() ? "PDT" : "PST"); |
115 |
|
|
116 |
|
string[] files = { ".project", ".plan" }; |
117 |
|
|
136 |
|
} |
137 |
|
} |
138 |
|
|
139 |
< |
public Finger(bool full) |
139 |
> |
private void Sessions(IDictionary<string, List<Login>> logins) |
140 |
|
{ |
141 |
< |
Wts.WTS_SESSION_INFO[] sessions = null; |
142 |
< |
uint count = 0; |
141 |
> |
Wts.WTS_SESSION_INFO[] sessions; |
142 |
> |
uint count; |
143 |
|
|
144 |
< |
Wts.EnumerateSessions(IntPtr.Zero, 0, 1, sessions, count); |
144 |
> |
Wts.EnumerateSessions(IntPtr.Zero, 0, 1, out sessions, out count); |
145 |
|
|
146 |
|
for (uint index = 0; index != count; ++index) |
147 |
|
{ |
148 |
< |
string name = null; |
149 |
< |
uint size = 0; |
148 |
> |
string name; |
149 |
> |
uint size; |
150 |
|
|
151 |
< |
Wts.QuerySessionInformation(IntPtr.Zero, sessions[index].SessionId, Wts.WTS_INFO_CLASS.WTSUserName, name, size); |
151 |
> |
Wts.QuerySessionInformation(IntPtr.Zero, sessions[index].SessionId, Wts.WTS_INFO_CLASS.WTSUserName, out name, out size); |
152 |
|
|
153 |
|
if (name.Length > 0) |
154 |
|
{ |
155 |
< |
string session = null; |
156 |
< |
|
157 |
< |
Wts.QuerySessionInformation(IntPtr.Zero, sessions[index].SessionId, Wts.WTS_INFO_CLASS.WTSWinStationName, session, size); |
155 |
> |
string session, client; |
156 |
> |
Wts.WTS_PROTOCOL_TYPE protocol; |
157 |
> |
Wts.WTS_CONNECTSTATE_CLASS status; |
158 |
> |
|
159 |
> |
Wts.QuerySessionInformation(IntPtr.Zero, sessions[index].SessionId, Wts.WTS_INFO_CLASS.WTSWinStationName, out session, out size); |
160 |
> |
Wts.QuerySessionInformation(IntPtr.Zero, sessions[index].SessionId, Wts.WTS_INFO_CLASS.WTSClientName, out client, out size); |
161 |
> |
Wts.QuerySessionInformation(IntPtr.Zero, sessions[index].SessionId, Wts.WTS_INFO_CLASS.WTSClientProtocolType, out protocol, out size); |
162 |
> |
Wts.QuerySessionInformation(IntPtr.Zero, sessions[index].SessionId, Wts.WTS_INFO_CLASS.WTSConnectState, out status, out size); |
163 |
|
|
164 |
|
if (!logins.ContainsKey(name)) |
165 |
|
logins.Add(string.Copy(name), new List<Login>()); |
124 |
– |
|
125 |
– |
logins[name].Add(new Login(name, sessions[index].SessionId, string.Copy(session))); |
166 |
|
|
167 |
< |
Wts.FreeMemory(session); |
167 |
> |
logins[name].Add(new Login(name, sessions[index].SessionId, session, client, protocol, status)); |
168 |
|
} |
129 |
– |
|
130 |
– |
Wts.FreeMemory(name); |
169 |
|
} |
170 |
+ |
} |
171 |
|
|
172 |
< |
Wts.FreeMemory(sessions); |
172 |
> |
public Finger(bool full) |
173 |
> |
{ |
174 |
> |
Sessions(logins); |
175 |
|
|
176 |
|
if (logins.Count < 1) |
177 |
|
writer.Write("No one logged on.\r\n"); |
183 |
|
|
184 |
|
foreach (KeyValuePair<string, List<Login>> login_ in logins) |
185 |
|
foreach (Login login in login_.Value) |
186 |
< |
writer.Write("{0,-15} {1,-20} {2,-6} {3,-13} {4}\r\n", login_.Key, login.Name, login.Id, login.Session, "Active"); |
186 |
> |
writer.Write("{0,-15} {1,-20} {2,-6} {3,-13} {4}\r\n", login_.Key, login.Name.Length > 20 ? login.Name.Substring(0, 20) : login.Name, login.Id, login.Session, login.Status); |
187 |
|
} |
188 |
|
} |
189 |
|
|
190 |
|
public Finger(ICollection<string> names) |
191 |
|
{ |
192 |
< |
uint count = 0; |
193 |
< |
Net.NET_DISPLAY_USER[] users = null; |
192 |
> |
uint count; |
193 |
> |
Net.NET_DISPLAY_USER[] users; |
194 |
|
|
195 |
< |
Net.QueryDisplayInformation(null, 1, 0, 100, uint.MaxValue, count, users); |
195 |
> |
Net.QueryDisplayInformation(null, 1, 0, 100, uint.MaxValue, out count, out users); |
196 |
> |
|
197 |
> |
Dictionary<string, List<Login>> logins_ = new Dictionary<string, List<Login>>(); |
198 |
> |
|
199 |
> |
Sessions(logins_); |
200 |
|
|
201 |
|
foreach (string name in names) |
202 |
+ |
{ |
203 |
+ |
bool not = true; |
204 |
+ |
|
205 |
|
for (uint index = 0; index != count; ++index) |
206 |
|
if (users[index].usri1_name.IndexOf(name, StringComparison.OrdinalIgnoreCase) != -1 || users[index].usri1_full_name.IndexOf(name, StringComparison.OrdinalIgnoreCase) != -1 && !logins.ContainsKey(users[index].usri1_name)) |
207 |
|
{ |
208 |
< |
logins.Add(string.Copy(users[index].usri1_name), new List<Login>()); |
208 |
> |
logins.Add(users[index].usri1_name, new List<Login>()); |
209 |
|
|
210 |
< |
// |
210 |
> |
logins[users[index].usri1_name].Add(new Login(users[index].usri1_name)); |
211 |
> |
|
212 |
> |
if (logins_.ContainsKey(users[index].usri1_name)) |
213 |
> |
logins[users[index].usri1_name].AddRange(logins_[users[index].usri1_name]); |
214 |
> |
|
215 |
> |
not = false; |
216 |
|
} |
217 |
|
|
218 |
< |
Net.ApiBufferFree(users); |
218 |
> |
if (not) |
219 |
> |
nots.Add(name); |
220 |
> |
} |
221 |
|
|
222 |
|
Full(); |
223 |
|
} |