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

int CToxProto::MapStatus(int status)
{
	switch (status) {
	case ID_STATUS_FREECHAT:
	case ID_STATUS_ONTHEPHONE:
		status = ID_STATUS_ONLINE;
		break;

	case ID_STATUS_NA:
	case ID_STATUS_OUTTOLUNCH:
		status = ID_STATUS_AWAY;
		break;

	case ID_STATUS_DND:
	case ID_STATUS_INVISIBLE:
		status = ID_STATUS_OCCUPIED;
		break;
	}
	return status;
}

TOX_USER_STATUS CToxProto::MirandaToToxStatus(int status)
{
	TOX_USER_STATUS userstatus = TOX_USER_STATUS_NONE;
	switch (status) {
	case ID_STATUS_AWAY:
		userstatus = TOX_USER_STATUS_AWAY;
		break;
	case ID_STATUS_OCCUPIED:
		userstatus = TOX_USER_STATUS_BUSY;
		break;
	}
	return userstatus;
}

int CToxProto::ToxToMirandaStatus(TOX_USER_STATUS userstatus)
{
	int status = ID_STATUS_OFFLINE;
	switch (userstatus) {
	case TOX_USER_STATUS_NONE:
		status = ID_STATUS_ONLINE;
		break;
	case TOX_USER_STATUS_AWAY:
		status = ID_STATUS_AWAY;
		break;
	case TOX_USER_STATUS_BUSY:
		status = ID_STATUS_OCCUPIED;
		break;
	}
	return status;
}

wchar_t* CToxProto::ToxErrorToString(TOX_ERR_NEW error)
{
	switch (error) {
	case TOX_ERR_NEW_NULL:
		return TranslateT("One of the arguments is missing");
	case TOX_ERR_NEW_MALLOC:
		return TranslateT("Unable to allocate enough memory");
	case TOX_ERR_NEW_PORT_ALLOC:
		return TranslateT("Unable to bind to a port");
	case TOX_ERR_NEW_PROXY_BAD_TYPE:
		return TranslateT("The proxy type is not valid");
	case TOX_ERR_NEW_PROXY_BAD_HOST:
		return TranslateT("The proxy host is not valid");
	case TOX_ERR_NEW_PROXY_BAD_PORT:
		return TranslateT("The proxy port is not valid");
	case TOX_ERR_NEW_PROXY_NOT_FOUND:
		return TranslateT("The proxy address could not be resolved");
	case TOX_ERR_NEW_LOAD_ENCRYPTED:
		return TranslateT("The profile is encrypted");
	case TOX_ERR_NEW_LOAD_BAD_FORMAT:
		return TranslateT("The data format is not valid");
	default:
		return TranslateT("Unknown error");
	}
}

wchar_t* CToxProto::ToxErrorToString(TOX_ERR_FRIEND_SEND_MESSAGE error)
{
	switch (error) {
	case TOX_ERR_FRIEND_SEND_MESSAGE_NULL:
		return TranslateT("One of the arguments is missing");
	case TOX_ERR_FRIEND_SEND_MESSAGE_FRIEND_NOT_FOUND:
		return TranslateT("The friend number did not designate a valid friend");
	case TOX_ERR_FRIEND_SEND_MESSAGE_FRIEND_NOT_CONNECTED:
		return TranslateT("This client is currently not connected to the friend");
	case TOX_ERR_FRIEND_SEND_MESSAGE_SENDQ:
		return TranslateT("An allocation error occurred while increasing the send queue size");
	case TOX_ERR_FRIEND_SEND_MESSAGE_TOO_LONG:
		return TranslateT("Message length exceeded TOX_MAX_MESSAGE_LENGTH");
	case TOX_ERR_FRIEND_SEND_MESSAGE_EMPTY:
		return TranslateT("Attempted to send a zero-length message");
	default:
		return TranslateT("Unknown error");
	}
}

void CToxProto::ShowNotification(const wchar_t *caption, const wchar_t *message, int flags, MCONTACT hContact)
{
	if (Miranda_IsTerminated()) {
		return;
	}

	if (ServiceExists(MS_POPUP_ADDPOPUPT) && db_get_b(NULL, "Popup", "ModuleIsEnabled", 1)) {
		POPUPDATAT ppd = { 0 };
		ppd.lchContact = hContact;
		wcsncpy(ppd.lpwzContactName, caption, MAX_CONTACTNAME);
		wcsncpy(ppd.lpwzText, message, MAX_SECONDLINE);
		ppd.lchIcon = IcoLib_GetIcon("Tox_main");

		if (!PUAddPopupT(&ppd))
			return;
	}

	MessageBox(NULL, message, caption, MB_OK | flags);
}

void CToxProto::ShowNotification(const wchar_t *message, int flags, MCONTACT hContact)
{
	ShowNotification(_A2W(MODULE), message, flags, hContact);
}

bool CToxProto::IsFileExists(const wchar_t* path)
{
	return _waccess(path, 0) == 0;
}

MEVENT CToxProto::AddEventToDb(MCONTACT hContact, WORD type, DWORD timestamp, DWORD flags, PBYTE pBlob, size_t cbBlob)
{
	DBEVENTINFO dbei = {};
	dbei.szModule = this->m_szModuleName;
	dbei.timestamp = timestamp;
	dbei.eventType = type;
	dbei.cbBlob = (DWORD)cbBlob;
	dbei.pBlob = pBlob;
	dbei.flags = flags;
	return db_event_add(hContact, &dbei);
}

INT_PTR CToxProto::ParseToxUri(WPARAM, LPARAM lParam)
{
	wchar_t *uri = (wchar_t*)lParam;
	if (mir_wstrlen(uri) <= 4)
		return 1;

	if (Accounts.getCount() == 0)
		return 1;

	CToxProto *proto = NULL;
	for (int i = 0; i < Accounts.getCount(); i++) {
		if (Accounts[i]->IsOnline()) {
			proto = Accounts[i];
			break;
		}
	}
	if (proto == NULL)
		return 1;

	if (wcschr(uri, '@') != NULL)
		return 1;

	PROTOSEARCHRESULT psr = { sizeof(psr) };
	psr.flags = PSR_UTF8;
	psr.id.a = mir_u2a(&uri[4]);

	ADDCONTACTSTRUCT acs = { HANDLE_SEARCHRESULT };
	acs.szProto = proto->m_szModuleName;
	acs.psr = &psr;

	CallService(MS_ADDCONTACT_SHOW, 0, (LPARAM)&acs);
	return 0;
}