diff --git a/telegram-adium/TelegramAccount.m b/telegram-adium/TelegramAccount.m index f48d684..ec9a230 100644 --- a/telegram-adium/TelegramAccount.m +++ b/telegram-adium/TelegramAccount.m @@ -131,7 +131,7 @@ [menu addItemWithTitle:@"Delete and exit..." target:self - action:@selector(deleteAndExitChat) + action:@selector(deleteAndExit) keyEquivalent:@"" tag:0]; @@ -143,20 +143,16 @@ connection_data *conn = purple_connection_get_protocol_data (purple_account_get_connection(account)); AIChat *chat = adium.interfaceController.activeChat; if (chat) { - export_chat_link_checked (conn->TLS, [chat.name UTF8String]); + export_chat_link_by_name (conn->TLS, [chat.name UTF8String]); } } -- (void)deleteAndExitChat +- (void)deleteAndExit { connection_data *conn = purple_connection_get_protocol_data (purple_account_get_connection(account)); AIChat *chat = adium.interfaceController.activeChat; if (chat) { - const char *name = [chat.name UTF8String]; - tgl_peer_t *P = tgp_blist_lookup_peer_get (conn->TLS, name); - if (P) { - leave_and_delete_chat (conn->TLS, P); - } + leave_and_delete_chat_by_name (conn->TLS, [chat.name UTF8String]); } } diff --git a/telegram-adium/TelegramJoinChatViewController.m b/telegram-adium/TelegramJoinChatViewController.m index 9ed1793..3aacd63 100644 --- a/telegram-adium/TelegramJoinChatViewController.m +++ b/telegram-adium/TelegramJoinChatViewController.m @@ -84,7 +84,7 @@ static void tgl_channel_iterator_cb (tgl_peer_t *peer, void *extra) { // Join by link NSString *link = [textField_joinByLink stringValue]; if ([link length]) { - import_chat_link_checked (conn->TLS, [link UTF8String]); + import_chat_link (conn->TLS, [link UTF8String]); return; } diff --git a/telegram-purple.c b/telegram-purple.c index 285f799..ec26ef9 100644 --- a/telegram-purple.c +++ b/telegram-purple.c @@ -247,12 +247,10 @@ static void on_get_dialog_list_done (struct tgl_state *TLS, void *extra, int suc g_warn_if_reached (); continue; } - // our own contact shouldn't show up in our buddy list if (tgl_get_peer_id (UC->id) == tgl_get_peer_id (TLS->our_id)) { continue; } - if (tgl_get_peer_type (UC->id) == TGL_PEER_USER) { if (! (UC->user.flags & TGLUF_DELETED)) { tgp_blist_contact_add (TLS, &UC->user); @@ -327,16 +325,8 @@ static void start_secret_chat (PurpleBlistNode *node, gpointer data) { static void create_chat_link_done (struct tgl_state *TLS, void *extra, int success, const char *url) { tgl_peer_t *P = extra; - if (success) { - assert (tgl_get_peer_type (P->id) == TGL_PEER_CHAT); - /* - tgp_chat_show (TLS, P); - serv_got_chat_in (tls_get_conn (TLS), tgl_get_peer_id (P->id), "WebPage", PURPLE_MESSAGE_SYSTEM, msg, time(NULL)); - */ - char *msg = g_strdup_printf (_("Invite link: %s"), url); - tgp_chat_got_in (TLS, P, P->id, msg, PURPLE_MESSAGE_SYSTEM, time(NULL)); g_free (msg); } else { @@ -344,23 +334,33 @@ static void create_chat_link_done (struct tgl_state *TLS, void *extra, int succe } } -static void export_chat_link_checked_gw (PurpleBlistNode *node, gpointer data) { - PurpleChat *chat = (PurpleChat*)node; - export_chat_link_checked (pbn_get_data (node)->TLS, purple_chat_get_name (chat)); -} - -void export_chat_link_checked (struct tgl_state *TLS, const char *name) { - tgl_peer_t *C = tgp_blist_lookup_peer_get (TLS, name); - if (! C) { - failure ("Chat \"%s\" not found, not exporting link.", name); - return; - } - if (C->chat.admin_id != tgl_get_peer_id (TLS->our_id)) { +void export_chat_link (struct tgl_state *TLS, tgl_peer_t *P) { + if (! (P->flags & (TGLPF_ADMIN | TGLPF_CREATOR))) { + // FIXME: Can TGLCHF_MODERATOR export links? purple_notify_error (_telegram_protocol, _("Creating chat link failed"), _("Creating chat link failed"), _("You need to be admin of the group to do that.")); return; } - tgl_do_export_chat_link (TLS, C->id, create_chat_link_done, C); + + if (tgl_get_peer_type(P->id) == TGL_PEER_CHAT) { + tgl_do_export_chat_link (TLS, P->id, create_chat_link_done, P); + } else if (tgl_get_peer_type(P->id) == TGL_PEER_CHANNEL) { + tgl_do_export_channel_link (TLS, P->id, create_chat_link_done, P); + } else { + g_warn_if_reached(); + } +} + +static void export_chat_link_checked_gw (PurpleBlistNode *node, gpointer data) { + PurpleChat *chat = (PurpleChat*)node; + export_chat_link_by_name (pbn_get_data (node)->TLS, purple_chat_get_name (chat)); +} + +void export_chat_link_by_name (struct tgl_state *TLS, const char *name) { + g_return_if_fail(name); + tgl_peer_t *C = tgp_blist_lookup_peer_get (TLS, name); + g_warn_if_fail(C != NULL); + export_chat_link (TLS, C); } static void leave_and_delete_chat_gw (PurpleBlistNode *node, gpointer data) { @@ -372,16 +372,32 @@ static void leave_and_delete_chat_gw (PurpleBlistNode *node, gpointer data) { void leave_and_delete_chat (struct tgl_state *TLS, tgl_peer_t *P) { g_return_if_fail (P); - if (P && !(P->user.flags & TGLCHF_LEFT)) { - tgl_do_del_user_from_chat (TLS, P->id, TLS->our_id, tgp_notify_on_error_gw, NULL); + + if (tgl_get_peer_type(P->id) == TGL_PEER_CHAT) { + if (!(P->chat.flags & TGLCHF_LEFT)) { + tgl_do_del_user_from_chat (TLS, P->id, TLS->our_id, tgp_notify_on_error_gw, NULL); + } + } else if (tgl_get_peer_type(P->id) == TGL_PEER_CHANNEL) { + tgl_do_leave_channel (TLS, P->id, tgp_notify_on_error_gw, NULL); + } else { + g_return_if_reached(); } + serv_got_chat_left (tls_get_conn (TLS), tgl_get_peer_id (P->id)); + PurpleChat *PC = tgp_blist_chat_find (TLS, P->id); if (PC) { purple_blist_remove_chat (PC); } } +void leave_and_delete_chat_by_name (struct tgl_state *TLS, const char *name) { + g_return_if_fail(name); + tgl_peer_t *P = tgp_blist_lookup_peer_get (TLS, name); + g_return_if_fail(P); + leave_and_delete_chat (TLS, P); +} + static void import_chat_link_done (struct tgl_state *TLS, void *extra, int success) { if (! success) { tgp_notify_on_error_gw (TLS, NULL, success); @@ -391,7 +407,7 @@ static void import_chat_link_done (struct tgl_state *TLS, void *extra, int succe _("Chat added to list of chat rooms.")); } -void import_chat_link_checked (struct tgl_state *TLS, const char *link) { +void import_chat_link (struct tgl_state *TLS, const char *link) { tgl_do_import_chat_link (TLS, link, (int) strlen(link), import_chat_link_done, NULL); } diff --git a/telegram-purple.h b/telegram-purple.h index 4424e90..b67f585 100644 --- a/telegram-purple.h +++ b/telegram-purple.h @@ -96,9 +96,10 @@ extern const char *pk_path; extern const char *user_pk_filename; extern const char *config_dir; extern PurplePlugin *_telegram_protocol; -void export_chat_link_checked (struct tgl_state *TLS, const char *name); -void import_chat_link_checked (struct tgl_state *TLS, const char *link); +void import_chat_link (struct tgl_state *TLS, const char *link); +void export_chat_link_by_name (struct tgl_state *TLS, const char *name); void leave_and_delete_chat (struct tgl_state *TLS, tgl_peer_t *P); +void leave_and_delete_chat_by_name (struct tgl_state *TLS, const char *name); void channel_load_photo (struct tgl_state *TLS, void *extra, int success, struct tgl_channel *C); #endif diff --git a/tgp-chat.c b/tgp-chat.c index a7631c4..9a6a087 100644 --- a/tgp-chat.c +++ b/tgp-chat.c @@ -15,21 +15,19 @@ along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA - Copyright Matthias Jentsch, Ben Wiederhake 2014-2015 + Copyright Matthias Jentsch, Ben Wiederhake 2014-2016 */ #include "tgp-chat.h" GHashTable *tgp_chat_info_new (struct tgl_state *TLS, tgl_peer_t *P) { - GHashTable *ht = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, g_free); g_hash_table_insert (ht, "subject", g_strdup (P->print_name)); - g_hash_table_insert (ht, "id", g_strdup_printf ("%d", tgl_get_peer_id (P->chat.id))); - + g_hash_table_insert (ht, "id", g_strdup_printf ("%d", tgl_get_peer_id (P->id))); + g_hash_table_insert (ht, "type", g_strdup_printf ("%d", tgl_get_peer_type (P->id))); if (tgl_get_peer_type (P->id) == TGL_PEER_CHANNEL) { g_hash_table_insert (ht, "last_server_id", g_strdup_printf ("%d", 0)); } - return ht; } @@ -43,9 +41,13 @@ int tgp_chat_has_id (PurpleChat *C) { } tgl_peer_id_t tgp_chat_get_id (PurpleChat *C) { - const char *id = g_hash_table_lookup (purple_chat_get_components (C), "id"); - assert (id && *id); - return TGL_MK_CHAT(atoi (id)); + const char *I = g_hash_table_lookup (purple_chat_get_components (C), "id"); + const char *T = g_hash_table_lookup (purple_chat_get_components (C), "type"); + int type = TGL_PEER_CHAT; + if (T && *T) { + type = atoi (T); + } + return tgl_set_peer_id (type, (I && *I) ? atoi (I) : 0); } void tgp_chat_blist_store (struct tgl_state *TLS, tgl_peer_t *P, const char *group) { @@ -69,6 +71,8 @@ void tgp_chat_blist_store (struct tgl_state *TLS, tgl_peer_t *P, const char *gro if (PC) { g_hash_table_replace (purple_chat_get_components (PC), g_strdup ("id"), g_strdup_printf ("%d", tgl_get_peer_id (P->id))); + g_hash_table_replace (purple_chat_get_components (PC), g_strdup ("type"), + g_strdup_printf ("%d", tgl_get_peer_type (P->id))); g_hash_table_replace (purple_chat_get_components (PC), g_strdup ("subject"), g_strdup (tgl_get_peer_type (P->id) == TGL_PEER_CHANNEL ? P->channel.title : P->chat.title)); } diff --git a/tgp-chat.h b/tgp-chat.h index 0e6986c..d8e831d 100644 --- a/tgp-chat.h +++ b/tgp-chat.h @@ -41,6 +41,8 @@ PurpleChat *tgp_chat_new (struct tgl_state *TLS, tgl_peer_t *P); tgl_peer_id_t tgp_chat_get_id (PurpleChat *C); int tgp_chat_has_id (PurpleChat *C); +void tgp_chat_blist_store (struct tgl_state *TLS, tgl_peer_t *P, const char *group); + PurpleConversation *tgp_chat_show (struct tgl_state *TLS, tgl_peer_t *P); int tgprpl_send_chat (PurpleConnection *gc, int id, const char *message, PurpleMessageFlags flags); char *tgprpl_get_chat_name (GHashTable *data); diff --git a/tgp-msg.c b/tgp-msg.c index 8b454c7..a9a76af 100644 --- a/tgp-msg.c +++ b/tgp-msg.c @@ -161,13 +161,8 @@ static char *format_service_msg (struct tgl_state *TLS, struct tgl_message *M) { case tgl_message_action_channel_create: { txt = g_strdup_printf (_("Channel %1$s created"), M->action.title); - // Add the new channel to the buddy list - PurpleBuddy *buddy = tgp_blist_buddy_new (TLS, fromPeer); - purple_blist_add_buddy (buddy, NULL, tgp_blist_group_init (_("Telegram Channels")), NULL); - // tgp_info_update_photo (buddy, fromPeer); - - purple_prpl_got_user_status (tls_get_pa (TLS), tgp_blist_lookup_purple_name (TLS, fromPeer->id), - "available", NULL); + // FIXME: check if this makes sense + tgp_chat_blist_store (TLS, fromPeer, _("Telegram Channels")); break; } default: