diff --git a/src/main/java/fr/delthas/skype/NotifConnector.java b/src/main/java/fr/delthas/skype/NotifConnector.java index 6fe5b90..7e3e00f 100644 --- a/src/main/java/fr/delthas/skype/NotifConnector.java +++ b/src/main/java/fr/delthas/skype/NotifConnector.java @@ -675,7 +675,9 @@ public synchronized void disconnect() { } private synchronized void sendPacket(String command, String parameters, String body) throws IOException { - String headerString = registration != null ? "Registration: " + registration + "\r\n" : ""; +// String headerString = registration != null ? "Registration: " + registration + "\r\n" : ""; + // Weird, but it's working better without sending the registration token. Solved the 911 problem but needs more testing. + String headerString = ""; String messageString = String.format("%s %d %s %d\r\n%s\r\n%s", command, ++sequenceNumber, parameters, body.getBytes(StandardCharsets.UTF_8).length + 2 + headerString.length(), headerString, body); try { @@ -698,6 +700,7 @@ private void connectTo(String hostname, int port) throws IOException { writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), StandardCharsets.UTF_8)); inputStream = new BufferedInputStream(socket.getInputStream()); sequenceNumber = 0; + authenticated = false; sendPacket("CNT", "CON", "2WindowsWindows 10.0 (build8 3600 I-586-6-45-7 Intel Core ien-US"); } @@ -779,7 +782,7 @@ private Object parseEntity(String rawEntity) { } else { name = rawEntity.substring(senderBegin + 1, senderEnd); } - if (network == 8) { + if (network == 8 || network == 2) { // Skype4Business contacts come with network == 2, courtesy of @metasonic return skype.getUser(name); } else if (network == 19) { return skype.getGroup(name); @@ -824,11 +827,7 @@ private String getXMLField(String XML, String fieldName) throws ParseException { } private String getSelfLiveUsername() { - if (microsoft) { - return "live:" + username.substring(0, username.indexOf('@')); - } else { - return username; - } + return skype.getUser(username).getLiveUsername(); } private static class Packet { diff --git a/src/main/java/fr/delthas/skype/Presence.java b/src/main/java/fr/delthas/skype/Presence.java index 3ed6a23..8f91fba 100644 --- a/src/main/java/fr/delthas/skype/Presence.java +++ b/src/main/java/fr/delthas/skype/Presence.java @@ -12,6 +12,10 @@ public enum Presence { * Away / Be Right Back (orange clock on Skype) */ AWAY("AWY"), + /** + * Idle / Absent + */ + IDLE("IDL"), /** * Busy / Do Not Disturb (red sign on Skype) */ diff --git a/src/main/java/fr/delthas/skype/User.java b/src/main/java/fr/delthas/skype/User.java index a6ea569..b4db6ce 100644 --- a/src/main/java/fr/delthas/skype/User.java +++ b/src/main/java/fr/delthas/skype/User.java @@ -21,6 +21,7 @@ public class User { private String city; private String displayName; private String avatarUrl; + private String liveUsername; private Presence presence = Presence.OFFLINE; User(Skype skype, String username) { @@ -247,6 +248,14 @@ void setPresence(Presence presence, boolean triggerListeners) { } } + String getLiveUsername() { + return liveUsername; + } + + void setLiveUsername(String liveUsername) { + this.liveUsername = liveUsername; + } + @Override public int hashCode() { final int prime = 31; diff --git a/src/main/java/fr/delthas/skype/WebConnector.java b/src/main/java/fr/delthas/skype/WebConnector.java index 032c3ac..52a4d78 100644 --- a/src/main/java/fr/delthas/skype/WebConnector.java +++ b/src/main/java/fr/delthas/skype/WebConnector.java @@ -86,10 +86,12 @@ private void updateContacts() throws IOException { updated = true; String selfResponse = sendRequest(Method.GET, "/users/self/profile").body(); JSONObject selfJSON = new JSONObject(selfResponse); - updateUser(selfJSON, false); + + User loggedUser = updateUser(selfJSON, false, username); String profilesResponse = - sendRequest(Method.GET, "https://contacts.skype.com/contacts/v2/users/" + getSelfLiveUsername() + "/contacts", true).body(); + sendRequest(Method.GET, "https://contacts.skype.com/contacts/v2/users/" + loggedUser.getLiveUsername() + "/contacts", true).body(); + try { JSONObject json = new JSONObject(profilesResponse); if (json.optString("message", null) != null) { @@ -108,6 +110,10 @@ private void updateContacts() throws IOException { } private User updateUser(JSONObject userJSON, boolean newContactType) throws ParseException { + return updateUser(userJSON, newContactType, null); + } + + private User updateUser(JSONObject userJSON, boolean newContactType, String username) throws ParseException { String userUsername; String userFirstName = null; String userLastName = null; @@ -118,7 +124,7 @@ private User updateUser(JSONObject userJSON, boolean newContactType) throws Pars String userAvatarUrl = null; try { if (!newContactType) { - userUsername = userJSON.getString("username"); + userUsername = userJSON.optString("username"); userFirstName = userJSON.optString("firstname", null); userLastName = userJSON.optString("lastname", null); userMood = userJSON.optString("mood", null); @@ -146,9 +152,13 @@ private User updateUser(JSONObject userJSON, boolean newContactType) throws Pars userUsername = mri.substring(senderBegin + 1); userDisplayName = userJSON.optString("display_name", null); JSONObject profileJSON = userJSON.getJSONObject("profile"); - JSONObject nameJSON = profileJSON.getJSONObject("name"); - userFirstName = nameJSON.optString("first", null); - userLastName = nameJSON.optString("surname", null); + + if (profileJSON.has("name")) { + JSONObject nameJSON = profileJSON.getJSONObject("name"); + userFirstName = nameJSON.optString("first", null); + userLastName = nameJSON.optString("surname", null); + } + userMood = profileJSON.optString("mood", null); if (profileJSON.has("locations")) { JSONObject locationJSON = profileJSON.optJSONArray("locations").optJSONObject(0); @@ -162,7 +172,7 @@ private User updateUser(JSONObject userJSON, boolean newContactType) throws Pars } catch (JSONException e) { throw new ParseException(e); } - User user = skype.getUser(userUsername); + User user = skype.getUser(username != null ? username : userUsername); user.setCity(getPlaintext(userCity)); user.setCountry(getPlaintext(userCountry)); user.setDisplayName(getPlaintext(userDisplayName)); @@ -170,6 +180,7 @@ private User updateUser(JSONObject userJSON, boolean newContactType) throws Pars user.setLastName(getPlaintext(userLastName)); user.setMood(getPlaintext(userMood)); user.setAvatarUrl(userAvatarUrl); + user.setLiveUsername(userUsername); return user; } @@ -220,6 +231,7 @@ private Response sendRequest(Method method, String apiPath, boolean absoluteApiP logger.finest("Sending " + method + " request at " + url); if (skypeToken != null) { conn.header("X-Skypetoken", skypeToken); + conn.header("Accept", "application/json"); } else { logger.fine("No token sent for the request at: " + url); } @@ -230,12 +242,4 @@ private Response sendRequest(Method method, String apiPath, boolean absoluteApiP private Response sendRequest(Method method, String apiPath, String... keyval) throws IOException { return sendRequest(method, apiPath, false, keyval); } - - private String getSelfLiveUsername() { - if (username.contains("@")) { - return "live:" + username.substring(0, username.indexOf('@')); - } else { - return username; - } - } }