home *** CD-ROM | disk | FTP | other *** search
/ The Net: Ultimate Internet Guide / WWLCD1.ISO / pc / java / in4wjcxu / src / como / irc / comopageclient.java < prev    next >
Encoding:
Java Source  |  1996-08-14  |  5.6 KB  |  223 lines

  1. package como.irc;
  2.  
  3. import java.awt.*;
  4. import java.applet.Applet;
  5. import java.net.*;
  6. import java.io.*;
  7. import java.util.Date;
  8. import java.util.Hashtable;
  9.  
  10. import como.awt.*;
  11. import como.commlet.*;
  12. import como.util.*;
  13. import como.sys.*;
  14.  
  15. public class ComoPageClient extends Applet implements ComoIRCFrontEnd {
  16.  
  17.     static int LOG_LINES = 4;
  18.     static int MIN_NAME_LENGTH = 2;
  19.     static int IRC_PORT = 6667;
  20.     static int IRC_MAX_NICK_LENGTH = 9;
  21.  
  22.     static final String VERSION = "V0.01";
  23.     static final String LOGOUT_BUTTON = "IMPORTANT: Please click here to LOG OUT";
  24.  
  25.     static int EVENT_INVITATION_ACCEPTED = 15015;
  26.     static int EVENT_INVITATION_REJECTED = 15016;
  27.  
  28.     UserPanel loginuserpanel;
  29.     Button logoutButton;
  30.     User ego = null;
  31.     
  32.     ServerIRC server = null;
  33.  
  34.     String channel;
  35.     String commlet;
  36.     String topic;
  37.     boolean autologout = false;
  38.     boolean invitable = false;
  39.     String name = null;
  40.     int ircport = IRC_PORT;
  41.     IrcChan ircchan = null;
  42.     int commletsrunning = 0;
  43.  
  44.     String[][] params = {
  45.         {"IRCPort","integer","Port to connect to"},
  46.         {"Channel","String","If set, that channel will be joined if it exists"},
  47.         {"Commlet","String","If set, the given channel will be opened with "+
  48.                     "this commlet if it doesn't exist"},
  49.         {"Name","String", "If set, user gets name automatically. "+
  50.                 "This name begin with the given String. "+
  51.                 "Otherwise, user has to log in."},
  52.         {"Invitable","String","If set, user may be invited by others"},
  53.         {"Autologout","String","If set, user will be logged out when last Commlet Session ends."},
  54.         {"Topic","String","Which topic to set if opening channel"}};
  55.  
  56.     public void init()    {
  57.  
  58.     channel = getParameter("Channel");
  59.     commlet = getParameter("Commlet");
  60.     topic = getParameter("Topic");
  61.     ircchan = new IrcChan(channel,commlet,topic);
  62.     name = getParameter("Name");
  63.     String s = getParameter("IRCPort");
  64.     try {
  65.         ircport = (new Integer(s.trim())).intValue();
  66.         }
  67.     catch (Exception e) { ircport = IRC_PORT; }
  68.     invitable = (getParameter("Invitable") != null);
  69.     autologout = (getParameter("Autologout") != null);
  70.     }
  71.  
  72.  public String[][] getParameterInfo() {
  73.     return params;
  74.     }
  75.     public String getAppletInfo() {
  76.     return "ComoPageClient "+VERSION+" (c) Jan Kautz & Ulrich Gall Tel. +49-9131-201329";
  77.     }
  78.     /** 
  79.     * Called after user logged in
  80.     */
  81.     public void showLoggedIn() {
  82.     removeAll();
  83.     }
  84.     /**
  85.     * Displays a dialog that will let the user login in.
  86.     */
  87.     public void showLogin() {
  88.         removeAll();
  89.         setLayout(new BorderLayout());
  90.         add("North",new Label("Please fill in the following information to log in:"));
  91.         ego = new User(0,"");
  92.         ego.put(ego.COMMENT,"");
  93.         loginuserpanel = new UserPanel(ego,"Click here to log in!");
  94.         add("Center",loginuserpanel);
  95.         ego = null;
  96.         paintAll(getGraphics());
  97.     }
  98.  
  99.     private void clearStatus() {
  100.         if (ego != null)
  101.         showStatus(ego.get(User.NAME).toString() + "'s Como Session");
  102.         else
  103.         showStatus("");
  104.     }
  105.     
  106.     public void logout() {
  107.         // Ask the user if he wants to log out
  108.         server.logout();
  109.         showLogin();
  110.     }
  111.     public void stop() {    
  112.     }
  113.     public void start() {
  114.     if (ego == null) {
  115.         if (name == null) {
  116.             showLogin();
  117.         }
  118.         else {
  119.         // automatically select a name
  120.         
  121.             ego = new User();
  122.             int a = (new Date()).hashCode();
  123.             if (a < 0) a = -a;
  124.             ego.put(ego.NAME,name + a);
  125.             try {
  126.                 server = new ServerIRC(ego,getDocumentBase().getHost(),ircport,this);
  127.             } catch ( IOException e ) {
  128.                 removeAll();
  129.                 paintAll(getGraphics());
  130.                 return;
  131.             }
  132.         }
  133.         if (channel != null) {
  134.             server.joinChannel(ircchan);
  135.             commletsrunning++;
  136.         }
  137.     }
  138.     }
  139.  
  140.     public boolean handleEvent(Event evt) {
  141.          if (super.handleEvent(evt)) return true;
  142.          if (evt.id == EVENT_INVITATION_REJECTED) {
  143.              ((Frame)evt.target).dispose();
  144.              return true;
  145.          }
  146.        if (evt.id == EVENT_INVITATION_ACCEPTED) {
  147.         log("Invitation to channel "+evt.arg.toString());
  148.           server.joinChannel((IrcChan)evt.arg);
  149.         commletsrunning++;
  150.             ((Frame)evt.target).dispose();
  151.             return true;
  152.       }
  153.     return false;
  154.     }
  155.     
  156.     public boolean action(Event evt,Object what) {
  157.         if (evt.target == logoutButton) {
  158.             logout();
  159.         }
  160.         else if (evt.target == loginuserpanel) {
  161.             ego = loginuserpanel.getUser();
  162.             if (ego.get(ego.NAME).toString().trim().length()  > MIN_NAME_LENGTH) {
  163.                 clearStatus();
  164.             ego.put(ego.COLOR, new Color(ego.hashCode()));
  165.                 removeAll();
  166.                 add(new Label("Logging in, please wait..."));
  167.                 paintAll(getGraphics());
  168.                 layout();
  169.                 paintAll(getGraphics());
  170.                 try {
  171.                     server = new ServerIRC(ego,getDocumentBase().getHost(),IRC_PORT,this);
  172.                 } catch( Exception e ) {
  173.                     new SmartFrame( "No connection to server, try again later!" );
  174.                 }
  175.                 showLoggedIn();
  176.                 return true;}
  177.         }
  178.         return false;        
  179.     }
  180.  
  181.  
  182. // To implement ComoIRCFrontEnd
  183.  
  184.     public void noConnection() {
  185.     logout();
  186.     }
  187.     public void handleInvitation(User u,IrcChan chan) {
  188.     if (invitable) {
  189.         SmartFrame f = new SmartFrame(this,
  190.              "Yes, accept",new Event(this,EVENT_INVITATION_ACCEPTED,chan),
  191.             "No, reject",new Event(this,EVENT_INVITATION_REJECTED,null));
  192.         f.setTitle("Invitation received...");
  193.         Panel p = new Panel();
  194.         p.setLayout(new VertLayout(VertLayout.CENTER));
  195.         String name = u.get(u.NAME).toString();
  196.         String comment = u.get(u.COMMENT).toString();
  197.         p.add(new Label("Invitation from "+name+"("+comment+")"));
  198.         p.add(new Label("Would you like to join a "+chan.commletname+ " Session?"));
  199.         p.add(new Label("Topic : "+chan.topic));
  200.         f.add("North",p);
  201.         f.pack();
  202.         f.show();
  203.         }
  204.         }
  205.     public void log(String s) {
  206.     showStatus(s);
  207.     }
  208.     public void addUser(String nick) {
  209.     }
  210.     public void userLeft(String nick) {
  211.     }
  212.     public Applet getApplet() {
  213.     return this;
  214.     }
  215.         public void leftChannel(IrcChan chan) {
  216.     commletsrunning--;
  217.     if ((commletsrunning == 0) && (autologout)) {
  218.         server.logout();    
  219.         ego = null;
  220.         }
  221.     }
  222. }
  223.