diff options
author | aunsane <aunsane@gmail.com> | 2017-03-12 21:19:21 +0300 |
---|---|---|
committer | aunsane <aunsane@gmail.com> | 2017-03-12 21:19:38 +0300 |
commit | b9c2a6953f0fa75a1c4de6079e9c97b3a60be7e5 (patch) | |
tree | 83940e0306fd9b0eca9dfe7ce679b437cde9331b /protocols/Slack/src/slack_contacts.cpp | |
parent | 9826bda7bcd2a2024eac0fcecffc9633f5ac79f6 (diff) |
Slack: initial commit
Diffstat (limited to 'protocols/Slack/src/slack_contacts.cpp')
-rw-r--r-- | protocols/Slack/src/slack_contacts.cpp | 165 |
1 files changed, 165 insertions, 0 deletions
diff --git a/protocols/Slack/src/slack_contacts.cpp b/protocols/Slack/src/slack_contacts.cpp new file mode 100644 index 0000000000..185d5110b8 --- /dev/null +++ b/protocols/Slack/src/slack_contacts.cpp @@ -0,0 +1,165 @@ +#include "stdafx.h" + +WORD CSlackProto::GetContactStatus(MCONTACT hContact) +{ + return getWord(hContact, "Status", ID_STATUS_OFFLINE); +} + +void CSlackProto::SetContactStatus(MCONTACT hContact, WORD status) +{ + WORD oldStatus = GetContactStatus(hContact); + if (oldStatus != status) + setWord(hContact, "Status", status); +} + +void CSlackProto::SetAllContactsStatus(WORD status) +{ + for (MCONTACT hContact = db_find_first(m_szModuleName); hContact; hContact = db_find_next(hContact, m_szModuleName)) + SetContactStatus(hContact, status); +} + +MCONTACT CSlackProto::GetContact(const char *userId) +{ + MCONTACT hContact = NULL; + for (hContact = db_find_first(m_szModuleName); hContact; hContact = db_find_next(hContact, m_szModuleName)) + { + ptrA contactPhone(getStringA(hContact, "UserId")); + if (mir_strcmp(userId, contactPhone) == 0) + break; + } + return hContact; +} + +MCONTACT CSlackProto::AddContact(const char *userId, const char *nick, bool isTemporary) +{ + MCONTACT hContact = GetContact(userId); + if (!hContact) + { + hContact = db_add_contact(); + Proto_AddToContact(hContact, m_szModuleName); + + setString(hContact, "UserId", userId); + + if (mir_strlen(nick)) + setWString(hContact, "Nick", ptrW(mir_utf8decodeW(nick))); + + DBVARIANT dbv; + if (!getWString("TeamName", &dbv) && Clist_GroupExists(dbv.ptszVal)) + { + db_set_ws(hContact, "CList", "Group", dbv.ptszVal); + db_free(&dbv); + } + + setByte(hContact, "Auth", 1); + setByte(hContact, "Grant", 1); + + if (isTemporary) + { + db_set_b(hContact, "CList", "NotOnList", 1); + } + } + return hContact; +} + +void CSlackProto::OnGotUserProfile(JSONNode &root) +{ + if (!root) + { + debugLogA(__FUNCTION__": failed to load user profile"); + return; + } + + bool isOk = root["ok"].as_bool(); + if (!isOk) + { + debugLogA(__FUNCTION__": failed to load users profile"); + return; + } + + JSONNode profile = root["profile"].as_node(); +} + +void CSlackProto::OnGotUserProfile(MCONTACT hContact, JSONNode &root) +{ + if (!root) + { + debugLogA(__FUNCTION__": failed to read users profile"); + return; + } + + CMStringW firstName = root["first_name"].as_mstring(); + setWString(hContact, "FirstName", firstName); + + CMStringW lastName = root["last_name"].as_mstring(); + setWString(hContact, "LastName", lastName); +} + +void CSlackProto::OnGotUserList(JSONNode &root) +{ + if (!root) + { + debugLogA(__FUNCTION__": failed to load users list"); + return; + } + + bool isOk = root["ok"].as_bool(); + if (!isOk) + { + debugLogA(__FUNCTION__": failed to load users list"); + return; + } + + JSONNode users = root["members"].as_array(); + for (size_t i = 0; i < users.size(); i++) + { + JSONNode user = users[i]; + + json_string userId = user["id"].as_string(); + json_string nick = user["name"].as_string(); + bool isDeleted = user["deleted"].as_bool(); + MCONTACT hContact = AddContact(userId.c_str(), nick.c_str(), isDeleted); + if (hContact) + { + json_string teamId = user["team_id"].as_string(); + setString(hContact, "TeamId", teamId.c_str()); + + CMStringW status = root["status"].as_mstring(); + if (!status.IsEmpty()) + setWString(hContact, "StatusMsg", status); + + JSONNode profile = root["profile"].as_node(); + OnGotUserProfile(hContact, profile); + } + } +} + +INT_PTR CSlackProto::OnRequestAuth(WPARAM hContact, LPARAM lParam) +{ + if (!IsOnline()) + { + return -1; // ??? + } + return 0; +} + +INT_PTR CSlackProto::OnGrantAuth(WPARAM hContact, LPARAM) +{ + if (!IsOnline()) + { + // TODO: warn + return 0; + } + + return 0; +} + +int CSlackProto::OnContactDeleted(MCONTACT hContact, LPARAM) +{ + if (!IsOnline()) + { + // TODO: warn + return 0; + } + + return 0; +}
\ No newline at end of file |