1
2
3
4
5
6
7
8
9
10
11
12
13 package org.abstracthorizon.mercury.finger;
14
15 import java.io.IOException;
16 import java.io.InputStream;
17 import java.io.InterruptedIOException;
18 import java.io.OutputStream;
19 import java.net.InetAddress;
20 import java.net.Socket;
21 import java.net.UnknownHostException;
22
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26
27
28
29
30
31 public class SimpleFinger {
32
33
34 protected final Logger logger = LoggerFactory.getLogger(SimpleFinger.class);
35
36
37 protected String userName;
38
39
40 protected String hostName;
41
42
43 protected InetAddress hostAddress;
44
45
46 protected int timeout = 60000;
47
48
49 protected String lastResult;
50
51
52
53
54 public SimpleFinger() {
55 }
56
57
58
59
60
61 public String getHostName() {
62 return hostName;
63 }
64
65
66
67
68
69 public void setHostName(String host) {
70 this.hostName = host;
71 }
72
73
74
75
76
77 public InetAddress getHostAddress() {
78 return hostAddress;
79 }
80
81
82
83
84
85 public void setHostAddress(InetAddress address) {
86 this.hostAddress = address;
87 }
88
89
90
91
92
93 public String getUser() {
94 return userName;
95 }
96
97
98
99
100
101 public void setUser(String user) {
102 this.userName = user;
103 }
104
105
106
107
108
109 public int getTimeout() {
110 return timeout;
111 }
112
113
114
115
116
117 public void setTimeout(int timeout) {
118 this.timeout = timeout;
119 }
120
121
122
123
124
125 public String getLastResult() {
126 return lastResult;
127 }
128
129
130
131
132
133
134 public void finger() throws UnknownHostException, IOException {
135 if ((hostAddress == null) && (hostName == null)) {
136 throw new IOException("Address or Host must be supplied");
137 } else if (hostAddress == null) {
138 hostAddress = InetAddress.getByName(hostName);
139 }
140
141 StringBuffer buf = new StringBuffer(64);
142 logger.debug("Connecting to: " + hostAddress + ":79");
143
144 Socket socket = new Socket(hostAddress, 79);
145 socket.setSoTimeout(timeout);
146 try {
147 OutputStream out = socket.getOutputStream();
148 String fingerCommand = null;
149 if ((userName != null) && (userName.length() > 0) && (hostName != null) && (hostName.length() > 0)) {
150 fingerCommand = "/W " + userName + "@" + hostName + "\r\n";
151 } else if ((userName != null) && (userName.length() > 0)) {
152 fingerCommand = "/W " + userName + "\r\n";
153 } else if ((hostName != null) && (hostName.length() > 0)) {
154 fingerCommand = "/W @" + hostName + "\r\n";
155 } else {
156 fingerCommand = "/W\r\n";
157 }
158 out.write(fingerCommand.getBytes());
159 out.flush();
160
161 InputStream is = socket.getInputStream();
162 logger.debug("Sent command '" + fingerCommand + "'");
163
164 int i = is.read();
165 while (i > 0) {
166 buf.append((char)i);
167 i = is.read();
168 }
169 logger.info("Finger to:" + hostAddress + ":79 response:" + buf);
170 } catch (InterruptedIOException e) {
171 logger.info("Finger timeout to:" + hostAddress + ":79 response:" + buf);
172 } catch (IOException e) {
173 logger.error("Finger exception to:" + hostAddress + ":79 " + e.getMessage());
174 } finally {
175 try {
176 socket.close();
177 } catch (IOException e) {
178 }
179 }
180 lastResult = buf.toString();
181 }
182
183
184
185
186
187
188
189
190 public static String finger(String user) throws UnknownHostException, IOException {
191 if ((user == null) || (user.length() == 0)) {
192 return finger(null, null);
193 }
194 int i = user.indexOf('@');
195 String u = "";
196 String h = "";
197 if (i >= 0) {
198 u = user.substring(0, i);
199 h = user.substring(i+1);
200 }
201 return finger(u, h);
202 }
203
204
205
206
207
208
209
210
211
212 public static String finger(String user, String host) throws UnknownHostException, IOException {
213 StringBuffer buf = new StringBuffer(64);
214 Socket socket = new Socket(host, 79);
215 socket.setSoTimeout(60000);
216 try {
217 OutputStream out = socket.getOutputStream();
218 if ((user != null) && (user.length() > 0) && (host != null) && (host.length() > 0)) {
219 out.write(("/W "+user+"\r\n").getBytes());
220 } else if ((user != null) && (user.length() > 0)) {
221 out.write(("/W @"+host+"\r\n").getBytes());
222 } else {
223 out.write(("/W\r\n").getBytes());
224 }
225 out.flush();
226
227 InputStream is = socket.getInputStream();
228
229 int i = is.read();
230 while (i >= 0) {
231 buf.append((char)i);
232 i = is.read();
233 }
234 } catch (IOException e) {
235 } finally {
236 try {
237 socket.close();
238 } catch (IOException e) {
239 }
240 }
241 return buf.toString();
242 }
243
244
245 public static void main(String[] args) throws Exception {
246 System.out.print(finger(args[0], args[1]));
247 }
248
249 }