forked from majn/telegram-purple
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tgp-chat.c
264 lines (210 loc) · 9 KB
/
tgp-chat.c
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/*
This file is part of telegram-purple
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 02111-1301 USA
Copyright Matthias Jentsch 2014-2015
*/
#include "tgp-chat.h"
#include "msglog.h"
#include "tgp-utils.h"
#include "telegram-base.h"
#include <tgl.h>
#include <assert.h>
GHashTable *tgp_chat_info_new (struct tgl_state *TLS, struct tgl_chat *chat) {
gchar *admin = g_strdup_printf ("%d", chat->admin_id);
gchar *title = g_strdup (chat->print_title);
gchar *name = p2tgl_strdup_id (chat->id);
GHashTable *ht = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, g_free);
g_hash_table_insert (ht, "subject", title);
g_hash_table_insert (ht, "id", name);
g_hash_table_insert (ht, "owner", admin);
return ht;
}
PurpleChat *p2tgl_chat_new (struct tgl_state *TLS, struct tgl_chat *chat) {
return purple_chat_new (tg_get_acc(TLS), chat->title, tgp_chat_info_new (TLS, chat));
}
void p2tgl_chat_update (PurpleChat *chat, tgl_peer_id_t id, int admin_id, const char *subject) {
GHashTable *ht = purple_chat_get_components (chat);
g_hash_table_replace (ht, g_strdup ("id"), g_strdup_printf ("%d", tgl_get_peer_id (id)));
g_hash_table_replace (ht, g_strdup ("owner"), g_strdup_printf ("%d", admin_id));
g_hash_table_replace (ht, g_strdup ("subject"), g_strdup (subject));
}
void tgp_chat_on_loaded_chat_full (struct tgl_state *TLS, struct tgl_chat *C) {
connection_data *conn = TLS->ev_base;
PurpleChat *PC = p2tgl_chat_find (TLS, C->id);
if (!PC) {
PC = p2tgl_chat_new (TLS, C);
if (purple_account_get_bool (conn->pa, TGP_KEY_JOIN_GROUP_CHATS, TGP_DEFAULT_JOIN_GROUP_CHATS)) {
purple_blist_add_chat (PC, NULL, NULL);
}
}
p2tgl_chat_update (PC, C->id, C->admin_id, C->print_title);
}
static void tgp_chat_on_loaded_chat_full_joining (struct tgl_state *TLS, void *_,
int success, struct tgl_chat *C) {
debug ("tgp_chat_on_loaded_chat_full_joining()");
if (! success) {
tgp_notify_on_error_gw (TLS, NULL, success);
return;
}
tgp_chat_on_loaded_chat_full (TLS, C);
tgp_chat_show (TLS, C);
// Check if the users attempts to join an empty chat
if (! C->user_list_size) {
p2tgl_got_chat_in (TLS, C->id, C->id, "You have already left this chat.",
PURPLE_MESSAGE_SYSTEM, time (NULL));
}
}
static void tgp_chat_add_all_users (struct tgl_state *TLS, PurpleConversation *conv, struct tgl_chat *C) {
GList *users = NULL,
*flags = NULL;
int i = 0;
for (; i < C->user_list_size; i++) {
struct tgl_chat_user *uid = (C->user_list + i);
users = g_list_append (users, g_strdup_printf ("%d", uid->user_id));
flags = g_list_append (flags, GINT_TO_POINTER(C->admin_id == uid->user_id ? PURPLE_CBFLAGS_FOUNDER : PURPLE_CBFLAGS_NONE));
}
purple_conv_chat_add_users (PURPLE_CONV_CHAT(conv), users, NULL, flags, FALSE);
while (users) {
p2tgl_conv_add_user_rename (tgl_peer_get (TLS, TGL_MK_USER(atoi (users->data))), conv);
users = g_list_next (users);
}
g_list_free_full (users, g_free);
g_list_free (flags);
}
void tgp_chat_users_update (struct tgl_state *TLS, struct tgl_chat *C) {
PurpleConversation *pc = purple_find_chat (tg_get_conn (TLS), tgl_get_peer_id (C->id));
if (pc) {
purple_conv_chat_clear_users (purple_conversation_get_chat_data (pc));
tgp_chat_add_all_users (TLS, pc, C);
}
}
PurpleConversation *tgp_chat_show (struct tgl_state *TLS, struct tgl_chat *C) {
connection_data *conn = TLS->ev_base;
PurpleConversation *convo = purple_find_chat (conn->gc, tgl_get_peer_id (C->id));
PurpleConvChat *chat = purple_conversation_get_chat_data (convo);
if (! convo || (chat && purple_conv_chat_has_left (chat))) {
convo = p2tgl_got_joined_chat (conn->TLS, C);
tgp_chat_users_update (conn->TLS, C);
}
return convo;
}
GList *tgprpl_chat_join_info (PurpleConnection * gc) {
struct proto_chat_entry *pce;
pce = g_new0 (struct proto_chat_entry, 1);
pce->label = "_Subject:";
pce->identifier = "subject";
pce->required = FALSE;
GList *info = g_list_append (NULL, pce);
pce = g_new0 (struct proto_chat_entry, 1);
pce->label = "_Invite Link:";
pce->identifier = "link";
pce->required = FALSE;
info = g_list_append (info, pce);
pce = g_new0 (struct proto_chat_entry, 1);
pce->label = "_Chat Id:";
pce->identifier = "id";
pce->required = FALSE;
return g_list_append (info, pce);
}
GHashTable *tgprpl_chat_info_defaults (PurpleConnection *gc, const char *chat_name) {
debug ("tgprpl_chat_info_defaults()");
connection_data *conn = purple_connection_get_protocol_data (gc);
if (chat_name) {
tgl_peer_t *P = tgl_peer_get_by_name (conn->TLS, chat_name);
if (P) {
debug ("found chat...");
return tgp_chat_info_new (conn->TLS, &P->chat);
}
warning ("Chat not found, returning empty defaults...");
}
return g_hash_table_new_full (g_str_hash, *g_str_equal, NULL, g_free);
}
void tgprpl_chat_join (PurpleConnection * gc, GHashTable *data) {
debug ("tgprpl_chat_join()");
connection_data *conn = purple_connection_get_protocol_data (gc);
// join existing chat by id when the user clicks on a chat in the buddy list
void *value = g_hash_table_lookup (data, "id");
if (value && atoi (value)) {
tgl_peer_id_t cid = TGL_MK_CHAT(atoi (value));
tgl_peer_t *P = tgl_peer_get (conn->TLS, cid);
if (P) {
debug ("joining chat by id %d ...", tgl_get_peer_id (cid));
tgl_do_get_chat_info (conn->TLS, cid, FALSE, tgp_chat_on_loaded_chat_full_joining, NULL);
} else {
warning ("Cannot join chat %d, peer not found...", tgl_get_peer_id (cid));
purple_serv_got_join_chat_failed (gc, data);
}
return;
}
// join chat by invite link provided in the chat join window
const char *link = g_hash_table_lookup (data, "link");
if (str_not_empty (link)) {
tgl_do_import_chat_link (conn->TLS, link, (int)strlen (link), tgp_notify_on_error_gw, NULL);
return;
}
// if a chat with this name doesn't exist yet, prompt to create one
const char *subject = g_hash_table_lookup (data, "subject");
if (str_not_empty (subject)) {
tgl_peer_t *P = tgl_peer_get_by_name (conn->TLS, subject);
// handle joining chats by print_names as used by the Adium plugin
if (P && tgl_get_peer_type (P->id) == TGL_PEER_CHAT) {
debug ("joining chat by subject %s ...", subject);
tgl_do_get_chat_info (conn->TLS, P->id, FALSE, tgp_chat_on_loaded_chat_full_joining, NULL);
return;
}
// user creates a new chat by providing its subject the chat join window
request_create_chat (conn->TLS, subject);
}
}
char *tgprpl_get_chat_name (GHashTable * data) {
return g_strdup (g_hash_table_lookup (data, "subject"));
}
static void tgp_chat_roomlist_it (tgl_peer_t *P, void *extra) {
connection_data *conn = extra;
if (tgl_get_peer_type (P->id) == TGL_PEER_CHAT && P->chat.users_num) {
char *name = g_strdup_printf ("%d", tgl_get_peer_id (P->id));
PurpleRoomlistRoom *room = purple_roomlist_room_new (PURPLE_ROOMLIST_ROOMTYPE_ROOM, P->chat.print_title, NULL);
purple_roomlist_room_add_field (conn->roomlist, room, name);
purple_roomlist_room_add_field (conn->roomlist, room, GINT_TO_POINTER(P->chat.users_num));
purple_roomlist_room_add (conn->roomlist, room);
g_free (name);
}
}
PurpleRoomlist *tgprpl_roomlist_get_list (PurpleConnection *gc) {
debug ("tgprpl_roomlist_get_list()");
connection_data *conn = purple_connection_get_protocol_data (gc);
GList *fields = NULL;
if (conn->roomlist) {
purple_roomlist_unref (conn->roomlist);
}
conn->roomlist = purple_roomlist_new (purple_connection_get_account (gc));
PurpleRoomlistField *f = purple_roomlist_field_new (PURPLE_ROOMLIST_FIELD_STRING, "", "id", TRUE);
fields = g_list_append (fields, f);
f = purple_roomlist_field_new (PURPLE_ROOMLIST_FIELD_INT, "Users", "users", FALSE);
fields = g_list_append (fields, f);
purple_roomlist_set_fields (conn->roomlist, fields);
tgl_peer_iterator_ex (conn->TLS, tgp_chat_roomlist_it, conn);
return conn->roomlist;
}
void tgprpl_roomlist_cancel (PurpleRoomlist *list) {
PurpleConnection *gc = purple_account_get_connection (list->account);
if (! gc) {
return;
}
connection_data *conn = purple_connection_get_protocol_data (gc);
purple_roomlist_set_in_progress (list, FALSE);
if (conn->roomlist == list) {
conn->roomlist = NULL;
purple_roomlist_unref (list);
}
}