Esempio n. 1
0
static Tox *init_tox(void)
{
    struct Tox_Options tox_opts;
    memset(&tox_opts, 0, sizeof(struct Tox_Options));
    tox_options_default(&tox_opts);

    Tox *m = load_tox(&tox_opts, DATA_FILE);

    if (!m)
        return NULL;

    tox_callback_self_connection_status(m, cb_self_connection_change, NULL);
    tox_callback_friend_connection_status(m, cb_friend_connection_change, NULL);
    tox_callback_friend_request(m, cb_friend_request, NULL);
    tox_callback_friend_message(m, cb_friend_message, NULL);
    tox_callback_group_invite(m, cb_group_invite, NULL);
    tox_callback_group_title(m, cb_group_titlechange, NULL);

    size_t s_len = tox_self_get_status_message_size(m);

    if (s_len == 0) {
        const char *statusmsg = "Send me the command 'help' for more info";
        tox_self_set_status_message(m, (uint8_t *) statusmsg, strlen(statusmsg), NULL);
    }

    size_t n_len = tox_self_get_name_size(m);

    if (n_len == 0)
        tox_self_set_name(m, (uint8_t *) "[LUGNSK]", strlen("[LUGNSK]"), NULL);

    return m;
}
Esempio n. 2
0
/* Updates own statusmessage */
void prompt_update_statusmessage(ToxWindow *prompt, Tox *m, const char *statusmsg)
{
    StatusBar *statusbar = prompt->stb;
    snprintf(statusbar->statusmsg, sizeof(statusbar->statusmsg), "%s", statusmsg);
    size_t len = strlen(statusbar->statusmsg);
    statusbar->statusmsg_len = len;

    TOX_ERR_SET_INFO err;
    tox_self_set_status_message(m, (uint8_t *) statusmsg, len, &err);

    if (err != TOX_ERR_SET_INFO_OK)
        line_info_add(prompt, NULL, NULL, NULL, SYS_MSG, 0, 0, "Failed to set note (error %d)\n", err);
}
Esempio n. 3
0
void Core::setStatusMessage(const QString& message)
{
    CString cMessage(message);

    if (tox_self_set_status_message(tox, cMessage.data(), cMessage.size(), nullptr) == false)
    {
        emit failedToSetStatusMessage(message);
    }
    else
    {
        saveConfiguration();
        emit statusMessageSet(message);
    }
}
Esempio n. 4
0
void Core::setStatusMessage(const QString& message)
{
    CString cMessage(message);

    if (tox_self_set_status_message(tox, cMessage.data(), cMessage.size(), nullptr) == false)
    {
        emit failedToSetStatusMessage(message);
    }
    else
    {
        if (ready)
            profile.saveToxSave();
        emit statusMessageSet(message);
    }
}
Esempio n. 5
0
File: core.cpp Progetto: Pik-9/qTox
void Core::setStatusMessage(const QString& message)
{
    if (message == getStatusMessage())
        return;

    CString cMessage(message);

    if (!tox_self_set_status_message(tox, cMessage.data(), cMessage.size(), nullptr))
    {
        emit failedToSetStatusMessage(message);
        return;
    }

    if (ready)
        profile.saveToxSave();
    emit statusMessageSet(message);
}
Esempio n. 6
0
void Tox_Dispatcher::set_self_status_message(std::string message)
{
    if (message.length() > TOX_MAX_STATUS_MESSAGE_LENGTH) {
        LOG(INFO) << "Trimming self status message to "
            << TOX_MAX_STATUS_MESSAGE_LENGTH << " characters";
        message = message.substr(0, TOX_MAX_STATUS_MESSAGE_LENGTH);
    }
    const size_t len = message.length();
    const uint8_t *s = len == 0 ? nullptr :
        reinterpret_cast<const uint8_t*>(&message[0]);
    TOX_ERR_SET_INFO err;
    lock.lock();
        if(tox_self_set_status_message(tox, s, len, &err)) {
            save_save_data();
        } else {
            LOG(WARNING)
                << "Setting self status message failed with error code: " << err
                << "... keeping the old one";
        }
    lock.unlock();
}
Esempio n. 7
0
static void cmd_statusmessage(Tox *m, uint32_t friendnum, int argc, char (*argv)[MAX_COMMAND_LENGTH])
{
    const char *outmsg = NULL;

    if (!friend_is_master(m, friendnum)) {
        authent_failed(m, friendnum);
        return;
    }

    if (argc < 1) {
        outmsg = "Error: message required";
        tox_friend_send_message(m, friendnum, TOX_MESSAGE_TYPE_NORMAL, (uint8_t *) outmsg, strlen(outmsg), NULL);
        return;
    }

    if (argv[1][0] != '\"') {
        outmsg = "Error: message must be enclosed in quotes";
        tox_friend_send_message(m, friendnum, TOX_MESSAGE_TYPE_NORMAL, (uint8_t *) outmsg, strlen(outmsg), NULL);
        return;
    }

    /* remove opening and closing quotes */
    char msg[MAX_COMMAND_LENGTH];
    snprintf(msg, sizeof(msg), "%s", &argv[1][1]);
    int len = strlen(msg) - 1;
    msg[len] = '\0';

    tox_self_set_status_message(m, (uint8_t *) msg, len, NULL);

    char name[TOX_MAX_NAME_LENGTH];
    tox_friend_get_name(m, friendnum, (uint8_t *) name, NULL);
    size_t nlen = tox_friend_get_name_size(m, friendnum, NULL);
    name[nlen] = '\0';

    printf("%s set status message to \"%s\"\n", name, msg);
    save_data(m, DATA_FILE);
}
Esempio n. 8
0
static int r2tox_connect() {
	if (tox) {
		printf ("Status: Online\n");
		print_tox_my_address (tox);
		return -1;
	}
	Tox *t = NULL;
	struct Tox_Options *options = tox_options_new(NULL);
	FILE *fd = fopen("tox.data", "rb");
	if (fd) {
		eprintf ("Using tox.data\n");
		size_t sz = fread (&data, 1, 4096, fd);
		fclose (fd);
		tox_options_set_savedata_length (options, sz);
		tox_options_set_savedata_type (options, TOX_SAVEDATA_TYPE_TOX_SAVE);
		tox_options_set_savedata_data (options, data, sz);
		t = tox_new (options, NULL);
		if (!t) {
			printf("cannot new\n");
			return 1;
		}
	} else {
		t = tox_new (NULL, NULL);
		if (!t) {
			eprintf ("cannot new\n");
			return 1;
		}
		// r2tox_save();
	}

	const char *username = "******";
	const char *status = "Available";
	tox_self_set_name (t, username, strlen(username), NULL);
	tox_self_set_status_message (t, status, strlen(status), NULL);

	tox_callback_friend_name(t, handle_friend_name);
	tox_callback_friend_request (t, handle_friend_request);
	tox_callback_friend_message (t, handle_friend_message);
	tox_callback_friend_lossy_packet (t, handle_friend_lossy_packet);
	tox_callback_friend_lossless_packet (t, handle_friend_lossless_packet);
	tox_callback_friend_read_receipt (t, handle_friend_read_receipt);
	tox_callback_conference_invite(t, handle_conference_invite);
	tox_callback_conference_message(t, handle_conference_message);
	tox_callback_conference_title(t, handle_conference_title);

	// bootstrap
	size_t i;
	for (i = 0; i < sizeof(nodes)/sizeof(DHT_node); i ++) {
		sodium_hex2bin(nodes[i].key_bin, sizeof(nodes[i].key_bin),
				nodes[i].key_hex, sizeof(nodes[i].key_hex)-1, NULL, NULL, NULL);
		tox_bootstrap(t, nodes[i].ip, nodes[i].port, nodes[i].key_bin, NULL);
	}

	print_tox_my_address (t);
	tox_callback_self_connection_status (t, self_connection_status_cb);

	tox = t;
	// thread here
	if (!thread) {
		thread = r_th_new (r2tox_mainloop, NULL, 1);
		r_th_start (thread, true);
	}
	return 0;
}
Esempio n. 9
0
static void test_one(void)
{
    uint8_t name[TOX_MAX_NAME_LENGTH];
    uint8_t status_message[TOX_MAX_STATUS_MESSAGE_LENGTH];

    uint8_t name2[TOX_MAX_NAME_LENGTH];
    uint8_t status_message2[TOX_MAX_STATUS_MESSAGE_LENGTH];

    uint32_t index[] = { 1, 2 };
    Tox *tox1 = tox_new_log(nullptr, nullptr, &index[0]);
    set_random_name_and_status_message(tox1, name, status_message);
    Tox *tox2 = tox_new_log(nullptr, nullptr, &index[1]);
    set_random_name_and_status_message(tox2, name2, status_message2);

    uint8_t address[TOX_ADDRESS_SIZE];
    tox_self_get_address(tox1, address);
    Tox_Err_Friend_Add error;
    uint32_t ret = tox_friend_add(tox1, address, (const uint8_t *)"m", 1, &error);
    ck_assert_msg(ret == UINT32_MAX && error == TOX_ERR_FRIEND_ADD_OWN_KEY, "Adding own address worked.");

    tox_self_get_address(tox2, address);
    uint8_t message[TOX_MAX_FRIEND_REQUEST_LENGTH + 1];
    ret = tox_friend_add(tox1, address, nullptr, 0, &error);
    ck_assert_msg(ret == UINT32_MAX && error == TOX_ERR_FRIEND_ADD_NULL, "Sending request with no message worked.");
    ret = tox_friend_add(tox1, address, message, 0, &error);
    ck_assert_msg(ret == UINT32_MAX && error == TOX_ERR_FRIEND_ADD_NO_MESSAGE, "Sending request with no message worked.");
    ret = tox_friend_add(tox1, address, message, sizeof(message), &error);
    ck_assert_msg(ret == UINT32_MAX && error == TOX_ERR_FRIEND_ADD_TOO_LONG,
                  "TOX_MAX_FRIEND_REQUEST_LENGTH is too big.");

    address[0]++;
    ret = tox_friend_add(tox1, address, (const uint8_t *)"m", 1, &error);
    ck_assert_msg(ret == UINT32_MAX && error == TOX_ERR_FRIEND_ADD_BAD_CHECKSUM,
                  "Adding address with bad checksum worked.");

    tox_self_get_address(tox2, address);
    ret = tox_friend_add(tox1, address, message, TOX_MAX_FRIEND_REQUEST_LENGTH, &error);
    ck_assert_msg(ret == 0 && error == TOX_ERR_FRIEND_ADD_OK, "Failed to add friend.");
    ret = tox_friend_add(tox1, address, message, TOX_MAX_FRIEND_REQUEST_LENGTH, &error);
    ck_assert_msg(ret == UINT32_MAX && error == TOX_ERR_FRIEND_ADD_ALREADY_SENT, "Adding friend twice worked.");

    tox_self_set_name(tox1, name, sizeof(name), nullptr);
    ck_assert_msg(tox_self_get_name_size(tox1) == sizeof(name), "Can't set name of TOX_MAX_NAME_LENGTH");

    tox_self_set_status_message(tox1, status_message, sizeof(status_message), nullptr);
    ck_assert_msg(tox_self_get_status_message_size(tox1) == sizeof(status_message),
                  "Can't set status message of TOX_MAX_STATUS_MESSAGE_LENGTH");

    tox_self_get_address(tox1, address);
    size_t save_size = tox_get_savedata_size(tox1);
    VLA(uint8_t, data, save_size);
    tox_get_savedata(tox1, data);

    tox_kill(tox2);
    Tox_Err_New err_n;

    struct Tox_Options *options = tox_options_new(nullptr);
    tox_options_set_savedata_type(options, TOX_SAVEDATA_TYPE_TOX_SAVE);
    tox_options_set_savedata_data(options, data, save_size);
    tox2 = tox_new_log(options, &err_n, &index[1]);
    ck_assert_msg(err_n == TOX_ERR_NEW_OK, "Load failed");

    ck_assert_msg(tox_self_get_name_size(tox2) == sizeof name, "Wrong name size.");
    ck_assert_msg(tox_self_get_status_message_size(tox2) == sizeof status_message, "Wrong status message size");

    uint8_t name_loaded[TOX_MAX_NAME_LENGTH] = { 0 };
    tox_self_get_name(tox2, name_loaded);
    ck_assert_msg(!memcmp(name, name_loaded, sizeof name), "Wrong name.");

    uint8_t status_message_loaded[TOX_MAX_STATUS_MESSAGE_LENGTH] = { 0 };
    tox_self_get_status_message(tox2, status_message_loaded);
    ck_assert_msg(!memcmp(status_message, status_message_loaded, sizeof status_message_loaded), "Wrong status message.");

    uint8_t address2[TOX_ADDRESS_SIZE] = { 0 };
    tox_self_get_address(tox2, address2);
    ck_assert_msg(memcmp(address2, address, TOX_ADDRESS_SIZE) == 0, "Wrong address.");
    uint8_t new_name[TOX_MAX_NAME_LENGTH] = { 0 };
    tox_self_get_name(tox2, new_name);
    ck_assert_msg(memcmp(name, new_name, TOX_MAX_NAME_LENGTH) == 0, "Wrong name");

    uint8_t sk[TOX_SECRET_KEY_SIZE];
    tox_self_get_secret_key(tox2, sk);
    tox_kill(tox2);

    tox_options_default(options);
    tox_options_set_savedata_type(options, TOX_SAVEDATA_TYPE_SECRET_KEY);
    tox_options_set_savedata_data(options, sk, sizeof(sk));
    tox2 = tox_new_log(options, &err_n, &index[1]);
    ck_assert_msg(err_n == TOX_ERR_NEW_OK, "Load failed");
    uint8_t address3[TOX_ADDRESS_SIZE];
    tox_self_get_address(tox2, address3);
    ck_assert_msg(memcmp(address3, address, TOX_PUBLIC_KEY_SIZE) == 0, "Wrong public key.");
    uint8_t pk[TOX_PUBLIC_KEY_SIZE];
    tox_self_get_public_key(tox2, pk);
    ck_assert_msg(memcmp(pk, address, TOX_PUBLIC_KEY_SIZE) == 0, "Wrong public key.");

    tox_options_free(options);
    tox_kill(tox1);
    tox_kill(tox2);
}
Esempio n. 10
0
static void test_set_status_message(void)
{
    printf("initialising 2 toxes\n");
    uint32_t index[] = { 1, 2 };
    const time_t cur_time = time(nullptr);
    Tox *const tox1 = tox_new_log(nullptr, nullptr, &index[0]);
    Tox *const tox2 = tox_new_log(nullptr, nullptr, &index[1]);

    ck_assert_msg(tox1 && tox2, "failed to create 2 tox instances");

    printf("tox1 adds tox2 as friend, tox2 adds tox1\n");
    uint8_t public_key[TOX_PUBLIC_KEY_SIZE];
    tox_self_get_public_key(tox2, public_key);
    tox_friend_add_norequest(tox1, public_key, nullptr);
    tox_self_get_public_key(tox1, public_key);
    tox_friend_add_norequest(tox2, public_key, nullptr);

    printf("bootstrapping tox2 off tox1\n");
    uint8_t dht_key[TOX_PUBLIC_KEY_SIZE];
    tox_self_get_dht_id(tox1, dht_key);
    const uint16_t dht_port = tox_self_get_udp_port(tox1, nullptr);

    tox_bootstrap(tox2, "localhost", dht_port, dht_key, nullptr);

    while (tox_self_get_connection_status(tox1) == TOX_CONNECTION_NONE ||
            tox_self_get_connection_status(tox2) == TOX_CONNECTION_NONE) {
        tox_iterate(tox1, nullptr);
        tox_iterate(tox2, nullptr);

        c_sleep(ITERATION_INTERVAL);
    }

    printf("toxes are online, took %ld seconds\n", time(nullptr) - cur_time);
    const time_t con_time = time(nullptr);

    while (tox_friend_get_connection_status(tox1, 0, nullptr) != TOX_CONNECTION_UDP ||
            tox_friend_get_connection_status(tox2, 0, nullptr) != TOX_CONNECTION_UDP) {
        tox_iterate(tox1, nullptr);
        tox_iterate(tox2, nullptr);

        c_sleep(ITERATION_INTERVAL);
    }

    printf("tox clients connected took %ld seconds\n", time(nullptr) - con_time);

    TOX_ERR_SET_INFO err_n;
    tox_callback_friend_status_message(tox2, status_callback);
    bool ret = tox_self_set_status_message(tox1, (const uint8_t *)STATUS_MESSAGE, sizeof(STATUS_MESSAGE),
                                           &err_n);
    ck_assert_msg(ret && err_n == TOX_ERR_SET_INFO_OK, "tox_self_set_status_message failed because %u\n", err_n);

    bool status_updated = false;

    while (!status_updated) {
        tox_iterate(tox1, nullptr);
        tox_iterate(tox2, &status_updated);
        c_sleep(ITERATION_INTERVAL);
    }

    ck_assert_msg(tox_friend_get_status_message_size(tox2, 0, nullptr) == sizeof(STATUS_MESSAGE),
                  "status message length not correct");
    uint8_t cmp_status[sizeof(STATUS_MESSAGE)];
    tox_friend_get_status_message(tox2, 0, cmp_status, nullptr);
    ck_assert_msg(memcmp(cmp_status, STATUS_MESSAGE, sizeof(STATUS_MESSAGE)) == 0,
                  "status message not correct");

    printf("test_set_status_message succeeded, took %ld seconds\n", time(nullptr) - cur_time);

    tox_kill(tox1);
    tox_kill(tox2);
}