home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 1998 October / DPPCPRO1098.ISO / Hotdog / files / snagpack.exe / AFKAL.CLASS (.txt) < prev    next >
Encoding:
Java Class File  |  1997-11-21  |  6.7 KB  |  337 lines

  1. import java.applet.Applet;
  2. import java.applet.AudioClip;
  3. import java.awt.BorderLayout;
  4. import java.awt.Button;
  5. import java.awt.Color;
  6. import java.awt.Component;
  7. import java.awt.Container;
  8. import java.awt.Event;
  9. import java.awt.GridLayout;
  10. import java.awt.Panel;
  11. import java.net.MalformedURLException;
  12. import java.net.URL;
  13. import java.util.Random;
  14.  
  15. public class afkal extends Applet {
  16.    private static final int defaultBorderWidth = 5;
  17.    private static final int defaultVGap = 4;
  18.    private static final int defaultHGap = 4;
  19.    private String encSuccessfulURL;
  20.    private URL successfulURL;
  21.    private String successfulTarget;
  22.    private URL unsuccessfulURL;
  23.    private String unsuccessfulTarget;
  24.    private int numButtons;
  25.    private String passSequence;
  26.    private int numRows;
  27.    private int numCols;
  28.    private int borderWidth;
  29.    private int hGap;
  30.    private int vGap;
  31.    private String regUser;
  32.    private String serialID;
  33.    private int CRC;
  34.    private String prevErrMsg;
  35.    private String key = new String();
  36.    private imagebut[] buttons;
  37.    Panel punchButtonPanel;
  38.    private String currentSequence;
  39.    private AudioClip clickSound;
  40.  
  41.    void addToKey(String var1) {
  42.       String var10001 = this.key;
  43.       this.key = var10001 + var1;
  44.    }
  45.  
  46.    private void printErrMsg(String var1) {
  47.       ((Applet)this).showStatus(var1);
  48.       System.err.println(var1);
  49.       this.prevErrMsg = var1;
  50.       throw new IllegalArgumentException();
  51.    }
  52.  
  53.    private String computeHash(String var1) {
  54.       hasher var2 = new hasher();
  55.       var2.init();
  56.       ((mdigest)var2).updateASCII(var1);
  57.       var2.finish();
  58.       return var2.digout();
  59.    }
  60.  
  61.    private void readParameters() {
  62.       this.regUser = ((Applet)this).getParameter("regUser");
  63.       if (this.regUser == null || this.regUser.equals("Unregistered") && !((Applet)this).getDocumentBase().toExternalForm().substring(0, 5).equals("file:")) {
  64.          this.notRegistered();
  65.       }
  66.  
  67.       this.generateCRC(this.regUser);
  68.       this.addToKey(this.regUser);
  69.  
  70.       try {
  71.          this.numButtons = Integer.parseInt(((Applet)this).getParameter("numButtons"));
  72.       } catch (Exception var9) {
  73.          this.printErrMsg("Invalid number of buttons passed to A.F.K.A.L applet");
  74.       }
  75.  
  76.       try {
  77.          this.numRows = Integer.parseInt(((Applet)this).getParameter("numRows"));
  78.       } catch (Exception var8) {
  79.          this.printErrMsg("Invalid number of rows passed to A.F.K.A.L applet");
  80.       }
  81.  
  82.       try {
  83.          this.numCols = Integer.parseInt(((Applet)this).getParameter("numCols"));
  84.       } catch (Exception var7) {
  85.          this.printErrMsg("Invalid number of columns passed to A.F.K.A.L applet");
  86.       }
  87.  
  88.       if (this.numButtons != this.numRows * this.numCols) {
  89.          this.printErrMsg("Number of rows and columns are not suitable for this number of buttons");
  90.       }
  91.  
  92.       this.encSuccessfulURL = ((Applet)this).getParameter("successfulURL");
  93.       if (this.encSuccessfulURL == null) {
  94.          this.printErrMsg("Missing successful URL passed to A.F.K.A.L applet");
  95.       }
  96.  
  97.       try {
  98.          this.unsuccessfulURL = new URL(((Applet)this).getParameter("unsuccessfulURL"));
  99.       } catch (MalformedURLException var6) {
  100.          this.printErrMsg("Bad unsuccessful URL passed to A.F.K.A.L applet");
  101.       }
  102.  
  103.       this.generateCRC(((Applet)this).getParameter("unsuccessfulURL"));
  104.       this.addToKey(((Applet)this).getParameter("unsuccessfulURL"));
  105.       this.passSequence = ((Applet)this).getParameter("passSequence");
  106.       if (this.passSequence == null) {
  107.          this.printErrMsg("No pass sequence passed to A.F.K.A.L applet");
  108.       }
  109.  
  110.       this.generateCRC(this.passSequence);
  111.       this.addToKey(this.passSequence);
  112.  
  113.       try {
  114.          this.hGap = Integer.parseInt(((Applet)this).getParameter("hGap"));
  115.       } catch (NumberFormatException var5) {
  116.          this.hGap = 4;
  117.       }
  118.  
  119.       try {
  120.          this.vGap = Integer.parseInt(((Applet)this).getParameter("vGap"));
  121.       } catch (NumberFormatException var4) {
  122.          this.vGap = 4;
  123.       }
  124.  
  125.       try {
  126.          this.borderWidth = Integer.parseInt(((Applet)this).getParameter("borderWidth"));
  127.       } catch (NumberFormatException var3) {
  128.          this.borderWidth = 5;
  129.       }
  130.  
  131.       String var1 = ((Applet)this).getParameter("background");
  132.       if (var1 != null) {
  133.          try {
  134.             ((Component)this).setBackground(new Color(Integer.parseInt(var1, 16)));
  135.          } catch (NumberFormatException var2) {
  136.             ((Applet)this).showStatus("A bad background color value was passed to the applet");
  137.             System.err.println("A bad background color value was passed to the applet");
  138.          }
  139.       }
  140.  
  141.       this.successfulTarget = ((Applet)this).getParameter("successfulTarget");
  142.       if (this.successfulTarget == null) {
  143.          this.successfulTarget = "_self";
  144.       }
  145.  
  146.       this.unsuccessfulTarget = ((Applet)this).getParameter("unsuccessfulTarget");
  147.       if (this.unsuccessfulTarget == null) {
  148.          this.unsuccessfulTarget = "_self";
  149.       }
  150.  
  151.    }
  152.  
  153.    private void generateCRC(String var1) {
  154.       for(int var2 = 0; var2 < var1.length(); ++var2) {
  155.          this.CRC += var1.charAt(var2);
  156.       }
  157.  
  158.    }
  159.  
  160.    private void shuffleButtons() {
  161.       Random var1 = new Random();
  162.       boolean[] var2 = new boolean[this.numButtons];
  163.  
  164.       for(int var3 = 0; var3 < this.numButtons; ++var3) {
  165.          var2[var3] = false;
  166.       }
  167.  
  168.       this.punchButtonPanel.removeAll();
  169.  
  170.       for(int var5 = 0; var5 < this.numButtons; ++var5) {
  171.          int var4;
  172.          for(var4 = (int)(var1.nextDouble() * (double)this.numButtons); var2[var4]; var4 = (int)(var1.nextDouble() * (double)this.numButtons)) {
  173.          }
  174.  
  175.          var2[var4] = true;
  176.          this.punchButtonPanel.add(this.buttons[var4]);
  177.       }
  178.  
  179.       this.punchButtonPanel.layout();
  180.       this.punchButtonPanel.validate();
  181.    }
  182.  
  183.    private boolean validateSequence() {
  184.       return this.passSequence.equals(this.computeHash(this.currentSequence));
  185.    }
  186.  
  187.    private void layoutMain() {
  188.       this.punchButtonPanel = new Panel();
  189.       this.punchButtonPanel.setLayout(new GridLayout(this.numRows, this.numCols, this.hGap, this.vGap));
  190.  
  191.       for(int var1 = 0; var1 < this.numButtons; ++var1) {
  192.          this.punchButtonPanel.add(this.buttons[var1]);
  193.       }
  194.  
  195.       Panel var4 = new Panel();
  196.       Button var2 = new Button("OK");
  197.       Button var3 = new Button("Reset");
  198.       ((Container)var4).add(var2);
  199.       ((Container)var4).add(var3);
  200.       ((Container)this).setLayout(new BorderLayout());
  201.       ((Container)this).add("Center", this.punchButtonPanel);
  202.       ((Container)this).add("South", var4);
  203.    }
  204.  
  205.    private void notRegistered() {
  206.       this.printErrMsg("A.F.K.A.L has not been registered and will only run locally");
  207.    }
  208.  
  209.    private String decode(String var1) {
  210.       encoder var2 = new encoder(this.key);
  211.       return var2.decodeFromHexStr(var1);
  212.    }
  213.  
  214.    private boolean checkCRC(String var1) {
  215.       String var2 = Integer.toString(this.CRC);
  216.       if (var2.length() != var1.length()) {
  217.          return false;
  218.       } else {
  219.          String var3 = new String();
  220.  
  221.          for(int var4 = 0; var4 < var2.length(); ++var4) {
  222.             var3 = var3 + (char)(65 + Character.digit(var2.charAt(var4), 10));
  223.          }
  224.  
  225.          return var3.equals(var1);
  226.       }
  227.    }
  228.  
  229.    public void start() {
  230.       if (this.prevErrMsg != null) {
  231.          ((Applet)this).showStatus(this.prevErrMsg);
  232.          System.err.println(this.prevErrMsg);
  233.       } else {
  234.          this.resetSequence();
  235.          ((Applet)this).showStatus("Shuffling buttons...");
  236.          this.shuffleButtons();
  237.          ((Applet)this).showStatus("");
  238.       }
  239.  
  240.    }
  241.  
  242.    public String getAppletInfo() {
  243.       return "A.F.K.A.L V1.0 (c) 1997 Sausage Software";
  244.    }
  245.  
  246.    public void init() {
  247.       this.clickSound = ((Applet)this).getAudioClip(((Applet)this).getDocumentBase(), "click.au");
  248.  
  249.       try {
  250.          this.readParameters();
  251.          this.buttons = new imagebut[this.numButtons];
  252.  
  253.          for(int var1 = 0; var1 < this.numButtons; ++var1) {
  254.             String var2 = ((Applet)this).getParameter("image" + var1);
  255.             if (var2 == null) {
  256.                this.printErrMsg("Not enough image filenames passed to A.F.K.A.L applet");
  257.             }
  258.  
  259.             this.generateCRC(var2);
  260.             this.addToKey(var2);
  261.             this.buttons[var1] = new imagebut(var2, var1, this.borderWidth, this.clickSound, this);
  262.          }
  263.  
  264.          this.serialID = ((Applet)this).getParameter("serialID");
  265.          if (this.serialID == null || !this.regUser.equals("Unregistered") && !this.checkCRC(this.serialID)) {
  266.             this.badSerial();
  267.          }
  268.  
  269.          this.addToKey(this.serialID);
  270.  
  271.          try {
  272.             this.successfulURL = new URL(this.decode(this.encSuccessfulURL));
  273.          } catch (MalformedURLException var3) {
  274.             this.printErrMsg("Bad successful URL passed to A.F.K.A.L applet");
  275.          }
  276.  
  277.          this.layoutMain();
  278.          this.currentSequence = null;
  279.       } catch (IllegalArgumentException var5) {
  280.          while(true) {
  281.             try {
  282.                Thread.sleep(Long.MAX_VALUE);
  283.             } catch (InterruptedException var4) {
  284.             }
  285.          }
  286.       }
  287.    }
  288.  
  289.    public boolean handleEvent(Event var1) {
  290.       if (var1.id == 1001) {
  291.          if (var1.target instanceof imagebut) {
  292.             imagebut var2 = (imagebut)var1.target;
  293.             if (this.currentSequence == null) {
  294.                this.currentSequence = "" + var2.getButtonNum();
  295.             } else {
  296.                String var10001 = this.currentSequence;
  297.                this.currentSequence = var10001 + "+" + var2.getButtonNum();
  298.             }
  299.          }
  300.  
  301.          if (var1.target instanceof Button) {
  302.             if (((Button)var1.target).getLabel().equals("Reset")) {
  303.                this.resetSequence();
  304.             } else if (this.currentSequence != null) {
  305.                if (this.validateSequence()) {
  306.                   ((Applet)this).showStatus("Trying to connect to " + this.successfulURL);
  307.                   if (!this.successfulTarget.equals("_self")) {
  308.                      this.resetSequence();
  309.                      this.shuffleButtons();
  310.                   }
  311.  
  312.                   ((Applet)this).getAppletContext().showDocument(this.successfulURL, this.successfulTarget);
  313.                } else {
  314.                   ((Applet)this).showStatus("Trying to connect to " + this.unsuccessfulURL);
  315.                   if (!this.unsuccessfulTarget.equals("_self")) {
  316.                      this.resetSequence();
  317.                      this.shuffleButtons();
  318.                   }
  319.  
  320.                   ((Applet)this).getAppletContext().showDocument(this.unsuccessfulURL, this.unsuccessfulTarget);
  321.                }
  322.             }
  323.          }
  324.       }
  325.  
  326.       return super.handleEvent(var1);
  327.    }
  328.  
  329.    private void badSerial() {
  330.       this.printErrMsg("A.F.K.A.L applet's serial ID is invalid. Rerun the Windows frontend to generate a new serial ID");
  331.    }
  332.  
  333.    public void resetSequence() {
  334.       this.currentSequence = null;
  335.    }
  336. }
  337.