ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/repos/CCSFinger/CCSFinger.cs
Revision: 710
Committed: 2006-03-24T01:37:37-08:00 (19 years, 3 months ago) by douglas
File size: 5680 byte(s)
Log Message:
I decided to do this in C#, it still has issues right now, and is currently working less than before. SuX0r!

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     private Net.USER_INFO_11 info = null;
23     private uint id;
24     private string session;
25    
26     public Login(string login, uint id, string session)
27     {
28     Net.UserGetInfo(null, login, 11, info);
29    
30     this.id = id;
31     this.session = session;
32     }
33    
34     ~Login()
35     {
36     Net.ApiBufferFree(info);
37     }
38    
39     public string Directory
40     {
41     get { return @"\\Zweihander\" + info.usri11_name; }
42     }
43    
44     public uint Id
45     {
46     get { return id; }
47     }
48    
49     public string Name
50     {
51     get { return info.usri11_full_name; }
52     }
53    
54     public string Session
55     {
56     get { return session; }
57     }
58     }
59    
60     private StringWriter writer = new StringWriter();
61     private SortedDictionary<string, List<Login>> logins = new SortedDictionary<string, List<Login>>();
62    
63     private void Full()
64     {
65     bool first = true;
66    
67     foreach (KeyValuePair<string, List<Login>> login in logins)
68     {
69     if (first)
70     first = true;
71     else
72     writer.Write("\r\n");
73    
74     writer.Write("Login: {0,-32} Name: {1}\r\nDirectory: {2}\r\n", login.Key, login.Value[0].Name, login.Value[0].Directory);
75    
76     foreach (Login login_ in login.Value)
77     writer.Write("{} {} {}\r\n");
78    
79     string[] files = { ".project", ".plan" };
80    
81     foreach (string file in files)
82     try
83     {
84     StreamReader reader = new StreamReader(new FileStream(Directory.GetFiles(login.Value[0].Directory, file)[0], FileMode.Open, FileAccess.Read));
85    
86     switch (file)
87     {
88     case ".project":
89     writer.Write("Project:\r\n");
90     break;
91     case ".plan":
92     writer.Write("Plan:\r\n");
93     break;
94     }
95    
96     writer.Write("{0}\r\n", reader.ReadToEnd());
97     }
98     catch (IndexOutOfRangeException) {}
99     }
100     }
101    
102     public Finger(bool full)
103     {
104     Wts.WTS_SESSION_INFO[] sessions = null;
105     uint count = 0;
106    
107     Wts.EnumerateSessions(IntPtr.Zero, 0, 1, sessions, count);
108    
109     for (uint index = 0; index != count; ++index)
110     {
111     string name = null;
112     uint size = 0;
113    
114     Wts.QuerySessionInformation(IntPtr.Zero, sessions[index].SessionId, Wts.WTS_INFO_CLASS.WTSUserName, name, size);
115    
116     if (name.Length > 0)
117     {
118     string session = null;
119    
120     Wts.QuerySessionInformation(IntPtr.Zero, sessions[index].SessionId, Wts.WTS_INFO_CLASS.WTSWinStationName, session, size);
121    
122     if (!logins.ContainsKey(name))
123     logins.Add(string.Copy(name), new List<Login>());
124    
125     logins[name].Add(new Login(name, sessions[index].SessionId, string.Copy(session)));
126    
127     Wts.FreeMemory(session);
128     }
129    
130     Wts.FreeMemory(name);
131     }
132    
133     Wts.FreeMemory(sessions);
134    
135     if (logins.Count < 1)
136     writer.Write("No one logged on.\r\n");
137     else if (full)
138     Full();
139     else
140     {
141     writer.Write("Login Name Id Session Status\r\n");
142    
143     foreach (KeyValuePair<string, List<Login>> login_ in logins)
144     foreach (Login login in login_.Value)
145     writer.Write("{0,-15} {1,-20} {2,-6} {3,-13} {4}\r\n", login_.Key, login.Name, login.Id, login.Session, "Active");
146     }
147     }
148    
149     public Finger(ICollection<string> names)
150     {
151     uint count = 0;
152     Net.NET_DISPLAY_USER[] users = null;
153    
154     Net.QueryDisplayInformation(null, 1, 0, 100, uint.MaxValue, count, users);
155    
156     foreach (string name in names)
157     for (uint index = 0; index != count; ++index)
158     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))
159     {
160     logins.Add(string.Copy(users[index].usri1_name), new List<Login>());
161    
162     //
163     }
164    
165     Net.ApiBufferFree(users);
166    
167     Full();
168     }
169    
170     public override string ToString()
171     {
172     return writer.ToString();
173     }
174     }
175    
176     /*private static void Do()
177     {
178     TcpListener listener = null;
179    
180     try
181     {
182     listener = new TcpListener(IPAddress.Any, 79);
183    
184     listener.Start();
185    
186     while (true)
187     {
188     TcpClient client = listener.AcceptTcpClient();
189     Thread thread = new Thread(new ParameterizedThreadStart(Do));
190    
191     thread.Start(client);
192     }
193     }
194     catch (SocketException exception)
195     {
196     Console.Error.WriteLine(exception);
197     }
198     finally
199     {
200     listener.Stop();
201     }
202     }*/
203    
204     private static void Do(object client)
205     {
206     /*NetworkStream stream = ((TcpClient)client).GetStream();
207     byte[] buffer = new byte[1024];
208     int size = 0, byte_;
209    
210     while (size != buffer.Length && (byte_ = stream.ReadByte()) != '\r' && byte_ != '\n' && byte_ != -1)
211     buffer[size++] = (byte)byte_;*/
212    
213     string line = /*Encoding.ASCII.GetString(buffer, 0, size)*/ Console.ReadLine();
214     List<string> names = new List<string>();
215     bool full = false;
216    
217     foreach (string name in line.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries))
218     {
219     if (name == "/W")
220     full = true;
221     else
222     names.Add(name);
223    
224     Console.WriteLine(name);
225     }
226    
227     //StreamWriter writer = new StreamWriter(stream, new UTF8Encoding(false));
228    
229     /*writer*/Console.Write(names.Count > 0 ? new Finger(names) : new Finger(full));
230     //writer.Close();
231     //stream.Close();
232     //((TcpClient)client).Close();
233     }
234    
235     public static int Main(string[] args)
236     {
237     Do(null);
238    
239     return 0;
240     }
241     }