forked from majn/telegram-purple
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtgp-2prpl.c
340 lines (281 loc) · 11.7 KB
/
tgp-2prpl.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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/*
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
*/
#include "telegram-purple.h"
#ifdef HAVE_LIBWEBP
#include <webp/decode.h>
#include "lodepng/lodepng.h"
#endif
#include "tgp-2prpl.h"
#include "tgp-structs.h"
#include "telegram-purple.h"
#include "tgp-utils.h"
#include "telegram-base.h"
#include "tgp-msg.h"
#include "tgp-net.h"
#include <server.h>
#include <tgl.h>
#include "msglog.h"
#include <assert.h>
PurpleAccount *tg_get_acc (struct tgl_state *TLS) {
return tg_get_data(TLS)->pa;
}
PurpleConnection *tg_get_conn (struct tgl_state *TLS) {
return tg_get_data (TLS)->gc;
}
connection_data *tg_get_data (struct tgl_state *TLS) {
return TLS->ev_base;
}
connection_data *gc_get_conn (PurpleConnection *gc) {
return purple_connection_get_protocol_data (gc);
}
connection_data *pa_get_conn (PurpleAccount *pa) {
return purple_connection_get_protocol_data (purple_account_get_connection (pa));
}
connection_data *pbn_get_conn (PurpleBlistNode *node) {
if (PURPLE_BLIST_NODE_IS_CHAT (node)) {
return pa_get_conn (purple_chat_get_account ((PurpleChat *)node));
}
if (PURPLE_BLIST_NODE_IS_BUDDY (node)) {
return pa_get_conn (purple_buddy_get_account ((PurpleBuddy *)node));
}
return NULL;
}
connection_data *c_get_conn (struct connection *c) {
struct tgl_state *TLS = c->TLS;
return TLS->ev_base;
}
int p2tgl_status_is_present (PurpleStatus *status) {
const char *name = purple_status_get_id (status);
return !(strcmp (name, "unavailable") == 0 || strcmp (name, "away") == 0);
}
int p2tgl_send_notifications (PurpleAccount *acct) {
int ret = purple_account_get_bool (acct, TGP_KEY_SEND_READ_NOTIFICATIONS, TGP_DEFAULT_SEND_READ_NOTIFICATIONS);
debug ("sending notifications: %d", ret);
return ret;
}
void p2tgl_got_chat_left (struct tgl_state *TLS, tgl_peer_id_t chat) {
serv_got_chat_left (tg_get_conn(TLS), tgl_get_peer_id(chat));
}
void p2tgl_got_chat_in (struct tgl_state *TLS, tgl_peer_id_t chat, tgl_peer_id_t who,
const char *message, int flags, time_t when) {
serv_got_chat_in (tg_get_conn(TLS), tgl_get_peer_id (chat), tgp_blist_peer_get_purple_name (TLS, who), flags, message, when);
}
void p2tgl_got_im_combo (struct tgl_state *TLS, tgl_peer_id_t who, const char *msg, int flags, time_t when) {
connection_data *conn = TLS->ev_base;
if (flags & PURPLE_MESSAGE_SYSTEM) {
tgp_msg_sys_out (TLS, msg, who, flags & PURPLE_MESSAGE_NO_LOG);
return;
}
/*
Outgoing messages are not well supported in different libpurple clients,
purple_conv_im_write should have the best among different versions. Unfortunately
this causes buggy formatting in Adium, so we don't use this workaround in that case.
NOTE: Outgoing messages will not work in Adium <= 1.6.0, there is no way to print outgoing
messages in those versions at all.
*/
#ifndef __ADIUM_
if (flags & PURPLE_MESSAGE_SEND) {
PurpleConversation *conv = p2tgl_find_conversation_with_account (TLS, who);
if (!conv) {
conv = purple_conversation_new (PURPLE_CONV_TYPE_IM, tg_get_acc (TLS),
tgp_blist_peer_get_purple_name (TLS, who));
}
purple_conv_im_write (purple_conversation_get_im_data (conv), tgp_blist_peer_get_purple_name (TLS, who),
msg, PURPLE_MESSAGE_SEND, when);
return;
}
#endif
serv_got_im (conn->gc, tgp_blist_peer_get_purple_name (TLS, who), msg, flags, when);
}
PurpleConversation *p2tgl_find_conversation_with_account (struct tgl_state *TLS, tgl_peer_id_t peer) {
int type = PURPLE_CONV_TYPE_IM;
if (tgl_get_peer_type (peer) == TGL_PEER_CHAT) {
type = PURPLE_CONV_TYPE_CHAT;
}
PurpleConversation *conv = purple_find_conversation_with_account (type,
tgp_blist_peer_get_purple_name (TLS, peer), tg_get_acc (TLS));
return conv;
}
void p2tgl_prpl_got_user_status (struct tgl_state *TLS, tgl_peer_id_t user, struct tgl_user_status *status) {
connection_data *data = TLS->ev_base;
if (status->online == 1) {
purple_prpl_got_user_status (tg_get_acc (TLS), tgp_blist_peer_get_purple_name (TLS, user), "available", NULL);
} else {
debug ("%d: when=%d", tgl_get_peer_id (user), status->when);
if (tgp_time_n_days_ago (
purple_account_get_int (data->pa, "inactive-days-offline", TGP_DEFAULT_INACTIVE_DAYS_OFFLINE)) > status->when && status->when) {
debug ("offline");
purple_prpl_got_user_status (tg_get_acc (TLS), tgp_blist_peer_get_purple_name (TLS, user), "offline", NULL);
} else {
debug ("mobile");
purple_prpl_got_user_status (tg_get_acc (TLS), tgp_blist_peer_get_purple_name (TLS, user), "mobile", NULL);
}
}
}
tgl_chat_id_t p2tgl_chat_get_id (PurpleChat *PC) {
char *name = g_hash_table_lookup (purple_chat_get_components (PC), "id");
if (! name || ! atoi (name)) {
warning ("p2tgl_chat_id_get: no id found in chat %s", PC->alias);
return TGL_MK_CHAT(0);
}
return TGL_MK_CHAT(atoi (name));
}
void p2tgl_conv_add_user (struct tgl_state *TLS, PurpleConversation *conv,
int user, char *message, int flags, int new_arrival) {
const char *name = tgp_blist_peer_get_purple_name (TLS, TGL_MK_USER (user));
g_return_if_fail (name);
purple_conv_chat_add_user (purple_conversation_get_chat_data (conv), name, message, flags, new_arrival);
}
PurpleNotifyUserInfo *p2tgl_notify_user_info_new (struct tgl_user *U) {
PurpleNotifyUserInfo *info = purple_notify_user_info_new();
if (str_not_empty (U->first_name) && str_not_empty (U->last_name)) {
purple_notify_user_info_add_pair (info, _("First name"), U->first_name);
purple_notify_user_info_add_pair (info, _("Last name"), U->last_name);
} else {
purple_notify_user_info_add_pair (info, _("Name"), U->print_name);
}
if (str_not_empty (U->username)) {
char *username = g_strdup_printf ("@%s", U->username);
purple_notify_user_info_add_pair (info, _("Username"), username);
g_free (username);
}
char *status = tgp_format_user_status (&U->status);
purple_notify_user_info_add_pair (info, _("Last seen"), status);
g_free (status);
if (str_not_empty (U->phone)) {
char *phone = g_strdup_printf ("+%s", U->phone);
purple_notify_user_info_add_pair (info, _("Phone"), phone);
g_free (phone);
}
return info;
}
PurpleNotifyUserInfo *p2tgl_notify_encrypted_chat_info_new (struct tgl_state *TLS, struct tgl_secret_chat *secret,
struct tgl_user *U) {
PurpleNotifyUserInfo *info = p2tgl_notify_user_info_new (U);
if (secret->state == sc_waiting) {
purple_notify_user_info_add_pair (info, "", _("Waiting for the user to get online ..."));
return info;
}
const char *ttl_key = _("Self destruction timer");
if (secret->ttl) {
char *ttl = g_strdup_printf ("%d", secret->ttl);
purple_notify_user_info_add_pair (info, ttl_key, ttl);
g_free (ttl);
} else {
purple_notify_user_info_add_pair (info, ttl_key, _("Off"));
}
if (secret->first_key_sha[0]) {
int sha1key_store_id = tgp_visualize_key (TLS, secret->first_key_sha);
if (sha1key_store_id != -1) {
char *ident_icon = tgp_format_img (sha1key_store_id);
purple_notify_user_info_add_pair (info, _("Secret key"), ident_icon);
g_free(ident_icon);
}
}
return info;
}
PurpleNotifyUserInfo *p2tgl_notify_peer_info_new (struct tgl_state *TLS, tgl_peer_t *P) {
switch (tgl_get_peer_type (P->id)) {
case TGL_PEER_ENCR_CHAT: {
struct tgl_secret_chat *chat = &P->encr_chat;
tgl_peer_t *partner = tgp_encr_chat_get_partner (TLS, chat);
return p2tgl_notify_encrypted_chat_info_new (TLS, chat, &partner->user);
break;
}
case TGL_PEER_USER:
return p2tgl_notify_user_info_new (&P->user);
break;
default:
return purple_notify_user_info_new ();
}
}
int p2tgl_imgstore_add_with_id (const char* filename) {
gchar *data = NULL;
size_t len;
GError *err = NULL;
g_file_get_contents (filename, &data, &len, &err);
int id = purple_imgstore_add_with_id (data, len, NULL);
return id;
}
#ifdef HAVE_LIBWEBP
static const int MAX_W = 256;
static const int MAX_H = 256;
int p2tgl_imgstore_add_with_id_webp (const char *filename) {
const uint8_t *data = NULL;
size_t len;
GError *err = NULL;
g_file_get_contents (filename, (gchar **) &data, &len, &err);
if (err) { warning ("cannot open file %s: %s.", filename, err->message); return 0; }
// downscale oversized sticker images displayed in chat, otherwise it would harm readabillity
WebPDecoderConfig config;
WebPInitDecoderConfig (&config);
if (! WebPGetFeatures(data, len, &config.input) == VP8_STATUS_OK) {
warning ("error reading webp bitstream: %s", filename);
g_free ((gchar *)data);
return 0;
}
config.options.use_scaling = 0;
config.options.scaled_width = config.input.width;
config.options.scaled_height = config.input.height;
if (config.options.scaled_width > MAX_W || config.options.scaled_height > MAX_H) {
const float max_scale_width = MAX_W * 1.0f / config.options.scaled_width;
const float max_scale_height = MAX_H * 1.0f / config.options.scaled_height;
if (max_scale_width < max_scale_height) {
/* => the width is most limiting */
config.options.scaled_width = MAX_W;
/* Can't use ' *= ', because we need to do the multiplication in float
* (or double), and only THEN cast back to int. */
config.options.scaled_height = (int) (config.options.scaled_height * max_scale_width);
} else {
/* => the height is most limiting */
config.options.scaled_height = MAX_H;
/* Can't use ' *= ', because we need to do the multiplication in float
* (or double), and only THEN cast back to int. */
config.options.scaled_width = (int) (config.options.scaled_width * max_scale_height);
}
config.options.use_scaling = 1;
}
config.output.colorspace = MODE_RGBA;
if (! WebPDecode(data, len, &config) == VP8_STATUS_OK) {
warning ("error decoding webp: %s", filename);
g_free ((gchar *)data);
return 0;
}
g_free ((gchar *)data);
const uint8_t *decoded = config.output.u.RGBA.rgba;
// convert to png
unsigned char* png = NULL;
size_t pnglen;
unsigned error = lodepng_encode32 (&png, &pnglen, decoded, config.options.scaled_width, config.options.scaled_height);
WebPFreeDecBuffer (&config.output);
if (error) {
warning ("error encoding webp as png: %s", filename);
return 0;
}
// will be owned by libpurple imgstore, which uses glib functions for managing memory
void *pngdub = g_memdup (png, (guint)pnglen);
free (png);
int imgStoreId = purple_imgstore_add_with_id (pngdub, pnglen, NULL);
return imgStoreId;
}
#endif
void p2tgl_buddy_icons_set_for_user (PurpleAccount *pa, tgl_peer_id_t id, const char* filename) {
gchar *data = NULL;
size_t len;
GError *err = NULL;
g_file_get_contents (filename, &data, &len, &err);
purple_buddy_icons_set_for_user (pa, tgp_blist_peer_get_purple_name (pa_get_conn (pa)->TLS, id), data, len, NULL);
}