Fix "crashes" on name changes

Do not assume that a name for each ID can only be set once anymore.
Remove assertion that names always exist, return 0 instead and propagate
error handling to the calling function.
This commit is contained in:
mjentsch 2015-10-31 09:50:40 +01:00
parent 87278f6742
commit 81b64d7542
6 changed files with 32 additions and 9 deletions

View file

@ -226,6 +226,9 @@ static void update_chat_handler (struct tgl_state *TLS, struct tgl_chat *chat, u
}
static void update_user_typing (struct tgl_state *TLS, struct tgl_user *U, enum tgl_typing_status status) {
g_return_if_fail (tgp_blist_peer_get_purple_name (TLS, U->id));
if (status == tgl_typing_typing) {
serv_got_typing (tg_get_conn(TLS), tgp_blist_peer_get_purple_name (TLS, U->id), 2, PURPLE_TYPING);
}

View file

@ -163,7 +163,12 @@ tgl_chat_id_t p2tgl_chat_get_id (PurpleChat *PC) {
void p2tgl_conv_add_user (struct tgl_state *TLS, PurpleConversation *conv,
int user, char *message, int flags, int new_arrival) {
purple_conv_chat_add_user (purple_conversation_get_chat_data (conv), tgp_blist_peer_get_purple_name (TLS, TGL_MK_USER (user)), message, flags, new_arrival);
const char *name = tgp_blist_peer_get_purple_name (TLS, TGL_MK_USER (user));
g_return_if_fail (name);
purple_conv_chat_add_user (purple_conversation_get_chat_data (conv), name, message, flags, new_arrival);
}
PurpleNotifyUserInfo *p2tgl_notify_user_info_new (struct tgl_user *U) {

View file

@ -27,16 +27,14 @@
const char *tgp_blist_peer_get_purple_name (struct tgl_state *TLS, tgl_peer_id_t id) {
const char *name = g_hash_table_lookup (tg_get_data (TLS)->id_to_purple_name, GINT_TO_POINTER(tgl_get_peer_id (id)));
if (! name) {
assert (0);
g_warn_if_reached();
return NULL;
}
return name;
}
void tgp_blist_peer_add_purple_name (struct tgl_state *TLS, tgl_peer_id_t id, const char *purple_name) {
assert (g_hash_table_lookup (tg_get_data (TLS)->id_to_purple_name, GINT_TO_POINTER(tgl_get_peer_id (id))) == NULL);
g_hash_table_insert (tg_get_data (TLS)->id_to_purple_name, GINT_TO_POINTER(tgl_get_peer_id (id)),
g_strdup (purple_name));
g_hash_table_replace (tg_get_data (TLS)->id_to_purple_name, GINT_TO_POINTER(tgl_get_peer_id (id)), g_strdup (purple_name));
}
tgl_peer_t *tgp_blist_peer_find (struct tgl_state *TLS, const char *purple_name) {

View file

@ -88,7 +88,12 @@ static void tgp_chat_add_all_users (struct tgl_state *TLS, PurpleConversation *c
int i = 0;
for (; i < C->user_list_size; i++) {
struct tgl_chat_user *uid = (C->user_list + i);
users = g_list_append (users, g_strdup (tgp_blist_peer_get_purple_name (TLS, TGL_MK_USER(uid->user_id))));
const char *name = tgp_blist_peer_get_purple_name (TLS, TGL_MK_USER(uid->user_id));
if (! name) {
g_warn_if_reached();
continue;
}
users = g_list_append (users, g_strdup (name));
flags = g_list_append (flags, GINT_TO_POINTER(C->admin_id == uid->user_id ? PURPLE_CBFLAGS_FOUNDER : PURPLE_CBFLAGS_NONE));
}
purple_conv_chat_add_users (PURPLE_CONV_CHAT(conv), users, NULL, flags, FALSE);

View file

@ -271,8 +271,11 @@ static PurpleXfer *tgprpl_new_xfer_recv (PurpleConnection * gc, const char *who)
return X;
}
void tgprpl_recv_file (PurpleConnection * gc, const char *who, struct tgl_message *M) {
void tgprpl_recv_file (PurpleConnection *gc, const char *who, struct tgl_message *M) {
debug ("tgprpl_recv_file()");
g_return_if_fail (who);
PurpleXfer *X = tgprpl_new_xfer_recv (gc, who);
const char *mime_type, *caption;
long long access_hash;

View file

@ -101,10 +101,13 @@ static char *format_service_msg (struct tgl_state *TLS, struct tgl_message *M) {
PurpleConversation *conv = tgp_chat_show (TLS, &chatPeer->chat);
if (conv) {
char *alias = peer->print_name;
const char *aliasLeft = tgp_blist_peer_get_purple_name (TLS, TGL_MK_USER (M->action.user));
txt = g_strdup_printf (_("%1$s deleted user %2$s."), txt_user, alias);
purple_conv_chat_remove_user (purple_conversation_get_chat_data (conv),
tgp_blist_peer_get_purple_name (TLS, TGL_MK_USER (M->action.user)), txt);
g_return_val_if_fail (aliasLeft, txt);
purple_conv_chat_remove_user (purple_conversation_get_chat_data (conv), aliasLeft, txt);
if (M->action.user == tgl_get_peer_id (TLS->our_id)) {
purple_conv_chat_left (purple_conversation_get_chat_data (conv));
}
@ -243,6 +246,9 @@ void tgp_msg_sys_out (struct tgl_state *TLS, const char *msg, tgl_peer_id_t to_i
case TGL_PEER_ENCR_CHAT: {
const char *name = tgp_blist_peer_get_purple_name (TLS, to_id);
PurpleConversation *conv = p2tgl_find_conversation_with_account (TLS, to_id);
g_return_if_fail (name);
if (! conv) {
conv = purple_conversation_new (PURPLE_CONV_TYPE_IM, tg_get_acc (TLS), name);
}
@ -357,6 +363,9 @@ static char *tgp_msg_sticker_display (struct tgl_state *TLS, tgl_peer_id_t from,
*flags |= PURPLE_MESSAGE_IMAGES;
#else
const char *txt_user = tgp_blist_peer_get_purple_name (TLS, from);
g_return_val_if_fail (txt_user, NULL);
text = g_strdup_printf (_("%s sent a sticker."), txt_user);
*flags |= PURPLE_MESSAGE_SYSTEM;
#endif