Guest User

Untitled

a guest
Nov 8th, 2018
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.72 KB | None | 0 0
  1. Thread d = new Thread(new Runnable() {
  2.  
  3. @Override
  4. public void run() {
  5. SmackConfiguration.setDefaultPacketReplyTimeout(10000);
  6. XMPPTCPConnectionConfiguration config = XMPPTCPConnectionConfiguration.builder()
  7. .setUsernameAndPassword("admin", "password")
  8. .setServiceName("192.168.0.200")
  9. .setHost("192.168.0.200")
  10. .setPort(5223).setSecurityMode(SecurityMode.ifpossible)
  11. .build();
  12.  
  13. AbstractXMPPConnection conn2 = new XMPPTCPConnection(config);
  14. try {
  15. conn2.connect();
  16. conn2.login();
  17.  
  18. Presence presence = new Presence(Presence.Type.unavailable);
  19. presence.setStatus("Gone fishing");
  20. // Send the packet (assume we have an XMPPConnection instance called "con").
  21. conn2.sendStanza(presence);
  22.  
  23. } catch (SmackException | IOException | XMPPException e) {
  24. e.printStackTrace();
  25. Log.d("TAG", e.toString());
  26. }
  27.  
  28. ChatManager chatmanager = ChatManager.getInstanceFor(conn2);
  29. Chat newChat = chatmanager.createChat("harsh@192.168.0.200");
  30.  
  31. try {
  32. newChat.sendMessage("Howdy!");
  33. }
  34. catch (NotConnectedException e) {
  35. e.printStackTrace();
  36. }
  37. }
  38. });
  39.  
  40. d.start();
  41.  
  42. public class NewClientActivity extends Activity {
  43. EditText etUsername, etPassword;
  44. Button bSubmit;
  45. AbstractXMPPConnection mConnection;
  46. ConnectionListener connectionListener = new ConnectionListener() {
  47. @Override
  48. public void connected(XMPPConnection xmppConnection) {
  49. Log.d("xmpp", "connected");
  50. try {
  51. SASLAuthentication.registerSASLMechanism(new SASLMechanism() {
  52. @Override
  53. protected void authenticateInternal(CallbackHandler callbackHandler) throws SmackException {
  54.  
  55. }
  56.  
  57. @Override
  58. protected byte[] getAuthenticationText() throws SmackException {
  59. byte[] authcid = toBytes('u0000' + this.authenticationId);
  60. byte[] passw = toBytes('u0000' + this.password);
  61. return ByteUtils.concact(authcid, passw);
  62. }
  63.  
  64. @Override
  65. public String getName() {
  66. return "PLAIN";
  67. }
  68.  
  69. @Override
  70. public int getPriority() {
  71. return 410;
  72. }
  73.  
  74. @Override
  75. public void checkIfSuccessfulOrThrow() throws SmackException {
  76.  
  77. }
  78.  
  79. @Override
  80. protected SASLMechanism newInstance() {
  81. return this;
  82. }
  83. });
  84. mConnection.login();
  85. } catch (XMPPException e) {
  86. runOnUiThread(new Runnable() {
  87. @Override
  88. public void run() {
  89. Toast.makeText(NewClientActivity.this, "Incorrect username or password", Toast.LENGTH_LONG).show();
  90. }
  91. });
  92. e.printStackTrace();
  93. } catch (SmackException e) {
  94. e.printStackTrace();
  95. } catch (IOException e) {
  96. e.printStackTrace();
  97. }
  98. }
  99.  
  100. @Override
  101. public void authenticated(XMPPConnection xmppConnection, boolean b) {
  102. Log.d("xmpp", "authenticated");
  103. runOnUiThread(new Runnable() {
  104. @Override
  105. public void run() {
  106. Toast.makeText(NewClientActivity.this,"Logged in successfully...",Toast.LENGTH_LONG ).show();
  107. }
  108. });
  109. }
  110.  
  111. @Override
  112. public void connectionClosed() {
  113. Log.d("xmpp", "connection closed");
  114. }
  115.  
  116. @Override
  117. public void connectionClosedOnError(Exception e) {
  118. Log.d("xmpp", "cononection closed on error");
  119. }
  120.  
  121. @Override
  122. public void reconnectionSuccessful() {
  123. Log.d("xmpp", "reconnection successful");
  124. }
  125.  
  126. @Override
  127. public void reconnectingIn(int i) {
  128. Log.d("xmpp", "reconnecting in " + i);
  129. }
  130.  
  131. @Override
  132. public void reconnectionFailed(Exception e) {
  133. Log.d("xmpp", "reconnection failed");
  134. }
  135. };
  136.  
  137. @Override
  138. protected void onCreate(Bundle savedInstanceState) {
  139. super.onCreate(savedInstanceState);
  140. setContentView(R.layout.activity_new_client);
  141. findViews();
  142. }
  143.  
  144. private void findViews() {
  145. etUsername = (EditText) findViewById(R.id.etUsername);
  146. etPassword = (EditText) findViewById(R.id.etPassword);
  147. bSubmit = (Button) findViewById(R.id.bSubmit);
  148. bSubmit.setOnClickListener(new View.OnClickListener() {
  149. @Override
  150. public void onClick(View v) {
  151. String[] params = new String[]{etUsername.getText().toString(), etPassword.getText().toString()};
  152. new Connect().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, params);
  153. }
  154. });
  155. }
  156.  
  157. class Connect extends AsyncTask<String, Void, Void> {
  158. @Override
  159. protected Void doInBackground(String... params) {
  160.  
  161. XMPPTCPConnectionConfiguration config = null;
  162. XMPPTCPConnectionConfiguration.Builder builder = XMPPTCPConnectionConfiguration.builder();
  163. builder.setServiceName("192.168.1.60").setHost("192.168.1.60")
  164. .setDebuggerEnabled(true)
  165. .setPort(5222)
  166. .setUsernameAndPassword(params[0], params[1])
  167. .setSecurityMode(ConnectionConfiguration.SecurityMode.disabled)
  168. .setCompressionEnabled(false);
  169. config = builder.build();
  170.  
  171. mConnection = new XMPPTCPConnection(config);
  172. try {
  173. //mConnection.setPacketReplyTimeout(10000);
  174. mConnection.addConnectionListener(connectionListener);
  175. mConnection.connect();
  176. } catch (SmackException e) {
  177. e.printStackTrace();
  178. } catch (IOException e) {
  179. e.printStackTrace();
  180. } catch (XMPPException e) {
  181. e.printStackTrace();
  182. }
  183. return null;
  184. }
  185. }
  186. }
  187.  
  188. private class MyLoginTask extends AsyncTask<String, String, String> {
  189. @Override
  190. protected String doInBackground(String... params) {
  191. // Create a connection to the jabber.org server.
  192. XMPPTCPConnectionConfiguration config = XMPPTCPConnectionConfiguration.builder()
  193. .setSecurityMode(ConnectionConfiguration.SecurityMode.disabled)
  194. .setUsernameAndPassword(USERNAME, PASSWORD)
  195. .setHost(HOST)
  196. //.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled)
  197. .setServiceName(HOST)
  198. .setDebuggerEnabled(true) // to view what's happening in detail
  199. .build();
  200.  
  201. AbstractXMPPConnection conn1 = new XMPPTCPConnection(config);
  202. conn1.setPacketReplyTimeout(30000);
  203. try {
  204. conn1.connect();
  205. if(conn1.isConnected()) {
  206. Log.w("app", "conn done");
  207. }
  208. conn1.login();
  209.  
  210. if(conn1.isAuthenticated()) {
  211. Log.w("app", "Auth done");
  212. }
  213. }
  214. catch (Exception e) {
  215. Log.w("app", e.toString());
  216. }
  217.  
  218. return "";
  219. }
  220.  
  221.  
  222. @Override
  223. protected void onPostExecute(String result) {
  224. }
  225.  
  226. }
  227.  
  228. public static void main(String[] args) throws IOException, XMPPException, SmackException, NoSuchAlgorithmException {
  229. XMPPTCPConnectionConfiguration config = XMPPTCPConnectionConfiguration.builder()
  230. .setServiceName("olx.intr")
  231. .setDebuggerEnabled(true)
  232. .setSecurityMode(ConnectionConfiguration.SecurityMode.disabled)
  233. .setHost("10.2.2.107")
  234. .setPort(5222)
  235. .setUsernameAndPassword("dhiren","olxindia")
  236. .build();
  237.  
  238. AbstractXMPPConnection conn2 = new XMPPTCPConnection(config);
  239. conn2.connect();
  240. SASLMechanism mechanism = new SASLDigestMD5Mechanism();
  241. SASLAuthentication.registerSASLMechanism(mechanism);
  242. SASLAuthentication.blacklistSASLMechanism("SCRAM-SHA-1");
  243. SASLAuthentication.unBlacklistSASLMechanism("DIGEST-MD5");
  244. try {
  245. conn2.login();
  246. }catch (Exception e){
  247. e.printStackTrace();
  248. }
  249. boolean authenticated = conn2.isAuthenticated();
  250. System.out.println(authenticated);
  251. }
Add Comment
Please, Sign In to add comment