tls is no optional

This commit is contained in:
Daniel Gultsch 2014-02-02 17:53:34 +01:00
parent bbdaf5b0bd
commit 3d9294684c
5 changed files with 91 additions and 60 deletions

View File

@ -16,6 +16,8 @@ public class Account extends AbstractEntity{
public static final String OPTIONS = "options"; public static final String OPTIONS = "options";
public static final String ROSTERVERSION = "rosterversion"; public static final String ROSTERVERSION = "rosterversion";
public static final int OPTION_USETLS = 0;
protected String username; protected String username;
protected String server; protected String server;
protected String password; protected String password;
@ -40,6 +42,10 @@ public class Account extends AbstractEntity{
this.rosterVersion = rosterVersion; this.rosterVersion = rosterVersion;
} }
public boolean isOptionSet(int option) {
return ((options & (1 << option)) != 0);
}
public String getUsername() { public String getUsername() {
return username; return username;
} }

View File

@ -46,18 +46,19 @@ public class XmppConnectionService extends Service {
@Override @Override
public void onMessagePacketReceived(Account account, MessagePacket packet) { public void onMessagePacketReceived(Account account, MessagePacket packet) {
String fullJid = packet.getFrom(); if (packet.getType()==MessagePacket.TYPE_CHAT) {
String jid = fullJid.split("/")[0]; Log.d(LOGTAG,account.getJid()+": message of type chat");
String name = jid.split("@")[0]; String fullJid = packet.getFrom();
Log.d(LOGTAG,"message received for "+account.getJid()+" from "+jid); String jid = fullJid.split("/")[0];
Log.d(LOGTAG,packet.toString()); String name = jid.split("@")[0];
Contact contact = new Contact(account,name,jid,null); //dummy contact Contact contact = new Contact(account,name,jid,null); //dummy contact
Conversation conversation = findOrCreateConversation(account, contact); Conversation conversation = findOrCreateConversation(account, contact);
Message message = new Message(conversation, fullJid, packet.getBody(), Message.ENCRYPTION_NONE, Message.STATUS_RECIEVED); Message message = new Message(conversation, fullJid, packet.getBody(), Message.ENCRYPTION_NONE, Message.STATUS_RECIEVED);
conversation.getMessages().add(message); conversation.getMessages().add(message);
databaseBackend.createMessage(message); databaseBackend.createMessage(message);
if (convChangedListener != null) { if (convChangedListener != null) {
convChangedListener.onConversationListChanged(); convChangedListener.onConversationListChanged();
}
} }
} }
}; };
@ -117,36 +118,41 @@ public class XmppConnectionService extends Service {
} }
public void getRoster(final Account account, final OnRosterFetchedListener listener) { public void getRoster(final Account account, final OnRosterFetchedListener listener) {
IqPacket iqPacket = new IqPacket(IqPacket.TYPE_GET); new Thread() {
Element query = new Element("query"); @Override
query.setAttribute("xmlns", "jabber:iq:roster"); public void run() {
query.setAttribute("ver", ""); IqPacket iqPacket = new IqPacket(IqPacket.TYPE_GET);
iqPacket.addChild(query); Element query = new Element("query");
try { query.setAttribute("xmlns", "jabber:iq:roster");
connections.get(account).sendIqPacket(iqPacket, new OnIqPacketReceived() { query.setAttribute("ver", "");
iqPacket.addChild(query);
@Override try {
public void onIqPacketReceived(Account account, IqPacket packet) { connections.get(account).sendIqPacket(iqPacket, new OnIqPacketReceived() {
Element roster = packet.findChild("query");
Log.d(LOGTAG,roster.toString()); @Override
List<Contact> contacts = new ArrayList<Contact>(); public void onIqPacketReceived(Account account, IqPacket packet) {
for(Element item : roster.getChildren()) { Element roster = packet.findChild("query");
String name = item.getAttribute("name"); Log.d(LOGTAG,roster.toString());
String jid = item.getAttribute("jid"); List<Contact> contacts = new ArrayList<Contact>();
if (name==null) { for(Element item : roster.getChildren()) {
name = jid.split("@")[0]; String name = item.getAttribute("name");
} String jid = item.getAttribute("jid");
Contact contact = new Contact(account, name, jid, null); if (name==null) {
contacts.add(contact); name = jid.split("@")[0];
} }
if (listener != null) { Contact contact = new Contact(account, name, jid, null);
listener.onRosterFetched(contacts); contacts.add(contact);
} }
} if (listener != null) {
}); listener.onRosterFetched(contacts);
} catch (IOException e) { }
Log.d(LOGTAG,"io error during roster fetch"); }
} });
} catch (IOException e) {
Log.d(LOGTAG,"io error during roster fetch");
}
}
}.start();
} }
public void addConversation(Conversation conversation) { public void addConversation(Conversation conversation) {

View File

@ -124,8 +124,11 @@ public class ConversationFragment extends Fragment {
} else { } else {
imageView.setImageURI(profilePicture); imageView.setImageURI(profilePicture);
} }
((TextView) view.findViewById(R.id.message_body)).setText(item TextView messageBody = (TextView) view.findViewById(R.id.message_body);
.getBody().trim()); String body = item.getBody();
if (body!=null) {
messageBody.setText(body.trim());
}
TextView time = (TextView) view.findViewById(R.id.message_time); TextView time = (TextView) view.findViewById(R.id.message_time);
if (item.getStatus() == Message.STATUS_UNSEND) { if (item.getStatus() == Message.STATUS_UNSEND) {
time.setTypeface(null, Typeface.ITALIC); time.setTypeface(null, Typeface.ITALIC);

View File

@ -4,6 +4,8 @@ import de.gultsch.chat.xml.Element;
public class MessagePacket extends Element { public class MessagePacket extends Element {
public static final int TYPE_CHAT = 0; 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) { private MessagePacket(String name) {
super(name); super(name);
@ -22,7 +24,12 @@ public class MessagePacket extends Element {
} }
public String getBody() { 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) { public void setTo(String to) {
@ -50,4 +57,16 @@ public class MessagePacket extends Element {
break; 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;
}
}
} }

View File

@ -38,7 +38,7 @@ public class XmppConnection implements Runnable {
private boolean isTlsEncrypted = false; private boolean isTlsEncrypted = false;
private boolean isAuthenticated = false; private boolean isAuthenticated = false;
private boolean shouldUseTLS = false; //private boolean shouldUseTLS = false;
private boolean shouldReConnect = true; private boolean shouldReConnect = true;
private boolean shouldBind = true; private boolean shouldBind = true;
private boolean shouldAuthenticate = true; private boolean shouldAuthenticate = true;
@ -83,10 +83,10 @@ public class XmppConnection implements Runnable {
} }
} }
} catch (UnknownHostException e) { } catch (UnknownHostException e) {
Log.d(LOGTAG, "error during connect. unknown host"); Log.d(LOGTAG,account.getJid()+": error during connect. unknown host");
return; return;
} catch (IOException e) { } 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; return;
} catch (XmlPullParserException e) { } catch (XmlPullParserException e) {
Log.d(LOGTAG,"xml exception "+e.getMessage()); Log.d(LOGTAG,"xml exception "+e.getMessage());
@ -109,7 +109,6 @@ public class XmppConnection implements Runnable {
private void processStream(Tag currentTag) throws XmlPullParserException, private void processStream(Tag currentTag) throws XmlPullParserException,
IOException { IOException {
Log.d(LOGTAG, "process Stream");
Tag nextTag; Tag nextTag;
while (!(nextTag = tagReader.readTag()).isEnd("stream")) { while (!(nextTag = tagReader.readTag()).isEnd("stream")) {
if (nextTag.isStart("error")) { if (nextTag.isStart("error")) {
@ -120,7 +119,7 @@ public class XmppConnection implements Runnable {
switchOverToTls(nextTag); switchOverToTls(nextTag);
} else if (nextTag.isStart("success")) { } else if (nextTag.isStart("success")) {
isAuthenticated = true; 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.readTag();
tagReader.reset(); tagReader.reset();
sendStartStream(); sendStartStream();
@ -194,14 +193,14 @@ public class XmppConnection implements Runnable {
private void sendStartTLS() throws XmlPullParserException, IOException { private void sendStartTLS() throws XmlPullParserException, IOException {
Tag startTLS = Tag.empty("starttls"); Tag startTLS = Tag.empty("starttls");
startTLS.setAttribute("xmlns", "urn:ietf:params:xml:ns:xmpp-tls"); 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(); tagWriter.writeTag(startTLS).flush();
} }
private void switchOverToTls(Tag currentTag) throws XmlPullParserException, private void switchOverToTls(Tag currentTag) throws XmlPullParserException,
IOException { IOException {
Tag nextTag = tagReader.readTag(); // should be proceed end tag 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; SSLSocket sslSocket;
try { try {
sslSocket = (SSLSocket) ((SSLSocketFactory) SSLSocketFactory sslSocket = (SSLSocket) ((SSLSocketFactory) SSLSocketFactory
@ -215,7 +214,7 @@ public class XmppConnection implements Runnable {
sendStartStream(); sendStartStream();
processStream(tagReader.readTag()); processStream(tagReader.readTag());
} catch (IOException e) { } 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("xmlns", "urn:ietf:params:xml:ns:xmpp-sasl");
auth.setAttribute("mechanism", "PLAIN"); auth.setAttribute("mechanism", "PLAIN");
auth.setContent(saslString); auth.setContent(saslString);
Log.d(LOGTAG,"sending sasl "+auth.toString()); Log.d(LOGTAG,account.getJid()+": sending sasl "+auth.toString());
tagWriter.writeElement(auth); tagWriter.writeElement(auth);
tagWriter.flush(); tagWriter.flush();
} }
@ -234,11 +233,10 @@ public class XmppConnection implements Runnable {
private void processStreamFeatures(Tag currentTag) private void processStreamFeatures(Tag currentTag)
throws XmlPullParserException, IOException { throws XmlPullParserException, IOException {
this.streamFeatures = tagReader.readElement(currentTag); this.streamFeatures = tagReader.readElement(currentTag);
Log.d(LOGTAG,"process stream features "+streamFeatures); Log.d(LOGTAG,account.getJid()+": process stream features "+streamFeatures);
if (this.streamFeatures.hasChild("starttls")&&shouldUseTLS) { if (this.streamFeatures.hasChild("starttls")&&account.isOptionSet(Account.OPTION_USETLS)) {
sendStartTLS(); sendStartTLS();
} } else if (this.streamFeatures.hasChild("mechanisms")&&shouldAuthenticate) {
if (this.streamFeatures.hasChild("mechanisms")&&shouldAuthenticate) {
sendSaslAuth(); sendSaslAuth();
} }
if (this.streamFeatures.hasChild("bind")&&shouldBind) { if (this.streamFeatures.hasChild("bind")&&shouldBind) {
@ -269,8 +267,7 @@ public class XmppConnection implements Runnable {
@Override @Override
public void onIqPacketReceived(Account account, IqPacket packet) { public void onIqPacketReceived(Account account, IqPacket packet) {
resource = packet.findChild("bind").findChild("jid").getContent().split("/")[1]; resource = packet.findChild("bind").findChild("jid").getContent().split("/")[1];
Log.d(LOGTAG,"answer for our bind was: "+packet.toString()); Log.d(LOGTAG,account.getJid()+": new resource is "+resource);
Log.d(LOGTAG,"new resource is "+resource);
} }
}); });
} }