diff --git a/msglog.c b/msglog.c index 0518025..c44abbc 100644 --- a/msglog.c +++ b/msglog.c @@ -20,60 +20,54 @@ #endif void hexdump (int *in_ptr, int *in_end) { - // TODO: figure out how to log hexdumps to purple log - int *ptr = in_ptr; - while (ptr < in_end) { - ++ ptr; - //printf (" %08x", *(ptr ++)); - } - //printf ("\n"); + // TODO: figure out how to log hexdumps to purple log + int *ptr = in_ptr; + while (ptr < in_end) { + ++ ptr; + //printf (" %08x", *(ptr ++)); + } + //printf ("\n"); } -void log_level_printf (const char* format, va_list ap, int level, char *color) -{ - char buffer[256]; - vsnprintf(buffer, sizeof(buffer), format, ap); - purple_debug(level, PLUGIN_ID, "%s%s%s ", color, buffer, COLOR_NORMAL); +void log_level_printf (const char* format, va_list ap, int level, char *color) { + char buffer[256]; + vsnprintf(buffer, sizeof(buffer), format, ap); + purple_debug(level, PLUGIN_ID, "%s%s%s ", color, buffer, COLOR_NORMAL); } -void debug(const char* format, ...) -{ - va_list ap; - va_start (ap, format); - log_level_printf (format, ap, PURPLE_DEBUG_MISC, COLOR_NORMAL); - va_end (ap); +void debug(const char* format, ...) { + va_list ap; + va_start (ap, format); + log_level_printf (format, ap, PURPLE_DEBUG_MISC, COLOR_NORMAL); + va_end (ap); } -void info(const char* format, ...) -{ - va_list ap; - va_start (ap, format); - log_level_printf (format, ap, PURPLE_DEBUG_INFO, COLOR_GREEN); - va_end (ap); +void info(const char* format, ...) { + va_list ap; + va_start (ap, format); + log_level_printf (format, ap, PURPLE_DEBUG_INFO, COLOR_GREEN); + va_end (ap); } -void warning(const char* format, ...) -{ - va_list ap; - va_start (ap, format); - log_level_printf (format, ap, PURPLE_DEBUG_WARNING, COLOR_YELLOW); - va_end (ap); +void warning(const char* format, ...) { + va_list ap; + va_start (ap, format); + log_level_printf (format, ap, PURPLE_DEBUG_WARNING, COLOR_YELLOW); + va_end (ap); } -void failure(const char* format, ...) -{ - va_list ap; - va_start (ap, format); - log_level_printf (format, ap, PURPLE_DEBUG_ERROR, COLOR_YELLOW); - va_end (ap); +void failure(const char* format, ...) { + va_list ap; + va_start (ap, format); + log_level_printf (format, ap, PURPLE_DEBUG_ERROR, COLOR_YELLOW); + va_end (ap); } -void fatal(const char* format, ...) -{ - va_list ap; - va_start (ap, format); - log_level_printf (format, ap, PURPLE_DEBUG_FATAL, COLOR_REDB); - va_end (ap); - info ("\n"); +void fatal(const char* format, ...) { + va_list ap; + va_start (ap, format); + log_level_printf (format, ap, PURPLE_DEBUG_FATAL, COLOR_REDB); + va_end (ap); + info ("\n"); } diff --git a/telegram-base.c b/telegram-base.c index 4a123bf..695d6d5 100644 --- a/telegram-base.c +++ b/telegram-base.c @@ -275,47 +275,47 @@ static void request_code (struct tgl_state *TLS) { telegram_conn *conn = TLS->ev_base; purple_request_input ( - conn->gc, // handle (the PurpleAccount) - "Telegram Code", // title + conn->gc, // handle (the PurpleAccount) + "Telegram Code", // title "Enter Telegram Code", // primary "Telegram wants to verify your identity, please enter the code, that you have received via SMS.", // secondary - NULL, // default_value - 0, // multiline - 0, // masked - "code", // hint - "OK", // ok_text - G_CALLBACK(request_code_entered), // ok_cb - "Cancel", // cancel_text - G_CALLBACK(request_code_canceled), // cancel_cb - conn->pa, // account - NULL, // who - NULL, // conv - TLS // user_data + NULL, // default_value + 0, // multiline + 0, // masked + "code", // hint + "OK", // ok_text + G_CALLBACK(request_code_entered), + "Cancel", // cancel_text + G_CALLBACK(request_code_canceled), + conn->pa, // account + NULL, // who + NULL, // conv + TLS // user_data ); } static void request_name_and_code (struct tgl_state *TLS) { - telegram_conn *conn = TLS->ev_base; + telegram_conn *conn = TLS->ev_base; - debug ("Phone is not registered, registering...\n"); + debug ("Phone is not registered, registering...\n"); - PurpleRequestFields* fields = purple_request_fields_new(); - PurpleRequestField* field = 0; + PurpleRequestFields* fields = purple_request_fields_new(); + PurpleRequestField* field = 0; - PurpleRequestFieldGroup* group = purple_request_field_group_new("Registration"); - field = purple_request_field_string_new("first_name", "First Name", "", 0); - purple_request_field_group_add_field(group, field); - field = purple_request_field_string_new("last_name", "Last Name", "", 0); - purple_request_field_group_add_field(group, field); - purple_request_fields_add_group(fields, group); + PurpleRequestFieldGroup* group = purple_request_field_group_new("Registration"); + field = purple_request_field_string_new("first_name", "First Name", "", 0); + purple_request_field_group_add_field(group, field); + field = purple_request_field_string_new("last_name", "Last Name", "", 0); + purple_request_field_group_add_field(group, field); + purple_request_fields_add_group(fields, group); - group = purple_request_field_group_new("Authorization"); - field = purple_request_field_string_new("code", "Telegram Code", "", 0); - purple_request_field_group_add_field(group, field); - purple_request_fields_add_group(fields, group); + group = purple_request_field_group_new("Authorization"); + field = purple_request_field_string_new("code", "Telegram Code", "", 0); + purple_request_field_group_add_field(group, field); + purple_request_fields_add_group(fields, group); - purple_request_fields(conn->gc, "Register", "Please register your phone number.", NULL, fields, "Ok", - G_CALLBACK( request_name_code_entered ), "Cancel", NULL, conn->pa, NULL, NULL, conn->gc); + purple_request_fields(conn->gc, "Register", "Please register your phone number.", NULL, fields, "Ok", + G_CALLBACK( request_name_code_entered ), "Cancel", NULL, conn->pa, NULL, NULL, conn->gc); } static void sign_in_callback (struct tgl_state *TLS, void *extra, int success, int registered, const char *mhash) { diff --git a/telegram-purple.c b/telegram-purple.c index 067c480..cf6015d 100644 --- a/telegram-purple.c +++ b/telegram-purple.c @@ -66,150 +66,125 @@ void on_user_get_info (struct tgl_state *TLS, void *show_info, int success, stru static void add_all_users_to_chat (PurpleConversation *pc, struct tgl_chat *chat); static telegram_conn *get_conn_from_buddy (PurpleBuddy *buddy) { - telegram_conn *c = purple_connection_get_protocol_data ( - purple_account_get_connection (purple_buddy_get_account (buddy))); - return c; + telegram_conn *c = purple_connection_get_protocol_data ( + purple_account_get_connection (purple_buddy_get_account (buddy))); + return c; } static const char *format_time(time_t date) { - struct tm *datetime = localtime(&date); - return purple_utf8_strftime("%Y.%m.%d %H:%M:%S", datetime); + struct tm *datetime = localtime(&date); + return purple_utf8_strftime("%Y.%m.%d %H:%M:%S", datetime); } static char *format_status(struct tgl_user_status *status) { - return status->online ? "Online" : "Mobile"; + return status->online ? "Online" : "Mobile"; } static char *format_img_full(int imgstore) { - return g_strdup_printf ("
", imgstore); + return g_strdup_printf ("
", imgstore); } static char *format_img_thumb(int imgstore, char *filename) { - return g_strdup_printf("
%s", - filename, imgstore, filename, filename); + return g_strdup_printf("
%s", + filename, imgstore, filename, filename); } -static void on_update_user_name (struct tgl_state *TLS, tgl_peer_t *user) -{ - debug("on_update_user_name(id=%d, type=%d)\n", user->id.id, user->id.type); - p2tgl_got_alias(TLS, user->id, p2tgl_strdup_alias(user)); +static void on_update_user_name (struct tgl_state *TLS, tgl_peer_t *user) { + debug("on_update_user_name(id=%d, type=%d)\n", user->id.id, user->id.type); + p2tgl_got_alias(TLS, user->id, p2tgl_strdup_alias(user)); } -static void on_update_chat_participants (struct tgl_state *TLS, tgl_peer_t *chat) -{ - debug("on_update_chat_participants(chat.id=%d)\n", chat->id.id); - // Refresh chat participants - PurpleConversation *pc = purple_find_chat(tg_get_conn(TLS), chat->chat.id.id); - if (pc) { - purple_conv_chat_clear_users (purple_conversation_get_chat_data(pc)); - add_all_users_to_chat (pc, chat); +static void on_update_chat_participants (struct tgl_state *TLS, tgl_peer_t *chat) { + debug("on_update_chat_participants(chat.id=%d)\n", chat->id.id); + // Refresh chat participants + PurpleConversation *pc = purple_find_chat(tg_get_conn(TLS), chat->chat.id.id); + if (pc) { + purple_conv_chat_clear_users (purple_conversation_get_chat_data(pc)); + add_all_users_to_chat (pc, chat); + } +} + +static int our_msg (struct tgl_state *TLS, struct tgl_message *M) { + return tgl_get_peer_id(M->from_id) == TLS->our_id; +} + +static PurpleConversation *chat_show (PurpleConnection *gc, int id) { + debug ("show chat"); + telegram_conn *conn = purple_connection_get_protocol_data(gc); + + PurpleConversation *convo = purple_find_chat(gc, id); + if (! convo) { + // chat exists + gchar *name = g_strdup_printf ("%d", id); + if (! g_hash_table_contains (conn->joining_chats, name)) { + // mark chat as already joining + g_hash_table_insert(conn->joining_chats, name, 0); + + // join chat first + tgl_do_get_chat_info (conn->TLS, TGL_MK_CHAT(id), 0, on_chat_get_info, NULL); } + g_free(name); + } + return convo; } -static int our_msg (struct tgl_state *TLS, struct tgl_message *M) -{ - return tgl_get_peer_id(M->from_id) == TLS->our_id; +static int chat_add_message(struct tgl_state *TLS, struct tgl_message *M) { + telegram_conn *conn = TLS->ev_base; + + if (chat_show (conn->gc, M->to_id.id)) { + p2tgl_got_chat_in(TLS, M->to_id, M->from_id, M->message, PURPLE_MESSAGE_RECV, M->date); + return 1; + } else { + // add message once the chat was initialised + g_queue_push_tail (conn->new_messages, M); + return 0; + } } -static PurpleConversation *chat_show (PurpleConnection *gc, int id) -{ - debug ("show chat"); - telegram_conn *conn = purple_connection_get_protocol_data(gc); - - PurpleConversation *convo = purple_find_chat(gc, id); - if (! convo) { - // chat exists - gchar *name = g_strdup_printf ("%d", id); - if (! g_hash_table_contains (conn->joining_chats, name)) { - // mark chat as already joining - g_hash_table_insert(conn->joining_chats, name, 0); - - // join chat first - tgl_do_get_chat_info (conn->TLS, TGL_MK_CHAT(id), 0, on_chat_get_info, NULL); - } - g_free(name); - } - return convo; +static void add_all_users_to_chat (PurpleConversation *pc, struct tgl_chat *chat) { + struct tgl_chat_user *curr = chat->user_list; + if (!curr) { + warning ("add_all_users_to_chat: chat contains no user list, cannot add users\n."); + return; + } + + int i; + for (i = 0; i < chat->user_list_size; i++) { + struct tgl_chat_user *uid = (curr + i); + int flags = (chat->admin_id == uid->user_id ? PURPLE_CBFLAGS_FOUNDER : PURPLE_CBFLAGS_NONE); + p2tgl_conv_add_user(pc, *uid, NULL, flags, 0); + } } -static int chat_add_message(struct tgl_state *TLS, struct tgl_message *M) -{ - telegram_conn *conn = TLS->ev_base; - - if (chat_show (conn->gc, M->to_id.id)) { - p2tgl_got_chat_in(TLS, M->to_id, M->from_id, M->message, PURPLE_MESSAGE_RECV, M->date); - return 1; - } else { - // add message once the chat was initialised - g_queue_push_tail (conn->new_messages, M); - return 0; - } +static const char *tgprpl_list_icon(PurpleAccount * acct, PurpleBuddy * buddy) { + return "telegram"; } -static void add_all_users_to_chat (PurpleConversation *pc, struct tgl_chat *chat) -{ - struct tgl_chat_user *curr = chat->user_list; - if (!curr) { - warning ("add_all_users_to_chat: chat contains no user list, cannot add users\n."); - return; - } - - int i; - for (i = 0; i < chat->user_list_size; i++) { - struct tgl_chat_user *uid = (curr + i); - int flags = (chat->admin_id == uid->user_id ? PURPLE_CBFLAGS_FOUNDER : PURPLE_CBFLAGS_NONE); - p2tgl_conv_add_user(pc, *uid, NULL, flags, 0); - } +static void tgprpl_tooltip_text(PurpleBuddy * buddy, PurpleNotifyUserInfo * info, gboolean full) { + debug ("tgprpl_tooltip_text()\n", buddy->name); + + tgl_peer_id_t *peer = purple_buddy_get_protocol_data(buddy); + if (!peer) { + purple_notify_user_info_add_pair_plaintext(info, "Status", "Offline"); + return; + } + tgl_peer_t *P = tgl_peer_get (get_conn_from_buddy(buddy)->TLS, *peer); + if (!P) { + warning ("tgprpl_tooltip_text: warning peer with id %d not found in tree.\n", peer->id); + return; + } + purple_notify_user_info_add_pair_plaintext(info, "Status", format_status(&P->user.status)); + purple_notify_user_info_add_pair_plaintext(info, "Last seen: ", format_time(P->user.status.when)); } -/** - * Returns the base icon name for the given buddy and account. - * If buddy is NULL and the account is non-NULL, it will return the - * name to use for the account's icon. If both are NULL, it will - * return the name to use for the protocol's icon. - * - * This must be implemented. - */ -static const char *tgprpl_list_icon(PurpleAccount * acct, PurpleBuddy * buddy) -{ - return "telegram"; -} - -/** - * Allows the prpl to add text to a buddy's tooltip. - */ -static void tgprpl_tooltip_text(PurpleBuddy * buddy, PurpleNotifyUserInfo * info, gboolean full) -{ - debug ("tgprpl_tooltip_text()\n", buddy->name); - - tgl_peer_id_t *peer = purple_buddy_get_protocol_data(buddy); - if (!peer) { - purple_notify_user_info_add_pair_plaintext(info, "Status", "Offline"); - return; - } - tgl_peer_t *P = tgl_peer_get (get_conn_from_buddy(buddy)->TLS, *peer); - if (!P) { - warning ("tgprpl_tooltip_text: warning peer with id %d not found in tree.\n", peer->id); - return; - } - purple_notify_user_info_add_pair_plaintext(info, "Status", format_status(&P->user.status)); - purple_notify_user_info_add_pair_plaintext(info, "Last seen: ", format_time(P->user.status.when)); -} - -/** - * Handle a proxy-close of telegram - * - * Remove all open inputs added to purple - */ - gboolean queries_timerfunc (gpointer data) { debug ("queries_timerfunc()\n"); telegram_conn *conn = data; if (conn->updated) { - debug ("State updated, storing current session...\n"); - conn->updated = 0; - write_state_file (conn->TLS); + debug ("State updated, storing current session...\n"); + conn->updated = 0; + write_state_file (conn->TLS); } return 1; } @@ -222,9 +197,9 @@ void telegram_on_ready (struct tgl_state *TLS) { purple_blist_add_account(conn->pa); tggroup = purple_find_group("Telegram"); if (tggroup == NULL) { - purple_debug_info (PLUGIN_ID, "PurpleGroup = NULL, creating"); - tggroup = purple_group_new ("Telegram"); - purple_blist_add_group (tggroup, NULL); + purple_debug_info (PLUGIN_ID, "PurpleGroup = NULL, creating"); + tggroup = purple_group_new ("Telegram"); + purple_blist_add_group (tggroup, NULL); } tgl_do_get_difference (TLS, 0, 0, 0); @@ -247,207 +222,201 @@ struct tgl_update_callback tgp_callback = { .type_notification = on_user_typing }; -/** - * This must be implemented. - */ -static void tgprpl_login(PurpleAccount * acct) -{ - purple_debug_info (PLUGIN_ID, "tgprpl_login()\n"); - PurpleConnection *gc = purple_account_get_connection(acct); - char const *username = purple_account_get_username(acct); - - struct tgl_state *TLS = tgl_state_alloc (); - - const char *dir = config_dir; - struct passwd *pw = getpwuid(getuid()); - int len = strlen (dir) + strlen (pw->pw_dir) + 2 + strlen (username); - TLS->base_path = malloc (len); - snprintf (TLS->base_path, len, "%s/%s/%s", pw->pw_dir, dir, username); - debug ("base configuration path: '%s'", TLS->base_path); - g_mkdir_with_parents(TLS->base_path, 0700); +static void tgprpl_login(PurpleAccount * acct) { + purple_debug_info (PLUGIN_ID, "tgprpl_login()\n"); + PurpleConnection *gc = purple_account_get_connection(acct); + char const *username = purple_account_get_username(acct); + + struct tgl_state *TLS = tgl_state_alloc (); + + const char *dir = config_dir; + struct passwd *pw = getpwuid(getuid()); + int len = strlen (dir) + strlen (pw->pw_dir) + 2 + strlen (username); + TLS->base_path = malloc (len); + snprintf (TLS->base_path, len, "%s/%s/%s", pw->pw_dir, dir, username); + debug ("base configuration path: '%s'", TLS->base_path); + g_mkdir_with_parents(TLS->base_path, 0700); - len += strlen ("/downloads"); - char *ddir = malloc (len); - sprintf (ddir, "%s/downloads", TLS->base_path); - tgl_set_download_directory (TLS, ddir); - g_mkdir_with_parents(ddir, 0700); - free (ddir); + len += strlen ("/downloads"); + char *ddir = malloc (len); + sprintf (ddir, "%s/downloads", TLS->base_path); + tgl_set_download_directory (TLS, ddir); + g_mkdir_with_parents(ddir, 0700); + free (ddir); - tgl_set_verbosity (TLS, 4); - tgl_set_rsa_key (TLS, pk_path); + tgl_set_verbosity (TLS, 4); + tgl_set_rsa_key (TLS, pk_path); - // create handle to store additional info for libpurple in - // the new telegram instance - telegram_conn *conn = g_new0(telegram_conn, 1); - conn->TLS = TLS; - conn->gc = gc; - conn->pa = acct; - conn->new_messages = g_queue_new(); - conn->joining_chats = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free); - purple_connection_set_protocol_data (gc, conn); - - tgl_set_ev_base (TLS, conn); - tgl_set_net_methods (TLS, &tgp_conn_methods); - tgl_set_timer_methods (TLS, &tgp_timers); - tgl_set_callback (TLS, &tgp_callback); + // create handle to store additional info for libpurple in + // the new telegram instance + telegram_conn *conn = g_new0(telegram_conn, 1); + conn->TLS = TLS; + conn->gc = gc; + conn->pa = acct; + conn->new_messages = g_queue_new(); + conn->joining_chats = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free); + purple_connection_set_protocol_data (gc, conn); + + tgl_set_ev_base (TLS, conn); + tgl_set_net_methods (TLS, &tgp_conn_methods); + tgl_set_timer_methods (TLS, &tgp_timers); + tgl_set_callback (TLS, &tgp_callback); - tgl_init (TLS); - purple_connection_set_state (conn->gc, PURPLE_CONNECTING); + tgl_init (TLS); + purple_connection_set_state (conn->gc, PURPLE_CONNECTING); - telegram_login (TLS); + telegram_login (TLS); } /** * This must be implemented. */ -static void tgprpl_close(PurpleConnection * gc) -{ - purple_debug_info(PLUGIN_ID, "tgprpl_close()\n"); - telegram_conn *conn = purple_connection_get_protocol_data(gc); - purple_timeout_remove(conn->timer); - tgl_free_all (conn->TLS); +static void tgprpl_close(PurpleConnection * gc) { + purple_debug_info(PLUGIN_ID, "tgprpl_close()\n"); + telegram_conn *conn = purple_connection_get_protocol_data(gc); + purple_timeout_remove(conn->timer); + tgl_free_all (conn->TLS); } -void message_received_handler(struct tgl_state *TLS, struct tgl_message *M) -{ - if (M->service) { - // TODO: handle service messages properly, currently adding them - // causes a segfault for an unknown reason - debug ("service message, skipping...\n"); - return; - } - if (M->flags & (FLAG_MESSAGE_EMPTY | FLAG_DELETED)) { - return; - } - if (! (M->flags & FLAG_CREATED)) { - return; - } - if (! tgl_get_peer_type (M->to_id)) { - warning ("Bad msg\n"); - return; - } +void message_received_handler(struct tgl_state *TLS, struct tgl_message *M) { + if (M->service) { + // TODO: handle service messages properly, currently adding them + // causes a segfault for an unknown reason + debug ("service message, skipping...\n"); + return; + } + if (M->flags & (FLAG_MESSAGE_EMPTY | FLAG_DELETED)) { + return; + } + if (! (M->flags & FLAG_CREATED)) { + return; + } + if (! tgl_get_peer_type (M->to_id)) { + warning ("Bad msg\n"); + return; + } - switch (tgl_get_peer_type (M->to_id)) { - case TGL_PEER_CHAT: - debug ("PEER_CHAT\n"); - chat_add_message (TLS, M); - break; - - case TGL_PEER_USER: - debug ("PEER_USER\n"); - if (our_msg(TLS, M)) { - p2tgl_got_im (TLS, M->to_id, M->message, PURPLE_MESSAGE_SEND, M->date); - } else { - p2tgl_got_im (TLS, M->from_id, M->message, PURPLE_MESSAGE_RECV, M->date); - } - break; - - case TGL_PEER_ENCR_CHAT: - break; - - case TGL_PEER_GEO_CHAT: - break; - } - - telegram_conn *conn = TLS->ev_base; - conn->updated = 1; + switch (tgl_get_peer_type (M->to_id)) { + case TGL_PEER_CHAT: + debug ("PEER_CHAT\n"); +#ifdef ADIUM_PLUGIN + // don't add our own chat messages in Adium, or + // else they will show up twice + // if (our_msg(TLS, M)) { return; } +#endif + chat_add_message (TLS, M); + break; + + case TGL_PEER_USER: + debug ("PEER_USER\n"); + if (our_msg(TLS, M)) { + p2tgl_got_im (TLS, M->to_id, M->message, PURPLE_MESSAGE_SEND, M->date); + } else { + p2tgl_got_im (TLS, M->from_id, M->message, PURPLE_MESSAGE_RECV, M->date); + } + break; + + case TGL_PEER_ENCR_CHAT: + break; + + case TGL_PEER_GEO_CHAT: + break; + } + + telegram_conn *conn = TLS->ev_base; + conn->updated = 1; } -void on_new_user_status (struct tgl_state *TLS, void *peer) -{ - tgl_peer_t *p = peer; - p2tgl_prpl_got_user_status(TLS, p->id, &p->user.status); +void on_new_user_status (struct tgl_state *TLS, void *peer) { + tgl_peer_t *p = peer; + p2tgl_prpl_got_user_status(TLS, p->id, &p->user.status); } -void on_user_typing (struct tgl_state *TLS, struct tgl_user *U, enum tgl_typing_status status) -{ - if (status == tgl_typing_typing) { - p2tgl_got_typing(TLS, U->id, 2); - } +void on_user_typing (struct tgl_state *TLS, struct tgl_user *U, enum tgl_typing_status status) { + if (status == tgl_typing_typing) { + p2tgl_got_typing(TLS, U->id, 2); + } } -void user_update_handler (struct tgl_state *TLS, struct tgl_user *user, unsigned flags) -{ - if (!(flags & FLAG_CREATED)) { return; } +void user_update_handler (struct tgl_state *TLS, struct tgl_user *user, unsigned flags) { + if (!(flags & FLAG_CREATED)) { return; } - if (TLS->our_id == tgl_get_peer_id (user->id)) { - p2tgl_connection_set_display_name (TLS, (tgl_peer_t *)user); - } else { - PurpleBuddy *buddy = p2tgl_buddy_find(TLS, user->id); - if (!buddy) { - buddy = p2tgl_buddy_new (TLS, (tgl_peer_t *)user); - purple_blist_add_buddy (buddy, NULL, tggroup, NULL); - tgl_do_get_user_info (TLS, user->id, 0, on_user_get_info, 0); - } - purple_buddy_set_protocol_data (buddy, (gpointer)user); - p2tgl_prpl_got_user_status (TLS, user->id, &user->status); + if (TLS->our_id == tgl_get_peer_id (user->id)) { + p2tgl_connection_set_display_name (TLS, (tgl_peer_t *)user); + } else { + PurpleBuddy *buddy = p2tgl_buddy_find(TLS, user->id); + if (!buddy) { + buddy = p2tgl_buddy_new (TLS, (tgl_peer_t *)user); + purple_blist_add_buddy (buddy, NULL, tggroup, NULL); + tgl_do_get_user_info (TLS, user->id, 0, on_user_get_info, 0); } + purple_buddy_set_protocol_data (buddy, (gpointer)user); + p2tgl_prpl_got_user_status (TLS, user->id, &user->status); + } } -void chat_update_handler (struct tgl_state *TLS, struct tgl_chat *chat, unsigned flags) -{ - if (!(flags & FLAG_CREATED)) { return; } - - telegram_conn *conn = TLS->ev_base; - - tgl_do_get_chat_info (TLS, chat->id, 0, on_chat_get_info, 0); - - PurpleChat *ch = p2tgl_chat_find (TLS, chat->id); - if (!ch) { - ch = p2tgl_chat_new (TLS, chat); - purple_blist_add_chat(ch, NULL, NULL); - } +void chat_update_handler (struct tgl_state *TLS, struct tgl_chat *chat, unsigned flags) { + if (!(flags & FLAG_CREATED)) { return; } + + telegram_conn *conn = TLS->ev_base; + + tgl_do_get_chat_info (TLS, chat->id, 0, on_chat_get_info, 0); + + PurpleChat *ch = p2tgl_chat_find (TLS, chat->id); + if (!ch) { + ch = p2tgl_chat_new (TLS, chat); + purple_blist_add_chat(ch, NULL, NULL); + } } -PurpleNotifyUserInfo *create_user_notify_info(struct tgl_user *usr) -{ - PurpleNotifyUserInfo *info = purple_notify_user_info_new(); - purple_notify_user_info_add_pair(info, "First name", usr->first_name); - purple_notify_user_info_add_pair(info, "Last name", usr->last_name); - purple_notify_user_info_add_pair(info, "Phone", usr->phone); - purple_notify_user_info_add_pair(info, "Status", usr->status.online == 1 ? "Online" : "Offline"); - return info; +PurpleNotifyUserInfo *create_user_notify_info(struct tgl_user *usr) { + PurpleNotifyUserInfo *info = purple_notify_user_info_new(); + purple_notify_user_info_add_pair(info, "First name", usr->first_name); + purple_notify_user_info_add_pair(info, "Last name", usr->last_name); + purple_notify_user_info_add_pair(info, "Phone", usr->phone); + purple_notify_user_info_add_pair(info, "Status", usr->status.online == 1 ? "Online" : "Offline"); + return info; } void on_userpic_loaded (struct tgl_state *TLS, void *extra, int success, char *filename) { - telegram_conn *conn = TLS->ev_base; + telegram_conn *conn = TLS->ev_base; - gchar *data = NULL; - size_t len; - GError *err = NULL; - g_file_get_contents (filename, &data, &len, &err); - int imgStoreId = purple_imgstore_add_with_id (g_memdup(data, (guint)len), len, NULL); - - struct download_desc *dld = extra; - struct tgl_user *U = dld->data; + gchar *data = NULL; + size_t len; + GError *err = NULL; + g_file_get_contents (filename, &data, &len, &err); + int imgStoreId = purple_imgstore_add_with_id (g_memdup(data, (guint)len), len, NULL); + + struct download_desc *dld = extra; + struct tgl_user *U = dld->data; - char *who = g_strdup_printf ("%d", tgl_get_peer_id (U->id)); - if (dld->type == 1) { - PurpleNotifyUserInfo *info = create_user_notify_info(U); - char *profile_image = profile_image = format_img_full(imgStoreId); - purple_notify_user_info_add_pair (info, "Profile image", profile_image); - purple_notify_userinfo (conn->gc, who, info, NULL, NULL); - g_free (profile_image); - } - purple_buddy_icons_set_for_user(conn->pa, who, g_memdup(data, (guint)len), len, NULL); - g_free(who); + char *who = g_strdup_printf ("%d", tgl_get_peer_id (U->id)); + if (dld->type == 1) { + PurpleNotifyUserInfo *info = create_user_notify_info(U); + char *profile_image = profile_image = format_img_full(imgStoreId); + purple_notify_user_info_add_pair (info, "Profile image", profile_image); + purple_notify_userinfo (conn->gc, who, info, NULL, NULL); + g_free (profile_image); + } + purple_buddy_icons_set_for_user(conn->pa, who, g_memdup(data, (guint)len), len, NULL); + g_free(who); } void on_user_get_info (struct tgl_state *TLS, void *show_info, int success, struct tgl_user *U) { - assert (success); - - if (U->photo.sizes_num == 0 && show_info) { - PurpleNotifyUserInfo *info = create_user_notify_info(U); - p2tgl_notify_userinfo(TLS, U->id, info, NULL, NULL); - } else { - struct download_desc *dld = malloc (sizeof(struct download_desc)); - dld->data = U; - dld->type = show_info ? 1 : 2; - tgl_do_load_photo (TLS, &U->photo, on_userpic_loaded, dld); - } + assert (success); + + if (U->photo.sizes_num == 0 && show_info) { + PurpleNotifyUserInfo *info = create_user_notify_info(U); + p2tgl_notify_userinfo(TLS, U->id, info, NULL, NULL); + } else { + struct download_desc *dld = malloc (sizeof(struct download_desc)); + dld->data = U; + dld->type = show_info ? 1 : 2; + tgl_do_load_photo (TLS, &U->photo, on_userpic_loaded, dld); + } } /** @@ -459,26 +428,25 @@ void on_user_get_info (struct tgl_state *TLS, void *show_info, int success, stru * errno values, or just big something. If the message should * not be echoed to the conversation window, return 0. */ -static int tgprpl_send_im(PurpleConnection * gc, const char *who, const char *message, PurpleMessageFlags flags) -{ - debug ("tgprpl_send_im()\n"); - - telegram_conn *conn = purple_connection_get_protocol_data(gc); - PurpleAccount *pa = conn->pa; - - PurpleBuddy *b = purple_find_buddy (pa, who); - if (!b) { - warning ("Buddy %s not found, cannot send IM\n", who); - return -1; - } - - tgl_peer_t *peer = tgl_peer_get(conn->TLS, TGL_MK_USER(atoi (who))); - if (!peer) { - warning ("Protocol data tgl_peer_t for %s not found, cannot send IM\n", who); - return -1; - } - tgl_do_send_message (conn->TLS, peer->id, message, (int)strlen (message), 0, 0); - return 1; +static int tgprpl_send_im(PurpleConnection * gc, const char *who, const char *message, PurpleMessageFlags flags) { + debug ("tgprpl_send_im()\n"); + + telegram_conn *conn = purple_connection_get_protocol_data(gc); + PurpleAccount *pa = conn->pa; + + PurpleBuddy *b = purple_find_buddy (pa, who); + if (!b) { + warning ("Buddy %s not found, cannot send IM\n", who); + return -1; + } + + tgl_peer_t *peer = tgl_peer_get(conn->TLS, TGL_MK_USER(atoi (who))); + if (!peer) { + warning ("Protocol data tgl_peer_t for %s not found, cannot send IM\n", who); + return -1; + } + tgl_do_send_message (conn->TLS, peer->id, message, (int)strlen (message), 0, 0); + return 1; } /** @@ -490,22 +458,21 @@ static int tgprpl_send_im(PurpleConnection * gc, const char *who, const char *me * some other negative value. You can use one of the valid * errno values, or just big something. * - * @param id The id of the chat to send the message to. + * @param id The id of the chat to send the message to. * @param message The message to send to the chat. * @param flags A bitwise OR of #PurpleMessageFlags representing - * message flags. - * @return A positive number or 0 in case of success, - * a negative error number in case of failure. + * message flags. + * @return A positive number or 0 in case of success, + * a negative error number in case of failure. */ -static int tgprpl_send_chat(PurpleConnection * gc, int id, const char *message, PurpleMessageFlags flags) -{ - purple_debug_info(PLUGIN_ID, "tgprpl_send_chat()\n"); - telegram_conn *conn = purple_connection_get_protocol_data (gc); - tgl_do_send_message (conn->TLS, TGL_MK_CHAT(id), message, (int)strlen (message), 0, 0); - - p2tgl_got_chat_in(conn->TLS, TGL_MK_CHAT(id), TGL_MK_USER(conn->TLS->our_id), message, - PURPLE_MESSAGE_RECV, time(NULL)); - return 1; +static int tgprpl_send_chat(PurpleConnection * gc, int id, const char *message, PurpleMessageFlags flags) { + purple_debug_info(PLUGIN_ID, "tgprpl_send_chat()\n"); + telegram_conn *conn = purple_connection_get_protocol_data (gc); + tgl_do_send_message (conn->TLS, TGL_MK_CHAT(id), message, (int)strlen (message), 0, 0); + + p2tgl_got_chat_in(conn->TLS, TGL_MK_CHAT(id), TGL_MK_USER(conn->TLS->our_id), message, + PURPLE_MESSAGE_RECV, time(NULL)); + return 1; } /** @@ -519,64 +486,56 @@ static int tgprpl_send_chat(PurpleConnection * gc, int id, const char *message, * @deprecated Since 2.8.0, add_buddy_with_invite is preferred. * @see add_buddy_with_invite */ -static void tgprpl_add_buddy(PurpleConnection * gc, PurpleBuddy * buddy, PurpleGroup * group) -{ - purple_debug_info(PLUGIN_ID, "tgprpl_add_buddy()\n"); +static void tgprpl_add_buddy(PurpleConnection * gc, PurpleBuddy * buddy, PurpleGroup * group) { + purple_debug_info(PLUGIN_ID, "tgprpl_add_buddy()\n"); } -static void tgprpl_add_buddies(PurpleConnection * gc, GList * buddies, GList * groups) -{ - purple_debug_info(PLUGIN_ID, "tgprpl_add_buddies()\n"); +static void tgprpl_add_buddies(PurpleConnection * gc, GList * buddies, GList * groups) { + purple_debug_info(PLUGIN_ID, "tgprpl_add_buddies()\n"); } -static void tgprpl_remove_buddy(PurpleConnection * gc, PurpleBuddy * buddy, PurpleGroup * group) -{ - purple_debug_info(PLUGIN_ID, "tgprpl_remove_buddy()\n"); +static void tgprpl_remove_buddy(PurpleConnection * gc, PurpleBuddy * buddy, PurpleGroup * group) { + purple_debug_info(PLUGIN_ID, "tgprpl_remove_buddy()\n"); } -static void tgprpl_remove_buddies(PurpleConnection * gc, GList * buddies, GList * groups) -{ - purple_debug_info(PLUGIN_ID, "tgprpl_remove_buddies()\n"); +static void tgprpl_remove_buddies(PurpleConnection * gc, GList * buddies, GList * groups){ + purple_debug_info(PLUGIN_ID, "tgprpl_remove_buddies()\n"); } -static void tgprpl_convo_closed(PurpleConnection * gc, const char *who) -{ - purple_debug_info(PLUGIN_ID, "tgprpl_convo_closed()\n"); +static void tgprpl_convo_closed(PurpleConnection * gc, const char *who){ + purple_debug_info(PLUGIN_ID, "tgprpl_convo_closed()\n"); } -static void tgprpl_add_deny(PurpleConnection * gc, const char *name) -{ - purple_debug_info(PLUGIN_ID, "tgprpl_add_deny()\n"); +static void tgprpl_add_deny(PurpleConnection * gc, const char *name){ + purple_debug_info(PLUGIN_ID, "tgprpl_add_deny()\n"); } -static void tgprpl_rem_deny(PurpleConnection * gc, const char *name) -{ - purple_debug_info(PLUGIN_ID, "tgprpl_rem_deny()\n"); +static void tgprpl_rem_deny(PurpleConnection * gc, const char *name){ + purple_debug_info(PLUGIN_ID, "tgprpl_rem_deny()\n"); } /** * @return If this protocol requires the PURPLE_TYPING message to - * be sent repeatedly to signify that the user is still - * typing, then the PRPL should return the number of - * seconds to wait before sending a subsequent notification. - * Otherwise the PRPL should return 0. + * be sent repeatedly to signify that the user is still + * typing, then the PRPL should return the number of + * seconds to wait before sending a subsequent notification. + * Otherwise the PRPL should return 0. */ -static unsigned int tgprpl_send_typing(PurpleConnection * gc, const char *who, PurpleTypingState typing) -{ - purple_debug_info(PLUGIN_ID, "tgprpl_send_typing()\n"); - /*telegram_conn *conn = purple_connection_get_protocol_data(gc); - PurpleBuddy *b = purple_find_buddy(conn->pa, who); - if (b) { - tgl_peer_id_t *peer = purple_buddy_get_protocol_data(b); - if (peer) { - tgl_do_update_typing (conn->tg, *peer); - } - }*/ - return 0; +static unsigned int tgprpl_send_typing(PurpleConnection * gc, const char *who, PurpleTypingState typing) { + purple_debug_info(PLUGIN_ID, "tgprpl_send_typing()\n"); + /*telegram_conn *conn = purple_connection_get_protocol_data(gc); + PurpleBuddy *b = purple_find_buddy(conn->pa, who); + if (b) { + tgl_peer_id_t *peer = purple_buddy_get_protocol_data(b); + if (peer) { + tgl_do_update_typing (conn->tg, *peer); + } + }*/ + return 0; } /** @@ -584,31 +543,28 @@ static unsigned int tgprpl_send_typing(PurpleConnection * gc, const char *who, P * does NOT own a reference to @a img; if it needs one, it must * #purple_imgstore_ref(@a img) itself. */ -static void tgprpl_set_buddy_icon(PurpleConnection * gc, PurpleStoredImage * img) -{ - purple_debug_info(PLUGIN_ID, "tgprpl_set_buddy_icon()\n"); +static void tgprpl_set_buddy_icon(PurpleConnection * gc, PurpleStoredImage * img) { + purple_debug_info(PLUGIN_ID, "tgprpl_set_buddy_icon()\n"); } /** * File transfer callback */ -static gboolean tgprpl_can_receive_file(PurpleConnection * gc, const char *who) -{ - purple_debug_info(PLUGIN_ID, "tgprpl_can_receive_file()\n"); - return 0; +static gboolean tgprpl_can_receive_file(PurpleConnection * gc, const char *who) { + purple_debug_info(PLUGIN_ID, "tgprpl_can_receive_file()\n"); + return 0; } /** * Checks whether offline messages to @a buddy are supported. * @return @c 1 if @a buddy can be sent messages while they are - * offline, or @c 0 if not. + * offline, or @c 0 if not. */ -static gboolean tgprpl_offline_message(const PurpleBuddy * buddy) -{ - purple_debug_info(PLUGIN_ID, "tgprpl_offline_message()\n"); - return 0; +static gboolean tgprpl_offline_message(const PurpleBuddy * buddy) { + purple_debug_info(PLUGIN_ID, "tgprpl_offline_message()\n"); + return 0; } /** @@ -616,58 +572,52 @@ static gboolean tgprpl_offline_message(const PurpleBuddy * buddy) * this must be implemented, and must add at least the offline and * online states. */ -static GList *tgprpl_status_types(PurpleAccount * acct) -{ - purple_debug_info(PLUGIN_ID, "tgprpl_status_types()\n"); - GList *types = NULL; - PurpleStatusType *type; - type = purple_status_type_new_with_attrs(PURPLE_STATUS_AVAILABLE, NULL, NULL, - 1, 1, 0, "message", "Message", purple_value_new(PURPLE_TYPE_STRING), NULL); - types = g_list_prepend(types, type); - - type = purple_status_type_new_with_attrs(PURPLE_STATUS_MOBILE, NULL, NULL, 1, - 1, 0, "message", "Message", purple_value_new(PURPLE_TYPE_STRING), NULL); - types = g_list_prepend(types, type); +static GList *tgprpl_status_types(PurpleAccount * acct) { + purple_debug_info(PLUGIN_ID, "tgprpl_status_types()\n"); + GList *types = NULL; + PurpleStatusType *type; + type = purple_status_type_new_with_attrs(PURPLE_STATUS_AVAILABLE, NULL, NULL, + 1, 1, 0, "message", "Message", purple_value_new(PURPLE_TYPE_STRING), NULL); + types = g_list_prepend(types, type); + + type = purple_status_type_new_with_attrs(PURPLE_STATUS_MOBILE, NULL, NULL, 1, + 1, 0, "message", "Message", purple_value_new(PURPLE_TYPE_STRING), NULL); + types = g_list_prepend(types, type); - type = purple_status_type_new(PURPLE_STATUS_OFFLINE, NULL, NULL, 1); - types = g_list_append(types, type); + type = purple_status_type_new(PURPLE_STATUS_OFFLINE, NULL, NULL, 1); + types = g_list_append(types, type); - return g_list_reverse(types); + return g_list_reverse(types); } -static void tgprpl_set_status(PurpleAccount * acct, PurpleStatus * status) -{ - purple_debug_info(PLUGIN_ID, "tgprpl_set_status()\n"); +static void tgprpl_set_status(PurpleAccount * acct, PurpleStatus * status) { + purple_debug_info(PLUGIN_ID, "tgprpl_set_status()\n"); } /** * Should arrange for purple_notify_userinfo() to be called with * @a who's user info. */ -static void tgprpl_get_info(PurpleConnection * gc, const char *username) -{ - - purple_debug_info(PLUGIN_ID, "tgprpl_get_info()\n"); - telegram_conn *conn = purple_connection_get_protocol_data(gc); - tgl_peer_id_t u = TGL_MK_USER(atoi(username)); - tgl_do_get_user_info (conn->TLS, u, 0, on_user_get_info, (void *)1l); - purple_debug_info(PLUGIN_ID, "tgprpl_get_info ready()\n"); +static void tgprpl_get_info(PurpleConnection * gc, const char *username) { + purple_debug_info(PLUGIN_ID, "tgprpl_get_info()\n"); + telegram_conn *conn = purple_connection_get_protocol_data(gc); + tgl_peer_id_t u = TGL_MK_USER(atoi(username)); + tgl_do_get_user_info (conn->TLS, u, 0, on_user_get_info, (void *)1l); + purple_debug_info(PLUGIN_ID, "tgprpl_get_info ready()\n"); } /** * change a buddy's group on a server list/roster */ -static void tgprpl_group_buddy(PurpleConnection * gc, const char *who, const char *old_group, const char *new_group) -{ - purple_debug_info(PLUGIN_ID, "tgprpl_group_buddy()\n"); +static void tgprpl_group_buddy(PurpleConnection * gc, const char *who, const char *old_group, const char *new_group) { + purple_debug_info(PLUGIN_ID, "tgprpl_group_buddy()\n"); } /** * rename a group on a server list/roster */ -static void tgprpl_rename_group(PurpleConnection * gc, const char *old_name, PurpleGroup * group, GList * moved_buddies) -{ - purple_debug_info(PLUGIN_ID, "tgprpl_rename_group()\n"); +static void tgprpl_rename_group(PurpleConnection * gc, const char *old_name, PurpleGroup * group, GList * moved_buddies) { + purple_debug_info(PLUGIN_ID, "tgprpl_rename_group()\n"); } /** @@ -677,16 +627,15 @@ static void tgprpl_rename_group(PurpleConnection * gc, const char *old_name, Pur * * @return A list of #proto_chat_entry structs */ -static GList *tgprpl_chat_join_info(PurpleConnection * gc) -{ - purple_debug_info(PLUGIN_ID, "tgprpl_chat_join_info()\n"); - struct proto_chat_entry *pce; +static GList *tgprpl_chat_join_info(PurpleConnection * gc) { + purple_debug_info(PLUGIN_ID, "tgprpl_chat_join_info()\n"); + struct proto_chat_entry *pce; - pce = g_new0(struct proto_chat_entry, 1); - pce->label = "_Subject:"; - pce->identifier = "subject"; - pce->required = TRUE; - return g_list_append(NULL, pce); + pce = g_new0(struct proto_chat_entry, 1); + pce->label = "_Subject:"; + pce->identifier = "subject"; + pce->required = TRUE; + return g_list_append(NULL, pce); } /** @@ -699,10 +648,9 @@ static GList *tgprpl_chat_join_info(PurpleConnection * gc) * @param chat_name The chat name to be turned into components * @return Hashtable containing the information extracted from chat_name */ -static GHashTable *tgprpl_chat_info_defaults(PurpleConnection * gc, const char *chat_name) -{ - purple_debug_info(PLUGIN_ID, "tgprpl_chat_info_defaults()\n"); - return NULL; +static GHashTable *tgprpl_chat_info_deflt(PurpleConnection * gc, const char *chat_name) { + purple_debug_info(PLUGIN_ID, "tgprpl_chat_info_defaults()\n"); + return NULL; } /** @@ -710,65 +658,64 @@ static GHashTable *tgprpl_chat_info_defaults(PurpleConnection * gc, const char * * #serv_got_joined_chat to be called. * * @param components A hashtable containing information required to - * join the chat as described by the entries returned - * by #chat_info. It may also be called when accepting - * an invitation, in which case this matches the - * data parameter passed to #serv_got_chat_invite. + * join the chat as described by the entries returned + * by #chat_info. It may also be called when accepting + * an invitation, in which case this matches the + * data parameter passed to #serv_got_chat_invite. */ -static void tgprpl_chat_join(PurpleConnection * gc, GHashTable * data) -{ - purple_debug_info(PLUGIN_ID, "tgprpl_chat_join()\n"); +static void tgprpl_chat_join(PurpleConnection * gc, GHashTable * data) { + purple_debug_info(PLUGIN_ID, "tgprpl_chat_join()\n"); - telegram_conn *conn = purple_connection_get_protocol_data(gc); - const char *groupname = g_hash_table_lookup(data, "subject"); + telegram_conn *conn = purple_connection_get_protocol_data(gc); + const char *groupname = g_hash_table_lookup(data, "subject"); - char *id = g_hash_table_lookup(data, "id"); - if (!id) { - warning ("Got no chat id, aborting...\n"); - return; - } - if (!purple_find_chat(gc, atoi(id))) { - tgl_do_get_chat_info (conn->TLS, TGL_MK_CHAT(atoi(id)), 0, on_chat_get_info, 0); - } else { - serv_got_joined_chat(conn->gc, atoi(id), groupname); - } + char *id = g_hash_table_lookup(data, "id"); + if (!id) { + warning ("Got no chat id, aborting...\n"); + return; + } + if (!purple_find_chat(gc, atoi(id))) { + tgl_do_get_chat_info (conn->TLS, TGL_MK_CHAT(atoi(id)), 0, on_chat_get_info, 0); + } else { + serv_got_joined_chat(conn->gc, atoi(id), groupname); + } } void on_chat_get_info (struct tgl_state *TLS, void *extra, int success, struct tgl_chat *C) { - assert (success); - - debug ("on_chat_joined(%d)\n", tgl_get_peer_id (C->id)); - telegram_conn *conn = TLS->ev_base; + assert (success); + + debug ("on_chat_joined(%d)\n", tgl_get_peer_id (C->id)); + telegram_conn *conn = TLS->ev_base; - PurpleConversation *conv; - if (!(conv = purple_find_chat(conn->gc, tgl_get_peer_id(C->id)))) { - // chat conversation is not existing, create it - conv = serv_got_joined_chat(conn->gc, tgl_get_peer_id(C->id), C->title); + PurpleConversation *conv; + if (!(conv = purple_find_chat(conn->gc, tgl_get_peer_id(C->id)))) { + // chat conversation is not existing, create it + conv = serv_got_joined_chat(conn->gc, tgl_get_peer_id(C->id), C->title); + } + purple_conv_chat_clear_users(purple_conversation_get_chat_data(conv)); + add_all_users_to_chat(conv, C); + + struct tgl_message *M = 0; + while ((M = g_queue_pop_head (conn->new_messages))) { + int id = tgl_get_peer_id (M->from_id); + if (!chat_add_message(TLS, M)) { + warning ("WARNING, chat %d still not existing... \n", tgl_get_peer_id (C->id)); + break; } - purple_conv_chat_clear_users(purple_conversation_get_chat_data(conv)); - add_all_users_to_chat(conv, C); - - struct tgl_message *M = 0; - while ((M = g_queue_pop_head (conn->new_messages))) { - int id = tgl_get_peer_id (M->from_id); - if (!chat_add_message(TLS, M)) { - warning ("WARNING, chat %d still not existing... \n", tgl_get_peer_id (C->id)); - break; - } - } - - gchar *name = g_strdup_printf ("%d", tgl_get_peer_id(C->id)); - g_hash_table_remove (conn->joining_chats, name); - g_free (name); + } + + gchar *name = g_strdup_printf ("%d", tgl_get_peer_id(C->id)); + g_hash_table_remove (conn->joining_chats, name); + g_free (name); } /** * Invite a user to join a chat. * - * @param id The id of the chat to invite the user to. + * @param id The id of the chat to invite the user to. * @param message A message displayed to the user when the invitation - * is received. - * @param who The name of the user to send the invation to. + * is received. + * @param who The name of the user to send the invation to. */ static void tgprpl_chat_invite(PurpleConnection * gc, int id, const char *message, const char *name) { purple_debug_info(PLUGIN_ID, "tgprpl_chat_invite()\n"); } /** @@ -778,172 +725,165 @@ static void tgprpl_chat_invite(PurpleConnection * gc, int id, const char *messag * * @param components A hashtable containing information about the chat. */ -static char *tgprpl_get_chat_name(GHashTable * data) -{ - purple_debug_info(PLUGIN_ID, "tgprpl_get_chat_name()\n"); - return g_strdup(g_hash_table_lookup(data, "subject")); +static char *tgprpl_get_chat_name(GHashTable * data) { + purple_debug_info(PLUGIN_ID, "tgprpl_get_chat_name()\n"); + return g_strdup(g_hash_table_lookup(data, "subject")); } /** * File transfer callback. */ -static PurpleXfer *tgprpl_new_xfer(PurpleConnection * gc, const char *who) -{ - purple_debug_info(PLUGIN_ID, "tgprpl_new_xfer()\n"); - return (PurpleXfer *)NULL; +static PurpleXfer *tgprpl_new_xfer(PurpleConnection * gc, const char *who) { + purple_debug_info(PLUGIN_ID, "tgprpl_new_xfer()\n"); + return (PurpleXfer *)NULL; } /** * File transfer callback. */ -static void tgprpl_send_file(PurpleConnection * gc, const char *who, const char *file) -{ - purple_debug_info(PLUGIN_ID, "tgprpl_send_file()\n"); +static void tgprpl_send_file(PurpleConnection * gc, const char *who, const char *file) { + purple_debug_info(PLUGIN_ID, "tgprpl_send_file()\n"); } // SEE prpl.h static PurplePluginProtocolInfo prpl_info = { - OPT_PROTO_NO_PASSWORD, /* options */ - NULL, /* user_splits, initialized in tgprpl_init() */ - NULL, /* protocol_options, initialized in tgprpl_init() */ - { /* icon_spec, a PurpleBuddyIconSpec */ - "png", /* format */ - 1, /* min_width */ - 1, /* min_height */ - 512, /* max_width */ - 512, /* max_height */ - 64000, /* max_filesize */ - PURPLE_ICON_SCALE_SEND, /* scale_rules */ - }, - tgprpl_list_icon, - NULL, - NULL, - tgprpl_tooltip_text, - tgprpl_status_types, - NULL, /* blist_node_menu */ - tgprpl_chat_join_info, - tgprpl_chat_info_defaults, /* chat_info_defaults */ - tgprpl_login, /* login */ - tgprpl_close, /* close */ - tgprpl_send_im, /* send_im */ - NULL, /* set_info */ - tgprpl_send_typing, /* send_typing */ - tgprpl_get_info, /* get_info */ - tgprpl_set_status, /* set_status */ - NULL, /* set_idle */ - NULL, /* change_passwd */ - tgprpl_add_buddy, /* add_buddy */ - tgprpl_add_buddies, /* add_buddies */ - tgprpl_remove_buddy, /* remove_buddy */ - tgprpl_remove_buddies, /* remove_buddies */ - NULL, /* add_permit */ - tgprpl_add_deny, /* add_deny */ - NULL, /* rem_permit */ - tgprpl_rem_deny, /* rem_deny */ - NULL, /* set_permit_deny */ - tgprpl_chat_join, /* join_chat */ - NULL, /* reject_chat */ - tgprpl_get_chat_name, /* get_chat_name */ - tgprpl_chat_invite, /* chat_invite */ - NULL, /* chat_leave */ - NULL, /* chat_whisper */ - tgprpl_send_chat, /* chat_send */ - NULL, /* keepalive */ - NULL, /* register_user */ - NULL, /* get_cb_info */ - NULL, /* get_cb_away */ - NULL, /* alias_buddy */ - tgprpl_group_buddy, /* group_buddy */ - tgprpl_rename_group, /* rename_group */ - NULL, /* buddy_free */ - tgprpl_convo_closed, /* convo_closed */ - purple_normalize_nocase, /* normalize */ - tgprpl_set_buddy_icon, /* set_buddy_icon */ - NULL, /* remove_group */ - NULL, /* get_cb_real_name */ - NULL, /* set_chat_topic */ - NULL, /* find_blist_chat */ - NULL, /* roomlist_get_list */ - NULL, /* roomlist_cancel */ - NULL, /* roomlist_expand_category */ - tgprpl_can_receive_file, /* can_receive_file */ - tgprpl_send_file, /* send_file */ - tgprpl_new_xfer, /* new_xfer */ - tgprpl_offline_message, /* offline_message */ - NULL, /* whiteboard_prpl_ops */ - NULL, /* send_raw */ - NULL, /* roomlist_room_serialize */ - NULL, /* unregister_user */ - NULL, /* send_attention */ - NULL, /* get_attention_types */ - sizeof(PurplePluginProtocolInfo), /* struct_size */ - NULL, /* get_account_text_table */ - NULL, /* initiate_media */ - NULL, /* get_media_caps */ - NULL, /* get_moods */ - NULL, /* set_public_alias */ - NULL, /* get_public_alias */ - NULL, /* add_buddy_with_invite */ - NULL /* add_buddies_with_invite */ + OPT_PROTO_NO_PASSWORD, + NULL, // user_splits, initialized in tgprpl_init() + NULL, // protocol_options, initialized in tgprpl_init() + { + "png", + 1, // min_width + 1, // min_height + 512, // max_width + 512, // max_height + 64000, // max_filesize + PURPLE_ICON_SCALE_SEND, + }, + tgprpl_list_icon, + NULL, + NULL, + tgprpl_tooltip_text, + tgprpl_status_types, + NULL, // blist_node_menu + tgprpl_chat_join_info, + tgprpl_chat_info_deflt, + tgprpl_login, + tgprpl_close, + tgprpl_send_im, + NULL, // set_info + tgprpl_send_typing, + tgprpl_get_info, + tgprpl_set_status, + NULL, // set_idle + NULL, // change_passwd + tgprpl_add_buddy, + tgprpl_add_buddies, + tgprpl_remove_buddy, + tgprpl_remove_buddies, + NULL, // add_permit + tgprpl_add_deny, + NULL, // rem_permit + tgprpl_rem_deny, + NULL, // set_permit_deny + tgprpl_chat_join, + NULL, // reject_chat + tgprpl_get_chat_name, + tgprpl_chat_invite, + NULL, // chat_leave + NULL, // chat_whisper + tgprpl_send_chat, + NULL, // keepalive + NULL, // register_user + NULL, // get_cb_info + NULL, // get_cb_away + NULL, // alias_buddy + tgprpl_group_buddy, + tgprpl_rename_group, + NULL, // buddy_free + tgprpl_convo_closed, + purple_normalize_nocase, + tgprpl_set_buddy_icon, + NULL, // remove_group + NULL, // get_cb_real_name + NULL, // set_chat_topic + NULL, // find_blist_chat + NULL, // roomlist_get_list + NULL, // roomlist_cancel + NULL, // roomlist_expand_category + tgprpl_can_receive_file, + tgprpl_send_file, + tgprpl_new_xfer, + tgprpl_offline_message, + NULL, // whiteboard_prpl_ops + NULL, // send_raw + NULL, // roomlist_room_serialize + NULL, // unregister_user + NULL, // send_attention + NULL, // get_attention_types + sizeof(PurplePluginProtocolInfo), + NULL, // get_account_text_table + NULL, // initiate_media + NULL, // get_media_caps + NULL, // get_moods + NULL, // set_public_alias + NULL, // get_public_alias + NULL, // add_buddy_with_invite + NULL // add_buddies_with_invite }; -static void tgprpl_init(PurplePlugin *plugin) -{ - - PurpleAccountOption *option; - GList *verification_values = NULL; - - // Extra Options +static void tgprpl_init(PurplePlugin *plugin) { + PurpleAccountOption *option; + GList *verification_values = NULL; + + // Extra Options #define ADD_VALUE(list, desc, v) { \ - PurpleKeyValuePair *kvp = g_new0(PurpleKeyValuePair, 1); \ - kvp->key = g_strdup((desc)); \ - kvp->value = g_strdup((v)); \ - list = g_list_prepend(list, kvp); \ + PurpleKeyValuePair *kvp = g_new0(PurpleKeyValuePair, 1); \ + kvp->key = g_strdup((desc)); \ + kvp->value = g_strdup((v)); \ + list = g_list_prepend(list, kvp); \ } - ADD_VALUE(verification_values, "Phone", TELEGRAM_AUTH_MODE_PHONE); - ADD_VALUE(verification_values, "SMS", TELEGRAM_AUTH_MODE_SMS); + ADD_VALUE(verification_values, "Phone", TELEGRAM_AUTH_MODE_PHONE); + ADD_VALUE(verification_values, "SMS", TELEGRAM_AUTH_MODE_SMS); - option = purple_account_option_list_new("Verification type", "verification_type", verification_values); - prpl_info.protocol_options = g_list_append(prpl_info.protocol_options, option); + option = purple_account_option_list_new("Verification type", "verification_type", verification_values); + prpl_info.protocol_options = g_list_append(prpl_info.protocol_options, option); - _telegram_protocol = plugin; + _telegram_protocol = plugin; } -static GList *tgprpl_actions(PurplePlugin * plugin, gpointer context) -{ - // return possible actions (See Libpurple doc) - return (GList *)NULL; +static GList *tgprpl_actions(PurplePlugin * plugin, gpointer context) { + // return possible actions (See Libpurple doc) + return (GList *)NULL; } static PurplePluginInfo plugin_info = { - PURPLE_PLUGIN_MAGIC, - PURPLE_MAJOR_VERSION, - PURPLE_MINOR_VERSION, - PURPLE_PLUGIN_PROTOCOL, - NULL, - 0, - NULL, - PURPLE_PRIORITY_DEFAULT, - PLUGIN_ID, - "Telegram", - TG_VERSION " libtgl: " TGL_VERSION " ", - "Telegram", - TG_DESCRIPTION, - TG_AUTHOR, - "https://github.com/majn/telegram-purple", - NULL, // on load - NULL, // on unload - NULL, // on destroy - NULL, // ui specific struct - &prpl_info, // plugin info struct - NULL, // prefs info - tgprpl_actions, // actions - NULL, // reserved - NULL, // reserved - NULL, // reserved - NULL // reserved + PURPLE_PLUGIN_MAGIC, + PURPLE_MAJOR_VERSION, + PURPLE_MINOR_VERSION, + PURPLE_PLUGIN_PROTOCOL, + NULL, + 0, + NULL, + PURPLE_PRIORITY_DEFAULT, + PLUGIN_ID, + "Telegram", + TG_VERSION " libtgl: " TGL_VERSION " ", + "Telegram", + TG_DESCRIPTION, + TG_AUTHOR, + "https://github.com/majn/telegram-purple", + NULL, // on load + NULL, // on unload + NULL, // on destroy + NULL, // ui specific struct + &prpl_info, + NULL, // prefs info + tgprpl_actions, + NULL, // reserved + NULL, // reserved + NULL, // reserved + NULL // reserved }; - PURPLE_INIT_PLUGIN(telegram, tgprpl_init, plugin_info) diff --git a/tgp-2prpl.c b/tgp-2prpl.c index e75e7f6..e141f1d 100644 --- a/tgp-2prpl.c +++ b/tgp-2prpl.c @@ -21,210 +21,211 @@ #include #include +#include static void sanitize_alias(char *buffer) { - size_t len = strlen(buffer); - gchar *curr; - while ((curr = g_utf8_strchr(buffer, len, '\n'))) { - *curr = 0x20; - } + size_t len = strlen(buffer); + gchar *curr; + while ((curr = g_utf8_strchr(buffer, len, '\n'))) { + *curr = 0x20; + } } static int user_get_alias (tgl_peer_t *user, char *buffer, int maxlen) { - char* last_name = (user->user.last_name && strlen(user->user.last_name)) ? user->user.last_name : ""; - char* first_name = (user->user.first_name && strlen(user->user.first_name)) ? user->user.first_name : ""; - sanitize_alias (last_name); - sanitize_alias (first_name); - if (strlen (first_name) && strlen(last_name)) { - return snprintf (buffer, maxlen, "%s %s", first_name, last_name); - } else if (strlen (first_name)) { - return snprintf (buffer, maxlen, "%s", first_name); - } else if (strlen (last_name)) { - return snprintf (buffer, maxlen, "%s", last_name); - } else { - return snprintf (buffer, maxlen, "%d", tgl_get_peer_id (user->id)); - } + char* last_name = (user->user.last_name && strlen(user->user.last_name)) ? user->user.last_name : ""; + char* first_name = (user->user.first_name && strlen(user->user.first_name)) ? user->user.first_name : ""; + sanitize_alias (last_name); + sanitize_alias (first_name); + if (strlen (first_name) && strlen(last_name)) { + return snprintf (buffer, maxlen, "%s %s", first_name, last_name); + } else if (strlen (first_name)) { + return snprintf (buffer, maxlen, "%s", first_name); + } else if (strlen (last_name)) { + return snprintf (buffer, maxlen, "%s", last_name); + } else { + return snprintf (buffer, maxlen, "%d", tgl_get_peer_id (user->id)); + } } PurpleAccount *tg_get_acc (struct tgl_state *TLS) { - return (PurpleAccount *) ((telegram_conn *)TLS->ev_base)->pa; + return (PurpleAccount *) ((telegram_conn *)TLS->ev_base)->pa; } PurpleConnection *tg_get_conn (struct tgl_state *TLS) { - return (PurpleConnection *) ((telegram_conn *)TLS->ev_base)->gc; + return (PurpleConnection *) ((telegram_conn *)TLS->ev_base)->gc; } static char *peer_strdup_id(tgl_peer_id_t user) { - return g_strdup_printf("%d", tgl_get_peer_id(user)); + return g_strdup_printf("%d", tgl_get_peer_id(user)); } char *p2tgl_strdup_alias(tgl_peer_t *user) { - char *alias = malloc(64); - if (user_get_alias(user, alias, 64) < 0) { - free (alias); - warning ("user name too long"); - return "!!! name too long"; - } - gchar *g_alias = g_strdup(alias); + char *alias = malloc(64); + if (user_get_alias(user, alias, 64) < 0) { free (alias); - return g_alias; + warning ("user name too long"); + return "!!! name too long"; + } + gchar *g_alias = g_strdup(alias); + free (alias); + return g_alias; } static PurpleChat *blist_find_chat_by_hasht_cond(PurpleConnection *gc, - int (*fn)(GHashTable *hasht, void *data), void *data) { - PurpleAccount *account = purple_connection_get_account(gc); - PurpleBlistNode *node = purple_blist_get_root(); - GHashTable *hasht; - while (node) { - if (PURPLE_BLIST_NODE_IS_CHAT(node)) { - PurpleChat *ch = PURPLE_CHAT(node); - if (purple_chat_get_account(ch) == account) { - hasht = purple_chat_get_components(ch); - if (fn(hasht, data)) - return ch; - } - } - node = purple_blist_node_next(node, 0); + int (*fn)(GHashTable *hasht, void *data), void *data) { + PurpleAccount *account = purple_connection_get_account(gc); + PurpleBlistNode *node = purple_blist_get_root(); + GHashTable *hasht; + while (node) { + if (PURPLE_BLIST_NODE_IS_CHAT(node)) { + PurpleChat *ch = PURPLE_CHAT(node); + if (purple_chat_get_account(ch) == account) { + hasht = purple_chat_get_components(ch); + if (fn(hasht, data)) + return ch; + } } - return NULL; + node = purple_blist_node_next(node, 0); + } + return NULL; } static int hasht_cmp_id(GHashTable *hasht, void *data) { - return !strcmp(g_hash_table_lookup(hasht, "id"), *((char **)data)); + return !strcmp(g_hash_table_lookup(hasht, "id"), *((char **)data)); } PurpleConversation *p2tgl_got_joined_chat (struct tgl_state *TLS, struct tgl_chat *chat) { - telegram_conn *conn = TLS->ev_base; - gchar *alias = p2tgl_strdup_alias(chat); - - PurpleConversation *conv = serv_got_joined_chat(conn->gc, tgl_get_peer_id(chat->id), alias); - - g_free(alias); - return conv; + telegram_conn *conn = TLS->ev_base; + gchar *alias = p2tgl_strdup_alias(chat); + + PurpleConversation *conv = serv_got_joined_chat(conn->gc, tgl_get_peer_id(chat->id), alias); + + g_free(alias); + return conv; } void p2tgl_got_chat_left (struct tgl_state *TLS, tgl_peer_id_t chat) { - serv_got_chat_left(tg_get_conn(TLS), tgl_get_peer_id(chat)); + serv_got_chat_left(tg_get_conn(TLS), tgl_get_peer_id(chat)); } void p2tgl_got_chat_in (struct tgl_state *TLS, tgl_peer_id_t chat, tgl_peer_id_t who, const char *message, int flags, time_t when) { - char *name = peer_strdup_id(who); - - serv_got_chat_in(tg_get_conn(TLS), chat.id, name, flags, message, when); - - g_free (name); + char *name = peer_strdup_id(who); + + serv_got_chat_in(tg_get_conn(TLS), chat.id, name, flags, message, when); + + g_free (name); } void p2tgl_got_alias (struct tgl_state *TLS, tgl_peer_id_t who, const char *alias) { - char *name = peer_strdup_id(who); - - serv_got_alias(tg_get_conn(TLS), name, alias); - - g_free (name); + char *name = peer_strdup_id(who); + + serv_got_alias(tg_get_conn(TLS), name, alias); + + g_free (name); } void p2tgl_got_im (struct tgl_state *TLS, tgl_peer_id_t who, const char *msg, int flags, time_t when) { - char *name = peer_strdup_id(who); - - serv_got_im(tg_get_conn(TLS), name, msg,flags, when); - - g_free (name); + char *name = peer_strdup_id(who); + + serv_got_im(tg_get_conn(TLS), name, msg,flags, when); + + g_free (name); } void p2tgl_got_typing (struct tgl_state *TLS, tgl_peer_id_t user, int timeout) { - char *who = g_strdup_printf("%d", tgl_get_peer_id(user)); - - serv_got_typing(tg_get_conn(TLS), who, timeout, PURPLE_TYPING); - - g_free(who); + char *who = g_strdup_printf("%d", tgl_get_peer_id(user)); + + serv_got_typing(tg_get_conn(TLS), who, timeout, PURPLE_TYPING); + + g_free(who); } PurpleBuddy *p2tgl_buddy_find (struct tgl_state *TLS, tgl_peer_id_t user) { - gchar *name = peer_strdup_id(user); - - PurpleBuddy *b = purple_find_buddy (tg_get_acc(TLS), name); - - g_free (name); - return b; + gchar *name = peer_strdup_id(user); + + PurpleBuddy *b = purple_find_buddy (tg_get_acc(TLS), name); + + g_free (name); + return b; } PurpleBuddy *p2tgl_buddy_new (struct tgl_state *TLS, tgl_peer_t *user) { - char *alias = p2tgl_strdup_alias (user); - char *name = peer_strdup_id (user->id); - - PurpleBuddy *b = purple_buddy_new(tg_get_acc(TLS), name, alias); - - g_free (alias); - g_free (name); - return b; + char *alias = p2tgl_strdup_alias (user); + char *name = peer_strdup_id (user->id); + + PurpleBuddy *b = purple_buddy_new(tg_get_acc(TLS), name, alias); + + g_free (alias); + g_free (name); + return b; } void p2tgl_prpl_got_user_status (struct tgl_state *TLS, tgl_peer_id_t user, struct tgl_user_status *status) { - - const char *state = status->online == 1 ? "available" : "mobile"; - char *name = peer_strdup_id (user); - char *when = g_strdup_printf("%d", status->when); - - purple_prpl_got_user_status (tg_get_acc(TLS), name, state, "last online", when, NULL); - - g_free(name); - g_free(when); + + const char *state = status->online == 1 ? "available" : "mobile"; + char *name = peer_strdup_id (user); + char *when = g_strdup_printf("%d", status->when); + + purple_prpl_got_user_status (tg_get_acc(TLS), name, state, "last online", when, NULL); + + g_free(name); + g_free(when); } PurpleChat *p2tgl_blist_find_chat(struct tgl_state *TLS, tgl_peer_id_t chat) { - char *name = peer_strdup_id(chat); - - PurpleChat *c = purple_blist_find_chat(tg_get_acc(TLS), name); - - g_free (name); - return c; + char *name = peer_strdup_id(chat); + + PurpleChat *c = purple_blist_find_chat(tg_get_acc(TLS), name); + + g_free (name); + return c; } PurpleChat *p2tgl_chat_new (struct tgl_state *TLS, struct tgl_chat *chat) { - gchar *admin = g_strdup_printf("%d", chat->admin_id); - gchar *title = g_strdup(chat->title); - gchar *name = peer_strdup_id (chat->id); - - GHashTable *ht = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free); - g_hash_table_insert(ht, g_strdup("subject"), title); - g_hash_table_insert(ht, g_strdup("id"), name); - g_hash_table_insert(ht, g_strdup("owner"), admin); - - return purple_chat_new(tg_get_acc(TLS), chat->title, ht); + gchar *admin = g_strdup_printf("%d", chat->admin_id); + gchar *title = g_strdup(chat->title); + gchar *name = peer_strdup_id (chat->id); + + GHashTable *ht = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free); + g_hash_table_insert(ht, g_strdup("subject"), title); + g_hash_table_insert(ht, g_strdup("id"), name); + g_hash_table_insert(ht, g_strdup("owner"), admin); + + return purple_chat_new(tg_get_acc(TLS), chat->title, ht); } PurpleChat *p2tgl_chat_find (struct tgl_state *TLS, tgl_peer_id_t id) { - char *name = peer_strdup_id(id); - PurpleChat *c = blist_find_chat_by_hasht_cond(tg_get_conn(TLS), hasht_cmp_id, &name); - g_free(name); - return c; + char *name = peer_strdup_id(id); + PurpleChat *c = blist_find_chat_by_hasht_cond(tg_get_conn(TLS), hasht_cmp_id, &name); + g_free(name); + return c; } void p2tgl_conv_add_user (PurpleConversation *conv, struct tgl_chat_user user, char *message, int flags, int new_arrival) { - PurpleConvChat *cdata = purple_conversation_get_chat_data(conv); - char *name = g_strdup_printf("%d", user.user_id); - - purple_conv_chat_add_user(cdata, name, message, flags, new_arrival); - - g_free(name); + PurpleConvChat *cdata = purple_conversation_get_chat_data(conv); + char *name = g_strdup_printf("%d", user.user_id); + + purple_conv_chat_add_user(cdata, name, message, flags, new_arrival); + + g_free(name); } void p2tgl_connection_set_display_name(struct tgl_state *TLS, tgl_peer_t *user) { - char *name = p2tgl_strdup_alias(user); - purple_connection_set_display_name(tg_get_conn(TLS), name); - g_free(name); + char *name = p2tgl_strdup_alias(user); + purple_connection_set_display_name(tg_get_conn(TLS), name); + g_free(name); } void *p2tgl_notify_userinfo(struct tgl_state *TLS, tgl_peer_id_t user, PurpleNotifyUserInfo *user_info, PurpleNotifyCloseCallback cb, gpointer user_data) { - char *name = peer_strdup_id(user); - void *handle = 0; - - handle = purple_notify_userinfo(tg_get_conn(TLS), name, user_info, cb, user_data); - - g_free(name); - return handle; + char *name = peer_strdup_id(user); + void *handle = 0; + + handle = purple_notify_userinfo(tg_get_conn(TLS), name, user_info, cb, user_data); + + g_free(name); + return handle; } diff --git a/tgp-net.c b/tgp-net.c index 010d509..94c035b 100644 --- a/tgp-net.c +++ b/tgp-net.c @@ -42,18 +42,13 @@ #include #include "tgp-net.h" -//#include "include.h" #include #include -//#include "mtproto-client.h" -//#include "mtproto-common.h" -//#include "tree.h" -//#include "tools.h" #include #include - -#include "telegram-purple.h" +#include +#include #ifndef POLLRDHUP #define POLLRDHUP 0