home *** CD-ROM | disk | FTP | other *** search
/ AOL File Library: 12,000 to 12,999 / 12000.zip / AOLDLs / Online-Tools / Java-Applets / JAVAAPPS.lzh / JAVAAPPS / GUESTBOO / GUESTBOO.EXE / guestbook.java < prev    next >
Encoding:
Java Source  |  1996-03-09  |  11.1 KB  |  393 lines

  1.  
  2.  
  3.  
  4.  
  5. /*
  6.  
  7.  * guestbook.java - 4 Feb 1996 - Version 1.02a
  8.  
  9.  *
  10.  
  11.  * Copyright 1996 by William Giel, L.S.
  12.  
  13.  *
  14.  
  15.  * E-mail: rvdi@usa.nai.net
  16.  
  17.  * WWW: http://www.nai.net/~rvdi/home.htm
  18.  
  19.  *
  20.  
  21.  * NOTE: TO RECEIVE GUEST BOOK ENTRIES, YOU MUST SPECIFY YOUR E-MAIL
  22.  
  23.  *       ADDRESS AS THE 'RECEIVER' PARAMETER IN THE APPLETS HTML TAG.
  24.  
  25.  *
  26.  
  27.  *
  28.  
  29.  * Permission to use, copy, modify, and distribute this software and its
  30.  
  31.  * documentation without fee for NON-COMMERCIAL purposes is hereby granted,
  32.  
  33.  * provided that any use properly credits the author, i.e. "Guestbook applet
  34.  
  35.  * courtesy of <A HREF="mailto:rvdi@usa.nai.net">Bill Giel</A>.
  36.  
  37.  *
  38.  
  39.  * Send class built up from code demonstated in sendmail.java
  40.  
  41.  * by Godmar Back, University of Utah, Computer Systems Lab, 1996
  42.  
  43.  * (a simple applet that sends you mail when your page is accessed)
  44.  
  45.  */
  46.  
  47.  
  48.  
  49. import java.awt.*;
  50.  
  51. import java.applet.*;
  52.  
  53. import java.lang.*;
  54.  
  55. import java.io.*;
  56.  
  57. import java.net.*;
  58.  
  59. import java.util.*;
  60.  
  61.  
  62.  
  63. class Send
  64.  
  65. {
  66.  
  67.     String        result = "";
  68.  
  69.     String         lastline;
  70.  
  71.     short        port = 25;
  72.  
  73.  
  74.  
  75.     DataInputStream     in;
  76.  
  77.     String mailhost, receiver,  sender;
  78.  
  79.  
  80.  
  81.     public Send( String host,String recvr)
  82.  
  83.     {
  84.  
  85.            mailhost=host;
  86.  
  87.            receiver=recvr;
  88.  
  89.            sender=recvr;
  90.  
  91.     }
  92.  
  93.     
  94.  
  95.     
  96.  
  97.     void expect(String expected, String msg) throws Exception
  98.  
  99.     {
  100.  
  101.         lastline = in.readLine();
  102.  
  103.  
  104.  
  105.         if (!lastline.startsWith(expected))throw new Exception(msg + ":" + lastline);
  106.  
  107.  
  108.  
  109.         while (lastline.startsWith(expected + "-"))lastline = in.readLine();
  110.  
  111.     }    
  112.  
  113.  
  114.  
  115.     public boolean mailMessage(String subject,String message) 
  116.  
  117.     {
  118.  
  119.  
  120.  
  121.         ////////////////////////////////////////
  122.  
  123.         //Will not send mail without a receiver!
  124.  
  125.         ////////////////////////////////////////
  126.  
  127.         if(null==receiver)return false;
  128.  
  129.         
  130.  
  131.         Socket s = null;
  132.  
  133.         try {
  134.  
  135.             String res;
  136.  
  137.  
  138.  
  139.             s = new Socket(mailhost, port);
  140.  
  141.  
  142.  
  143.             PrintStream p = new PrintStream(s.getOutputStream());
  144.  
  145.             in = new DataInputStream(s.getInputStream());
  146.  
  147.  
  148.  
  149.             expect("220", "no greeting");
  150.  
  151.  
  152.  
  153.             String helohost = InetAddress.getLocalHost().toString();
  154.  
  155.             p.print("HELO " + helohost + "\r\n");
  156.  
  157.             expect("250", "helo");
  158.  
  159.  
  160.  
  161.             int pos;
  162.  
  163.             String hello = "Hello ";
  164.  
  165.  
  166.  
  167.             if ((pos = lastline.indexOf(hello)) != -1) {
  168.  
  169.                     helohost = lastline.substring(pos + hello.length());
  170.  
  171.                     helohost = helohost.substring(0, helohost.indexOf(' '));
  172.  
  173.             }
  174.  
  175.             
  176.  
  177.             p.print("MAIL FROM: " + sender + "\r\n");
  178.  
  179.             expect("250", "mail from");
  180.  
  181.         
  182.  
  183.             p.print("RCPT TO: " + receiver + "\r\n");
  184.  
  185.             expect("250", "rcpt to");
  186.  
  187.         
  188.  
  189.             p.print("DATA\r\n");
  190.  
  191.             expect("354", "data");
  192.  
  193.  
  194.  
  195.             p.print("Subject: " + subject);
  196.  
  197.             p.print(" (" + helohost + ")");
  198.  
  199.             
  200.  
  201.             p.print("\r\n\r\n");
  202.  
  203.             //Use two CRLF's above because we need a null line following
  204.  
  205.             //standard fields to indicate following DATA is message body.
  206.  
  207.             
  208.  
  209.             DataInputStream is =
  210.  
  211.                     new DataInputStream(new StringBufferInputStream(message));
  212.  
  213.  
  214.  
  215.             while (is.available() > 0) {
  216.  
  217.                 String ln = is.readLine();
  218.  
  219.                 if (ln.equals("."))
  220.  
  221.                 ln = "..";
  222.  
  223.                 p.println(ln);
  224.  
  225.                
  226.  
  227.             }
  228.  
  229.  
  230.  
  231.             String days="SunMonTueWedThuFriSat";
  232.  
  233.             String months="JanFebMarAprMayJunJulAugSepOctNovDec";
  234.  
  235.             Date date=new Date();
  236.  
  237.             p.print("(Accessed at " +
  238.  
  239.                 Integer.toString(date.getHours()) + ":" +
  240.  
  241.                 Integer.toString(date.getMinutes()) + " on " +
  242.  
  243.                 days.substring(date.getDay()*3,date.getDay()*3+3) + ", " +
  244.  
  245.                 Integer.toString(date.getDate()) + " " +
  246.  
  247.                 months.substring(date.getMonth()*3,date.getMonth()*3+3) + " " +
  248.  
  249.                 Integer.toString(date.getYear()+1900) + ")");
  250.  
  251.             
  252.  
  253.  
  254.  
  255.             p.print("\r\n.\r\n");
  256.  
  257.             expect("250", "end of data");
  258.  
  259.  
  260.  
  261.             p.print("QUIT\r\n");
  262.  
  263.             expect("221", "quit");
  264.  
  265.         } catch(Exception e)
  266.  
  267.             {
  268.  
  269.                 result = e.getMessage();
  270.  
  271.                 return false;
  272.  
  273.             }finally
  274.  
  275.                 {
  276.  
  277.                     try {
  278.  
  279.                         if (s != null)s.close();
  280.  
  281.                     } catch(Exception e) 
  282.  
  283.                     result = e.getMessage();
  284.  
  285.                 }            
  286.  
  287.         return true;
  288.  
  289.     }
  290.  
  291.   
  292.  
  293. }
  294.  
  295.  
  296.  
  297. class guestbookWindow extends Frame
  298.  
  299. {
  300.  
  301.     static final int FONTHEIGHT=12;
  302.  
  303.     static final String FONTSTRING="Helvetica";
  304.  
  305.        
  306.  
  307.     TextArea txt3=null;
  308.  
  309.     TextField txt1=null;
  310.  
  311.     TextField txt2=null;
  312.  
  313.  
  314.  
  315.     Send send=null;
  316.  
  317.  
  318.  
  319.     AppletContext appletContext;
  320.  
  321.    
  322.  
  323.  
  324.  
  325.     public guestbookWindow(AppletContext app, String mailhost,String receiver)
  326.  
  327.     {
  328.  
  329.            send= new Send(mailhost,receiver);
  330.  
  331.  
  332.  
  333.            appletContext=app;
  334.  
  335.   
  336.  
  337.            Label lbl1,lbl2,lbl3;
  338.  
  339.            Button butt1, butt2;
  340.  
  341.  
  342.  
  343.            GridBagLayout gridbag=new GridBagLayout();
  344.  
  345.            GridBagConstraints c=new GridBagConstraints();
  346.  
  347.             
  348.  
  349.            setFont(new Font(FONTSTRING,Font.BOLD + Font.ITALIC,FONTHEIGHT));
  350.  
  351.            setLayout(gridbag);
  352.  
  353.             
  354.  
  355.         setBackground(Color.lightGray);
  356.  
  357.  
  358.  
  359.         c.fill=GridBagConstraints.NONE;
  360.  
  361.         c.weightx=1.0;c.weighty=1.0;
  362.  
  363.         c.ipadx=4;c.ipady=4;
  364.  
  365.         c.insets=new Insets(5,5,5,5);
  366.  
  367.             
  368.  
  369.           lbl1 = new Label("Your Name (Optional):");
  370.  
  371.            gridbag.setConstraints(lbl1,c);
  372.  
  373.            add(lbl1);
  374.  
  375.  
  376.  
  377.            c.anchor=GridBagConstraints.WEST;
  378.  
  379.         txt1 = new TextField("", 20);
  380.  
  381.            gridbag.setConstraints(txt1,c);
  382.  
  383.            add(txt1);
  384.  
  385.             
  386.  
  387.         c.anchor=GridBagConstraints.CENTER;            
  388.  
  389.         butt1 = new Button("Send");
  390.  
  391.         gridbag.setConstraints(butt1,c);
  392.  
  393.         add(butt1);
  394.  
  395.         
  396.  
  397.         c.gridwidth=GridBagConstraints.REMAINDER;
  398.  
  399.         butt2 = new Button("Cancel");            
  400.  
  401.         gridbag.setConstraints(butt2,c);
  402.  
  403.         add(butt2);
  404.  
  405.  
  406.  
  407.         c.gridwidth=1;
  408.  
  409.         c.weightx=1.0;c.weighty=1.0;
  410.  
  411.         lbl2 = new Label("Your EMail(Optional):");
  412.  
  413.         gridbag.setConstraints(lbl2,c);
  414.  
  415.         add(lbl2);
  416.  
  417.         
  418.  
  419.         c.gridwidth=GridBagConstraints.REMAINDER;            
  420.  
  421.         c.anchor=GridBagConstraints.WEST;                                           
  422.  
  423.         txt2 = new TextField("", 20);
  424.  
  425.         gridbag.setConstraints(txt2,c);
  426.  
  427.         add(txt2);
  428.  
  429.  
  430.  
  431.         c.gridwidth=GridBagConstraints.REMAINDER;
  432.  
  433.         c.fill=GridBagConstraints.BOTH;               
  434.  
  435.         lbl3=new Label("Any comments or suggestions?");
  436.  
  437.         gridbag.setConstraints(lbl3,c);
  438.  
  439.         add(lbl3);
  440.  
  441.  
  442.  
  443.         c.gridwidth=GridBagConstraints.REMAINDER;
  444.  
  445.         c.fill=GridBagConstraints.BOTH;                       
  446.  
  447.         txt3 = new TextArea(5,66);
  448.  
  449.         gridbag.setConstraints(txt3,c);
  450.  
  451.         add(txt3);
  452.  
  453.     }
  454.  
  455.  
  456.  
  457.     public boolean action(Event evt, Object arg)
  458.  
  459.     {
  460.  
  461.         if(arg.equals("Cancel"))
  462.  
  463.         {
  464.  
  465.             dispose();
  466.  
  467.             return true;
  468.  
  469.         }
  470.  
  471.         else if(arg.equals("Send")){
  472.  
  473.             if(txt1.getText().length()>0 ||
  474.  
  475.                txt2.getText().length()>0 ||
  476.  
  477.                txt3.getText().length()>0){
  478.  
  479.                     String message="Guest: " + txt1.getText() + "\n" +
  480.  
  481.                                    "Address: " + txt2.getText() + "\n\n" +
  482.  
  483.                                    txt3.getText() ;
  484.  
  485.                        
  486.  
  487.                     if(true==send.mailMessage("Guestbook Entry!",message))
  488.  
  489.                         appletContext.showStatus("Entry logged into guest book!");
  490.  
  491.                     else
  492.  
  493.                         appletContext.showStatus("Entry NOT logged.");
  494.  
  495.             }
  496.  
  497.             else appletContext.showStatus("Nothing to send!");
  498.  
  499.             dispose();
  500.  
  501.             return true;
  502.  
  503.         }
  504.  
  505.         return false;
  506.  
  507.     }
  508.  
  509.  
  510.  
  511.     public synchronized boolean handleEvent(Event e)
  512.  
  513.     {
  514.  
  515.         if (e.id == Event.WINDOW_ICONIFY) {
  516.  
  517.             hide(); 
  518.  
  519.             return true;
  520.  
  521.         }
  522.  
  523.         if (e.id == Event.WINDOW_DESTROY) {
  524.  
  525.                  dispose();
  526.  
  527.                 return true;
  528.  
  529.         }   
  530.  
  531.         return super.handleEvent(e);
  532.  
  533.     }
  534.  
  535.  
  536.  
  537.     public void show()
  538.  
  539.     {
  540.  
  541.         txt1.setText("");
  542.  
  543.         txt2.setText("");
  544.  
  545.         txt3.setText("");
  546.  
  547.         super.show();
  548.  
  549.     }
  550.  
  551. }
  552.  
  553.  
  554.  
  555. public class guestbook extends Applet
  556.  
  557. {
  558.  
  559.     static final int FONTHEIGHT=12;
  560.  
  561.     static final String FONTSTRING="Helvetica";
  562.  
  563.     final String BUTTON = "Guest Book";
  564.  
  565.     final String VERSION = "GUESTBOOK.JAVA - v1.02a - 4 Feb 1996";
  566.  
  567.     
  568.  
  569.     guestbookWindow window=null;
  570.  
  571.     Image image=null;
  572.  
  573.     Button button;
  574.  
  575.     int width, height;
  576.  
  577.     MediaTracker  tracker = new MediaTracker(this);
  578.  
  579.  
  580.  
  581.     ///////////////////////////////////////////////////
  582.  
  583.     //Applet parameters - pretty much self-explanatory
  584.  
  585.     ///////////////////////////////////////////////////
  586.  
  587.     public String[][] getParameterInfo()
  588.  
  589.     {    
  590.  
  591.         String[][] info = {
  592.  
  593.             {"width",       "int",      "width of the applet, in pixels"},
  594.  
  595.             {"height",      "int",      "height of the applet, in pixels"},
  596.  
  597.             {"receiver",    "string",    "SMTP 'RCPT TO:' parameter <null>"},
  598.  
  599.             {"imageurl",    "string",    "name of icon to display <null>"},
  600.  
  601.             {"title",        "string",    "title for popup dialog <Guest Book>"},
  602.  
  603.         };
  604.  
  605.         return info;
  606.  
  607.     }
  608.  
  609.  
  610.  
  611.     /////////////////////////////////////
  612.  
  613.     //Applet name, author and info lines
  614.  
  615.     /////////////////////////////////////
  616.  
  617.     public String getAppletInfo()
  618.  
  619.     {
  620.  
  621.         return (    VERSION + " - simulates a guest log\n" +
  622.  
  623.                     "by E-mailing guest data to page owner, by Bill Giel\n" +
  624.  
  625.                     "http://www.nai.net/~rvdi/home.htm  or  rvdi@usa.nai.net\n" +
  626.  
  627.                     "Copyright 1996 by William Giel.");
  628.  
  629.     }
  630.  
  631.     
  632.  
  633.  
  634.  
  635.     public void init()
  636.  
  637.     {
  638.  
  639.         String receiver, szImage, szTitle;
  640.  
  641.         URL imageURL=null;
  642.  
  643.  
  644.  
  645.         receiver = getParameter("receiver");
  646.  
  647.         if(null == receiver)
  648.  
  649.             showStatus("No RECEIVER parameter - applet will not log entry!");
  650.  
  651.  
  652.  
  653.         if(null==(szTitle=getParameter("title")))
  654.  
  655.             szTitle="Guest Book";
  656.  
  657.  
  658.  
  659.            
  660.  
  661.         szImage=getParameter("IMAGEURL");
  662.  
  663.     
  664.  
  665.         window=new guestbookWindow(getAppletContext(),getCodeBase().getHost(),receiver);
  666.  
  667.         window.setTitle(szTitle);
  668.  
  669.         window.pack();
  670.  
  671.  
  672.  
  673.            setFont(new Font(FONTSTRING,Font.BOLD + Font.ITALIC,FONTHEIGHT));
  674.  
  675.          add (button = new Button(BUTTON));
  676.  
  677.         width=size().width; height=size().height;
  678.  
  679.  
  680.  
  681.         if(null != szImage){
  682.  
  683.             try{
  684.  
  685.                  imageURL=new URL(getDocumentBase(),szImage);
  686.  
  687.             } catch (MalformedURLException e)
  688.  
  689.                {
  690.  
  691.                        imageURL=null;
  692.  
  693.                     image=null;
  694.  
  695.                }
  696.  
  697.         }
  698.  
  699.                 
  700.  
  701.         if(imageURL != null){
  702.  
  703.                image=getImage(imageURL);
  704.  
  705.                if(image != null)
  706.  
  707.                 tracker.addImage(image,0);
  708.  
  709.         }
  710.  
  711.         button.move((width-button.size().width)/2,
  712.  
  713.                         (width-button.size().width)/2);
  714.  
  715.  
  716.  
  717.     }
  718.  
  719.  
  720.  
  721.     public void paint(Graphics g)
  722.  
  723.     {
  724.  
  725.         Color color=g.getColor();
  726.  
  727.         g.setColor(Color.lightGray);
  728.  
  729.         g.fill3DRect(0,0,size().width,size().height,true);
  730.  
  731.         g.setColor(color);
  732.  
  733.  
  734.  
  735.         if(image != null){
  736.  
  737.             try{
  738.  
  739.                 tracker.waitForID(0);
  740.  
  741.             }catch (InterruptedException e)
  742.  
  743.                 {
  744.  
  745.                     return;
  746.  
  747.                 }
  748.  
  749.               
  750.  
  751.             g.drawImage(image,(width-image.getWidth(this))/2,button.size().height
  752.  
  753.                 +2*(height-image.getHeight(this)-button.size().height)/3,this);
  754.  
  755.  
  756.  
  757.         }
  758.  
  759.     }
  760.  
  761.  
  762.  
  763.     public boolean action(Event evt, Object arg)
  764.  
  765.     {
  766.  
  767.         if(arg.equals(BUTTON) && !window.isShowing())
  768.  
  769.         {
  770.  
  771.             window.show();
  772.  
  773.             return true;
  774.  
  775.         }
  776.  
  777.         else return false;  
  778.  
  779.     }
  780.  
  781. }
  782.  
  783.     
  784.  
  785.