home *** CD-ROM | disk | FTP | other *** search
Java Source | 1996-03-09 | 11.1 KB | 393 lines |
-
-
-
-
- /*
-
- * guestbook.java - 4 Feb 1996 - Version 1.02a
-
- *
-
- * Copyright 1996 by William Giel, L.S.
-
- *
-
- * E-mail: rvdi@usa.nai.net
-
- * WWW: http://www.nai.net/~rvdi/home.htm
-
- *
-
- * NOTE: TO RECEIVE GUEST BOOK ENTRIES, YOU MUST SPECIFY YOUR E-MAIL
-
- * ADDRESS AS THE 'RECEIVER' PARAMETER IN THE APPLETS HTML TAG.
-
- *
-
- *
-
- * Permission to use, copy, modify, and distribute this software and its
-
- * documentation without fee for NON-COMMERCIAL purposes is hereby granted,
-
- * provided that any use properly credits the author, i.e. "Guestbook applet
-
- * courtesy of <A HREF="mailto:rvdi@usa.nai.net">Bill Giel</A>.
-
- *
-
- * Send class built up from code demonstated in sendmail.java
-
- * by Godmar Back, University of Utah, Computer Systems Lab, 1996
-
- * (a simple applet that sends you mail when your page is accessed)
-
- */
-
-
-
- import java.awt.*;
-
- import java.applet.*;
-
- import java.lang.*;
-
- import java.io.*;
-
- import java.net.*;
-
- import java.util.*;
-
-
-
- class Send
-
- {
-
- String result = "";
-
- String lastline;
-
- short port = 25;
-
-
-
- DataInputStream in;
-
- String mailhost, receiver, sender;
-
-
-
- public Send( String host,String recvr)
-
- {
-
- mailhost=host;
-
- receiver=recvr;
-
- sender=recvr;
-
- }
-
-
-
-
-
- void expect(String expected, String msg) throws Exception
-
- {
-
- lastline = in.readLine();
-
-
-
- if (!lastline.startsWith(expected))throw new Exception(msg + ":" + lastline);
-
-
-
- while (lastline.startsWith(expected + "-"))lastline = in.readLine();
-
- }
-
-
-
- public boolean mailMessage(String subject,String message)
-
- {
-
-
-
- ////////////////////////////////////////
-
- //Will not send mail without a receiver!
-
- ////////////////////////////////////////
-
- if(null==receiver)return false;
-
-
-
- Socket s = null;
-
- try {
-
- String res;
-
-
-
- s = new Socket(mailhost, port);
-
-
-
- PrintStream p = new PrintStream(s.getOutputStream());
-
- in = new DataInputStream(s.getInputStream());
-
-
-
- expect("220", "no greeting");
-
-
-
- String helohost = InetAddress.getLocalHost().toString();
-
- p.print("HELO " + helohost + "\r\n");
-
- expect("250", "helo");
-
-
-
- int pos;
-
- String hello = "Hello ";
-
-
-
- if ((pos = lastline.indexOf(hello)) != -1) {
-
- helohost = lastline.substring(pos + hello.length());
-
- helohost = helohost.substring(0, helohost.indexOf(' '));
-
- }
-
-
-
- p.print("MAIL FROM: " + sender + "\r\n");
-
- expect("250", "mail from");
-
-
-
- p.print("RCPT TO: " + receiver + "\r\n");
-
- expect("250", "rcpt to");
-
-
-
- p.print("DATA\r\n");
-
- expect("354", "data");
-
-
-
- p.print("Subject: " + subject);
-
- p.print(" (" + helohost + ")");
-
-
-
- p.print("\r\n\r\n");
-
- //Use two CRLF's above because we need a null line following
-
- //standard fields to indicate following DATA is message body.
-
-
-
- DataInputStream is =
-
- new DataInputStream(new StringBufferInputStream(message));
-
-
-
- while (is.available() > 0) {
-
- String ln = is.readLine();
-
- if (ln.equals("."))
-
- ln = "..";
-
- p.println(ln);
-
-
-
- }
-
-
-
- String days="SunMonTueWedThuFriSat";
-
- String months="JanFebMarAprMayJunJulAugSepOctNovDec";
-
- Date date=new Date();
-
- p.print("(Accessed at " +
-
- Integer.toString(date.getHours()) + ":" +
-
- Integer.toString(date.getMinutes()) + " on " +
-
- days.substring(date.getDay()*3,date.getDay()*3+3) + ", " +
-
- Integer.toString(date.getDate()) + " " +
-
- months.substring(date.getMonth()*3,date.getMonth()*3+3) + " " +
-
- Integer.toString(date.getYear()+1900) + ")");
-
-
-
-
-
- p.print("\r\n.\r\n");
-
- expect("250", "end of data");
-
-
-
- p.print("QUIT\r\n");
-
- expect("221", "quit");
-
- } catch(Exception e)
-
- {
-
- result = e.getMessage();
-
- return false;
-
- }finally
-
- {
-
- try {
-
- if (s != null)s.close();
-
- } catch(Exception e)
-
- result = e.getMessage();
-
- }
-
- return true;
-
- }
-
-
-
- }
-
-
-
- class guestbookWindow extends Frame
-
- {
-
- static final int FONTHEIGHT=12;
-
- static final String FONTSTRING="Helvetica";
-
-
-
- TextArea txt3=null;
-
- TextField txt1=null;
-
- TextField txt2=null;
-
-
-
- Send send=null;
-
-
-
- AppletContext appletContext;
-
-
-
-
-
- public guestbookWindow(AppletContext app, String mailhost,String receiver)
-
- {
-
- send= new Send(mailhost,receiver);
-
-
-
- appletContext=app;
-
-
-
- Label lbl1,lbl2,lbl3;
-
- Button butt1, butt2;
-
-
-
- GridBagLayout gridbag=new GridBagLayout();
-
- GridBagConstraints c=new GridBagConstraints();
-
-
-
- setFont(new Font(FONTSTRING,Font.BOLD + Font.ITALIC,FONTHEIGHT));
-
- setLayout(gridbag);
-
-
-
- setBackground(Color.lightGray);
-
-
-
- c.fill=GridBagConstraints.NONE;
-
- c.weightx=1.0;c.weighty=1.0;
-
- c.ipadx=4;c.ipady=4;
-
- c.insets=new Insets(5,5,5,5);
-
-
-
- lbl1 = new Label("Your Name (Optional):");
-
- gridbag.setConstraints(lbl1,c);
-
- add(lbl1);
-
-
-
- c.anchor=GridBagConstraints.WEST;
-
- txt1 = new TextField("", 20);
-
- gridbag.setConstraints(txt1,c);
-
- add(txt1);
-
-
-
- c.anchor=GridBagConstraints.CENTER;
-
- butt1 = new Button("Send");
-
- gridbag.setConstraints(butt1,c);
-
- add(butt1);
-
-
-
- c.gridwidth=GridBagConstraints.REMAINDER;
-
- butt2 = new Button("Cancel");
-
- gridbag.setConstraints(butt2,c);
-
- add(butt2);
-
-
-
- c.gridwidth=1;
-
- c.weightx=1.0;c.weighty=1.0;
-
- lbl2 = new Label("Your EMail(Optional):");
-
- gridbag.setConstraints(lbl2,c);
-
- add(lbl2);
-
-
-
- c.gridwidth=GridBagConstraints.REMAINDER;
-
- c.anchor=GridBagConstraints.WEST;
-
- txt2 = new TextField("", 20);
-
- gridbag.setConstraints(txt2,c);
-
- add(txt2);
-
-
-
- c.gridwidth=GridBagConstraints.REMAINDER;
-
- c.fill=GridBagConstraints.BOTH;
-
- lbl3=new Label("Any comments or suggestions?");
-
- gridbag.setConstraints(lbl3,c);
-
- add(lbl3);
-
-
-
- c.gridwidth=GridBagConstraints.REMAINDER;
-
- c.fill=GridBagConstraints.BOTH;
-
- txt3 = new TextArea(5,66);
-
- gridbag.setConstraints(txt3,c);
-
- add(txt3);
-
- }
-
-
-
- public boolean action(Event evt, Object arg)
-
- {
-
- if(arg.equals("Cancel"))
-
- {
-
- dispose();
-
- return true;
-
- }
-
- else if(arg.equals("Send")){
-
- if(txt1.getText().length()>0 ||
-
- txt2.getText().length()>0 ||
-
- txt3.getText().length()>0){
-
- String message="Guest: " + txt1.getText() + "\n" +
-
- "Address: " + txt2.getText() + "\n\n" +
-
- txt3.getText() ;
-
-
-
- if(true==send.mailMessage("Guestbook Entry!",message))
-
- appletContext.showStatus("Entry logged into guest book!");
-
- else
-
- appletContext.showStatus("Entry NOT logged.");
-
- }
-
- else appletContext.showStatus("Nothing to send!");
-
- dispose();
-
- return true;
-
- }
-
- return false;
-
- }
-
-
-
- public synchronized boolean handleEvent(Event e)
-
- {
-
- if (e.id == Event.WINDOW_ICONIFY) {
-
- hide();
-
- return true;
-
- }
-
- if (e.id == Event.WINDOW_DESTROY) {
-
- dispose();
-
- return true;
-
- }
-
- return super.handleEvent(e);
-
- }
-
-
-
- public void show()
-
- {
-
- txt1.setText("");
-
- txt2.setText("");
-
- txt3.setText("");
-
- super.show();
-
- }
-
- }
-
-
-
- public class guestbook extends Applet
-
- {
-
- static final int FONTHEIGHT=12;
-
- static final String FONTSTRING="Helvetica";
-
- final String BUTTON = "Guest Book";
-
- final String VERSION = "GUESTBOOK.JAVA - v1.02a - 4 Feb 1996";
-
-
-
- guestbookWindow window=null;
-
- Image image=null;
-
- Button button;
-
- int width, height;
-
- MediaTracker tracker = new MediaTracker(this);
-
-
-
- ///////////////////////////////////////////////////
-
- //Applet parameters - pretty much self-explanatory
-
- ///////////////////////////////////////////////////
-
- public String[][] getParameterInfo()
-
- {
-
- String[][] info = {
-
- {"width", "int", "width of the applet, in pixels"},
-
- {"height", "int", "height of the applet, in pixels"},
-
- {"receiver", "string", "SMTP 'RCPT TO:' parameter <null>"},
-
- {"imageurl", "string", "name of icon to display <null>"},
-
- {"title", "string", "title for popup dialog <Guest Book>"},
-
- };
-
- return info;
-
- }
-
-
-
- /////////////////////////////////////
-
- //Applet name, author and info lines
-
- /////////////////////////////////////
-
- public String getAppletInfo()
-
- {
-
- return ( VERSION + " - simulates a guest log\n" +
-
- "by E-mailing guest data to page owner, by Bill Giel\n" +
-
- "http://www.nai.net/~rvdi/home.htm or rvdi@usa.nai.net\n" +
-
- "Copyright 1996 by William Giel.");
-
- }
-
-
-
-
-
- public void init()
-
- {
-
- String receiver, szImage, szTitle;
-
- URL imageURL=null;
-
-
-
- receiver = getParameter("receiver");
-
- if(null == receiver)
-
- showStatus("No RECEIVER parameter - applet will not log entry!");
-
-
-
- if(null==(szTitle=getParameter("title")))
-
- szTitle="Guest Book";
-
-
-
-
-
- szImage=getParameter("IMAGEURL");
-
-
-
- window=new guestbookWindow(getAppletContext(),getCodeBase().getHost(),receiver);
-
- window.setTitle(szTitle);
-
- window.pack();
-
-
-
- setFont(new Font(FONTSTRING,Font.BOLD + Font.ITALIC,FONTHEIGHT));
-
- add (button = new Button(BUTTON));
-
- width=size().width; height=size().height;
-
-
-
- if(null != szImage){
-
- try{
-
- imageURL=new URL(getDocumentBase(),szImage);
-
- } catch (MalformedURLException e)
-
- {
-
- imageURL=null;
-
- image=null;
-
- }
-
- }
-
-
-
- if(imageURL != null){
-
- image=getImage(imageURL);
-
- if(image != null)
-
- tracker.addImage(image,0);
-
- }
-
- button.move((width-button.size().width)/2,
-
- (width-button.size().width)/2);
-
-
-
- }
-
-
-
- public void paint(Graphics g)
-
- {
-
- Color color=g.getColor();
-
- g.setColor(Color.lightGray);
-
- g.fill3DRect(0,0,size().width,size().height,true);
-
- g.setColor(color);
-
-
-
- if(image != null){
-
- try{
-
- tracker.waitForID(0);
-
- }catch (InterruptedException e)
-
- {
-
- return;
-
- }
-
-
-
- g.drawImage(image,(width-image.getWidth(this))/2,button.size().height
-
- +2*(height-image.getHeight(this)-button.size().height)/3,this);
-
-
-
- }
-
- }
-
-
-
- public boolean action(Event evt, Object arg)
-
- {
-
- if(arg.equals(BUTTON) && !window.isShowing())
-
- {
-
- window.show();
-
- return true;
-
- }
-
- else return false;
-
- }
-
- }
-
-
-
-