import java.awt.Graphics;
import java.awt.Image;
import java.awt.Color;
import java.awt.Event;
import java.awt.Font;
import java.net.*;
import java.awt.*;
import java.io.*;

// ****************************************************
// written by Troy Errthum
// hack a part all ya like!
// more info at:  http://www.errthum.com/troy/spamsux/
// ****************************************************

public class SpamSux extends java.applet.Applet {

// ****************************************************
// global variables
// ****************************************************

        Font f;
        Color forecolor;
        Color linkcolor;
        Image background;

        String[] email1 = new String[99];
        String[] email2 = new String[99];
        String[] email3 = new String[99];
        String[] link1 = new String[99];
        String[] link2 = new String[99];
        String maker;
        String picture;

		int fontsize;
        int infore;
        int inback;
        int inlink;
        int k = 1;
        int go = 1;
        int column0;
        int column1;
        int column2;
        int row0;
        int row1;

// ****************************************************
// record last mouse position
// ****************************************************
        public boolean mouseMove(Event evt, int c, int d) {
            float index = ((d - row0 + row1)/(row1));
            int place = (int)index;
            int curse = 0;
        	if ((c < (column1)) && (email2[place+1] != null) && (email3[place+1] != null)) {
				setCursor(Cursor.getPredefinedCursor(Cursor.TEXT_CURSOR));
				getAppletContext().showStatus("Click name to copy email to clipboard.");
				curse=1;
            }
			if ((c < (column2)) && (c > (column1)) && (email2[place+1] != null) && (email3[place+1] != null)) {
				setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
				getAppletContext().showStatus("mailto:" + email2[place+1] + "@" + email3[place+1]);
				curse=1;
            }
            if ((c > (column2)) && (link2[place+1] != null)) {
				setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
				getAppletContext().showStatus(link2[place+1]);
				curse=1;
            }
            if (curse == 0) {
				setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
				getAppletContext().showStatus("These emails are hidden from spambots.");
			}

			return true;
        }

// ****************************************************
// email on mouse clicks
// ****************************************************
        public boolean mouseDown(Event evt, int e, int f) {
                float index = ((f - row0 + row1)/(row1));
                int place = (int)index;
        	if ((e < (column1)) && (email2[place+1] != null) && (email3[place+1] != null))
                        popMessage(email2[place+1] + "@" + email3[place+1]);
		if ((e < (column2)) && (e > (column1)) && (email2[place+1] != null) && (email3[place+1] != null)) {
			try {
          			getAppletContext().showDocument(new URL("mailto:" + email2[place+1] + "@" + email3[place+1]));
          		}
                        catch (Exception ex) {}
                }
                if (e > (column2)) {
			try {
          			getAppletContext().showDocument(new URL(link2[place+1]));
          		}
                        catch (Exception ex) {}
                }
                return true;
        }

// ****************************************************
// initialize
// ****************************************************
	public void init() {

		int style = Font.BOLD;

		maker = getParameter("author");

		String paramsize = getParameter("size");
                if (paramsize == null)
                        fontsize = 16;
                else
                        fontsize = Integer.parseInt(paramsize);

		String paramrow0 = getParameter("row0");
                if (paramrow0 == null)
                        row0 = fontsize;
                else
                        row0 = Integer.parseInt(paramrow0);

		String paramrow1 = getParameter("row1");
                if (paramrow1 == null)
                        row1 = fontsize;
                else
                        row1 = Integer.parseInt(paramrow1);

		String paramcol0 = getParameter("col0");
                if (paramcol0 == null)
                        column0 = fontsize;
                else
                        column0 = Integer.parseInt(paramcol0);

		String paramcol1 = getParameter("col1");
                if (paramcol1 == null)
                        column1 = fontsize * 15;
                else
                        column1 = Integer.parseInt(paramcol1);

		String paramcol2 = getParameter("col2");
                if (paramcol2 == null)
                        column2 = fontsize * 35;
                else
                        column2 = Integer.parseInt(paramcol2);

		String paramstyle = getParameter("style");
                if (paramstyle != null) {
                	if (paramstyle.equals("PLAIN"))
				style = Font.PLAIN;
                        if (paramstyle.equals("ITALIC"))
                                style = Font.ITALIC;
                        if (paramstyle.equals("BOLD"))
                                style = Font.BOLD;
                        if (paramstyle.equals("BOTH"))
                                style = Font.BOLD + Font.ITALIC;
                }
                else
                        style = Font.BOLD;

		String paramfont = getParameter("font");
                if (paramfont != null)
                	f = new Font(paramfont, style, fontsize);
                else
                        f = new Font("TimesRoman", style, fontsize);

		String paramback = getParameter("bgcolor");
                if (paramback == null)
                        paramback = "FFFFFF";
                Color backcolor = new Color(Integer.valueOf(String.valueOf(paramback), 16).intValue());
    		setBackground(backcolor);

		String paramfore = getParameter("fgcolor");
                if (paramfore == null)
                        paramfore = "000000";
                forecolor = new Color(Integer.valueOf(String.valueOf(paramfore), 16).intValue());

		String paramlink = getParameter("lxcolor");
                if (paramlink == null)
                        paramlink = "0000FF";
                linkcolor = new Color(Integer.valueOf(String.valueOf(paramlink), 16).intValue());

		String paramtext = getParameter("infile");

		if (paramtext != null)
		{
			String thisLine;   // Read and parse text field from file
			try
			{
				String slink = new String(paramtext + "spamsux.txt");
				URLConnection link = (new URL(slink).openConnection());
				InputStreamReader fin =  new InputStreamReader(link.getInputStream());
				BufferedReader myInput = new BufferedReader(fin);
				while ((thisLine = myInput.readLine()) != null)
				{
					int mark1 = thisLine.indexOf(';');
					int mark2 = thisLine.indexOf(';',mark1 + 1);
					int mark3 = thisLine.indexOf(';',mark2 + 1);
					int mark4 = thisLine.indexOf(';',mark3 + 1);
					int mark5 = thisLine.indexOf(';',mark4 + 1);

					email1[k] = thisLine.substring(0,mark1);
					email2[k] = thisLine.substring(mark1 + 1,mark2);
					email3[k] = thisLine.substring(mark2 + 1,mark3);
					link1[k] = thisLine.substring(mark3 + 1,mark4);
					link2[k] = thisLine.substring(mark4 + 1,mark5);

					if (email2[k].equals(""))
					{
						email2[k] = null;
						email3[k] = null;
					}
					if (link1[k].equals(""))
					{
						link1[k] = null;
						link2[k] = null;
					}

					k++;
				}
				k++;
			}
			catch (Exception e)
			{
				e.printStackTrace();
			}
		}
		else //old way with parms
		{
			while (go == 1)
			{
				email1[k] = getParameter("rname" + k);
				email2[k] = getParameter("netid" + k);
				email3[k] = getParameter("email" + k);
				link1[k] = getParameter("linkn" + k);
				link2[k] = getParameter("linku" + k);
				if (email1[k] == null)
						go = 0;
				k++;
            }
		}

                picture = getParameter("picture");
                if (picture != null)
                	background = getImage(getCodeBase(), String.valueOf(picture));
	}



// ****************************************************
// display image
// ****************************************************
        public void paint(Graphics screen) {

                if (picture != null)
			for (int m = 0; m < getSize().width; m += background.getWidth(this)) {
                        	for (int n = 0; n < getSize().height; n += background.getHeight(this)) {
                                        screen.drawImage(background, m, n, this);
                                }
                        }
		screen.setFont(f);
		if (maker.equals("Troy Errthum")) {
                	for (int j = 0; j < (k-2); j++) {
                                screen.setColor(forecolor);
                        	screen.drawString(email1[j+1], column0, ((row1) * j + row0));
                                screen.setColor(linkcolor);
                        	if ((email2[j+1] != null) && (email3[j+1] != null))
					screen.drawString(email2[j+1] + "@" + email3[j+1], column1, ((row1) * j + row0));
                                if (link1[j+1] != null)
					screen.drawString(link1[j+1], column2, ((row1) * j + row0));
                	} ;
                }
        }

// ****************************************************
// pop up message box for copy/paste
// ****************************************************
        void popMessage(String copier) {
                CopyMe message = new CopyMe(new Frame("") , copier, true);
     		requestFocus();
     		if (message.id)
        		System.out.println("Ok pressed");
     		message.dispose();
        }
}