diff --git a/tgp-ft.c b/tgp-ft.c index 7d413eb..8ab57e2 100644 --- a/tgp-ft.c +++ b/tgp-ft.c @@ -26,9 +26,35 @@ #include #include #include +#include "telegram-purple.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) { + type = tgp_mime_to_filetype (mime); + } + if (!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()"); @@ -219,8 +245,12 @@ static PurpleXfer *tgprpl_new_xfer_recv (PurpleConnection * gc, const char *who) void tgprpl_recv_file (PurpleConnection * gc, const char *who, struct tgl_document *D) { debug ("tgprpl_recv_file()"); PurpleXfer *X = tgprpl_new_xfer_recv (gc, who); + + char *filename = tgp_strdup_determine_filename (D->mime_type, D->caption, D->flags, + D->access_hash); + purple_xfer_set_filename (X, filename); + g_free (filename); - purple_xfer_set_filename (X, D->caption ? D->caption : D->mime_type); tgprpl_xfer_init_data (X, purple_connection_get_protocol_data (gc), D, NULL); purple_xfer_request (X); } @@ -229,7 +259,11 @@ void tgprpl_recv_encr_file (PurpleConnection * gc, const char *who, struct tgl_e debug ("tgprpl_recv_encr_file()"); PurpleXfer *X = tgprpl_new_xfer_recv (gc, who); - purple_xfer_set_filename (X, D->caption ? D->caption : D->mime_type); + char *filename = tgp_strdup_determine_filename (D->mime_type, D->caption, D->flags, + D->access_hash); + purple_xfer_set_filename (X, filename); + g_free (filename); + tgprpl_xfer_init_data (X, purple_connection_get_protocol_data (gc), NULL, D); purple_xfer_request (X); } diff --git a/tgp-utils.c b/tgp-utils.c index 5d10449..caf737c 100644 --- a/tgp-utils.c +++ b/tgp-utils.c @@ -124,3 +124,14 @@ void tgp_g_list_free_full (GList *list, GDestroyNotify free_func) { g_list_foreach (list, (GFunc)free_func, NULL); g_list_free (list); } + +const char *tgp_mime_to_filetype (const char *mime) { + int len = (int) strlen (mime); + int i; + for (i = 0; i < len - 1; i ++) { + if (mime[i] == '/') { + return mime + i + 1; + } + } + return NULL; +} diff --git a/tgp-utils.h b/tgp-utils.h index b11ca00..c27f0d4 100644 --- a/tgp-utils.h +++ b/tgp-utils.h @@ -49,5 +49,6 @@ long tgp_time_n_days_ago (int days); char *tgp_g_format_size (gint64 size); void tgp_g_queue_free_full (GQueue *queue, GDestroyNotify free_func); void tgp_g_list_free_full (GList *list, GDestroyNotify free_func); +const char *tgp_mime_to_filetype (const char *mime); #endif