#include <gmime/gmime.h>
#include <string.h>
#include <stdio.h>

#define MSG_BODY \
"Subject: test\n" \
"Date: Thu, 08 Mar 2012 13:01:33 -0500\n" \
"From: alice@example.org\n" \
"To: bob@example.org\n" \
"Message-ID: abc123@example.org\n" \
"\n" \
"Hi Bob\n"

const char msg[] = MSG_BODY;

const char mbox[] = "From alice@example.org\n" MSG_BODY;

int onbuf(const char const * buf) {
  GMimeStream* stream;
  GMimeParser* parser;
  GMimeMessage* message;

  stream = g_mime_stream_mem_new_with_buffer(buf, strlen(buf));
  if (NULL == stream) {
    fprintf(stderr, "failed to create stream\n");
    return 1;
  }
  parser = g_mime_parser_new_with_stream(stream);
  if (NULL == parser) {
    fprintf(stderr, "failed to create parser\n");
    return 2;
  }
  if (g_mime_parser_get_scan_from(parser))
    printf("scan_from: true\n");
  else
    printf("scan_from: false\n");
  message = g_mime_parser_construct_message(parser);
  if (NULL == message) {
    fprintf(stderr, "failed to create message\n");
    return 3;
  }

  return 0;
}

int main() {
  g_mime_init(0);
  printf(" msg: %d\n", onbuf(msg));
  printf("mbox: %d\n", onbuf(mbox));
  g_mime_shutdown();
  return 0;
}
