mirror of
https://github.com/qTox/qTox.git
synced 2024-03-22 14:00:36 +08:00
docs(net): Change comment style
This commit is contained in:
parent
394c4bcc99
commit
52ff1c2aa8
|
@ -17,7 +17,6 @@
|
|||
along with qTox. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
|
||||
#include "src/net/autoupdate.h"
|
||||
#include "src/persistence/serialize.h"
|
||||
#include "src/persistence/settings.h"
|
||||
|
@ -40,6 +39,13 @@
|
|||
#include <shellapi.h>
|
||||
#endif
|
||||
|
||||
/**
|
||||
@file autoupdate.cpp
|
||||
|
||||
For now we only support auto updates on Windows and OS X, although extending it is not a technical issue.
|
||||
Linux users are expected to use their package managers or update manually through official channels.
|
||||
*/
|
||||
|
||||
#ifdef Q_OS_WIN
|
||||
#ifdef Q_OS_WIN64
|
||||
const QString AutoUpdater::platform = "win64";
|
||||
|
@ -72,6 +78,50 @@ const QString AutoUpdater::updaterBin;
|
|||
const QString AutoUpdater::updateServer;
|
||||
unsigned char AutoUpdater::key[crypto_sign_PUBLICKEYBYTES];
|
||||
#endif
|
||||
|
||||
/**
|
||||
@var unsigned char AutoUpdater::UpdateFileMeta::sig[crypto_sign_BYTES]
|
||||
@brief Signature of the file (ed25519)
|
||||
|
||||
@var QString AutoUpdater::UpdateFileMeta::id
|
||||
@brief Unique id of the file
|
||||
|
||||
@var QString AutoUpdater::UpdateFileMeta::installpath
|
||||
@brief Local path including the file name. May be relative to qtox-updater or absolute
|
||||
|
||||
@var uint64_t AutoUpdater::UpdateFileMeta::size
|
||||
@brief Size in bytes of the file
|
||||
*/
|
||||
|
||||
/**
|
||||
@var static const QString AutoUpdater::updateServer
|
||||
@brief Hostname of the qTox update server
|
||||
|
||||
@var static const QString AutoUpdater::platform
|
||||
@brief Name of platform we're trying to get updates for
|
||||
|
||||
@var static const QString AutoUpdater::checkURI
|
||||
@brief URI of the file containing the latest version string
|
||||
|
||||
@var static const QString AutoUpdater::flistURI
|
||||
@brief URI of the file containing info on each file (hash, signature, size, name, ..)
|
||||
|
||||
@var static const QString AutoUpdater::filesURI
|
||||
@brief URI of the actual files of the latest version
|
||||
|
||||
@var static const QString AutoUpdater::updaterBin
|
||||
@brief Path to the qtox-updater binary
|
||||
|
||||
@var static std::atomic_bool AutoUpdater::abortFlag
|
||||
@brief If true, try to abort everything.
|
||||
|
||||
@var static std::atomic_bool AutoUpdater::isDownloadingUpdate
|
||||
@brief We'll pretend there's no new update available if we're already updating
|
||||
|
||||
@var static QMutex AutoUpdater::progressVersionMutex
|
||||
@brief No, we can't just make the QString atomic
|
||||
*/
|
||||
|
||||
const QString AutoUpdater::checkURI = AutoUpdater::updateServer+"/qtox/"+AutoUpdater::platform+"/version";
|
||||
const QString AutoUpdater::flistURI = AutoUpdater::updateServer+"/qtox/"+AutoUpdater::platform+"/flist";
|
||||
const QString AutoUpdater::filesURI = AutoUpdater::updateServer+"/qtox/"+AutoUpdater::platform+"/files/";
|
||||
|
@ -81,6 +131,19 @@ std::atomic<float> AutoUpdater::progressValue{0};
|
|||
QString AutoUpdater::progressVersion;
|
||||
QMutex AutoUpdater::progressVersionMutex;
|
||||
|
||||
/**
|
||||
@class AutoUpdater
|
||||
@brief Handles checking and applying updates for qTox.
|
||||
@note Do *NOT* use auto update unless AUTOUPDATE_ENABLED is defined to 1.
|
||||
*/
|
||||
|
||||
/**
|
||||
@brief Checks if an update is available for download.
|
||||
@return True if an update is available for download, false otherwise.
|
||||
|
||||
Connects to the qTox update server, and check if an update is available for download
|
||||
Will call getUpdateVersion, and as such may block and processEvents.
|
||||
*/
|
||||
bool AutoUpdater::isUpdateAvailable()
|
||||
{
|
||||
if (isDownloadingUpdate)
|
||||
|
@ -95,6 +158,11 @@ bool AutoUpdater::isUpdateAvailable()
|
|||
return !diff.isEmpty();
|
||||
}
|
||||
|
||||
/**
|
||||
@brief Fetch the version info of the last update available from the qTox update server
|
||||
@note Will try to follow qTox's proxy settings, may block and processEvents
|
||||
@return Avaliable version info.
|
||||
*/
|
||||
AutoUpdater::VersionInfo AutoUpdater::getUpdateVersion()
|
||||
{
|
||||
VersionInfo versionInfo;
|
||||
|
@ -159,6 +227,11 @@ AutoUpdater::VersionInfo AutoUpdater::getUpdateVersion()
|
|||
return versionInfo;
|
||||
}
|
||||
|
||||
/**
|
||||
@brief Parses and validates a flist file.
|
||||
@param flistData Install file data.
|
||||
@return An empty list on error.
|
||||
*/
|
||||
QList<AutoUpdater::UpdateFileMeta> AutoUpdater::parseFlist(QByteArray flistData)
|
||||
{
|
||||
QList<UpdateFileMeta> flist;
|
||||
|
@ -218,6 +291,11 @@ QList<AutoUpdater::UpdateFileMeta> AutoUpdater::parseFlist(QByteArray flistData)
|
|||
return flist;
|
||||
}
|
||||
|
||||
/**
|
||||
@brief Gets the update server's flist.
|
||||
@note Will try to follow qTox's proxy settings, may block and processEvents
|
||||
@return An empty array on error
|
||||
*/
|
||||
QByteArray AutoUpdater::getUpdateFlist()
|
||||
{
|
||||
QByteArray flist;
|
||||
|
@ -247,6 +325,11 @@ QByteArray AutoUpdater::getUpdateFlist()
|
|||
return flist;
|
||||
}
|
||||
|
||||
/**
|
||||
@brief Generates a list of files we need to update.
|
||||
@param updateFlist List of files available to update.
|
||||
@return List of files we need to update.
|
||||
*/
|
||||
QList<AutoUpdater::UpdateFileMeta> AutoUpdater::genUpdateDiff(QList<UpdateFileMeta> updateFlist)
|
||||
{
|
||||
QList<UpdateFileMeta> diff;
|
||||
|
@ -258,6 +341,11 @@ QList<AutoUpdater::UpdateFileMeta> AutoUpdater::genUpdateDiff(QList<UpdateFileMe
|
|||
return diff;
|
||||
}
|
||||
|
||||
/**
|
||||
@brief Checks if we have an up to date version of this file locally installed.
|
||||
@param fileMeta File to check.
|
||||
@return True if file doesn't need updates, false if need.
|
||||
*/
|
||||
bool AutoUpdater::isUpToDate(AutoUpdater::UpdateFileMeta fileMeta)
|
||||
{
|
||||
QString appDir = qApp->applicationDirPath();
|
||||
|
@ -273,6 +361,14 @@ bool AutoUpdater::isUpToDate(AutoUpdater::UpdateFileMeta fileMeta)
|
|||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
@brief Tries to fetch the file from the update server.
|
||||
@note Note that a file with an empty but non-null QByteArray is not an error, merely a file of size 0.
|
||||
@note Will try to follow qTox's proxy settings, may block and processEvents.
|
||||
@param fileMeta Meta data fo file to update.
|
||||
@param progressCallback Callback function, which will connected with QNetworkReply::downloadProgress
|
||||
@return A file with a null QByteArray on error.
|
||||
*/
|
||||
AutoUpdater::UpdateFile AutoUpdater::getUpdateFile(UpdateFileMeta fileMeta,
|
||||
std::function<void(int,int)> progressCallback)
|
||||
{
|
||||
|
@ -305,7 +401,11 @@ AutoUpdater::UpdateFile AutoUpdater::getUpdateFile(UpdateFileMeta fileMeta,
|
|||
return file;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
@brief Will try to download an update, if successful qTox will apply it after a restart
|
||||
@note Will try to follow qTox's proxy settings, may block and processEvents
|
||||
@result True if successful and qTox will apply it after a restart
|
||||
*/
|
||||
bool AutoUpdater::downloadUpdate()
|
||||
{
|
||||
// Updates only for supported platforms
|
||||
|
@ -433,6 +533,13 @@ fail:
|
|||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
@brief Checks if an update is downloaded and ready to be installed.
|
||||
@note If result is true, call installLocalUpdate,
|
||||
@return True if an update is downloaded, false if partially downloaded.
|
||||
|
||||
If an update was partially downloaded, the function will resume asynchronously and return false.
|
||||
*/
|
||||
bool AutoUpdater::isLocalUpdateReady()
|
||||
{
|
||||
// Updates only for supported platforms
|
||||
|
@ -472,6 +579,13 @@ bool AutoUpdater::isLocalUpdateReady()
|
|||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
@brief Launches the qTox updater to try to install the local update and exits immediately.
|
||||
|
||||
@note Will not check that the update actually exists, use isLocalUpdateReady first for that.
|
||||
The qTox updater will restart us after the update is done.
|
||||
If we fail to start the qTox updater, we will delete the update and exit.
|
||||
*/
|
||||
void AutoUpdater::installLocalUpdate()
|
||||
{
|
||||
qDebug() << "About to start the qTox updater to install a local update";
|
||||
|
@ -511,6 +625,14 @@ void AutoUpdater::installLocalUpdate()
|
|||
exit(0);
|
||||
}
|
||||
|
||||
/**
|
||||
@brief Checks update an show dialog asking to download it.
|
||||
@note Runs asynchronously in its own thread, and will return immediatly
|
||||
|
||||
Will call isUpdateAvailable, and as such may processEvents.
|
||||
Connects to the qTox update server, if an update is found
|
||||
shows a dialog to the user asking to download it.
|
||||
*/
|
||||
void AutoUpdater::checkUpdatesAsyncInteractive()
|
||||
{
|
||||
if (isDownloadingUpdate)
|
||||
|
@ -519,6 +641,11 @@ void AutoUpdater::checkUpdatesAsyncInteractive()
|
|||
QtConcurrent::run(&AutoUpdater::checkUpdatesAsyncInteractiveWorker);
|
||||
}
|
||||
|
||||
/**
|
||||
@brief Does the actual work for checkUpdatesAsyncInteractive
|
||||
|
||||
Blocking, but otherwise has the same properties than checkUpdatesAsyncInteractive
|
||||
*/
|
||||
void AutoUpdater::checkUpdatesAsyncInteractiveWorker()
|
||||
{
|
||||
if (!isUpdateAvailable())
|
||||
|
@ -557,18 +684,32 @@ void AutoUpdater::checkUpdatesAsyncInteractiveWorker()
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@brief Thread safe setter
|
||||
@param version Version to set.
|
||||
*/
|
||||
void AutoUpdater::setProgressVersion(QString version)
|
||||
{
|
||||
QMutexLocker lock(&progressVersionMutex);
|
||||
progressVersion = version;
|
||||
}
|
||||
|
||||
/**
|
||||
@brief Abort update process.
|
||||
|
||||
@note Aborting will make some functions try to return early.
|
||||
Call before qTox exits to avoid the updater running in the background.
|
||||
*/
|
||||
void AutoUpdater::abortUpdates()
|
||||
{
|
||||
abortFlag = true;
|
||||
isDownloadingUpdate = false;
|
||||
}
|
||||
|
||||
/**
|
||||
@brief Functions giving info on the progress of update downloads.
|
||||
@return Version as string.
|
||||
*/
|
||||
QString AutoUpdater::getProgressVersion()
|
||||
{
|
||||
QMutexLocker lock(&progressVersionMutex);
|
||||
|
|
|
@ -28,8 +28,6 @@
|
|||
#include <atomic>
|
||||
#include <functional>
|
||||
|
||||
/// For now we only support auto updates on Windows and OS X, although extending it is not a technical issue.
|
||||
/// Linux users are expected to use their package managers or update manually through official channels.
|
||||
#ifdef Q_OS_WIN
|
||||
#define AUTOUPDATE_ENABLED 1
|
||||
#elif defined(Q_OS_OSX)
|
||||
|
@ -38,17 +36,15 @@
|
|||
#define AUTOUPDATE_ENABLED 0
|
||||
#endif
|
||||
|
||||
/// Handles checking and applying updates for qTox
|
||||
/// Do *NOT* use auto update unless AUTOUPDATE_ENABLED is defined to 1
|
||||
class AutoUpdater
|
||||
{
|
||||
public:
|
||||
struct UpdateFileMeta
|
||||
{
|
||||
unsigned char sig[crypto_sign_BYTES]; ///< Signature of the file (ed25519)
|
||||
QString id; ///< Unique id of the file
|
||||
QString installpath; ///< Local path including the file name. May be relative to qtox-updater or absolute
|
||||
uint64_t size; ///< Size in bytes of the file
|
||||
unsigned char sig[crypto_sign_BYTES];
|
||||
QString id;
|
||||
QString installpath;
|
||||
uint64_t size;
|
||||
|
||||
bool operator==(const UpdateFileMeta& other)
|
||||
{
|
||||
|
@ -71,72 +67,41 @@ public:
|
|||
};
|
||||
|
||||
public:
|
||||
/// Connects to the qTox update server, if an updat is found shows a dialog to the user asking to download it
|
||||
/// Runs asynchronously in its own thread, and will return immediatly
|
||||
/// Will call isUpdateAvailable, and as such may processEvents
|
||||
static void checkUpdatesAsyncInteractive();
|
||||
/// Connects to the qTox update server, returns true if an update is available for download
|
||||
/// Will call getUpdateVersion, and as such may block and processEvents
|
||||
static bool isUpdateAvailable();
|
||||
/// Fetch the version info of the last update available from the qTox update server
|
||||
/// Will try to follow qTox's proxy settings, may block and processEvents
|
||||
static VersionInfo getUpdateVersion();
|
||||
/// Will try to download an update, if successful returns true and qTox will apply it after a restart
|
||||
/// Will try to follow qTox's proxy settings, may block and processEvents
|
||||
static bool downloadUpdate();
|
||||
/// Returns true if an update is downloaded and ready to be installed,
|
||||
/// if so, call installLocalUpdate.
|
||||
/// If an update was partially downloaded, the function will resume asynchronously and return false
|
||||
static bool isLocalUpdateReady();
|
||||
/// Launches the qTox updater to try to install the local update and exits immediately
|
||||
/// Will not check that the update actually exists, use isLocalUpdateReady first for that
|
||||
/// The qTox updater will restart us after the update is done
|
||||
/// Note: If we fail to start the qTox updater, we will delete the update and exit
|
||||
[[ noreturn ]] static void installLocalUpdate();
|
||||
/// Aborting will make some functions try to return early
|
||||
/// Call before qTox exits to avoid the updater running in the background
|
||||
static void abortUpdates();
|
||||
/// Functions giving info on the progress of update downloads
|
||||
static QString getProgressVersion();
|
||||
static int getProgressValue();
|
||||
|
||||
protected:
|
||||
/// Parses and validates a flist file. Returns an empty list on error
|
||||
static QList<UpdateFileMeta> parseFlist(QByteArray flistData);
|
||||
/// Gets the update server's flist. Returns an empty array on error
|
||||
/// Will try to follow qTox's proxy settings, may block and processEvents
|
||||
static QByteArray getUpdateFlist();
|
||||
/// Generates a list of files we need to update
|
||||
static QList<UpdateFileMeta> genUpdateDiff(QList<UpdateFileMeta> updateFlist);
|
||||
/// Checks if we have an up to date version of this file locally installed
|
||||
static bool isUpToDate(UpdateFileMeta file);
|
||||
/// Tries to fetch the file from the update server. Returns a file with a null QByteArray on error.
|
||||
/// Note that a file with an empty but non-null QByteArray is not an error, merely a file of size 0.
|
||||
/// Will try to follow qTox's proxy settings, may block and processEvents
|
||||
static UpdateFile getUpdateFile(UpdateFileMeta fileMeta, std::function<void(int,int)> progressCallback);
|
||||
/// Does the actual work for checkUpdatesAsyncInteractive
|
||||
/// Blocking, but otherwise has the same properties than checkUpdatesAsyncInteractive
|
||||
static void checkUpdatesAsyncInteractiveWorker();
|
||||
/// Thread safe setter
|
||||
static void setProgressVersion(QString version);
|
||||
|
||||
private:
|
||||
AutoUpdater() = delete;
|
||||
|
||||
private:
|
||||
// Constants
|
||||
static const QString updateServer; ///< Hostname of the qTox update server
|
||||
static const QString platform; ///< Name of platform we're trying to get updates for
|
||||
static const QString checkURI; ///< URI of the file containing the latest version string
|
||||
static const QString flistURI; ///< URI of the file containing info on each file (hash, signature, size, name, ..)
|
||||
static const QString filesURI; ///< URI of the actual files of the latest version
|
||||
static const QString updaterBin; ///< Path to the qtox-updater binary
|
||||
static const QString updateServer;
|
||||
static const QString platform;
|
||||
static const QString checkURI;
|
||||
static const QString flistURI;
|
||||
static const QString filesURI;
|
||||
static const QString updaterBin;
|
||||
static unsigned char key[];
|
||||
static std::atomic_bool abortFlag; ///< If true, try to abort everything.
|
||||
static std::atomic_bool isDownloadingUpdate; ///< We'll pretend there's no new update available if we're already updating
|
||||
static std::atomic_bool abortFlag;
|
||||
static std::atomic_bool isDownloadingUpdate;
|
||||
static std::atomic<float> progressValue;
|
||||
static QString progressVersion;
|
||||
static QMutex progressVersionMutex; ///< No, we can't just make the QString atomic
|
||||
static QMutex progressVersionMutex;
|
||||
};
|
||||
|
||||
#endif // AUTOUPDATE_H
|
||||
|
|
|
@ -23,6 +23,14 @@
|
|||
#include <QObject>
|
||||
#include <QDebug>
|
||||
|
||||
/**
|
||||
@class AvatarBroadcaster
|
||||
|
||||
Takes care of broadcasting avatar changes to our friends in a smart way
|
||||
Cache a copy of our current avatar and friends who have received it
|
||||
so we don't spam avatar transfers to a friend who already has it.
|
||||
*/
|
||||
|
||||
QByteArray AvatarBroadcaster::avatarData;
|
||||
QMap<uint32_t, bool> AvatarBroadcaster::friendsSentTo;
|
||||
|
||||
|
@ -32,10 +40,15 @@ static auto autoBroadcast = [](uint32_t friendId, Status)
|
|||
AvatarBroadcaster::sendAvatarTo(friendId);
|
||||
};
|
||||
|
||||
/**
|
||||
@brief Set our current avatar.
|
||||
@param data Byte array on avater.
|
||||
*/
|
||||
void AvatarBroadcaster::setAvatar(QByteArray data)
|
||||
{
|
||||
if (avatarData == data)
|
||||
return;
|
||||
|
||||
avatarData = data;
|
||||
friendsSentTo.clear();
|
||||
|
||||
|
@ -44,6 +57,10 @@ void AvatarBroadcaster::setAvatar(QByteArray data)
|
|||
sendAvatarTo(friendId);
|
||||
}
|
||||
|
||||
/**
|
||||
@brief Send our current avatar to this friend, if not already sent
|
||||
@param friendId Id of friend to send avatar.
|
||||
*/
|
||||
void AvatarBroadcaster::sendAvatarTo(uint32_t friendId)
|
||||
{
|
||||
if (friendsSentTo.contains(friendId) && friendsSentTo[friendId])
|
||||
|
@ -54,6 +71,10 @@ void AvatarBroadcaster::sendAvatarTo(uint32_t friendId)
|
|||
friendsSentTo[friendId] = true;
|
||||
}
|
||||
|
||||
/**
|
||||
@brief Setup auto broadcast sending avatar.
|
||||
@param state If true, we automatically broadcast our avatar to friends when they come online.
|
||||
*/
|
||||
void AvatarBroadcaster::enableAutoBroadcast(bool state)
|
||||
{
|
||||
QObject::disconnect(autoBroadcastConn);
|
||||
|
|
|
@ -24,20 +24,14 @@
|
|||
#include <QByteArray>
|
||||
#include <QMap>
|
||||
|
||||
/// Takes care of broadcasting avatar changes to our friends in a smart way
|
||||
/// Cache a copy of our current avatar and friends who have received it
|
||||
/// so we don't spam avatar transfers to a friend who already has it.
|
||||
class AvatarBroadcaster
|
||||
{
|
||||
private:
|
||||
AvatarBroadcaster()=delete;
|
||||
|
||||
public:
|
||||
/// Set our current avatar
|
||||
static void setAvatar(QByteArray data);
|
||||
/// Send our current avatar to this friend, if not already sent
|
||||
static void sendAvatarTo(uint32_t friendId);
|
||||
/// If true, we automatically broadcast our avatar to friends when they come online
|
||||
static void enableAutoBroadcast(bool state = true);
|
||||
|
||||
private:
|
||||
|
|
|
@ -29,6 +29,22 @@
|
|||
|
||||
#define TOX_HEX_ID_LENGTH 2*TOX_ADDRESS_SIZE
|
||||
|
||||
/**
|
||||
@class ToxDNS
|
||||
@brief Handles tox1 and tox3 DNS queries.
|
||||
*/
|
||||
|
||||
/**
|
||||
@struct tox3_server
|
||||
@brief Represents a tox3 server.
|
||||
|
||||
@var const char* tox3_server::name
|
||||
@brief Hostname of the server, e.g. toxme.se.
|
||||
|
||||
@var uint8_t* tox3_server::pubkey
|
||||
@brief Public key of the tox3 server, usually 256bit long.
|
||||
*/
|
||||
|
||||
const ToxDNS::tox3_server ToxDNS::pinnedServers[]
|
||||
{
|
||||
{"toxme.se", (uint8_t[32]){0x5D, 0x72, 0xC5, 0x17, 0xDF, 0x6A, 0xEC, 0x54, 0xF1, 0xE9, 0x77, 0xA6, 0xB6, 0xF2, 0x59, 0x14,
|
||||
|
@ -50,10 +66,14 @@ void ToxDNS::showWarning(const QString &message)
|
|||
warning.exec();
|
||||
}
|
||||
|
||||
/**
|
||||
@brief Try to fetch the first entry of the given TXT record.
|
||||
@param record Record to search.
|
||||
@param silent May display message boxes on error if silent is false.
|
||||
@return An empty object on failure. May block for up to ~3s.
|
||||
*/
|
||||
QByteArray ToxDNS::fetchLastTextRecord(const QString& record, bool silent)
|
||||
{
|
||||
QByteArray result;
|
||||
|
||||
QDnsLookup dns;
|
||||
dns.setType(QDnsLookup::TXT);
|
||||
dns.setName(record);
|
||||
|
@ -71,7 +91,7 @@ QByteArray ToxDNS::fetchLastTextRecord(const QString& record, bool silent)
|
|||
if (!silent)
|
||||
showWarning(tr("The connection timed out","The DNS gives the Tox ID associated to toxme.se addresses"));
|
||||
|
||||
return result;
|
||||
return QByteArray();
|
||||
}
|
||||
|
||||
if (dns.error() == QDnsLookup::NotFoundError)
|
||||
|
@ -79,14 +99,14 @@ QByteArray ToxDNS::fetchLastTextRecord(const QString& record, bool silent)
|
|||
if (!silent)
|
||||
showWarning(tr("This address does not exist","The DNS gives the Tox ID associated to toxme.se addresses"));
|
||||
|
||||
return result;
|
||||
return QByteArray();
|
||||
}
|
||||
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;
|
||||
return QByteArray();
|
||||
}
|
||||
|
||||
const QList<QDnsTextRecord> textRecords = dns.textRecords();
|
||||
|
@ -95,7 +115,7 @@ QByteArray ToxDNS::fetchLastTextRecord(const QString& record, bool silent)
|
|||
if (!silent)
|
||||
showWarning(tr("No text record found", "Error with the DNS"));
|
||||
|
||||
return result;
|
||||
return QByteArray();
|
||||
}
|
||||
|
||||
const QList<QByteArray> textRecordValues = textRecords.last().values();
|
||||
|
@ -104,13 +124,20 @@ QByteArray ToxDNS::fetchLastTextRecord(const QString& record, bool silent)
|
|||
if (!silent)
|
||||
showWarning(tr("Unexpected number of values in text record", "Error with the DNS"));
|
||||
|
||||
return result;
|
||||
return QByteArray();
|
||||
}
|
||||
|
||||
result = textRecordValues.first();
|
||||
return result;
|
||||
return textRecordValues.first();
|
||||
}
|
||||
|
||||
/**
|
||||
@brief Send query to DNS to find Tox Id.
|
||||
@note Will *NOT* fallback on queryTox1 anymore.
|
||||
@param server Server to sending query.
|
||||
@param record Should look like user@domain.tld.
|
||||
@param silent If true, there will be no output on error.
|
||||
@return Tox Id string.
|
||||
*/
|
||||
QString ToxDNS::queryTox3(const tox3_server& server, const QString &record, bool silent)
|
||||
{
|
||||
QByteArray nameData = record.left(record.indexOf('@')).toUtf8(), id, realRecord;
|
||||
|
@ -190,44 +217,40 @@ fallbackOnTox1:
|
|||
return toxIdStr;
|
||||
}
|
||||
|
||||
/**
|
||||
@brief Tries to map a text string to a ToxId struct, will query Tox DNS records if necessary.
|
||||
@param address Adress to search for Tox ID.
|
||||
@param silent If true, there will be no output on error.
|
||||
@return Found Tox Id.
|
||||
*/
|
||||
ToxId ToxDNS::resolveToxAddress(const QString &address, bool silent)
|
||||
{
|
||||
ToxId toxId;
|
||||
|
||||
if (address.isEmpty())
|
||||
{
|
||||
return toxId;
|
||||
}
|
||||
else if (ToxId::isToxId(address))
|
||||
{
|
||||
toxId = ToxId(address);
|
||||
return toxId;
|
||||
}
|
||||
else
|
||||
{
|
||||
// If we're querying one of our pinned server, do a toxdns3 request directly
|
||||
QString servname = address.mid(address.indexOf('@')+1);
|
||||
for (const ToxDNS::tox3_server& pin : ToxDNS::pinnedServers)
|
||||
{
|
||||
if (servname == pin.name)
|
||||
{
|
||||
toxId = ToxId(queryTox3(pin, address, silent));
|
||||
return toxId;
|
||||
}
|
||||
}
|
||||
return ToxId();
|
||||
|
||||
// Otherwise try toxdns3 if we can get a pubkey or fallback to toxdns1
|
||||
QByteArray pubkey = fetchLastTextRecord("_tox."+servname, true);
|
||||
if (!pubkey.isEmpty())
|
||||
{
|
||||
pubkey = QByteArray::fromHex(pubkey);
|
||||
if (ToxId::isToxId(address))
|
||||
return ToxId(address);
|
||||
|
||||
QByteArray servnameData = servname.toUtf8();
|
||||
ToxDNS::tox3_server server;
|
||||
server.name = servnameData.data();
|
||||
server.pubkey = (uint8_t*)pubkey.data();
|
||||
toxId = ToxId(queryTox3(server, address, silent));
|
||||
}
|
||||
return toxId;
|
||||
// If we're querying one of our pinned servers, do a toxdns3 request directly
|
||||
QString servname = address.mid(address.indexOf('@')+1);
|
||||
for (const ToxDNS::tox3_server& pin : ToxDNS::pinnedServers)
|
||||
{
|
||||
if (servname == pin.name)
|
||||
return ToxId(queryTox3(pin, address, silent));
|
||||
}
|
||||
|
||||
// Otherwise try toxdns3 if we can get a pubkey or fallback to toxdns1
|
||||
QByteArray pubkey = fetchLastTextRecord("_tox."+servname, true);
|
||||
if (!pubkey.isEmpty())
|
||||
{
|
||||
pubkey = QByteArray::fromHex(pubkey);
|
||||
|
||||
QByteArray servnameData = servname.toUtf8();
|
||||
ToxDNS::tox3_server server;
|
||||
server.name = servnameData.data();
|
||||
server.pubkey = (uint8_t*)pubkey.data();
|
||||
return ToxId(queryTox3(server, address, silent));
|
||||
}
|
||||
|
||||
return ToxId();
|
||||
}
|
||||
|
|
|
@ -26,34 +26,29 @@
|
|||
#include <QDnsLookup>
|
||||
#include <QObject>
|
||||
|
||||
/// Handles tox1 and tox3 DNS queries
|
||||
class ToxDNS : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
struct tox3_server ///< Represents a tox3 server
|
||||
struct tox3_server
|
||||
{
|
||||
tox3_server()=default;
|
||||
tox3_server(const char* _name, uint8_t _pk[32]):name{_name},pubkey{_pk}{}
|
||||
|
||||
const char* name; ///< Hostname of the server, e.g. toxme.se
|
||||
uint8_t* pubkey; ///< Public key of the tox3 server, usually 256bit long
|
||||
const char* name;
|
||||
uint8_t* pubkey;
|
||||
};
|
||||
|
||||
public:
|
||||
/// Tries to map a text string to a ToxId struct, will query Tox DNS records if necessary
|
||||
static ToxId resolveToxAddress(const QString& address, bool silent=true);
|
||||
static QString queryTox3(const tox3_server& server, const QString& record, bool silent=true); ///< Record should look like user@domain.tld, will *NOT* fallback on queryTox1 anymore
|
||||
static QString queryTox3(const tox3_server& server, const QString& record, bool silent=true);
|
||||
|
||||
protected:
|
||||
static void showWarning(const QString& message);
|
||||
ToxDNS()=default;
|
||||
|
||||
private:
|
||||
/// Try to fetch the first entry of the given TXT record
|
||||
/// Returns an empty object on failure. May block for up to ~3s
|
||||
/// May display message boxes on error if silent if false
|
||||
static QByteArray fetchLastTextRecord(const QString& record, bool silent=true);
|
||||
|
||||
public:
|
||||
|
|
|
@ -30,6 +30,14 @@
|
|||
#include <string>
|
||||
#include <ctime>
|
||||
|
||||
/**
|
||||
@class Toxme
|
||||
@brief This class implements a client for the toxme.se API
|
||||
|
||||
@note The class is thread safe
|
||||
@note May process events while waiting for blocking calls
|
||||
*/
|
||||
|
||||
QByteArray Toxme::makeJsonRequest(QString url, QString json, QNetworkReply::NetworkError &error)
|
||||
{
|
||||
if (error)
|
||||
|
@ -136,6 +144,11 @@ QByteArray Toxme::prepareEncryptedJson(QString url, int action, QString payload)
|
|||
return json.toUtf8();
|
||||
}
|
||||
|
||||
/**
|
||||
@brief Converts a toxme address to a Tox ID.
|
||||
@param address Toxme address.
|
||||
@return Found ToxId (an empty ID on error).
|
||||
*/
|
||||
ToxId Toxme::lookup(QString address)
|
||||
{
|
||||
// JSON injection ?
|
||||
|
@ -204,6 +217,16 @@ Toxme::ExecCode Toxme::extractError(QString json)
|
|||
return ExecCode(r);
|
||||
}
|
||||
|
||||
/**
|
||||
@brief Creates a new toxme address associated with a Tox ID.
|
||||
@param[out] code Tox error code @see getErrorMessage.
|
||||
@param[in] server Create toxme account on this server.
|
||||
@param[in] id ToxId of current user.
|
||||
@param[in] address Create toxme account with this adress.
|
||||
@param[in] keepPrivate If true, the address will not be published on toxme site.
|
||||
@param[in] bio A short optional description of yourself if you want to publish your address.
|
||||
@return password on success, else sets code parameter and returns an empty QString.
|
||||
*/
|
||||
QString Toxme::createAddress(ExecCode &code, QString server, ToxId id, QString address,
|
||||
bool keepPrivate, QString bio)
|
||||
{
|
||||
|
@ -271,6 +294,12 @@ QString Toxme::getPass(QString json, ExecCode &code) {
|
|||
return json;
|
||||
}
|
||||
|
||||
/**
|
||||
@brief Deletes the address associated with your current Tox ID.
|
||||
@param server Server to delete the address from.
|
||||
@param id ToxId to delete.
|
||||
@return Status code returned from server.
|
||||
*/
|
||||
Toxme::ExecCode Toxme::deleteAddress(QString server, ToxId id)
|
||||
{
|
||||
const QString payload{"{\"public_key\":\""+id.toString().left(64)+"\","
|
||||
|
@ -289,10 +318,10 @@ Toxme::ExecCode Toxme::deleteAddress(QString server, ToxId id)
|
|||
}
|
||||
|
||||
/**
|
||||
* @brief Return string of the corresponding error code
|
||||
* @param errorCode Code to get error message
|
||||
* @return Source error message
|
||||
*/
|
||||
@brief Return string of the corresponding error code
|
||||
@param errorCode Code to get error message
|
||||
@return Source error message
|
||||
*/
|
||||
QString Toxme::getErrorMessage(int errorCode)
|
||||
{
|
||||
switch (errorCode) {
|
||||
|
@ -336,10 +365,10 @@ QString Toxme::getErrorMessage(int errorCode)
|
|||
}
|
||||
|
||||
/**
|
||||
* @brief Return translated error message
|
||||
* @param errorCode Code to translate
|
||||
* @return Translated Toxme error message
|
||||
*/
|
||||
@brief Return translated error message
|
||||
@param errorCode Code to translate
|
||||
@return Translated Toxme error message
|
||||
*/
|
||||
QString Toxme::translateErrorMessage(int errorCode)
|
||||
{
|
||||
switch (errorCode) {
|
||||
|
|
|
@ -30,9 +30,6 @@
|
|||
|
||||
class QNetworkAccessManager;
|
||||
|
||||
/// This class implements a client for the toxme.se API
|
||||
/// The class is thread safe
|
||||
/// May process events while waiting for blocking calls
|
||||
class Toxme
|
||||
{
|
||||
public:
|
||||
|
@ -45,22 +42,15 @@ public:
|
|||
NoPassword = 4
|
||||
};
|
||||
|
||||
/// Converts a toxme.se address to a Tox ID, returns an empty ID on error
|
||||
static ToxId lookup(QString address);
|
||||
/// Creates a new toxme.se address associated with a Tox ID.
|
||||
/// If keepPrivate, the address will not be published on toxme.se
|
||||
/// The bio is a short optional description of yourself if you want to publish your address.
|
||||
/// If it passed without error, return password, else return errorCode in QString
|
||||
static QString createAddress(ExecCode &code, QString server, ToxId id, QString address,
|
||||
bool keepPrivate=true, QString bio=QString());
|
||||
/// Deletes the address associated with your current Tox ID
|
||||
static ExecCode deleteAddress(QString server, ToxId id);
|
||||
/// Return string of the corresponding error code
|
||||
static QString getErrorMessage(int errorCode);
|
||||
static QString translateErrorMessage(int errorCode);
|
||||
|
||||
private:
|
||||
Toxme()=delete;
|
||||
Toxme() = delete;
|
||||
static QByteArray makeJsonRequest(QString url, QString json, QNetworkReply::NetworkError &error);
|
||||
static QByteArray prepareEncryptedJson(QString url, int action, QString payload);
|
||||
static QByteArray getServerPubkey(QString url, QNetworkReply::NetworkError &error);
|
||||
|
|
|
@ -44,6 +44,12 @@ bool toxURIEventHandler(const QByteArray& eventData)
|
|||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
@brief Shows a dialog asking whether or not to add this tox address as a friend.
|
||||
@note Will wait until the core is ready first.
|
||||
@param toxURI Tox URI to try to add.
|
||||
@return True, if tox URI is correct, false otherwise.
|
||||
*/
|
||||
bool handleToxURI(const QString &toxURI)
|
||||
{
|
||||
Core* core = Core::getInstance();
|
||||
|
@ -75,6 +81,7 @@ bool handleToxURI(const QString &toxURI)
|
|||
.arg(Nexus::getCore()->getUsername()));
|
||||
if (dialog.exec() == QDialog::Accepted)
|
||||
Core::getInstance()->requestFriendship(toxId, dialog.getRequestMessage());
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
|
@ -23,8 +23,6 @@
|
|||
|
||||
#include <QDialog>
|
||||
|
||||
/// Shows a dialog asking whether or not to add this tox address as a friend
|
||||
/// Will wait until the core is ready first
|
||||
bool handleToxURI(const QString& toxURI);
|
||||
|
||||
// Internals
|
||||
|
|
Loading…
Reference in New Issue
Block a user