Пример #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
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;
}
Пример #3
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;
}
Пример #4
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;
}
Пример #5
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;
}
Пример #6
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;
}
Пример #7
0
parsed_article *parse_file(const char *file_name) {
  static parsed_article pa;
  GMimeStream *stream;
  GMimeMessage *msg = NULL;
  int offset;
  int file;

  printf("%s\n", file_name);

  if ((file = open(file_name, O_RDONLY|O_STREAMING)) == -1) {
    fprintf(stderr, "Can't open %s\n", file_name);
    return NULL;
  }

  stream = g_mime_stream_fs_new(file);
  msg = g_mime_parser_construct_message(stream);
  g_mime_stream_unref(stream);

  if (msg != 0) {
    hstrcpy(pa.from, g_mime_message_get_header(msg, "From"));
    hstrcpy(pa.subject, g_mime_message_get_subject(msg));
    hstrcpy(pa.message_id, g_mime_message_get_message_id(msg));
    hstrcpy(pa.references, g_mime_message_get_header(msg, "references"));
    hstrcpy(pa.xref, g_mime_message_get_header(msg, "xref"));
    hstrcpy(pa.original_message_id,
	    g_mime_message_get_header(msg, "original-message-id"));
    g_mime_message_get_date(msg, &pa.date, &offset);

    if (pa.xref != NULL &&
	strstr(pa.xref, "gmane.spam.detected") != NULL)
      pa.spamp = 1;
    else
      pa.spamp = 0;

    g_mime_message_foreach_part(msg, count_part, (gpointer) &pa);

    g_mime_object_unref(GMIME_OBJECT(msg));
  
  }
  close(file);
  return &pa;
}
Пример #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
document* parse_file(const char *file_name) {
  int tallied_length = 0;
  GMimeStream *stream;
  GMimeMessage *msg = 0;
  // struct stat stat_buf;
  const char *author, *subject, *xref, *xref_end;
  time_t date;
  int offset;
  int num_words = 0;
  int file;
  InternetAddress *iaddr;
  InternetAddressList *iaddr_list;
  char *address;

#if DEBUG
  printf("%s\n", file_name);
#endif

  /*
  if ((file = stat(file_name, &stat_buf)) == -1) {
    perror("tokenizer");
    return NULL;
  }
  */

  // |O_STREAMING
  if ((file = open(file_name, O_RDONLY|O_STREAMING)) == -1) {
    perror("tokenizer");
    return NULL;
  }

#ifdef POSIX_FADV_NOREUSE
  no_reuse(file);
#endif

  stream = g_mime_stream_fs_new(file);
  msg = g_mime_parser_construct_message(stream);
  g_mime_stream_unref(stream);

  if (msg != 0) {
    table = g_hash_table_new(g_str_hash, g_str_equal);
    bufp = buffer;
    dword_table[0].word = NULL;
    bzero(saved_body, MAX_SAVED_BODY_LENGTH);
    saved_body_length = 0;
    author = g_mime_message_get_sender(msg);
    subject = g_mime_message_get_subject(msg);
    xref = g_mime_message_get_header(msg, "Xref");
    g_mime_message_get_date(msg, &date, &offset);
    if (author != NULL && subject != NULL && xref != NULL) {
      tallied_length = tally_string(author, tallied_length);
      strncpy(doc.author, author, MAX_HEADER_LENGTH-1);

      /* Get the address from the From header. */
      if ((iaddr_list = internet_address_parse_string(author)) != NULL) {
	iaddr = iaddr_list->address;
	internet_address_set_name(iaddr, NULL);
	address = internet_address_to_string(iaddr, FALSE);
	strncpy(doc.address, address, MAX_HEADER_LENGTH-1);
	downcase_string(doc.address);
	free(address);
	internet_address_list_destroy(iaddr_list);
      } else {
	*doc.address = 0;
      }

      tallied_length = tally_string(subject, tallied_length);
      strncpy(doc.subject, subject, MAX_HEADER_LENGTH-1);

      doc.time = date;

      if ((xref = strchr(xref, ' ')) != NULL) {
	xref++;
	xref_end = strchr(xref, ':');
	*doc.group = 0;
	strncat(doc.group, xref, min(xref_end-xref, MAX_HEADER_LENGTH-1));
	xref_end++;
	sscanf(xref_end, "%d", &doc.article);
      }

      g_mime_message_foreach_part(msg, partFound, (gpointer) &tallied_length);

      strncpy(doc.body, saved_body, MAX_SAVED_BODY_LENGTH);

      g_hash_table_foreach(table, add_word_to_table, (gpointer) &num_words);
      dword_table[num_words].word = NULL;
      g_hash_table_destroy(table);
      g_mime_object_unref(GMIME_OBJECT(msg));
    } else {
      close(file);
      return NULL;
    }
  }
  close(file);
  doc.words = dword_table;
  doc.num_words = num_words;

  return &doc;
}
Пример #11
0
static void
test_parser (GMimeParser *parser, GMimeStream *mbox, GMimeStream *summary)
{
	gint64 message_begin, message_end, headers_begin, headers_end;
	GMimeFormatOptions *format = g_mime_format_options_get_default ();
	InternetAddressList *list;
	GMimeMessage *message;
	char *marker, *buf;
	const char *subject;
	GMimeObject *body;
	GDateTime *date;
	int nmsg = 0;
	
	while (!g_mime_parser_eos (parser)) {
		message_begin = g_mime_parser_tell (parser);
		if (!(message = g_mime_parser_construct_message (parser, NULL)))
			throw (exception_new ("failed to parse message #%d", nmsg));
		
		message_end = g_mime_parser_tell (parser);
		
		headers_begin = g_mime_parser_get_headers_begin (parser);
		headers_end = g_mime_parser_get_headers_end (parser);
		
		g_mime_stream_printf (summary, "message offsets: %" G_GINT64_FORMAT ", %" G_GINT64_FORMAT "\n",
				      message_begin, message_end);
		g_mime_stream_printf (summary, "header offsets: %" G_GINT64_FORMAT ", %" G_GINT64_FORMAT "\n",
				      headers_begin, headers_end);
		
		marker = g_mime_parser_get_mbox_marker (parser);
		g_mime_stream_printf (summary, "%s\n", marker);
		
		if ((list = g_mime_message_get_from (message)) != NULL &&
		    internet_address_list_length (list) > 0) {
			buf = internet_address_list_to_string (list, format, FALSE);
			g_mime_stream_printf (summary, "From: %s\n", buf);
			g_free (buf);
		}
		
		if ((list = g_mime_message_get_addresses (message, GMIME_ADDRESS_TYPE_TO)) != NULL &&
		    internet_address_list_length (list) > 0) {
			buf = internet_address_list_to_string (list, format, FALSE);
			g_mime_stream_printf (summary, "To: %s\n", buf);
			g_free (buf);
		}
		
		if (!(subject = g_mime_message_get_subject (message)))
			subject = "";
		g_mime_stream_printf (summary, "Subject: %s\n", subject);
		
		if (!(date = g_mime_message_get_date (message))) {
			date = g_date_time_new_from_unix_utc (0);
		} else {
			g_date_time_ref (date);
		}
		buf = g_mime_utils_header_format_date (date);
		g_mime_stream_printf (summary, "Date: %s\n", buf);
		g_date_time_unref (date);
		g_free (buf);
		
		body = g_mime_message_get_mime_part (message);
		print_mime_struct (summary, body, 0);
		g_mime_stream_write (summary, "\n", 1);
		
		if (mbox) {
			if (nmsg > 0)
				g_mime_stream_write (mbox, "\n", 1);
			
			g_mime_stream_printf (mbox, "%s\n", marker);
			g_mime_object_write_to_stream ((GMimeObject *) message, format, mbox);
		}
		
		g_object_unref (message);
		g_free (marker);
		nmsg++;
	}
}
Пример #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
}