From 4d044144b39b822138088b268a45cb71cd62f779 Mon Sep 17 00:00:00 2001 From: Jaidyn Ann Date: Sat, 3 Jul 2021 15:00:05 -0500 Subject: [PATCH] (purple) Roster management Fixes #28 --- protocols/purple/PurpleApp.cpp | 63 +++++++++++++++++++++++++++-- protocols/purple/PurpleProtocol.cpp | 23 +++++++++++ protocols/purple/PurpleProtocol.h | 2 + 3 files changed, 85 insertions(+), 3 deletions(-) diff --git a/protocols/purple/PurpleApp.cpp b/protocols/purple/PurpleApp.cpp index 641a100..aa4b9b4 100644 --- a/protocols/purple/PurpleApp.cpp +++ b/protocols/purple/PurpleApp.cpp @@ -33,6 +33,7 @@ #include #include +#include #include "Purple.h" #include "PurpleMessages.h" @@ -156,7 +157,7 @@ PurpleApp::ImMessage(BMessage* msg) case IM_SEND_MESSAGE: { BString body; - if (msg->FindString("body", &body) != B_OK) return; + if (msg->FindString("body", &body) != B_OK) break; PurpleConversation* conv = _ConversationFromMessage(msg); PurpleConvChat* chat = purple_conversation_get_chat_data(conv); @@ -217,7 +218,7 @@ PurpleApp::ImMessage(BMessage* msg) PurpleConversation* conv = _ConversationFromMessage(msg); PurpleConvChat* chat = purple_conversation_get_chat_data(conv); PurpleConvIm* im = purple_conversation_get_im_data(conv); - if (chat == NULL && im == NULL) return; + if (chat == NULL && im == NULL) break; BStringList user_ids; if (im != NULL) @@ -239,11 +240,14 @@ PurpleApp::ImMessage(BMessage* msg) } case IM_GET_CONTACT_LIST: { + PurpleAccount* account = _AccountFromMessage(msg); + BStringList user_ids; GSList* buddies = purple_blist_get_buddies(); for (int i = 0; buddies != NULL; buddies = buddies->next) { PurpleBuddy* buddy = (PurpleBuddy*)buddies->data; - user_ids.Add(BString(purple_buddy_get_name(buddy))); + if (purple_buddy_get_account(buddy) == account) + user_ids.Add(BString(purple_buddy_get_name(buddy))); } BMessage roster(IM_MESSAGE); @@ -252,6 +256,59 @@ PurpleApp::ImMessage(BMessage* msg) SendMessage(_AccountFromMessage(msg), roster); break; } + case IM_CONTACT_LIST_ADD_CONTACT: + { + PurpleAccount* account = _AccountFromMessage(msg); + BString user_id = msg->FindString("user_id"); + const char* user_name = msg->FindString("user_name"); + if (user_id.IsEmpty() == true || account == NULL) break; + + PurpleGroup* group = purple_find_group("Buddies"); + if (group == NULL) { + group = purple_group_new("Buddies"); + purple_blist_add_group(group, NULL); + } + + PurpleBuddy* buddy = + purple_buddy_new(account, user_id.String(), user_name); + + purple_blist_add_buddy(buddy, NULL, group, NULL); + purple_account_add_buddy_with_invite(account, buddy, NULL); + break; + } + case IM_CONTACT_LIST_REMOVE_CONTACT: + { + PurpleAccount* account = _AccountFromMessage(msg); + BString user_id = msg->FindString("user_id"); + if (user_id.IsEmpty() == true || account == NULL) break; + + PurpleBuddy* buddy = purple_find_buddy(account, user_id.String()); + if (buddy == NULL) return; + purple_blist_remove_buddy(buddy); + purple_account_remove_buddy(account, buddy, NULL); + break; + } + case IM_GET_EXTENDED_CONTACT_INFO: + { + PurpleAccount* account = _AccountFromMessage(msg); + BString user_id = msg->FindString("user_id"); + if (user_id.IsEmpty() == true || account == NULL) break; + + PurpleBuddy* buddy = purple_find_buddy(account, user_id.String()); + if (buddy == NULL) return; + + BString user_name = purple_buddy_get_alias(buddy); + if (user_name.IsEmpty() == true) + user_name = purple_buddy_get_server_alias(buddy); + + BMessage info(IM_MESSAGE); + info.AddInt32("im_what", IM_EXTENDED_CONTACT_INFO); + info.AddString("user_id", user_id); + if (user_name.IsEmpty() == false) + info.AddString("user_name", user_name); + SendMessage(account, info); + break; + } case IM_ROOM_SEND_INVITE: { PurpleAccount* account = _AccountFromMessage(msg); diff --git a/protocols/purple/PurpleProtocol.cpp b/protocols/purple/PurpleProtocol.cpp index 46bb474..f49d55d 100644 --- a/protocols/purple/PurpleProtocol.cpp +++ b/protocols/purple/PurpleProtocol.cpp @@ -208,6 +208,8 @@ PurpleProtocol::UpdateSettings(BMessage* msg) BMessage PurpleProtocol::SettingsTemplate(const char* name) { + if (strcmp(name, "roster") == 0) + return _RosterTemplate(); return fSettingsTemplate; } @@ -320,3 +322,24 @@ PurpleProtocol::_SendPrplMessage(BMessage* msg) if (fPrplMessenger->IsValid()) fPrplMessenger->SendMessage(msg); } + + +BMessage +PurpleProtocol::_RosterTemplate() +{ + BMessage temp; + BMessage id; + id.AddString("name", "user_id"); + id.AddString("description", "Username:"); + id.AddString("error", "You can't friend someone without a nick."); + id.AddInt32("type", B_STRING_TYPE); + temp.AddMessage("setting", &id); + + BMessage name; + name.AddString("name", "user_name"); + name.AddString("description", "Alias:"); + name.AddInt32("type", B_STRING_TYPE); + temp.AddMessage("setting", &name); + + return temp; +} diff --git a/protocols/purple/PurpleProtocol.h b/protocols/purple/PurpleProtocol.h index b7d3afe..fd0f2a5 100644 --- a/protocols/purple/PurpleProtocol.h +++ b/protocols/purple/PurpleProtocol.h @@ -81,6 +81,8 @@ public: private: void _SendPrplMessage(BMessage* msg); + BMessage _RosterTemplate(); + ChatProtocolMessengerInterface* fMessenger; BMessenger* fPrplMessenger; thread_id fBirdThread;