Example #1
0
xmlnode * _h_elim_image ( const char *name ,
                          const char *id   ,
                          SEXP_VALUE *args ,
                          gpointer data    )
{
    ASSERT_ALISTP( args, id, name );
    xmlnode *rval = xnode_new( "alist" );
    int image_id  = (int)ALIST_VAL_INT( args, "image-id" );

    if( image_id <= 0 )
    {
        sexp_val_free( args );
        return response_error( EINVAL, id, name, "bad image ID" );
    }

    fprintf( stderr, "searching for image id %d\n", image_id );
    PurpleStoredImage *image = purple_imgstore_find_by_id( image_id );
    
    if( !image )
    {
        sexp_val_free( args );
        return response_error( ENOENT, id, name, "image ID not found" );
    }

    size_t size = IGET( image , size );

    AL_INT ( rval, "image-id"  , image_id );
    AL_INT ( rval, "image-size", size     );
    AL_STR ( rval, "image-file", IGET( image, filename  ) );
    AL_STR ( rval, "image-type", IGET( image, extension ) );
    AL_DATA( rval, "image-data", IGET( image, data      ) , size );

    sexp_val_free( args );
    return response_value( 0, id, name, rval );
}
Example #2
0
void purple_imgstore_unref_by_id(int id)
{
	PurpleStoredImage *img = purple_imgstore_find_by_id(id);

	g_return_if_fail(img != NULL);

	purple_imgstore_unref(img);
}
Example #3
0
ggp_image_prepare_result ggp_image_prepare(PurpleConversation *conv,
	const int stored_id, uint64_t *id)
{
	PurpleConnection *gc = purple_conversation_get_connection(conv);
	ggp_image_session_data *sdata = ggp_image_get_sdata(gc);
	PurpleStoredImage *image = purple_imgstore_find_by_id(stored_id);
	size_t image_size;
	gconstpointer image_data;
	uint32_t image_crc;
	ggp_image_sent *sent_image;

	if (!image) {
		purple_debug_error("gg", "ggp_image_prepare: image %d "
			"not found in image store\n", stored_id);
		return GGP_IMAGE_PREPARE_FAILURE;
	}

	image_size = purple_imgstore_get_size(image);

	if (image_size > GGP_IMAGE_SIZE_MAX) {
		purple_debug_warning("gg", "ggp_image_prepare: image "
			"is too big (max bytes: %d)\n", GGP_IMAGE_SIZE_MAX);
		return GGP_IMAGE_PREPARE_TOO_BIG;
	}

	purple_imgstore_ref(image);
	image_data = purple_imgstore_get_data(image);
	image_crc = gg_crc32(0, image_data, image_size);

	purple_debug_info("gg", "ggp_image_prepare: image prepared "
		"[id=%d, crc=%u, size=%" G_GSIZE_FORMAT "]\n",
		stored_id, image_crc, image_size);

	*id = ggp_image_params_to_id(image_crc, image_size);

	sent_image = g_new(ggp_image_sent, 1);
	sent_image->id = stored_id;
	sent_image->conv_name = g_strdup(purple_conversation_get_name(conv));
	g_hash_table_insert(sdata->sent_images, ggp_uint64dup(*id),
		sent_image);

	return GGP_IMAGE_PREPARE_OK;
}
Example #4
0
/*------------------------------------------------------------------------
 * Insert an inline image command.
 *
 *  @param mx				The message text as processed so far.
 *  @oaram id				The imgstore ID of the inline image.
 */
static void inline_image_add( GString* mx, int id )
{
	PurpleStoredImage *image;
	gconstpointer img_data;
	gsize img_size;
	gchar* enc;

	image = purple_imgstore_find_by_id( id );
	if ( image == NULL )
		return;

	img_data = purple_imgstore_get_data( image );
	img_size = purple_imgstore_get_size( image );

	enc = purple_base64_encode( img_data, img_size );

	g_string_append( mx, "::op=img|dat=" );
	g_string_append( mx, enc );
	g_string_append_c( mx, ':' );

	g_free( enc );
}
Example #5
0
void ggp_image_send(PurpleConnection *gc,
	const struct gg_event_image_request *image_request)
{
	GGPInfo *accdata = purple_connection_get_protocol_data(gc);
	ggp_image_session_data *sdata = ggp_image_get_sdata(gc);
	ggp_image_sent *sent_image;
	PurpleStoredImage *image;
	PurpleConversation *conv;
	uint64_t id;
	gchar *gg_filename;

	purple_debug_info("gg", "ggp_image_send: got image request "
		"[uin=%u, crc=%u, size=%u]\n",
		image_request->sender,
		image_request->crc32,
		image_request->size);

	id = ggp_image_params_to_id(image_request->crc32, image_request->size);

	sent_image = g_hash_table_lookup(sdata->sent_images, &id);

	if (sent_image == NULL && image_request->sender == ggp_str_to_uin(
		purple_account_get_username(purple_connection_get_account(gc))))
	{
		purple_debug_misc("gg", "ggp_image_send: requested image "
			"not found, but this may be another session request\n");
		return;
	}
	if (sent_image == NULL) {
		purple_debug_warning("gg", "ggp_image_send: requested image "
			"not found\n");
		return;
	}

	purple_debug_misc("gg", "ggp_image_send: requested image found "
		"[id=" GGP_IMAGE_ID_FORMAT ", stored id=%d, conv=%s]\n",
		id,
		sent_image->id,
		sent_image->conv_name);

	image = purple_imgstore_find_by_id(sent_image->id);

	if (!image) {
		purple_debug_error("gg", "ggp_image_send: requested image "
			"found, but doesn't exists in image store\n");
		g_hash_table_remove(sdata->sent_images,
			GINT_TO_POINTER(image_request->crc32));
		return;
	}

	/* TODO: check allowed recipients */
	gg_filename = g_strdup_printf(GGP_IMAGE_ID_FORMAT, id);
	gg_image_reply(accdata->session, image_request->sender,
		gg_filename,
		purple_imgstore_get_data(image),
		purple_imgstore_get_size(image));
	g_free(gg_filename);

	conv = purple_conversations_find_with_account(
		sent_image->conv_name,
		purple_connection_get_account(gc));
	if (conv != NULL) {
		gchar *msg = g_strdup_printf(_("Image delivered to %u."),
			image_request->sender);
		purple_conversation_write(conv, "", msg,
			PURPLE_MESSAGE_NO_LOG | PURPLE_MESSAGE_NOTIFY,
			time(NULL));
		g_free(msg);
	}
}
Example #6
0
int tgp_msg_send (struct tgl_state *TLS, const char *message, tgl_peer_id_t to) {
  // search for outgoing embedded image tags and send them
  gchar *img = NULL;
  gchar *stripped = NULL;
  if ((img = g_strrstr (message, "<IMG")) || (img = g_strrstr (message, "<img"))) {
    if (tgl_get_peer_type(to) == TGL_PEER_ENCR_CHAT) {
      tgp_msg_err_out (TLS, "Sorry, sending documents to encrypted chats not yet supported.", to);
      return 0;
    }
    
    debug ("img found: %s", img);
    gchar *id;
    if ((id = g_strrstr (img, "ID=\"")) || (id = g_strrstr (img, "id=\""))) {
      id += 4;
      debug ("id found: %s", id);
      int imgid = atoi (id);
      if (imgid > 0) {
        PurpleStoredImage *psi = purple_imgstore_find_by_id (imgid);
        gchar *tmp = g_build_filename(g_get_tmp_dir(), purple_imgstore_get_filename (psi), NULL) ;
        GError *err = NULL;
        gconstpointer data = purple_imgstore_get_data (psi);
        g_file_set_contents (tmp, data, purple_imgstore_get_size (psi), &err);
        if (! err) {
          stripped = purple_markup_strip_html (message);
          tgl_do_send_document (TLS, to, tmp, stripped, (int)strlen (stripped), TGL_SEND_MSG_FLAG_DOCUMENT_AUTO, tgp_msg_send_done, NULL);
          g_free (stripped);
          return 1;
        } else {
          failure ("Cannot store image, temp directory not available: %s\n", err->message);
          g_error_free (err);
          return 0;
        }
      }
    }
    // no image id found in image
    return 0;
  }
  
#ifndef __ADIUM_
  /*
    Adium won't escape any HTML markup and just pass any user-input through,
    while Pidgin will replace special chars with the escape chars and also add 
    additional markup for RTL languages and such.

    First, we remove any HTML markup added by Pidgin, since Telegram won't handle it properly.
    User-entered HTML is still escaped and therefore won't be harmed.
   */
  stripped = purple_markup_strip_html (message);
  
  /* 
    now unescape the markup, so that html special chars will still show
    up properly in Telegram
   */
  gchar *unescaped = purple_unescape_text (stripped);
  int ret = tgp_msg_send_split (TLS, stripped, to);
  
  g_free (unescaped);
  g_free (stripped);
  return ret;
#endif
  
  return tgp_msg_send_split (TLS, message, to);
}
Example #7
0
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;
}
Example #8
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;
}