Exemplo n.º 1
0
static void collector_foreach_callback(GMimeObject *parent, GMimeObject *part, gpointer user_data) {
  g_return_if_fail(user_data != NULL);

  PartCollectorData *fdata = (PartCollectorData *) user_data;

  if (GMIME_IS_MESSAGE_PART(part)) {

    if (fdata->recursion_depth++ < RECURSION_LIMIT) {
      GMimeMessage *message = g_mime_message_part_get_message((GMimeMessagePart *) part); // transfer none
      if (message)
        g_mime_message_foreach(message, collector_foreach_callback, user_data);

    } else {
      g_printerr("endless recursion detected: %d\r\n", fdata->recursion_depth);
      return;
    }

  } else if (GMIME_IS_MESSAGE_PARTIAL(part)) {
    // Save into an array ? Todo: Look into the specs
  } else if (GMIME_IS_MULTIPART(part)) {
    // Nothing special needed on multipart, let descend further
  } else if (GMIME_IS_PART(part)) {
    collect_part(part, fdata, GMIME_IS_MULTIPART(parent));
    fdata->part_id++;
  } else {
    g_assert_not_reached();
  }
}
Exemplo n.º 2
0
/**
 * g_mime_multipart_get_subpart_from_content_id: 
 * @multipart: a multipart
 * @content_id: the content id of the part to look for
 *
 * Gets the mime part with the content-id @content_id from the
 * multipart @multipart.
 *
 * Returns: (transfer none): the #GMimeObject whose content-id matches
 * the search string, or %NULL if a match cannot be found.
 **/
GMimeObject *
g_mime_multipart_get_subpart_from_content_id (GMimeMultipart *multipart, const char *content_id)
{
	GMimeObject *object = (GMimeObject *) multipart;
	GMimeObject *subpart, *part;
	GMimeMultipart *mpart;
	guint i;
	
	g_return_val_if_fail (GMIME_IS_MULTIPART (multipart), NULL);
	g_return_val_if_fail (content_id != NULL, NULL);
	
	if (object->content_id && !strcmp (object->content_id, content_id))
		return object;
	
	for (i = 0; i < multipart->children->len; i++) {
		subpart = multipart->children->pdata[i];
		
		if (subpart->content_id && !strcmp (subpart->content_id, content_id))
			return subpart;
		
		if (GMIME_IS_MULTIPART (subpart)) {
			mpart = (GMimeMultipart *) subpart;
			if ((part = g_mime_multipart_get_subpart_from_content_id (mpart, content_id)))
				return part;
		}
	}
	
	return NULL;
}
Exemplo n.º 3
0
static void part_extractor_foreach_callback(GMimeObject *parent, GMimeObject *part, gpointer user_data) {
  PartExtractorData *a_data = (PartExtractorData *) user_data;

  if (GMIME_IS_MESSAGE_PART(part)) {

    if (a_data->recursion_depth < RECURSION_LIMIT) {
      GMimeMessage *message = g_mime_message_part_get_message((GMimeMessagePart *) part); // transfer none
      if (message)
        g_mime_message_foreach(message, part_extractor_foreach_callback, a_data);

    } else {
      g_printerr("endless recursion detected: %d\r\n", a_data->recursion_depth);
      return;
    }

  } else if (GMIME_IS_MESSAGE_PARTIAL (part)) {
    // Save into an array ? Todo: Look into the specs
  } else if (GMIME_IS_MULTIPART (part)) {
    // Nothing special needed on multipart, let descend further
  } else if (GMIME_IS_PART (part)) {

    // We are interested only in the part 0 (counting down by same logic)
    if (a_data->part_id == 0)
      extract_part(part, a_data);

    a_data->part_id--;

  } else {
    g_assert_not_reached();
  }
}
Exemplo n.º 4
0
/**
 * sign_prepare:
 * @mime_part: MIME part
 *
 * Prepare a part (and all subparts) to be signed. To do this we need
 * to set the encoding of all parts (that are not already encoded to
 * either QP or Base64 or 7-bit) to QP.
 *
 * Ref: RFC 3156, sect. 3.
 **/
static void
sign_prepare(GMimeObject * mime_part)
{
    GMimeContentEncoding encoding;
    GMimeObject *subpart;

    if (GMIME_IS_MULTIPART(mime_part)) {
        GMimeMultipart *multipart;
        int i, n;

	multipart = (GMimeMultipart *) mime_part;

	if (GMIME_IS_MULTIPART_SIGNED(multipart) ||
	    GMIME_IS_MULTIPART_ENCRYPTED(multipart)) {
	    /* must not modify these parts as they must be treated as opaque */
	    return;
	}

	n = g_mime_multipart_get_count(multipart);
	for (i = 0; i < n; i++) {
	    subpart = g_mime_multipart_get_part(multipart, i);
	    sign_prepare(subpart);
	}
    } else if (GMIME_IS_MESSAGE_PART(mime_part)) {
	subpart = GMIME_MESSAGE_PART(mime_part)->message->mime_part;
	sign_prepare(subpart);
    } else {
	encoding = g_mime_part_get_content_encoding(GMIME_PART(mime_part));
	if ((encoding != GMIME_CONTENT_ENCODING_BASE64) && (encoding != GMIME_CONTENT_ENCODING_7BIT))
	    g_mime_part_set_content_encoding(GMIME_PART(mime_part),
					     GMIME_CONTENT_ENCODING_QUOTEDPRINTABLE);
    }
}
Exemplo n.º 5
0
/**
 * g_mime_multipart_clear:
 * @multipart: a #GMimeMultipart object
 *
 * Removes all subparts from @multipart.
 **/
void
g_mime_multipart_clear (GMimeMultipart *multipart)
{
	g_return_if_fail (GMIME_IS_MULTIPART (multipart));
	
	GMIME_MULTIPART_GET_CLASS (multipart)->clear (multipart);
}
Exemplo n.º 6
0
static gboolean
handle_mime_object (MuMsg *msg, GMimeObject *mobj, GMimeObject *parent,
		    MuMsgOptions opts, unsigned *index, gboolean decrypted,
		    MuMsgPartForeachFunc func, gpointer user_data)
{
	if (GMIME_IS_PART (mobj))
		return handle_part
			(msg, GMIME_PART(mobj), parent,
			 opts, index, decrypted, func, user_data);
	else if (GMIME_IS_MESSAGE_PART (mobj))
		return handle_message_part
			(msg, GMIME_MESSAGE_PART(mobj),
			 parent, opts, index, decrypted, func, user_data);
	else if ((opts & MU_MSG_OPTION_VERIFY) &&
	         GMIME_IS_MULTIPART_SIGNED (mobj)) {
		check_signature
			(msg, GMIME_MULTIPART_SIGNED (mobj), opts);
		return handle_multipart
			(msg, GMIME_MULTIPART (mobj), mobj, opts,
			 index, decrypted, func, user_data);
	} else if ((opts & MU_MSG_OPTION_DECRYPT) &&
	           GMIME_IS_MULTIPART_ENCRYPTED (mobj))
		return handle_encrypted_part
			(msg, GMIME_MULTIPART_ENCRYPTED (mobj),
			 opts, index, func, user_data);
	else if (GMIME_IS_MULTIPART (mobj))
		return handle_multipart
			(msg, GMIME_MULTIPART (mobj), parent, opts,
			 index, decrypted, func, user_data);
	return TRUE;
}
Exemplo n.º 7
0
/**
 * g_mime_multipart_set_boundary:
 * @multipart: a #GMimeMultipart object
 * @boundary: boundary or %NULL to autogenerate one
 *
 * Sets @boundary as the boundary on the multipart. If @boundary is
 * %NULL, then a boundary will be auto-generated for you.
 **/
void
g_mime_multipart_set_boundary (GMimeMultipart *multipart, const char *boundary)
{
	g_return_if_fail (GMIME_IS_MULTIPART (multipart));
	
	GMIME_MULTIPART_GET_CLASS (multipart)->set_boundary (multipart, boundary);
}
Exemplo n.º 8
0
/**
 * g_mime_multipart_get_count:
 * @multipart: a #GMimeMultipart object
 *
 * Gets the number of parts contained within @multipart.
 *
 * Returns: the number of parts contained within @multipart.
 **/
int
g_mime_multipart_get_count (GMimeMultipart *multipart)
{
	g_return_val_if_fail (GMIME_IS_MULTIPART (multipart), -1);
	
	return GMIME_MULTIPART_GET_CLASS (multipart)->get_count (multipart);
}
Exemplo n.º 9
0
/**
 * g_mime_multipart_get_boundary:
 * @multipart: a #GMimeMultipart object
 *
 * Gets the boundary on the multipart. If the internal boundary is
 * %NULL, then an auto-generated boundary will be set on the multipart
 * and returned.
 *
 * Returns: the boundary on the multipart.
 **/
const char *
g_mime_multipart_get_boundary (GMimeMultipart *multipart)
{
	g_return_val_if_fail (GMIME_IS_MULTIPART (multipart), NULL);
	
	return GMIME_MULTIPART_GET_CLASS (multipart)->get_boundary (multipart);
}
Exemplo n.º 10
0
/**
 * g_mime_multipart_get_postface:
 * @multipart: a #GMimeMultipart object
 *
 * Gets the postface on the multipart.
 *
 * Returns: a pointer to the postface string on the multipart.
 **/
const char *
g_mime_multipart_get_postface (GMimeMultipart *multipart)
{
	g_return_val_if_fail (GMIME_IS_MULTIPART (multipart), NULL);
	
	return multipart->postface;
}
Exemplo n.º 11
0
static void process_message_callback(GMimeObject *part, gpointer user_data)
{
	struct mime_cbinfo *cbinfo = user_data;

	cbinfo->count++;

	/* We strip off the headers before we get here, so should only see GMIME_IS_PART */
	if (GMIME_IS_MESSAGE_PART(part)) {
		ast_log(LOG_WARNING, "Got unexpected GMIME_IS_MESSAGE_PART\n");
		return;
	} else if (GMIME_IS_MESSAGE_PARTIAL(part)) {
		ast_log(LOG_WARNING, "Got unexpected GMIME_IS_MESSAGE_PARTIAL\n");
		return;
	} else if (GMIME_IS_MULTIPART(part)) {
		GList *l;
		
		ast_log(LOG_WARNING, "Got unexpected GMIME_IS_MULTIPART, trying to process subparts\n");
		l = GMIME_MULTIPART(part)->subparts;
		while (l) {
			process_message_callback(l->data, cbinfo);
			l = l->next;
		}
	} else if (GMIME_IS_PART(part)) {
		const char *filename;

		if (ast_strlen_zero(filename = g_mime_part_get_filename(GMIME_PART(part)))) {
			ast_debug(1, "Skipping part with no filename\n");
			return;
		}

		post_raw(GMIME_PART(part), cbinfo->post_dir, filename);
	} else {
		ast_log(LOG_ERROR, "Encountered unknown MIME part. This should never happen!\n");
	}
}
Exemplo n.º 12
0
/**
 * g_mime_multipart_contains:
 * @multipart: a #GMimeMultipart object
 * @part: a #GMimeObject
 *
 * Checks if @part is contained within @multipart.
 *
 * Returns: %TRUE if @part is a subpart of @multipart or %FALSE
 * otherwise.
 **/
gboolean
g_mime_multipart_contains (GMimeMultipart *multipart, GMimeObject *part)
{
	g_return_val_if_fail (GMIME_IS_MULTIPART (multipart), FALSE);
	g_return_val_if_fail (GMIME_IS_OBJECT (part), FALSE);
	
	return GMIME_MULTIPART_GET_CLASS (multipart)->contains (multipart, part);
}
Exemplo n.º 13
0
/**
 * g_mime_multipart_get_part:
 * @multipart: a #GMimeMultipart object
 * @index: the 0-based index of the part
 *
 * Gets the part at the specified @index within the multipart.
 *
 * Returns: (transfer none): the part at position @index.
 **/
GMimeObject *
g_mime_multipart_get_part (GMimeMultipart *multipart, int index)
{
	g_return_val_if_fail (GMIME_IS_MULTIPART (multipart), NULL);
	g_return_val_if_fail (index >= 0, NULL);
	
	return GMIME_MULTIPART_GET_CLASS (multipart)->get_part (multipart, index);
}
Exemplo n.º 14
0
/**
 * g_mime_multipart_add:
 * @multipart: a #GMimeMultipart object
 * @part: a #GMimeObject
 *
 * Appends a mime part to @multipart.
 **/
void
g_mime_multipart_add (GMimeMultipart *multipart, GMimeObject *part)
{
	g_return_if_fail (GMIME_IS_MULTIPART (multipart));
	g_return_if_fail (GMIME_IS_OBJECT (part));
	
	GMIME_MULTIPART_GET_CLASS (multipart)->add (multipart, part);
}
Exemplo n.º 15
0
/**
 * g_mime_multipart_set_postface:
 * @multipart: a #GMimeMultipart object
 * @postface: postface
 *
 * Sets the postface on the multipart.
 **/
void
g_mime_multipart_set_postface (GMimeMultipart *multipart, const char *postface)
{
	g_return_if_fail (GMIME_IS_MULTIPART (multipart));
	
	g_free (multipart->postface);
	multipart->postface = g_strdup (postface);
}
Exemplo n.º 16
0
/**
 * g_mime_multipart_foreach: 
 * @multipart: a #GMimeMultipart
 * @callback: (scope call): function to call for each of @multipart's
 *   subparts.
 * @user_data: user-supplied callback data
 * 
 * Recursively calls @callback on each of @multipart's subparts.
 **/
void
g_mime_multipart_foreach (GMimeMultipart *multipart, GMimeObjectForeachFunc callback, gpointer user_data)
{
	g_return_if_fail (GMIME_IS_MULTIPART (multipart));
	g_return_if_fail (callback != NULL);
	
	multipart_foreach (multipart, callback, user_data);
}
Exemplo n.º 17
0
/**
 * g_mime_multipart_index_of:
 * @multipart: a #GMimeMultipart object
 * @part: a #GMimeObject
 *
 * Gets the 0-based index of @part within @multipart.
 *
 * Returns: the 0-based index of @part within @multipart or %-1 if not found.
 **/
int
g_mime_multipart_index_of (GMimeMultipart *multipart, GMimeObject *part)
{
	g_return_val_if_fail (GMIME_IS_MULTIPART (multipart), -1);
	g_return_val_if_fail (GMIME_IS_OBJECT (part), -1);
	
	return GMIME_MULTIPART_GET_CLASS (multipart)->index_of (multipart, part);
}
Exemplo n.º 18
0
/**
 * g_mime_multipart_insert:
 * @multipart: a #GMimeMultipart object
 * @part: a #GMimeObject
 * @index: the 0-based index to insert the part
 *
 * Inserts @part into @multipart at the specified @index.
 **/
void
g_mime_multipart_insert (GMimeMultipart *multipart, int index, GMimeObject *part)
{
	g_return_if_fail (GMIME_IS_MULTIPART (multipart));
	g_return_if_fail (GMIME_IS_OBJECT (part));
	g_return_if_fail (index >= 0);
	
	GMIME_MULTIPART_GET_CLASS (multipart)->insert (multipart, index, part);
}
Exemplo n.º 19
0
void remove_message(GMimeMessage *message) {
  GMimeObject *part;

  part = g_mime_message_get_mime_part(message);

  if (GMIME_IS_MULTIPART(part))
    remove_multipart((GMimeMultipart*)part);
  else
    remove_part((GMimeObject*)message, part);

}
Exemplo n.º 20
0
static void
multipart_foreach (GMimeMultipart *multipart, GMimeObjectForeachFunc callback, gpointer user_data)
{
	GMimeObject *part;
	guint i;
	
	for (i = 0; i < multipart->children->len; i++) {
		part = (GMimeObject *) multipart->children->pdata[i];
		callback ((GMimeObject *) multipart, part, user_data);
		
		if (GMIME_IS_MULTIPART (part))
			multipart_foreach ((GMimeMultipart *) part, callback, user_data);
	}
}
Exemplo n.º 21
0
static GMimeObject* add_part_to_existing(GMimeObject* mp, GMimeObject* p)
{
    GMimeObject* rv = mp;
    /* there was a previous part, switch to multipart or just add if we already switched */
    /* g_debug("mp=%p (%s), p=%p (%s)", mp, G_OBJECT_TYPE_NAME(mp), p, G_OBJECT_TYPE_NAME(p)); */
    if ( GMIME_IS_MULTIPART(mp) ) {
        g_mime_multipart_add(GMIME_MULTIPART(mp), p);
    } else {
        GMimeMultipart* mlp = g_mime_multipart_new();

        g_mime_multipart_add(mlp, mp);
        g_mime_multipart_add(mlp, p);
        rv = GMIME_OBJECT(mlp);
    }

    return rv;
}
Exemplo n.º 22
0
/**
 * g_mime_multipart_replace:
 * @multipart: a #GMimeMultipart object
 * @index: the 0-based index of the part to replace
 * @replacement: a #GMimeObject to use as the replacement
 *
 * Replaces the part at the specified @index within @multipart with
 * @replacement.
 *
 * Returns: (transfer full): the part that was replaced or %NULL
 * if the part was not contained within the multipart.
 **/
GMimeObject *
g_mime_multipart_replace (GMimeMultipart *multipart, int index, GMimeObject *replacement)
{
	GMimeObject *replaced;
	
	g_return_val_if_fail (GMIME_IS_MULTIPART (multipart), NULL);
	g_return_val_if_fail (GMIME_IS_OBJECT (replacement), NULL);
	g_return_val_if_fail (index >= 0, NULL);
	
	if ((guint) index >= multipart->children->len)
		return NULL;
	
	replaced = multipart->children->pdata[index];
	multipart->children->pdata[index] = replacement;
	g_object_ref (replacement);
	
	return replaced;
}
Exemplo n.º 23
0
static void
print_body (GMimeMessage *msg)
{
	GMimeObject		*body;
	GMimeDataWrapper	*wrapper;
	GMimeStream		*stream;

	body = g_mime_message_get_body (msg);

	if (GMIME_IS_MULTIPART(body))
		body = g_mime_multipart_get_part (GMIME_MULTIPART(body), 0);

	if (!GMIME_IS_PART(body))
		return;

	wrapper = g_mime_part_get_content_object (GMIME_PART(body));
	if (!GMIME_IS_DATA_WRAPPER(wrapper))
		return;

	stream = g_mime_data_wrapper_get_stream (wrapper);
	if (!GMIME_IS_STREAM(stream))
		return;

	do {
		char	buf[512];
		ssize_t	len;

		len = g_mime_stream_read (stream, buf, sizeof(buf));
		if (len == -1)
			break;

		if (write (fileno(stdout), buf, len) == -1)
			break;

		if (len < (int)sizeof(buf))
			break;

	} while (1);
}
Exemplo n.º 24
0
int remove_part(GMimeObject *parent, GMimeObject *part) {
  int attachment = is_attachment(part);

  if (attachment && GMIME_IS_MULTIPART(parent)) {
    g_mime_multipart_remove((GMimeMultipart*)parent, part);
    return 1;
  }
  else if (attachment && GMIME_IS_MESSAGE(parent)) { // I'm not sure if this could ever occur or even makes sense.
    GMimePart *new_part;
    new_part = g_mime_part_new_with_type("text", "plain");
    g_mime_part_set_content_object(new_part,
				   g_mime_data_wrapper_new_with_stream(g_mime_stream_mem_new_with_buffer("Attachment Removed.", 19),
								       GMIME_CONTENT_ENCODING_DEFAULT));
    g_mime_message_set_mime_part((GMimeMessage*)parent, (GMimeObject*)new_part);
    g_object_unref(part);
  }
  else if (GMIME_IS_MESSAGE_PART(part)) {
    remove_message(g_mime_message_part_get_message((GMimeMessagePart*)part));
  }

  return 0;
}
Exemplo n.º 25
0
static void
mime_foreach_callback (GMimeObject * parent,
	GMimeObject * part,
	gpointer user_data)
{
	GMimeContentType *type;

	if (GMIME_IS_MESSAGE_PART (part)) {
		/* message/rfc822 or message/news */
		GMimeMessage *message;

		/* g_mime_message_foreach_part() won't descend into
			   child message parts, so if we want to count any
			   subparts of this child message, we'll have to call
			   g_mime_message_foreach_part() again here. */
		rspamd_printf ("got message part %p: parent: %p\n",
						part, parent);
		message = g_mime_message_part_get_message ((GMimeMessagePart *) part);
		g_mime_message_foreach (message, mime_foreach_callback, part);
	}
	else if (GMIME_IS_MULTIPART (part)) {
		type = (GMimeContentType *) g_mime_object_get_content_type (GMIME_OBJECT (
						part));
		rspamd_printf ("got multipart part %p, boundary: %s: parent: %p, type: %s/%s\n",
					part, g_mime_multipart_get_boundary (GMIME_MULTIPART(part)),
					parent,
					g_mime_content_type_get_media_type (type),
					g_mime_content_type_get_media_subtype (type));
	}
	else {
		type = (GMimeContentType *) g_mime_object_get_content_type (GMIME_OBJECT (
				part));
		rspamd_printf ("got normal part %p, parent: %p, type: %s/%s\n",
				part,
				parent,
				g_mime_content_type_get_media_type (type),
				g_mime_content_type_get_media_subtype (type));
	}
}
Exemplo n.º 26
0
static void
print_mime_struct (GMimeStream *stream, GMimeObject *part, int depth)
{
	GMimeMultipart *multipart;
	GMimeMessagePart *mpart;
	GMimeContentType *type;
	GMimeObject *subpart;
	GMimeObject *body;
	GMimeMessage *msg;
	int i, n;
	
	print_depth (stream, depth);
	
	type = g_mime_object_get_content_type (part);
	
	g_mime_stream_printf (stream, "Content-Type: %s/%s\n",
			      g_mime_content_type_get_media_type (type),
			      g_mime_content_type_get_media_subtype (type));
	
	if (GMIME_IS_MULTIPART (part)) {
		multipart = (GMimeMultipart *) part;
		
		n = g_mime_multipart_get_count (multipart);
		for (i = 0; i < n; i++) {
			subpart = g_mime_multipart_get_part (multipart, i);
			print_mime_struct (stream, subpart, depth + 1);
		}
	} else if (GMIME_IS_MESSAGE_PART (part)) {
		mpart = (GMimeMessagePart *) part;
		msg = g_mime_message_part_get_message (mpart);
		
		if (msg != NULL) {
			body = g_mime_message_get_mime_part (msg);
			
			print_mime_struct (stream, body, depth + 1);
		}
	}
}
Exemplo n.º 27
0
static void
write_part(GMimeObject *part)
{
	GList *l;
	const GMimeContentType *ct;
	ct = g_mime_object_get_content_type(GMIME_OBJECT(part));
	
	if (GMIME_IS_MULTIPART(part)) {
		if (g_mime_content_type_is_type(ct, "multipart", "alternative")) {
			choose_alternative(part);
			level += g_list_length(GMIME_MULTIPART(part)->subparts);
		} else {
			l = GMIME_MULTIPART(part)->subparts;
			while (l != NULL) {
				write_part(l->data);
				l = l->next;
				level++;
			}
		}
	} else if (GMIME_IS_MESSAGE_PART(part)) {
	} else if (GMIME_IS_PART(part)) {
		display_part(part, ct);
	}
}
Exemplo n.º 28
0
static void
count_foreach_callback (GMimeObject *parent, GMimeObject *part, gpointer user_data)
{
    int *count = user_data;
    (*count)++;

    /* 'part' points to the current part node that
     * g_mime_message_foreach() is iterating over */

    /* find out what class 'part' is... */
    if (GMIME_IS_MESSAGE_PART (part))
    {
        /* message/rfc822 or message/news */
        GMimeMessage *message;

        /* g_mime_message_foreach() won't descend into
                   child message parts, so if we want to count any
                   subparts of this child message, we'll have to call
                   g_mime_message_foreach() again here. */
        printf("\n===============descend into subparts=================\n");
        message = g_mime_message_part_get_message ((GMimeMessagePart *) part);
        g_mime_message_foreach (message, count_foreach_callback, count);
    }
    else if (GMIME_IS_MESSAGE_PARTIAL (part))
    {
        /* message/partial */

        /* this is an incomplete message part, probably a
                   large message that the sender has broken into
                   smaller parts and is sending us bit by bit. we
                   could save some info about it so that we could
                   piece this back together again once we get all the
                   parts? */
        printf("\n===============partial=================\n");
    }
    else if (GMIME_IS_MULTIPART (part))
    {
        /* multipart/mixed, multipart/alternative,
         * multipart/related, multipart/signed,
         * multipart/encrypted, etc... */

        /* we'll get to finding out if this is a
         * signed/encrypted multipart later... */
        printf("\n===============multipart=================\n");
    }
    else if (GMIME_IS_PART (part))
    {
        /* a normal leaf part, could be text/plain or
         * image/jpeg etc */
        printf("\n===============normal leaf part=================\n");
        printf("====>>>>====>>>>====>>>>Decode start here<<<<====<<<<====<<<<====\n");
        {
#if 0
            printf("\ndecode this normal leaf part================\n" );
            //GMimeStream *stream = g_mime_stream_file_new (stdout);
            //GMimeDataWrapper * content=g_mime_part_get_content_object(part);
            //g_mime_data_wrapper_write_to_stream(content,stream);
#endif
        }
        AnalyseMessageParse((GMimePart*)part);
    }
    else
    {
        printf("\n===============descend not reached=================\n");
        g_assert_not_reached ();
    }
}
Exemplo n.º 29
0
void
format_part_json (const void *ctx, mime_node_t *node, notmuch_bool_t first)
{
    /* Any changes to the JSON format should be reflected in the file
     * devel/schemata. */

    if (node->envelope_file) {
        printf ("{");
        format_message_json (ctx, node->envelope_file);

        printf ("\"headers\": ");
        format_headers_json (ctx, GMIME_MESSAGE (node->part), FALSE);

        printf (", \"body\": [");
        format_part_json (ctx, mime_node_child (node, 0), first);

        printf ("]}");
        return;
    }

    void *local = talloc_new (ctx);
    /* The disposition and content-type metadata are associated with
     * the envelope for message parts */
    GMimeObject *meta = node->envelope_part ?
                        GMIME_OBJECT (node->envelope_part) : node->part;
    GMimeContentType *content_type = g_mime_object_get_content_type (meta);
    const char *cid = g_mime_object_get_content_id (meta);
    const char *filename = GMIME_IS_PART (node->part) ?
                           g_mime_part_get_filename (GMIME_PART (node->part)) : NULL;
    const char *terminator = "";
    int i;

    if (!first)
        printf (", ");

    printf ("{\"id\": %d", node->part_num);

    if (node->decrypt_attempted)
        printf (", \"encstatus\": [{\"status\": \"%s\"}]",
                node->decrypt_success ? "good" : "bad");

    if (node->verify_attempted) {
        printf (", \"sigstatus\": ");
        format_part_sigstatus_json (node);
    }

    printf (", \"content-type\": %s",
            json_quote_str (local, g_mime_content_type_to_string (content_type)));

    if (cid)
        printf (", \"content-id\": %s", json_quote_str (local, cid));

    if (filename)
        printf (", \"filename\": %s", json_quote_str (local, filename));

    if (GMIME_IS_PART (node->part)) {
        /* For non-HTML text parts, we include the content in the
         * JSON. Since JSON must be Unicode, we handle charset
         * decoding here and do not report a charset to the caller.
         * For text/html parts, we do not include the content. If a
         * caller is interested in text/html parts, it should retrieve
         * them separately and they will not be decoded. Since this
         * makes charset decoding the responsibility on the caller, we
         * report the charset for text/html parts.
         */
        if (g_mime_content_type_is_type (content_type, "text", "html")) {
            const char *content_charset = g_mime_object_get_content_type_parameter (meta, "charset");

            if (content_charset != NULL)
                printf (", \"content-charset\": %s", json_quote_str (local, content_charset));
        } else if (g_mime_content_type_is_type (content_type, "text", "*")) {
            GMimeStream *stream_memory = g_mime_stream_mem_new ();
            GByteArray *part_content;
            show_text_part_content (node->part, stream_memory, 0);
            part_content = g_mime_stream_mem_get_byte_array (GMIME_STREAM_MEM (stream_memory));

            printf (", \"content\": %s", json_quote_chararray (local, (char *) part_content->data, part_content->len));
            g_object_unref (stream_memory);
        }
    } else if (GMIME_IS_MULTIPART (node->part)) {
        printf (", \"content\": [");
        terminator = "]";
    } else if (GMIME_IS_MESSAGE (node->part)) {
        printf (", \"content\": [{");
        printf ("\"headers\": ");
        format_headers_json (local, GMIME_MESSAGE (node->part), FALSE);

        printf (", \"body\": [");
        terminator = "]}]";
    }

    talloc_free (local);

    for (i = 0; i < node->nchildren; i++)
        format_part_json (ctx, mime_node_child (node, i), i == 0);

    printf ("%s}", terminator);
}
Exemplo n.º 30
0
void
format_part_sprinter (const void *ctx, sprinter_t *sp, mime_node_t *node,
		      notmuch_bool_t first, notmuch_bool_t output_body,
		      notmuch_bool_t include_html)
{
    /* Any changes to the JSON or S-Expression format should be
     * reflected in the file devel/schemata. */

    if (node->envelope_file) {
	sp->begin_map (sp);
	format_message_sprinter (sp, node->envelope_file);

	sp->map_key (sp, "headers");
	format_headers_sprinter (sp, GMIME_MESSAGE (node->part), FALSE);

	if (output_body) {
	    sp->map_key (sp, "body");
	    sp->begin_list (sp);
	    format_part_sprinter (ctx, sp, mime_node_child (node, 0), first, TRUE, include_html);
	    sp->end (sp);
	}
	sp->end (sp);
	return;
    }

    /* The disposition and content-type metadata are associated with
     * the envelope for message parts */
    GMimeObject *meta = node->envelope_part ?
	GMIME_OBJECT (node->envelope_part) : node->part;
    GMimeContentType *content_type = g_mime_object_get_content_type (meta);
    const char *cid = g_mime_object_get_content_id (meta);
    const char *filename = GMIME_IS_PART (node->part) ?
	g_mime_part_get_filename (GMIME_PART (node->part)) : NULL;
    int nclose = 0;
    int i;

    sp->begin_map (sp);

    sp->map_key (sp, "id");
    sp->integer (sp, node->part_num);

    if (node->decrypt_attempted) {
	sp->map_key (sp, "encstatus");
	sp->begin_list (sp);
	sp->begin_map (sp);
	sp->map_key (sp, "status");
	sp->string (sp, node->decrypt_success ? "good" : "bad");
	sp->end (sp);
	sp->end (sp);
    }

    if (node->verify_attempted) {
	sp->map_key (sp, "sigstatus");
	format_part_sigstatus_sprinter (sp, node);
    }

    sp->map_key (sp, "content-type");
    sp->string (sp, g_mime_content_type_to_string (content_type));

    if (cid) {
	sp->map_key (sp, "content-id");
	sp->string (sp, cid);
    }

    if (filename) {
	sp->map_key (sp, "filename");
	sp->string (sp, filename);
    }

    if (GMIME_IS_PART (node->part)) {
	/* For non-HTML text parts, we include the content in the
	 * JSON. Since JSON must be Unicode, we handle charset
	 * decoding here and do not report a charset to the caller.
	 * For text/html parts, we do not include the content unless
	 * the --include-html option has been passed. If a html part
	 * is not included, it can be requested directly. This makes
	 * charset decoding the responsibility on the caller so we
	 * report the charset for text/html parts.
	 */
	if (g_mime_content_type_is_type (content_type, "text", "*") &&
	    (include_html ||
	     ! g_mime_content_type_is_type (content_type, "text", "html")))
	{
	    GMimeStream *stream_memory = g_mime_stream_mem_new ();
	    GByteArray *part_content;
	    show_text_part_content (node->part, stream_memory, 0);
	    part_content = g_mime_stream_mem_get_byte_array (GMIME_STREAM_MEM (stream_memory));
	    sp->map_key (sp, "content");
	    sp->string_len (sp, (char *) part_content->data, part_content->len);
	    g_object_unref (stream_memory);
	} else {
	    format_omitted_part_meta_sprinter (sp, meta, GMIME_PART (node->part));
	}
    } else if (GMIME_IS_MULTIPART (node->part)) {
	sp->map_key (sp, "content");
	sp->begin_list (sp);
	nclose = 1;
    } else if (GMIME_IS_MESSAGE (node->part)) {
	sp->map_key (sp, "content");
	sp->begin_list (sp);
	sp->begin_map (sp);

	sp->map_key (sp, "headers");
	format_headers_sprinter (sp, GMIME_MESSAGE (node->part), FALSE);

	sp->map_key (sp, "body");
	sp->begin_list (sp);
	nclose = 3;
    }

    for (i = 0; i < node->nchildren; i++)
	format_part_sprinter (ctx, sp, mime_node_child (node, i), i == 0, TRUE, include_html);

    /* Close content structures */
    for (i = 0; i < nclose; i++)
	sp->end (sp);
    /* Close part map */
    sp->end (sp);
}