static void tgp_info_load_channel_done (struct tgl_state *TLS, void *extra, int success, struct tgl_channel *C) { g_return_if_fail(success); PurpleNotifyUserInfo *info = purple_notify_user_info_new (); if (str_not_empty (C->about)) { purple_notify_user_info_add_pair (info, _("Description"), C->about); } if (str_not_empty (C->username)) { char *link = g_strdup_printf ("https://telegram.me/%s", C->username); purple_notify_user_info_add_pair (info, _("Link"), link); g_free (link); } if (str_not_empty (C->print_title)) { purple_notify_user_info_add_pair (info, _("Print Title"), C->print_title); } char *admins = g_strdup_printf ("%d", C->admins_count); purple_notify_user_info_add_pair (info, _("Administrators"), admins); g_free (admins); char *participants = g_strdup_printf ("%d", C->participants_count); purple_notify_user_info_add_pair (info, _("Participants"), participants); g_free (participants); char *kicked = g_strdup_printf ("%d", C->kicked_count); purple_notify_user_info_add_pair (info, _("Kicked"), kicked); g_free (kicked); purple_notify_userinfo (tls_get_conn (TLS), tgp_blist_lookup_purple_name (TLS, C->id), info, NULL, NULL); }
void tgprpl_chat_join (PurpleConnection *gc, GHashTable *data) { debug ("tgprpl_chat_join()"); g_return_if_fail(data); // auto joins will cause chat joins at a time when the dialogue list is not ready, remember // those chats and join them once the dialogue list is fetched if (! gc_get_data (gc)->dialogues_ready) { g_return_if_fail (data); const char *id = g_hash_table_lookup (data, "id"); if (id) { debug ("attempting to join chat %s while not ready, queue up for later", id); gc_get_data (gc)->pending_joins = g_list_append (gc_get_data (gc)->pending_joins, g_strdup (id)); } return; } // join existing chat by id when the user clicks on a chat in the buddy list void *value = g_hash_table_lookup (data, "id"); if (value && atoi (value)) { tgl_peer_id_t cid = TGL_MK_CHAT(atoi (value)); tgl_peer_t *P = tgl_peer_get (gc_get_tls (gc), cid); if (P) { debug ("joining chat by id %d ...", tgl_get_peer_id (cid)); tgl_do_get_chat_info (gc_get_tls (gc), cid, FALSE, tgp_chat_on_loaded_chat_full_joining, NULL); } else { warning ("Cannot join chat %d, peer not found...", tgl_get_peer_id (cid)); purple_serv_got_join_chat_failed (gc, data); } return; } // join chat by invite link provided in the chat join window const char *link = g_hash_table_lookup (data, "link"); if (str_not_empty (link)) { tgl_do_import_chat_link (gc_get_tls (gc), link, (int)strlen (link), tgp_notify_on_error_gw, NULL); return; } // if a chat with this name doesn't exist yet, prompt to create one const char *subject = g_hash_table_lookup (data, "subject"); if (str_not_empty (subject)) { tgl_peer_t *P = tgl_peer_get_by_name (gc_get_tls (gc), subject); // handle joining chats by print_names as used by the Adium plugin if (P && tgl_get_peer_type (P->id) == TGL_PEER_CHAT) { debug ("joining chat by subject %s ...", subject); tgl_do_get_chat_info (gc_get_tls (gc), P->id, FALSE, tgp_chat_on_loaded_chat_full_joining, NULL); return; } // user creates a new chat by providing its subject the chat join window request_create_chat (gc_get_tls (gc), subject); } }
static char *tgp_strdup_determine_filename (const char *mime, const char *caption, int flags, long long hash) { if (caption) { return g_strdup (caption); } const char *type = NULL; if (mime) { if (flags & TGLDF_VIDEO) { // video message type = "mp4"; } else if (flags & TGLDF_AUDIO) { // audio message type = "ogg"; } else { // document message type = tgp_mime_to_filetype (mime); } } if (! str_not_empty(type)) { if (flags & TGLDF_IMAGE) { type = "png"; } else if (flags & TGLDF_AUDIO) { type = "ogg"; } else if (flags & TGLDF_VIDEO) { type = "mp4"; } else if (flags & TGLDF_STICKER) { type = "webp"; } else { type = "bin"; } } return g_strdup_printf ("%lld.%s", ABS(hash), type); }
void tgprpl_chat_join (PurpleConnection * gc, GHashTable *data) { debug ("tgprpl_chat_join()"); connection_data *conn = purple_connection_get_protocol_data (gc); // join existing chat by id when the user clicks on a chat in the buddy list void *value = g_hash_table_lookup (data, "id"); if (value && atoi (value)) { tgl_peer_id_t cid = TGL_MK_CHAT(atoi (value)); tgl_peer_t *P = tgl_peer_get (conn->TLS, cid); if (P) { debug ("joining chat by id %d ...", tgl_get_peer_id (cid)); tgl_do_get_chat_info (conn->TLS, cid, FALSE, tgp_chat_on_loaded_chat_full_joining, NULL); } else { warning ("Cannot join chat %d, peer not found...", tgl_get_peer_id (cid)); purple_serv_got_join_chat_failed (gc, data); } return; } // join chat by invite link provided in the chat join window const char *link = g_hash_table_lookup (data, "link"); if (str_not_empty (link)) { tgl_do_import_chat_link (conn->TLS, link, (int)strlen (link), tgp_notify_on_error_gw, NULL); return; } // if a chat with this name doesn't exist yet, prompt to create one const char *subject = g_hash_table_lookup (data, "subject"); if (str_not_empty (subject)) { tgl_peer_t *P = tgl_peer_get_by_name (conn->TLS, subject); // handle joining chats by print_names as used by the Adium plugin if (P && tgl_get_peer_type (P->id) == TGL_PEER_CHAT) { debug ("joining chat by subject %s ...", subject); tgl_do_get_chat_info (conn->TLS, P->id, FALSE, tgp_chat_on_loaded_chat_full_joining, NULL); return; } // user creates a new chat by providing its subject the chat join window request_create_chat (conn->TLS, subject); } }
static void tgp_info_load_user_done (struct tgl_state *TLS, void *extra, int success, struct tgl_user *U) { g_return_if_fail(success); // user info PurpleNotifyUserInfo *info = purple_notify_user_info_new (); if (str_not_empty (U->first_name) && str_not_empty (U->last_name)) { purple_notify_user_info_add_pair (info, _("First name"), U->first_name); purple_notify_user_info_add_pair (info, _("Last name"), U->last_name); } else { purple_notify_user_info_add_pair (info, _("Name"), U->print_name); } if (str_not_empty (U->username)) { char *username = g_strdup_printf ("@%s", U->username); purple_notify_user_info_add_pair (info, _("Username"), username); g_free (username); } char *status = tgp_format_user_status (&U->status); purple_notify_user_info_add_pair (info, _("Last seen"), status); g_free (status); if (str_not_empty (U->phone)) { char *phone = g_strdup_printf ("+%s", U->phone); purple_notify_user_info_add_pair (info, _("Phone"), phone); g_free (phone); } // secret chat info tgl_peer_t *O = extra; if (O && tgl_get_peer_type (O->id) == TGL_PEER_ENCR_CHAT) { struct tgl_secret_chat *secret = &O->encr_chat; if (secret->state == sc_waiting) { purple_notify_user_info_add_pair (info, "", _("Waiting for the user to get online...")); } else { const char *ttl_key = _("Self destruction timer"); if (secret->ttl) { char *ttl = g_strdup_printf ("%d", secret->ttl); purple_notify_user_info_add_pair (info, ttl_key, ttl); g_free (ttl); } else { purple_notify_user_info_add_pair (info, ttl_key, _("Timer is not enabled.")); } if (secret->first_key_sha[0]) { int sha1key = tgp_visualize_key (TLS, secret->first_key_sha); if (sha1key != -1) { char *ident_icon = tgp_format_img (sha1key); purple_notify_user_info_add_pair (info, _("Secret key"), ident_icon); g_free(ident_icon); } } } } const char *who = NULL; if (tgl_get_peer_type (O->id) == TGL_PEER_ENCR_CHAT) { who = tgp_blist_lookup_purple_name (TLS, O->id); } else { who = tgp_blist_lookup_purple_name (TLS, U->id); } purple_notify_userinfo (tls_get_conn (TLS), who, info, NULL, NULL); }
static void tgp_msg_display (struct tgl_state *TLS, struct tgp_msg_loading *C) { connection_data *conn = TLS->ev_base; struct tgl_message *M = C->msg; char *text = NULL; int flags = 0; if (M->flags & (TGLMF_EMPTY | TGLMF_DELETED)) { return; } if (!(M->flags & TGLMF_CREATED)) { return; } if (!tgl_get_peer_type (M->to_id)) { warning ("Bad msg\n"); return; } // only display new messages, ignore updates or deletions if (!M->message || tgp_outgoing_msg (TLS, M) || !tgl_get_peer_type (M->to_id)) { return; } // Mark messages that contain a mention as if they contained our current nick name // FIXME: doesn't work in Adium if (M->flags & TGLMF_MENTION) { flags |= PURPLE_MESSAGE_NICK; } // handle messages that failed to load if (C->error) { const char *err = C->error_msg; if (! err) { err = _("failed loading message"); } tgp_msg_err_out (TLS, err, tgp_our_msg (TLS, M) ? M->from_id : M->to_id); return; } // format the message text if (M->flags & TGLMF_SERVICE) { text = format_service_msg (TLS, M); flags |= PURPLE_MESSAGE_SYSTEM; } else if (M->media.type != tgl_message_media_none) { switch (M->media.type) { case tgl_message_media_photo: { if (M->media.photo) { g_return_if_fail(C->data != NULL); text = tgp_msg_photo_display (TLS, C->data, &flags); if (str_not_empty (text)) { if (str_not_empty (M->media.caption)) { char *old = text; text = g_strdup_printf ("%s<br>%s", old, M->media.caption); g_free (old); } } } break; } case tgl_message_media_document: if (M->media.document->flags & TGLDF_STICKER) { g_return_if_fail(C->data != NULL); text = tgp_msg_sticker_display (TLS, M->from_id, C->data, &flags); } else if (M->media.document->flags & TGLDF_IMAGE) { g_return_if_fail(C->data != NULL); text = tgp_msg_photo_display (TLS, C->data, &flags); } else { if (! tgp_our_msg(TLS, M)) { tgprpl_recv_file (conn->gc, tgp_blist_lookup_purple_name (TLS, M->from_id), M); } return; } break; case tgl_message_media_video: case tgl_message_media_audio: { if (! tgp_our_msg(TLS, M)) { tgprpl_recv_file (conn->gc, tgp_blist_lookup_purple_name (TLS, M->from_id), M); } } break; case tgl_message_media_document_encr: if (M->media.encr_document->flags & TGLDF_STICKER) { g_return_if_fail(C->data != NULL); text = tgp_msg_sticker_display (TLS, M->from_id, C->data, &flags); } if (M->media.encr_document->flags & TGLDF_IMAGE) { g_return_if_fail(C->data != NULL); text = tgp_msg_photo_display (TLS, C->data, &flags); } else { if (! tgp_our_msg(TLS, M)) { tgprpl_recv_file (conn->gc, tgp_blist_lookup_purple_name (TLS, M->to_id), M); } return; } break; case tgl_message_media_contact: text = g_strdup_printf ("<b>%s %s</b> %s", M->media.first_name, M->media.last_name, M->media.phone); break; case tgl_message_media_venue: { char *address = NULL; if (M->media.venue.address && strcmp (M->media.venue.title, M->media.venue.address)) { address = g_strdup_printf (" %s", M->media.venue.address); } char *pos = format_geo_link_osm (M->media.venue.geo.latitude, M->media.geo.longitude); text = g_strdup_printf ("<a href=\"%s\">%s</a>%s", pos, M->media.venue.title ? M->media.venue.title : "", address ? address : ""); if (address) { g_free (address); } g_free (pos); break; } case tgl_message_media_geo: { char *pos = format_geo_link_osm (M->media.venue.geo.latitude, M->media.geo.longitude); text = g_strdup_printf ("<a href=\"%s\">%s</a>", pos, pos); g_free (pos); break; } case tgl_message_media_webpage: { char *msg = g_strdup (M->message); text = purple_markup_escape_text (msg, strlen (msg)); g_free (msg); break; } default: warning ("received unknown media type: %d", M->media.type); break; } } else { if (str_not_empty (M->message)) { text = purple_markup_escape_text (M->message, strlen (M->message)); } flags |= PURPLE_MESSAGE_RECV; } if (tgl_get_peer_type (M->to_id) != TGL_PEER_ENCR_CHAT && ! (M->flags & TGLMF_UNREAD)) { flags |= PURPLE_MESSAGE_DELAYED; } // some service messages (like removing/adding users from chats) might print the message // text through other means and leave the text empty if (! str_not_empty (text)) { return; } // display the message to the user switch (tgl_get_peer_type (M->to_id)) { case TGL_PEER_CHAT: { tgl_peer_t *P = tgl_peer_get (TLS, M->to_id); g_return_if_fail(P != NULL); if (tgp_chat_show (TLS, &P->chat)) { p2tgl_got_chat_in (TLS, M->to_id, M->from_id, text, flags, M->date); } break; } case TGL_PEER_ENCR_CHAT: { p2tgl_got_im_combo (TLS, M->to_id, text, flags, M->date); break; } case TGL_PEER_USER: { if (tgp_our_msg (TLS, M)) { flags |= PURPLE_MESSAGE_SEND; flags &= ~PURPLE_MESSAGE_RECV; p2tgl_got_im_combo (TLS, M->to_id, text, flags, M->date); } else { p2tgl_got_im_combo (TLS, M->from_id, text, flags, M->date); } break; } } g_free (text); }