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

bool CToxProto::InitToxCore()
{
	debugLogA("CToxProto::InitToxCore: initializing tox core");

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

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

		if (nlus.useProxy)
		{
			if (nlus.proxyType == PROXYTYPE_HTTP || nlus.proxyType == PROXYTYPE_HTTPS)
			{
				debugLogA("CToxProto::InitToxCore: setting http user proxy config");
				options.proxy_type = TOX_PROXY_HTTP;
				strcpy(&options.proxy_address[0], nlus.szProxyServer);
				options.proxy_port = nlus.wProxyPort;
			}

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

	debugLogA("CToxProto::InitToxCore: loading tox profile");

	tox = tox_new(&options);
	password = mir_utf8encodeW(ptrT(getTStringA("Password")));
	bool isProfileLoaded = LoadToxProfile();
	if (isProfileLoaded)
	{
		debugLogA("CToxProto::InitToxCore: tox profile load successfully");

		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);

		uint8_t data[TOX_FRIEND_ADDRESS_SIZE];
		tox_get_address(tox, data);
		ToxHexAddress address(data, TOX_FRIEND_ADDRESS_SIZE);
		setString(TOX_SETTINGS_ID, address);

		int size = tox_get_self_name_size(tox);
		std::string nick(size, 0);
		tox_get_self_name(tox, (uint8_t*)nick.data());
		setWString("Nick", ptrW(Utf8DecodeW(nick.c_str())));

		//temporary
		size = tox_get_self_status_message_size(tox);
		std::string statusmes(size, 0);
		tox_get_self_status_message(tox, (uint8_t*)statusmes.data(), size);
		setWString("StatusMsg", ptrW(Utf8DecodeW(statusmes.c_str())));

		std::tstring avatarPath = GetAvatarFilePath();
		if (IsFileExists(avatarPath))
		{
			SetToxAvatar(avatarPath);
		}
	}
	else
	{
		debugLogA("CToxProto::InitToxCore: failed to load tox profile");

		if (password != NULL)
		{
			mir_free(password);
			password = NULL;
		}
		tox_kill(tox);
	}

	return isProfileLoaded;
}

void CToxProto::UninitToxCore()
{
	for (size_t i = 0; i < transfers->Count(); i++)
	{
		FileTransferParam *transfer = transfers->GetAt(i);
		transfer->status = CANCELED;
		tox_file_send_control(tox, transfer->friendNumber, transfer->GetDirection(), transfer->fileNumber, TOX_FILECONTROL_KILL, NULL, 0);
		ProtoBroadcastAck(transfer->pfts.hContact, ACKTYPE_FILE, ACKRESULT_DENIED, (HANDLE)transfer, 0);
		transfers->Remove(transfer);
	}

	ptrA nickname(mir_utf8encodeW(ptrT(getTStringA("Nick"))));
	tox_set_name(tox, (uint8_t*)(char*)nickname, mir_strlen(nickname));

	//temporary
	ptrA statusmes(mir_utf8encodeW(ptrT(getTStringA("StatusMsg"))));
	tox_set_status_message(tox, (uint8_t*)(char*)statusmes, mir_strlen(statusmes));

	SaveToxProfile();
	if (password != NULL)
	{
		mir_free(password);
		password = NULL;
	}
	tox_kill(tox);
}