summaryrefslogtreecommitdiff
path: root/protocols/Telegram/src/proto.cpp
blob: 84944a6b4946b018ed598f2c56b39e1a03b3be59 (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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#include "stdafx.h"

static int CompareRequests(const TG_REQUEST_BASE *p1, const TG_REQUEST_BASE *p2)
{
	if (p1->requestId == p2->requestId)
		return 0;

	return (p1->requestId < p2->requestId) ? -1 : 1;
}

static int CompareUsers(const TG_USER *p1, const TG_USER *p2)
{
	if (p1->id == p2->id)
		return 0;

	return (p1->id < p2->id) ? -1 : 1;
}

CTelegramProto::CTelegramProto(const char* protoName, const wchar_t* userName) :
	PROTO<CTelegramProto>(protoName, userName),
	m_impl(*this),
	m_arUsers(10, CompareUsers),
	m_arRequests(10, CompareRequests),
	m_szOwnPhone(this, "Phone"), 
	m_wszDeviceName(this, "DeviceName", L"Miranda NG"),
	m_wszDefaultGroup(this, "DefaultGroup", L"Telegram"),
	m_bUsePopups(this, "UsePopups", true),
	m_bHideGroupchats(this, "HideChats", true)
{
	m_iOwnId = _atoi64(getMStringA(DBKEY_ID));

	CreateProtoService(PS_CREATEACCMGRUI, &CTelegramProto::SvcCreateAccMgrUI);
	CreateProtoService(PS_GETAVATARCAPS, &CTelegramProto::SvcGetAvatarCaps);
	CreateProtoService(PS_GETAVATARINFO, &CTelegramProto::SvcGetAvatarInfo);
	CreateProtoService(PS_GETMYAVATAR, &CTelegramProto::SvcGetMyAvatar);
	CreateProtoService(PS_SETMYAVATAR, &CTelegramProto::SvcSetMyAvatar);

	HookProtoEvent(ME_OPT_INITIALISE, &CTelegramProto::OnOptionsInit);
	HookProtoEvent(ME_DB_EVENT_MARKED_READ, &CTelegramProto::OnDbMarkedRead);

	// avatar
	CreateDirectoryTreeW(GetAvatarPath());

	// default contacts group
	if (m_wszDefaultGroup == NULL)
		m_wszDefaultGroup = mir_wstrdup(L"WhatsApp");
	m_iBaseGroup = Clist_GroupCreate(0, m_wszDefaultGroup);

	// create standard network connection
	NETLIBUSER nlu = {};
	nlu.flags = NUF_UNICODE;
	nlu.szSettingsModule = m_szModuleName;
	nlu.szDescriptiveName.w = m_tszUserName;
	m_hNetlibUser = Netlib_RegisterUser(&nlu);

	// groupchat initialization
	GCREGISTER gcr = {};
	gcr.dwFlags = GC_TYPNOTIF | GC_DATABASE;
	gcr.ptszDispName = m_tszUserName;
	gcr.pszModule = m_szModuleName;
	Chat_Register(&gcr);

	// HookProtoEvent(ME_GC_EVENT, &WhatsAppProto::GcEventHook);
	// HookProtoEvent(ME_GC_BUILDMENU, &WhatsAppProto::GcMenuHook);
}

CTelegramProto::~CTelegramProto()
{
}

void CTelegramProto::OnModulesLoaded()
{
	CMStringA szId(getMStringA(DBKEY_ID));
	if (!szId.IsEmpty())
		m_arUsers.insert(new TG_USER(_atoi64(szId.c_str()), 0));

	for (auto &cc : AccContacts()) {
		ptrA szPath(getStringA(cc, "AvatarPath"));
		if (szPath) {
			delSetting(cc, "AvatarPath");
			delSetting(cc, DBKEY_AVATAR_HASH);
		}

		bool isGroupChat = isChatRoom(cc);
		szId = getMStringA(cc, isGroupChat ? "ChatRoomID" : DBKEY_ID);
		if (!szId.IsEmpty()) {
			auto *pUser = new TG_USER(_atoi64(szId.c_str()), cc, isGroupChat);
			pUser->szAvatarHash = getMStringA(cc, DBKEY_AVATAR_HASH);
			m_arUsers.insert(pUser);
		}
	}
}

void CTelegramProto::OnShutdown()
{
	m_bTerminated = true;
}

void CTelegramProto::OnErase()
{
	m_bUnregister = true;
	ServerThread(0);

	DeleteDirectoryTreeW(GetProtoFolder(), false);
}

int CTelegramProto::OnDbMarkedRead(WPARAM hContact, LPARAM hDbEvent)
{
	if (!hContact)
		return 0;

	// filter out only events of my protocol
	const char *szProto = Proto_GetBaseAccountName(hContact);
	if (mir_strcmp(szProto, m_szModuleName))
		return 0;

	ptrA userId(getStringA(hContact, DBKEY_ID));
	if (userId) {
		DBEVENTINFO dbei = {};
		db_event_get(hDbEvent, &dbei);
		if (dbei.szId) {
			mir_cslock lck(m_csMarkRead);
			if (m_markContact) {
				if (m_markContact != hContact)
					SendMarkRead();

				m_impl.m_markRead.Stop();
			}

			m_markContact = hContact;
			m_markIds.push_back(_atoi64(dbei.szId));
			m_impl.m_markRead.Start(500);
		}
	}

	return 0;
}

INT_PTR CTelegramProto::GetCaps(int type, MCONTACT)
{
	switch (type) {
	case PFLAGNUM_1:
		return PF1_IM | PF1_FILE | PF1_CHAT | PF1_BASICSEARCH | PF1_ADDSEARCHRES | PF1_MODEMSGRECV;
	case PFLAGNUM_2:
		return PF2_ONLINE | PF2_SHORTAWAY | PF2_LONGAWAY;
	case PFLAGNUM_4:
		return PF4_NOCUSTOMAUTH | PF4_NOAUTHDENYREASON | PF4_IMSENDOFFLINE | PF4_OFFLINEFILES | PF4_SUPPORTTYPING | PF4_AVATARS | PF4_SERVERMSGID;
	case PFLAGNUM_5:
		return PF2_SHORTAWAY | PF2_LONGAWAY;
	case PFLAG_UNIQUEIDTEXT:
		return (INT_PTR)L"Phone";
	default:
		return 0;
	}
}

int CTelegramProto::SendMsg(MCONTACT hContact, int, const char *pszMessage)
{
	ptrA szId(getStringA(hContact, DBKEY_ID));
	if (szId == nullptr)
		return 0;

	return SendTextMessage(_atoi64(szId), pszMessage);
}

int CTelegramProto::SetStatus(int iNewStatus)
{
	if (m_iDesiredStatus == iNewStatus)
		return 0;
	
	int oldStatus = m_iStatus;

	// Routing statuses not supported by Telegram
	switch (iNewStatus) {
	case ID_STATUS_OFFLINE:
		m_iDesiredStatus = iNewStatus;
		break;

	case ID_STATUS_ONLINE:
	case ID_STATUS_FREECHAT:
	default:
		m_iDesiredStatus = ID_STATUS_ONLINE;
		break;
	}

	if (m_iDesiredStatus == ID_STATUS_OFFLINE) {
		if (isRunning())
			SendQuery(new TD::close());

		m_iStatus = m_iDesiredStatus = ID_STATUS_OFFLINE;
		ProtoBroadcastAck(NULL, ACKTYPE_STATUS, ACKRESULT_SUCCESS, (HANDLE)oldStatus, m_iStatus);
	}
	else if (!isRunning() && !IsStatusConnecting(m_iStatus)) {
		m_iStatus = ID_STATUS_CONNECTING;
		ProtoBroadcastAck(NULL, ACKTYPE_STATUS, ACKRESULT_SUCCESS, (HANDLE)oldStatus, m_iStatus);

		ForkThread(&CTelegramProto::ServerThread);
	}
	else if (isRunning()) {
		m_iStatus = m_iDesiredStatus;
		ProtoBroadcastAck(0, ACKTYPE_STATUS, ACKRESULT_SUCCESS, (HANDLE)oldStatus, m_iStatus);
	}
	else ProtoBroadcastAck(NULL, ACKTYPE_STATUS, ACKRESULT_SUCCESS, (HANDLE)oldStatus, m_iStatus);

	return 0;
}