clang-format

This commit is contained in:
Håvard Pettersson 2018-12-22 21:16:45 +01:00
parent 4ca6ae3169
commit d0ab7b0139
10 changed files with 402 additions and 340 deletions

View File

@ -19,21 +19,21 @@
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <unistd.h>
#include <wordexp.h>
#include <tox/tox.h>
#include <weechat/weechat-plugin.h>
#include "twc-bootstrap.h"
#include "twc-tfer.h"
#include "twc-chat.h"
#include "twc-config.h"
#include "twc-friend-request.h"
#include "twc-group-invite.h"
#include "twc-list.h"
#include "twc-profile.h"
#include "twc-tfer.h"
#include "twc-utils.h"
#include "twc.h"
@ -52,8 +52,9 @@ enum TWC_FRIEND_MATCH
#define TWC_CHECK_PROFILE(profile) \
if (!profile) \
{ \
weechat_printf(NULL, "%s%s: command \"%s\" must be executed on a Tox " \
"buffer", \
weechat_printf(NULL, \
"%s%s: command \"%s\" must be executed on a Tox " \
"buffer", \
weechat_prefix("error"), weechat_plugin->name, \
argv[0]); \
return WEECHAT_RC_OK; \
@ -66,10 +67,11 @@ enum TWC_FRIEND_MATCH
#define TWC_CHECK_CHAT(chat) \
if (!chat) \
{ \
weechat_printf( \
NULL, "%s%s: command \"%s\" must be executed in a chat " \
"buffer", \
weechat_prefix("error"), weechat_plugin->name, argv[0]); \
weechat_printf(NULL, \
"%s%s: command \"%s\" must be executed in a chat " \
"buffer", \
weechat_prefix("error"), weechat_plugin->name, \
argv[0]); \
return WEECHAT_RC_OK; \
}
@ -80,10 +82,11 @@ enum TWC_FRIEND_MATCH
#define TWC_CHECK_GROUP_CHAT(chat) \
if (!chat || chat->group_number < 0) \
{ \
weechat_printf( \
NULL, "%s%s: command \"%s\" must be executed in a group " \
"chat buffer ", \
weechat_prefix("error"), weechat_plugin->name, argv[0]); \
weechat_printf(NULL, \
"%s%s: command \"%s\" must be executed in a group " \
"chat buffer ", \
weechat_prefix("error"), weechat_plugin->name, \
argv[0]); \
return WEECHAT_RC_OK; \
}
@ -135,7 +138,7 @@ enum TWC_FRIEND_MATCH
* Make sure a file exists.
*/
#define TWC_CHECK_FILE_EXISTS(filename) \
if(access(filename, F_OK) == -1 ) \
if (access(filename, F_OK) == -1) \
{ \
weechat_printf(NULL, "%sFile \"%s\" does not exist", \
weechat_prefix("error"), filename); \
@ -143,8 +146,10 @@ enum TWC_FRIEND_MATCH
}
#define TWC_RETURN_WITH_FILE_ERROR(filename, type) \
weechat_printf(NULL, "%s\"%s\" must be a regular file or pipe, " \
"not a %s", weechat_prefix("error"), filename, type); \
weechat_printf(NULL, \
"%s\"%s\" must be a regular file or pipe, " \
"not a %s", \
weechat_prefix("error"), filename, type); \
return WEECHAT_RC_ERROR;
/**
@ -503,8 +508,8 @@ twc_cmd_friend(const void *pointer, void *data, struct t_gui_buffer *buffer,
/* /friend requests */
else if (argc == 2 && weechat_strcasecmp(argv[1], "requests") == 0)
{
weechat_printf(profile->buffer, "%sPending friend requests:",
weechat_prefix("network"));
weechat_printf(profile->buffer,
"%sPending friend requests:", weechat_prefix("network"));
size_t index;
struct t_twc_list_item *item;
@ -514,8 +519,9 @@ twc_cmd_friend(const void *pointer, void *data, struct t_gui_buffer *buffer,
twc_bin2hex(item->friend_request->tox_id, TOX_PUBLIC_KEY_SIZE,
hex_address);
weechat_printf(profile->buffer, "%s[%d] Address: %s\n"
"[%d] Message: %s",
weechat_printf(profile->buffer,
"%s[%d] Address: %s\n"
"[%d] Message: %s",
weechat_prefix("network"), index, hex_address, index,
item->friend_request->message);
}
@ -1142,8 +1148,9 @@ twc_cmd_tox(const void *pointer, void *data, struct t_gui_buffer *buffer,
}
else
{
weechat_printf(NULL, "%s%s: You must confirm deletion with either "
"\"-keepdata\" or \"-yes\" (see /help tox)",
weechat_printf(NULL,
"%s%s: You must confirm deletion with either "
"\"-keepdata\" or \"-yes\" (see /help tox)",
weechat_prefix("error"), weechat_plugin->name);
return WEECHAT_RC_OK;
}
@ -1200,7 +1207,7 @@ twc_cmd_tox(const void *pointer, void *data, struct t_gui_buffer *buffer,
*/
int
twc_cmd_send(const void *pointer, void *data, struct t_gui_buffer *buffer,
int argc, char **argv, char **argv_eol)
int argc, char **argv, char **argv_eol)
{
if (argc == 1)
return WEECHAT_RC_ERROR;
@ -1217,14 +1224,17 @@ twc_cmd_send(const void *pointer, void *data, struct t_gui_buffer *buffer,
{
if (profile->buffer == buffer || profile->tfer->buffer == buffer)
{
weechat_printf(profile->buffer, "%s%s", weechat_prefix("error"), "you must specify a friend");
weechat_printf(profile->buffer, "%s%s", weechat_prefix("error"),
"you must specify a friend");
return WEECHAT_RC_ERROR;
}
snprintf(recipient, TOX_MAX_NAME_LENGTH, "%s", weechat_buffer_get_string(buffer, "name"));
snprintf(recipient, TOX_MAX_NAME_LENGTH, "%s",
weechat_buffer_get_string(buffer, "name"));
struct t_twc_chat *chat = twc_chat_search_buffer(buffer);
if (chat->group_number != -1)
{
weechat_printf(profile->buffer, "%s%s", weechat_prefix("error"), "the file transmission is "
weechat_printf(profile->buffer, "%s%s", weechat_prefix("error"),
"the file transmission is "
"allowed only between friends");
return WEECHAT_RC_ERROR;
}
@ -1238,11 +1248,12 @@ twc_cmd_send(const void *pointer, void *data, struct t_gui_buffer *buffer,
/* /send <number>|<name>|<Tox ID> <file> */
if (argc >= 3)
{
/* do a shell split in case a friend has spaces in his name
/* do a shell split in case a friend has spaces in his name
* and join the name */
int shell_argc;
char **shell_argv = weechat_string_split_shell(argv_eol[1], &shell_argc);
for (int i = 0; i < shell_argc -1; i++)
char **shell_argv =
weechat_string_split_shell(argv_eol[1], &shell_argc);
for (int i = 0; i < shell_argc - 1; i++)
{
strcat(recipient, shell_argv[i]);
if (i < shell_argc - 2)
@ -1258,7 +1269,7 @@ twc_cmd_send(const void *pointer, void *data, struct t_gui_buffer *buffer,
TWC_CHECK_FILE_EXISTS(filename);
uint32_t friend_number = twc_match_friend(profile, recipient);
TWC_CHECK_FRIEND_NUMBER(profile, (signed) friend_number, recipient);
TWC_CHECK_FRIEND_NUMBER(profile, (signed)friend_number, recipient);
struct stat st;
stat(filename, &st);
@ -1277,30 +1288,34 @@ twc_cmd_send(const void *pointer, void *data, struct t_gui_buffer *buffer,
case S_IFLNK:
break;
default:
weechat_printf(NULL, "%sunknown file type", weechat_prefix("error"));
weechat_printf(NULL, "%sunknown file type",
weechat_prefix("error"));
return WEECHAT_RC_ERROR;
}
char *stripped_name = twc_tfer_file_name_strip(filename, FILENAME_MAX + 1 - strlen(filename));
char *stripped_name =
twc_tfer_file_name_strip(filename, FILENAME_MAX + 1 - strlen(filename));
TOX_ERR_FILE_SEND error;
uint32_t file_number = tox_file_send(profile->tox, friend_number, TOX_FILE_KIND_DATA,
S_ISFIFO(st.st_mode) ? UINT64_MAX : (size_t)st.st_size,
NULL, (uint8_t *)stripped_name, strlen(filename), &error);
uint32_t file_number =
tox_file_send(profile->tox, friend_number, TOX_FILE_KIND_DATA,
S_ISFIFO(st.st_mode) ? UINT64_MAX : (size_t)st.st_size,
NULL, (uint8_t *)stripped_name, strlen(filename), &error);
free(stripped_name);
if (error != TOX_ERR_FILE_SEND_OK)
{
weechat_printf(profile->buffer, "%ssending \"%s\" has been failed: %s",
weechat_prefix("error"), filename, twc_tox_err_file_send(error));
weechat_prefix("error"), filename,
twc_tox_err_file_send(error));
return WEECHAT_RC_ERROR;
}
if (!(profile->tfer->buffer))
{
twc_tfer_load(profile);
}
struct t_twc_tfer_file *file = twc_tfer_file_new(profile, recipient, filename,
friend_number, file_number,
st.st_size, TWC_TFER_FILE_TYPE_UPLOADING);
struct t_twc_tfer_file *file = twc_tfer_file_new(
profile, recipient, filename, friend_number, file_number, st.st_size,
TWC_TFER_FILE_TYPE_UPLOADING);
if (!file)
{
weechat_printf(profile->buffer, "%scannot open the file \"%s\"",
@ -1347,10 +1362,11 @@ twc_commands_init()
" || decline",
twc_cmd_friend, NULL, NULL);
weechat_hook_command("group", "manage group chats", "create"
" || invites"
" || join <number>"
" || decline <number>",
weechat_hook_command("group", "manage group chats",
"create"
" || invites"
" || join <number>"
" || decline <number>",
" create: create a new group chat\n"
"invites: list group chat invites\n"
" join: join a group chat by its invite ID\n"
@ -1416,12 +1432,13 @@ twc_commands_init()
NULL, NULL);
weechat_hook_command(
"tox", "manage Tox profiles", "list"
" || create <name>"
" || delete <name> -yes|-keepdata"
" || load [<name>...]"
" || unload [<name>...]"
" || reload [<name>...]",
"tox", "manage Tox profiles",
"list"
" || create <name>"
" || delete <name> -yes|-keepdata"
" || load [<name>...]"
" || unload [<name>...]"
" || reload [<name>...]",
" list: list all Tox profile\n"
"create: create a new Tox profile\n"
"delete: delete a Tox profile; requires either -yes "
@ -1437,12 +1454,13 @@ twc_commands_init()
" || unload %(tox_loaded_profiles)|%*"
" || reload %(tox_loaded_profiles)|%*",
twc_cmd_tox, NULL, NULL);
weechat_hook_command("send", "send a file to a friend",
"<file>"
" || <number>|<name>|<Tox ID> <file>",
"file: path to the file\n"
"number, name, Tox ID: the friend you are sending the file to\n",
"%(filename)"
" || %(tox_friend_name)|%(tox_friend_tox_id) %(filename)",
twc_cmd_send, NULL, NULL);
weechat_hook_command(
"send", "send a file to a friend",
"<file>"
" || <number>|<name>|<Tox ID> <file>",
"file: path to the file\n"
"number, name, Tox ID: the friend you are sending the file to\n",
"%(filename)"
" || %(tox_friend_name)|%(tox_friend_tox_id) %(filename)",
twc_cmd_send, NULL, NULL);
}

View File

@ -40,9 +40,19 @@ struct t_config_option *twc_config_friend_request_message;
struct t_config_option *twc_config_short_id_size;
char *twc_profile_option_names[TWC_PROFILE_NUM_OPTIONS] = {
"save_file", "autoload", "autojoin", "autojoin_delay",
"max_friend_requests", "proxy_address", "proxy_port", "proxy_type",
"udp", "ipv6", "passphrase", "logging", "downloading_path",
"save_file",
"autoload",
"autojoin",
"autojoin_delay",
"max_friend_requests",
"proxy_address",
"proxy_port",
"proxy_type",
"udp",
"ipv6",
"passphrase",
"logging",
"downloading_path",
};
/**
@ -227,9 +237,10 @@ twc_config_init_option(struct t_twc_profile *profile,
break;
case TWC_PROFILE_OPTION_AUTOJOIN_DELAY:
type = "integer";
description = "delay befor do autojoin (in ms) this required to "
"tox from entering incorrect state and stop processing "
"group events";
description =
"delay befor do autojoin (in ms) this required to "
"tox from entering incorrect state and stop processing "
"group events";
min = 0;
max = INT_MAX;
default_value = "5000";
@ -291,8 +302,9 @@ twc_config_init_option(struct t_twc_profile *profile,
break;
case TWC_PROFILE_OPTION_DOWNLOADING_PATH:
type = "string";
description = "path to downloaded files (\"%h\" will be replaced by "
"WeeChat home folder and \"%p\" by profile name";
description =
"path to downloaded files (\"%h\" will be replaced by "
"WeeChat home folder and \"%p\" by profile name";
default_value = "%h/tfer/%p/";
break;
default:

View File

@ -58,7 +58,8 @@ twc_group_chat_invite_add(struct t_twc_profile *profile, int32_t friend_number,
invite->data_size = size;
if (TWC_PROFILE_OPTION_BOOLEAN(profile, TWC_PROFILE_OPTION_AUTOJOIN))
invite->autojoin_delay = TWC_PROFILE_OPTION_INTEGER(profile, TWC_PROFILE_OPTION_AUTOJOIN_DELAY);
invite->autojoin_delay = TWC_PROFILE_OPTION_INTEGER(
profile, TWC_PROFILE_OPTION_AUTOJOIN_DELAY);
else
invite->autojoin_delay = 0;

View File

@ -20,8 +20,8 @@
#ifndef TOX_WEECHAT_LIST_H
#define TOX_WEECHAT_LIST_H
#include <stdlib.h>
#include "twc-tfer.h"
#include <stdlib.h>
struct t_twc_list
{

View File

@ -257,8 +257,9 @@ twc_tox_new_print_error(struct t_twc_profile *profile,
weechat_prefix("error"), options->proxy_host);
break;
case TOX_ERR_NEW_LOAD_ENCRYPTED:
weechat_printf(profile->buffer, "%scould not load Tox (encrypted "
"data files are not yet supported)",
weechat_printf(profile->buffer,
"%scould not load Tox (encrypted "
"data files are not yet supported)",
weechat_prefix("error"));
break;
case TOX_ERR_NEW_LOAD_BAD_FORMAT:
@ -394,7 +395,8 @@ twc_profile_load(struct t_twc_profile *profile)
{
weechat_printf(profile->buffer,
"%scould not decrypt Tox data file (no passphrase "
"specified)", weechat_prefix("error"));
"specified)",
weechat_prefix("error"));
return TWC_RC_ERROR;
}
}
@ -453,13 +455,15 @@ twc_profile_load(struct t_twc_profile *profile)
tox_callback_friend_request(profile->tox, twc_friend_request_callback);
tox_callback_conference_invite(profile->tox, twc_group_invite_callback);
tox_callback_conference_message(profile->tox, twc_group_message_callback);
tox_callback_conference_peer_list_changed(profile->tox,
twc_group_peer_list_changed_callback);
tox_callback_conference_peer_list_changed(
profile->tox, twc_group_peer_list_changed_callback);
tox_callback_conference_peer_name(profile->tox,
twc_group_peer_name_callback);
tox_callback_conference_title(profile->tox, twc_group_title_callback);
tox_callback_file_recv_control(profile->tox, twc_file_recv_control_callback);
tox_callback_file_chunk_request(profile->tox, twc_file_chunk_request_callback);
tox_callback_file_recv_control(profile->tox,
twc_file_recv_control_callback);
tox_callback_file_chunk_request(profile->tox,
twc_file_chunk_request_callback);
tox_callback_file_recv(profile->tox, twc_file_recv_callback);
tox_callback_file_recv_chunk(profile->tox, twc_file_recv_chunk_callback);
@ -597,7 +601,7 @@ twc_profile_search_tox(struct Tox *tox)
{
size_t profile_index;
struct t_twc_list_item *profile_item;
twc_list_foreach(twc_profiles, profile_index, profile_item)
twc_list_foreach (twc_profiles, profile_index, profile_item)
{
if (profile_item->profile->tox == tox)
return profile_item->profile;
@ -685,7 +689,8 @@ twc_profile_free(struct t_twc_profile *profile)
/* close tfer's buffer */
if (profile->tfer->buffer)
{
weechat_buffer_set_pointer(profile->tfer->buffer, "close_callback", NULL);
weechat_buffer_set_pointer(profile->tfer->buffer, "close_callback",
NULL);
weechat_buffer_close(profile->tfer->buffer);
}

View File

@ -17,43 +17,52 @@
#include <stdbool.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include <unistd.h>
#include <tox/tox.h>
#include <weechat/weechat-plugin.h>
#include "twc-tfer.h"
#include "twc-profile.h"
#include "twc-list.h"
#include "twc-profile.h"
#include "twc-tfer.h"
#include "twc-utils.h"
#include "twc.h"
#define PROGRESS_BAR_LEN (50)
#define TWC_TFER_UPDATE_STATUS_AND_RETURN(fmt, ...) {\
sprintf(status, fmt, ##__VA_ARGS__); \
twc_tfer_update_status(profile->tfer, status); \
weechat_string_free_split(argv); \
free(status); \
return WEECHAT_RC_OK; \
}
#define TWC_TFER_UPDATE_STATUS_AND_RETURN(fmt, ...) \
do \
{ \
sprintf(status, fmt, ##__VA_ARGS__); \
twc_tfer_update_status(profile->tfer, status); \
weechat_string_free_split(argv); \
free(status); \
return WEECHAT_RC_OK; \
} while (0)
#define TWC_TFER_MESSAGE(present, past) {\
int result = twc_tfer_file_ ## present(profile, n); \
switch (result) \
{ \
case 1: \
TWC_TFER_UPDATE_STATUS_AND_RETURN("request number %ld has been " #past, n); \
case 0: \
TWC_TFER_UPDATE_STATUS_AND_RETURN("request number %ld cannot be " #past " because " \
"of tox internal issues", n); \
case -1: \
TWC_TFER_UPDATE_STATUS_AND_RETURN("request number %ld cannot be " #past, n); \
} }\
#define TWC_TFER_MESSAGE(present, past) \
do \
{ \
int result = twc_tfer_file_##present(profile, n); \
switch (result) \
{ \
case 1: \
TWC_TFER_UPDATE_STATUS_AND_RETURN( \
"request number %ld has been " #past, n); \
case 0: \
TWC_TFER_UPDATE_STATUS_AND_RETURN( \
"request number %ld cannot be " #past " because " \
"of tox internal issues", \
n); \
case -1: \
TWC_TFER_UPDATE_STATUS_AND_RETURN( \
"request number %ld cannot be " #past, n); \
} \
} while (0)
/**
* Create a new "tfer" object that handles a list of transmitting files and
* Create a new "tfer" object that handles a list of transmitting files and
* a buffer for managing them.
*/
struct t_twc_tfer *
@ -76,11 +85,9 @@ twc_tfer_load(struct t_twc_profile *profile)
struct t_gui_buffer *buffer;
char *name = malloc(sizeof(profile->name) + 5);
sprintf(name, "tfer/%s", profile->name);
profile->tfer->buffer = buffer = weechat_buffer_new(name,
twc_tfer_buffer_input_callback,
(void *)profile, NULL,
twc_tfer_buffer_close_callback,
(void *)profile, NULL);
profile->tfer->buffer = buffer = weechat_buffer_new(
name, twc_tfer_buffer_input_callback, (void *)profile, NULL,
twc_tfer_buffer_close_callback, (void *)profile, NULL);
free(name);
if (!buffer)
return TWC_RC_ERROR;
@ -104,9 +111,7 @@ twc_tfer_print_legend(struct t_twc_tfer *tfer)
char *text[TWC_TFER_LEGEND_LINES] = {
"status: OK", /* This line is reserved for the status */
"r: refresh | a <n>: accept | d <n>: decline",
"p <n>: pause | c <n>: continue | b <n>: abort",
"files:"
};
"p <n>: pause | c <n>: continue | b <n>: abort", "files:"};
int i;
for (i = 0; i < TWC_TFER_LEGEND_LINES; i++)
{
@ -132,23 +137,26 @@ twc_tfer_expanded_path(struct t_twc_profile *profile, const char *base_path)
/**
* Set profile-associated path for downloads.
* If it is impossible to create a directory with the path that
* If it is impossible to create a directory with the path that
* has been set in tox.profile.<name>.downloading_path then default
* value will be used.
*/
void
twc_tfer_update_downloading_path(struct t_twc_profile *profile)
{
const char *base_path = TWC_PROFILE_OPTION_STRING(profile,
TWC_PROFILE_OPTION_DOWNLOADING_PATH);
const char *base_path =
TWC_PROFILE_OPTION_STRING(profile, TWC_PROFILE_OPTION_DOWNLOADING_PATH);
char *full_path = twc_tfer_expanded_path(profile, base_path);
if (!weechat_mkdir_parents(full_path, 0755))
{
char *bad_path = full_path;
base_path = weechat_config_string(twc_config_profile_default[TWC_PROFILE_OPTION_DOWNLOADING_PATH]);
base_path = weechat_config_string(
twc_config_profile_default[TWC_PROFILE_OPTION_DOWNLOADING_PATH]);
full_path = twc_tfer_expanded_path(profile, base_path);
weechat_printf(profile->buffer, "cannot create directory \"%s\","
"using default value: \"%s\"", bad_path, full_path);
weechat_printf(profile->buffer,
"cannot create directory \"%s\","
"using default value: \"%s\"",
bad_path, full_path);
free(bad_path);
weechat_mkdir_parents(full_path, 0755);
}
@ -170,7 +178,7 @@ twc_tfer_file_check(const char *filename)
* Returns a pointer to allocated string and must be freed after use.
*/
char *
twc_tfer_file_unique_name(const char* original)
twc_tfer_file_unique_name(const char *original)
{
char *name = malloc(sizeof(char) * (FILENAME_MAX + 1));
name[FILENAME_MAX] = '\0';
@ -189,7 +197,7 @@ twc_tfer_file_unique_name(const char* original)
}
else
extension = "";
char body[strlen(name)+1];
char body[strlen(name) + 1];
strcpy(body, name);
/* check if there is already a postfix number in the end of the file
@ -214,8 +222,7 @@ twc_tfer_file_unique_name(const char* original)
{
snprintf(name, FILENAME_MAX, "%s(%i)%s", body, i, extension);
i++;
}
while (twc_tfer_file_check(name));
} while (twc_tfer_file_check(name));
return name;
}
@ -244,10 +251,10 @@ twc_tfer_file_name_strip(const char *original, size_t size)
* Create a new file.
*/
struct t_twc_tfer_file *
twc_tfer_file_new(struct t_twc_profile *profile,
const char *nickname, const char *filename,
uint32_t friend_number, uint32_t file_number,
uint64_t size, enum t_twc_tfer_file_type filetype)
twc_tfer_file_new(struct t_twc_profile *profile, const char *nickname,
const char *filename, uint32_t friend_number,
uint32_t file_number, uint64_t size,
enum t_twc_tfer_file_type filetype)
{
struct t_twc_tfer_file *file = malloc(sizeof(struct t_twc_tfer_file));
file->status = TWC_TFER_FILE_STATUS_REQUEST;
@ -264,8 +271,8 @@ twc_tfer_file_new(struct t_twc_profile *profile,
{
char *full_path = malloc(sizeof(char) * (FILENAME_MAX + 1));
sprintf(full_path, "%s", profile->tfer->downloading_path);
char *final_name = twc_tfer_file_name_strip(filename,
FILENAME_MAX + 1 - strlen(full_path));
char *final_name = twc_tfer_file_name_strip(
filename, FILENAME_MAX + 1 - strlen(full_path));
if (!final_name)
return NULL;
@ -286,8 +293,8 @@ twc_tfer_file_new(struct t_twc_profile *profile,
}
else
{
file->filename = twc_tfer_file_name_strip(filename,
FILENAME_MAX + 1 - strlen(filename));
file->filename = twc_tfer_file_name_strip(
filename, FILENAME_MAX + 1 - strlen(filename));
file->full_path = NULL;
file->fp = fopen(filename, "r");
}
@ -295,7 +302,6 @@ twc_tfer_file_new(struct t_twc_profile *profile,
if (!(file->fp))
return NULL;
return file;
}
/**
@ -322,14 +328,8 @@ twc_tfer_file_get_type_str(struct t_twc_tfer_file *file)
const char *
twc_tfer_file_get_status_str(struct t_twc_tfer_file *file)
{
char *statuses[] = {
"[request]",
"",
"[paused]",
"[done]",
"[declined]",
"[aborted]"
};
char *statuses[] = {"[request]", "", "[paused]",
"[done]", "[declined]", "[aborted]"};
return statuses[file->status];
}
@ -341,7 +341,7 @@ twc_tfer_cut_size(size_t size)
{
float ret = size;
int i = 0;
while((ret>1024) && (i < TWC_MAX_SIZE_SUFFIX))
while ((ret > 1024) && (i < TWC_MAX_SIZE_SUFFIX))
{
ret /= 1024.0;
i++;
@ -358,7 +358,7 @@ twc_tfer_size_suffix(uint64_t size)
char *suffixes[] = {"", "K", "M", "G", "T"};
uint64_t ret = size;
int i = 0;
while((ret > 1024) && (i < TWC_MAX_SIZE_SUFFIX))
while ((ret > 1024) && (i < TWC_MAX_SIZE_SUFFIX))
{
ret /= 1024.0;
i++;
@ -374,7 +374,7 @@ twc_tfer_cut_speed(float speed)
{
float ret = speed;
int i = 0;
while((ret>1024) && (i < TWC_MAX_SPEED_SUFFIX))
while ((ret > 1024) && (i < TWC_MAX_SPEED_SUFFIX))
{
ret /= 1024.0;
i++;
@ -391,7 +391,7 @@ twc_tfer_speed_suffix(float speed)
char *suffixes[] = {"bytes/s", "KB/s", "MB/s", "GB/s", "TB/s"};
uint64_t ret = speed;
int i = 0;
while((ret > 1024) && (i < TWC_MAX_SPEED_SUFFIX))
while ((ret > 1024) && (i < TWC_MAX_SPEED_SUFFIX))
{
ret /= 1024.0;
i++;
@ -407,7 +407,7 @@ twc_tfer_get_time()
{
struct timespec tp;
clock_gettime(CLOCK_REALTIME, &tp);
return (double)tp.tv_sec + (double)(tp.tv_nsec/1E9);
return (double)tp.tv_sec + (double)(tp.tv_nsec / 1E9);
}
/**
@ -441,8 +441,7 @@ twc_tfer_file_update(struct t_twc_tfer *tfer, struct t_twc_tfer_file *file)
{
remainder = remainder / 10;
indent++;
}
while (remainder > 0);
} while (remainder > 0);
indent += 5; /* length of ") => " */
char placeholder[indent + 1];
memset(placeholder, ' ', indent);
@ -450,21 +449,16 @@ twc_tfer_file_update(struct t_twc_tfer *tfer, struct t_twc_tfer_file *file)
const char *status = twc_tfer_file_get_status_str(file);
if (file->size == UINT64_MAX)
{
weechat_printf_y(tfer->buffer, line, "%i) %s %s: %s [STREAM]",
index, type, file->nickname, file->filename);
weechat_printf_y(tfer->buffer, line, "%i) %s %s: %s [STREAM]", index,
type, file->nickname, file->filename);
}
else
{
float display_size = twc_tfer_cut_size(file->size);
const char *size_suffix = twc_tfer_size_suffix(file->size);
weechat_printf_y(tfer->buffer, line, "%i) %s %s: %s %i (%.2f%s)",
index,
type,
file->nickname,
file->filename,
file->size,
display_size,
size_suffix);
weechat_printf_y(tfer->buffer, line, "%i) %s %s: %s %i (%.2f%s)", index,
type, file->nickname, file->filename, file->size,
display_size, size_suffix);
}
if (file->status == TWC_TFER_FILE_STATUS_IN_PROGRESS)
{
@ -473,14 +467,14 @@ twc_tfer_file_update(struct t_twc_tfer *tfer, struct t_twc_tfer_file *file)
const char *speed_suffix = twc_tfer_speed_suffix(speed);
if (file->size == UINT64_MAX)
{
weechat_printf_y(tfer->buffer, line + 1, "%s%.2f%s",
placeholder, display_speed, speed_suffix);
weechat_printf_y(tfer->buffer, line + 1, "%s%.2f%s", placeholder,
display_speed, speed_suffix);
return;
}
double ratio = (double)(file->position)/(double)(file->size);
double ratio = (double)(file->position) / (double)(file->size);
int percents = (int)(ratio * 100);
char progress_bar[PROGRESS_BAR_LEN+1];
char progress_bar[PROGRESS_BAR_LEN + 1];
memset(progress_bar, ' ', PROGRESS_BAR_LEN);
int i;
for (i = 0; i < PROGRESS_BAR_LEN * ratio; i++)
@ -493,31 +487,28 @@ twc_tfer_file_update(struct t_twc_tfer *tfer, struct t_twc_tfer_file *file)
const char *pos_suffix = twc_tfer_size_suffix(file->position);
weechat_printf_y(tfer->buffer, line + 1, "%s%i%% [%s] %.2f%s %.2f%s",
placeholder,
percents,
progress_bar,
display_pos,
pos_suffix,
display_speed,
speed_suffix);
placeholder, percents, progress_bar, display_pos,
pos_suffix, display_speed, speed_suffix);
}
else
weechat_printf_y(tfer->buffer, line + 1, "%s%s",
placeholder,status);
weechat_printf_y(tfer->buffer, line + 1, "%s%s", placeholder, status);
}
/**
* Allocate and return "uint8_t data[length]" chunk of data starting from "position".
* Allocate and return "uint8_t data[length]" chunk of data starting from
* "position".
*/
uint8_t *
twc_tfer_file_get_chunk(struct t_twc_tfer_file *file, uint64_t position, size_t length)
twc_tfer_file_get_chunk(struct t_twc_tfer_file *file, uint64_t position,
size_t length)
{
fseek(file->fp, position, SEEK_SET);
uint8_t *data = malloc(sizeof(uint8_t) * length);
size_t read = fread(data, sizeof(uint8_t), length, file->fp);
while ((read < length) && !feof(file->fp))
{
read += fread(data + read * sizeof(uint8_t), sizeof(uint8_t), length - read, file->fp);
read += fread(data + read * sizeof(uint8_t), sizeof(uint8_t),
length - read, file->fp);
}
if (read != length)
return NULL;
@ -528,13 +519,15 @@ twc_tfer_file_get_chunk(struct t_twc_tfer_file *file, uint64_t position, size_t
* Write a chunk to the file.
*/
bool
twc_tfer_file_write_chunk(struct t_twc_tfer_file *file, const uint8_t *data, uint64_t position, size_t length)
twc_tfer_file_write_chunk(struct t_twc_tfer_file *file, const uint8_t *data,
uint64_t position, size_t length)
{
fseek(file->fp, position, SEEK_SET);
size_t wrote = fwrite(data, sizeof(uint8_t), length, file->fp);
while (wrote < length)
{
wrote += fwrite(data + wrote * sizeof(uint8_t), sizeof(uint8_t), length - wrote, file->fp);
wrote += fwrite(data + wrote * sizeof(uint8_t), sizeof(uint8_t),
length - wrote, file->fp);
}
if (wrote != length)
@ -550,7 +543,7 @@ twc_tfer_file_get_by_number(struct t_twc_tfer *tfer, uint32_t file_number)
{
size_t index;
struct t_twc_list_item *item;
twc_list_foreach(tfer->files, index, item)
twc_list_foreach (tfer->files, index, item)
{
if (item->file->file_number == file_number)
return item->file;
@ -566,7 +559,7 @@ twc_tfer_file_get_index(struct t_twc_tfer *tfer, struct t_twc_tfer_file *file)
{
size_t index;
struct t_twc_list_item *item;
twc_list_foreach(tfer->files, index, item)
twc_list_foreach (tfer->files, index, item)
{
if (item->file == file)
return index;
@ -593,7 +586,7 @@ twc_tfer_buffer_update(struct t_twc_tfer *tfer)
{
size_t index;
struct t_twc_list_item *item;
twc_list_foreach(tfer->files, index, item)
twc_list_foreach (tfer->files, index, item)
{
twc_tfer_file_update(tfer, item->file);
}
@ -608,11 +601,11 @@ twc_tfer_buffer_refresh(struct t_twc_tfer *tfer)
{
size_t index;
struct t_twc_list_item *item;
twc_list_foreach(tfer->files, index, item)
twc_list_foreach (tfer->files, index, item)
{
enum t_twc_tfer_file_status status = item->file->status;
if (status == TWC_TFER_FILE_STATUS_DECLINED ||
status == TWC_TFER_FILE_STATUS_ABORTED ||
status == TWC_TFER_FILE_STATUS_ABORTED ||
status == TWC_TFER_FILE_STATUS_DONE)
{
struct t_twc_tfer_file *file = twc_list_remove(item);
@ -626,9 +619,9 @@ twc_tfer_buffer_refresh(struct t_twc_tfer *tfer)
/**
* Send TOX_FILE_CONTROL command to a client.
* "сheck" is a file status that a file should be in before sending a control command.
* "send" is a control comand you are going to send.
* "set" is a file status that will be set after successful sending a control command.
* "сheck" is a file status that a file should be in before sending a control
* command. "send" is a control comand you are going to send. "set" is a file
* status that will be set after successful sending a control command.
*/
int
twc_tfer_file_send_control(struct t_twc_profile *profile, size_t index,
@ -649,8 +642,10 @@ twc_tfer_file_send_control(struct t_twc_profile *profile, size_t index,
&control_error);
if (control_error)
{
weechat_printf(profile->buffer, "%scannot send control command for \"%s\" file: %s",
weechat_prefix("error"), file->filename, twc_tox_err_file_control(control_error));
weechat_printf(profile->buffer,
"%scannot send control command for \"%s\" file: %s",
weechat_prefix("error"), file->filename,
twc_tox_err_file_control(control_error));
return 0;
}
else
@ -658,7 +653,8 @@ twc_tfer_file_send_control(struct t_twc_profile *profile, size_t index,
if (send == TOX_FILE_CONTROL_CANCEL)
{
fclose(file->fp);
if (file->type == TWC_TFER_FILE_TYPE_DOWNLOADING && file->size != UINT64_MAX)
if (file->type == TWC_TFER_FILE_TYPE_DOWNLOADING &&
file->size != UINT64_MAX)
remove(file->full_path);
}
file->status = set;
@ -667,8 +663,6 @@ twc_tfer_file_send_control(struct t_twc_profile *profile, size_t index,
}
}
/**
* Accept a file with number <index> in the list.
* Returns 1 if successful, 0 when there's an issue with tox calls
@ -677,10 +671,9 @@ twc_tfer_file_send_control(struct t_twc_profile *profile, size_t index,
int
twc_tfer_file_accept(struct t_twc_profile *profile, size_t index)
{
return twc_tfer_file_send_control(profile, index,
TWC_TFER_FILE_STATUS_REQUEST,
TOX_FILE_CONTROL_RESUME,
TWC_TFER_FILE_STATUS_IN_PROGRESS);
return twc_tfer_file_send_control(
profile, index, TWC_TFER_FILE_STATUS_REQUEST, TOX_FILE_CONTROL_RESUME,
TWC_TFER_FILE_STATUS_IN_PROGRESS);
}
/**
@ -691,10 +684,9 @@ twc_tfer_file_accept(struct t_twc_profile *profile, size_t index)
int
twc_tfer_file_decline(struct t_twc_profile *profile, size_t index)
{
return twc_tfer_file_send_control(profile, index,
TWC_TFER_FILE_STATUS_REQUEST,
TOX_FILE_CONTROL_CANCEL,
TWC_TFER_FILE_STATUS_DECLINED);
return twc_tfer_file_send_control(
profile, index, TWC_TFER_FILE_STATUS_REQUEST, TOX_FILE_CONTROL_CANCEL,
TWC_TFER_FILE_STATUS_DECLINED);
}
/**
@ -705,10 +697,9 @@ twc_tfer_file_decline(struct t_twc_profile *profile, size_t index)
int
twc_tfer_file_pause(struct t_twc_profile *profile, size_t index)
{
return twc_tfer_file_send_control(profile, index,
TWC_TFER_FILE_STATUS_IN_PROGRESS,
TOX_FILE_CONTROL_PAUSE,
TWC_TFER_FILE_STATUS_PAUSED);
return twc_tfer_file_send_control(
profile, index, TWC_TFER_FILE_STATUS_IN_PROGRESS,
TOX_FILE_CONTROL_PAUSE, TWC_TFER_FILE_STATUS_PAUSED);
}
/**
@ -719,10 +710,9 @@ twc_tfer_file_pause(struct t_twc_profile *profile, size_t index)
int
twc_tfer_file_continue(struct t_twc_profile *profile, size_t index)
{
return twc_tfer_file_send_control(profile, index,
TWC_TFER_FILE_STATUS_PAUSED,
TOX_FILE_CONTROL_RESUME,
TWC_TFER_FILE_STATUS_IN_PROGRESS);
return twc_tfer_file_send_control(
profile, index, TWC_TFER_FILE_STATUS_PAUSED, TOX_FILE_CONTROL_RESUME,
TWC_TFER_FILE_STATUS_IN_PROGRESS);
}
/**
@ -733,10 +723,9 @@ twc_tfer_file_continue(struct t_twc_profile *profile, size_t index)
int
twc_tfer_file_abort(struct t_twc_profile *profile, size_t index)
{
return twc_tfer_file_send_control(profile, index,
TWC_TFER_FILE_STATUS_IN_PROGRESS,
TOX_FILE_CONTROL_CANCEL,
TWC_TFER_FILE_STATUS_ABORTED);
return twc_tfer_file_send_control(
profile, index, TWC_TFER_FILE_STATUS_IN_PROGRESS,
TOX_FILE_CONTROL_CANCEL, TWC_TFER_FILE_STATUS_ABORTED);
}
/**
@ -751,9 +740,13 @@ twc_tfer_buffer_input_callback(const void *pointer, void *data,
profile = (struct t_twc_profile *)pointer;
int argc;
char **argv = weechat_string_split_shell(input_data, &argc);
char *status = malloc(sizeof(char) * weechat_window_get_integer(weechat_current_window(), "win_width") + 1);
char *status =
malloc(sizeof(char) * weechat_window_get_integer(
weechat_current_window(), "win_width") +
1);
/* refresh file list, i.e delete files that have been marked as "denied", "aborted" and "done" */
/* refresh file list, i.e delete files that have been marked as "denied",
* "aborted" and "done" */
if (weechat_strcasecmp(argv[0], "r") == 0)
{
if (argc == 1)
@ -763,7 +756,8 @@ twc_tfer_buffer_input_callback(const void *pointer, void *data,
}
else
{
TWC_TFER_UPDATE_STATUS_AND_RETURN("this command doesn't accept any arguments");
TWC_TFER_UPDATE_STATUS_AND_RETURN(
"this command doesn't accept any arguments");
}
}
if (strstr("adpcbADPCB", argv[0]) && argc < 2)
@ -771,14 +765,16 @@ twc_tfer_buffer_input_callback(const void *pointer, void *data,
if (argc == 2)
{
size_t n = (size_t)strtol(argv[1], NULL, 0);
if ((n == 0 && strcmp(argv[1], "0") != 0) || n > (profile->tfer->files->count - 1))
if ((n == 0 && strcmp(argv[1], "0") != 0) ||
n > (profile->tfer->files->count - 1))
{
TWC_TFER_UPDATE_STATUS_AND_RETURN("<n> must be existing number of file");
TWC_TFER_UPDATE_STATUS_AND_RETURN(
"<n> must be existing number of file");
}
/* accept */
if (weechat_strcasecmp(argv[0], "a") == 0)
{
TWC_TFER_MESSAGE(accept, accepted);
TWC_TFER_MESSAGE(accept, accepted);
}
/* decline */
if (weechat_strcasecmp(argv[0], "d") == 0)

View File

@ -1,4 +1,4 @@
/*
/*
* This file is part of Tox-WeeChat.
*
* Tox-WeeChat is free software: you can redistribute it and/or modify
@ -47,14 +47,14 @@ enum t_twc_tfer_file_status
enum t_twc_tfer_file_type
{
TWC_TFER_FILE_TYPE_DOWNLOADING,
TWC_TFER_FILE_TYPE_UPLOADING,
TWC_TFER_FILE_TYPE_UPLOADING,
};
struct t_twc_tfer_file
{
enum t_twc_tfer_file_status status;
enum t_twc_tfer_file_type type;
uint64_t position; /* already transmitted (in bytes) */
uint64_t position; /* already transmitted (in bytes) */
uint64_t size;
uint32_t friend_number;
uint32_t file_number;
@ -103,19 +103,21 @@ char *
twc_tfer_file_name_strip(const char *original, size_t size);
struct t_twc_tfer_file *
twc_tfer_file_new(struct t_twc_profile *profile,
const char *nickname, const char *filename,
uint32_t friend_number, uint32_t file_number,
uint64_t size, enum t_twc_tfer_file_type filetype);
twc_tfer_file_new(struct t_twc_profile *profile, const char *nickname,
const char *filename, uint32_t friend_number,
uint32_t file_number, uint64_t size,
enum t_twc_tfer_file_type filetype);
void
twc_tfer_file_add(struct t_twc_tfer *tfer, struct t_twc_tfer_file *file);
uint8_t *
twc_tfer_file_get_chunk(struct t_twc_tfer_file *file, uint64_t position, size_t length);
twc_tfer_file_get_chunk(struct t_twc_tfer_file *file, uint64_t position,
size_t length);
bool
twc_tfer_file_write_chunk(struct t_twc_tfer_file *file, const uint8_t *data, uint64_t position, size_t length);
twc_tfer_file_write_chunk(struct t_twc_tfer_file *file, const uint8_t *data,
uint64_t position, size_t length);
struct t_twc_tfer_file *
twc_tfer_file_get_by_number(struct t_twc_tfer *tfer, uint32_t file_number);

View File

@ -32,13 +32,15 @@
#include "twc-group-invite.h"
#include "twc-message-queue.h"
#include "twc-profile.h"
#include "twc-utils.h"
#include "twc-tfer.h"
#include "twc-utils.h"
#include "twc.h"
#include "twc-tox-callbacks.h"
#define TWC_TFER_FILE_UPDATE_STATUS(st) do { \
#define TWC_TFER_FILE_UPDATE_STATUS(st) \
do \
{ \
file->status = st; \
twc_tfer_file_update(profile->tfer, file); \
} while (0)
@ -57,8 +59,7 @@ twc_do_timer_cb(const void *pointer, void *data, int remaining_calls)
interval = tox_iteration_interval(profile->tox);
tox_iterate(profile->tox, profile);
struct t_hook *hook =
weechat_hook_timer(interval, 0, 1,
twc_do_timer_cb, profile, NULL);
weechat_hook_timer(interval, 0, 1, twc_do_timer_cb, profile, NULL);
profile->tox_do_timer = hook;
/* check connection status */
@ -70,24 +71,26 @@ twc_do_timer_cb(const void *pointer, void *data, int remaining_calls)
if (TWC_PROFILE_OPTION_BOOLEAN(profile, TWC_PROFILE_OPTION_AUTOJOIN))
{
struct t_twc_group_chat_invite *invite;
for(i = 0; (invite = twc_group_chat_invite_with_index(profile, i)); i++)
if(invite->autojoin_delay <= 0)
for (i = 0; (invite = twc_group_chat_invite_with_index(profile, i));
i++)
if (invite->autojoin_delay <= 0)
{
struct t_twc_chat *friend_chat =
twc_chat_search_friend(profile, invite->friend_number, false);
char *friend_name = twc_get_name_nt(profile->tox, invite->friend_number);
struct t_twc_chat *friend_chat = twc_chat_search_friend(
profile, invite->friend_number, false);
char *friend_name =
twc_get_name_nt(profile->tox, invite->friend_number);
char *type_str;
switch (invite->group_chat_type)
{
case TOX_CONFERENCE_TYPE_TEXT:
type_str = "a text-only group chat";
break;
case TOX_CONFERENCE_TYPE_AV:
type_str = "an audio/vikdeo group chat";
break;
default:
type_str = "a group chat";
break;
case TOX_CONFERENCE_TYPE_TEXT:
type_str = "a text-only group chat";
break;
case TOX_CONFERENCE_TYPE_AV:
type_str = "an audio/vikdeo group chat";
break;
default:
type_str = "a group chat";
break;
}
rc = twc_group_chat_invite_join(invite);
@ -99,17 +102,19 @@ twc_do_timer_cb(const void *pointer, void *data, int remaining_calls)
if (friend_chat)
{
weechat_printf_date_tags(
friend_chat->buffer, 0, tags,
"%sWe joined the %s%s%s's invite to %s.",
weechat_prefix("network"), weechat_color("chat_nick_other"),
friend_name, weechat_color("reset"), type_str);
friend_chat->buffer, 0, tags,
"%sWe joined the %s%s%s's invite to %s.",
weechat_prefix("network"),
weechat_color("chat_nick_other"), friend_name,
weechat_color("reset"), type_str);
tags = "";
}
weechat_printf_date_tags(
profile->buffer, 0, tags,
"%sWe joined the %s%s%s's invite to %s.",
weechat_prefix("network"), weechat_color("chat_nick_other"),
friend_name, weechat_color("reset"), type_str);
profile->buffer, 0, tags,
"%sWe joined the %s%s%s's invite to %s.",
weechat_prefix("network"),
weechat_color("chat_nick_other"), friend_name,
weechat_color("reset"), type_str);
}
else
{
@ -117,19 +122,21 @@ twc_do_timer_cb(const void *pointer, void *data, int remaining_calls)
if (friend_chat)
{
weechat_printf_date_tags(
friend_chat->buffer, 0, tags,
"%s%s%s%s invites you to join %s, but we failed to "
"process the invite. Please try again.",
weechat_prefix("network"), weechat_color("chat_nick_other"),
friend_name, weechat_color("reset"));
friend_chat->buffer, 0, tags,
"%s%s%s%s invites you to join %s, but we failed to "
"process the invite. Please try again.",
weechat_prefix("network"),
weechat_color("chat_nick_other"), friend_name,
weechat_color("reset"));
tags = "";
}
weechat_printf_date_tags(
profile->buffer, 0, tags,
"%s%s%s%s invites you to join %s, but we failed to "
"process the invite. Please try again.",
weechat_prefix("network"), weechat_color("chat_nick_other"),
friend_name, weechat_color("reset"));
profile->buffer, 0, tags,
"%s%s%s%s invites you to join %s, but we failed to "
"process the invite. Please try again.",
weechat_prefix("network"),
weechat_color("chat_nick_other"), friend_name,
weechat_color("reset"));
}
}
else
@ -239,7 +246,7 @@ twc_name_change_callback(Tox *tox, uint32_t friend_number, const uint8_t *name,
size_t index;
struct t_twc_list_item *item;
struct t_twc_tfer_file *file;
twc_list_foreach(profile->tfer->files, index, item)
twc_list_foreach (profile->tfer->files, index, item)
{
file = item->file;
if (file->friend_number == friend_number)
@ -361,12 +368,12 @@ twc_group_invite_callback(Tox *tox, uint32_t friend_number,
friend_name, weechat_color("reset"), type_str, rc);
tags = "";
}
weechat_printf_date_tags(
profile->buffer, 0, tags,
"%s%s%s%s invites you to join %s. Type "
"\"/group join %d\" to accept.",
weechat_prefix("network"), weechat_color("chat_nick_other"),
friend_name, weechat_color("reset"), type_str, rc);
weechat_printf_date_tags(profile->buffer, 0, tags,
"%s%s%s%s invites you to join %s. Type "
"\"/group join %d\" to accept.",
weechat_prefix("network"),
weechat_color("chat_nick_other"), friend_name,
weechat_color("reset"), type_str, rc);
}
else
{
@ -505,10 +512,8 @@ twc_group_peer_list_changed_callback(Tox *tox, uint32_t group_number,
void
twc_group_peer_name_callback(Tox *tox, uint32_t group_number,
uint32_t peer_number,
const uint8_t *pname,
size_t pname_len,
void *data)
uint32_t peer_number, const uint8_t *pname,
size_t pname_len, void *data)
{
struct t_twc_profile *profile = data;
struct t_twc_chat *chat =
@ -549,26 +554,26 @@ twc_group_peer_name_callback(Tox *tox, uint32_t group_number,
prev_name = weechat_list_string(n);
name = twc_null_terminate(pname, pname_len);
nick = weechat_nicklist_search_nick(chat->buffer,
chat->nicklist_group, prev_name);
nick = weechat_nicklist_search_nick(chat->buffer, chat->nicklist_group,
prev_name);
weechat_nicklist_remove_nick(chat->buffer, nick);
err = TOX_ERR_CONFERENCE_PEER_QUERY_OK;
rc = tox_conference_peer_number_is_ours(tox, group_number, peer_number, &err);
rc = tox_conference_peer_number_is_ours(tox, group_number, peer_number,
&err);
if ((err == TOX_ERR_CONFERENCE_PEER_QUERY_OK) && (!rc))
weechat_printf(chat->buffer, "%s%s is now known as %s",
weechat_prefix("network"), prev_name, name);
weechat_list_set(n, name);
weechat_nicklist_add_nick(chat->buffer, chat->nicklist_group, name,
NULL, NULL, NULL, 1);
weechat_nicklist_add_nick(chat->buffer, chat->nicklist_group, name, NULL,
NULL, NULL, 1);
free(name);
}
void
twc_group_title_callback(Tox *tox, uint32_t group_number, uint32_t peer_number,
const uint8_t *title, size_t length, void *data)
@ -587,55 +592,62 @@ twc_group_title_callback(Tox *tox, uint32_t group_number, uint32_t peer_number,
}
void
twc_file_recv_control_callback(Tox *tox, uint32_t friend_number, uint32_t file_number,
TOX_FILE_CONTROL control, void *user_data)
twc_file_recv_control_callback(Tox *tox, uint32_t friend_number,
uint32_t file_number, TOX_FILE_CONTROL control,
void *user_data)
{
struct t_twc_profile *profile = twc_profile_search_tox(tox);
struct t_twc_tfer_file *file = twc_tfer_file_get_by_number(profile->tfer, file_number);
struct t_twc_tfer_file *file =
twc_tfer_file_get_by_number(profile->tfer, file_number);
if (!file)
{
weechat_printf(profile->tfer->buffer, "%sthere is no file with number %i in queue",
weechat_printf(profile->tfer->buffer,
"%sthere is no file with number %i in queue",
weechat_prefix("error"), file_number);
return;
}
switch (control)
{
case TOX_FILE_CONTROL_RESUME:
TWC_TFER_FILE_UPDATE_STATUS(TWC_TFER_FILE_STATUS_IN_PROGRESS);
break;
case TOX_FILE_CONTROL_PAUSE:
if (file->position !=0)
TWC_TFER_FILE_UPDATE_STATUS(TWC_TFER_FILE_STATUS_PAUSED);
break;
case TOX_FILE_CONTROL_CANCEL:
fclose(file->fp);
if (file->type == TWC_TFER_FILE_TYPE_DOWNLOADING && file->size != UINT64_MAX)
remove(file->full_path);
if (file->position != 0)
{
TWC_TFER_FILE_UPDATE_STATUS(TWC_TFER_FILE_STATUS_ABORTED);
}
else
{
TWC_TFER_FILE_UPDATE_STATUS(TWC_TFER_FILE_STATUS_DECLINED);
}
break;
case TOX_FILE_CONTROL_RESUME:
TWC_TFER_FILE_UPDATE_STATUS(TWC_TFER_FILE_STATUS_IN_PROGRESS);
break;
case TOX_FILE_CONTROL_PAUSE:
if (file->position != 0)
TWC_TFER_FILE_UPDATE_STATUS(TWC_TFER_FILE_STATUS_PAUSED);
break;
case TOX_FILE_CONTROL_CANCEL:
fclose(file->fp);
if (file->type == TWC_TFER_FILE_TYPE_DOWNLOADING &&
file->size != UINT64_MAX)
remove(file->full_path);
if (file->position != 0)
{
TWC_TFER_FILE_UPDATE_STATUS(TWC_TFER_FILE_STATUS_ABORTED);
}
else
{
TWC_TFER_FILE_UPDATE_STATUS(TWC_TFER_FILE_STATUS_DECLINED);
}
break;
}
}
void
twc_file_chunk_request_callback(Tox *tox, uint32_t friend_number, uint32_t file_number,
uint64_t position, size_t length, void *user_data)
twc_file_chunk_request_callback(Tox *tox, uint32_t friend_number,
uint32_t file_number, uint64_t position,
size_t length, void *user_data)
{
struct t_twc_profile *profile = twc_profile_search_tox(tox);
struct t_twc_tfer_file *file = twc_tfer_file_get_by_number(profile->tfer, file_number);
struct t_twc_tfer_file *file =
twc_tfer_file_get_by_number(profile->tfer, file_number);
/* the file is missing */
if (!file)
{
weechat_printf(profile->tfer->buffer, "%sthere is no file with number %i in queue",
weechat_printf(profile->tfer->buffer,
"%sthere is no file with number %i in queue",
weechat_prefix("error"), file_number);
return;
}
}
/* 0-length chunk requested that means the file transmission is completed */
if (length == 0)
{
@ -657,10 +669,12 @@ twc_file_chunk_request_callback(Tox *tox, uint32_t friend_number, uint32_t file_
return;
}
enum TOX_ERR_FILE_SEND_CHUNK error;
tox_file_send_chunk(profile->tox, friend_number, file_number, position, data, length, &error);
tox_file_send_chunk(profile->tox, friend_number, file_number, position,
data, length, &error);
if (error)
weechat_printf(profile->buffer, "%s%s: chunk sending error: %s",
weechat_prefix("error"), file->filename, twc_tox_err_file_send_chunk(error));
weechat_prefix("error"), file->filename,
twc_tox_err_file_send_chunk(error));
else
{
file->position += length;
@ -677,14 +691,16 @@ twc_file_chunk_request_callback(Tox *tox, uint32_t friend_number, uint32_t file_
void
twc_file_recv_callback(Tox *tox, uint32_t friend_number, uint32_t file_number,
uint32_t kind, uint64_t file_size, const uint8_t *filename,
size_t filename_length, void *user_data)
uint32_t kind, uint64_t file_size,
const uint8_t *filename, size_t filename_length,
void *user_data)
{
struct t_twc_profile *profile = twc_profile_search_tox(tox);
if (kind == TOX_FILE_KIND_AVATAR)
{
TOX_ERR_FILE_CONTROL error;
tox_file_control(tox, friend_number, file_number, TOX_FILE_CONTROL_CANCEL, &error);
tox_file_control(tox, friend_number, file_number,
TOX_FILE_CONTROL_CANCEL, &error);
if (error)
{
weechat_printf(profile->buffer, "%scannot cancel avatar receiving",
@ -694,14 +710,15 @@ twc_file_recv_callback(Tox *tox, uint32_t friend_number, uint32_t file_number,
}
char *name = twc_get_name_nt(tox, friend_number);
char *fname = twc_null_terminate(filename, filename_length);
struct t_twc_tfer_file *file = twc_tfer_file_new(profile, name, fname,
friend_number, file_number,
file_size, TWC_TFER_FILE_TYPE_DOWNLOADING);
struct t_twc_tfer_file *file =
twc_tfer_file_new(profile, name, fname, friend_number, file_number,
file_size, TWC_TFER_FILE_TYPE_DOWNLOADING);
free(name);
free(fname);
if (!file)
{
weechat_printf(profile->buffer, "%scannot open the file \"%s\" with write permissions",
weechat_printf(profile->buffer,
"%scannot open the file \"%s\" with write permissions",
weechat_prefix("error"), filename);
return;
}
@ -715,19 +732,24 @@ twc_file_recv_callback(Tox *tox, uint32_t friend_number, uint32_t file_number,
}
void
twc_file_recv_chunk_callback(Tox *tox, uint32_t friend_number, uint32_t file_number, uint64_t position,
const uint8_t *data, size_t length, void *user_data)
twc_file_recv_chunk_callback(Tox *tox, uint32_t friend_number,
uint32_t file_number, uint64_t position,
const uint8_t *data, size_t length,
void *user_data)
{
struct t_twc_profile *profile = twc_profile_search_tox(tox);
struct t_twc_tfer_file *file = twc_tfer_file_get_by_number(profile->tfer, file_number);
struct t_twc_tfer_file *file =
twc_tfer_file_get_by_number(profile->tfer, file_number);
/* the file is missing */
if (!file)
{
weechat_printf(profile->tfer->buffer, "%sthere is no file with number %i in queue",
weechat_printf(profile->tfer->buffer,
"%sthere is no file with number %i in queue",
weechat_prefix("error"), file_number);
return;
}
/* 0-length chunk transmitted that means the file transmission is completed */
/* 0-length chunk transmitted that means the file transmission is completed
*/
if (length == 0)
{
TWC_TFER_FILE_UPDATE_STATUS(TWC_TFER_FILE_STATUS_DONE);

View File

@ -66,30 +66,34 @@ twc_group_peer_list_changed_callback(Tox *tox, uint32_t group_number,
void
twc_group_peer_name_callback(Tox *tox, uint32_t group_number,
uint32_t peer_number,
const uint8_t *nick,
size_t nick_len,
void *data);
uint32_t peer_number, const uint8_t *nick,
size_t nick_len, void *data);
void
twc_group_title_callback(Tox *tox, uint32_t group_number, uint32_t peer_number,
const uint8_t *title, size_t length, void *data);
void
twc_file_recv_control_callback(Tox *tox, uint32_t friend_number, uint32_t file_number, TOX_FILE_CONTROL control,
void *user_data);
twc_file_recv_control_callback(Tox *tox, uint32_t friend_number,
uint32_t file_number, TOX_FILE_CONTROL control,
void *user_data);
void
twc_file_chunk_request_callback(Tox *tox, uint32_t friend_number, uint32_t file_number, uint64_t position,
size_t length, void *user_data);
twc_file_chunk_request_callback(Tox *tox, uint32_t friend_number,
uint32_t file_number, uint64_t position,
size_t length, void *user_data);
void
twc_file_recv_callback(Tox *tox, uint32_t friend_number, uint32_t file_number, uint32_t kind, uint64_t file_size,
const uint8_t *filename, size_t filename_length, void *user_data);
twc_file_recv_callback(Tox *tox, uint32_t friend_number, uint32_t file_number,
uint32_t kind, uint64_t file_size,
const uint8_t *filename, size_t filename_length,
void *user_data);
void
twc_file_recv_chunk_callback(Tox *tox, uint32_t friend_number, uint32_t file_number, uint64_t position,
const uint8_t *data, size_t length, void *user_data);
twc_file_recv_chunk_callback(Tox *tox, uint32_t friend_number,
uint32_t file_number, uint64_t position,
const uint8_t *data, size_t length,
void *user_data);
#ifndef NDEBUG
void

View File

@ -245,12 +245,13 @@ twc_tox_err_file_control(enum TOX_ERR_FILE_CONTROL error)
"success",
"the friend number passed did not designate a valid friend",
"this client is currently not connected to the friend",
"no file transfer with the given file number was found for the given friend",
"no file transfer with the given file number was found for the given "
"friend",
"a RESUME control was sent, but the file transfer is running normally",
"A RESUME control was sent, but the file transfer was paused by the other party",
"A RESUME control was sent, but the file transfer was paused by the "
"other party",
"a PAUSE control was sent, but the file transfer was already paused",
"packet queue is full"
};
"packet queue is full"};
return messages[error];
}
@ -259,10 +260,11 @@ twc_tox_err_file_get(enum TOX_ERR_FILE_GET error)
{
char *messages[] = {
"success",
"one of the arguments to the function was NULL when it was not expected",
"one of the arguments to the function was NULL when it was not "
"expected",
"the friend number passed did not designate a valid friend",
"no file transfer with the given number was found for the given friend"
};
"no file transfer with the given number was found for the given "
"friend"};
return messages[error];
}
@ -273,11 +275,11 @@ twc_tox_err_file_seek(enum TOX_ERR_FILE_SEEK error)
"success",
"the friend number passed did not designate a valid friend",
"the client is currently not connected to the friend",
"no file transfer with the given file number was found for the given friend",
"no file transfer with the given file number was found for the given "
"friend",
"file was not in a state where it could be seeked",
"seek position was invalid",
"packet queue is full"
};
"packet queue is full"};
return messages[error];
}
@ -286,12 +288,12 @@ twc_tox_err_file_send(enum TOX_ERR_FILE_SEND error)
{
char *messages[] = {
"success",
"one of the arguments of the function was NULL when it was not expected",
"one of the arguments of the function was NULL when it was not "
"expected",
"the friend number passed did not designate a valid friend",
"this client is currently not connected to the friend",
"filename lenth exceeded TOX_MAX_FILENAME_LENGTH bytes",
"too many ongoing transfers"
};
"too many ongoing transfers"};
return messages[error];
}
@ -303,11 +305,11 @@ twc_tox_err_file_send_chunk(enum TOX_ERR_FILE_SEND_CHUNK error)
"the length parameter was non-zero, but data was NULL",
"the friend number passed did not designate a valid friend",
"this client is currently not connected to the friend",
"no file transfer with the given file number was found for the given friend",
"no file transfer with the given file number was found for the given "
"friend",
"not called from the request chunk callback",
"attempted to send more or less data than requested",
"packet queue is full",
"position parameter was wrong"
};
"position parameter was wrong"};
return messages[error];
}