/** * Return the users own name, null-terminated. Must be freed. */ char * twc_get_self_name_nt(Tox *tox) { size_t length = tox_self_get_name_size(tox); uint8_t name[length]; tox_self_get_name(tox, name); return twc_null_terminate(name, length); }
/** * Get the null-terminated name of a Tox friend. Must be freed. */ char * twc_get_name_nt(Tox *tox, int32_t friend_number) { TOX_ERR_FRIEND_QUERY err; size_t length = tox_friend_get_name_size(tox, friend_number, &err); if ((err != TOX_ERR_FRIEND_QUERY_OK) || (length == 0)) return twc_get_friend_id_short(tox, friend_number); uint8_t name[length]; tox_friend_get_name(tox, friend_number, name, &err); return twc_null_terminate(name, length); }
/** * Return the null-terminated status message of a Tox friend. Must be freed. */ char * twc_get_status_message_nt(Tox *tox, int32_t friend_number) { TOX_ERR_FRIEND_QUERY err; size_t length = tox_friend_get_status_message_size(tox, friend_number, &err); if ((err != TOX_ERR_FRIEND_QUERY_OK) || (length == SIZE_MAX)) { char *msg = malloc(1); *msg = 0; return msg; } uint8_t message[length]; tox_friend_get_status_message(tox, friend_number, message, &err); return twc_null_terminate(message, length); }
/** * 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 + 1] = {0}; TOX_ERR_CONFERENCE_PEER_QUERY err = TOX_ERR_CONFERENCE_PEER_QUERY_OK; int length = tox_conference_peer_get_name_size(tox, group_number, peer_number, &err); if ((err == TOX_ERR_CONFERENCE_PEER_QUERY_OK) && (length <= TOX_MAX_NAME_LENGTH)) { tox_conference_peer_get_name(tox, group_number, peer_number, name, &err); if (err == TOX_ERR_CONFERENCE_PEER_QUERY_OK) return twc_null_terminate(name, length); else return "<unknown>"; } else return "<unknown>"; }
void twc_friend_request_callback(Tox *tox, const uint8_t *public_key, const uint8_t *message, uint16_t length, void *data) { struct t_twc_profile *profile = data; char *message_nt = twc_null_terminate(message, length); int rc = twc_friend_request_add(profile, public_key, message_nt); if (rc == -1) { weechat_printf(profile->buffer, "%sReceived a friend request, but your friend request list is full!", weechat_prefix("warning")); } else { char hex_address[TOX_CLIENT_ID_SIZE * 2 + 1]; twc_bin2hex(public_key, TOX_CLIENT_ID_SIZE, hex_address); weechat_printf(profile->buffer, "%sReceived a friend request from %s with message \"%s\"; " "accept it with \"/friend accept %d\"", weechat_prefix("network"), hex_address, message_nt, rc); if (rc == -2) { weechat_printf(profile->buffer, "%sFailed to save friend request, try manually " "accepting with /friend add", weechat_prefix("error")); } } free(message_nt); }
void twc_handle_friend_message(Tox *tox, int32_t friend_number, const uint8_t *message, uint16_t length, void *data, enum TWC_MESSAGE_TYPE message_type) { struct t_twc_profile *profile = data; struct t_twc_chat *chat = twc_chat_search_friend(profile, friend_number, true); char *name = twc_get_name_nt(profile->tox, friend_number); char *message_nt = twc_null_terminate(message, length); twc_chat_print_message(chat, "", name, message_nt, message_type); free(name); free(message_nt); }
void twc_name_change_callback(Tox *tox, int32_t friend_number, const uint8_t *name, uint16_t length, void *data) { struct t_twc_profile *profile = data; struct t_twc_chat *chat = twc_chat_search_friend(profile, friend_number, false); char *old_name = twc_get_name_nt(profile->tox, friend_number); char *new_name = twc_null_terminate(name, length); if (strcmp(old_name, new_name) != 0) { if (chat) { twc_chat_queue_refresh(chat); weechat_printf(chat->buffer, "%s%s is now known as %s", weechat_prefix("network"), old_name, new_name); } weechat_printf(profile->buffer, "%s%s is now known as %s", weechat_prefix("network"), old_name, new_name); } free(old_name); free(new_name); }