import java.util.Enumeration; import java.io.IOException; import java.io.FileInputStream; import java.net.URL; import java.net.MalformedURLException; import java.util.Properties; import lava.net.common.UNA; import lava.net.common.UNL; import lava.net.common.Value; import lava.net.mmp.MMPCenter; import lava.net.psyc.PSYCDeliveryException; import lava.net.psyc.PSYCMessageCenter; import lava.net.psyc.packages.AuthenticationServer; import lava.net.psyc.packages.AuthenticationServerListener; import lava.net.psyc.packages.Biff; import lava.net.psyc.packages.BiffListener; import lava.net.psyc.packages.Echo; import lava.net.psyc.packages.Trace; import lava.net.psyc.packages.Statistics; import lava.net.psyc.packages.LinkServer; import lava.net.psyc.packages.LinkServerListener; import lava.net.psyc.packages.IdentificationChecker; import lava.net.psyc.packages.IdentificationCheckerListener; import lava.net.psyc.packages.NotificationServer; import lava.net.psyc.packages.NotificationServerListener; /** * **/ public class PSYCUser implements LinkServerListener, // AuthenticationServerListener, // NotificationServerListener, // BiffListener /*, IdentificationCheckerListener */ { /** * **/ private String mailPassword = null; /** * **/ private String userPassword = null; /** * **/ private PSYCMessageCenter center = null; /** * **/ private Statistics stats = null; /** * **/ private LinkServer user = null; /** * **/ private AuthenticationServer auth = null; /** * **/ private Biff mail = null; /** * **/ private NotificationServer notify = null; /** * **/ private Value buddies = null; /** * **/ public PSYCUser() { this (null); } /** * **/ public PSYCUser(MMPCenter mmpCenter) { this (mmpCenter,null,"holbe"); } /** * **/ public PSYCUser(MMPCenter mmpCenter, Properties settings, String name) { if(settings == null) { settings = new Properties(); try { settings.load(new FileInputStream("server.config")); } catch(IOException e) { } } name = "psyc.server.users." + name + "."; mailPassword = settings.getProperty(name + "mailPassword"); userPassword = settings.getProperty(name + "userPassword"); stats = new Statistics(); stats.addEvent("Statistics package created."); center = new PSYCMessageCenter(null,mmpCenter); stats.addEvent("PSYC system is up."); center.setName(null,settings.getProperty(name + "name")); center.setDescription(null,settings.getProperty(name + "description")); center.setNickname(null,settings.getProperty(name + "nickname")); try { center.setURLDescription(null,// new URL(settings.getProperty(name + "url"))); } catch(MalformedURLException e) { center.setURLDescription(null,null); } center.setHumanInformation(null,// settings.getProperty(name + "information")); center.setContentType(null,"text/plain"); center.setGreeting(settings.getProperty(name + "greetingMethod"),// settings.getProperty(name + "greetingBody")); String b = settings.getProperty(name + "buddies"); if(b != null) buddies = new Value(b); center.addPackage(user = new LinkServer(this)); stats.addEvent("PSYC system included Link Server package."); center.addPackage(auth = new AuthenticationServer(this)); stats.addEvent("PSYC system included Authentication Server package."); center.addPackage(new Echo()); stats.addEvent("PSYC system included Echo package."); center.addPackage(new Trace()); stats.addEvent("PSYC system included Trace package."); center.addPackage(stats); stats.addEvent("PSYC system included Statistics package."); center.addPackage(mail = new Biff(this)); stats.addEvent("PSYC system included Mail Notification package."); center.addPackage(notify = new NotificationServer(this)); stats.addEvent("PSYC system included Notification package."); stats.addEvent("PSYC system completed initialization."); //center.addPackage(new IdentificationChecker(this)); } /** * **/ public PSYCMessageCenter getCenter() { return center; } /** * **/ //public void wrongIdentification(UNL source) { // System.out.println("wrong Identification by: " + source); // center.removeVariable(source, center.identificationTag); //} //public void temporarilyIdentification(UNL source) { // System.out.println("temporarily set Identification by: " + source); // center.removeVariable(source, center.identificationTag); //} private void connect(UNL client) { center.augmentLocation(null,client); if(buddies != null) center.assignVariable(client,"_list_buddies",buddies,false); user.link(client); notify.signon(); } /** * **/ private void disconnect(UNL client) { center.diminishLocation(null,client); user.unlink(client); notify.signoff(); } /** * **/ private void disconnectRaw(UNL client) { center.diminishLocation(null,client); user.removeLinked(client); notify.signoff(); } /** * **/ public void notificationRequestSubscribe(UNL source, String body) { try { notify.subscribe(source); if(user.getAllLinked().hasMoreElements()) notify.signon(source); } catch(PSYCDeliveryException e) { try { notify.unsubscribe(source); } catch(PSYCDeliveryException ex) { } } } /** * **/ public void notificationUnsubscribed(UNL source, String body) { } /** * **/ public void peerLinkRequest(UNL client, String body) { stats.addEvent(1,"" + client + ": wants to connect" + // (body == null || body.length() <= 0 ? "." : ": " + body)); if(userPassword == null || // userPassword.equals(auth.getPassword(client))) { stats.addEvent(1,"" + client + // ": password is given and correct, connect it."); connect(client); } else { stats.addEvent(2,"" + client + // ": password is not given or incorrect, ask it."); try { auth.queryPassword(client); } catch(PSYCDeliveryException e) { stats.addEvent(3,"" + client + // ": error in asking password: " + e.getMessage() + // " ... disconnect it raw."); disconnectRaw(client); } } } /** * **/ public void authenticationGotPassword(UNL client, String password) { stats.addEvent(1,"" + client + ": sent me a password."); if(user.wantsLink(client)) { if(userPassword == null || userPassword.equals(password)) { stats.addEvent(1,"" + client + // ": password is correct, connect it."); connect(client); } else try { stats.addEvent(2,"" + client + // ": password is incorrect, disconnect it."); auth.invalidPassword(client); disconnect(client); } catch(PSYCDeliveryException e) { stats.addEvent(3,"" + client + // ": error in disconnecting client: " + e.getMessage() + // " ... disconnect it raw."); disconnectRaw(client); } } } /** * **/ public void peerUnlinked(UNL client, String body) { stats.addEvent(1,"" + client + " gots disconnected" + // (body == null || body.length() <= 0 ? "." : ": " + body)); center.diminishLocation(null,client); notify.signoff(); } /** * **/ public void gotMail(UNL source, String location, String from, String to, // String date, String subject, String body) { if(mailPassword != null && // !mailPassword.equals(auth.getPassword(source)) && // !user.isLinked(source)) { stats.addEvent(2,"" + source + // " sent information about received mail with wrong password."); return; } stats.addEvent(1,"" + source + // " sent information about received mail."); UNL myUser; for(Enumeration e = user.getAllLinked();e.hasMoreElements();) { myUser = (UNL)e.nextElement(); try { mail.inform(myUser,location,from,to,date,subject,body); stats.addEvent(0,"PSYC system informed " + myUser + // " about receiving mail."); } catch(PSYCDeliveryException ex) { if(ex.getErrorCode() == PSYCDeliveryException.NOT_SUPPORTED) { stats.addEvent(1,"PSYC system could not inform " + myUser + // " about receiving mail (it doesn't support it)."); } else { stats.addEvent(3,"PSYC system could not inform " + myUser + // " about receiving mail (" + ex.getMessage() + ")."); } } } } /** * **/ public void clearMail(UNL source, String body) { if(mailPassword != null && // !mailPassword.equals(auth.getPassword(source)) && // !user.isLinked(source)) { stats.addEvent(2,"" + source + // " sent mail status reset with wrong password."); return; } stats.addEvent(1,"" + source + " sent mail status reset" + // (body == null || body.length() <= 0 ? "." : ": " + body)); UNL myUser; for(Enumeration e = user.getAllLinked();e.hasMoreElements();) { myUser = (UNL)e.nextElement(); try { mail.clear(myUser,body); stats.addEvent(0,"PSYC system informed " + myUser + // " about mail status reset."); } catch(PSYCDeliveryException ex) { if(ex.getErrorCode() == PSYCDeliveryException.NOT_SUPPORTED) { stats.addEvent(1,"PSYC system could not inform " + myUser + // " about mail status reset (it doesn't support it)."); } else { stats.addEvent(3,"PSYC system could not inform " + myUser + // " about mail status reset (" + ex.getMessage() + ")."); } } } } /** * **/ public static void main(String args[]) { PSYCUser s = new PSYCUser(); } }