Advertisement
Guest User

javafibsppp

a guest
Apr 22nd, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 12.69 KB | None | 0 0
  1. package com.fibs.javafibs.model;
  2.  
  3. import javax.swing.ImageIcon;
  4.  
  5. import com.fibs.javafibs.gui.util.IconHandler;
  6.  
  7. public class PPPInfo {
  8.  
  9.     // ////////////////////////////////////////////////////////
  10.     // Constructor, Object state and defaulting
  11.     // ////////////////////////////////////////////////////////
  12.  
  13.     final private Gender gender;
  14.     final private PPPClient client;
  15.     final private String countryCode;
  16.     final private String givenName;
  17.     final private String familyName;
  18.  
  19.     public PPPInfo() {
  20.         this(null, null, null, null, null);
  21.     }
  22.  
  23.     public PPPInfo(String rawUserName) {
  24.         this(parseGivenName(rawUserName), parseFamilyName(rawUserName), parseCountry(rawUserName),
  25.                 parseGender(rawUserName), parsePPPClient(rawUserName));
  26.     }
  27.  
  28.     public PPPInfo(String givenName, String familyName, String countryCode, Gender gender) {
  29.         this(givenName, familyName, countryCode, gender, PPPInfo.PPPClient.JAVAFIBS2001);
  30.     }
  31.  
  32.     public PPPInfo(String givenName, String familyName, String countryCode, Gender gender, PPPClient client) {
  33.         this.givenName = givenName == null ? "" : givenName;
  34.         this.familyName = familyName == null ? "" : familyName;
  35.         this.gender = gender == null ? Gender.GENDER_UNKNOWN : gender;
  36.         this.client = (client == null) ? PPPClient.JAVAFIBS2001 : client;
  37.  
  38.         if (countryCode == null) {
  39.             this.countryCode = "NT";
  40.         } else if (countryCode.length() == 2) {
  41.             this.countryCode = countryCode;
  42.         } else {
  43.             this.countryCode = countryCode.substring(0, 2);
  44.         }
  45.     }
  46.  
  47.     // ///////////////////////////////////////////////////////////////////////////////
  48.     // Inner classes, Enum style
  49.     // ///////////////////////////////////////////////////////////////////////////////
  50.  
  51.     final public static class PPPClient {
  52.  
  53.         public final static PPPClient JAVAFIBS2001 = new PPPClient('!', "JavaFIBS 2001");
  54.  
  55.         private final static PPPClient[] KNOWN_PPP_CLIENTS = {
  56.         // JF code range
  57.                 // Char20: Specifies the client using this protocol.
  58.                 // (0100001 - 0100111 = JavaFIBS)
  59.                 // ! = 33 - 0x21 - 041 - %00100001
  60.                 // " = 34 - 0x22 - 042 - %00100010
  61.                 // # = 35 - 0x23 - 043 - %00100011
  62.                 // $ = 36 - 0x24 - 044 - %00100100
  63.                 // % = 37 - 0x25 - 045 - %00100101
  64.                 // & = 38 - 0x26 - 046 - %00100110 - &
  65.                 // ' = 39 - 0x27 - 047 - %00100111
  66.  
  67.                 JAVAFIBS2001, new PPPClient('"', "JavaFIBS Applet"), new PPPClient('#', "JavaFIBS"),
  68.                 // "JavaFIBS 2001 Applet", never released
  69.                 new PPPClient('$', "JavaFIBS (Reserved)"),
  70.                 // JavaFIBS 2001 (Registered), never released
  71.                 new PPPClient('%', "JavaFIBS (Reserved)"),
  72.                 // JavaFIBS 2oo3D, never released
  73.                 new PPPClient('&', "JavaFIBS (Reserved)"), new PPPClient('\'', "JavaFIBS (Reserved)"),
  74.  
  75.                 // Other clients supporting PPP
  76.  
  77.                 // used by boomslang's bots
  78.                 new PPPClient('x', "JavaCLIPLib"),
  79.                 // Delfibs (as per March 2008)
  80.                 new PPPClient('D', "Delfibs"),
  81.                 // =NTTourney1rganizer2
  82.                 new PPPClient('2', "TourneyBot") };
  83.  
  84.         private final char id;
  85.         private final String name;
  86.  
  87.         protected PPPClient(char id, String name) {
  88.             this.name = name;
  89.             this.id = id;
  90.         }
  91.  
  92.         protected static PPPClient getById(char id) {
  93.             for (int i = 0; i < KNOWN_PPP_CLIENTS.length; i++) {
  94.                 if (KNOWN_PPP_CLIENTS[i].id == id) {
  95.                     return KNOWN_PPP_CLIENTS[i];
  96.                 }
  97.             }
  98.             return JAVAFIBS2001;
  99.         }
  100.  
  101.         public char getId() {
  102.             return id;
  103.         }
  104.  
  105.         public String getName() {
  106.             return name;
  107.         }
  108.  
  109.         // FIXME: JDK 1.5 @Override
  110.         public boolean equals(Object o) {
  111.             if (this == o) {
  112.                 return true;
  113.             }
  114.             if (!(o instanceof PPPClient)) {
  115.                 return false;
  116.             }
  117.             return ((PPPClient) o).id == this.id;
  118.         }
  119.     }
  120.  
  121.     final public static class Gender {
  122.  
  123.         /*
  124.          * Char1: Identifies this protocol and holds the gender of the player
  125.          *
  126.          * 01111 xy ----- -- | | | specifies the gender (00 = male ">", 10 = female "<",
  127.          * 01 = computer "=", 11 = unknown "?") specifies that this protocol is used
  128.          * (60) (along with a string length of 20).
  129.          */
  130.  
  131.         private static final int GENDER_MASK = 3;
  132.  
  133.         public final static Gender GENDER_MALE = new Gender(0, "Male", IconHandler
  134.                 .getIcon(IconHandler.GENDER_MALE_ICON));
  135.         public final static Gender GENDER_COMPUTER = new Gender(1, "Computer", IconHandler
  136.                 .getIcon(IconHandler.GENDER_COMPUTER_ICON));
  137.         public final static Gender GENDER_FEMALE = new Gender(2, "Female", IconHandler
  138.                 .getIcon(IconHandler.GENDER_FEMALE_ICON));
  139.         public final static Gender GENDER_UNKNOWN = new Gender(3, "Unknown", IconHandler
  140.                 .getIcon(IconHandler.GENDER_UNKNOWN_ICON));
  141.  
  142.         final private int id;
  143.         final private String labelText;
  144.         final private ImageIcon imageIcon;
  145.  
  146.         protected Gender(int id, String labelText, ImageIcon imageIcon) {
  147.             this.id = id & GENDER_MASK;
  148.             this.labelText = labelText;
  149.             this.imageIcon = imageIcon;
  150.         }
  151.  
  152.         public static Gender getById(int id) {
  153.             switch (id & GENDER_MASK) {
  154.             case 0:
  155.                 return GENDER_MALE;
  156.             case 1:
  157.                 return GENDER_COMPUTER;
  158.             case 2:
  159.                 return GENDER_FEMALE;
  160.             default:
  161.                 return GENDER_UNKNOWN;
  162.             }
  163.         }
  164.  
  165.         // FIXME: make Locale dependend
  166.         public String getLabelText() {
  167.             return labelText;
  168.         }
  169.  
  170.         public ImageIcon getIcon() {
  171.             return imageIcon;
  172.         }
  173.  
  174.         public int getId() {
  175.             return id;
  176.         }
  177.  
  178.         // FIXME: JDK 1.5 @Override
  179.         public boolean equals(Object o) {
  180.             if (this == o) {
  181.                 return true;
  182.             }
  183.             if (!(o instanceof Gender)) {
  184.                 return false;
  185.             }
  186.             return ((Gender) o).id == this.id;
  187.         }
  188.     }
  189.  
  190.     // //////////////////////////////////////////////////////////
  191.     // Parsing constants
  192.     // //////////////////////////////////////////////////////////
  193.  
  194.     private static final int FLAGS_CHAR_POSITION = 0;
  195.     private static final int NAME_START_POSITION = 3;
  196.     private static final int NAME_END_POSITION = 18;
  197.     private static final int CLIENT_CHAR_POSITION = 19;
  198.     private static final int PROTOCOL_LENGTH = 20;
  199.  
  200.     private final static int NAME_LENGTH = 16;
  201.  
  202.     private static final int PROTOCOL_MASK = 124;
  203.     private static final int PROTOCOL_ID = 60;
  204.     private static final int FAMILY_NAME_FIRST_LETTER_CORRECTION = 30;
  205.  
  206.     // //////////////////////////////////////////////////////////
  207.     // Public Utility methods
  208.     // //////////////////////////////////////////////////////////
  209.  
  210.     /**
  211.      * Checks to see if this string is a PPP string
  212.      */
  213.     public static boolean isPPP(String pppstring) {
  214.         return pppstring != null && pppstring.length() == PROTOCOL_LENGTH
  215.                 && ((int) (pppstring.charAt(0)) & PROTOCOL_MASK) == PROTOCOL_ID;
  216.     }
  217.  
  218.     // //////////////////////////////////////////////////////////
  219.     // Actual parsing
  220.     // //////////////////////////////////////////////////////////
  221.  
  222.     public static Gender parseGender(String rawUserName) {
  223.         try {
  224.             return Gender.getById(rawUserName.charAt(FLAGS_CHAR_POSITION));
  225.         } catch (Exception e) {
  226.             return Gender.GENDER_UNKNOWN;
  227.         }
  228.     }
  229.  
  230.     public static PPPClient parsePPPClient(String rawUserName) {
  231.         try {
  232.             return PPPClient.getById(rawUserName.charAt(CLIENT_CHAR_POSITION));
  233.         } catch (Exception e) {
  234.             return PPPClient.JAVAFIBS2001;
  235.         }
  236.     }
  237.  
  238.     public static String parseCountry(String rawUserName) {
  239.         try {
  240.             return rawUserName.substring(1, NAME_START_POSITION).trim().substring(0, 2);
  241.         } catch (Exception e) {
  242.             return "NT";
  243.         }
  244.     }
  245.  
  246.     public static String parseGivenName(String rawUserName) {
  247.         try {
  248.             // Case: empty name
  249.             if (rawUserName.charAt(NAME_START_POSITION) == '_') {
  250.                 return "";
  251.             }
  252.  
  253.             for (int i = NAME_START_POSITION; i <= NAME_END_POSITION; i++) {
  254.                 final char c = rawUserName.charAt(i);
  255.                 // Given name ends on start of family name
  256.                 if (c < 'A') {
  257.                     return decodeName(rawUserName.substring(NAME_START_POSITION, i).trim());
  258.                 }
  259.             }
  260.             // We did not find a family name, so all the string is the given name
  261.             int givenNameEnd = NAME_END_POSITION;
  262.             while (rawUserName.charAt(--givenNameEnd) == '_') {
  263.             }
  264.             return rawUserName.substring(NAME_START_POSITION, givenNameEnd+1);
  265.         } catch (Exception e) {
  266.             return "";
  267.         }
  268.     }
  269.  
  270.     public static String parseFamilyName(String rawUserName) {
  271.         try {
  272.             // Case: empty name
  273.             if (rawUserName.charAt(NAME_START_POSITION) == '_') {
  274.                 return "";
  275.             }
  276.            
  277.             // Find end of firstName section
  278.             int familyNameStart;
  279.             for (familyNameStart = NAME_START_POSITION; familyNameStart <= NAME_END_POSITION; familyNameStart++) {
  280.                 // Given name ends on either end of name (_), ort start of family name
  281.                 if (rawUserName.charAt(familyNameStart) < 'A') {
  282.                     // Case: we only got a given name
  283.                     break;
  284.                 }
  285.             }
  286.             // Check if we do have a familly name at all
  287.             if (familyNameStart+1 >= NAME_END_POSITION) {
  288.                 return "";
  289.             }
  290.            
  291.             int familyNameEnd;
  292.             for (familyNameEnd = NAME_END_POSITION; familyNameEnd >= familyNameStart; familyNameEnd-- ) {
  293.                 if (rawUserName.charAt(familyNameEnd) != '_') {
  294.                     break;
  295.                 }
  296.             }
  297.             return decodeFamilyName(rawUserName.substring(familyNameStart, familyNameEnd+1));
  298.         } catch (Exception e) {
  299.             return "";
  300.         }
  301.     }
  302.  
  303.     // ///////////////////////////////////////////////////////////////
  304.     // Getter
  305.     // ///////////////////////////////////////////////////////////////
  306.  
  307.     public Gender getGender() {
  308.         return gender;
  309.     }
  310.  
  311.     public PPPClient getClient() {
  312.         return client;
  313.     }
  314.  
  315.     public String getCountryCode() {
  316.         return countryCode;
  317.     }
  318.  
  319.     public String getGivenName() {
  320.         return givenName;
  321.     }
  322.  
  323.     public String getFamilyName() {
  324.         return familyName;
  325.     }
  326.  
  327.     // /////////////////////////////////////////////////////////////////////////
  328.     // Overrides
  329.     // /////////////////////////////////////////////////////////////////////////
  330.  
  331.     // JDK 1.5: @Override
  332.     public String toString() {
  333.         try {
  334.             String s = new StringBuffer().append((char) (60 + gender.getId()))
  335.                     .append(countryCode.substring(0, 2)) // First 2 letters
  336.                     // only
  337.                     .append(encodeNames()).append(client.getId()).toString();
  338.             return s;
  339.         } catch (Exception e) {
  340.             e.printStackTrace();
  341.             return "?NT________________!";
  342.         }
  343.  
  344.     }
  345.  
  346.     ////////////////////////////////////////////////////////////////////////////////
  347.     // Dirty detail section for the toString() and parsing methods
  348.     ////////////////////////////////////////////////////////////////////////////////
  349.  
  350.     protected String encodeNames() throws IndexOutOfBoundsException, NullPointerException {
  351.  
  352.         if ("".equals(familyName) && !"".equals(givenName)) {
  353.             return padNames(encodeName(givenName));
  354.         } else if (!"".equals(familyName) && "".equals(givenName)) {
  355.             return padNames(encodeFamilyName(familyName));
  356.         }
  357.  
  358.         if (givenName.length() + familyName.length() <= NAME_LENGTH) {
  359.             return padNames(encodeName(givenName) + encodeFamilyName(familyName));
  360.         }
  361.         if (familyName.length() + 1 <= NAME_LENGTH) {
  362.             // Case: We use 1 initial and the complete family name (P
  363.             // Longstockings)
  364.             return padNames("" + givenName.charAt(0) + encodeFamilyName(familyName));
  365.         }
  366.         if (givenName.length() + 1 <= NAME_LENGTH) {
  367.             // Case: We use the given name and the familiy name initial (Pippi
  368.             // L)
  369.             return padNames(encodeName(givenName) + encodeFamilyName("" + familyName.charAt(0)));
  370.         }
  371.         // last resort, we truncate to initials for both (P L)
  372.  
  373.         return padNames("" + givenName.charAt(0) + encodeFamilyName("" + familyName.charAt(0)));
  374.     }
  375.  
  376.     private final static String padNames(String names) throws IndexOutOfBoundsException {
  377.         int len = names.length();
  378.         if (len == 0) {
  379.             return "________________"; // 16 _
  380.         } else if (len < NAME_LENGTH) {
  381.             StringBuffer sb = new StringBuffer(names);
  382.             while (sb.length() < NAME_LENGTH) {
  383.                 sb.append('_');
  384.             }
  385.             return sb.toString();
  386.         } else if (len > NAME_LENGTH) {
  387.             return names.substring(0, NAME_LENGTH);
  388.         } else if (len == NAME_LENGTH) {
  389.             return names;
  390.         } else {
  391. //          assert "Can not happen";
  392.             return "________________";
  393.         }
  394.     }
  395.  
  396.     protected final static String encodeFamilyName(final String name) throws IndexOutOfBoundsException,
  397.             NullPointerException {
  398.         return "".equals(name) ? "" : new StringBuffer(name.length()).append(
  399.                 (char) (name.charAt(0) - FAMILY_NAME_FIRST_LETTER_CORRECTION)).append(encodeName(name.substring(1))).toString();
  400.     }
  401.  
  402.     protected final static String decodeFamilyName(final String name) throws IndexOutOfBoundsException,
  403.             NullPointerException {
  404.         return "".equals(name) ? "" : new StringBuffer(name.length()).append(
  405.                 (char) (name.charAt(0) + FAMILY_NAME_FIRST_LETTER_CORRECTION)).append(decodeName(name.substring(1)))
  406.                 .toString();
  407.     }
  408.    
  409.     protected final static String encodeName(final String name) throws NullPointerException {
  410.         return (name.indexOf(' ') != -1) ? name.replace(' ', '_') : name;      
  411.     }
  412.  
  413.     protected final static String decodeName(final String name) throws NullPointerException {
  414.         return (name.indexOf('_') != -1) ? name.replace('_', ' ') : name;      
  415.     }
  416. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement