summaryrefslogtreecommitdiff
path: root/protocols/Tox/src/tox_account.cpp
blob: a733d5f480eb1ea21f4821f6db853ef49e3a85a1 (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
#include "common.h"
#include "tox_bootstrap.h"

bool CToxProto::IsOnline()
{
	return isConnected && m_iStatus > ID_STATUS_OFFLINE;
}

int CToxProto::OnAccountLoaded(WPARAM, LPARAM)
{
	HookProtoEvent(ME_OPT_INITIALISE, &CToxProto::OnOptionsInit);
	HookProtoEvent(ME_USERINFO_INITIALISE, &CToxProto::OnUserInfoInit);
	HookProtoEvent(ME_PROTO_ACCLISTCHANGED, &CToxProto::OnAccountListChanged);
	HookProtoEvent(ME_DB_CONTACT_SETTINGCHANGED, &CToxProto::OnSettingsChanged);
	HookProtoEvent(ME_MSG_PRECREATEEVENT, &CToxProto::OnPreCreateMessage);	

	InitNetlib();

	return 0;
}

void CToxProto::InitToxCore()
{
	std::tstring profilePath = GetToxProfilePath();
	hProfile = CreateFile(
		profilePath.c_str(),
		GENERIC_READ | GENERIC_WRITE,
		FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
		NULL,
		OPEN_ALWAYS,
		FILE_ATTRIBUTE_NORMAL,
		NULL);

	Tox_Options options = { 0 };
	options.udp_disabled = getByte("DisableUDP", 0);
	options.ipv6enabled = !getByte("DisableIPv6", 0);

	if (hNetlib)
	{
		NETLIBUSERSETTINGS nlus = { sizeof(NETLIBUSERSETTINGS) };
		CallService(MS_NETLIB_GETUSERSETTINGS, (WPARAM)hNetlib, (LPARAM)&nlus);

		if (nlus.useProxy)
		{
			if (nlus.proxyType == PROXYTYPE_SOCKS4 || nlus.proxyType == PROXYTYPE_SOCKS5)
			{
				debugLogA("CToxProto::InitToxCore: setting socks user proxy config");
				options.proxy_enabled = 1;
				strcpy(&options.proxy_address[0], nlus.szProxyServer);
				options.proxy_port = nlus.wProxyPort;
			}
		}
	}

	tox = tox_new(&options);
	tox_callback_friend_request(tox, OnFriendRequest, this);
	tox_callback_friend_message(tox, OnFriendMessage, this);
	tox_callback_friend_action(tox, OnFriendAction, this);
	tox_callback_typing_change(tox, OnTypingChanged, this);
	tox_callback_name_change(tox, OnFriendNameChange, this);
	tox_callback_status_message(tox, OnStatusMessageChanged, this);
	tox_callback_user_status(tox, OnUserStatusChanged, this);
	tox_callback_read_receipt(tox, OnReadReceipt, this);
	tox_callback_connection_status(tox, OnConnectionStatusChanged, this);
	// file transfers
	tox_callback_file_control(tox, OnFileRequest, this);
	tox_callback_file_send_request(tox, OnFriendFile, this);
	tox_callback_file_data(tox, OnFileData, this);
	// avatars
	tox_callback_avatar_info(tox, OnGotFriendAvatarInfo, this);
	tox_callback_avatar_data(tox, OnGotFriendAvatarData, this);

	LoadToxProfile();

	int size = tox_get_self_name_size(tox);
	std::vector<uint8_t> username(size);
	tox_get_self_name(tox, &username[0]);
	std::string nick(username.begin(), username.end());
	setWString("Nick", ptrW(Utf8DecodeW(nick.c_str())));

	std::vector<uint8_t> pubKey(TOX_FRIEND_ADDRESS_SIZE);
	tox_get_address(tox, &pubKey[0]);
	std::string address = DataToHexString(pubKey);
	setString(NULL, TOX_SETTINGS_ID, address.c_str());

	std::tstring avatarPath = GetAvatarFilePath();
	if (IsFileExists(avatarPath))
	{
		SetToxAvatar(avatarPath);
	}
}

void CToxProto::UninitToxCore()
{
	SaveToxProfile();
	tox_kill(tox);
	CloseHandle(hProfile);
}

void CToxProto::DoBootstrap()
{
	static int j = 0;
	int i = 0;
	while (i < 2)
	{
		struct bootstrap_node *d = &bootstrap_nodes[j % SIZEOF(bootstrap_nodes)];
		tox_bootstrap_from_address(tox, d->address, d->port, d->key);
		i++; j++;
	}
}

void CToxProto::DoTox()
{
	mir_cslock lck(toxLock);

	tox_do(tox);
	uint32_t interval = tox_do_interval(tox);
	Sleep(interval);
}

void CToxProto::PollingThread(void*)
{
	debugLogA("CToxProto::PollingThread: entering");

	isConnected = false;
	DoBootstrap();

	while (!isTerminated)
	{
		DoTox();

		if (!isConnected)
		{
			if (tox_isconnected(tox))
			{
				isConnected = true;
				debugLogA("CToxProto::PollingThread: successfuly connected to DHT");

				LoadFriendList();

				debugLogA("CToxProto::PollingThread: changing status from %i to %i", ID_STATUS_CONNECTING, m_iDesiredStatus);
				m_iStatus = m_iDesiredStatus;
				ProtoBroadcastAck(NULL, ACKTYPE_STATUS, ACKRESULT_SUCCESS, (HANDLE)ID_STATUS_CONNECTING, m_iStatus);
			}
			else
			{
				DoBootstrap();
			}
		}
		/*else
		{
			if (!tox_isconnected(tox))
			{
				debugLogA("CToxProto::PollingThread: disconnected from DHT");
				SetStatus(ID_STATUS_OFFLINE);
			}
		}*/
	}

	isConnected = false;

	debugLogA("CToxProto::PollingThread: leaving");
}