Exemple #1
0
END_TEST

START_TEST(test_save_friend)
{
    Tox *tox1 = tox_new(0);
    Tox *tox2 = tox_new(0);
    ck_assert_msg(tox1 || tox2, "Failed to create 2 tox instances");
    uint32_t to_compare = 974536;
    tox_callback_friend_request(tox2, accept_friend_request, &to_compare);
    uint8_t address[TOX_FRIEND_ADDRESS_SIZE];
    tox_get_address(tox2, address);
    int test = tox_add_friend(tox1, address, (uint8_t *)"Gentoo", 7);
    ck_assert_msg(test == 0, "Failed to add friend error code: %i", test);
    uint32_t size = tox_encrypted_size(tox1);
    uint8_t data[size];
    test = tox_encrypted_save(tox1, data, "correcthorsebatterystaple", 25);
    ck_assert_msg(test == 0, "failed to encrypted save");
    ck_assert_msg(tox_is_data_encrypted(data) == 1, "magic number missing");
    Tox *tox3 = tox_new(0);
    test = tox_encrypted_load(tox3, data, size, "correcthorsebatterystaple", 25);
    ck_assert_msg(test == 0, "failed to encrypted load");
    uint8_t address2[TOX_CLIENT_ID_SIZE];
    test = tox_get_client_id(tox3, 0, address2);
    ck_assert_msg(test == 0, "no friends!");
    ck_assert_msg(memcmp(address, address2, TOX_CLIENT_ID_SIZE) == 0, "addresses don't match!");
}
Exemple #2
0
void print_friendlist(Tox *m)
{
    new_lines("[i] Friend List:");

    char name[TOX_MAX_NAME_LENGTH + 1];
    uint8_t fraddr_bin[TOX_FRIEND_ADDRESS_SIZE];
    char fraddr_str[FRADDR_TOSTR_BUFSIZE];

    /* account for the longest name and the longest "base" string and number (int) and id_str */
    char fstring[TOX_MAX_NAME_LENGTH + strlen(ptrn_friend) + 21 + id_str_len];

    uint32_t i = 0;

    while (getfriendname_terminated(m, i, name) != -1) {
        if (!tox_get_client_id(m, i, fraddr_bin))
            fraddr_to_str(fraddr_bin, fraddr_str);
        else
            sprintf(fraddr_str, "???");

        if (strlen(name) <= 0) {
            sprintf(fstring, ptrn_friend, i, "No name?", fraddr_str);
        } else {
            sprintf(fstring, ptrn_friend, i, (uint8_t *)name, fraddr_str);
        }

        i++;
        new_lines(fstring);
    }

    if (i == 0)
        new_lines("+ no friends! D:");
}
Exemple #3
0
/* Writes log filename for fid to dest. returns length written */
static int log_file_name(uint8_t *dest, size_t size_dest, Tox *tox, int fid)
{
    if (size_dest < TOX_CLIENT_ID_SIZE * 2 + sizeof(".txt"))
        return -1;

    uint8_t client_id[TOX_CLIENT_ID_SIZE];
    tox_get_client_id(tox, fid, client_id);
    cid_to_string(dest, client_id); dest += TOX_CLIENT_ID_SIZE * 2;
    memcpy((char*)dest, ".txt", sizeof(".txt"));

    return TOX_CLIENT_ID_SIZE * 2 + sizeof(".txt");
}
Exemple #4
0
void friend_message(Tox *m, int32_t friendnumber, const uint8_t *bin, uint16_t msglength, void *userdata) {
	uint8_t client_id_bin[TOX_CLIENT_ID_SIZE+1];
    uint8_t client_id_str[TOX_CLIENT_ID_SIZE*2+1];
    tox_get_client_id(m,friendnumber,client_id_bin);
    hex_bin_to_string(client_id_bin,TOX_CLIENT_ID_SIZE,client_id_str);
    // 添加消息觸發器
    trigger_msg_listener(msg_listener_list,bin,client_id_str);
        
    if(strcmp(bin,"HANDSHAKE") == 0){
        printf("HANDSHAKE RECEIVED\n");
        return;
    }
        
    // parse message
    uint8_t *uuid = (uint8_t *)malloc(sizeof(uint8_t)*UUID_LENGTH + 1);
	uint8_t *cmd = (uint8_t *)malloc(sizeof(uint8_t)*CMD_STR_LENGTH);
    uint8_t *data = (uint8_t *)malloc(sizeof(uint8_t)*MY_MESSAGE_LENGTH);
    uint32_t *length = (uint32_t *)malloc(sizeof(uint32_t));
    unpack_msg_bin(bin, uuid, cmd, data, length);
	// print msg content
	/*printf("uuid:%s\n", uuid);
	printf("cmd:%s\n", cmd);
	printf("data:%s\n", data);
	printf("length:%d\n", *length);*/
        
    if(strcmp(cmd,"UNKNOWN_CMD") == 0){
            
    }else if(strcmp(cmd,"RAW_DATA") == 0){
        on_remote_data_received(uuid, data, *length, client_id_bin);
    }else{
        printf("CMD:%s\n",cmd);
        if(strcmp(cmd,"CLOSE_SOCK") == 0){
            int32_t sockfd = get_local_socks(msocks_list,uuid);
            close_local_socks(msocks_list,sockfd);
        }
        if(strcmp(cmd, "CREATE_SOCK_SUCCESS") == 0){
            int32_t sockfd = get_local_socks(msocks_list,uuid);
			pthread_t new_sock_thread;
            uint32_t *msockfd = malloc(sizeof(uint32_t));
			*msockfd = sockfd;
            pthread_create(&new_sock_thread, NULL, on_remote_sock_created, (void *)msockfd);
        }
    }
        
    // free data
    free(uuid);
    free(cmd);
    free(data);
    free(length);
}
Exemple #5
0
QString Core::getFriendAddress(int friendNumber) const
{
    // If we don't know the full address of the client, return just the id, otherwise get the full address
    uint8_t rawid[TOX_CLIENT_ID_SIZE];
    tox_get_client_id(tox, friendNumber, rawid);
    QByteArray data((char*)rawid,TOX_CLIENT_ID_SIZE);
    QString id = data.toHex().toUpper();

    QList<QString>& friendAddresses = Settings::getInstance().friendAddresses;
    for (QString addr : friendAddresses)
        if (addr.toUpper().contains(id))
            return addr;

    return id;
}
Exemple #6
0
Fichier : tox.c Projet : Impyy/uTox
void log_write(Tox *tox, int fid, const uint8_t *message, uint16_t length, _Bool self)
{
    if(!logging_enabled) {
        return;
    }

    uint8_t path[512], *p;
    uint8_t name[TOX_MAX_NAME_LENGTH];
    int namelen;
    uint8_t client_id[TOX_CLIENT_ID_SIZE];
    FILE *file;

    p = path + datapath(path);
    tox_get_client_id(tox, fid, client_id);
    cid_to_string(p, client_id); p += TOX_CLIENT_ID_SIZE * 2;
    strcpy((char*)p, ".txt");

    file = fopen((char*)path, "ab");
    if(file) {
        time_t rawtime;
        time(&rawtime);

        if(self) {
            namelen = tox_get_self_name(tox, name);
        } else if((namelen = tox_get_name(tox, fid, name)) == -1) {
            //error reading name
            namelen = 0;
        }

        struct {
            uint64_t time;
            uint16_t namelen, length;
            uint8_t flags, zeroes[3];
        } header = {
            .time = rawtime,
            .namelen = namelen,
            .length = length,
            .flags = self,
        };

        fwrite(&header, sizeof(header), 1, file);
        fwrite(name, namelen, 1, file);
        fwrite(message, length, 1, file);
        fclose(file);
    }
}
Exemple #7
0
void friendlist_onFriendAdded(ToxWindow *self, Tox *m, int32_t num, bool sort)
{
    if (Friends.max_idx < 0)
        return;


    Friends.num_friends = tox_count_friendlist(m);
    realloc_friends(Friends.max_idx + 1);
    memset(&Friends.list[Friends.max_idx], 0, sizeof(ToxicFriend));

    int i;

    for (i = 0; i <= Friends.max_idx; ++i) {
        if (Friends.list[i].active)
            continue;

        Friends.list[i].num = num;
        Friends.list[i].active = true;
        Friends.list[i].chatwin = -1;
        Friends.list[i].online = false;
        Friends.list[i].status = TOX_USERSTATUS_NONE;
        Friends.list[i].logging_on = (bool) user_settings->autolog == AUTOLOG_ON;
        tox_get_client_id(m, num, (uint8_t *) Friends.list[i].pub_key);
        update_friend_last_online(i, tox_get_last_online(m, i));

        char tempname[TOX_MAX_NAME_LENGTH] = {0};
        int len = get_nick_truncate(m, tempname, num);

        if (len == -1 || tempname[0] == '\0') {
            strcpy(Friends.list[i].name, UNKNOWN_NAME);
            Friends.list[i].namelength = strlen(UNKNOWN_NAME);
        } else {    /* Enforce toxic's maximum name length */
            snprintf(Friends.list[i].name, sizeof(Friends.list[i].name), "%s", tempname);
            Friends.list[i].namelength = strlen(Friends.list[i].name);
        }

        if (i == Friends.max_idx)
            ++Friends.max_idx;

        if (sort)
            sort_friendlist_index();

        return;
    }
}
Exemple #8
0
void friendlist_onFriendAdded(ToxWindow *self, Tox *m, int32_t num, bool sort)
{
    if (max_friends_index < 0 || max_friends_index >= MAX_FRIENDS_NUM)
        return;

    int i;

    for (i = 0; i <= max_friends_index; ++i) {
        if (!friends[i].active) {
            friends[i].num = num;
            friends[i].active = true;
            friends[i].chatwin = -1;
            friends[i].online = false;
            friends[i].status = TOX_USERSTATUS_NONE;
            friends[i].logging_on = (bool) user_settings_->autolog == AUTOLOG_ON;
            tox_get_client_id(m, num, (uint8_t *) friends[i].pub_key);
            update_friend_last_online(i, tox_get_last_online(m, i));

            char tempname[TOX_MAX_NAME_LENGTH] = {0};
            int len = get_nick_truncate(m, tempname, num);

            if (len == -1 || tempname[0] == '\0') {
                strcpy(friends[i].name, UNKNOWN_NAME);
                friends[i].namelength = strlen(UNKNOWN_NAME);
            } else {    /* Enforce toxic's maximum name length */
                friends[i].namelength = len;
                snprintf(friends[i].name, sizeof(friends[i].name), "%s", tempname);
            }

            num_friends = tox_count_friendlist(m);

            if (i == max_friends_index)
                ++max_friends_index;

            if (sort)
                sort_friendlist_index();

            return;
        }
    }
}
Exemple #9
0
void friendlist_onFriendAdded(ToxWindow *self, Tox *m, int num, bool sort)
{
    if (max_friends_index < 0 || max_friends_index >= MAX_FRIENDS_NUM)
        return;

    int i;

    for (i = 0; i <= max_friends_index; ++i) {
        if (!friends[i].active) {
            friends[i].num = num;
            friends[i].active = true;
            friends[i].chatwin = -1;
            friends[i].online = false;
            friends[i].status = TOX_USERSTATUS_NONE;
            friends[i].namelength = tox_get_name(m, num, friends[i].name);
            tox_get_client_id(m, num, friends[i].pub_key);

            if (friends[i].namelength == -1 || friends[i].name[0] == '\0') {
                strcpy(friends[i].name, (uint8_t *) UNKNOWN_NAME);
                friends[i].namelength = strlen(UNKNOWN_NAME) + 1;
            } else {    /* Enforce toxic's maximum name length */
                friends[i].name[TOXIC_MAX_NAME_LENGTH] = '\0';
                friends[i].namelength = strlen(friends[i].name) + 1;
            }

            num_friends = tox_count_friendlist(m);

            if (i == max_friends_index)
                ++max_friends_index;

            if (sort)
                sort_friendlist_index(m);

            return;
        }
    }
}
Exemple #10
0
void Core::loadFriends()
{
    const uint32_t friendCount = tox_count_friendlist(tox);
    if (friendCount > 0) {
        // assuming there are not that many friends to fill up the whole stack
        int32_t *ids = new int32_t[friendCount];
        tox_get_friendlist(tox, ids, friendCount);
        uint8_t clientId[TOX_CLIENT_ID_SIZE];
        for (int32_t i = 0; i < static_cast<int32_t>(friendCount); ++i) {
            if (tox_get_client_id(tox, ids[i], clientId) == 0) {
                emit friendAdded(ids[i], CUserId::toString(clientId));

                const int nameSize = tox_get_name_size(tox, ids[i]);
                if (nameSize > 0) {
                    uint8_t *name = new uint8_t[nameSize];
                    if (tox_get_name(tox, ids[i], name) == nameSize) {
                        emit friendUsernameLoaded(ids[i], CString::toString(name, nameSize));
                    }
                    delete[] name;
                }

                const int statusMessageSize = tox_get_status_message_size(tox, ids[i]);
                if (statusMessageSize > 0) {
                    uint8_t *statusMessage = new uint8_t[statusMessageSize];
                    if (tox_get_status_message(tox, ids[i], statusMessage, statusMessageSize) == statusMessageSize) {
                        emit friendStatusMessageLoaded(ids[i], CString::toString(statusMessage, statusMessageSize));
                    }
                    delete[] statusMessage;
                }

                checkLastOnline(ids[i]);
            }

        }
        delete[] ids;
    }
}
Exemple #11
0
static _Bool load_save(Tox *tox)
{
    {
        uint8_t path[512], *p;
        uint32_t size;

        p = path + datapath(path);
        strcpy((char*)p, "tox_save");

        void *data = file_raw((char*)path, &size);
        if(!data) {
            p = path + datapath_old(path);
            strcpy((char*)p, "tox_save");
            data = file_raw((char*)path, &size);
            if (!data) {
                data = file_raw("tox_save", &size);
                if(!data) {
                    return 0;
                }
            }
        }

        tox_load(tox, data, size);
        free(data);
    }

    friends = tox_count_friendlist(tox);

    uint32_t i = 0;
    while(i != friends) {
        int size;
        FRIEND *f = &friend[i];
        uint8_t name[TOX_MAX_NAME_LENGTH];

        f->msg.scroll = 1.0;

        tox_get_client_id(tox, i, f->cid);

        size = tox_get_name(tox, i, name);

        friend_setname(f, name, size);

        size = tox_get_status_message_size(tox, i);
        f->status_message = malloc(size);
        tox_get_status_message(tox, i, f->status_message, size);
        f->status_length = size;

        log_read(tox, i);

        i++;
    }

    self.name_length = tox_get_self_name(tox, self.name);
    self.statusmsg_length = tox_get_self_status_message_size(tox);
    self.statusmsg = malloc(self.statusmsg_length);
    tox_get_self_status_message(tox, self.statusmsg, self.statusmsg_length);
    self.status = tox_get_self_user_status(tox);


    return 1;
}
Exemple #12
0
Fichier : tox.c Projet : Impyy/uTox
void log_read(Tox *tox, int fid)
{
    uint8_t path[512], *p, *pp, *end;
    uint8_t client_id[TOX_CLIENT_ID_SIZE];
    uint32_t size, i;

    p = path + datapath(path);
    tox_get_client_id(tox, fid, client_id);
    cid_to_string(p, client_id); p += TOX_CLIENT_ID_SIZE * 2;
    strcpy((char*)p, ".txt");

    p = pp = file_raw((char*)path, &size);
    if(!p) {
        return;
    }

    end = p + size;

    /* todo: some checks to avoid crashes with corrupted log files */
    /* first find the last 128 messages in the log */
    i = 0;
    while(p < end) {
        uint16_t namelen, length;
        memcpy(&namelen, p + 8, 2);
        memcpy(&length, p + 10, 2);
        p += 16 + namelen + length;;

        if(++i > 128) {
            memcpy(&namelen, pp + 8, 2);
            memcpy(&length, pp + 10, 2);
            pp += 16 + namelen + length;
        }
    }

    if(i > 128) {
        i = 128;
    }

    MSG_DATA *m = &friend[fid].msg;
    m->data = malloc(sizeof(void*) * i);
    m->n = i;
    i = 0;

    /* add the messages */
    p = pp;
    while(p < end) {
        uint64_t time;
        uint16_t namelen, length;
        uint8_t flags;
        memcpy(&time, p, 8);
        memcpy(&namelen, p + 8, 2);
        memcpy(&length, p + 10, 2);
        flags = p[12];
        p += 16;

        MESSAGE *msg = malloc(sizeof(MESSAGE) + length);
        msg->flags = flags;
        msg->length = length;
        memcpy(msg->msg, p + namelen, length);

        struct tm *ti;
        time_t rawtime = time;
        ti = localtime(&rawtime);

        msg->time = ti->tm_hour * 60 + ti->tm_min;

        m->data[i++] = msg;

        debug("loaded backlog: %.*s: %.*s\n", namelen, p, length, p + namelen);
        p += namelen + length;
    }
}