qq_im_format *qq_im_fmt_new_by_purple(const gchar *msg) { qq_im_format *fmt; const gchar *start, *end, *last; GData *attribs; gchar *tmp; unsigned char *rgb; g_return_val_if_fail(msg != NULL, NULL); fmt = qq_im_fmt_new(); last = msg; while (purple_markup_find_tag("font", last, &start, &end, &attribs)) { tmp = g_datalist_get_data(&attribs, "face"); if (tmp && strlen(tmp) > 0) { if (fmt->font) g_free(fmt->font); fmt->font_len = strlen(tmp); fmt->font = g_strdup(tmp); } tmp = g_datalist_get_data(&attribs, "size"); if (tmp) { fmt->attr = atoi(tmp) * 3 + 1; fmt->attr &= 0x0f; } tmp = g_datalist_get_data(&attribs, "color"); if (tmp && strlen(tmp) > 1) { rgb = purple_base16_decode(tmp + 1, NULL); g_memmove(fmt->rgb, rgb, 3); g_free(rgb); } g_datalist_clear(&attribs); last = end + 1; } if (purple_markup_find_tag("b", msg, &start, &end, &attribs)) { fmt->attr |= 0x20; g_datalist_clear(&attribs); } if (purple_markup_find_tag("i", msg, &start, &end, &attribs)) { fmt->attr |= 0x40; g_datalist_clear(&attribs); } if (purple_markup_find_tag("u", msg, &start, &end, &attribs)) { fmt->attr |= 0x80; g_datalist_clear(&attribs); } return fmt; }
gboolean qq_im_smiley_none(const gchar *msg) { const gchar *start, *end, *last; GData *attribs; gchar *tmp; gboolean ret = FALSE; g_return_val_if_fail(msg != NULL, TRUE); last = msg; while (purple_markup_find_tag("font", last, &start, &end, &attribs)) { tmp = g_datalist_get_data(&attribs, "sml"); if (tmp && strlen(tmp) > 0) { if (strcmp(tmp, "none") == 0) { ret = TRUE; break; } } g_datalist_clear(&attribs); last = end + 1; } return ret; }
SilcDList silcpurple_image_message(const char *msg, SilcMessageFlags *mflags) { SilcMime mime = NULL, p; SilcDList list, parts = NULL; const char *start, *end, *last; GData *attribs; char *type; gboolean images = FALSE; last = msg; while (last && *last && purple_markup_find_tag("img", last, &start, &end, &attribs)) { PurpleStoredImage *image = NULL; const char *id; /* Check if there is text before image */ if (start - last) { char *text, *tmp; p = silc_mime_alloc(); /* Add content type */ silc_mime_add_field(p, "Content-Type", "text/plain; charset=utf-8"); tmp = g_strndup(last, start - last); text = purple_unescape_html(tmp); g_free(tmp); /* Add text */ silc_mime_add_data(p, (const unsigned char *)text, strlen(text)); g_free(text); if (!parts) parts = silc_dlist_init(); silc_dlist_add(parts, p); } id = g_datalist_get_data(&attribs, "id"); if (id && (image = purple_imgstore_find_by_id(atoi(id)))) { unsigned long imglen = purple_imgstore_get_size(image); gconstpointer img = purple_imgstore_get_data(image); p = silc_mime_alloc(); /* Add content type */ type = silcpurple_file2mime(purple_imgstore_get_filename(image)); if (!type) { g_datalist_clear(&attribs); last = end + 1; continue; } silc_mime_add_field(p, "Content-Type", type); g_free(type); /* Add content transfer encoding */ silc_mime_add_field(p, "Content-Transfer-Encoding", "binary"); /* Add image data */ silc_mime_add_data(p, img, imglen); if (!parts) parts = silc_dlist_init(); silc_dlist_add(parts, p); images = TRUE; } g_datalist_clear(&attribs); /* Continue after tag */ last = end + 1; } /* Check for text after the image(s) */ if (images && last && *last) { char *tmp = purple_unescape_html(last); p = silc_mime_alloc(); /* Add content type */ silc_mime_add_field(p, "Content-Type", "text/plain; charset=utf-8"); /* Add text */ silc_mime_add_data(p, (const unsigned char *)tmp, strlen(tmp)); g_free(tmp); if (!parts) parts = silc_dlist_init(); silc_dlist_add(parts, p); } /* If there weren't any images, don't return anything. */ if (!images) { if (parts) silc_dlist_uninit(parts); return NULL; } if (silc_dlist_count(parts) > 1) { /* Multipart MIME message */ char b[32]; mime = silc_mime_alloc(); silc_mime_add_field(mime, "MIME-Version", "1.0"); g_snprintf(b, sizeof(b), "b%4X%4X", (unsigned int)time(NULL), silc_dlist_count(parts)); silc_mime_set_multipart(mime, "mixed", b); silc_dlist_start(parts); while ((p = silc_dlist_get(parts)) != SILC_LIST_END) silc_mime_add_multipart(mime, p); } else { /* Simple MIME message */ silc_dlist_start(parts); mime = silc_dlist_get(parts); silc_mime_add_field(mime, "MIME-Version", "1.0"); } *mflags &= ~SILC_MESSAGE_FLAG_UTF8; *mflags |= SILC_MESSAGE_FLAG_DATA; /* Encode message. Fragment if it is too large */ list = silc_mime_encode_partial(mime, 0xfc00); silc_dlist_uninit(parts); /* Added multiparts gets freed here */ silc_mime_free(mime); return list; }
int PurpleLine::send_message(std::string to, const char *markup) { // Parse markup and send message as parts if it contains images bool any_sent = false; std::cout << "debug ---> " << std::endl; std::cout << to << std::endl; std::cout << markup << std::endl; for (const char *p = markup; p && *p; ) { const char *start, *end; const char *filestart, *fileend; GData *attributes; GData *fileattributes; std::cout << "Sending LINE Message..." << std::endl; bool img_found = purple_markup_find_tag("IMG", p, &start, &end, &attributes); bool imgfile_found = purple_markup_find_tag("FILE", p, &filestart, &fileend, &fileattributes); std::cout << "---Image file ? " << std::endl; std::cout << img_found << std::endl; std::cout << imgfile_found << std::endl; std::string text; if (img_found) { // Image found but there's text before it, store it text = std::string(p, start - p); p = end + 1; } else { // No image found, store the rest of the text text = std::string(p); // Break the loop p = NULL; } // If the text is not all whitespace, send it as a text message if (text.find_first_not_of("\t\n\r ") != std::string::npos && !imgfile_found) { line::Message msg; msg.contentType = line::ContentType::NONE; msg.from = profile.mid; msg.to = to; msg.text = markup_unescape(text); send_message(msg); any_sent = true; } if (imgfile_found) { std::cout << "Start upload file!!" << std::endl; char *path = (char *)g_datalist_get_data(&fileattributes, "path"); std::cout << path << std::endl; gchar *data = NULL; size_t len; GError *err = NULL; if (!g_file_get_contents(path, &data, &len, &err)) { std::cout << "Error get file contents!!" << std::endl; continue; } std::cout << "Success get file content!!" << std::endl; PurpleStoredImage *img = purple_imgstore_add(data, len, path); if (!img) { std::cout << "Error image stored add!!" << std::endl; continue; } std::cout << "Success image stored add!!" << std::endl; std::string img_data( (const char *)purple_imgstore_get_data(img), purple_imgstore_get_size(img)); line::Message msg; msg.contentType = line::ContentType::IMAGE; msg.from = profile.mid; msg.to = to; send_message(msg, [this, img_data](line::Message &msg_back) { upload_media(msg_back.id, "image", img_data); }); any_sent = true; } if (img_found) { // Image test int image_id = std::stoi((char *)g_datalist_get_data(&attributes, "id")); g_datalist_clear(&attributes); std::stringstream ss; ss << "(img ID: " << image_id << ")"; PurpleStoredImage *img = purple_imgstore_find_by_id(image_id); if (!img) { purple_debug_warning("line", "Tried to send non-existent image: %d\n", image_id); continue; } std::string img_data( (const char *)purple_imgstore_get_data(img), purple_imgstore_get_size(img)); line::Message msg; msg.contentType = line::ContentType::IMAGE; msg.from = profile.mid; msg.to = to; send_message(msg, [this, img_data](line::Message &msg_back) { upload_media(msg_back.id, "image", img_data); }); any_sent = true; } } return any_sent ? 1 : 0; }