From 3d9294684c739cc54de8dfa08ad22097ca8a1811 Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Sun, 2 Feb 2014 17:53:34 +0100 Subject: [PATCH] tls is no optional --- src/de/gultsch/chat/entities/Account.java | 6 ++ .../chat/services/XmppConnectionService.java | 90 ++++++++++--------- .../gultsch/chat/ui/ConversationFragment.java | 7 +- src/de/gultsch/chat/xmpp/MessagePacket.java | 21 ++++- src/de/gultsch/chat/xmpp/XmppConnection.java | 27 +++--- 5 files changed, 91 insertions(+), 60 deletions(-) diff --git a/src/de/gultsch/chat/entities/Account.java b/src/de/gultsch/chat/entities/Account.java index dcb5516a4..79712b155 100644 --- a/src/de/gultsch/chat/entities/Account.java +++ b/src/de/gultsch/chat/entities/Account.java @@ -16,6 +16,8 @@ public class Account extends AbstractEntity{ public static final String OPTIONS = "options"; public static final String ROSTERVERSION = "rosterversion"; + public static final int OPTION_USETLS = 0; + protected String username; protected String server; protected String password; @@ -40,6 +42,10 @@ public class Account extends AbstractEntity{ this.rosterVersion = rosterVersion; } + public boolean isOptionSet(int option) { + return ((options & (1 << option)) != 0); + } + public String getUsername() { return username; } diff --git a/src/de/gultsch/chat/services/XmppConnectionService.java b/src/de/gultsch/chat/services/XmppConnectionService.java index 2fc3c937e..d7ee76eec 100644 --- a/src/de/gultsch/chat/services/XmppConnectionService.java +++ b/src/de/gultsch/chat/services/XmppConnectionService.java @@ -46,18 +46,19 @@ public class XmppConnectionService extends Service { @Override public void onMessagePacketReceived(Account account, MessagePacket packet) { - String fullJid = packet.getFrom(); - String jid = fullJid.split("/")[0]; - String name = jid.split("@")[0]; - Log.d(LOGTAG,"message received for "+account.getJid()+" from "+jid); - Log.d(LOGTAG,packet.toString()); - Contact contact = new Contact(account,name,jid,null); //dummy contact - Conversation conversation = findOrCreateConversation(account, contact); - Message message = new Message(conversation, fullJid, packet.getBody(), Message.ENCRYPTION_NONE, Message.STATUS_RECIEVED); - conversation.getMessages().add(message); - databaseBackend.createMessage(message); - if (convChangedListener != null) { - convChangedListener.onConversationListChanged(); + if (packet.getType()==MessagePacket.TYPE_CHAT) { + Log.d(LOGTAG,account.getJid()+": message of type chat"); + String fullJid = packet.getFrom(); + String jid = fullJid.split("/")[0]; + String name = jid.split("@")[0]; + Contact contact = new Contact(account,name,jid,null); //dummy contact + Conversation conversation = findOrCreateConversation(account, contact); + Message message = new Message(conversation, fullJid, packet.getBody(), Message.ENCRYPTION_NONE, Message.STATUS_RECIEVED); + conversation.getMessages().add(message); + databaseBackend.createMessage(message); + if (convChangedListener != null) { + convChangedListener.onConversationListChanged(); + } } } }; @@ -117,36 +118,41 @@ public class XmppConnectionService extends Service { } public void getRoster(final Account account, final OnRosterFetchedListener listener) { - IqPacket iqPacket = new IqPacket(IqPacket.TYPE_GET); - Element query = new Element("query"); - query.setAttribute("xmlns", "jabber:iq:roster"); - query.setAttribute("ver", ""); - iqPacket.addChild(query); - try { - connections.get(account).sendIqPacket(iqPacket, new OnIqPacketReceived() { - - @Override - public void onIqPacketReceived(Account account, IqPacket packet) { - Element roster = packet.findChild("query"); - Log.d(LOGTAG,roster.toString()); - List contacts = new ArrayList(); - for(Element item : roster.getChildren()) { - String name = item.getAttribute("name"); - String jid = item.getAttribute("jid"); - if (name==null) { - name = jid.split("@")[0]; - } - Contact contact = new Contact(account, name, jid, null); - contacts.add(contact); - } - if (listener != null) { - listener.onRosterFetched(contacts); - } - } - }); - } catch (IOException e) { - Log.d(LOGTAG,"io error during roster fetch"); - } + new Thread() { + @Override + public void run() { + IqPacket iqPacket = new IqPacket(IqPacket.TYPE_GET); + Element query = new Element("query"); + query.setAttribute("xmlns", "jabber:iq:roster"); + query.setAttribute("ver", ""); + iqPacket.addChild(query); + try { + connections.get(account).sendIqPacket(iqPacket, new OnIqPacketReceived() { + + @Override + public void onIqPacketReceived(Account account, IqPacket packet) { + Element roster = packet.findChild("query"); + Log.d(LOGTAG,roster.toString()); + List contacts = new ArrayList(); + for(Element item : roster.getChildren()) { + String name = item.getAttribute("name"); + String jid = item.getAttribute("jid"); + if (name==null) { + name = jid.split("@")[0]; + } + Contact contact = new Contact(account, name, jid, null); + contacts.add(contact); + } + if (listener != null) { + listener.onRosterFetched(contacts); + } + } + }); + } catch (IOException e) { + Log.d(LOGTAG,"io error during roster fetch"); + } + } + }.start(); } public void addConversation(Conversation conversation) { diff --git a/src/de/gultsch/chat/ui/ConversationFragment.java b/src/de/gultsch/chat/ui/ConversationFragment.java index d6398c823..990c38b6b 100644 --- a/src/de/gultsch/chat/ui/ConversationFragment.java +++ b/src/de/gultsch/chat/ui/ConversationFragment.java @@ -124,8 +124,11 @@ public class ConversationFragment extends Fragment { } else { imageView.setImageURI(profilePicture); } - ((TextView) view.findViewById(R.id.message_body)).setText(item - .getBody().trim()); + TextView messageBody = (TextView) view.findViewById(R.id.message_body); + String body = item.getBody(); + if (body!=null) { + messageBody.setText(body.trim()); + } TextView time = (TextView) view.findViewById(R.id.message_time); if (item.getStatus() == Message.STATUS_UNSEND) { time.setTypeface(null, Typeface.ITALIC); diff --git a/src/de/gultsch/chat/xmpp/MessagePacket.java b/src/de/gultsch/chat/xmpp/MessagePacket.java index de486a5da..ab0c05c3d 100644 --- a/src/de/gultsch/chat/xmpp/MessagePacket.java +++ b/src/de/gultsch/chat/xmpp/MessagePacket.java @@ -4,6 +4,8 @@ import de.gultsch.chat.xml.Element; public class MessagePacket extends Element { public static final int TYPE_CHAT = 0; + public static final int TYPE_UNKNOWN = 1; + public static final int TYPE_NO = 2; private MessagePacket(String name) { super(name); @@ -22,7 +24,12 @@ public class MessagePacket extends Element { } public String getBody() { - return this.findChild("body").getContent(); + Element body = this.findChild("body"); + if (body!=null) { + return body.getContent(); + } else { + return null; + } } public void setTo(String to) { @@ -50,4 +57,16 @@ public class MessagePacket extends Element { break; } } + + public int getType() { + String type = getAttribute("type"); + if (type==null) { + return TYPE_NO; + } + if (type.equals("chat")) { + return TYPE_CHAT; + } else { + return TYPE_UNKNOWN; + } + } } diff --git a/src/de/gultsch/chat/xmpp/XmppConnection.java b/src/de/gultsch/chat/xmpp/XmppConnection.java index f82c401e0..2303d2394 100644 --- a/src/de/gultsch/chat/xmpp/XmppConnection.java +++ b/src/de/gultsch/chat/xmpp/XmppConnection.java @@ -38,7 +38,7 @@ public class XmppConnection implements Runnable { private boolean isTlsEncrypted = false; private boolean isAuthenticated = false; - private boolean shouldUseTLS = false; + //private boolean shouldUseTLS = false; private boolean shouldReConnect = true; private boolean shouldBind = true; private boolean shouldAuthenticate = true; @@ -83,10 +83,10 @@ public class XmppConnection implements Runnable { } } } catch (UnknownHostException e) { - Log.d(LOGTAG, "error during connect. unknown host"); + Log.d(LOGTAG,account.getJid()+": error during connect. unknown host"); return; } catch (IOException e) { - Log.d(LOGTAG, "error during connect. io exception. falscher port?"); + Log.d(LOGTAG, account.getJid()+": error during connect. io exception. falscher port?"); return; } catch (XmlPullParserException e) { Log.d(LOGTAG,"xml exception "+e.getMessage()); @@ -109,7 +109,6 @@ public class XmppConnection implements Runnable { private void processStream(Tag currentTag) throws XmlPullParserException, IOException { - Log.d(LOGTAG, "process Stream"); Tag nextTag; while (!(nextTag = tagReader.readTag()).isEnd("stream")) { if (nextTag.isStart("error")) { @@ -120,7 +119,7 @@ public class XmppConnection implements Runnable { switchOverToTls(nextTag); } else if (nextTag.isStart("success")) { isAuthenticated = true; - Log.d(LOGTAG,"read success tag in stream. reset again"); + Log.d(LOGTAG,account.getJid()+": read success tag in stream. reset again"); tagReader.readTag(); tagReader.reset(); sendStartStream(); @@ -194,14 +193,14 @@ public class XmppConnection implements Runnable { private void sendStartTLS() throws XmlPullParserException, IOException { Tag startTLS = Tag.empty("starttls"); startTLS.setAttribute("xmlns", "urn:ietf:params:xml:ns:xmpp-tls"); - Log.d(LOGTAG, "sending starttls"); + Log.d(LOGTAG,account.getJid()+": sending starttls"); tagWriter.writeTag(startTLS).flush(); } private void switchOverToTls(Tag currentTag) throws XmlPullParserException, IOException { Tag nextTag = tagReader.readTag(); // should be proceed end tag - Log.d(LOGTAG, "now switch to ssl"); + Log.d(LOGTAG,account.getJid()+": now switch to ssl"); SSLSocket sslSocket; try { sslSocket = (SSLSocket) ((SSLSocketFactory) SSLSocketFactory @@ -215,7 +214,7 @@ public class XmppConnection implements Runnable { sendStartStream(); processStream(tagReader.readTag()); } catch (IOException e) { - Log.d(LOGTAG, "error on ssl" + e.getMessage()); + Log.d(LOGTAG, account.getJid()+": error on ssl '" + e.getMessage()+"'"); } } @@ -226,7 +225,7 @@ public class XmppConnection implements Runnable { auth.setAttribute("xmlns", "urn:ietf:params:xml:ns:xmpp-sasl"); auth.setAttribute("mechanism", "PLAIN"); auth.setContent(saslString); - Log.d(LOGTAG,"sending sasl "+auth.toString()); + Log.d(LOGTAG,account.getJid()+": sending sasl "+auth.toString()); tagWriter.writeElement(auth); tagWriter.flush(); } @@ -234,11 +233,10 @@ public class XmppConnection implements Runnable { private void processStreamFeatures(Tag currentTag) throws XmlPullParserException, IOException { this.streamFeatures = tagReader.readElement(currentTag); - Log.d(LOGTAG,"process stream features "+streamFeatures); - if (this.streamFeatures.hasChild("starttls")&&shouldUseTLS) { + Log.d(LOGTAG,account.getJid()+": process stream features "+streamFeatures); + if (this.streamFeatures.hasChild("starttls")&&account.isOptionSet(Account.OPTION_USETLS)) { sendStartTLS(); - } - if (this.streamFeatures.hasChild("mechanisms")&&shouldAuthenticate) { + } else if (this.streamFeatures.hasChild("mechanisms")&&shouldAuthenticate) { sendSaslAuth(); } if (this.streamFeatures.hasChild("bind")&&shouldBind) { @@ -269,8 +267,7 @@ public class XmppConnection implements Runnable { @Override public void onIqPacketReceived(Account account, IqPacket packet) { resource = packet.findChild("bind").findChild("jid").getContent().split("/")[1]; - Log.d(LOGTAG,"answer for our bind was: "+packet.toString()); - Log.d(LOGTAG,"new resource is "+resource); + Log.d(LOGTAG,account.getJid()+": new resource is "+resource); } }); }