/** * Add a friend message to the message queue and tries to send it if the * friend is online. Handles splitting of messages. (TODO: actually split messages) */ void twc_message_queue_add_friend_message(struct t_twc_profile *profile, int32_t friend_number, const char *message, enum TWC_MESSAGE_TYPE message_type) { struct t_twc_queued_message *queued_message = malloc(sizeof(struct t_twc_queued_message)); time_t rawtime = time(NULL); queued_message->time = malloc(sizeof(struct tm)); memcpy(queued_message->time, gmtime(&rawtime), sizeof(struct tm)); queued_message->message = strdup(message); queued_message->message_type = message_type; // create a queue if needed and add message struct t_twc_list *message_queue = twc_message_queue_get_or_create(profile, friend_number); twc_list_item_new_data_add(message_queue, queued_message); // flush if friend is online if (profile->tox && tox_get_friend_connection_status(profile->tox, friend_number) == 1) twc_message_queue_flush_friend(profile, friend_number); }
/** * Add a new friend request to a profile. * * Returns index on success, -1 on a full friend request list and -2 for any * other error. */ int twc_friend_request_add(struct t_twc_profile *profile, const uint8_t *client_id, const char *message) { size_t max_request_count = TWC_PROFILE_OPTION_INTEGER(profile, TWC_PROFILE_OPTION_MAX_FRIEND_REQUESTS); if (profile->friend_requests->count >= max_request_count) return -1; // create a new request struct t_twc_friend_request *request = malloc(sizeof(struct t_twc_friend_request)); if (!request) return -2; request->profile = profile; request->message = strdup(message); memcpy(request->tox_id, client_id, TOX_PUBLIC_KEY_SIZE); if (!twc_list_item_new_data_add(profile->friend_requests, request)) return -2; return profile->friend_requests->count - 1; }
/** * Return a list of all friend requests for the given profile. */ struct t_twc_list * twc_sqlite_friend_requests(struct t_twc_profile *profile) { int64_t profile_id; if (!twc_sqlite_profile_id(profile, &profile_id)) { weechat_printf(NULL, "missing profile!"); return NULL; } TWC_SQLITE_STMT(statement, "SELECT id, tox_id, message " "FROM friend_requests " "WHERE profile_id == ?"); sqlite3_bind_int(statement, 1, profile_id); struct t_twc_list *friend_requests = twc_list_new(); int rc; while ((rc = sqlite3_step(statement)) == SQLITE_ROW) { struct t_twc_friend_request *request = twc_sqlite_friend_request_row(statement, profile); twc_list_item_new_data_add(friend_requests, request); } TWC_SQLITE_DEBUG_RC(rc, SQLITE_DONE) return friend_requests; }
/** * Add a new group invite to a profile. * * Returns the index of the invite on success, -1 on error. */ int twc_group_chat_invite_add(struct t_twc_profile *profile, int32_t friend_number, uint8_t group_chat_type, uint8_t *data, size_t size) { /* create a new invite object */ struct t_twc_group_chat_invite *invite = malloc(sizeof(struct t_twc_group_chat_invite)); if (!invite) return -1; uint8_t *data_copy = malloc(size); memcpy(data_copy, data, size); invite->profile = profile; invite->friend_number = friend_number; invite->group_chat_type = group_chat_type; invite->data = data_copy; 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); else invite->autojoin_delay = 0; twc_list_item_new_data_add(profile->group_chat_invites, invite); return profile->group_chat_invites->count - 1; }
/** * Add a new group invite to a profile. * * Returns the index of the invite on success, -1 on error. */ int twc_group_chat_invite_add(struct t_twc_profile *profile, int32_t friend_number, uint8_t *data, size_t size) { // create a new invite object struct t_twc_group_chat_invite *invite = malloc(sizeof(struct t_twc_group_chat_invite)); if (!invite) return -1; uint8_t *data_copy = malloc(size); memcpy(data_copy, data, size); invite->profile = profile; invite->friend_number = friend_number; invite->data = data_copy; invite->data_size = size; twc_list_item_new_data_add(profile->group_chat_invites, invite); return profile->group_chat_invites->count - 1; }