ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/repos/CCSFinger/Finger.cs
Revision: 729
Committed: 2006-03-31T08:51:06-08:00 (19 years, 2 months ago) by douglas
File size: 5288 byte(s)
Log Message:
Refactor! And handle more than half a year ago last login.

File Contents

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