forked from majn/telegram-purple
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtgp-ft.c
326 lines (269 loc) · 10 KB
/
tgp-ft.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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/*
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 <tgl-queries.h>
#include "tgp-utils.h"
#include "tgp-ft.h"
#include "tgp-structs.h"
#include "msglog.h"
#include <purple.h>
#include <glib.h>
#include <glib/gstdio.h>
#include "telegram-purple.h"
#include "telegram-base.h"
static void tgprpl_xfer_free_data (struct tgp_xfer_send_data *data);
static char *tgp_strdup_determine_filename (const char *mime, const char *caption,
int flags, long long hash) {
if (caption) {
return g_strdup (caption);
}
const char *type = NULL;
if (mime) {
if (flags & TGLDF_VIDEO) {
// video message
type = "mp4";
} else if (flags & TGLDF_AUDIO) {
// audio message
type = "ogg";
} else {
// document message
type = tgp_mime_to_filetype (mime);
}
}
if (! str_not_empty(type)) {
if (flags & TGLDF_IMAGE) {
type = "png";
} else if (flags & TGLDF_AUDIO) {
type = "ogg";
} else if (flags & TGLDF_VIDEO) {
type = "mp4";
} else if (flags & TGLDF_STICKER) {
type = "webp";
} else {
type = "bin";
}
}
return g_strdup_printf ("%lld.%s", ABS(hash), type);
}
static void tgprpl_xfer_recv_on_finished (struct tgl_state *TLS, void *_data, int success, const char *filename) {
debug ("tgprpl_xfer_recv_on_finished()");
struct tgp_xfer_send_data *data = _data;
char *selected = g_strdup(purple_xfer_get_local_filename (data->xfer));
if (success) {
if (!data->done) {
debug ("purple_xfer_set_completed");
purple_xfer_set_bytes_sent (data->xfer, purple_xfer_get_size (data->xfer));
purple_xfer_set_completed (data->xfer, TRUE);
purple_xfer_end (data->xfer);
}
} else {
tgp_notify_on_error_gw (TLS, NULL, success);
failure ("ERROR xfer failed");
}
data->xfer->data = NULL;
tgprpl_xfer_free_data (data);
debug ("moving transferred file from tgl directory %s to selected target %s", selected, filename);
g_unlink (selected);
g_rename (filename, selected);
g_free (selected);
}
static void tgprpl_xfer_on_finished (struct tgl_state *TLS, void *_data, int success, struct tgl_message *M) {
debug ("tgprpl_xfer_on_finished()");
struct tgp_xfer_send_data *data = _data;
if (success) {
if (!data->done) {
debug ("purple_xfer_set_completed");
purple_xfer_set_bytes_sent (data->xfer, purple_xfer_get_size (data->xfer));
purple_xfer_set_completed (data->xfer, TRUE);
purple_xfer_end(data->xfer);
}
write_secret_chat_file (TLS);
} else {
tgp_notify_on_error_gw (TLS, NULL, success);
failure ("ERROR xfer failed");
}
data->xfer->data = NULL;
tgprpl_xfer_free_data (data);
}
static void tgprpl_xfer_canceled (PurpleXfer *X) {
struct tgp_xfer_send_data *data = X->data;
tgprpl_xfer_free_data (data);
}
static gboolean tgprpl_xfer_upload_progress (gpointer _data) {
PurpleXfer *X = _data;
struct tgp_xfer_send_data *data = X->data;
connection_data *conn = data->conn;
PurpleXferType type = purple_xfer_get_type(X);
switch (type) {
case PURPLE_XFER_SEND:
purple_xfer_set_size (X, conn->TLS->cur_uploading_bytes);
purple_xfer_set_bytes_sent (X, conn->TLS->cur_uploaded_bytes);
purple_xfer_update_progress (X);
debug ("PURPLE_XFER_SEND progress %d / %d", conn->TLS->cur_uploaded_bytes, conn->TLS->cur_uploading_bytes);
if (conn->TLS->cur_uploaded_bytes == conn->TLS->cur_uploading_bytes) {
data->timer = 0;
return FALSE;
}
break;
case PURPLE_XFER_RECEIVE:
purple_xfer_set_size (X, conn->TLS->cur_downloading_bytes);
purple_xfer_set_bytes_sent (X, conn->TLS->cur_downloaded_bytes);
purple_xfer_update_progress (X);
debug ("PURPLE_XFER_RECEIVE progress %d / %d", conn->TLS->cur_downloaded_bytes, conn->TLS->cur_downloading_bytes);
if (conn->TLS->cur_downloading_bytes == conn->TLS->cur_downloaded_bytes) {
data->timer = 0;
return FALSE;
}
break;
default:
case PURPLE_XFER_UNKNOWN:
failure ("ERROR: tgprpl_xfer_upload_progress xfer type PURPLE_XFER_UNKNOWN.");
return FALSE;
break;
}
return TRUE;
}
static void tgprpl_xfer_recv_init (PurpleXfer *X) {
debug ("tgprpl_xfer_recv_init");
struct tgp_xfer_send_data *data = X->data;
struct tgl_state *TLS = data->conn->TLS;
struct tgl_message *M = data->msg;
struct tgl_document *D = M->media.document;
tgl_peer_t *P = NULL;
purple_xfer_start (X, -1, NULL, 0);
const char *who = purple_xfer_get_remote_user (X);
P = tgp_blist_peer_find (TLS, who);
if (P) {
switch (M->media.type) {
case tgl_message_media_document:
tgl_do_load_document (TLS, D, tgprpl_xfer_recv_on_finished, data);
break;
case tgl_message_media_document_encr:
tgl_do_load_encr_document (TLS, M->media.encr_document,
tgprpl_xfer_recv_on_finished, data);
break;
case tgl_message_media_audio:
tgl_do_load_audio (TLS, D, tgprpl_xfer_recv_on_finished, data);
break;
case tgl_message_media_video:
tgl_do_load_video (TLS, D, tgprpl_xfer_recv_on_finished, data);
break;
default:
failure ("Unknown message media type: %d, XFER not possible.", M->media.type);
break;
}
} else {
warning ("User not found, not downloading...");
}
data->timer = purple_timeout_add (100, tgprpl_xfer_upload_progress, X);
}
static void tgprpl_xfer_send_init (PurpleXfer *X) {
struct tgp_xfer_send_data *data = X->data;
purple_xfer_start (X, -1, NULL, 0);
const char *file = purple_xfer_get_filename (X);
const char *localfile = purple_xfer_get_local_filename (X);
const char *who = purple_xfer_get_remote_user (X);
debug ("xfer_on_init (file=%s, local=%s, who=%s)", file, localfile, who);
tgl_peer_t *P = tgp_blist_peer_find (data->conn->TLS, who);
if (tgl_get_peer_type (P->id) == TGL_PEER_ENCR_CHAT) {
purple_xfer_error (PURPLE_XFER_SEND, data->conn->pa, who, _("Sorry, sending documents to encrypted chats not yet supported."));
return;
}
if (P) {
tgl_do_send_document (data->conn->TLS, P->id, (char*) localfile, NULL,
0, TGL_SEND_MSG_FLAG_DOCUMENT_AUTO, tgprpl_xfer_on_finished, data);
}
data->timer = purple_timeout_add (100, tgprpl_xfer_upload_progress, X);
}
static void tgprpl_xfer_init_data (PurpleXfer *X, connection_data *conn, struct tgl_message *msg) {
if (!X->data) {
struct tgp_xfer_send_data *data = g_malloc0 (sizeof (struct tgp_xfer_send_data));
data->xfer = X;
data->conn = conn;
data->msg = msg;
X->data = data;
}
}
static void tgprpl_xfer_free_data (struct tgp_xfer_send_data *data) {
if (data->timer) { purple_input_remove(data->timer); }
data->timer = 0;
g_free (data);
}
void tgprpl_xfer_free_all (connection_data *conn) {
GList *xfers = purple_xfers_get_all();
while (xfers) {
PurpleXfer *xfer = xfers->data;
struct tgp_xfer_send_data *data = xfer->data;
if (data) {
purple_xfer_cancel_local (xfer);
}
xfers = g_list_next(xfers);
}
}
PurpleXfer *tgprpl_new_xfer (PurpleConnection * gc, const char *who) {
debug ("tgprpl_new_xfer()");
connection_data *conn = purple_connection_get_protocol_data (gc);
PurpleXfer *X = purple_xfer_new (conn->pa, PURPLE_XFER_SEND, who);
if (X) {
purple_xfer_set_init_fnc (X, tgprpl_xfer_send_init);
purple_xfer_set_cancel_send_fnc (X, tgprpl_xfer_canceled);
tgprpl_xfer_init_data (X, purple_connection_get_protocol_data (gc), NULL);
}
return (PurpleXfer *)X;
}
static PurpleXfer *tgprpl_new_xfer_recv (PurpleConnection * gc, const char *who) {
connection_data *conn = purple_connection_get_protocol_data (gc);
PurpleXfer *X = purple_xfer_new (conn->pa, PURPLE_XFER_RECEIVE, who);
purple_xfer_set_init_fnc (X, tgprpl_xfer_recv_init);
purple_xfer_set_cancel_recv_fnc (X, tgprpl_xfer_canceled);
return X;
}
void tgprpl_recv_file (PurpleConnection *gc, const char *who, struct tgl_message *M) {
debug ("tgprpl_recv_file()");
g_return_if_fail (who);
PurpleXfer *X = tgprpl_new_xfer_recv (gc, who);
const char *mime_type, *caption;
long long access_hash;
int flags, size;
if (M->media.type == tgl_message_media_document_encr) {
mime_type = M->media.encr_document->mime_type;
caption = M->media.encr_document->caption;
access_hash = M->media.encr_document->access_hash;
flags = M->media.encr_document->flags;
size = M->media.encr_document->size;
} else {
mime_type = M->media.document->mime_type;
caption = M->media.document->caption;
access_hash = M->media.document->access_hash;
flags = M->media.document->flags;
size = M->media.document->size;
}
char *filename = tgp_strdup_determine_filename (mime_type, caption, flags, access_hash);
purple_xfer_set_filename (X, filename);
g_free (filename);
purple_xfer_set_size (X, size);
tgprpl_xfer_init_data (X, purple_connection_get_protocol_data (gc), M);
purple_xfer_request (X);
}
void tgprpl_send_file (PurpleConnection * gc, const char *who, const char *file) {
debug ("tgprpl_send_file()");
PurpleXfer *X = tgprpl_new_xfer (gc, who);
if (file) {
purple_xfer_request_accepted (X, file);
debug ("starting xfer...");
} else {
purple_xfer_request (X);
}
}