Skip to content

Commit

Permalink
Ensure buddies exist when message received
Browse files Browse the repository at this point in the history
If a message arrives from and the user is pulling contacts, then add the
buddy to the contact list if they're not there already. Sometimes the
messages come before the buddy list.

In Bitlbee, a message from someone not in your buddy list gets sent to
the control channel. If messages arrive before the buddy information,
the control channel gets littered. This fix prevents that.
  • Loading branch information
yourealwaysbe committed Jul 25, 2021
1 parent 0cb7076 commit 15ed605
Showing 1 changed file with 56 additions and 16 deletions.
72 changes: 56 additions & 16 deletions libgowhatsapp.c
Original file line number Diff line number Diff line change
Expand Up @@ -282,34 +282,70 @@ PurpleConversation *gowhatsapp_find_conversation(char *username, PurpleAccount *
return conv;
}

static void gowhatsapp_refresh_contactlist(PurpleConnection *pc, gowhatsapp_message_t *gwamsg)
{
GoWhatsappAccount *gwa = purple_connection_get_protocol_data(pc);
static PurpleGroup * gowhatsapp_get_purple_group() {
PurpleGroup *group = purple_blist_find_group("Whatsapp");
if (!group) {
group = purple_group_new("Whatsapp");
purple_blist_add_group(group, NULL);
}
return group;
}

if (!purple_account_get_bool(gwa->account, GOWHATSAPP_FETCH_CONTACTS_OPTION, TRUE)) {
/*
* Ensure buddy in the buddy list. Updates existing entry if there is
* one. In both cases only if fetch contacts is enabled.
*
* Does not add [email protected]!
*/
static void gowhatsapp_ensure_buddy_in_blist(
PurpleAccount *account, char *remoteJid, char *display_name
) {
if (!strcmp(remoteJid, "[email protected]")) {
return;
}

if(!strcmp(gwamsg->remoteJid, "status@broadcast")){
return;
gboolean fetch_contacts = purple_account_get_bool(
account, GOWHATSAPP_FETCH_CONTACTS_OPTION, TRUE
);

PurpleBuddy *buddy = purple_blist_find_buddy(account, remoteJid);

if (!buddy && fetch_contacts) {
PurpleGroup *group = gowhatsapp_get_purple_group();
buddy = purple_buddy_new(account, remoteJid, display_name);
purple_blist_add_buddy(buddy, NULL, group, NULL);
gowhatsapp_assume_buddy_online(account, buddy);
}

if (buddy && fetch_contacts) {
purple_blist_alias_buddy(buddy, display_name);
}
}

static void gowhatsapp_refresh_buddy(
PurpleAccount *account, gowhatsapp_message_t *gwamsg
) {
char *display_name = gwamsg->remoteJid;
if (strlen(gwamsg->text)) {
display_name = gwamsg->text;
}

PurpleBuddy *buddy = purple_blist_find_buddy(gwa->account, gwamsg->remoteJid);
if (!buddy) {
PurpleGroup *group = purple_blist_find_group("Whatsapp");
if (!group) {
group = purple_group_new("Whatsapp");
purple_blist_add_group(group, NULL);
}
buddy = purple_buddy_new(gwa->account, gwamsg->remoteJid, display_name);
purple_blist_add_buddy(buddy, NULL, group, NULL);
gowhatsapp_assume_buddy_online(gwa->account, buddy);
gowhatsapp_ensure_buddy_in_blist(account, gwamsg->remoteJid, display_name);
}

static void gowhatsapp_refresh_contactlist(PurpleConnection *pc, gowhatsapp_message_t *gwamsg)
{
GoWhatsappAccount *gwa = purple_connection_get_protocol_data(pc);

if (!purple_account_get_bool(gwa->account, GOWHATSAPP_FETCH_CONTACTS_OPTION, TRUE)) {
return;
}

if(!strcmp(gwamsg->remoteJid, "status@broadcast")){
return;
}

gowhatsapp_refresh_buddy(gwa->account, gwamsg);
}

static void gowhatsapp_refresh_presence(PurpleConnection *pc, gowhatsapp_message_t *gwamsg)
Expand Down Expand Up @@ -362,6 +398,10 @@ gowhatsapp_display_message(PurpleConnection *pc, gowhatsapp_message_t *gwamsg)
purple_conversation_write(conv, gwamsg->remoteJid, content, flags, gwamsg->timestamp);
}
} else {
// messages sometimes arrive before buddy has been
// created... this method will be missing a display
// name, but i don't think i ever saw one of them anyway
gowhatsapp_ensure_buddy_in_blist(gwa->account, gwamsg->remoteJid, gwamsg->remoteJid);
// normal mode: direct incoming message
purple_serv_got_im(pc, gwamsg->remoteJid, content, flags, gwamsg->timestamp);
}
Expand Down

0 comments on commit 15ed605

Please sign in to comment.