import java.awt.Button; import java.awt.Checkbox; import java.awt.CheckboxGroup; import java.awt.Dimension; import java.awt.Frame; import java.awt.Graphics; import java.awt.GridLayout; import java.awt.Image; import java.awt.MediaTracker; import java.awt.Panel; import java.awt.TextArea; import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.InputEvent; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.event.WindowEvent; import java.awt.event.WindowListener; import java.net.URL; import java.util.Enumeration; import java.util.Vector; import lava.net.common.UNA; import lava.net.common.UNL; import lava.net.psyc.PSYCDeliveryException; import lava.net.psyc.PSYCMessageCenter; import lava.net.psyc.UNI; import lava.net.psyc.packages.Authentication; import lava.net.psyc.packages.AuthenticationListener; import lava.net.psyc.packages.AuthenticationWontGivePasswordException; 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.LinkPeer; import lava.net.psyc.packages.LinkPeerListener; import lava.net.psyc.packages.Statistics; /** * **/ public class PSYCBiff extends Panel implements WindowListener, // MouseListener, AuthenticationListener, BiffListener, LinkPeerListener { /** * **/ public PSYCMessageCenter center = null; /** * **/ public Biff biff_backend = null; /** * **/ public LinkPeer user_backend = null; /** * **/ public Authentication auth_backend = null; /** * **/ public String password = null; /** * **/ public String base = null; /** * **/ public final static String ButtonTextApply = "Apply"; /** * **/ public final static String ButtonTextSave = "Save"; /** * **/ public final static String ButtonTextReload = "Reload"; /** * **/ public final static String path = "PSYCBiff.pics"; /** * **/ public final static String[][] pics = {// {"biff-down.gif", "biff-up.gif"}, // {"mail-down.gif", "mail-up.gif"}, // {"mailempty.gif", "mailfull.gif"},// }; /** * **/ private Image[][] image = new Image[pics.length][]; /** * **/ Toolkit toolkit = Toolkit.getDefaultToolkit(); /** * **/ private Frame frame = new Frame(); /** * **/ private Dimension dimension = new Dimension(0,0); /** * **/ private Vector infoBuffer = new Vector(); /** * **/ private int set = 0; /** * **/ private int position = 0; /** * **/ private int maxPics = 0; class Informer extends TextArea implements WindowListener, MouseListener { /** * **/ private Frame frame = new Frame(); /** * **/ Informer() { super(25,80); setEditable(false); addMouseListener(this); frame.addWindowListener(this); frame.setTitle("PSYC Biff - More Information"); frame.setResizable(false); frame.add(this); if(infoBuffer.size() <= 0) append("No information about received mails."); else { for(Enumeration e = infoBuffer.elements();e.hasMoreElements();) { String[] info = (String[])e.nextElement(); boolean gotInfo = false; if(info[0] != null) { append("Location : "); append(info[0]); append("\n"); gotInfo = true; } if(info[1] != null) { append("From : "); append(info[1]); append("\n"); gotInfo = true; } if(info[2] != null) { append("To : "); append(info[2]); append("\n"); gotInfo = true; } if(info[3] != null) { append("Date : "); append(info[3]); append("\n"); gotInfo = true; } if(info[4] != null) { append("Subject : "); append(info[4]); append("\n"); gotInfo = true; } if(info[5] != null) { append("\n"); append(info[5]); append("\n"); gotInfo = true; } if(!gotInfo) append("No information about that mail.\n"); if(e.hasMoreElements()) { for(int i = 0;i < 80;++i) append("-"); append("\n"); } } } frame.pack(); frame.show(); } /** * **/ public void mouseClicked(MouseEvent e) { if((e.getModifiers() & InputEvent.BUTTON3_MASK) != 0) { frame.dispose(); } } /** * **/ public void mousePressed(MouseEvent e) { } /** * **/ public void mouseReleased(MouseEvent e) { } /** * **/ public void mouseEntered(MouseEvent e) { } /** * **/ public void mouseExited(MouseEvent e) { } /** * **/ public void windowOpened(WindowEvent e) { } /** * **/ public void windowClosing(WindowEvent e) { frame.dispose(); } /** * **/ public void windowClosed(WindowEvent e) { } /** * **/ public void windowIconified(WindowEvent e) { } /** * **/ public void windowDeiconified(WindowEvent e) { } /** * **/ public void windowActivated(WindowEvent e) { } /** * **/ public void windowDeactivated(WindowEvent e) { } } class Preferencer extends Panel implements WindowListener, // MouseListener, ActionListener { /** * **/ private Frame frame = new Frame(); /** * **/ private CheckboxGroup group = new CheckboxGroup(); class MyCheckbox extends Checkbox { /** * **/ int number = -1; /** * **/ MyCheckbox(String l, CheckboxGroup g, boolean s, int n) { super(l,g,s); number = n; } } class Picture extends Panel { /** * **/ private Image image = null; /** * **/ Picture(Image image) { this.image = image; } /** * **/ public Dimension getPreferredSize() { return dimension; } /** * **/ public void paint(Graphics g) { update(g); } /** * **/ public void update(Graphics g) { Dimension d = getSize(); g.drawImage(image,0,0,d.width,d.height,this); } } /** * **/ Preferencer() { super(); addMouseListener(this); frame.addWindowListener(this); frame.setTitle("PSYC Biff - Preferences"); frame.setResizable(false); frame.add(this); int max = maxPics > 1 ? maxPics : 2; setLayout(new GridLayout(image.length + 1,max)); MyCheckbox c; Picture p; Panel a; Button b; int i, j; for(i = 0;i < image.length;++i) { add(c = new MyCheckbox("Set " + i,group,i == set,i)); c.addMouseListener(this); for(j = 0;j < image[i].length;++j) { add(p = new Picture(image[i][j])); p.addMouseListener(this); } for(;j < max;++j) { add(a = new Panel()); a.addMouseListener(this); } } add(b = new Button(ButtonTextApply)); b.addMouseListener(this); b.addActionListener(this); add(b = new Button(ButtonTextSave)); b.addMouseListener(this); b.addActionListener(this); // TODO: save b.setEnabled(false); add(b = new Button(ButtonTextReload)); b.addMouseListener(this); b.addActionListener(this); // TODO: load b.setEnabled(false); for(j = 2;j < max;++j) { add(a = new Panel()); a.addMouseListener(this); } frame.pack(); frame.show(); } /** * **/ public void actionPerformed(ActionEvent e) { String command = e.getActionCommand(); boolean save = command.equals(ButtonTextSave); if(save || command.equals(ButtonTextApply)) { set = ((MyCheckbox)group.getSelectedCheckbox()).number; PSYCBiff.this.repaint(); // this works too //this$0.repaint(); if(save) { // TODO: save } } else { // TODO: load } } /** * **/ public void mouseClicked(MouseEvent e) { if((e.getModifiers() & InputEvent.BUTTON2_MASK) != 0) { frame.dispose(); } } /** * **/ public void mousePressed(MouseEvent e) { } /** * **/ public void mouseReleased(MouseEvent e) { } /** * **/ public void mouseEntered(MouseEvent e) { } /** * **/ public void mouseExited(MouseEvent e) { } /** * **/ public void windowOpened(WindowEvent e) { } /** * **/ public void windowClosing(WindowEvent e) { frame.dispose(); } /** * **/ public void windowClosed(WindowEvent e) { } /** * **/ public void windowIconified(WindowEvent e) { } /** * **/ public void windowDeiconified(WindowEvent e) { } /** * **/ public void windowActivated(WindowEvent e) { } /** * **/ public void windowDeactivated(WindowEvent e) { } } /** * **/ public PSYCBiff() { this (null); } /** * **/ public PSYCBiff(String base) { super(); //System.out.println("java.browser = " + // // System.getProperty("java.browser")); //System.out.println("browser = " + System.getProperty("browser")); this.base = base; addMouseListener(this); frame.addWindowListener(this); frame.setTitle("PSYC Biff"); frame.setResizable(false); frame.add(this); MediaTracker tracker = new MediaTracker(this); int i, j; for(i = 0;i < pics.length;++i) { image[i] = new Image[pics[i].length]; if(pics[i].length > maxPics) maxPics = pics[i].length; for(j = 0;j < pics[i].length;++j) { if(base != null) { try { image[i][j] = toolkit.getImage(new URL(base + path + // "/" + pics[i][j])); } catch(Exception e) { System.err.println("error while loading pic: " + e); } } else { image[i][j] = toolkit.getImage(path + // System.getProperty("file.separator","/") + pics[i][j]); } tracker.addImage(image[i][j],0); } } try { tracker.waitForAll(); } catch(InterruptedException e) { } int maxWidth = 0; int maxHeight = 0; for(i = 0;i < image.length;++i) { for(j = 0;j < image[i].length;++j) { if(maxWidth < image[i][j].getWidth(null)) maxWidth = image[i][j].getWidth(null); if(maxHeight < image[i][j].getHeight(null)) maxHeight = image[i][j].getHeight(null); } } dimension = new Dimension(maxWidth,maxHeight); frame.pack(); frame.show(); } /** * **/ public Dimension getPreferredSize() { return dimension; } /** * **/ public void paint(Graphics g) { update(g); } /** * **/ public void update(Graphics g) { Dimension d = getSize(); g.drawImage(image[set][position],0,0,d.width,d.height,this); } /** * **/ public void mouseClicked(MouseEvent e) { int mod = e.getModifiers(); if((mod & InputEvent.BUTTON1_MASK) != 0) { if(position == 0) return; ++position; if(position >= image[set].length) { position = 0; try { biff_backend.clear(center.getIdentification(null),// "I read my mail."); } catch(PSYCDeliveryException ex) { } } repaint(); } else if((mod & InputEvent.BUTTON2_MASK) != 0) new Preferencer(); else if((mod & InputEvent.BUTTON3_MASK) != 0) new Informer(); } /** * **/ public void mousePressed(MouseEvent e) { } /** * **/ public void mouseReleased(MouseEvent e) { } /** * **/ public void mouseEntered(MouseEvent e) { } /** * **/ public void mouseExited(MouseEvent e) { } /** * **/ public void windowOpened(WindowEvent e) { } /** * **/ public void windowClosing(WindowEvent e) { center.close(); if(base != null) frame.dispose(); else System.exit(0); } /** * **/ public void windowClosed(WindowEvent e) { } /** * **/ public void windowIconified(WindowEvent e) { } /** * **/ public void windowDeiconified(WindowEvent e) { } /** * **/ public void windowActivated(WindowEvent e) { } /** * **/ public void windowDeactivated(WindowEvent e) { } /** * **/ public void gotMail(UNL source, String location, String from, String to, // String date, String subject, String body) { if(!user_backend.isLinked(source)) { System.err.println("*** Untrusted source " + source + // " wants to inform you about receiving mail."); System.err.println("Location: " + location); System.err.println("From : " + from); System.err.println("To : " + to); System.err.println("Date : " + date); System.err.println("Subject: " + subject); System.err.println(); System.err.println(body); return; } String[] infoEntry = {location, from, to, date, subject, body}; infoBuffer.addElement(infoEntry); toolkit.beep(); position = 1; repaint(); } /** * **/ public void clearMail(UNL source, String body) { if(!user_backend.isLinked(source)) { System.err.println("*** Untrusted source " + source + // " wants to reset your biff state."); System.err.println(body); return; } infoBuffer.removeAllElements(); position = 0; repaint(); } /** * **/ private void connect() { try { user_backend.link(); } catch(PSYCDeliveryException e) { System.err.println("could not connect to user program " + // center.getIdentification(null) + ": " + e); } } /** * **/ public String authenticationGetPassword(UNL source, String body) // throws AuthenticationWontGivePasswordException { System.out.println(source + " wants a password" + // (body == null || body.length() <= 0 ? "." : ": " + body)); if(!user_backend.isLinked(source)) { System.out.println(" We don't wanna connect it, why the hell " + // "it wants a password?"); System.out.println(" I'll give it NO password!"); throw new AuthenticationWontGivePasswordException(); } else { System.out.println(" I'll give it: " + password); return password; } } /** * **/ public void authenticationInvalidPassword(UNL source, String body) { System.out.println(source + " refuses my password" + // (body == null || body.length() <= 0 ? "." : ": " + body)); } /** * **/ public void serverLinked(UNL server, String body) { System.out.println(server + " accepts me as user" + // (body == null || body.length() <= 0 ? "." : ": " + body)); if(center.getName(null) == null) center.setName(null,center.getName(server)); if(center.getDescription(null) == null) center.setDescription(null,center.getDescription(server)); if(center.getNickname(null) == null) center.setNickname(null,center.getNickname(server)); if(center.getURLDescription(null) == null) center.setURLDescription(null,center.getURLDescription(server)); if(center.getHumanInformation(null) == null) center.setHumanInformation(null,center.getHumanInformation(server)); if(center.getContentType(null) == null) center.setContentType(null,center.getContentType(server)); } /** * **/ public void serverUnlinked(UNL server, String body) { System.out.println(server + " disconnects me" + // (body == null || body.length() <= 0 ? "." : ": " + body)); } /** * **/ public static void main(String args[]) throws Exception { PSYCBiff biff = new PSYCBiff(); biff.password = "blahfasel"; //UNA[] listen = { new UNA("0.0.0.0:5000") }; //biff.center = new PSYCMessageCenter(null, listen); biff.center = new PSYCMessageCenter(); //UNA proxy = new UNA("psyc://localhost:4004"); //System.out.println("proxy: " + proxy); //biff.center.getMMPCenter().setProxy(null, proxy); biff.center.setIdentification(null,// //new UNI("psyc://psyc.tu-ilmenau.de/")); //new UNI("psyc://xanadu.lava.de/holbe")); new UNI("psyc://127.2/~holbe")); biff.center.setContentType(null,"text/plain"); biff.center.setGreeting("_notice_session_start",// "Hello.\n\nWelcome to the wonderful world of PSYC!"); biff.center.addPackage(biff.biff_backend = new Biff(biff)); biff.center.addPackage(biff.auth_backend = new Authentication(biff)); System.out.println("connecting to " + // biff.center.getIdentification(null)); biff.center.addPackage(biff.user_backend = new LinkPeer(biff)); biff.center.addPackage(new Echo()); biff.center.addPackage(new Trace()); biff.center.addPackage(new Statistics()); } }