1 |
douglas |
725 |
// Douglas Thrift |
2 |
|
|
// |
3 |
|
|
// CCS Computer Science |
4 |
|
|
// |
5 |
|
|
// Finger |
6 |
|
|
|
7 |
|
|
using System; |
8 |
|
|
using System.Collections.Generic; |
9 |
|
|
using System.IO; |
10 |
|
|
|
11 |
|
|
public class Finger |
12 |
|
|
{ |
13 |
|
|
private StreamWriter writer; |
14 |
|
|
private SortedDictionary<string, List<Login>> logins = new SortedDictionary<string, List<Login>>(); |
15 |
|
|
private bool forward = false; |
16 |
|
|
private List<string> nots = new List<string>(); |
17 |
|
|
|
18 |
|
|
private void Full() |
19 |
|
|
{ |
20 |
|
|
if (forward) |
21 |
|
|
writer.Write("finger: forwarding service denied\r\n"); |
22 |
|
|
|
23 |
|
|
foreach (string not in nots) |
24 |
|
|
writer.Write("finger: {0}: no such user\r\n", not); |
25 |
|
|
|
26 |
|
|
bool first = true; |
27 |
|
|
|
28 |
|
|
foreach (KeyValuePair<string, List<Login>> login in logins) |
29 |
|
|
{ |
30 |
|
|
if (first) |
31 |
|
|
first = false; |
32 |
|
|
else |
33 |
|
|
writer.Write("\r\n"); |
34 |
|
|
|
35 |
|
|
writer.Write("Login: {0,-32} Name: {1}\r\nDirectory: {2}\r\n", login.Key, login.Value[0].Name, login.Value[0].Directory); |
36 |
|
|
|
37 |
|
|
foreach (Login login_ in login.Value) |
38 |
|
|
if (login_.Id != uint.MaxValue) |
39 |
|
|
writer.Write("{0} on {1}, from {2}\r\n", login_.Status, login_.Id, login_.Client); |
40 |
|
|
|
41 |
|
|
if (login.Value[0].LastLogon != new DateTime(1970, 1, 1)) |
42 |
|
|
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"); |
43 |
|
|
else |
44 |
|
|
writer.Write("Never logged in.\r\n"); |
45 |
|
|
|
46 |
|
|
writer.Write("No Mail.\r\n"); |
47 |
|
|
|
48 |
|
|
string[] files = { ".project", ".plan" }; |
49 |
|
|
|
50 |
|
|
foreach (string file in files) |
51 |
|
|
try |
52 |
|
|
{ |
53 |
|
|
StreamReader reader = new StreamReader(new FileStream(Directory.GetFiles(login.Value[0].Directory, file)[0], FileMode.Open, FileAccess.Read)); |
54 |
|
|
|
55 |
|
|
switch (file) |
56 |
|
|
{ |
57 |
|
|
case ".project": |
58 |
|
|
writer.Write("Project:\r\n"); |
59 |
|
|
break; |
60 |
|
|
case ".plan": |
61 |
|
|
writer.Write("Plan:\r\n"); |
62 |
|
|
break; |
63 |
|
|
} |
64 |
|
|
|
65 |
|
|
writer.Write("{0}", reader.ReadToEnd()); |
66 |
|
|
} |
67 |
|
|
catch (Exception) |
68 |
|
|
{ |
69 |
|
|
if (file == ".plan") |
70 |
|
|
writer.Write("No Plan.\r\n"); |
71 |
|
|
} |
72 |
|
|
} |
73 |
|
|
} |
74 |
|
|
|
75 |
|
|
private void Sessions(IDictionary<string, List<Login>> logins) |
76 |
|
|
{ |
77 |
|
|
Wts.WTS_SESSION_INFO[] sessions; |
78 |
|
|
uint count; |
79 |
|
|
|
80 |
|
|
Wts.EnumerateSessions(IntPtr.Zero, 0, 1, out sessions, out count); |
81 |
|
|
|
82 |
|
|
for (uint index = 0; index != count; ++index) |
83 |
|
|
{ |
84 |
|
|
string name; |
85 |
|
|
uint size; |
86 |
|
|
|
87 |
|
|
Wts.QuerySessionInformation(IntPtr.Zero, sessions[index].SessionId, Wts.WTS_INFO_CLASS.WTSUserName, out name, out size); |
88 |
|
|
|
89 |
|
|
if (name.Length > 0) |
90 |
|
|
{ |
91 |
|
|
string session, client; |
92 |
|
|
Wts.WTS_PROTOCOL_TYPE protocol; |
93 |
|
|
Wts.WTS_CONNECTSTATE_CLASS status; |
94 |
|
|
|
95 |
|
|
Wts.QuerySessionInformation(IntPtr.Zero, sessions[index].SessionId, Wts.WTS_INFO_CLASS.WTSWinStationName, out session, out size); |
96 |
|
|
Wts.QuerySessionInformation(IntPtr.Zero, sessions[index].SessionId, Wts.WTS_INFO_CLASS.WTSClientName, out client, out size); |
97 |
|
|
Wts.QuerySessionInformation(IntPtr.Zero, sessions[index].SessionId, Wts.WTS_INFO_CLASS.WTSClientProtocolType, out protocol, out size); |
98 |
|
|
Wts.QuerySessionInformation(IntPtr.Zero, sessions[index].SessionId, Wts.WTS_INFO_CLASS.WTSConnectState, out status, out size); |
99 |
|
|
|
100 |
|
|
if (!logins.ContainsKey(name)) |
101 |
|
|
logins.Add(string.Copy(name), new List<Login>()); |
102 |
|
|
|
103 |
|
|
logins[name].Add(new Login(name, sessions[index].SessionId, session, client, protocol, status)); |
104 |
|
|
} |
105 |
|
|
} |
106 |
|
|
} |
107 |
|
|
|
108 |
|
|
public Finger(StreamWriter writer, bool full) |
109 |
|
|
{ |
110 |
|
|
this.writer = writer; |
111 |
|
|
|
112 |
|
|
Sessions(logins); |
113 |
|
|
|
114 |
|
|
if (logins.Count < 1) |
115 |
|
|
writer.Write("No one logged on.\r\n"); |
116 |
|
|
else if (full) |
117 |
|
|
Full(); |
118 |
|
|
else |
119 |
|
|
{ |
120 |
|
|
writer.Write("Login Name Id Session Status\r\n"); |
121 |
|
|
|
122 |
|
|
foreach (KeyValuePair<string, List<Login>> login_ in logins) |
123 |
|
|
foreach (Login login in login_.Value) |
124 |
|
|
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); |
125 |
|
|
} |
126 |
|
|
} |
127 |
|
|
|
128 |
|
|
public Finger(StreamWriter writer, ICollection<string> names) |
129 |
|
|
{ |
130 |
|
|
this.writer = writer; |
131 |
|
|
|
132 |
|
|
uint count; |
133 |
|
|
Net.NET_DISPLAY_USER[] users; |
134 |
|
|
|
135 |
|
|
Net.QueryDisplayInformation(null, 1, 0, 100, uint.MaxValue, out count, out users); |
136 |
|
|
|
137 |
|
|
Dictionary<string, List<Login>> logins_ = new Dictionary<string, List<Login>>(); |
138 |
|
|
|
139 |
|
|
Sessions(logins_); |
140 |
|
|
|
141 |
|
|
foreach (string name in names) |
142 |
|
|
if (name.Contains("@")) |
143 |
|
|
forward = true; |
144 |
|
|
else |
145 |
|
|
{ |
146 |
|
|
bool not = true; |
147 |
|
|
|
148 |
|
|
for (uint index = 0; index != count; ++index) |
149 |
|
|
if (users[index].usri1_name.IndexOf(name, StringComparison.OrdinalIgnoreCase) != -1 || users[index].usri1_full_name.IndexOf(name, StringComparison.OrdinalIgnoreCase) != -1) |
150 |
|
|
try |
151 |
|
|
{ |
152 |
|
|
logins.Add(users[index].usri1_name, new List<Login>()); |
153 |
|
|
|
154 |
|
|
logins[users[index].usri1_name].Add(new Login(users[index].usri1_name)); |
155 |
|
|
|
156 |
|
|
if (logins_.ContainsKey(users[index].usri1_name)) |
157 |
|
|
logins[users[index].usri1_name].AddRange(logins_[users[index].usri1_name]); |
158 |
|
|
} |
159 |
|
|
catch (ArgumentException) { } |
160 |
|
|
finally |
161 |
|
|
{ |
162 |
|
|
not = false; |
163 |
|
|
} |
164 |
|
|
|
165 |
|
|
if (not) |
166 |
|
|
nots.Add(name); |
167 |
|
|
} |
168 |
|
|
|
169 |
|
|
Full(); |
170 |
|
|
} |
171 |
|
|
} |