static void on_userpic_loaded (struct tgl_state *TLS, void *extra, int success, char *filename) { if (!success) { struct download_desc *dld = extra; struct tgl_user *U = dld->data; warning ("Can not load userpic for user %s %s\n", U->first_name, U->last_name); } 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; if (imgStoreId <= 0) { warning ("Can not load userpic for user %s %s\n", U->first_name, U->last_name); } 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); }
static void waprpl_get_info(PurpleConnection * gc, const char *username) { PurpleNotifyUserInfo *info = purple_notify_user_info_new(); purple_debug_info(WHATSAPP_ID, "Fetching %s's user info for %s\n", username, gc->account->username); /* Get user status */ whatsapp_connection *wconn = purple_connection_get_protocol_data(gc); const char *status_string = waAPI_getuserstatusstring(wconn->waAPI, username); /* Get user picture (big one) */ char *profile_image = ""; char *icon; int len; int res = waAPI_queryavatar(wconn->waAPI, username, &icon, &len); if (res) { int iid = purple_imgstore_add_with_id(g_memdup(icon, len), len, NULL); profile_image = g_strdup_printf("<img id=\"%u\">", iid); } purple_notify_user_info_add_pair(info, "Status", status_string); purple_notify_user_info_add_pair(info, "Profile image", profile_image); if (res) g_free(profile_image); purple_notify_userinfo(gc, username, info, NULL, NULL); waprpl_check_output(gc); }
/*------------------------------------------------------------------------ * Display the current splash-screen. * * @param session The MXit session object */ void splash_display(struct MXitSession* session) { const char* splashId = NULL; char* filename; gchar* imgdata; gsize imglen; int imgid = -1; /* Get current splash ID */ splashId = splash_current(session); if (splashId == NULL) /* no splash-screen */ return; purple_debug_info(MXIT_PLUGIN_ID, "Display Splash: '%s'\n", splashId); /* Load splash-screen image from file */ filename = g_strdup_printf("%s" G_DIR_SEPARATOR_S "mxit" G_DIR_SEPARATOR_S "%s.png", purple_user_dir(), purple_escape_filename(splashId)); if (g_file_get_contents(filename, &imgdata, &imglen, NULL)) { char buf[128]; /* Add splash-image to imagestore */ imgid = purple_imgstore_add_with_id(g_memdup(imgdata, imglen), imglen, NULL); /* Generate and display message */ g_snprintf(buf, sizeof(buf), "<img id=\"%d\">", imgid); /* Open a request-type popup to display the image */ { PurpleRequestFields* fields; PurpleRequestFieldGroup* group; PurpleRequestField* field; fields = purple_request_fields_new(); group = purple_request_field_group_new(NULL); purple_request_fields_add_group(fields, group); field = purple_request_field_image_new("splash", "", imgdata, imglen); /* add splash image */ purple_request_field_group_add_field(group, field); if (splash_clickable(session)) { purple_request_fields(session->con, _("MXit Advertising"), NULL, NULL, fields, _("More Information"), G_CALLBACK(splash_click_ok), _("Close"), NULL, session->acc, NULL, NULL, session->con); } else { purple_request_fields(session->con, _("MXit Advertising"), NULL, NULL, fields, _("Continue"), G_CALLBACK(splash_click_ok), _("Close"), NULL, session->acc, NULL, NULL, session->con); } } /* Release reference to image */ purple_imgstore_unref_by_id(imgid); g_free(imgdata); } g_free(filename); }
static void query_chat_image(PurpleConnection *gc) { whatsapp_connection *wconn = purple_connection_get_protocol_data(gc); char *who, *prev, *url, *author; int size; unsigned long timestamp; if (waAPI_querychatimage(wconn->waAPI, &who, &prev, &size, &url, &author, ×tamp)) { purple_debug_info(WHATSAPP_ID, "Got image from %s: %s\n", who, url); int imgid = purple_imgstore_add_with_id(g_memdup(prev, size), size, NULL); char *msg = g_strdup_printf("<a href=\"%s\"><img id=\"%u\"></a><br/><a href=\"%s\">%s</a>", url, imgid, url, url); conv_add_message(gc, who, msg, author, timestamp); g_free(msg); } }
static void pb_got_conv_image(PurpleUtilFetchUrlData *url_data, gpointer user_data, const gchar *url_text, gsize len, const gchar *error_message) { PurpleConversation *conv = user_data; gint icon_id; gchar *msg_tmp; if (!url_text || !url_text[0] || url_text[0] == '{') return; icon_id = purple_imgstore_add_with_id((gpointer)url_text, len, NULL); msg_tmp = g_strdup_printf("<img id='%d'>", icon_id); purple_conversation_write(conv, conv->name, msg_tmp, PURPLE_MESSAGE_SYSTEM, time(NULL)); g_free(msg_tmp); purple_imgstore_unref_by_id(icon_id); }
static void purple_xfer_conversation_write_internal(PurpleXfer *xfer, const char *message, gboolean is_error, gboolean print_thumbnail) { PurpleConversation *conv = NULL; PurpleMessageFlags flags = PURPLE_MESSAGE_SYSTEM; char *escaped; gconstpointer thumbnail_data; gsize size; g_return_if_fail(xfer != NULL); g_return_if_fail(message != NULL); thumbnail_data = purple_xfer_get_thumbnail(xfer, &size); conv = purple_find_conversation_with_account(PURPLE_CONV_TYPE_IM, xfer->who, purple_xfer_get_account(xfer)); if (conv == NULL) return; escaped = g_markup_escape_text(message, -1); if (is_error) flags |= PURPLE_MESSAGE_ERROR; if (print_thumbnail && thumbnail_data) { gchar *message_with_img; gpointer data = g_memdup(thumbnail_data, size); int id = purple_imgstore_add_with_id(data, size, NULL); message_with_img = g_strdup_printf("<img id='%d'> %s", id, escaped); purple_conversation_write(conv, NULL, message_with_img, flags, time(NULL)); purple_imgstore_unref_by_id(id); g_free(message_with_img); } else { purple_conversation_write(conv, NULL, escaped, flags, time(NULL)); } g_free(escaped); }
static void skypeweb_got_imagemessage(PurpleUtilFetchUrlData *url_data, gpointer user_data, const gchar *url_text, gsize len, const gchar *error_message) { PurpleConversation *conv = user_data; PurpleConnection *pc; SkypeWebAccount *sa; gint icon_id; gchar *msg_tmp; // Conversation could have been closed before we retrieved the image if (!purple_conversation_is_valid(conv)) { return; } pc = purple_conversation_get_connection(conv); sa = purple_connection_get_protocol_data(pc); sa->url_datas = g_slist_remove(sa->url_datas, url_data); if (url_text == NULL && url_data->data_len) { gchar *location; location = skypeweb_string_get_chunk(url_data->webdata, url_data->data_len, "Location: https://", "/"); if (location && *location) { skypeweb_download_uri_to_conv(sa, location, conv); g_free(location); } return; } if (!url_text || !len || url_text[0] == '{' || url_text[0] == '<') return; if (error_message && *error_message) return; icon_id = purple_imgstore_add_with_id(g_memdup(url_text, len), len, NULL); msg_tmp = g_strdup_printf("<img id='%d'>", icon_id); purple_conversation_write(conv, conv->name, msg_tmp, PURPLE_MESSAGE_SYSTEM | PURPLE_MESSAGE_IMAGES, time(NULL)); g_free(msg_tmp); purple_imgstore_unref_by_id(icon_id); }
void msn_switchboard_show_ink(MsnSwitchBoard *swboard, const char *passport, const char *data) { PurpleConnection *gc; guchar *image_data; size_t image_len; int imgid; char *image_msg; if (!purple_str_has_prefix(data, "base64:")) { purple_debug_error("msn", "Ignoring Ink not in Base64 format.\n"); return; } gc = purple_account_get_connection(swboard->session->account); data += sizeof("base64:") - 1; image_data = purple_base64_decode(data, &image_len); if (!image_data || !image_len) { purple_debug_error("msn", "Unable to decode Ink from Base64 format.\n"); return; } imgid = purple_imgstore_add_with_id(image_data, image_len, NULL); image_msg = g_strdup_printf("<IMG ID='%d'/>", imgid); if (swboard->current_users > 1 || ((swboard->conv != NULL) && purple_conversation_get_type(swboard->conv) == PURPLE_CONV_TYPE_CHAT)) serv_got_chat_in(gc, swboard->chat_id, passport, 0, image_msg, time(NULL)); else serv_got_im(gc, passport, image_msg, 0, time(NULL)); purple_imgstore_unref_by_id(imgid); g_free(image_msg); }
void on_message_load_photo (struct tgl_state *TLS, void *extra, int success, char *filename) { 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); char *image = format_img_full(imgStoreId); struct tgl_message *M = extra; switch (tgl_get_peer_type (M->to_id)) { case TGL_PEER_CHAT: debug ("PEER_CHAT\n"); if (!our_msg(TLS, M)) { chat_add_message (TLS, M, image); } break; case TGL_PEER_USER: debug ("PEER_USER\n"); if (out_msg(TLS, M)) { p2tgl_got_im (TLS, M->to_id, image, PURPLE_MESSAGE_SEND | PURPLE_MESSAGE_IMAGES, M->date); } else { p2tgl_got_im (TLS, M->from_id, image, PURPLE_MESSAGE_RECV | PURPLE_MESSAGE_IMAGES, M->date); } break; case TGL_PEER_ENCR_CHAT: break; case TGL_PEER_GEO_CHAT: break; } g_free (image); telegram_conn *conn = TLS->ev_base; conn->updated = 1; }
static void waprpl_process_incoming_events(PurpleConnection *gc) { whatsapp_connection * wconn = purple_connection_get_protocol_data(gc); PurpleAccount * acc = purple_connection_get_account(gc); switch (waAPI_loginstatus(wconn->waAPI)) { case 0: purple_connection_update_progress(gc, "Connecting", 0, 4); break; case 1: purple_connection_update_progress(gc, "Sending auth", 1, 4); break; case 2: purple_connection_update_progress(gc, "Waiting response", 2, 4); break; case 3: purple_connection_update_progress(gc, "Connected", 3, 4); purple_connection_set_state(gc, PURPLE_CONNECTED); if (!wconn->connected) waprpl_insert_contacts(gc); wconn->connected = 1; break; default: break; }; char * msg, * who, * prev, * url, *author; int status; int size; double lat,lng; // Incoming messages while (waAPI_querychat(wconn->waAPI, &who, &msg, &author)) { purple_debug_info(WHATSAPP_ID, "Got chat message from %s: %s\n", who,msg); if (isgroup(who)) { // Search fot the combo PurpleBlistNode* node = purple_blist_get_root(); GHashTable* hasht = NULL; while (node != 0) { if (PURPLE_BLIST_NODE_IS_CHAT(node)) { PurpleChat * ch = PURPLE_CHAT(node); if (purple_chat_get_account(ch) == acc) { hasht = purple_chat_get_components(ch); if (strcmp(g_hash_table_lookup(hasht, "id"),who) == 0) { break; } } } node = purple_blist_node_next(node,FALSE); } int convo_id = chatid_to_convo(who); PurpleConversation *convo = purple_find_chat(gc, convo_id); // Create a window if it's not open yet if (!convo) waprpl_chat_join(gc,hasht); if (convo != NULL) { serv_got_chat_in(gc, purple_conv_chat_get_id(PURPLE_CONV_CHAT(convo)), author, PURPLE_MESSAGE_RECV, msg, time(NULL)); }else{ printf("Received group message but could not find the group! %s\n",msg); } }else{ // Search fot the combo PurpleConversation *convo = purple_find_conversation_with_account(PURPLE_CONV_TYPE_IM, who, acc); if (!convo) convo = purple_conversation_new(PURPLE_CONV_TYPE_IM, acc, who); serv_got_chat_in(gc, purple_conv_chat_get_id(PURPLE_CONV_IM(convo)), who, PURPLE_MESSAGE_RECV, msg, time(NULL)); purple_conv_im_write(PURPLE_CONV_IM(convo), who, msg, PURPLE_MESSAGE_RECV, time(NULL)); } } while (waAPI_querychatimage(wconn->waAPI, &who, &prev, &size, &url)) { printf("Got chat image %s %s\n",who,url); purple_debug_info(WHATSAPP_ID, "Got image from %s: %s\n", who,url); // Search fot the combo PurpleConversation *convo = purple_find_conversation_with_account(PURPLE_CONV_TYPE_IM, who, acc); if (!convo) convo = purple_conversation_new(PURPLE_CONV_TYPE_IM, acc, who); int imgid = purple_imgstore_add_with_id(g_memdup(prev, size), size, NULL); serv_got_chat_in(gc, purple_conv_chat_get_id(PURPLE_CONV_IM(convo)), who, PURPLE_MESSAGE_RECV, msg, time(NULL)); purple_conv_im_write(PURPLE_CONV_IM(convo), who, g_strdup_printf("<a href=\"%s\"><img id=\"%u\"></a>",url,imgid), PURPLE_MESSAGE_RECV | PURPLE_MESSAGE_IMAGES, time(NULL)); } while (waAPI_querychatlocation(wconn->waAPI, &who, &prev, &size, &lat, &lng)) { purple_debug_info(WHATSAPP_ID, "Got geomessage from: %s Coordinates (%f %f)\n", who,(float)lat,(float)lng); // Search fot the combo PurpleConversation *convo = purple_find_conversation_with_account(PURPLE_CONV_TYPE_IM, who, acc); if (!convo) convo = purple_conversation_new(PURPLE_CONV_TYPE_IM, acc, who); int imgid = purple_imgstore_add_with_id(g_memdup(prev, size), size, NULL); serv_got_chat_in(gc, purple_conv_chat_get_id(PURPLE_CONV_IM(convo)), who, PURPLE_MESSAGE_RECV, msg, time(NULL)); purple_conv_im_write(PURPLE_CONV_IM(convo), who, g_strdup_printf("<a href=\"http://openstreetmap.org/?lat=%f&lon=%f&zoom=16\"><img src=\"%u\"></a>",lat,lng,imgid), PURPLE_MESSAGE_RECV | PURPLE_MESSAGE_IMAGES, time(NULL)); } while (waAPI_querychatsound(wconn->waAPI, &who, &url)) { purple_debug_info(WHATSAPP_ID, "Got chat sound from %s: %s\n", who,url); // Search fot the combo PurpleConversation *convo = purple_find_conversation_with_account(PURPLE_CONV_TYPE_IM, who, acc); if (!convo) convo = purple_conversation_new(PURPLE_CONV_TYPE_IM, acc, who); serv_got_chat_in(gc, purple_conv_chat_get_id(PURPLE_CONV_IM(convo)), who, PURPLE_MESSAGE_RECV, msg, time(NULL)); purple_conv_im_write(PURPLE_CONV_IM(convo), who, g_strdup_printf("<a href=\"%s\">%s</a>",url,url), PURPLE_MESSAGE_RECV , time(NULL)); } // User status change while (waAPI_querystatus(wconn->waAPI, &who, &status)) { if (status == 1) { purple_prpl_got_user_status(acc, who, "available", "message","", NULL); } else { purple_prpl_got_user_status(acc, who, "unavailable", "message","", NULL); } } // User typing info notify while (waAPI_querytyping(wconn->waAPI, &who, &status)) { if (status == 1) { serv_got_typing(gc,who,0,PURPLE_TYPING); } else { serv_got_typing_stopped(gc,who); } } // User profile picture char * icon, * hash; int len; while (waAPI_queryicon(wconn->waAPI, &who, &icon, &len, &hash)) { purple_buddy_icons_set_for_user(acc,who, g_memdup(icon,len),len, hash); } // Groups update if (waAPI_getgroupsupdated(wconn->waAPI)) { // Delete/update the chats that are in our list PurpleBlistNode* node = purple_blist_get_root(); while (node != 0) { if (PURPLE_BLIST_NODE_IS_CHAT(node)) { PurpleChat * ch = PURPLE_CHAT(node); if (purple_chat_get_account(ch) == acc) { int found = 0; GHashTable *hasht = purple_chat_get_components(ch); char * grid = g_hash_table_lookup(hasht, "id"); char * glist = waAPI_getgroups(wconn->waAPI); gchar **gplist = g_strsplit(glist,",",0); while (*gplist) { if (strcmp(*gplist,grid) == 0) { // The group is in the system, update the fields char *id,*sub,*own; waAPI_getgroupinfo(wconn->waAPI, *gplist, &sub, &own, 0); g_hash_table_insert(hasht, g_strdup("subject"), g_strdup(sub)); g_hash_table_insert(hasht, g_strdup("owner"), g_strdup(own)); found = 1; break; } gplist++; } // The group was deleted if (!found) { PurpleBlistNode* del = node; node = purple_blist_node_next(node,FALSE); purple_blist_remove_chat(del); } } } node = purple_blist_node_next(node,FALSE); } // Add new groups char * glist = waAPI_getgroups(wconn->waAPI); gchar **gplist = g_strsplit(glist,",",0); while (*gplist) { int found = 0; PurpleBlistNode* node = purple_blist_get_root(); PurpleChat * ch; while (node != 0) { if (PURPLE_BLIST_NODE_IS_CHAT(node)) { ch = PURPLE_CHAT(node); if (purple_chat_get_account(ch) == acc) { char * grid = g_hash_table_lookup(purple_chat_get_components(ch), "id"); if (strcmp(*gplist,grid) == 0) { found = 1; break; } } } node = purple_blist_node_next(node,FALSE); } if (!found) { char *sub,*own; waAPI_getgroupinfo(wconn->waAPI, *gplist, &sub, &own, 0); purple_debug_info("waprpl", "New group found %s %s\n", *gplist,sub); GHashTable * htable = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free); g_hash_table_insert(htable, g_strdup("subject"), g_strdup(sub)); g_hash_table_insert(htable, g_strdup("id"), g_strdup(*gplist)); g_hash_table_insert(htable, g_strdup("owner"), g_strdup(own)); ch = purple_chat_new(acc,sub,htable); purple_blist_add_chat(ch,NULL,NULL); } // Now update the open conversation that may exist char * id = g_hash_table_lookup(purple_chat_get_components(ch), "id"); int prplid = chatid_to_convo(id); PurpleConversation * conv = purple_find_chat(gc, prplid); if (conv) { char *subject, *owner, *part; if (!waAPI_getgroupinfo(wconn->waAPI, id, &subject, &owner, &part)) return; purple_conv_chat_clear_users(purple_conversation_get_chat_data(conv)); gchar **plist = g_strsplit(part,",",0); while (*plist) { purple_conv_chat_add_user (purple_conversation_get_chat_data(conv), *plist,"",PURPLE_CBFLAGS_NONE | (!strcmp(owner,*plist) ? PURPLE_CBFLAGS_FOUNDER : 0),FALSE); plist++; } } gplist++; } } }
static void waprpl_process_incoming_events(PurpleConnection * gc) { whatsapp_connection *wconn = purple_connection_get_protocol_data(gc); PurpleAccount *acc = purple_connection_get_account(gc); switch (waAPI_loginstatus(wconn->waAPI)) { case 0: purple_connection_update_progress(gc, "Connecting", 0, 4); break; case 1: purple_connection_update_progress(gc, "Sending authorization", 1, 4); break; case 2: purple_connection_update_progress(gc, "Awaiting response", 2, 4); break; case 3: purple_connection_update_progress(gc, "Connection established", 3, 4); purple_connection_set_state(gc, PURPLE_CONNECTED); if (!wconn->connected) waprpl_insert_contacts(gc); wconn->connected = 1; PurpleAccount *account = purple_connection_get_account(gc); PurpleStatus *status = purple_account_get_active_status(account); waprpl_set_status(account, status); break; default: break; }; char *msg, *who, *prev, *url, *author; int status; int size; double lat, lng; unsigned long timestamp; /* Incoming messages */ while (1) { int r = waAPI_querynext(wconn->waAPI); switch (r) { case 0: if (waAPI_querychat(wconn->waAPI, &who, &msg, &author, ×tamp)) { purple_debug_info(WHATSAPP_ID, "Got chat message from %s: %s\n", who, msg); conv_add_message(gc, who, msg, author, timestamp); } break; case 1: if (waAPI_querychatimage(wconn->waAPI, &who, &prev, &size, &url, &author, ×tamp)) { purple_debug_info(WHATSAPP_ID, "Got image from %s: %s\n", who, url); int imgid = purple_imgstore_add_with_id(g_memdup(prev, size), size, NULL); char *msg = g_strdup_printf("<a href=\"%s\"><img id=\"%u\"></a><br/><a href=\"%s\">%s</a>", url, imgid, url, url); conv_add_message(gc, who, msg, author, timestamp); g_free(msg); } break; case 2: if (waAPI_querychatlocation(wconn->waAPI, &who, &prev, &size, &lat, &lng, &author, ×tamp)) { purple_debug_info(WHATSAPP_ID, "Got geomessage from: %s Coordinates (%f %f)\n", who, (float)lat, (float)lng); int imgid = purple_imgstore_add_with_id(g_memdup(prev, size), size, NULL); char *msg = g_strdup_printf("<a href=\"http://openstreetmap.org/?lat=%f&lon=%f&zoom=16\"><img src=\"%u\"></a>", lat, lng, imgid); conv_add_message(gc, who, msg, author, timestamp); g_free(msg); } break; case 3: if (waAPI_querychatsound(wconn->waAPI, &who, &url, &author, ×tamp)) { purple_debug_info(WHATSAPP_ID, "Got chat sound from %s: %s\n", who, url); char *msg = g_strdup_printf("<a href=\"%s\">%s</a>", url, url); conv_add_message(gc, who, msg, author, timestamp); g_free(msg); } break; default: break; }; if (r < 0) break; } /* User status change */ while (waAPI_querystatus(wconn->waAPI, &who, &status)) { if (status == 1) { purple_prpl_got_user_status(acc, who, "available", "message", "", NULL); } else { purple_prpl_got_user_status(acc, who, "unavailable", "message", "", NULL); } } /* User typing info notify */ while (waAPI_querytyping(wconn->waAPI, &who, &status)) { if (status == 1) { purple_debug_info(WHATSAPP_ID, "%s is typing\n", who); serv_got_typing(gc, who, 0, PURPLE_TYPING); } else { purple_debug_info(WHATSAPP_ID, "%s is not typing\n", who); serv_got_typing(gc, who, 0, PURPLE_NOT_TYPING); serv_got_typing_stopped(gc, who); } } /* User profile picture */ char *icon, *hash; int len; while (waAPI_queryicon(wconn->waAPI, &who, &icon, &len, &hash)) { purple_buddy_icons_set_for_user(acc, who, g_memdup(icon, len), len, hash); } /* Groups update */ if (waAPI_getgroupsupdated(wconn->waAPI)) { /* Delete/update the chats that are in our list */ PurpleBlistNode *node; for (node = purple_blist_get_root(); node; node = purple_blist_node_next(node, FALSE)) { if (!PURPLE_BLIST_NODE_IS_CHAT(node)) continue; PurpleChat *ch = PURPLE_CHAT(node); if (purple_chat_get_account(ch) != acc) continue; GHashTable *hasht = purple_chat_get_components(ch); char *grid = g_hash_table_lookup(hasht, "id"); char *glist = waAPI_getgroups(wconn->waAPI); gchar **gplist = g_strsplit(glist, ",", 0); if (str_array_find(gplist, grid) >= 0) { /* The group is in the system, update the fields */ char *sub, *own; waAPI_getgroupinfo(wconn->waAPI, grid, &sub, &own, 0); g_hash_table_insert(hasht, g_strdup("subject"), g_strdup(sub)); g_hash_table_insert(hasht, g_strdup("owner"), g_strdup(own)); } else { /* The group was deleted */ PurpleChat *del = (PurpleChat *) node; node = purple_blist_node_next(node, FALSE); purple_blist_remove_chat(del); } g_strfreev(gplist); } /* Add new groups */ char *glist = waAPI_getgroups(wconn->waAPI); gchar **gplist = g_strsplit(glist, ",", 0); gchar **p; for (p = gplist; *p; p++) { gchar *gpid = *p; PurpleChat *ch = blist_find_chat_by_id(gc, gpid); if (!ch) { char *sub, *own; waAPI_getgroupinfo(wconn->waAPI, gpid, &sub, &own, 0); purple_debug_info("waprpl", "New group found %s %s\n", gpid, sub); GHashTable *htable = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free); g_hash_table_insert(htable, g_strdup("subject"), g_strdup(sub)); g_hash_table_insert(htable, g_strdup("id"), g_strdup(gpid)); g_hash_table_insert(htable, g_strdup("owner"), g_strdup(own)); ch = purple_chat_new(acc, sub, htable); purple_blist_add_chat(ch, NULL, NULL); } /* Now update the open conversation that may exist */ char *id = g_hash_table_lookup(purple_chat_get_components(ch), "id"); int prplid = chatid_to_convo(id); PurpleConversation *conv = purple_find_chat(gc, prplid); char *subject, *owner, *part; if (conv && waAPI_getgroupinfo(wconn->waAPI, id, &subject, &owner, &part)) conv_add_participants(conv, part, owner); } g_strfreev(gplist); } }
/*------------------------------------------------------------------------ * Callback function invoked when a custom emoticon request to the WAP site completes. * * @param url_data * @param user_data The Markup message object * @param url_text The data returned from the WAP site * @param len The length of the data returned * @param error_message Descriptive error message */ static void emoticon_returned( PurpleUtilFetchUrlData* url_data, gpointer user_data, const gchar* url_text, gsize len, const gchar* error_message ) { struct RXMsgData* mx = (struct RXMsgData*) user_data; const gchar* data = url_text; unsigned int pos = 0; int id; char* str; int em_size = 0; char* em_data = NULL; char* em_id = NULL; int* intptr = NULL; int res; purple_debug_info( MXIT_PLUGIN_ID, "emoticon_returned\n" ); /* remove request from the async outstanding calls list */ mx->session->async_calls = g_slist_remove( mx->session->async_calls, url_data ); if ( !url_text ) { /* no reply from the WAP site */ purple_debug_error( MXIT_PLUGIN_ID, "Error contacting the MXit WAP site. Please try again later (emoticon).\n" ); goto done; } #ifdef MXIT_DEBUG_EMO hex_dump( data, len ); #endif /* validate that the returned data starts with the magic constant that indicates it is a custom emoticon */ if ( len - pos < strlen( MXIT_FRAME_MAGIC ) || memcmp( MXIT_FRAME_MAGIC, &data[pos], strlen( MXIT_FRAME_MAGIC ) ) != 0 ) { purple_debug_error( MXIT_PLUGIN_ID, "Invalid emoticon received from wapsite (bad magic)\n" ); goto done; } pos += strlen( MXIT_FRAME_MAGIC ); /* validate the image frame desc byte */ if ( data[pos] != '\x6F' ) { purple_debug_error( MXIT_PLUGIN_ID, "Invalid emoticon received from wapsite (bad frame desc)\n" ); goto done; } pos++; /* get the frame image data length */ res = asn_getlength( &data[pos], len - pos, &em_size ); if ( res <= 0 ) { purple_debug_error( MXIT_PLUGIN_ID, "Invalid emoticon received from wapsite (bad frame length)\n" ); goto done; } pos += res; #ifdef MXIT_DEBUG_EMO purple_debug_info( MXIT_PLUGIN_ID, "read the length '%i'\n", em_size ); #endif /* utf-8 (emoticon name) */ res = asn_getUtf8( &data[pos], len - pos, 0x0C, &str ); if ( res <= 0 ) { purple_debug_error( MXIT_PLUGIN_ID, "Invalid emoticon received from wapsite (bad name string)\n" ); goto done; } pos += res; #ifdef MXIT_DEBUG_EMO purple_debug_info( MXIT_PLUGIN_ID, "read the string '%s'\n", str ); #endif g_free( str ); str = NULL; /* utf-8 (emoticon shortcut) */ res = asn_getUtf8( &data[pos], len - pos, 0x81, &str ); if ( res <= 0 ) { purple_debug_error( MXIT_PLUGIN_ID, "Invalid emoticon received from wapsite (bad shortcut string)\n" ); goto done; } pos += res; #ifdef MXIT_DEBUG_EMO purple_debug_info( MXIT_PLUGIN_ID, "read the string '%s'\n", str ); #endif em_id = str; /* validate the image data type */ if ( len - pos < 1 || data[pos] != '\x82' ) { purple_debug_error( MXIT_PLUGIN_ID, "Invalid emoticon received from wapsite (bad data type)\n" ); g_free( em_id ); goto done; } pos++; /* get the data length */ res = asn_getlength( &data[pos], len - pos, &em_size ); if ( res <= 0 ) { /* bad frame length */ purple_debug_error( MXIT_PLUGIN_ID, "Invalid emoticon received from wapsite (bad data length)\n" ); g_free( em_id ); goto done; } pos += res; #ifdef MXIT_DEBUG_EMO purple_debug_info( MXIT_PLUGIN_ID, "read the length '%i'\n", em_size ); #endif if ( len - pos < (gsize)em_size ) { /* not enough bytes left in data! */ purple_debug_error( MXIT_PLUGIN_ID, "Invalid emoticon received from wapsite (data length too long)\n"); g_free( em_id ); goto done; } /* strip the mxit markup tags from the emoticon id (eg, .{XY} -> XY) */ if ( ( em_id[0] == '.' ) && ( em_id[1] == '{' ) ) { char emo[MXIT_MAX_EMO_ID + 1]; parse_emoticon_str( &em_id[2], emo ); strcpy( em_id, emo ); } if ( g_hash_table_lookup( mx->session->iimages, em_id ) ) { /* emoticon found in the table, so ignore this one */ g_free( em_id ); goto done; } /* make a copy of the data */ em_data = g_malloc( em_size ); memcpy( em_data, &data[pos], em_size ); /* we now have the emoticon, store it in the imagestore */ id = purple_imgstore_add_with_id( em_data, em_size, NULL ); /* map the mxit emoticon id to purple image id */ intptr = g_malloc( sizeof( int ) ); *intptr = id; g_hash_table_insert( mx->session->iimages, em_id, intptr ); mx->flags |= PURPLE_MESSAGE_IMAGES; done: mx->img_count--; if ( ( mx->img_count == 0 ) && ( mx->converted ) ) { /* * this was the last outstanding emoticon for this message, * so we can now display it to the user. */ mxit_show_message( mx ); } }
static void waprpl_process_incoming_events(PurpleConnection * gc) { whatsapp_connection *wconn = purple_connection_get_protocol_data(gc); PurpleAccount *acc = purple_connection_get_account(gc); int err; do { char * reason; err = waAPI_geterror(wconn->waAPI, &reason); if (err != 0) { PurpleConnectionError errcode = PURPLE_CONNECTION_ERROR_OTHER_ERROR; if (err == 1) errcode = PURPLE_CONNECTION_ERROR_AUTHENTICATION_FAILED; purple_connection_error_reason(gc, errcode, reason); g_free(reason); } } while (err != 0); switch (waAPI_loginstatus(wconn->waAPI)) { case 0: purple_connection_update_progress(gc, "Connecting", 0, 4); break; case 1: purple_connection_update_progress(gc, "Sending authorization", 1, 4); break; case 2: purple_connection_update_progress(gc, "Awaiting response", 2, 4); break; case 3: if (!wconn->connected) { purple_connection_update_progress(gc, "Connection established", 3, 4); purple_connection_set_state(gc, PURPLE_CONNECTED); PurpleAccount *account = purple_connection_get_account(gc); PurpleStatus *status = purple_account_get_active_status(account); waprpl_insert_contacts(gc); waprpl_set_status(account, status); wconn->connected = 1; } break; default: break; }; /* Groups update */ if (waAPI_getgroupsupdated(wconn->waAPI)) { purple_debug_info(WHATSAPP_ID, "Receiving update information from my groups\n"); /* Delete/update the chats that are in our list */ PurpleBlistNode *node; for (node = purple_blist_get_root(); node; node = purple_blist_node_next(node, FALSE)) { if (!PURPLE_BLIST_NODE_IS_CHAT(node)) continue; PurpleChat *ch = PURPLE_CHAT(node); if (purple_chat_get_account(ch) != acc) continue; GHashTable *hasht = purple_chat_get_components(ch); char *grid = g_hash_table_lookup(hasht, "id"); char *glist = waAPI_getgroups(wconn->waAPI); gchar **gplist = g_strsplit(glist, ",", 0); if (str_array_find(gplist, grid) >= 0) { /* The group is in the system, update the fields */ char *sub, *own; waAPI_getgroupinfo(wconn->waAPI, grid, &sub, &own, 0); g_hash_table_replace(hasht, g_strdup("subject"), sub); g_hash_table_replace(hasht, g_strdup("owner"), own); purple_blist_alias_chat(ch, sub); } else { /* The group was deleted */ PurpleChat *del = (PurpleChat *) node; node = purple_blist_node_next(node, FALSE); purple_blist_remove_chat(del); } g_strfreev(gplist); g_free(glist); } /* Add new groups */ char *glist = waAPI_getgroups(wconn->waAPI); gchar **gplist = g_strsplit(glist, ",", 0); gchar **p; for (p = gplist; *p; p++) { gchar *gpid = *p; PurpleChat *ch = blist_find_chat_by_id(gc, gpid); if (!ch) ch = create_chat_group(gpid, wconn, acc); /* Now update the open conversation that may exist */ char *id = g_hash_table_lookup(purple_chat_get_components(ch), "id"); int prplid = chatid_to_convo(id); PurpleConversation *conv = purple_find_chat(gc, prplid); char *subject, *owner, *part; if (conv && waAPI_getgroupinfo(wconn->waAPI, id, &subject, &owner, &part)) { conv_add_participants(conv, part, owner); } } g_strfreev(gplist); g_free(glist); } t_message m; while (waAPI_querymsg(wconn->waAPI, &m)) { switch (m.type) { case 0: purple_debug_info(WHATSAPP_ID, "Got chat message from %s: %s\n", m.who, m.message); conv_add_message(gc, m.who, m.message, m.author, m.t); break; case 1: { purple_debug_info(WHATSAPP_ID, "Got image from %s: %s\n", m.who, m.message); int imgid = purple_imgstore_add_with_id(g_memdup(m.image, m.imagelen), m.imagelen, NULL); char *msg = g_strdup_printf("<a href=\"%s\"><img id=\"%u\"></a><br/><a href=\"%s\">%s</a>", m.url, imgid, m.url, m.url); conv_add_message(gc, m.who, msg, m.author, m.t); g_free(msg); } break; case 2: { purple_debug_info(WHATSAPP_ID, "Got geomessage from: %s Coordinates (%f %f)\n", m.who, (float)m.lat, (float)m.lng); char * lat = dbl2str(m.lat); char * lng = dbl2str(m.lng); char *msg = g_strdup_printf("<a href=\"http://openstreetmap.org/?lat=%s&lon=%s&zoom=20\">" "http://openstreetmap.org/?lat=%s&lon=%s&zoom=20</a>", lat, lng, lat, lng); conv_add_message(gc, m.who, msg, m.author, m.t); g_free(msg); g_free(lng); g_free(lat); } break; case 3: { purple_debug_info(WHATSAPP_ID, "Got chat sound from %s: %s\n", m.who, m.url); char *msg = g_strdup_printf("<a href=\"%s\">%s</a>", m.url, m.url); conv_add_message(gc, m.who, msg, m.author, m.t); g_free(msg); } break; case 4: { purple_debug_info(WHATSAPP_ID, "Got chat video from %s: %s\n", m.who, m.url); char *msg = g_strdup_printf("<a href=\"%s\">%s</a>", m.url, m.url); conv_add_message(gc, m.who, msg, m.author, m.t); g_free(msg); } break; case 5: { purple_debug_info(WHATSAPP_ID, "Got phone call from %s\n", m.who); conv_add_message(gc, m.who, "[Trying to voice-call you]", m.author, m.t); } break; default: purple_debug_info(WHATSAPP_ID, "Got an unrecognized message!\n"); break; }; g_free(m.who); g_free(m.author); g_free(m.message); } while (1) { int typer; char msgid[128]; if (!waAPI_queryreceivedmsg(wconn->waAPI, msgid, &typer)) break; purple_debug_info(WHATSAPP_ID, "Received message %s type: %d\n", msgid, typer); purple_signal_emit(purple_connection_get_prpl(gc), "whatsapp-message-received", gc, msgid, typer); } /* Status changes, typing notices and profile pictures. */ query_status(gc); query_typing(gc); query_icon(gc); }