summaryrefslogtreecommitdiffstats
blob: 33d2c58dc0e42b3a659438baf03506133282be06 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*
 * Copyright (c) 2010 Remko Tronçon
 * Licensed under the GNU General Public License v3.
 * See Documentation/Licenses/GPLv3.txt for more information.
 */

#include <iostream>
#include <boost/lexical_cast.hpp>

#include <sqlite3.h>
#include <Swiften/History/SQLiteHistoryManager.h>
#include <sstream>

inline std::string getEscapedString(const std::string& s) {
	std::string result(s);
	// result.replaceAll('\'', std::string("\\'"));
	return result;
}

namespace Swift {

SQLiteHistoryManager::SQLiteHistoryManager(const std::string& file) : db_(0) {
	sqlite3_open(file.c_str(), &db_);
	if (!db_) {
		std::cerr << "Error opening database " << file << std::endl; // FIXME
	}

	char* errorMessage;
	int result = sqlite3_exec(db_, "CREATE TABLE IF NOT EXISTS messages('baseID' INTEGER, 'from' INTEGER, 'nick' STRING, 'message' STRING, 'time' INTEGER)", 0, 0, &errorMessage);
	if (result != SQLITE_OK) {
		std::cerr << "SQL Error: " << errorMessage << std::endl;
		sqlite3_free(errorMessage);
	}

	result = sqlite3_exec(db_, "CREATE TABLE IF NOT EXISTS jids('id' INTEGER PRIMARY KEY ASC AUTOINCREMENT, 'jid' STRING UNIQUE NOT NULL)", 0, 0, &errorMessage);
	if (result != SQLITE_OK) {
		std::cerr << "SQL Error: " << errorMessage << std::endl;
		sqlite3_free(errorMessage);
	}

	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() {
	sqlite3_close(db_);
}

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("INSERT INTO messages('baseID', 'from', 'nick', 'message', 'time') VALUES(") +
					boost::lexical_cast<std::string>(getIDForJID(message.getBaseJID())) + ", " +
					boost::lexical_cast<std::string>(getIDForJID(message.getFromJID())) + ", '" +
					message.getDisplayNick() + "', '" +
					getEscapedString(message.getMessage()) + "', " +
					boost::lexical_cast<std::string>(secondsSinceEpoch) + ")";
	char* errorMessage;
	int result = sqlite3_exec(db_, statement.c_str(), 0, 0, &errorMessage);
	if (result != SQLITE_OK) {
		std::cerr << "SQL Error: " << errorMessage << std::endl;
		sqlite3_free(errorMessage);
	}
}

std::vector<HistoryMessage> SQLiteHistoryManager::getMessages(const JID& baseJID) const {
	std::vector<HistoryMessage> result;
	sqlite3_stmt* selectStatement;
	boost::optional<int> baseID = getIDFromJID(baseJID);
	if (!baseID) {
		return std::vector<HistoryMessage>();
	}

	std::stringstream selectQuery;
	selectQuery << "SELECT messages.'baseID', messages.'from', messages.'nick', messages.'message', messages.'time' FROM messages WHERE baseID=" << *baseID;

	int r = sqlite3_prepare(db_, selectQuery.str().c_str(), selectQuery.str().size(), &selectStatement, NULL);
	if (r != SQLITE_OK) {
		std::cout << "Error: " << sqlite3_errmsg(db_) << std::endl;
	}
	r = sqlite3_step(selectStatement);
	while (r == SQLITE_ROW) {
		boost::optional<JID> baseJID(getJIDFromID(sqlite3_column_int(selectStatement, 0)));
		boost::optional<JID> from(getJIDFromID(sqlite3_column_int(selectStatement, 1)));
		std::string nick(reinterpret_cast<const char*>(sqlite3_column_text(selectStatement, 2)));
		std::string message(reinterpret_cast<const char*>(sqlite3_column_text(selectStatement, 3)));
		int secondsSinceEpoch(sqlite3_column_int(selectStatement, 4));
		boost::posix_time::ptime time(boost::gregorian::date(1970, 1, 1), boost::posix_time::seconds(secondsSinceEpoch));

		result.push_back(HistoryMessage(message, (baseJID ? *baseJID : JID()), (from ? *from : JID()), nick, time));
		r = sqlite3_step(selectStatement);
	}
	if (r != SQLITE_DONE) {
		std::cout << "Error: " << sqlite3_errmsg(db_) << std::endl;
	}
	sqlite3_finalize(selectStatement);
	return result;
}

int SQLiteHistoryManager::getIDForJID(const JID& jid) {
	boost::optional<int> id = getIDFromJID(jid);
	if (id) {
		return *id;
	}
	else {
		return addJID(jid);
	}
}

int SQLiteHistoryManager::addJID(const JID& jid) {
	std::string statement = std::string("INSERT INTO jids('jid') VALUES('") + getEscapedString(jid.toString()) + "')";
	char* errorMessage;
	int result = sqlite3_exec(db_, statement.c_str(), 0, 0, &errorMessage);
	if (result != SQLITE_OK) {
		std::cerr << "SQL Error: " << errorMessage << std::endl;
		sqlite3_free(errorMessage);
	}
	return sqlite3_last_insert_rowid(db_);
}

boost::optional<JID> SQLiteHistoryManager::getJIDFromID(int id) const {
	boost::optional<JID> result;
	sqlite3_stmt* selectStatement;
	std::string selectQuery("SELECT jid FROM jids WHERE id=" + boost::lexical_cast<std::string>(id));
	int r = sqlite3_prepare(db_, selectQuery.c_str(), selectQuery.size(), &selectStatement, NULL);
	if (r != SQLITE_OK) {
		std::cout << "Error: " << sqlite3_errmsg(db_) << std::endl;
	}
	r = sqlite3_step(selectStatement);
	if (r == SQLITE_ROW) {
		result = boost::optional<JID>(reinterpret_cast<const char*>(sqlite3_column_text(selectStatement, 0)));
	}
	sqlite3_finalize(selectStatement);
	return result;
}

boost::optional<int> SQLiteHistoryManager::getIDFromJID(const JID& jid) const {
	boost::optional<int> result;
	sqlite3_stmt* selectStatement;
	std::string selectQuery("SELECT id FROM jids WHERE jid='" + jid.toString() + "'");
	int r = sqlite3_prepare(db_, selectQuery.c_str(), selectQuery.size(), &selectStatement, NULL);
	if (r != SQLITE_OK) {
		std::cout << "Error: " << sqlite3_errmsg(db_) << std::endl;
	}
	r = sqlite3_step(selectStatement);
	if (r == SQLITE_ROW) {
		result = boost::optional<int>(sqlite3_column_int(selectStatement, 0));
	}
	sqlite3_finalize(selectStatement);
	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);
	}
}

std::vector<JID> SQLiteHistoryManager::getAllContacts() const {
	std::string query("SELECT conversations.'id' FROM conversations");
	sqlite3_stmt* selectStatement;

	int 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);
	std::vector<JID> result;
	while (r == SQLITE_ROW) {
		boost::optional<JID> contactJID(getJIDFromID(sqlite3_column_int(selectStatement, 0)));
		if (contactJID) {
			result.push_back(*contactJID);
		}

		r = sqlite3_step(selectStatement);
	}
	if (r != SQLITE_DONE) {
		std::cout << "Error: " << sqlite3_errmsg(db_) << std::endl;
	}

	sqlite3_finalize(selectStatement);
	return result;
}

}