Skip to content
This repository was archived by the owner on Apr 9, 2019. It is now read-only.
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions src/main/java/fr/delthas/skype/NotifConnector.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is suprising indeed, maybe they removed recently like they removed Challenge. Let's include this change for now if its works for you.

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 {
Expand All @@ -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", "<connect><ver>2</ver><agent><os>Windows</os><osVer>Windows 10.0 (build</osVer><proc>8 3600 I-586-6-45-7 Intel Core i</proc><lcid>en-US</lcid></agent></connect>");
}

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/fr/delthas/skype/Presence.java
Original file line number Diff line number Diff line change
Expand Up @@ -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)
*/
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/fr/delthas/skype/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -247,6 +248,14 @@ void setPresence(Presence presence, boolean triggerListeners) {
}
}

String getLiveUsername() {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(I made these private, probably doesn't need to be in the library public api)

return liveUsername;
}

void setLiveUsername(String liveUsername) {
this.liveUsername = liveUsername;
}

@Override
public int hashCode() {
final int prime = 31;
Expand Down
34 changes: 19 additions & 15 deletions src/main/java/fr/delthas/skype/WebConnector.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
Expand All @@ -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");
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code will only be executed for the current user. If username is null, userUsername will be empty, which will set the current User username to the empty string. This will request "https://contacts.skype.com/contacts/v2/users//contacts" immediately after in WebConnector.java:93, which cannot work. Set this to getString maybe?

userFirstName = userJSON.optString("firstname", null);
userLastName = userJSON.optString("lastname", null);
userMood = userJSON.optString("mood", null);
Expand Down Expand Up @@ -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);

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed the default set-s for firstName and lastName, since when not set we should return null and let the library user decide what to do, rather than return a default value ourselves.

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);
Expand All @@ -162,14 +172,15 @@ 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));
user.setFirstName(getPlaintext(userFirstName));
user.setLastName(getPlaintext(userLastName));
user.setMood(getPlaintext(userMood));
user.setAvatarUrl(userAvatarUrl);
user.setLiveUsername(userUsername);
return user;
}

Expand Down Expand Up @@ -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);
}
Expand All @@ -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;
}
}
}