2014-09-18 18:55:52 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2014 Håvard Pettersson <haavard.pettersson@gmail.com>
|
|
|
|
*
|
|
|
|
* This file is part of Tox-WeeChat.
|
|
|
|
*
|
|
|
|
* Tox-WeeChat 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.
|
|
|
|
*
|
|
|
|
* Tox-WeeChat 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 Tox-WeeChat. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2014-09-02 13:58:34 +02:00
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
2014-09-02 20:24:47 +02:00
|
|
|
#include <weechat/weechat-plugin.h>
|
2014-09-02 18:47:08 +02:00
|
|
|
#include <tox/tox.h>
|
|
|
|
|
2014-09-28 03:29:34 +02:00
|
|
|
#include "twc-utils.h"
|
2014-09-02 13:58:34 +02:00
|
|
|
|
2014-09-28 03:29:34 +02:00
|
|
|
/**
|
|
|
|
* Convert a hex string to it's binary equivalent of max size bytes.
|
|
|
|
*/
|
2014-09-07 03:04:42 +02:00
|
|
|
void
|
2014-09-28 03:29:34 +02:00
|
|
|
twc_hex2bin(const char *hex, size_t size, char *out)
|
2014-09-02 13:58:34 +02:00
|
|
|
{
|
|
|
|
const char *position = hex;
|
|
|
|
|
2014-09-28 03:29:34 +02:00
|
|
|
size_t i;
|
|
|
|
for (i = 0; i < size; ++i)
|
2014-09-02 13:58:34 +02:00
|
|
|
{
|
2014-09-07 03:04:42 +02:00
|
|
|
sscanf(position, "%2hhx", &out[i]);
|
2014-09-02 13:58:34 +02:00
|
|
|
position += 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-28 03:29:34 +02:00
|
|
|
/**
|
|
|
|
* Convert size bytes to a hex string. out must be at lesat size * 2 + 1
|
|
|
|
* bytes.
|
|
|
|
*/
|
2014-09-07 03:04:42 +02:00
|
|
|
void
|
2014-09-28 03:29:34 +02:00
|
|
|
twc_bin2hex(const uint8_t *bin, size_t size, char *out)
|
2014-09-02 13:58:34 +02:00
|
|
|
{
|
2014-09-07 03:04:42 +02:00
|
|
|
char *position = out;
|
2014-09-02 13:58:34 +02:00
|
|
|
|
2014-09-28 03:29:34 +02:00
|
|
|
size_t i;
|
|
|
|
for (i = 0; i < size; ++i)
|
2014-09-02 13:58:34 +02:00
|
|
|
{
|
|
|
|
sprintf(position, "%02X", bin[i]);
|
|
|
|
position += 2;
|
|
|
|
}
|
|
|
|
*position = 0;
|
|
|
|
}
|
|
|
|
|
2014-09-28 03:29:34 +02:00
|
|
|
/**
|
|
|
|
* Return a null-terminated copy of str. Must be freed.
|
|
|
|
*/
|
2014-09-02 18:47:08 +02:00
|
|
|
char *
|
2014-09-28 03:29:34 +02:00
|
|
|
twc_null_terminate(const uint8_t *str, size_t length)
|
2014-09-02 18:47:08 +02:00
|
|
|
{
|
|
|
|
char *str_null = malloc(length + 1);
|
|
|
|
memcpy(str_null, str, length);
|
|
|
|
str_null[length] = 0;
|
|
|
|
|
|
|
|
return str_null;
|
|
|
|
}
|
|
|
|
|
2014-09-28 03:29:34 +02:00
|
|
|
/**
|
|
|
|
* Get the null-terminated name of a Tox friend. Must be freed.
|
|
|
|
*/
|
2014-09-02 18:47:08 +02:00
|
|
|
char *
|
2014-09-28 03:29:34 +02:00
|
|
|
twc_get_name_nt(Tox *tox, int32_t friend_number)
|
2014-09-02 18:47:08 +02:00
|
|
|
{
|
|
|
|
size_t length = tox_get_name_size(tox, friend_number);
|
|
|
|
uint8_t name[length];
|
|
|
|
|
2014-09-02 20:24:47 +02:00
|
|
|
// if no name, return client ID instead
|
2014-09-04 18:58:50 +02:00
|
|
|
if (!length)
|
2014-09-28 03:29:34 +02:00
|
|
|
return twc_get_friend_id_short(tox, friend_number);
|
2014-09-02 20:24:47 +02:00
|
|
|
|
2014-09-07 03:04:42 +02:00
|
|
|
tox_get_name(tox, friend_number, name);
|
2014-09-28 03:29:34 +02:00
|
|
|
return twc_null_terminate(name, length);
|
2014-09-02 18:47:08 +02:00
|
|
|
}
|
|
|
|
|
2014-09-28 03:29:34 +02:00
|
|
|
/**
|
|
|
|
* Return the null-terminated status message of a Tox friend. Must be freed.
|
|
|
|
*/
|
2014-09-02 18:47:08 +02:00
|
|
|
char *
|
2014-09-28 03:29:34 +02:00
|
|
|
twc_get_status_message_nt(Tox *tox, int32_t friend_number)
|
2014-09-02 18:47:08 +02:00
|
|
|
{
|
|
|
|
size_t length = tox_get_status_message_size(tox, friend_number);
|
|
|
|
uint8_t message[length];
|
|
|
|
tox_get_status_message(tox, friend_number, message, length);
|
|
|
|
|
2014-09-28 03:29:34 +02:00
|
|
|
return twc_null_terminate(message, length);
|
2014-09-02 18:47:08 +02:00
|
|
|
}
|
|
|
|
|
2014-10-04 23:13:49 +02:00
|
|
|
/**
|
|
|
|
* Return the name of a group chat peer as a null terminated string. Must be
|
|
|
|
* freed.
|
|
|
|
*/
|
|
|
|
char *
|
|
|
|
twc_get_peer_name_nt(Tox *tox, int32_t group_number, int32_t peer_number)
|
|
|
|
{
|
|
|
|
uint8_t name[TOX_MAX_NAME_LENGTH] = {0};
|
|
|
|
|
|
|
|
tox_group_peername(tox, group_number, peer_number, name);
|
|
|
|
return twc_null_terminate(name, strlen((char *)name));
|
|
|
|
}
|
|
|
|
|
2014-09-28 03:29:34 +02:00
|
|
|
/**
|
|
|
|
* Return the users own name, null-terminated. Must be freed.
|
|
|
|
*/
|
2014-09-02 18:47:08 +02:00
|
|
|
char *
|
2014-09-28 03:29:34 +02:00
|
|
|
twc_get_self_name_nt(Tox *tox)
|
2014-09-02 18:47:08 +02:00
|
|
|
{
|
|
|
|
size_t length = tox_get_self_name_size(tox);
|
|
|
|
uint8_t name[length];
|
|
|
|
tox_get_self_name(tox, name);
|
|
|
|
|
2014-09-28 03:29:34 +02:00
|
|
|
return twc_null_terminate(name, length);
|
2014-09-02 18:47:08 +02:00
|
|
|
}
|
2014-09-28 03:29:34 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a friend's Tox ID in short form. Return value must be freed.
|
|
|
|
*/
|
|
|
|
char *
|
|
|
|
twc_get_friend_id_short(Tox *tox, int32_t friend_number)
|
|
|
|
{
|
|
|
|
uint8_t client_id[TOX_CLIENT_ID_SIZE];
|
|
|
|
tox_get_client_id(tox, friend_number, client_id);
|
|
|
|
|
|
|
|
// TODO: config
|
|
|
|
size_t short_id_length = 8;
|
|
|
|
|
|
|
|
char *hex_address = malloc(short_id_length + 1);
|
|
|
|
twc_bin2hex(client_id,
|
|
|
|
short_id_length / 2,
|
|
|
|
hex_address);
|
|
|
|
|
|
|
|
return hex_address;
|
|
|
|
}
|
|
|
|
|
2014-10-04 23:50:44 +02:00
|
|
|
/**
|
|
|
|
* Reverse the bytes of a 32-bit integer.
|
|
|
|
*/
|
|
|
|
uint32_t
|
|
|
|
twc_uint32_reverse_bytes(uint32_t num)
|
|
|
|
{
|
|
|
|
uint32_t res = 0;
|
|
|
|
|
|
|
|
res += num & 0xFF; num >>= 8; res <<= 8;
|
|
|
|
res += num & 0xFF; num >>= 8; res <<= 8;
|
|
|
|
res += num & 0xFF; num >>= 8; res <<= 8;
|
|
|
|
res += num & 0xFF;
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|