Load skype buddies from database if it's possible

This commit is contained in:
HanzZ 2013-02-24 10:31:05 +01:00
parent f90bf5d3de
commit 882d7b2f02
4 changed files with 92 additions and 78 deletions

View file

@ -20,6 +20,7 @@
#include "skype.h"
#include "skypeplugin.h"
#include "skypedb.h"
#include "transport/config.h"
#include "transport/logging.h"
@ -95,9 +96,9 @@ void Skype::login() {
return;
}
std::string db_path = createSkypeDirectory();
m_db = createSkypeDirectory();
bool spawned = spawnSkype(db_path);
bool spawned = spawnSkype(m_db);
if (!spawned) {
m_np->handleDisconnected(m_user, pbnetwork::CONNECTION_ERROR_AUTHENTICATION_IMPOSSIBLE, "Error spawning the Skype instance.");
return;
@ -229,51 +230,55 @@ bool Skype::loadSkypeBuddies() {
}
}
std::string friends = send_command("GET AUTH_CONTACTS_PROFILES");
// Try to load skype buddies from database, if it fails
// fallback to old method.
if (!SkypeDB::loadBuddies(m_np, m_db, m_user, group_map)) {
std::string friends = send_command("GET AUTH_CONTACTS_PROFILES");
char **full_friends_list = g_strsplit((strchr(friends.c_str(), ' ')+1), ";", 0);
if (full_friends_list && full_friends_list[0])
{
//in the format of: username;full name;phone;office phone;mobile phone;
// online status;friendly name;voicemail;mood
// (comma-seperated lines, usernames can have comma's)
for (int i=0; full_friends_list[i] && full_friends_list[i+1] && *full_friends_list[i] != '\0'; i+=8)
char **full_friends_list = g_strsplit((strchr(friends.c_str(), ' ')+1), ";", 0);
if (full_friends_list && full_friends_list[0])
{
std::string buddy = full_friends_list[i];
//in the format of: username;full name;phone;office phone;mobile phone;
// online status;friendly name;voicemail;mood
// (comma-seperated lines, usernames can have comma's)
if (buddy[0] == ',') {
buddy.erase(buddy.begin());
for (int i=0; full_friends_list[i] && full_friends_list[i+1] && *full_friends_list[i] != '\0'; i+=8)
{
std::string buddy = full_friends_list[i];
if (buddy[0] == ',') {
buddy.erase(buddy.begin());
}
if (buddy.rfind(",") != std::string::npos) {
buddy = buddy.substr(buddy.rfind(","));
}
if (buddy[0] == ',') {
buddy.erase(buddy.begin());
}
LOG4CXX_INFO(logger, "Got buddy " << buddy);
std::string st = full_friends_list[i + 5];
pbnetwork::StatusType status = getStatus(st);
std::string alias = full_friends_list[i + 6];
std::string mood_text = "";
if (full_friends_list[i + 8] && *full_friends_list[i + 8] != '\0' && *full_friends_list[i + 8] != ',') {
mood_text = full_friends_list[i + 8];
}
std::vector<std::string> groups;
if (group_map.find(buddy) != group_map.end()) {
groups.push_back(group_map[buddy]);
}
m_np->handleBuddyChanged(m_user, buddy, alias, groups, status, mood_text);
}
if (buddy.rfind(",") != std::string::npos) {
buddy = buddy.substr(buddy.rfind(","));
}
if (buddy[0] == ',') {
buddy.erase(buddy.begin());
}
LOG4CXX_INFO(logger, "Got buddy " << buddy);
std::string st = full_friends_list[i + 5];
pbnetwork::StatusType status = getStatus(st);
std::string alias = full_friends_list[i + 6];
std::string mood_text = "";
if (full_friends_list[i + 8] && *full_friends_list[i + 8] != '\0' && *full_friends_list[i + 8] != ',') {
mood_text = full_friends_list[i + 8];
}
std::vector<std::string> groups;
if (group_map.find(buddy) != group_map.end()) {
groups.push_back(group_map[buddy]);
}
m_np->handleBuddyChanged(m_user, buddy, alias, groups, status, mood_text);
}
g_strfreev(full_friends_list);
}
g_strfreev(full_friends_list);
send_command("SET AUTOAWAY OFF");
send_command("SET USERSTATUS ONLINE");

View file

@ -87,5 +87,6 @@ class Skype {
int fd_output;
std::map<std::string, std::string> m_groups;
SkypePlugin *m_np;
std::string m_db;
};

View file

@ -42,8 +42,6 @@
#include "skypeplugin.h"
#include "skypeplugin.h"
// Prepare the SQL statement
#define PREP_STMT(sql, str) \
if(sqlite3_prepare_v2(db, std::string(str).c_str(), -1, &sql, NULL)) { \
@ -116,41 +114,51 @@ bool getAvatar(const std::string &db_path, const std::string &name, std::string
return ret;
}
bool loadBuddies(Transport::NetworkPlugin *np, const std::string &db_path) {
bool loadBuddies(SkypePlugin *np, const std::string &db_path, std::string &user, std::map<std::string, std::string> &group_map) {
bool ret = false;
// sqlite3 *db;
// LOG4CXX_INFO(logger, "Opening database " << db_path);
// if (sqlite3_open(db_path.c_str(), &db)) {
// sqlite3_close(db);
// LOG4CXX_ERROR(logger, "Can't open database");
// }
// else {
// sqlite3_stmt *stmt;
// PREP_STMT(stmt, "SELECT avatar_image FROM Contacts WHERE skypename=?");
// if (stmt) {
// BEGIN(stmt);
// BIND_STR(stmt, name);
// if(sqlite3_step(stmt) == SQLITE_ROW) {
// int size = sqlite3_column_bytes(stmt, 0);
// const void *data = sqlite3_column_blob(stmt, 0);
// photo = std::string((const char *)data + 1, size - 1);
// ret = true;
// }
// else {
// LOG4CXX_ERROR(logger, (sqlite3_errmsg(db) == NULL ? "" : sqlite3_errmsg(db)));
// }
//
// int ret;
// while((ret = sqlite3_step(stmt)) == SQLITE_ROW) {
// }
// FINALIZE_STMT(stmt);
// }
// else {
// LOG4CXX_ERROR(logger, "Can't create prepared statement");
// LOG4CXX_ERROR(logger, (sqlite3_errmsg(db) == NULL ? "" : sqlite3_errmsg(db)));
// }
// sqlite3_close(db);
// }
sqlite3 *db;
LOG4CXX_INFO(logger, "Opening database " << db_path);
if (sqlite3_open(db_path.c_str(), &db)) {
sqlite3_close(db);
LOG4CXX_ERROR(logger, "Can't open database");
}
else {
sqlite3_stmt *stmt;
PREP_STMT(stmt, "select skypename, aliases, fullname, displayname, mood_text from Contacts;");
if (stmt) {
BEGIN(stmt);
int ret2;
while((ret2 = sqlite3_step(stmt)) == SQLITE_ROW) {
std::string buddy = (const char *) sqlite3_column_text(stmt, 0);
std::string alias = (const char *) sqlite3_column_text(stmt, 1);
std::string fullname = (const char *) sqlite3_column_text(stmt, 2);
std::string displayname = (const char *) sqlite3_column_text(stmt, 3);
std::string mood_text = (const char *) sqlite3_column_text(stmt, 4);
if (alias.empty()) {
alias = displayname;
}
std::vector<std::string> groups;
if (group_map.find(buddy) != group_map.end()) {
groups.push_back(group_map[buddy]);
}
np->handleBuddyChanged(user, buddy, alias, groups, pbnetwork::STATUS_NONE, mood_text);
}
if (ret2 != SQLITE_DONE) {
FINALIZE_STMT(stmt);
ret = false;
}
else {
ret = true;
}
}
else {
LOG4CXX_ERROR(logger, "Can't create prepared statement");
LOG4CXX_ERROR(logger, (sqlite3_errmsg(db) == NULL ? "" : sqlite3_errmsg(db)));
}
sqlite3_close(db);
}
return ret;
}

View file

@ -30,6 +30,6 @@ class SkypePlugin;
namespace SkypeDB {
bool getAvatar(const std::string &db, const std::string &name, std::string &avatar);
bool loadBuddies(SkypePlugin *np, const std::string &db);
bool loadBuddies(SkypePlugin *np, const std::string &db, std::string &user, std::map<std::string, std::string> &group_map);
}