Пример #1
0
int main (int argc, char* argv[]) {
  if (argc < 2) {
    fprintf(stderr, "Usage: %s MSG_FILE\n", argv[0]);
    return 1;
  }

  g_mime_init(0);

  FILE* fp = fopen(argv[1], "r");
  if (fp == NULL) {
    fprintf(stderr, "Cannot open the file %s\n", argv[1]);
    return 1;
  }

  GMimeStream* stream = g_mime_stream_file_new(fp);
  GMimeParser* parser = g_mime_parser_new_with_stream(stream);
  GMimeMessage* message = g_mime_parser_construct_message(parser);

  if (message == NULL) {
    fprintf(stderr, "Cannot construct message\n");
    return 1;
  }

  printf("Date    : %s\n", g_mime_message_get_date_as_string(message));
  printf("Subject : %s\n", g_mime_message_get_subject(message));
  printf("From    : %s\n", g_mime_message_get_sender(message));
  printf("To      : %s\n", internet_address_list_to_string(
        g_mime_message_get_all_recipients(message), FALSE));

  fclose(fp);
  return 0;
}
Пример #2
0
MNMessage *
mn_message_new_from_mime_stream_full (GType type,
				      MNMailbox *mailbox,
				      GMimeStream *mime_stream,
				      const char *mid,
				      const char *uri,
				      MNMessageFlags flags,
				      gboolean handle_status,
				      GError **err)
{
  GMimeParser *parser;
  GMimeMessage *mime_message;
  MNMessage *message = NULL;

  g_return_val_if_fail(type != 0, NULL);
  g_return_val_if_fail(MN_IS_MAILBOX(mailbox), NULL);
  g_return_val_if_fail(GMIME_IS_STREAM(mime_stream), NULL);

  parser = g_mime_parser_new_with_stream(mime_stream);
  mime_message = g_mime_parser_construct_message(parser);
  g_object_unref(parser);

  if (mime_message)
    {
      message = mn_message_new_from_mime_message_full(type, mailbox, mime_message, mid, uri, flags, handle_status);
      g_object_unref(mime_message);
    }
  else
    g_set_error(err, 0, 0, _("unable to parse MIME message"));

  return message;
}
Пример #3
0
void sipe_mime_parts_foreach(const gchar *type,
			     const gchar *body,
			     sipe_mime_parts_cb callback,
			     gpointer user_data)
{
	gchar *doc = g_strdup_printf("Content-Type: %s\r\n\r\n%s", type, body);
	GMimeStream *stream = g_mime_stream_mem_new_with_buffer(doc, strlen(doc));

	if (stream) {
		GMimeParser *parser = g_mime_parser_new_with_stream(stream);
		GMimeMultipart *multipart = (GMimeMultipart *)g_mime_parser_construct_part(parser);

		if (multipart) {
			struct gmime_callback_data cd = {callback, user_data};

			SIPE_DEBUG_INFO("sipe_mime_parts_foreach: %d parts", g_mime_multipart_get_count(multipart));

			g_mime_multipart_foreach(multipart, gmime_callback, &cd);
			g_object_unref(multipart);
		}

		g_object_unref(parser);
		g_object_unref(stream);
	}
	g_free(doc);
}
Пример #4
0
int main(int argc, char **argv) {
  GByteArray *garray;
  GMimeStream *stream;
  GMimeParser *parse;
  GMimeMessage *message;
  char buf[1];
  int len;

  g_mime_init(0);

  garray = g_byte_array_new();
  
  while (!feof(stdin)) {
    len = fread(buf, 1, 1, stdin);
    if (len == 1)
      g_byte_array_append(garray, buf, len);
  }

  stream = g_mime_stream_mem_new_with_byte_array(garray);
  parse = g_mime_parser_new_with_stream(stream);
  message = g_mime_parser_construct_message(parse);

  remove_message(message);

  printf(g_mime_object_to_string((GMimeObject*)message));
  
  g_object_unref(message);
  g_object_unref(parse);
  g_object_unref(stream);

  return 0;
}
Пример #5
0
int main (int argc, const char* argv[])
{
	GMimeMessage *message;
	GMimeParser  *parser;
	GMimeStream  *stream;
	const char   *file = argv[1];

	int fd;

	if (argc < 2) {
		printf("No file name given\n");
		return 1;
	}
	
	if (argc == 3) {
		text_only = 1;
		file = argv[2];
	}
	
	g_mime_init(0);
	g_mime_iconv_init();
	
	if ((fd = open(file, O_RDONLY)) == -1) {
		printf("Error opening file %s\n", file);
		return 1;
	}
	
	stream = g_mime_stream_fs_new(fd);
	parser = g_mime_parser_new_with_stream(stream);
	
	g_mime_parser_set_scan_from(parser, FALSE);
	g_object_unref(stream);
	
	message = g_mime_parser_construct_message(parser);
	g_object_unref(parser);
	
	if (message) {
		write_part(message->mime_part);
	} else {
		printf("Error constructing message\n");
		return 1;
	}
	
	g_mime_iconv_shutdown();
	return 0;
}
Пример #6
0
static GMimeMessage* gmime_message_from_stream(GMimeStream *stream) {
  g_return_val_if_fail(stream != NULL, NULL);

  GMimeParser *parser = g_mime_parser_new_with_stream(stream);
  if (!parser) {
    g_printerr("failed to create parser\r\n");
    return NULL;
  }

  GMimeMessage *message = g_mime_parser_construct_message(parser);
  g_object_unref (parser);
  if (!message) {
    g_printerr("failed to construct message\r\n");
    return NULL;
  }

  return message;
}
Пример #7
0
static GMimeMessage *parse_message(FILE *f)
{
	GMimeMessage *message;
	GMimeParser *parser;
	GMimeStream *stream;

	stream = g_mime_stream_file_new(f);

	parser = g_mime_parser_new_with_stream(stream);
	g_mime_parser_set_respect_content_length(parser, 1);
	
	g_object_unref(stream);

	message = g_mime_parser_construct_message(parser);

	g_object_unref(parser);

	return message;
}
Пример #8
0
static gboolean
test_stream (GMimeStream *stream, GMimeStream *out_stream)
{
  GMimeParser *parser;
  GMimeMessage *msg;
  gboolean rv;

  parser = NULL;
  msg    = NULL;

  parser = g_mime_parser_new_with_stream (stream);
  if (!parser) {
    g_warning ("failed to create parser");
    rv = FALSE;
    goto leave;
  }


  msg = g_mime_parser_construct_message (parser);
  if (!msg) {
    g_warning ("failed to construct message");
    rv = FALSE;
    goto leave;
  }

  extract_headers(msg, out_stream);

 leave:
  if (parser)
    g_object_unref (parser);
  else
    g_object_unref (stream);

  if (msg)
    g_object_unref (msg);

  return rv;
}
Пример #9
0
/**
 * Output a list of the parts included in the current message.
 */
void dump_mail( char *filename )
{
    std::string result;

    GMimeMessage *m_message;
    GMimeParser *parser;
    GMimeStream *stream;
    int fd;

    std::cout << "File: " << filename << std::endl;

    if ((fd = open( filename, O_RDONLY, 0)) == -1)
    {
        std::cout << "Opening failed ..." << std::endl;
        return;
    }

    stream = g_mime_stream_fs_new (fd);

    parser = g_mime_parser_new_with_stream (stream);
    g_object_unref (stream);

    m_message = g_mime_parser_construct_message (parser);
    g_object_unref (parser);

    /**
     * Create an iterator
     */
    GMimePartIter *iter =  g_mime_part_iter_new ((GMimeObject *) m_message);
    int count = 1;

    /**
     * Iterate over the message.
     */
    do
    {
        GMimeObject *part  = g_mime_part_iter_get_current (iter);

        if ( ( GMIME_IS_OBJECT( part ) ) &&
             ( GMIME_IS_PART(part) ) )
        {
            /**
             * Get the content-type
             */
            GMimeContentType *content_type = g_mime_object_get_content_type (part);
            const char *filename = g_mime_object_get_content_disposition_parameter(part, "filename");
            gchar *type = g_mime_content_type_to_string ( content_type );
            std::cout << count << " " << type << std::endl;
            if ( filename )
                std::cout << "\t[This is an attachment with file-name " << filename << "]" << std::endl;
        }


        /**
         * Highlight inline parts - which are not attachments.
         */
        GMimeContentDisposition *disp = g_mime_object_get_content_disposition(part);
        if ( disp != NULL )
        {
            const char *str = g_mime_content_disposition_get_disposition (disp);
            std::cout << "\tNOTE: Content-Disposition: " << str << std::endl;

        }

       count += 1;
    }
    while (g_mime_part_iter_next (iter));

    g_mime_part_iter_free (iter);
    g_object_unref(m_message);
}
Пример #10
0
int main (int argc, char **argv)
{
	const char *datadir = "data/mbox";
	char input[256], output[256], *tmp, *p, *q;
	GMimeStream *istream, *ostream, *mstream, *pstream;
	GMimeParser *parser;
	const char *dent;
	const char *path;
	struct stat st;
	GDir *dir;
	int i;
#ifdef ENABLE_MBOX_MATCH
	int fd;

	if (mkdir ("./tmp", 0755) == -1 && errno != EEXIST)
		return 0;
#endif
	
	g_mime_init ();
	
	testsuite_init (argc, argv);
	
	path = datadir;
	for (i = 1; i < argc; i++) {
		if (argv[i][0] != '-') {
			path = argv[i];
			break;
		}
	}
	
	testsuite_start ("Mbox parser");
	
	if (stat (path, &st) == -1)
		goto exit;
	
	if (S_ISDIR (st.st_mode)) {
		/* automated testsuite */
		p = g_stpcpy (input, path);
		*p++ = G_DIR_SEPARATOR;
		p = g_stpcpy (p, "input");
		
		if (!(dir = g_dir_open (input, 0, NULL)))
			goto exit;
		
		*p++ = G_DIR_SEPARATOR;
		*p = '\0';
		
		q = g_stpcpy (output, path);
		*q++ = G_DIR_SEPARATOR;
		q = g_stpcpy (q, "output");
		*q++ = G_DIR_SEPARATOR;
		*q = '\0';
		
		while ((dent = g_dir_read_name (dir))) {
			if (!g_str_has_suffix (dent, ".mbox"))
				continue;
			
			strcpy (p, dent);
			strcpy (q, dent);
			
			tmp = NULL;
			parser = NULL;
			istream = NULL;
			ostream = NULL;
			mstream = NULL;
			pstream = NULL;
			
			testsuite_check ("%s", dent);
			try {
				if (!(istream = g_mime_stream_fs_open (input, O_RDONLY, 0, NULL))) {
					throw (exception_new ("could not open `%s': %s",
							      input, g_strerror (errno)));
				}
				
				if (!(ostream = g_mime_stream_fs_open (output, O_RDONLY, 0, NULL))) {
					throw (exception_new ("could not open `%s': %s",
							      output, g_strerror (errno)));
				}
				
#ifdef ENABLE_MBOX_MATCH
				tmp = g_strdup_printf ("./tmp/%s", dent);
				if ((fd = open (tmp, O_CREAT | O_RDWR | O_TRUNC, 0644)) == -1) {
					throw (exception_new ("could not open `%s': %s",
							      tmp, g_strerror (errno)));
				}
				
				mstream = g_mime_stream_fs_new (fd);
#endif
				
				parser = g_mime_parser_new_with_stream (istream);
				g_mime_parser_set_persist_stream (parser, TRUE);
				g_mime_parser_set_format (parser, GMIME_FORMAT_MBOX);
				
				if (!g_mime_parser_get_persist_stream (parser))
					throw (exception_new ("persist stream check failed"));
				
				if (g_mime_parser_get_format (parser) != GMIME_FORMAT_MBOX)
					throw (exception_new ("format check failed"));
				
				if (strstr (dent, "content-length") != NULL) {
					g_mime_parser_set_respect_content_length (parser, TRUE);
					
					if (!g_mime_parser_get_respect_content_length (parser))
						throw (exception_new ("respect content-length check failed"));
				} else {
					g_mime_parser_set_respect_content_length (parser, FALSE);
					
					if (g_mime_parser_get_respect_content_length (parser))
						throw (exception_new ("respect content-length check failed"));
				}
				
				g_mime_parser_set_header_regex (parser, "^X-Evolution", xevcb, NULL);
				
				pstream = g_mime_stream_mem_new ();
				test_parser (parser, mstream, pstream);
				
#ifdef ENABLE_MBOX_MATCH
				g_mime_stream_flush (mstream);
				g_mime_stream_reset (istream);
				g_mime_stream_reset (mstream);
				if (!streams_match (istream, mstream))
					throw (exception_new ("mboxes do not match for `%s'", dent));
#endif
				
				g_mime_stream_reset (ostream);
				g_mime_stream_reset (pstream);
				if (!streams_match (ostream, pstream))
					throw (exception_new ("summaries do not match for `%s'", dent));
				
				testsuite_check_passed ();
				
#ifdef ENABLE_MBOX_MATCH
				unlink (tmp);
#endif
			} catch (ex) {
				if (parser != NULL)
					testsuite_check_failed ("%s: %s", dent, ex->message);
				else
					testsuite_check_warn ("%s: %s", dent, ex->message);
			} finally;
			
			if (mstream != NULL)
				g_object_unref (mstream);
			
			if (pstream != NULL)
				g_object_unref (pstream);
			
			if (istream != NULL)
				g_object_unref (istream);
			
			if (ostream != NULL)
				g_object_unref (ostream);
			
			if (parser != NULL)
				g_object_unref (parser);
			
			g_free (tmp);
		}
		
		g_dir_close (dir);
	} else if (S_ISREG (st.st_mode)) {
		/* manually run test on a single file */
		if (!(istream = g_mime_stream_fs_open (path, O_RDONLY, 0, NULL)))
			goto exit;
		
		parser = g_mime_parser_new_with_stream (istream);
		g_mime_parser_set_format (parser, GMIME_FORMAT_MBOX);
		
#ifdef ENABLE_MBOX_MATCH
		tmp = g_strdup ("./tmp/mbox-test.XXXXXX");
		if ((fd = g_mkstemp (tmp)) == -1) {
			g_object_unref (istream);
			g_object_unref (parser);
			g_free (tmp);
			goto exit;
		}
		
		mstream = g_mime_stream_fs_new (fd);
#else
		mstream = NULL;
#endif
		
		ostream = g_mime_stream_file_new (stdout);
		g_mime_stream_file_set_owner ((GMimeStreamFile *) ostream, FALSE);
		
		testsuite_check ("user-input mbox: `%s'", path);
		try {
			test_parser (parser, mstream, ostream);
			
#ifdef ENABLE_MBOX_MATCH
			g_mime_stream_reset (istream);
			g_mime_stream_reset (mstream);
			if (!streams_match (istream, mstream))
				throw (exception_new ("`%s' does not match `%s'", tmp, path));
			
			unlink (tmp);
#endif
			
			testsuite_check_passed ();
		} catch (ex) {
			testsuite_check_failed ("user-input mbox `%s': %s", path, ex->message);
		} finally;
		
		g_object_unref (istream);
		g_object_unref (ostream);
		g_object_unref (parser);
		
#ifdef ENABLE_MBOX_MATCH
		g_object_unref (mstream);
		g_free (tmp);
#endif
	} else {
		goto exit;
	}
	
 exit:
	
#ifdef ENABLE_MBOX_MATCH
	//if ((dir = g_dir_open ("./tmp", 0, NULL))) {
	//	p = g_stpcpy (input, "./tmp");
	//	*p++ = G_DIR_SEPARATOR;
	//	
	//	while ((dent = g_dir_read_name (dir))) {
	//		strcpy (p, dent);
	//		unlink (input);
	//	}
	//	
	//	g_dir_close (dir);
	//}
	
	//rmdir ("./tmp");
#endif
	
	testsuite_end ();
	
	g_mime_shutdown ();
	
	return testsuite_exit ();
}
Пример #11
0
gboolean
g_mime_gpgme_mps_sign(GMimeMultipartSigned * mps, GMimeObject * content,
		      const gchar * userid, gpgme_protocol_t protocol,
		      GtkWindow * parent, GError ** err)
{
    GMimeStream *stream;
    GMimeStream *filtered;
    GMimeStream *sigstream;
    GMimeFilter *filter;
    GMimeContentType *content_type;
    GMimeDataWrapper *wrapper;
    GMimeParser *parser;
    GMimePart *signature;
    gchar *micalg;
    const gchar *proto_type;
    const gchar *sig_subtype;
    gpgme_hash_algo_t hash_algo;

    g_return_val_if_fail(GMIME_IS_MULTIPART_SIGNED(mps), FALSE);
    g_return_val_if_fail(GMIME_IS_OBJECT(content), FALSE);

    /* Prepare all the parts for signing... */
    sign_prepare(content);

    /* get the cleartext */
    stream = g_mime_stream_mem_new();
    filtered = g_mime_stream_filter_new(stream);

    /* Note: see rfc3156, section 3 - second note */
    filter = g_mime_filter_from_new(GMIME_FILTER_FROM_MODE_ARMOR);
    g_mime_stream_filter_add(GMIME_STREAM_FILTER(filtered), filter);
    g_object_unref(filter);

    /* Note: see rfc3156, section 5.4 (this is the main difference between rfc2015 and rfc3156) */
    filter = g_mime_filter_strip_new();
    g_mime_stream_filter_add(GMIME_STREAM_FILTER(filtered), filter);
    g_object_unref(filter);

    g_mime_object_write_to_stream(content, filtered);
    g_mime_stream_flush(filtered);
    g_object_unref(filtered);
    g_mime_stream_reset(stream);

    /* Note: see rfc2015 or rfc3156, section 5.1 */
    filtered = g_mime_stream_filter_new(stream);
    filter = g_mime_filter_crlf_new(TRUE, FALSE);
    g_mime_stream_filter_add(GMIME_STREAM_FILTER(filtered), filter);
    g_object_unref(filter);

    /* construct the signature stream */
    sigstream = g_mime_stream_mem_new();

    /* sign the content stream */
    hash_algo =
	libbalsa_gpgme_sign(userid, filtered, sigstream, protocol, FALSE,
			    parent, err);
    g_object_unref(filtered);
    if (hash_algo == GPGME_MD_NONE) {
	g_object_unref(sigstream);
	g_object_unref(stream);
	return FALSE;
    }

    g_mime_stream_reset(sigstream);
    g_mime_stream_reset(stream);

    /* set the multipart/signed protocol and micalg */
    content_type = g_mime_object_get_content_type(GMIME_OBJECT(mps));
    if (protocol == GPGME_PROTOCOL_OpenPGP) {
	micalg =
	    g_strdup_printf("PGP-%s", gpgme_hash_algo_name(hash_algo));
	proto_type = "application/pgp-signature";
	sig_subtype = "pgp-signature";
    } else {
	micalg = g_strdup(gpgme_hash_algo_name(hash_algo));
	proto_type = "application/pkcs7-signature";
	sig_subtype = "pkcs7-signature";
    }
    g_mime_content_type_set_parameter(content_type, "micalg", micalg);
    g_free(micalg);
    g_mime_content_type_set_parameter(content_type, "protocol", proto_type);
    g_mime_multipart_set_boundary(GMIME_MULTIPART(mps), NULL);

    /* construct the content part */
    parser = g_mime_parser_new_with_stream(stream);
    content = g_mime_parser_construct_part(parser);
    g_object_unref(stream);
    g_object_unref(parser);

    /* construct the signature part */
    signature = g_mime_part_new_with_type("application", sig_subtype);

    wrapper = g_mime_data_wrapper_new();
    g_mime_data_wrapper_set_stream(wrapper, sigstream);
    g_mime_part_set_content_object(signature, wrapper);
    g_object_unref(sigstream);
    g_object_unref(wrapper);

    /* FIXME: temporary hack, this info should probably be set in
     * the CipherContext class - maybe ::sign can take/output a
     * GMimePart instead. */
    if (protocol == GPGME_PROTOCOL_CMS) {
	g_mime_part_set_content_encoding(signature,
					 GMIME_CONTENT_ENCODING_BASE64);
	g_mime_part_set_filename(signature, "smime.p7m");
    }

    /* save the content and signature parts */
    /* FIXME: make sure there aren't any other parts?? */
    g_mime_multipart_add(GMIME_MULTIPART(mps), content);
    g_mime_multipart_add(GMIME_MULTIPART(mps), (GMimeObject *) signature);
    g_object_unref(signature);
    g_object_unref(content);

    return TRUE;
}
Пример #12
0
static void
rspamd_process_file (struct rspamd_config *cfg, const gchar *fname, gint mode)
{
	struct rspamd_task *task;
	gint fd;
	gpointer map;
	struct stat st;
	GError *err = NULL;
#if 0
	GMimeMessage *message;
	GMimeParser *parser;
	GMimeStream *stream;
	GByteArray tmp;
#endif
	struct rspamd_mime_part *part;
	guint i;
	gdouble ts1, ts2;

	fd = open (fname, O_RDONLY);

	if (fd == -1) {
		rspamd_fprintf (stderr, "cannot open %s: %s\n", fname, strerror (errno));
		exit (EXIT_FAILURE);
	}

	if (fstat (fd, &st) == -1) {
		rspamd_fprintf (stderr, "cannot stat %s: %s\n", fname, strerror (errno));
		exit (EXIT_FAILURE);
	}

	map = mmap (NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
	close (fd);

	if (map == MAP_FAILED) {
		rspamd_fprintf (stderr, "cannot mmap %s: %s\n", fname, strerror (errno));
		exit (EXIT_FAILURE);
	}

	task = rspamd_task_new (NULL, cfg);
	task->msg.begin = map;
	task->msg.len = st.st_size;

	ts1 = rspamd_get_ticks ();

	if (mode == MODE_NORMAL) {
		if (!rspamd_mime_parse_task (task, &err)) {
			rspamd_fprintf (stderr, "cannot parse %s: %e\n", fname, err);
			g_error_free (err);
		}
	}
#if 0
	else if (mode == MODE_GMIME) {
		tmp.data = map;
		tmp.len = st.st_size;
		stream = g_mime_stream_mem_new_with_byte_array (&tmp);
		g_mime_stream_mem_set_owner (GMIME_STREAM_MEM (stream), FALSE);
		parser = g_mime_parser_new_with_stream (stream);
		message = g_mime_parser_construct_message (parser);
	}
#endif
	ts2 = rspamd_get_ticks ();
	total_time += ts2 - ts1;

	if (mode == MODE_NORMAL) {
		for (i = 0; i < task->parts->len; i ++) {
			part = g_ptr_array_index (task->parts, i);

			if (part->ct->flags & RSPAMD_CONTENT_TYPE_MULTIPART) {
				rspamd_show_multipart (part);
			}
			else if (part->ct->flags & RSPAMD_CONTENT_TYPE_MESSAGE) {
				rspamd_show_message (part);
			}
			else {
				rspamd_show_normal (part);
			}
		}
	}
#if 0
	else if (mode == MODE_GMIME) {
		g_mime_message_foreach (message, mime_foreach_callback, NULL);
	}
#endif

	rspamd_task_free (task);
	munmap (map, st.st_size);
#if 0
	if (mode == MODE_GMIME) {
		g_object_unref (message);
	}
#endif
}