import java.io.*;
import java.util.*;
import java.net.*;

public class FingerServer extends MTServerBase {
  static Properties prop=null;
  public FingerServer() {
    if (prop==null) {
      prop = new Properties();
      try {
        prop.load(new FileInputStream("FingerServer.prop"));
        }
      catch (IOException e) {
        System.out.println("FingerServer.prop not present");
        }
      }
    }
  
  void socketClose() {
    try {
      socket.close();
      }
    catch (IOException e) {}
    }

  public void run() {
   BufferedReader is;
   OutputStreamWriter os;
   String s;
   String cmd;
   try {
     is = new BufferedReader(
       new InputStreamReader(socket.getInputStream()));
     os = new OutputStreamWriter(socket.getOutputStream(),
      " iso8859_1");
     }
   catch (Exception ioe) {
     System.out.println("Can't create streams\n" + ioe);
     socketClose();
     return;
     }
   // read command line
   try {
     cmd=is.readLine();
     }
   catch (IOException e) {
     socketClose();
     return;
    }
   
   // look up entry
   if (cmd.equals("")) 
      cmd="*default";

// parse multiple requests
   StringTokenizer tok = new StringTokenizer(cmd);
   String token;
   while (tok.hasMoreTokens()) {  
     token=tok.nextToken();
     if (token.equals("/W")||token.equals("/w"))
       continue;
     if (token.indexOf('@')==-1)
       s=prop.getProperty(token,"Not Found");
     else
       s="Finger forwarding service denied";
    // write it out
    try {
      s+="\r\n";
      os.write(s,0,s.length());
      os.flush();
      }
    catch (IOException we) {
      System.out.println("Write exception: " + we);
      }
    }
   
  socketClose();
  }

  public static void main(String [] args) {
    startServer(79,FingerServer.class);
    }

}	   
