1
0
mirror of https://github.com/qTox/qTox.git synced 2024-03-22 14:00:36 +08:00

Merge branch 'pr1527' into back_in_the_game

This commit is contained in:
tux3 2015-04-20 12:00:53 +02:00
commit 2d213b7616
No known key found for this signature in database
GPG Key ID: 7E086DD661263264
3 changed files with 123 additions and 44 deletions

View File

@ -174,6 +174,7 @@ void Core::make_tox(QByteArray savedata)
toxOptions.proxy_type = TOX_PROXY_TYPE_SOCKS5;
else if (proxyType == ProxyType::ptHTTP)
toxOptions.proxy_type = TOX_PROXY_TYPE_HTTP;
QByteArray proxyAddrData = proxyAddr.toUtf8();
/// TODO: We're leaking a tiny amount of memory there, go fix that later
char* proxyAddrCopy = new char[proxyAddrData.size()+1];
@ -205,7 +206,9 @@ void Core::make_tox(QByteArray savedata)
return;
}
else
{
qWarning() << "Core failed to start with IPv6, falling back to IPv4. LAN discovery may not work properly.";
}
}
else if (toxOptions.proxy_type != TOX_PROXY_TYPE_NONE)
{
@ -338,7 +341,9 @@ void Core::start()
setAvatar(data);
}
else
{
qDebug() << "Core: Error loading self avatar";
}
ready = true;
@ -369,7 +374,9 @@ void Core::process()
#endif
if (checkConnection())
{
tolerance = CORE_DISCONNECT_TOLERANCE;
}
else if (!(--tolerance))
{
bootstrapDht();
@ -385,13 +392,16 @@ bool Core::checkConnection()
//static int count = 0;
bool toxConnected = tox_self_get_connection_status(tox) != TOX_CONNECTION_NONE;
if (toxConnected && !isConnected) {
if (toxConnected && !isConnected)
{
qDebug() << "Core: Connected to DHT";
emit connected();
isConnected = true;
//if (count) qDebug() << "Core: disconnect count:" << count;
//count = 0;
} else if (!toxConnected && isConnected) {
}
else if (!toxConnected && isConnected)
{
qDebug() << "Core: Disconnected to DHT";
emit disconnected();
isConnected = false;
@ -421,10 +431,14 @@ void Core::bootstrapDht()
const Settings::DhtServer& dhtServer = dhtServerList[j % listSize];
if (tox_bootstrap(tox, dhtServer.address.toLatin1().data(),
dhtServer.port, CUserId(dhtServer.userId).data(), nullptr) == 1)
{
qDebug() << QString("Core: Bootstrapping from ")+dhtServer.name+QString(", addr ")+dhtServer.address.toLatin1().data()
+QString(", port ")+QString().setNum(dhtServer.port);
}
else
{
qDebug() << "Core: Error bootstrapping from "+dhtServer.name;
}
j++;
i++;
@ -487,7 +501,8 @@ void Core::onConnectionStatusChanged(Tox*/* tox*/, uint32_t friendId, TOX_CONNEC
{
Status friendStatus = status ? Status::Online : Status::Offline;
emit static_cast<Core*>(core)->friendStatusChanged(friendId, friendStatus);
if (friendStatus == Status::Offline) {
if (friendStatus == Status::Offline)
{
static_cast<Core*>(core)->checkLastOnline(friendId);
for (ToxFile& f : fileSendQueue)
@ -506,7 +521,9 @@ void Core::onConnectionStatusChanged(Tox*/* tox*/, uint32_t friendId, TOX_CONNEC
emit static_cast<Core*>(core)->fileTransferBrokenUnbroken(f, true);
}
}
} else {
}
else
{
for (ToxFile& f : fileRecvQueue)
{
if (f.friendId == friendId && f.status == ToxFile::BROKEN)
@ -563,6 +580,7 @@ void Core::onGroupTitleChange(Tox*, int groupnumber, int peernumber, const uint8
QString author;
if (peernumber >= 0)
author = core->getGroupPeerName(groupnumber, peernumber);
emit core->groupTitleChanged(groupnumber, author, CString::toString(title, len));
}
@ -779,9 +797,12 @@ void Core::onReadReceiptCallback(Tox*, uint32_t friendnumber, uint32_t receipt,
void Core::acceptFriendRequest(const QString& userId)
{
uint32_t friendId = tox_friend_add_norequest(tox, CUserId(userId).data(), nullptr);
if (friendId == UINT32_MAX) {
if (friendId == UINT32_MAX)
{
emit failedToAddFriend(userId);
} else {
}
else
{
saveConfiguration();
emit friendAdded(friendId, userId);
}
@ -1069,9 +1090,13 @@ void Core::removeFriend(uint32_t friendId, bool fake)
{
if (!isReady() || fake)
return;
if (tox_friend_delete(tox, friendId, nullptr) == false) {
if (tox_friend_delete(tox, friendId, nullptr) == false)
{
emit failedToRemoveFriend(friendId);
} else {
}
else
{
saveConfiguration();
emit friendRemoved(friendId);
}
@ -1081,6 +1106,7 @@ void Core::removeGroup(int groupId, bool fake)
{
if (!isReady() || fake)
return;
tox_del_groupchat(tox, groupId);
if (groupCalls[groupId].active)
@ -1102,9 +1128,12 @@ void Core::setUsername(const QString& username)
{
CString cUsername(username);
if (tox_self_set_name(tox, cUsername.data(), cUsername.size(), nullptr) == false) {
if (tox_self_set_name(tox, cUsername.data(), cUsername.size(), nullptr) == false)
{
emit failedToSetUsername(username);
} else {
}
else
{
emit usernameSet(username);
saveConfiguration();
}
@ -1171,7 +1200,9 @@ void Core::setStatusMessage(const QString& message)
if (tox_self_set_status_message(tox, cMessage.data(), cMessage.size(), nullptr) == false) {
emit failedToSetStatusMessage(message);
} else {
}
else
{
saveConfiguration();
emit statusMessageSet(message);
}
@ -1180,7 +1211,8 @@ void Core::setStatusMessage(const QString& message)
void Core::setStatus(Status status)
{
TOX_USER_STATUS userstatus;
switch (status) {
switch (status)
{
case Status::Online:
userstatus = TOX_USER_STATUS_NONE;
break;
@ -1213,11 +1245,14 @@ QString Core::sanitize(QString name)
QList<QChar> banned = {'/', '\\', ':', '<', '>', '"', '|', '?', '*'};
for (QChar c : banned)
name.replace(c, '_');
// also remove leading and trailing periods
if (name[0] == '.')
name[0] = '_';
if (name.endsWith('.'))
name[name.length()-1] = '_';
return name;
}
@ -1229,18 +1264,21 @@ QByteArray Core::loadToxSave(QString path)
QFile configurationFile(path);
qDebug() << "Core::loadConfiguration: reading from " << path;
if (!configurationFile.exists()) {
if (!configurationFile.exists())
{
qWarning() << "The Tox configuration file was not found";
return data;
}
if (!configurationFile.open(QIODevice::ReadOnly)) {
if (!configurationFile.open(QIODevice::ReadOnly))
{
qCritical() << "File " << path << " cannot be opened";
return data;
}
qint64 fileSize = configurationFile.size();
if (fileSize > 0) {
if (fileSize > 0)
{
data = configurationFile.readAll();
/* TODO: Clean this up
int error = tox_load(tox, reinterpret_cast<uint8_t *>(data.data()), data.size());
@ -1282,7 +1320,8 @@ void Core::saveConfiguration()
QString dir = Settings::getSettingsDirPath();
QDir directory(dir);
if (!directory.exists() && !directory.mkpath(directory.absolutePath())) {
if (!directory.exists() && !directory.mkpath(directory.absolutePath()))
{
qCritical() << "Error while creating directory " << dir;
return;
}
@ -1341,17 +1380,21 @@ void Core::switchConfiguration(const QString& profile)
void Core::loadFriends()
{
const uint32_t friendCount = tox_self_get_friend_list_size(tox);
if (friendCount > 0) {
if (friendCount > 0)
{
// assuming there are not that many friends to fill up the whole stack
uint32_t *ids = new uint32_t[friendCount];
tox_self_get_friend_list(tox, ids);
uint8_t clientId[TOX_PUBLIC_KEY_SIZE];
for (int32_t i = 0; i < static_cast<int32_t>(friendCount); ++i) {
if (tox_friend_get_public_key(tox, ids[i], clientId, nullptr)) {
for (int32_t i = 0; i < static_cast<int32_t>(friendCount); ++i)
{
if (tox_friend_get_public_key(tox, ids[i], clientId, nullptr))
{
emit friendAdded(ids[i], CUserId::toString(clientId));
const size_t nameSize = tox_friend_get_name_size(tox, ids[i], nullptr);
if (nameSize != SIZE_MAX) {
if (nameSize != SIZE_MAX)
{
uint8_t *name = new uint8_t[nameSize];
if (tox_friend_get_name(tox, ids[i], name, nullptr))
emit friendUsernameChanged(ids[i], CString::toString(name, nameSize));
@ -1359,9 +1402,11 @@ void Core::loadFriends()
}
const size_t statusMessageSize = tox_friend_get_status_message_size(tox, ids[i], nullptr);
if (statusMessageSize != SIZE_MAX) {
if (statusMessageSize != SIZE_MAX)
{
uint8_t *statusMessage = new uint8_t[statusMessageSize];
if (tox_friend_get_status_message(tox, ids[i], statusMessage, nullptr)) {
if (tox_friend_get_status_message(tox, ids[i], statusMessage, nullptr))
{
emit friendStatusMessageChanged(ids[i], CString::toString(statusMessage, statusMessageSize));
}
delete[] statusMessage;
@ -1377,9 +1422,8 @@ void Core::loadFriends()
void Core::checkLastOnline(uint32_t friendId) {
const uint64_t lastOnline = tox_friend_get_last_online(tox, friendId, nullptr);
if (lastOnline != UINT64_MAX) {
if (lastOnline != UINT64_MAX)
emit friendLastSeenChanged(friendId, QDateTime::fromTime_t(lastOnline));
}
}
int Core::getGroupNumberPeers(int groupId) const
@ -1436,6 +1480,7 @@ QList<QString> Core::getGroupPeerNames(int groupId) const
}
for (int i=0; i<nPeers; i++)
names.push_back(CString::toString(namesArray[i], lengths[i]));
return names;
}
@ -1623,9 +1668,7 @@ bool Core::hasFriendWithPublicKey(const QString &pubkey) const
{
// Valid length check
if (pubkey.length() != (TOX_PUBLIC_KEY_SIZE * 2))
{
return false;
}
bool found = false;
const size_t friendCount = tox_self_get_friend_list_size(tox);
@ -1656,7 +1699,8 @@ QString Core::getFriendAddress(uint32_t friendNumber) const
{
// If we don't know the full address of the client, return just the id, otherwise get the full address
uint8_t rawid[TOX_PUBLIC_KEY_SIZE];
if (!tox_friend_get_public_key(tox, friendNumber, rawid, nullptr)) {
if (!tox_friend_get_public_key(tox, friendNumber, rawid, nullptr))
{
qWarning() << "Core::getFriendAddress: Getting public key failed";
return QString();
}
@ -1673,7 +1717,8 @@ QString Core::getFriendAddress(uint32_t friendNumber) const
QString Core::getFriendUsername(uint32_t friendnumber) const
{
size_t namesize = tox_friend_get_name_size(tox, friendnumber, nullptr);
if (namesize == SIZE_MAX) {
if (namesize == SIZE_MAX)
{
qWarning() << "Core::getFriendUsername: Failed to get name size for friend "<<friendnumber;
return QString();
}

View File

@ -107,6 +107,7 @@ QByteArray Core::encryptData(const QByteArray& data, PasswordType passtype)
{
if (!pwsaltedkeys[passtype])
return QByteArray();
uint8_t encrypted[data.size() + TOX_PASS_ENCRYPTION_EXTRA_LENGTH];
if (!tox_pass_key_encrypt(reinterpret_cast<const uint8_t*>(data.data()), data.size(),
pwsaltedkeys[passtype], encrypted, nullptr))
@ -121,6 +122,7 @@ QByteArray Core::decryptData(const QByteArray& data, PasswordType passtype)
{
if (!pwsaltedkeys[passtype])
return QByteArray();
int sz = data.size() - TOX_PASS_ENCRYPTION_EXTRA_LENGTH;
uint8_t decrypted[sz];
if (!tox_pass_key_decrypt(reinterpret_cast<const uint8_t*>(data.data()), data.size(),
@ -237,10 +239,12 @@ void Core::checkEncryptedHistory()
{
if (!exists || HistoryKeeper::checkPassword())
return;
dialogtxt = tr("The chat history password failed. Please try another?", "used only when pw set before load() doesn't work");
}
else
dialogtxt = a;
dialogtxt += "\n" + c;
if (pwsaltedkeys[ptMain])
@ -287,7 +291,8 @@ void Core::saveConfiguration(const QString& path)
}
QSaveFile configurationFile(path);
if (!configurationFile.open(QIODevice::WriteOnly)) {
if (!configurationFile.open(QIODevice::WriteOnly))
{
qCritical() << "File " << path << " cannot be opened";
return;
}
@ -304,7 +309,8 @@ void Core::saveConfiguration(const QString& path)
else
fileSize = tox_get_savedata_size(tox);
if (fileSize > 0 && fileSize <= std::numeric_limits<int32_t>::max()) {
if (fileSize > 0 && fileSize <= std::numeric_limits<int32_t>::max())
{
uint8_t *data = new uint8_t[fileSize];
if (encrypt)

View File

@ -58,35 +58,45 @@ QByteArray ToxDNS::fetchLastTextRecord(const QString& record, bool silent)
qApp->processEvents();
QThread::msleep(100);
}
if (timeout >= 30) {
if (timeout >= 30)
{
dns.abort();
if (!silent)
showWarning(tr("The connection timed out","The DNS gives the Tox ID associated to toxme.se addresses"));
return result;
}
if (dns.error() == QDnsLookup::NotFoundError) {
if (dns.error() == QDnsLookup::NotFoundError)
{
if (!silent)
showWarning(tr("This address does not exist","The DNS gives the Tox ID associated to toxme.se addresses"));
return result;
}
else if (dns.error() != QDnsLookup::NoError) {
else if (dns.error() != QDnsLookup::NoError)
{
if (!silent)
showWarning(tr("Error while looking up DNS","The DNS gives the Tox ID associated to toxme.se addresses"));
return result;
}
const QList<QDnsTextRecord> textRecords = dns.textRecords();
if (textRecords.isEmpty()) {
if (textRecords.isEmpty())
{
if (!silent)
showWarning(tr("No text record found", "Error with the DNS"));
return result;
}
const QList<QByteArray> textRecordValues = textRecords.last().values();
if (textRecordValues.length() != 1) {
if (textRecordValues.length() != 1)
{
if (!silent)
showWarning(tr("Unexpected number of values in text record", "Error with the DNS"));
return result;
}
@ -104,7 +114,8 @@ QString ToxDNS::queryTox1(const QString& record, bool silent)
// Check toxdns protocol version
int verx = entry.indexOf("v=");
if (verx) {
if (verx)
{
verx += 2;
int verend = entry.indexOf(';', verx);
if (verend)
@ -114,6 +125,7 @@ QString ToxDNS::queryTox1(const QString& record, bool silent)
{
if (!silent)
showWarning(tr("The version of Tox DNS used by this server is not supported", "Error with the DNS"));
return toxId;
}
}
@ -121,23 +133,29 @@ QString ToxDNS::queryTox1(const QString& record, bool silent)
// Get the tox id
int idx = entry.indexOf("id=");
if (idx < 0) {
if (idx < 0)
{
if (!silent)
showWarning(tr("The DNS lookup does not contain any Tox ID", "Error with the DNS"));
return toxId;
}
idx += 3;
if (entry.length() < idx + static_cast<int>(TOX_HEX_ID_LENGTH)) {
if (entry.length() < idx + static_cast<int>(TOX_HEX_ID_LENGTH))
{
if (!silent)
showWarning(tr("The DNS lookup does not contain a valid Tox ID", "Error with the DNS"));
return toxId;
}
toxId = entry.mid(idx, TOX_HEX_ID_LENGTH);
if (!ToxID::isToxId(toxId)) {
if (!ToxID::isToxId(toxId))
{
if (!silent)
showWarning(tr("The DNS lookup does not contain a valid Tox ID", "Error with the DNS"));
return toxId;
}
@ -178,7 +196,8 @@ QString ToxDNS::queryTox3(const tox3_server& server, const QString &record, bool
// Check toxdns protocol version
verx = entry.indexOf("v=");
if (verx!=-1) {
if (verx!=-1)
{
verx += 2;
int verend = entry.indexOf(';', verx);
if (verend!=-1)
@ -194,7 +213,8 @@ QString ToxDNS::queryTox3(const tox3_server& server, const QString &record, bool
// Get and decrypt the tox id
idx = entry.indexOf("id=");
if (idx < 0) {
if (idx < 0)
{
qWarning() << "queryTox3: Server "<<server.name<<" returned an empty id, using tox1 as a fallback";
goto fallbackOnTox1;
}
@ -217,6 +237,7 @@ QString ToxDNS::queryTox3(const tox3_server& server, const QString &record, bool
fallbackOnTox1:
if (tox_dns3)
tox_dns3_kill(tox_dns3);
#if TOX1_SILENT_FALLBACK
toxIdStr = queryTox1(record, silent);
#elif TOX1_ASK_FALLBACK
@ -226,6 +247,7 @@ Should tox1 be used anyway?\n\
If unsure, press No, so that request to ToxDNS service will not be made using unsecure protocol."), QMessageBox::Yes|QMessageBox::No, QMessageBox::No);
if (btn == QMessageBox::Yes)
queryTox1(record, silent);
#endif
return toxIdStr;
}
@ -234,12 +256,17 @@ ToxID ToxDNS::resolveToxAddress(const QString &address, bool silent)
{
ToxID toxId;
if (address.isEmpty()) {
if (address.isEmpty())
{
return toxId;
} else if (ToxID::isToxId(address)) {
}
else if (ToxID::isToxId(address))
{
toxId = ToxID::fromString(address);
return toxId;
} else {
}
else
{
// If we're querying one of our pinned server, do a tox3 request directly
QString servname = address.mid(address.indexOf('@')+1);
for (const ToxDNS::tox3_server& pin : ToxDNS::pinnedServers)
@ -272,6 +299,7 @@ Should tox1 be used anyway?\n\
If unsure, press No, so that request to ToxDNS service will not be made using unsecure protocol."), QMessageBox::Ok|QMessageBox::No, QMessageBox::No);
if (btn == QMessageBox::Ok)
toxId = ToxID::fromString(queryTox1(address, silent));
#else
return toxId;
#endif