ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/repos/CCSFinger/CCSFinger.cs
(Generate patch)

Comparing CCSFinger/CCSFinger.cs (file contents):
Revision 710 by douglas, 2006-03-24T01:37:37-08:00 vs.
Revision 720 by douglas, 2006-03-30T04:25:10-08:00

# Line 1 | Line 1
1 < // Douglas Thrift
1 > // Douglas Thrift
2   //
3   // CCS Computer Science
4   //
# Line 19 | Line 19 | public class CCSFinger : ServiceBase
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
# Line 46 | Line 60 | public class CCSFinger : ServiceBase
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; }
# Line 55 | Line 80 | public class CCSFinger : ServiceBase
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)
102                          {
103                                  if (first)
104 <                                        first = true;
104 >                                        first = false;
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 <                                        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 >                                if (login.Value[0].LastLogon != new DateTime(1970, 1, 1))
115 >                                        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");
116 >                                else
117 >                                        writer.Write("Never logged in.\r\n");
118 >
119 >                                writer.Write("No Mail.\r\n");
120  
121                                  string[] files = { ".project", ".plan" };
122  
# Line 93 | Line 135 | public class CCSFinger : ServiceBase
135                                                                  break;
136                                                  }
137  
138 <                                                writer.Write("{0}\r\n", reader.ReadToEnd());
138 >                                                writer.Write("{0}", reader.ReadToEnd());
139 >                                        }
140 >                                        catch (Exception)
141 >                                        {
142 >                                                if (file == ".plan")
143 >                                                        writer.Write("No Plan.\r\n");
144                                          }
98                                        catch (IndexOutOfRangeException) {}
145                          }
146                  }
147  
148 <                public Finger(bool full)
148 >                private void Sessions(IDictionary<string, List<Login>> logins)
149                  {
150 <                        Wts.WTS_SESSION_INFO[] sessions = null;
151 <                        uint count = 0;
150 >                        Wts.WTS_SESSION_INFO[] sessions;
151 >                        uint count;
152  
153 <                        Wts.EnumerateSessions(IntPtr.Zero, 0, 1, sessions, count);
153 >                        Wts.EnumerateSessions(IntPtr.Zero, 0, 1, out sessions, out count);
154  
155                          for (uint index = 0; index != count; ++index)
156                          {
157 <                                string name = null;
158 <                                uint size = 0;
157 >                                string name;
158 >                                uint size;
159  
160 <                                Wts.QuerySessionInformation(IntPtr.Zero, sessions[index].SessionId, Wts.WTS_INFO_CLASS.WTSUserName, name, size);
160 >                                Wts.QuerySessionInformation(IntPtr.Zero, sessions[index].SessionId, Wts.WTS_INFO_CLASS.WTSUserName, out name, out size);
161  
162                                  if (name.Length > 0)
163                                  {
164 <                                        string session = null;
165 <
166 <                                        Wts.QuerySessionInformation(IntPtr.Zero, sessions[index].SessionId, Wts.WTS_INFO_CLASS.WTSWinStationName, session, size);
164 >                                        string session, client;
165 >                                        Wts.WTS_PROTOCOL_TYPE protocol;
166 >                                        Wts.WTS_CONNECTSTATE_CLASS status;
167 >
168 >                                        Wts.QuerySessionInformation(IntPtr.Zero, sessions[index].SessionId, Wts.WTS_INFO_CLASS.WTSWinStationName, out session, out size);
169 >                                        Wts.QuerySessionInformation(IntPtr.Zero, sessions[index].SessionId, Wts.WTS_INFO_CLASS.WTSClientName, out client, out size);
170 >                                        Wts.QuerySessionInformation(IntPtr.Zero, sessions[index].SessionId, Wts.WTS_INFO_CLASS.WTSClientProtocolType, out protocol, out size);
171 >                                        Wts.QuerySessionInformation(IntPtr.Zero, sessions[index].SessionId, Wts.WTS_INFO_CLASS.WTSConnectState, out status, out size);
172  
173                                          if (!logins.ContainsKey(name))
174                                                  logins.Add(string.Copy(name), new List<Login>());
124                                        
125                                        logins[name].Add(new Login(name, sessions[index].SessionId, string.Copy(session)));
175  
176 <                                        Wts.FreeMemory(session);
176 >                                        logins[name].Add(new Login(name, sessions[index].SessionId, session, client, protocol, status));
177                                  }
129
130                                Wts.FreeMemory(name);
178                          }
179 +                }
180  
181 <                        Wts.FreeMemory(sessions);
181 >                public Finger(bool full)
182 >                {
183 >                        Sessions(logins);
184  
185                          if (logins.Count < 1)
186                                  writer.Write("No one logged on.\r\n");
# Line 142 | Line 192 | public class CCSFinger : ServiceBase
192  
193                                  foreach (KeyValuePair<string, List<Login>> login_ in logins)
194                                          foreach (Login login in login_.Value)
195 <                                                writer.Write("{0,-15} {1,-20} {2,-6} {3,-13} {4}\r\n", login_.Key, login.Name, login.Id, login.Session, "Active");
195 >                                                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);
196                          }
197                  }
198  
199                  public Finger(ICollection<string> names)
200                  {
201 <                        uint count = 0;
202 <                        Net.NET_DISPLAY_USER[] users = null;
201 >                        uint count;
202 >                        Net.NET_DISPLAY_USER[] users;
203 >
204 >                        Net.QueryDisplayInformation(null, 1, 0, 100, uint.MaxValue, out count, out users);
205  
206 <                        Net.QueryDisplayInformation(null, 1, 0, 100, uint.MaxValue, count, users);
206 >                        Dictionary<string, List<Login>> logins_ = new Dictionary<string, List<Login>>();
207 >
208 >                        Sessions(logins_);
209  
210                          foreach (string name in names)
211 +                        {
212 +                                bool not = true;
213 +
214                                  for (uint index = 0; index != count; ++index)
215                                          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))
216                                          {
217 <                                                logins.Add(string.Copy(users[index].usri1_name), new List<Login>());
217 >                                                logins.Add(users[index].usri1_name, new List<Login>());
218 >
219 >                                                logins[users[index].usri1_name].Add(new Login(users[index].usri1_name));
220  
221 <                                                //
221 >                                                if (logins_.ContainsKey(users[index].usri1_name))
222 >                                                        logins[users[index].usri1_name].AddRange(logins_[users[index].usri1_name]);
223 >
224 >                                                not = false;
225                                          }
226  
227 <                        Net.ApiBufferFree(users);
227 >                                if (not)
228 >                                        nots.Add(name);
229 >                        }
230  
231                          Full();
232                  }
# Line 173 | Line 237 | public class CCSFinger : ServiceBase
237                  }
238          }
239  
240 <        /*private static void Do()
240 >        private static void Do()
241          {
242                  TcpListener listener = null;
243  
# Line 199 | Line 263 | public class CCSFinger : ServiceBase
263                  {
264                          listener.Stop();
265                  }
266 <        }*/
266 >        }
267  
268          private static void Do(object client)
269          {
270 <                /*NetworkStream stream = ((TcpClient)client).GetStream();
270 >                NetworkStream stream = ((TcpClient)client).GetStream();
271                  byte[] buffer = new byte[1024];
272                  int size = 0, byte_;
273  
274                  while (size != buffer.Length && (byte_ = stream.ReadByte()) != '\r' && byte_ != '\n' && byte_ != -1)
275 <                        buffer[size++] = (byte)byte_;*/
275 >                        buffer[size++] = (byte)byte_;
276  
277 <                string line = /*Encoding.ASCII.GetString(buffer, 0, size)*/ Console.ReadLine();
277 >                string line = Encoding.ASCII.GetString(buffer, 0, size)/*Console.ReadLine()*/;
278                  List<string> names = new List<string>();
279                  bool full = false;
280  
# Line 220 | Line 284 | public class CCSFinger : ServiceBase
284                                  full = true;
285                          else
286                                  names.Add(name);
223
224                        Console.WriteLine(name);
287                  }
288  
289 <                //StreamWriter writer = new StreamWriter(stream, new UTF8Encoding(false));
289 >                StreamWriter writer = new StreamWriter(stream, new UTF8Encoding(false));
290  
291 <                /*writer*/Console.Write(names.Count > 0 ? new Finger(names) : new Finger(full));
292 <                //writer.Close();
291 >                writer/*Console*/.Write(names.Count > 0 ? new Finger(names) : new Finger(full));
292 >                writer.Close();
293                  //stream.Close();
294                  //((TcpClient)client).Close();
295          }
296  
297          public static int Main(string[] args)
298          {
299 <                Do(null);
299 >                Do(/*null*/);
300  
301                  return 0;
302          }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines