diff options
author | Catalin Badea <catalin.badea392@gmail.com> | 2012-07-01 20:13:27 (GMT) |
---|---|---|
committer | Cătălin Badea <catalin.badea392@gmail.com> | 2012-08-11 15:52:57 (GMT) |
commit | 5b030224c25dcb7e0f8dabe9ba80cc6d6397320b (patch) | |
tree | 71aa4e7d4ddeaa172fe233b096d4c354cc9ecb6f /Swiften | |
parent | dd28860d46fb873bd9ff221f0ef639c932c22bb8 (diff) | |
download | swift-contrib-5b030224c25dcb7e0f8dabe9ba80cc6d6397320b.zip swift-contrib-5b030224c25dcb7e0f8dabe9ba80cc6d6397320b.tar.bz2 |
Dump message logs using new db structure. Avatars not working
Diffstat (limited to 'Swiften')
-rw-r--r-- | Swiften/History/HistoryManager.h | 5 | ||||
-rw-r--r-- | Swiften/History/HistoryMessage.h | 9 | ||||
-rw-r--r-- | Swiften/History/SQLiteHistoryManager.cpp | 122 | ||||
-rw-r--r-- | Swiften/History/SQLiteHistoryManager.h | 5 |
4 files changed, 92 insertions, 49 deletions
diff --git a/Swiften/History/HistoryManager.h b/Swiften/History/HistoryManager.h index 1c0d784..a10bb41 100644 --- a/Swiften/History/HistoryManager.h +++ b/Swiften/History/HistoryManager.h @@ -6,6 +6,7 @@ #pragma once +#include <set> #include <vector> #include <Swiften/JID/JID.h> #include <Swiften/History/HistoryMessage.h> @@ -17,7 +18,7 @@ namespace Swift { virtual void addMessage(const HistoryMessage& message) = 0; - virtual std::vector<HistoryMessage> getMessages(const JID& selfJID, const JID& contactJID) const = 0; - virtual std::vector<JID> getAllContacts() const = 0; + virtual std::vector<HistoryMessage> getMessages(const JID& selfJID, const JID& contactJID, bool isGroupChat) const = 0; + virtual void getAllContacts(const JID& selfJID, std::set<JID>& mucs, std::set<JID>& contacts) const = 0; }; } diff --git a/Swiften/History/HistoryMessage.h b/Swiften/History/HistoryMessage.h index 0bcff99..021d6b9 100644 --- a/Swiften/History/HistoryMessage.h +++ b/Swiften/History/HistoryMessage.h @@ -16,10 +16,12 @@ namespace Swift { const std::string& message, const JID& fromJID, const JID& toJID, + bool isGroupChat, const boost::posix_time::ptime& time) : message_(message), fromJID_(fromJID), toJID_(toJID), + isGroupChat_(isGroupChat), time_(time) { } @@ -35,18 +37,23 @@ namespace Swift { return toJID_; } + bool isGroupChat() const { + return isGroupChat_; + } + boost::posix_time::ptime getTime() const { return time_; } bool operator==(const HistoryMessage& o) const { - return message_ == o.message_ && fromJID_ == o.fromJID_ && toJID_ == o.toJID_ && time_ == o.time_; + return message_ == o.message_ && fromJID_ == o.fromJID_ && toJID_ == o.toJID_ && isGroupChat_ == o.isGroupChat_ && time_ == o.time_; } private: std::string message_; JID fromJID_; JID toJID_; + bool isGroupChat_; // TODO: <- maybe use Message::Type ? boost::posix_time::ptime time_; }; } diff --git a/Swiften/History/SQLiteHistoryManager.cpp b/Swiften/History/SQLiteHistoryManager.cpp index 9f02ca2..af09b66 100644 --- a/Swiften/History/SQLiteHistoryManager.cpp +++ b/Swiften/History/SQLiteHistoryManager.cpp @@ -30,7 +30,7 @@ SQLiteHistoryManager::SQLiteHistoryManager(const std::string& file) : db_(0) { } char* errorMessage; - int result = sqlite3_exec(db_, "CREATE TABLE IF NOT EXISTS messages('message' STRING, 'fromBare' INTEGER, 'fromResource' STRING, 'toBare' INTEGER, 'toResource' STRING, 'time' INTEGER)", 0, 0, &errorMessage); + int result = sqlite3_exec(db_, "CREATE TABLE IF NOT EXISTS messages('message' STRING, 'fromBare' INTEGER, 'fromResource' STRING, 'toBare' INTEGER, 'toResource' STRING, 'groupChat' INTEGER, 'time' INTEGER)", 0, 0, &errorMessage); if (result != SQLITE_OK) { std::cerr << "SQL Error: " << errorMessage << std::endl; sqlite3_free(errorMessage); @@ -41,13 +41,6 @@ SQLiteHistoryManager::SQLiteHistoryManager(const std::string& file) : db_(0) { std::cerr << "SQL Error: " << errorMessage << std::endl; sqlite3_free(errorMessage); } - - // FIXME: remove - result = sqlite3_exec(db_, "CREATE TABLE IF NOT EXISTS conversations('id' INTEGER UNIQUE NOT NULL, 'time' INTEGER)", 0, 0, &errorMessage); - if (result != SQLITE_OK) { - std::cerr << "SQL Error: " << errorMessage << std::endl; - sqlite3_free(errorMessage); - } } SQLiteHistoryManager::~SQLiteHistoryManager() { @@ -55,16 +48,15 @@ SQLiteHistoryManager::~SQLiteHistoryManager() { } void SQLiteHistoryManager::addMessage(const HistoryMessage& message) { - // updateConversation(message); - int secondsSinceEpoch = (message.getTime() - boost::posix_time::ptime(boost::gregorian::date(1970, 1, 1))).total_seconds(); - std::string statement = std::string("'message', 'fromBare', 'fromResource', 'toBare', 'toResource', 'time') VALUES(") + + std::string statement = std::string("INSERT INTO messages('message', 'fromBare', 'fromResource', 'toBare', 'toResource', 'groupChat', 'time') VALUES(") + "'" + getEscapedString(message.getMessage()) + "', " + boost::lexical_cast<std::string>(getIDForJID(message.getFromJID().toBare())) + ", '" + getEscapedString(message.getFromJID().getResource()) + "', " + - boost::lexical_cast<std::string>(getIDForJID(message.getFromJID().toBare())) + ", '" + + boost::lexical_cast<std::string>(getIDForJID(message.getToJID().toBare())) + ", '" + getEscapedString(message.getToJID().getResource()) + "', " + + boost::lexical_cast<std::string>(message.isGroupChat()) + ", " + boost::lexical_cast<std::string>(secondsSinceEpoch) + ")"; char* errorMessage; int result = sqlite3_exec(db_, statement.c_str(), 0, 0, &errorMessage); @@ -74,7 +66,7 @@ void SQLiteHistoryManager::addMessage(const HistoryMessage& message) { } } -std::vector<HistoryMessage> SQLiteHistoryManager::getMessages(const JID& selfJID, const JID& contactJID) const { +std::vector<HistoryMessage> SQLiteHistoryManager::getMessages(const JID& selfJID, const JID& contactJID, bool isGroupChat) const { sqlite3_stmt* selectStatement; boost::optional<int> selfID = getIDFromJID(selfJID.toBare()); @@ -85,11 +77,22 @@ std::vector<HistoryMessage> SQLiteHistoryManager::getMessages(const JID& selfJID return std::vector<HistoryMessage>(); } - std::string selectQuery = "SELECT * FROM messages WHERE (fromBare=" + - boost::lexical_cast<std::string>(*selfID) + " AND fromBare=" + - boost::lexical_cast<std::string>(*contactID) + ") OR (fromBare=" + - boost::lexical_cast<std::string>(*contactID) + " AND fromBare=" + - boost::lexical_cast<std::string>(*selfID) + ")"; + std::string selectQuery = "SELECT * FROM messages WHERE groupChat=" + boost::lexical_cast<std::string>(isGroupChat); + if (contactJID.isBare()) { + // match only bare jid + selectQuery += " AND ((fromBare=" + boost::lexical_cast<std::string>(*selfID) + " AND toBare=" + + boost::lexical_cast<std::string>(*contactID) + ") OR (fromBare=" + + boost::lexical_cast<std::string>(*contactID) + " AND toBare=" + boost::lexical_cast<std::string>(*selfID) + "))"; + } + else { + // match resource too + selectQuery += " AND ((fromBare=" + boost::lexical_cast<std::string>(*selfID) + " AND (toBare=" + + boost::lexical_cast<std::string>(*contactID) +" AND toResource='" + + getEscapedString(contactJID.getResource()) + "')) OR ((fromBare=" + + boost::lexical_cast<std::string>(*contactID) + " AND fromResource='" + + getEscapedString(contactJID.getResource()) + "') AND toBare=" + + boost::lexical_cast<std::string>(*selfID) + "))"; + } int r = sqlite3_prepare(db_, selectQuery.c_str(), selectQuery.size(), &selectStatement, NULL); if (r != SQLITE_OK) { @@ -116,11 +119,14 @@ std::vector<HistoryMessage> SQLiteHistoryManager::getMessages(const JID& selfJID toJID = boost::optional<JID>(JID(toJID->getNode(), toJID->getDomain(), toResource)); } + // message type + bool isGroupChat = sqlite3_column_int(selectStatement, 5); + // timestamp - int secondsSinceEpoch(sqlite3_column_int(selectStatement, 5)); + int secondsSinceEpoch(sqlite3_column_int(selectStatement, 6)); boost::posix_time::ptime time(boost::gregorian::date(1970, 1, 1), boost::posix_time::seconds(secondsSinceEpoch)); - result.push_back(HistoryMessage(message, (fromJID ? *fromJID : JID()), (toJID ? *toJID : JID()), time)); + result.push_back(HistoryMessage(message, (fromJID ? *fromJID : JID()), (toJID ? *toJID : JID()), isGroupChat, time)); r = sqlite3_step(selectStatement); } if (r != SQLITE_DONE) { @@ -184,28 +190,15 @@ boost::optional<int> SQLiteHistoryManager::getIDFromJID(const JID& jid) const { return result; } -void SQLiteHistoryManager::updateConversation(const HistoryMessage& message) { - - /* - int id = getIDForJID(message.getBaseJID()); - int secondsSinceEpoch = (message.getTime() - boost::posix_time::ptime(boost::gregorian::date(1970, 1, 1))).total_seconds(); // this might fail in 2038 - - std::stringstream statement; - statement << "insert or replace into conversations('id', 'time') VALUES(" << - id <<", " << secondsSinceEpoch << ")"; - - char* errorMessage; - int result = sqlite3_exec(db_, statement.str().c_str(), 0, 0, &errorMessage); - - if (result != SQLITE_OK) { - std::cerr << "SQL Error: " << errorMessage << std::endl; - sqlite3_free(errorMessage); +void SQLiteHistoryManager::getAllContacts(const JID& selfJID, std::set<JID>& mucs, std::set<JID>& contacts) const { + // get id + boost::optional<int> id = getIDFromJID(selfJID); + if (!id) { + return; } - */ -} -std::vector<JID> SQLiteHistoryManager::getAllContacts() const { - std::string query("SELECT conversations.'id' FROM conversations"); + // get all MUCs + std::string query = "SELECT DISTINCT messages.'fromBare' FROM messages WHERE groupChat=1 AND toBare=" + boost::lexical_cast<std::string>(*id); sqlite3_stmt* selectStatement; int r = sqlite3_prepare(db_, query.c_str(), query.size(), &selectStatement, NULL); @@ -214,21 +207,64 @@ std::vector<JID> SQLiteHistoryManager::getAllContacts() const { } r = sqlite3_step(selectStatement); - std::vector<JID> result; while (r == SQLITE_ROW) { boost::optional<JID> contactJID(getJIDFromID(sqlite3_column_int(selectStatement, 0))); if (contactJID) { - result.push_back(*contactJID); + mucs.insert(*contactJID); } r = sqlite3_step(selectStatement); } + if (r != SQLITE_DONE) { std::cout << "Error: " << sqlite3_errmsg(db_) << std::endl; } + sqlite3_finalize(selectStatement); + + // get all contacts + query = "SELECT DISTINCT messages.'fromBare', messages.'fromResource', messages.'toBare', messages.'toResource' FROM messages WHERE groupChat=0 AND (toBare=" + + boost::lexical_cast<std::string>(*id) + " OR fromBare=" + boost::lexical_cast<std::string>(*id) + ")"; + + r = sqlite3_prepare(db_, query.c_str(), query.size(), &selectStatement, NULL); + if (r != SQLITE_OK) { + std::cout << "Error: " << sqlite3_errmsg(db_) << std::endl; + } + + r = sqlite3_step(selectStatement); + while (r == SQLITE_ROW) { + int fromBareID = sqlite3_column_int(selectStatement, 0); + std::string fromResource(reinterpret_cast<const char*>(sqlite3_column_text(selectStatement, 1))); + int toBareID = sqlite3_column_int(selectStatement, 2); + std::string toResource(reinterpret_cast<const char*>(sqlite3_column_text(selectStatement, 3))); + std::string resource; + + boost::optional<JID> contactJID; + + if (fromBareID == *id) { + contactJID = getJIDFromID(toBareID); + resource = toResource; + } + else { + contactJID = getJIDFromID(fromBareID); + resource = fromResource; + } + // check if it is a MUC contact (from a private conversation) + if (contactJID && mucs.count(*contactJID)) { + contactJID = boost::optional<JID>(JID(contactJID->getNode(), contactJID->getDomain(), resource)); + } + + if (contactJID) { + contacts.insert(*contactJID); + } + + r = sqlite3_step(selectStatement); + } + + if (r != SQLITE_DONE) { + std::cout << "Error: " << sqlite3_errmsg(db_) << std::endl; + } sqlite3_finalize(selectStatement); - return result; } } diff --git a/Swiften/History/SQLiteHistoryManager.h b/Swiften/History/SQLiteHistoryManager.h index 9d9ec05..480d570 100644 --- a/Swiften/History/SQLiteHistoryManager.h +++ b/Swiften/History/SQLiteHistoryManager.h @@ -19,13 +19,12 @@ namespace Swift { ~SQLiteHistoryManager(); void addMessage(const HistoryMessage& message); - std::vector<HistoryMessage> getMessages(const JID& selfJID, const JID& contactJID) const; - std::vector<JID> getAllContacts() const; + std::vector<HistoryMessage> getMessages(const JID& selfJID, const JID& contactJID, bool isGroupChat) const; + void getAllContacts(const JID& selfJID, std::set<JID>& mucs, std::set<JID>& contacts) const; private: int getIDForJID(const JID&); int addJID(const JID&); - void updateConversation(const HistoryMessage& message); boost::optional<JID> getJIDFromID(int id) const; boost::optional<int> getIDFromJID(const JID& jid) const; |