2015-04-04 09:26:38 +02:00
|
|
|
/* avatars.c
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* Copyright (C) 2015 Toxic All Rights Reserved.
|
|
|
|
*
|
|
|
|
* This file is part of Toxic.
|
|
|
|
*
|
|
|
|
* Toxic 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 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Toxic 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 Toxic. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "misc_tools.h"
|
|
|
|
#include "file_transfers.h"
|
|
|
|
#include "friendlist.h"
|
2015-04-05 03:15:34 +02:00
|
|
|
#include "avatars.h"
|
2015-04-04 09:26:38 +02:00
|
|
|
|
|
|
|
extern FriendsList Friends;
|
|
|
|
|
|
|
|
static struct Avatar {
|
2015-04-09 08:14:23 +02:00
|
|
|
char name[TOX_MAX_FILENAME_LENGTH + 1];
|
2015-04-04 09:26:38 +02:00
|
|
|
size_t name_len;
|
|
|
|
char path[PATH_MAX + 1];
|
|
|
|
size_t path_len;
|
|
|
|
off_t size;
|
|
|
|
} Avatar;
|
|
|
|
|
2020-11-01 21:55:07 +01:00
|
|
|
/* Compares the first size bytes of fp to signature.
|
|
|
|
*
|
|
|
|
* Returns 0 if they are the same
|
|
|
|
* Returns 1 if they differ
|
|
|
|
* Returns -1 on error.
|
|
|
|
*
|
|
|
|
* On success this function will seek back to the beginning of fp.
|
|
|
|
*/
|
|
|
|
static int check_file_signature(const unsigned char *signature, size_t size, FILE *fp)
|
|
|
|
{
|
|
|
|
char *buf = malloc(size);
|
|
|
|
|
|
|
|
if (buf == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fread(buf, size, 1, fp) != 1) {
|
|
|
|
free(buf);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ret = memcmp(signature, buf, size);
|
|
|
|
|
|
|
|
free(buf);
|
|
|
|
|
|
|
|
if (fseek(fp, 0L, SEEK_SET) == -1) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret == 0 ? 0 : 1;
|
|
|
|
}
|
2015-04-04 09:26:38 +02:00
|
|
|
|
|
|
|
static void avatar_clear(void)
|
|
|
|
{
|
2020-11-11 03:25:27 +01:00
|
|
|
Avatar = (struct Avatar) {
|
|
|
|
0
|
|
|
|
};
|
2015-04-04 09:26:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Sends avatar to friendnum.
|
|
|
|
*
|
|
|
|
* Returns 0 on success.
|
|
|
|
* Returns -1 on failure.
|
|
|
|
*/
|
|
|
|
int avatar_send(Tox *m, uint32_t friendnum)
|
|
|
|
{
|
2018-10-10 05:06:57 +02:00
|
|
|
Tox_Err_File_Send err;
|
2015-04-04 09:26:38 +02:00
|
|
|
uint32_t filenum = tox_file_send(m, friendnum, TOX_FILE_KIND_AVATAR, (size_t) Avatar.size,
|
|
|
|
NULL, (uint8_t *) Avatar.name, Avatar.name_len, &err);
|
2016-09-25 03:07:04 +02:00
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
if (Avatar.size == 0) {
|
2015-04-04 09:26:38 +02:00
|
|
|
return 0;
|
2018-07-18 17:33:16 +02:00
|
|
|
}
|
2015-04-04 09:26:38 +02:00
|
|
|
|
|
|
|
if (err != TOX_ERR_FILE_SEND_OK) {
|
2020-11-03 00:08:54 +01:00
|
|
|
fprintf(stderr, "tox_file_send failed for friendnumber %u (error %d)\n", friendnum, err);
|
2015-04-04 09:26:38 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-08-18 21:12:48 +02:00
|
|
|
struct FileTransfer *ft = new_file_transfer(NULL, friendnum, filenum, FILE_TRANSFER_SEND, TOX_FILE_KIND_AVATAR);
|
2015-04-04 09:26:38 +02:00
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
if (!ft) {
|
2015-04-04 09:26:38 +02:00
|
|
|
return -1;
|
2018-07-18 17:33:16 +02:00
|
|
|
}
|
2015-04-04 09:26:38 +02:00
|
|
|
|
|
|
|
ft->file = fopen(Avatar.path, "r");
|
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
if (ft->file == NULL) {
|
2015-04-04 09:26:38 +02:00
|
|
|
return -1;
|
2018-07-18 17:33:16 +02:00
|
|
|
}
|
2015-04-04 09:26:38 +02:00
|
|
|
|
2015-04-09 08:14:23 +02:00
|
|
|
snprintf(ft->file_name, sizeof(ft->file_name), "%s", Avatar.name);
|
2015-04-05 03:15:34 +02:00
|
|
|
ft->file_size = Avatar.size;
|
2015-04-04 09:26:38 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Sends avatar to all friends */
|
|
|
|
static void avatar_send_all(Tox *m)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
for (i = 0; i < Friends.max_idx; ++i) {
|
2018-07-18 17:33:16 +02:00
|
|
|
if (Friends.list[i].connection_status != TOX_CONNECTION_NONE) {
|
2015-04-04 09:26:38 +02:00
|
|
|
avatar_send(m, Friends.list[i].num);
|
2018-07-18 17:33:16 +02:00
|
|
|
}
|
2015-04-04 09:26:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Sets avatar to path and sends it to all online contacts.
|
|
|
|
*
|
|
|
|
* Returns 0 on success.
|
|
|
|
* Returns -1 on failure.
|
|
|
|
*/
|
|
|
|
int avatar_set(Tox *m, const char *path, size_t path_len)
|
|
|
|
{
|
2018-07-18 17:33:16 +02:00
|
|
|
if (path_len == 0 || path_len >= sizeof(Avatar.path)) {
|
2015-04-04 09:26:38 +02:00
|
|
|
return -1;
|
2018-07-18 17:33:16 +02:00
|
|
|
}
|
2015-04-04 09:26:38 +02:00
|
|
|
|
|
|
|
FILE *fp = fopen(path, "rb");
|
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
if (fp == NULL) {
|
2015-04-04 09:26:38 +02:00
|
|
|
return -1;
|
2018-07-18 17:33:16 +02:00
|
|
|
}
|
2015-04-04 09:26:38 +02:00
|
|
|
|
2020-02-29 20:14:56 +01:00
|
|
|
unsigned char PNG_signature[8] = {0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A};
|
2015-04-04 09:26:38 +02:00
|
|
|
|
|
|
|
if (check_file_signature(PNG_signature, sizeof(PNG_signature), fp) != 0) {
|
|
|
|
fclose(fp);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(fp);
|
|
|
|
|
2015-04-05 03:15:34 +02:00
|
|
|
off_t size = file_size(path);
|
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
if (size == 0 || size > MAX_AVATAR_FILE_SIZE) {
|
2015-04-05 03:15:34 +02:00
|
|
|
return -1;
|
2018-07-18 17:33:16 +02:00
|
|
|
}
|
2015-04-05 03:15:34 +02:00
|
|
|
|
2015-04-04 09:26:38 +02:00
|
|
|
get_file_name(Avatar.name, sizeof(Avatar.name), path);
|
|
|
|
Avatar.name_len = strlen(Avatar.name);
|
2015-04-09 08:14:23 +02:00
|
|
|
snprintf(Avatar.path, sizeof(Avatar.path), "%s", path);
|
2015-04-04 09:26:38 +02:00
|
|
|
Avatar.path_len = path_len;
|
2015-04-05 03:15:34 +02:00
|
|
|
Avatar.size = size;
|
2015-04-04 09:26:38 +02:00
|
|
|
|
|
|
|
avatar_send_all(m);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Unsets avatar and sends to all online contacts.
|
|
|
|
*
|
|
|
|
* Returns 0 on success.
|
|
|
|
* Returns -1 on failure.
|
|
|
|
*/
|
|
|
|
void avatar_unset(Tox *m)
|
|
|
|
{
|
|
|
|
avatar_clear();
|
|
|
|
avatar_send_all(m);
|
|
|
|
}
|
|
|
|
|
2018-10-10 05:06:57 +02:00
|
|
|
void on_avatar_file_control(Tox *m, struct FileTransfer *ft, Tox_File_Control control)
|
2015-04-04 09:26:38 +02:00
|
|
|
{
|
|
|
|
switch (control) {
|
|
|
|
case TOX_FILE_CONTROL_RESUME:
|
|
|
|
if (ft->state == FILE_TRANSFER_PENDING) {
|
|
|
|
ft->state = FILE_TRANSFER_STARTED;
|
|
|
|
} else if (ft->state == FILE_TRANSFER_PAUSED) {
|
|
|
|
ft->state = FILE_TRANSFER_STARTED;
|
|
|
|
}
|
2016-09-25 03:07:04 +02:00
|
|
|
|
2015-04-04 09:26:38 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case TOX_FILE_CONTROL_PAUSE:
|
|
|
|
ft->state = FILE_TRANSFER_PAUSED;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TOX_FILE_CONTROL_CANCEL:
|
|
|
|
close_file_transfer(NULL, m, ft, -1, NULL, silent);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void on_avatar_chunk_request(Tox *m, struct FileTransfer *ft, uint64_t position, size_t length)
|
|
|
|
{
|
2018-07-18 17:33:16 +02:00
|
|
|
if (ft->state != FILE_TRANSFER_STARTED) {
|
2015-04-04 09:26:38 +02:00
|
|
|
return;
|
2018-07-18 17:33:16 +02:00
|
|
|
}
|
2015-04-04 09:26:38 +02:00
|
|
|
|
|
|
|
if (length == 0) {
|
|
|
|
close_file_transfer(NULL, m, ft, -1, NULL, silent);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ft->file == NULL) {
|
|
|
|
close_file_transfer(NULL, m, ft, TOX_FILE_CONTROL_CANCEL, NULL, silent);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ft->position != position) {
|
|
|
|
if (fseek(ft->file, position, SEEK_SET) == -1) {
|
|
|
|
close_file_transfer(NULL, m, ft, TOX_FILE_CONTROL_CANCEL, NULL, silent);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ft->position = position;
|
|
|
|
}
|
|
|
|
|
2020-10-28 05:54:06 +01:00
|
|
|
uint8_t *send_data = malloc(length);
|
|
|
|
|
|
|
|
if (send_data == NULL) {
|
|
|
|
close_file_transfer(NULL, m, ft, TOX_FILE_CONTROL_CANCEL, NULL, silent);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t send_length = fread(send_data, 1, length, ft->file);
|
2015-04-04 09:26:38 +02:00
|
|
|
|
|
|
|
if (send_length != length) {
|
|
|
|
close_file_transfer(NULL, m, ft, TOX_FILE_CONTROL_CANCEL, NULL, silent);
|
2020-10-28 05:54:06 +01:00
|
|
|
free(send_data);
|
2015-04-04 09:26:38 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-10-10 05:06:57 +02:00
|
|
|
Tox_Err_File_Send_Chunk err;
|
2015-04-04 09:26:38 +02:00
|
|
|
tox_file_send_chunk(m, ft->friendnum, ft->filenum, position, send_data, send_length, &err);
|
|
|
|
|
2020-10-28 05:54:06 +01:00
|
|
|
free(send_data);
|
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
if (err != TOX_ERR_FILE_SEND_CHUNK_OK) {
|
2015-07-04 07:19:16 +02:00
|
|
|
fprintf(stderr, "tox_file_send_chunk failed in avatar callback (error %d)\n", err);
|
2018-07-18 17:33:16 +02:00
|
|
|
}
|
2015-04-04 09:26:38 +02:00
|
|
|
|
|
|
|
ft->position += send_length;
|
2015-08-18 21:12:48 +02:00
|
|
|
ft->last_keep_alive = get_unix_time();
|
2015-04-04 09:26:38 +02:00
|
|
|
}
|