ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/repos/CCSFinger/CCSFinger.cs
Revision: 718
Committed: 2006-03-30T02:50:17-08:00 (19 years, 3 months ago) by douglas
File size: 7892 byte(s)
Log Message:
Getting somewhere!

File Contents

# User Rev Content
1 douglas 710 // Douglas Thrift
2     //
3     // CCS Computer Science
4     //
5     // Windows Finger Daemon
6    
7     using System;
8     using System.Collections.Generic;
9     using System.IO;
10     using System.Net;
11     using System.Net.Sockets;
12     using System.ServiceProcess;
13     using System.Text;
14     using System.Threading;
15    
16     public class CCSFinger : ServiceBase
17     {
18     private class Finger
19     {
20     private class Login
21     {
22 douglas 718 private Net.USER_INFO_11 info;
23 douglas 710 private uint id;
24     private string session;
25 douglas 718 private string client;
26     private Wts.WTS_PROTOCOL_TYPE protocol;
27     private Wts.WTS_CONNECTSTATE_CLASS status;
28     private DateTime logon = DateTime.MinValue;
29 douglas 710
30 douglas 718 public Login(string login)
31 douglas 710 {
32 douglas 718 Net.UserGetInfo(null, login, 11, out info);
33 douglas 710
34 douglas 718 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 douglas 710 this.id = id;
42     this.session = session;
43 douglas 718 this.client = client;
44     this.protocol = protocol;
45     this.status = status;
46 douglas 710 }
47    
48 douglas 718 public string Client
49 douglas 710 {
50 douglas 718 get { return protocol == Wts.WTS_PROTOCOL_TYPE.WTS_PROTOCOL_TYPE_CONSOLE ? "the Console" : client; }
51 douglas 710 }
52    
53     public string Directory
54     {
55     get { return @"\\Zweihander\" + info.usri11_name; }
56     }
57    
58     public uint Id
59     {
60     get { return id; }
61     }
62    
63 douglas 718 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 douglas 710 public string Name
75     {
76     get { return info.usri11_full_name; }
77     }
78    
79     public string Session
80     {
81     get { return session; }
82     }
83 douglas 718
84     public string Status
85     {
86     get { return status.ToString().Substring(3); }
87     }
88 douglas 710 }
89    
90     private StringWriter writer = new StringWriter();
91     private SortedDictionary<string, List<Login>> logins = new SortedDictionary<string, List<Login>>();
92 douglas 718 private List<string> nots = new List<string>();
93 douglas 710
94     private void Full()
95     {
96 douglas 718 foreach (string not in nots)
97     writer.Write("finger: {0}: no such user\r\n", not);
98    
99 douglas 710 bool first = true;
100    
101     foreach (KeyValuePair<string, List<Login>> login in logins)
102     {
103     if (first)
104     first = true;
105     else
106     writer.Write("\r\n");
107    
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 douglas 718 if (login_.Id != uint.MaxValue)
112     writer.Write("{0} on {1}, from {2}\r\n", login_.Status, login_.Id, login_.Client);
113 douglas 710
114 douglas 718 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 douglas 710 string[] files = { ".project", ".plan" };
117    
118     foreach (string file in files)
119     try
120     {
121     StreamReader reader = new StreamReader(new FileStream(Directory.GetFiles(login.Value[0].Directory, file)[0], FileMode.Open, FileAccess.Read));
122    
123     switch (file)
124     {
125     case ".project":
126     writer.Write("Project:\r\n");
127     break;
128     case ".plan":
129     writer.Write("Plan:\r\n");
130     break;
131     }
132    
133     writer.Write("{0}\r\n", reader.ReadToEnd());
134     }
135     catch (IndexOutOfRangeException) {}
136     }
137     }
138    
139 douglas 718 private void Sessions(IDictionary<string, List<Login>> logins)
140 douglas 710 {
141 douglas 718 Wts.WTS_SESSION_INFO[] sessions;
142     uint count;
143 douglas 710
144 douglas 718 Wts.EnumerateSessions(IntPtr.Zero, 0, 1, out sessions, out count);
145 douglas 710
146     for (uint index = 0; index != count; ++index)
147     {
148 douglas 718 string name;
149     uint size;
150 douglas 710
151 douglas 718 Wts.QuerySessionInformation(IntPtr.Zero, sessions[index].SessionId, Wts.WTS_INFO_CLASS.WTSUserName, out name, out size);
152 douglas 710
153     if (name.Length > 0)
154     {
155 douglas 718 string session, client;
156     Wts.WTS_PROTOCOL_TYPE protocol;
157     Wts.WTS_CONNECTSTATE_CLASS status;
158 douglas 710
159 douglas 718 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 douglas 710
164     if (!logins.ContainsKey(name))
165     logins.Add(string.Copy(name), new List<Login>());
166    
167 douglas 718 logins[name].Add(new Login(name, sessions[index].SessionId, session, client, protocol, status));
168 douglas 710 }
169     }
170 douglas 718 }
171 douglas 710
172 douglas 718 public Finger(bool full)
173     {
174     Sessions(logins);
175 douglas 710
176     if (logins.Count < 1)
177     writer.Write("No one logged on.\r\n");
178     else if (full)
179     Full();
180     else
181     {
182     writer.Write("Login Name Id Session Status\r\n");
183    
184     foreach (KeyValuePair<string, List<Login>> login_ in logins)
185     foreach (Login login in login_.Value)
186 douglas 718 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 douglas 710 }
188     }
189    
190     public Finger(ICollection<string> names)
191     {
192 douglas 718 uint count;
193     Net.NET_DISPLAY_USER[] users;
194 douglas 710
195 douglas 718 Net.QueryDisplayInformation(null, 1, 0, 100, uint.MaxValue, out count, out users);
196 douglas 710
197 douglas 718 Dictionary<string, List<Login>> logins_ = new Dictionary<string, List<Login>>();
198    
199     Sessions(logins_);
200    
201 douglas 710 foreach (string name in names)
202 douglas 718 {
203     bool not = true;
204    
205 douglas 710 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 douglas 718 logins.Add(users[index].usri1_name, new List<Login>());
209 douglas 710
210 douglas 718 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 douglas 710 }
217    
218 douglas 718 if (not)
219     nots.Add(name);
220     }
221 douglas 710
222     Full();
223     }
224    
225     public override string ToString()
226     {
227     return writer.ToString();
228     }
229     }
230    
231     /*private static void Do()
232     {
233     TcpListener listener = null;
234    
235     try
236     {
237     listener = new TcpListener(IPAddress.Any, 79);
238    
239     listener.Start();
240    
241     while (true)
242     {
243     TcpClient client = listener.AcceptTcpClient();
244     Thread thread = new Thread(new ParameterizedThreadStart(Do));
245    
246     thread.Start(client);
247     }
248     }
249     catch (SocketException exception)
250     {
251     Console.Error.WriteLine(exception);
252     }
253     finally
254     {
255     listener.Stop();
256     }
257     }*/
258    
259     private static void Do(object client)
260     {
261     /*NetworkStream stream = ((TcpClient)client).GetStream();
262     byte[] buffer = new byte[1024];
263     int size = 0, byte_;
264    
265     while (size != buffer.Length && (byte_ = stream.ReadByte()) != '\r' && byte_ != '\n' && byte_ != -1)
266     buffer[size++] = (byte)byte_;*/
267    
268     string line = /*Encoding.ASCII.GetString(buffer, 0, size)*/ Console.ReadLine();
269     List<string> names = new List<string>();
270     bool full = false;
271    
272     foreach (string name in line.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries))
273     {
274     if (name == "/W")
275     full = true;
276     else
277     names.Add(name);
278    
279     Console.WriteLine(name);
280     }
281    
282     //StreamWriter writer = new StreamWriter(stream, new UTF8Encoding(false));
283    
284     /*writer*/Console.Write(names.Count > 0 ? new Finger(names) : new Finger(full));
285     //writer.Close();
286     //stream.Close();
287     //((TcpClient)client).Close();
288     }
289    
290     public static int Main(string[] args)
291     {
292     Do(null);
293    
294     return 0;
295     }
296     }