diff options
author | jgeboski <jgeboski@gmail.com> | 2015-01-24 14:34:43 -0500 |
---|---|---|
committer | jgeboski <jgeboski@gmail.com> | 2015-01-24 14:34:43 -0500 |
commit | 06956d1aae2252b33a8f981e3b73c4cd1c2452b6 (patch) | |
tree | 3a9ddffa020db227e78b693ea2b8a63c047f2450 | |
parent | a41d5d2fb4b81de3a46d3d652feac246aa5eddee (diff) | |
download | bitlbee-facebook-06956d1aae2252b33a8f981e3b73c4cd1c2452b6.tar.gz bitlbee-facebook-06956d1aae2252b33a8f981e3b73c4cd1c2452b6.tar.bz2 bitlbee-facebook-06956d1aae2252b33a8f981e3b73c4cd1c2452b6.tar.xz |
facebook: create group chats directly with the fbjoin command
Currently, the plugin relies on the 'chat add' root command in order to
create a new group chat. Aside from being an ugly hack, this prevents
the plugin from being able to automatically join the bitlbee user into
the newly created channel. In order to allow for automatic joining, the
plugin should directly create the group chat, rather than relying on
the "chat add" root command.
-rw-r--r-- | README | 2 | ||||
-rw-r--r-- | facebook/facebook.c | 85 | ||||
-rw-r--r-- | facebook/facebook.h | 6 |
3 files changed, 77 insertions, 16 deletions
@@ -43,14 +43,12 @@ Usage: Group Chats (existing chat): > fbchats [acc] > fbjoin [acc] <index> <channel> - > /join #<channel> > /topic <message> > /invite <user> Group Chats (creating chat): > fbcreate [acc] <user,user,...> > fbjoin [acc] 1 <channel> - > /join #<channel> > /topic <message> > /invite <user> diff --git a/facebook/facebook.c b/facebook/facebook.c index e153893..e2b1e6a 100644 --- a/facebook/facebook.c +++ b/facebook/facebook.c @@ -399,6 +399,63 @@ void fb_data_free(fb_data_t *fata) } /** + * Creates a new #groupchat and adds it to the #fb_data. The returned + * #groupchat should be freed with #fb_data_groupchat_free() when no + * longer needed. + * + * @param ic The #im_connection. + * @param tid The thread #fb_id. + * @param name The name of the channel. + * + * @return The #groupchat or NULL on error. + **/ +struct groupchat *fb_data_groupchat_new(struct im_connection *ic, + fb_id_t tid, + const gchar *name) +{ + fb_data_t *fata = ic->proto_data; + struct groupchat *gc; + gchar stid[FB_ID_STRMAX]; + + FB_ID_TO_STR(tid, stid); + + if (bee_chat_by_title(ic->bee, ic, stid) != NULL) + return NULL; + + gc = imcb_chat_new(ic, stid); + fata->gcs = g_slist_prepend(fata->gcs, gc); + + if (name != NULL) + imcb_chat_name_hint(gc, name); + + imcb_chat_add_buddy(gc, ic->acc->user); + fb_api_thread_info(fata->api, tid); + + return gc; +} + +/** + * Frees all memory used by a #groupchat and removes it from the + * #fb_data. + * + * @param gc The #groupchat. + **/ +void fb_data_groupchat_free(struct groupchat *gc) +{ + fb_data_t *fata; + + if (G_UNLIKELY(gc == NULL)) + return; + + if (G_LIKELY(gc->ic != NULL)) { + fata = gc->ic->proto_data; + fata->gcs = g_slist_remove(fata->gcs, gc); + } + + imcb_chat_free(gc); +} + +/** * Implements #prpl->init(). This initializes an account. * * @param acc The #account. @@ -608,10 +665,7 @@ void fb_chat_invite(struct groupchat *gc, char *who, char *message) **/ void fb_chat_leave(struct groupchat *gc) { - fb_data_t *fata = gc->ic->proto_data; - - fata->gcs = g_slist_remove(fata->gcs, gc); - imcb_chat_free(gc); + fb_data_groupchat_free(gc); } /** @@ -647,13 +701,15 @@ struct groupchat *fb_chat_join(struct im_connection *ic, const char *room, fb_id_t tid; struct groupchat *gc; - gc = imcb_chat_new(ic, room); - fata->gcs = g_slist_prepend(fata->gcs, gc); - imcb_chat_add_buddy(gc, ic->acc->user); - tid = FB_ID_FROM_STR(room); - fb_api_thread_info(fata->api, tid); + gc = fb_data_groupchat_new(ic, tid, NULL); + if (gc == NULL) { + imcb_error(fata->ic, "Failed to join chat: %" FB_ID_FORMAT, tid); + return NULL; + } + + fb_api_thread_info(fata->api, tid); return gc; } @@ -870,9 +926,9 @@ static void fb_cmd_fbjoin(irc_t *irc, char **args) account_t *acc; fb_data_t *fata; fb_id_t *tid; + gchar *name; guint oset; gint64 indx; - gchar stid[FB_ID_STRMAX]; acc = fb_cmd_account(irc, args, 2, &oset); @@ -880,6 +936,7 @@ static void fb_cmd_fbjoin(irc_t *irc, char **args) return; fata = acc->ic->proto_data; + name = args[oset + 1]; indx = g_ascii_strtoll(args[oset], NULL, 10); tid = g_slist_nth_data(fata->tids, indx - 1); @@ -888,10 +945,10 @@ static void fb_cmd_fbjoin(irc_t *irc, char **args) return; } - FB_ID_TO_STR(*tid, stid); - - gchar *cmd[] = {"chat", "add", acc->tag, stid, args[oset + 1], NULL}; - root_command(irc, cmd); + if (fb_data_groupchat_new(acc->ic, *tid, name) == NULL) { + imcb_error(fata->ic, "Failed to join chat: %s (%" FB_ID_FORMAT ")", + name, *tid); + } } /** diff --git a/facebook/facebook.h b/facebook/facebook.h index f397fa8..f5ff7ea 100644 --- a/facebook/facebook.h +++ b/facebook/facebook.h @@ -45,4 +45,10 @@ fb_data_t *fb_data_new(account_t *acc); void fb_data_free(fb_data_t *fata); +struct groupchat *fb_data_groupchat_new(struct im_connection *ic, + fb_id_t tid, + const gchar *name); + +void fb_data_groupchat_free(struct groupchat *gc); + #endif /* _FACEBOOK_H */ |