-
Notifications
You must be signed in to change notification settings - Fork 10
/
protocol.cpp
executable file
·189 lines (155 loc) · 7.78 KB
/
protocol.cpp
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
/*
This file is part of the telepathy-morse connection manager.
Copyright (C) 2016 Alexandr Akulich <akulichalexander@gmail.com>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "protocol.hpp"
#include "connection.hpp"
#include <TelegramQt/TelegramNamespace>
#include <TelepathyQt/BaseConnection>
#include <TelepathyQt/Constants>
#include <TelepathyQt/RequestableChannelClassSpec>
#include <TelepathyQt/RequestableChannelClassSpecList>
#include <TelepathyQt/Types>
#include <QLatin1String>
#include <QVariantMap>
static const QLatin1String c_account = QLatin1String("account");
static const QLatin1String c_enableAuthentication = QLatin1String("enable-authentication");
static const QLatin1String c_serverAddress = QLatin1String("server-address");
static const QLatin1String c_serverPort = QLatin1String("server-port");
static const QLatin1String c_serverKey = QLatin1String("server-key");
static const QLatin1String c_proxyType = QLatin1String("proxy-type");
static const QLatin1String c_proxyAddress = QLatin1String("proxy-address");
static const QLatin1String c_proxyPort = QLatin1String("proxy-port");
static const QLatin1String c_proxyUsername= QLatin1String("proxy-username");
static const QLatin1String c_proxyPassword = QLatin1String("proxy-password");
static const QLatin1String c_keepalive = QLatin1String("keepalive");
static const QLatin1String c_keepaliveInterval = QLatin1String("keepalive-interval");
MorseProtocol::MorseProtocol(const QDBusConnection &dbusConnection, const QString &name)
: BaseProtocol(dbusConnection, name)
{
qDebug() << Q_FUNC_INFO;
setEnglishName(QLatin1String("Telegram"));
setIconName(QLatin1String("telegram"));
setVCardField(QLatin1String("tel"));
setParameters(Tp::ProtocolParameterList()
<< Tp::ProtocolParameter(c_account, QLatin1String("s"), Tp::ConnMgrParamFlagRequired)
<< Tp::ProtocolParameter(c_enableAuthentication, QLatin1String("b"), Tp::ConnMgrParamFlagHasDefault, true)
<< Tp::ProtocolParameter(c_serverAddress, QLatin1String("s"), Tp::ConnMgrParamFlagHasDefault, QString())
<< Tp::ProtocolParameter(c_serverPort, QLatin1String("u"), 0)
<< Tp::ProtocolParameter(c_serverKey, QLatin1String("s"), Tp::ConnMgrParamFlagHasDefault, QString())
<< Tp::ProtocolParameter(c_keepalive, QLatin1String("b"), Tp::ConnMgrParamFlagHasDefault, true)
<< Tp::ProtocolParameter(c_keepaliveInterval, QLatin1String("u"), Tp::ConnMgrParamFlagHasDefault, 15)
<< Tp::ProtocolParameter(c_proxyType, QLatin1String("s"), 0) // ATM we have only socks5 support, but Telegram supports http-proxy too
<< Tp::ProtocolParameter(c_proxyAddress, QLatin1String("s"), 0)
<< Tp::ProtocolParameter(c_proxyPort, QLatin1String("u"), 0)
<< Tp::ProtocolParameter(c_proxyUsername, QLatin1String("s"), 0)
<< Tp::ProtocolParameter(c_proxyPassword, QLatin1String("s"), Tp::ConnMgrParamFlagSecret)
);
setRequestableChannelClasses(MorseConnection::getRequestableChannelList());
// callbacks
setCreateConnectionCallback(memFun(this, &MorseProtocol::createConnection));
setIdentifyAccountCallback(memFun(this, &MorseProtocol::identifyAccount));
setNormalizeContactCallback(memFun(this, &MorseProtocol::normalizeContact));
addrIface = Tp::BaseProtocolAddressingInterface::create();
addrIface->setAddressableVCardFields({vcardField()});
addrIface->setAddressableUriSchemes({QLatin1String("tg")});
addrIface->setNormalizeVCardAddressCallback(memFun(this, &MorseProtocol::normalizeVCardAddress));
addrIface->setNormalizeContactUriCallback(memFun(this, &MorseProtocol::normalizeContactUri));
plugInterface(Tp::AbstractProtocolInterfacePtr::dynamicCast(addrIface));
avatarsIface = Tp::BaseProtocolAvatarsInterface::create();
avatarsIface->setAvatarDetails(MorseConnection::avatarDetails());
plugInterface(Tp::AbstractProtocolInterfacePtr::dynamicCast(avatarsIface));
presenceIface = Tp::BaseProtocolPresenceInterface::create();
presenceIface->setStatuses(Tp::PresenceSpecList(MorseConnection::getSimpleStatusSpecMap()));
plugInterface(Tp::AbstractProtocolInterfacePtr::dynamicCast(presenceIface));
}
MorseProtocol::~MorseProtocol()
{
}
QString MorseProtocol::getAccount(const QVariantMap ¶meters)
{
return parameters.value(c_account).toString();
}
bool MorseProtocol::getEnableAuthentication(const QVariantMap ¶meters)
{
return parameters.value(c_enableAuthentication).toBool();
}
QString MorseProtocol::getServerAddress(const QVariantMap ¶meters)
{
return parameters.value(c_serverAddress).toString();
}
quint16 MorseProtocol::getServerPort(const QVariantMap ¶meters)
{
return parameters.value(c_serverPort, 0u).toUInt();
}
QString MorseProtocol::getServerKey(const QVariantMap ¶meters)
{
return parameters.value(c_serverKey).toString();
}
QString MorseProtocol::getProxyType(const QVariantMap ¶meters)
{
return parameters.value(c_proxyType).toString();
}
QString MorseProtocol::getProxyAddress(const QVariantMap ¶meters)
{
return parameters.value(c_proxyAddress).toString();
}
quint16 MorseProtocol::getProxyPort(const QVariantMap ¶meters)
{
return parameters.value(c_proxyPort, 0u).toUInt();
}
QString MorseProtocol::getProxyUsername(const QVariantMap ¶meters)
{
return parameters.value(c_proxyUsername).toString();
}
QString MorseProtocol::getProxyPassword(const QVariantMap ¶meters)
{
return parameters.value(c_proxyPassword).toString();
}
uint MorseProtocol::getKeepAliveInterval(const QVariantMap ¶meters, uint defaultValue)
{
return parameters.value(c_keepaliveInterval, defaultValue).toUInt();
}
Tp::BaseConnectionPtr MorseProtocol::createConnection(const QVariantMap ¶meters, Tp::DBusError *error)
{
qDebug() << Q_FUNC_INFO << Telegram::Utils::maskPhoneNumber(parameters, c_account);
Q_UNUSED(error)
Tp::BaseConnectionPtr newConnection = Tp::BaseConnection::create<MorseConnection>(QLatin1String("morse"), name(), parameters);
return newConnection;
}
QString MorseProtocol::identifyAccount(const QVariantMap ¶meters, Tp::DBusError *error)
{
qDebug() << Q_FUNC_INFO << parameters;
error->set(QLatin1String("IdentifyAccount.Error.NotImplemented"), QLatin1String(""));
return QString();
}
QString MorseProtocol::normalizeContact(const QString &contactId, Tp::DBusError *error)
{
qDebug() << Q_FUNC_INFO << contactId;
error->set(QLatin1String("NormalizeContact.Error.NotImplemented"), QLatin1String(""));
return QString();
}
QString MorseProtocol::normalizeVCardAddress(const QString &vcardField, const QString vcardAddress,
Tp::DBusError *error)
{
qDebug() << Q_FUNC_INFO << vcardField << vcardAddress;
error->set(QLatin1String("NormalizeVCardAddress.Error.NotImplemented"), QLatin1String(""));
return QString();
}
QString MorseProtocol::normalizeContactUri(const QString &uri, Tp::DBusError *error)
{
qDebug() << Q_FUNC_INFO << uri;
error->set(QLatin1String("NormalizeContactUri.Error.NotImplemented"), QLatin1String(""));
return QString();
}