Advertisement
Guest User

Untitled

a guest
Feb 3rd, 2012
4,033
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.96 KB | None | 0 0
  1.             public void login()
  2.                 throws IOException, LoginFailureException, CorruptStreamException
  3.             {
  4.                 out.streamStart(connection.domain, connection.resource);
  5.                 System.err.println("sent stream start");
  6.                 sendFeatures();
  7.                 System.err.println("sent features");
  8.                 sendAuth();
  9.                 System.err.println("sent auth");
  10.                 in.streamStart();
  11.                 System.err.println("read stream start");
  12.                 String challengeData = readFeaturesAndChallenge();
  13.                 System.err.println("read features and challenge");
  14.                 sendResponse(challengeData);
  15.                 System.err.println("sent response");
  16.                 readSuccess();
  17.             }
  18.  
  19.             private void sendFeatures()
  20.                 throws IOException
  21.             {
  22.                 out.write(new ProtocolTreeNode("stream:features", null, connection.supports_receipt_acks ? (new ProtocolTreeNode[] {
  23.                     new ProtocolTreeNode("receipt_acks", null)
  24.                 }) : null), false);
  25.             }
  26.  
  27.             private void sendAuth()
  28.                 throws IOException
  29.             {
  30.                 ProtocolTreeNode node = new ProtocolTreeNode("auth", new KeyValue[] {
  31.                     new KeyValue("xmlns", "urn:ietf:params:xml:ns:xmpp-sasl"), new KeyValue("mechanism", "DIGEST-MD5-1")
  32.                 });
  33.                 out.write(node);
  34.             }
  35.  
  36.             private String readFeaturesAndChallenge()
  37.                 throws IOException, CorruptStreamException
  38.             {
  39. label0:
  40.                 {
  41.                     ProtocolTreeNode root;
  42.                     boolean server_supports_receipt_acks;
  43. label1:
  44.                     do
  45.                     {
  46.                         for(server_supports_receipt_acks = false; (root = in.nextTree()) != null; server_supports_receipt_acks = root.getChild("receipt_acks") != null)
  47.                             if(!ProtocolTreeNode.tagEquals(root, "stream:features"))
  48.                                 continue label1;
  49.  
  50.                         break label0;
  51.                     } while(!ProtocolTreeNode.tagEquals(root, "challenge"));
  52.                     connection.supports_receipt_acks = connection.supports_receipt_acks && server_supports_receipt_acks;
  53.                     String data = new String(Base64.decode(root.data.getBytes()));
  54.                     return data;
  55.                 }
  56.                 throw new CorruptStreamException("fell out of loop in readFeaturesAndChallenge");
  57.             }
  58.  
  59.             private void sendResponse(String challengeData)
  60.                 throws IOException
  61.             {
  62.                 String response = getResponse(challengeData);
  63.                 ProtocolTreeNode node = new ProtocolTreeNode("response", new KeyValue[] {
  64.                     new KeyValue("xmlns", "urn:ietf:params:xml:ns:xmpp-sasl")
  65.                 }, new String(Base64.encode(response.getBytes())));
  66.                 out.write(node);
  67.             }
  68.  
  69.             private void readSuccess()
  70.                 throws CorruptStreamException, IOException, LoginFailureException
  71.             {
  72.                 ProtocolTreeNode node = in.nextTree();
  73.                 if(ProtocolTreeNode.tagEquals(node, "failure"))
  74.                     throw new LoginFailureException(0);
  75.                 ProtocolTreeNode.require(node, "success");
  76.                 String expiration = node.getAttributeValue("expiration");
  77.                 if(expiration != null)
  78.                     try
  79.                     {
  80.                         connection.expire_date = Long.parseLong(expiration);
  81.                     }
  82.                     catch(NumberFormatException xxx)
  83.                     {
  84.                         throw new IOException("invalid expire date: " + expiration);
  85.                     }
  86.                 String kind = node.getAttributeValue("kind");
  87.                 if("paid".equals(kind))
  88.                     connection.account_kind = 1;
  89.                 else
  90.                 if("free".equals(kind))
  91.                     connection.account_kind = 0;
  92.                 else
  93.                     connection.account_kind = -1;
  94.                 String status = node.getAttributeValue("status");
  95.                 if("expired".equals(status))
  96.                 {
  97.                     LoginFailureException failex = new LoginFailureException(1);
  98.                     failex.expire_date = connection.expire_date;
  99.                     throw failex;
  100.                 }
  101.                 if("active".equals(status))
  102.                 {
  103.                     if(expiration == null)
  104.                         throw new IOException("active account with no expiration");
  105.                 } else
  106.                 {
  107.                     connection.account_kind = -1;
  108.                 }
  109.             }
  110.  
  111.             public WhatsApp(TreeNodeReader r, TreeNodeWriter w, MessageDigest d)
  112.             {
  113.                 super(d, r, w);
  114.             }
  115.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement