예제 #1
0
int acl_xml_decode(const char *in, ACL_VSTRING *out)
{
    int   n = 0, len;
    const char *ptr = in, *pre;
    const ACL_TOKEN *token;
    const XML_SPEC *spec;

    acl_pthread_once(&__token_once, xml_decode_init);
    if (__token_tree == NULL)
        acl_msg_fatal("__token_tree null");

    while (*ptr != 0) {
        pre = ptr;
        token = acl_token_tree_match(__token_tree, &ptr, NULL, NULL);
        if (token == NULL) {
            pre = markup_unescape(pre, out);
            len = (int) (ptr - pre);
            if (len > 0)
                acl_vstring_memcat(out, pre, len);
            break;
        }
        spec = (const XML_SPEC*) token->ctx;
        acl_assert(spec != NULL);

        len = (int) (ptr - pre - spec->len);
        if (len > 0)
            acl_vstring_memcat(out, pre, len);
        acl_vstring_strcat(out, spec->str);
        n++;
    }

    ACL_VSTRING_TERMINATE(out);
    return (n);
}
예제 #2
0
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;
}