2014-11-16 20:41:03 +01:00
|
|
|
/*
|
|
|
|
This file is part of telegram-purple
|
2014-11-16 16:33:27 +01:00
|
|
|
|
2014-11-16 20:41:03 +01:00
|
|
|
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-05-24 00:22:31 +02:00
|
|
|
|
2015-10-04 16:58:25 +02:00
|
|
|
#include "telegram-purple.h"
|
2015-05-24 00:22:31 +02:00
|
|
|
|
2015-06-04 14:27:49 +02:00
|
|
|
#ifdef HAVE_LIBWEBP
|
|
|
|
#include <webp/decode.h>
|
|
|
|
#endif
|
|
|
|
|
2015-11-21 22:14:57 +01:00
|
|
|
PurpleAccount *tls_get_pa (struct tgl_state *TLS) {
|
|
|
|
return tls_get_data (TLS)->pa;
|
2014-11-16 16:33:27 +01:00
|
|
|
}
|
|
|
|
|
2015-11-21 22:14:57 +01:00
|
|
|
PurpleConnection *tls_get_conn (struct tgl_state *TLS) {
|
|
|
|
return tls_get_data (TLS)->gc;
|
2015-10-11 14:17:24 +02:00
|
|
|
}
|
|
|
|
|
2015-11-21 22:14:57 +01:00
|
|
|
connection_data *tls_get_data (struct tgl_state *TLS) {
|
2015-10-11 14:17:24 +02:00
|
|
|
return TLS->ev_base;
|
2014-11-16 16:33:27 +01:00
|
|
|
}
|
|
|
|
|
2015-11-21 22:14:57 +01:00
|
|
|
connection_data *gc_get_data (PurpleConnection *gc) {
|
2015-10-11 14:17:24 +02:00
|
|
|
return purple_connection_get_protocol_data (gc);
|
2014-11-16 16:33:27 +01:00
|
|
|
}
|
|
|
|
|
2015-11-21 22:14:57 +01:00
|
|
|
struct tgl_state *gc_get_tls (PurpleConnection *gc) {
|
|
|
|
return ((connection_data *)purple_connection_get_protocol_data (gc))->TLS;
|
|
|
|
}
|
|
|
|
|
|
|
|
connection_data *pa_get_data (PurpleAccount *pa) {
|
2015-10-11 14:17:24 +02:00
|
|
|
return purple_connection_get_protocol_data (purple_account_get_connection (pa));
|
2014-11-16 16:33:27 +01:00
|
|
|
}
|
|
|
|
|
2015-11-21 22:14:57 +01:00
|
|
|
connection_data *pbn_get_data (PurpleBlistNode *node) {
|
2015-10-11 14:17:24 +02:00
|
|
|
if (PURPLE_BLIST_NODE_IS_CHAT (node)) {
|
2015-11-21 22:14:57 +01:00
|
|
|
return pa_get_data (purple_chat_get_account((PurpleChat *) node));
|
2015-10-11 14:17:24 +02:00
|
|
|
}
|
|
|
|
if (PURPLE_BLIST_NODE_IS_BUDDY (node)) {
|
2015-11-21 22:14:57 +01:00
|
|
|
return pa_get_data (purple_buddy_get_account((PurpleBuddy *) node));
|
2015-10-11 14:17:24 +02:00
|
|
|
}
|
|
|
|
return NULL;
|
2014-11-16 16:33:27 +01:00
|
|
|
}
|
|
|
|
|
2015-05-30 15:52:17 +02:00
|
|
|
int p2tgl_status_is_present (PurpleStatus *status) {
|
2014-12-24 02:44:10 +01:00
|
|
|
const char *name = purple_status_get_id (status);
|
|
|
|
return !(strcmp (name, "unavailable") == 0 || strcmp (name, "away") == 0);
|
|
|
|
}
|
|
|
|
|
2015-11-26 00:02:36 +01:00
|
|
|
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) {
|
2015-12-28 17:44:49 +01:00
|
|
|
serv_got_chat_in (tls_get_conn (TLS), tgl_get_peer_id (chat), tgp_blist_lookup_purple_name (TLS, who), flags,
|
2015-11-26 00:02:36 +01:00
|
|
|
message, when);
|
2015-03-21 00:56:32 +01:00
|
|
|
}
|
|
|
|
|
2015-01-28 18:51:07 +01:00
|
|
|
void p2tgl_got_im_combo (struct tgl_state *TLS, tgl_peer_id_t who, const char *msg, int flags, time_t when) {
|
2015-10-11 14:17:24 +02:00
|
|
|
connection_data *conn = TLS->ev_base;
|
2015-01-28 18:51:07 +01:00
|
|
|
|
2015-10-16 00:33:23 +02:00
|
|
|
if (flags & PURPLE_MESSAGE_SYSTEM) {
|
|
|
|
tgp_msg_sys_out (TLS, msg, who, flags & PURPLE_MESSAGE_NO_LOG);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2015-01-28 18:51:07 +01:00
|
|
|
Outgoing messages are not well supported in different libpurple clients,
|
2015-10-11 14:17:24 +02:00
|
|
|
purple_conv_im_write should have the best among different versions. Unfortunately
|
2015-01-28 18:51:07 +01:00
|
|
|
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) {
|
2015-11-25 11:31:07 +01:00
|
|
|
conv = purple_conversation_new (PURPLE_CONV_TYPE_IM, tls_get_pa (TLS),
|
2015-12-28 17:44:49 +01:00
|
|
|
tgp_blist_lookup_purple_name (TLS, who));
|
2015-01-28 18:51:07 +01:00
|
|
|
}
|
2015-12-28 17:44:49 +01:00
|
|
|
purple_conv_im_write (purple_conversation_get_im_data (conv), tgp_blist_lookup_purple_name (TLS, who),
|
|
|
|
msg, PURPLE_MESSAGE_SEND, when);
|
2015-01-28 18:51:07 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
2015-12-28 17:44:49 +01:00
|
|
|
serv_got_im (conn->gc, tgp_blist_lookup_purple_name (TLS, who), msg, flags, when);
|
2014-11-16 16:33:27 +01:00
|
|
|
}
|
|
|
|
|
2015-01-28 18:51:07 +01:00
|
|
|
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;
|
|
|
|
}
|
2015-12-28 17:44:49 +01:00
|
|
|
PurpleConversation *conv = purple_find_conversation_with_account (type, tgp_blist_lookup_purple_name (TLS, peer),
|
2015-11-21 22:14:57 +01:00
|
|
|
tls_get_pa (TLS));
|
2015-01-28 18:51:07 +01:00
|
|
|
return conv;
|
|
|
|
}
|
|
|
|
|
2014-11-16 16:33:27 +01:00
|
|
|
void p2tgl_prpl_got_user_status (struct tgl_state *TLS, tgl_peer_id_t user, struct tgl_user_status *status) {
|
2015-03-13 16:57:08 +01:00
|
|
|
connection_data *data = TLS->ev_base;
|
|
|
|
|
2014-11-17 16:27:01 +03:00
|
|
|
if (status->online == 1) {
|
2015-12-28 17:44:49 +01:00
|
|
|
purple_prpl_got_user_status (tls_get_pa (TLS), tgp_blist_lookup_purple_name (TLS, user), "available", NULL);
|
2014-11-17 16:27:01 +03:00
|
|
|
} else {
|
2015-10-02 17:02:03 +02:00
|
|
|
debug ("%d: when=%d", tgl_get_peer_id (user), status->when);
|
2015-10-31 21:49:56 +01:00
|
|
|
if (tgp_time_n_days_ago (
|
|
|
|
purple_account_get_int (data->pa, "inactive-days-offline", TGP_DEFAULT_INACTIVE_DAYS_OFFLINE)) > status->when && status->when) {
|
2015-03-13 16:57:08 +01:00
|
|
|
debug ("offline");
|
2015-12-28 17:44:49 +01:00
|
|
|
purple_prpl_got_user_status (tls_get_pa (TLS), tgp_blist_lookup_purple_name (TLS, user), "offline", NULL);
|
2015-10-11 14:17:24 +02:00
|
|
|
} else {
|
2015-03-13 16:57:08 +01:00
|
|
|
debug ("mobile");
|
2015-12-28 17:44:49 +01:00
|
|
|
purple_prpl_got_user_status (tls_get_pa (TLS), tgp_blist_lookup_purple_name (TLS, user), "mobile", NULL);
|
2015-03-13 16:57:08 +01:00
|
|
|
}
|
2014-11-17 16:27:01 +03:00
|
|
|
}
|
2014-11-16 16:33:27 +01:00
|
|
|
}
|
|
|
|
|
2015-11-26 00:02:36 +01:00
|
|
|
void p2tgl_conv_add_user (struct tgl_state *TLS, PurpleConversation *conv, int user, char *message, int flags,
|
|
|
|
int new_arrival) {
|
2015-12-28 17:44:49 +01:00
|
|
|
const char *name = tgp_blist_lookup_purple_name (TLS, TGL_MK_USER (user));
|
2015-10-31 09:50:40 +01:00
|
|
|
g_return_if_fail (name);
|
|
|
|
purple_conv_chat_add_user (purple_conversation_get_chat_data (conv), name, message, flags, new_arrival);
|
2014-11-23 19:52:08 +01:00
|
|
|
}
|
2015-01-21 23:49:42 +01:00
|
|
|
|
|
|
|
PurpleNotifyUserInfo *p2tgl_notify_user_info_new (struct tgl_user *U) {
|
|
|
|
PurpleNotifyUserInfo *info = purple_notify_user_info_new();
|
2015-10-17 18:09:42 +02:00
|
|
|
|
2015-10-11 14:17:24 +02:00
|
|
|
if (str_not_empty (U->first_name) && str_not_empty (U->last_name)) {
|
2015-10-07 21:23:03 +02:00
|
|
|
purple_notify_user_info_add_pair (info, _("First name"), U->first_name);
|
|
|
|
purple_notify_user_info_add_pair (info, _("Last name"), U->last_name);
|
2015-01-21 23:49:42 +01:00
|
|
|
} else {
|
2015-10-04 17:12:04 +02:00
|
|
|
purple_notify_user_info_add_pair (info, _("Name"), U->print_name);
|
2015-01-21 23:49:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (str_not_empty (U->username)) {
|
2015-10-06 16:12:37 +02:00
|
|
|
char *username = g_strdup_printf ("@%s", U->username);
|
2015-10-07 21:23:03 +02:00
|
|
|
purple_notify_user_info_add_pair (info, _("Username"), username);
|
2015-10-06 16:12:37 +02:00
|
|
|
g_free (username);
|
2015-01-21 23:49:42 +01:00
|
|
|
}
|
|
|
|
|
2015-03-13 16:57:08 +01:00
|
|
|
char *status = tgp_format_user_status (&U->status);
|
2015-10-04 17:12:04 +02:00
|
|
|
purple_notify_user_info_add_pair (info, _("Last seen"), status);
|
2015-01-21 23:49:42 +01:00
|
|
|
g_free (status);
|
|
|
|
|
|
|
|
if (str_not_empty (U->phone)) {
|
2015-10-11 14:17:24 +02:00
|
|
|
char *phone = g_strdup_printf ("+%s", U->phone);
|
2015-10-04 17:12:04 +02:00
|
|
|
purple_notify_user_info_add_pair (info, _("Phone"), phone);
|
2015-01-21 23:49:42 +01:00
|
|
|
g_free (phone);
|
|
|
|
}
|
|
|
|
|
|
|
|
return info;
|
|
|
|
}
|
|
|
|
|
2015-10-11 14:17:24 +02:00
|
|
|
PurpleNotifyUserInfo *p2tgl_notify_encrypted_chat_info_new (struct tgl_state *TLS, struct tgl_secret_chat *secret,
|
2015-11-26 00:02:36 +01:00
|
|
|
struct tgl_user *U) {
|
2015-01-21 23:49:42 +01:00
|
|
|
PurpleNotifyUserInfo *info = p2tgl_notify_user_info_new (U);
|
|
|
|
|
|
|
|
if (secret->state == sc_waiting) {
|
2015-11-21 14:37:11 +01:00
|
|
|
purple_notify_user_info_add_pair (info, "", _("Waiting for the user to get online..."));
|
2015-01-21 23:49:42 +01:00
|
|
|
return info;
|
|
|
|
}
|
|
|
|
|
2015-10-07 21:23:03 +02:00
|
|
|
const char *ttl_key = _("Self destruction timer");
|
2015-01-21 23:49:42 +01:00
|
|
|
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 {
|
2015-10-19 19:04:30 +02:00
|
|
|
purple_notify_user_info_add_pair (info, ttl_key, _("Timer is not enabled."));
|
2015-01-21 23:49:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (secret->first_key_sha[0]) {
|
2015-05-24 00:23:28 +02:00
|
|
|
int sha1key_store_id = tgp_visualize_key (TLS, secret->first_key_sha);
|
2015-01-21 23:49:42 +01:00
|
|
|
if (sha1key_store_id != -1) {
|
2015-07-30 11:45:50 +02:00
|
|
|
char *ident_icon = tgp_format_img (sha1key_store_id);
|
2015-10-04 17:12:04 +02:00
|
|
|
purple_notify_user_info_add_pair (info, _("Secret key"), ident_icon);
|
2015-01-21 23:49:42 +01:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
case TGL_PEER_USER:
|
|
|
|
return p2tgl_notify_user_info_new (&P->user);
|
|
|
|
|
|
|
|
default:
|
|
|
|
return purple_notify_user_info_new ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-23 20:56:50 +01:00
|
|
|
int p2tgl_imgstore_add_with_id (const char* filename) {
|
2015-01-21 23:49:42 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-11-16 16:00:09 +01:00
|
|
|
int p2tgl_imgstore_add_with_id_raw (const unsigned char *raw_bgra, unsigned width, unsigned height) {
|
2015-11-28 20:17:47 +01:00
|
|
|
// Heavily inspired by: https://github.com/EionRobb/pidgin-opensteamworks/blob/master/libsteamworks.cpp#L113
|
2015-11-16 09:12:18 +01:00
|
|
|
const unsigned char tga_header[] = {
|
2015-11-28 20:17:47 +01:00
|
|
|
// No ID; no color map; uncompressed true color
|
2015-11-16 09:12:18 +01:00
|
|
|
0,0,2,
|
2015-11-28 20:17:47 +01:00
|
|
|
// No color map metadata
|
2015-11-16 09:12:18 +01:00
|
|
|
0,0,0,0,0,
|
2015-11-28 20:17:47 +01:00
|
|
|
// No offsets
|
2015-11-16 09:12:18 +01:00
|
|
|
0,0,0,0,
|
2015-11-28 20:17:47 +01:00
|
|
|
// Dimensions
|
2015-11-16 09:12:18 +01:00
|
|
|
width&0xFF,(width/256)&0xFF,height&0xFF,(height/256)&0xFF,
|
2015-11-28 20:17:47 +01:00
|
|
|
// 32 bits per pixel
|
2015-11-16 09:12:18 +01:00
|
|
|
32,
|
2015-11-28 20:17:47 +01:00
|
|
|
// "Origin in upper left-hand corner"
|
2015-11-16 09:12:18 +01:00
|
|
|
32};
|
|
|
|
// Will be owned by libpurple imgstore, which uses glib functions for managing memory
|
|
|
|
const unsigned tga_len = sizeof(tga_header) + width * height * 4;
|
|
|
|
unsigned char *tga = g_malloc(tga_len);
|
|
|
|
memcpy(tga, tga_header, sizeof(tga_header));
|
2015-11-28 20:17:47 +01:00
|
|
|
// From the documentation: "The 4 byte entry contains 1 byte each of blue, green, red, and attribute."
|
2015-11-16 16:00:09 +01:00
|
|
|
memcpy(tga + sizeof(tga_header), raw_bgra, width * height * 4);
|
2015-11-16 09:12:18 +01:00
|
|
|
return purple_imgstore_add_with_id (tga, tga_len, NULL);
|
|
|
|
}
|
|
|
|
|
2015-05-24 00:22:31 +02:00
|
|
|
#ifdef HAVE_LIBWEBP
|
2015-10-08 17:28:48 +02:00
|
|
|
|
|
|
|
static const int MAX_W = 256;
|
|
|
|
static const int MAX_H = 256;
|
|
|
|
|
2015-05-24 00:22:31 +02:00
|
|
|
int p2tgl_imgstore_add_with_id_webp (const char *filename) {
|
2015-06-04 14:27:49 +02:00
|
|
|
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;
|
|
|
|
}
|
2015-10-08 17:28:48 +02:00
|
|
|
|
|
|
|
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) {
|
2015-11-28 20:17:47 +01:00
|
|
|
// => the width is most limiting
|
2015-10-08 17:28:48 +02:00
|
|
|
config.options.scaled_width = MAX_W;
|
2015-11-28 20:17:47 +01:00
|
|
|
// Can't use ' *= ', because we need to do the multiplication in float
|
|
|
|
// (or double), and only THEN cast back to int.
|
2015-10-08 17:28:48 +02:00
|
|
|
config.options.scaled_height = (int) (config.options.scaled_height * max_scale_width);
|
|
|
|
} else {
|
2015-11-28 20:17:47 +01:00
|
|
|
// => the height is most limiting
|
2015-10-08 17:28:48 +02:00
|
|
|
config.options.scaled_height = MAX_H;
|
2015-11-28 20:17:47 +01:00
|
|
|
// Can't use ' *= ', because we need to do the multiplication in float
|
|
|
|
// (or double), and only THEN cast back to int.
|
2015-10-08 17:28:48 +02:00
|
|
|
config.options.scaled_width = (int) (config.options.scaled_width * max_scale_height);
|
|
|
|
}
|
|
|
|
config.options.use_scaling = 1;
|
2015-06-04 14:27:49 +02:00
|
|
|
}
|
2015-11-16 16:00:09 +01:00
|
|
|
config.output.colorspace = MODE_BGRA;
|
2015-06-04 14:27:49 +02:00
|
|
|
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;
|
|
|
|
|
2015-11-16 09:24:20 +01:00
|
|
|
// convert and add
|
|
|
|
int imgStoreId = p2tgl_imgstore_add_with_id_raw(decoded, config.options.scaled_width, config.options.scaled_height);
|
2015-06-04 14:27:49 +02:00
|
|
|
WebPFreeDecBuffer (&config.output);
|
2015-05-24 00:22:31 +02:00
|
|
|
return imgStoreId;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-10-11 14:17:24 +02:00
|
|
|
void p2tgl_buddy_icons_set_for_user (PurpleAccount *pa, tgl_peer_id_t id, const char* filename) {
|
2015-01-21 23:49:42 +01:00
|
|
|
gchar *data = NULL;
|
|
|
|
size_t len;
|
|
|
|
GError *err = NULL;
|
|
|
|
g_file_get_contents (filename, &data, &len, &err);
|
2015-12-28 17:44:49 +01:00
|
|
|
purple_buddy_icons_set_for_user (pa, tgp_blist_lookup_purple_name (pa_get_data (pa)->TLS, id), data, len, NULL);
|
2015-01-21 23:49:42 +01:00
|
|
|
}
|
|
|
|
|