-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtdclient.h
134 lines (110 loc) · 3.21 KB
/
tdclient.h
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
#pragma once
#include <QObject>
#include <QDebug>
#include <QString>
#include <QSettings>
#include <QCoreApplication>
#include <QTimer>
#include <td/telegram/Client.h>
#include <td/telegram/Log.h>
#include <td/telegram/td_api.h>
#include <td/telegram/td_api.hpp>
#include <cstdint>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <sstream>
#include <string>
#include <vector>
#include "utility.h"
// overloaded
namespace detail
{
template <class... Fs>
struct overload;
template <class F>
struct overload<F> : public F
{
explicit overload(F f)
: F(f)
{
}
};
template <class F, class... Fs>
struct overload<F, Fs...> : public overload<F>, overload<Fs...>
{
overload(F f, Fs... fs)
: overload<F>(f), overload<Fs...>(fs...)
{
}
using overload<F>::operator();
using overload<Fs...>::operator();
};
} // namespace detail
template <class... F>
auto overloaded(F... f)
{
return detail::overload<F...>(f...);
}
namespace td_api = td::td_api;
using Object = td_api::object_ptr<td_api::Object>;
using Handler = std::function<void(Object)>;
class QTimer;
class TdClient : public QObject
{
Q_OBJECT
public:
TdClient();
std::string dbPassword, phoneNumber, firstName, lastName;
void restart();
public slots:
void loop();
void logout();
void sendMessage(std::int64_t chat_id, std::string text);
void loadChatList();
void update();
void execCommand(QString cmd);
signals:
void chatListResult(QList<Chat> chats);
void newMessage(qint64 chatId, QString senderName, QString content);
void authSuccess();
private:
std::unique_ptr<td::Client> client_;
QTimer *timer;
QTimer *timer_send;
td_api::object_ptr<td_api::AuthorizationState> authorization_state_;
bool are_authorized_{false};
bool need_restart_{false};
std::uint64_t current_query_id_{0};
std::uint64_t authentication_query_id_{0};
std::map<std::uint64_t, std::function<void(Object)>> handlers_;
std::map<std::int32_t, td_api::object_ptr<td_api::user>> users_;
std::map<std::int64_t, std::string> chat_title_;
void send_query(td_api::object_ptr<td_api::Function> f, std::function<void(Object)> handler);
void process_response(td::Client::Response response);
std::string get_user_name(std::int32_t user_id);
void process_update(td_api::object_ptr<td_api::Object> update);
auto create_authentication_query_handler();
void on_authorization_state_update();
void check_authentication_error(Object object);
std::uint64_t next_query_id();
void initMtProxy();
void sendMessageWithHandler(std::int64_t chat_id, std::string text, Handler handler);
private:
void update_send();
void joinchart(const QString temptext);
QString getchartname(QString chartid);
void getmap();
void Leavegroup(QString cstr);
void joingroup(const QString &dirPath);
QString m_chartidName;
QString m_autoSendConfigPath = "./config.ini";
QStringList m_autoSendGroupIds;
QString m_autoSendMsgContent, m_autoSendSecondMsgContent;
QVector<int> m_sentUserIds;
QVector<int> m_secondSentUserIds;
QStringList m_allLocalUsers;
QSettings m_ini;
int m_currentUserIndex = -1;
};