Exemple #1
0
static void
test_address_sync (void)
{
	const char *raw_value, *value;
	InternetAddress *addr, *ia;
	InternetAddressList *list;
	GMimeHeaderList *headers;
	GMimeMessage *message;
	GMimeObject *object;
	GMimeHeader *header;
	
	message = g_mime_message_new (TRUE);
	list = g_mime_message_get_addresses (message, GMIME_ADDRESS_TYPE_TO);
	object = (GMimeObject *) message;
	headers = object->headers;
	
	testsuite_check ("address header synchronization");
	try {
		/* first, check that the To recipients are empty */
		if (list == NULL || internet_address_list_length (list) != 0)
			throw (exception_new ("unexpected initial internet address list"));
		
		/* now check that the initial header value is null */
		if ((value = g_mime_object_get_header (object, "To")) != NULL)
			throw (exception_new ("unexpected initial value"));
		
		header = g_mime_header_list_get_header (headers, "To");
		if ((raw_value = g_mime_header_get_raw_value (header)) != NULL)
			throw (exception_new ("unexpected initial raw_value"));
		
		/* now try adding an address */
		addr = internet_address_mailbox_new ("Tester", "*****@*****.**");
		internet_address_list_add (list, addr);
		
		if (!(value = g_mime_object_get_header (object, "To")))
			throw (exception_new ("address list header unexpectedly null after adding recipient"));
		if (strcmp ("Tester <*****@*****.**>", value) != 0)
			throw (exception_new ("unexpected address list header after adding recipient"));
		header = g_mime_header_list_get_header (headers, "To");
		if (!(raw_value = g_mime_header_get_raw_value (header)))
			throw (exception_new ("raw_value is null after adding recipient"));
		if (strcmp (" Tester <*****@*****.**>\n", raw_value) != 0)
			throw (exception_new ("unexpected raw_value after adding recipient: %s", raw_value));
		
		/* now let's try changing the address name to make sure signals properly chain up */
		internet_address_set_name (addr, "Eva Lucy-Ann Tester");
		if (!(value = g_mime_object_get_header (object, "To")))
			throw (exception_new ("address list header unexpectedly null after changing name"));
		if (strcmp ("Eva Lucy-Ann Tester <*****@*****.**>", value) != 0)
			throw (exception_new ("unexpected address list header after changing name: %s", value));
		header = g_mime_header_list_get_header (headers, "To");
		if (!(raw_value = g_mime_header_get_raw_value (header)))
			throw (exception_new ("raw_value is null after changing name"));
		if (strcmp (" Eva Lucy-Ann Tester <*****@*****.**>\n", raw_value) != 0)
			throw (exception_new ("unexpected raw_value after changing name"));
		
		/* now let's try changing the address mailbox... */
		internet_address_mailbox_set_addr ((InternetAddressMailbox *) addr, "*****@*****.**");
		if (!(value = g_mime_object_get_header (object, "To")))
			throw (exception_new ("address list header unexpectedly null after changing mailbox"));
		if (strcmp ("Eva Lucy-Ann Tester <*****@*****.**>", value) != 0)
			throw (exception_new ("unexpected address list header after changing mailbox"));
		header = g_mime_header_list_get_header (headers, "To");
		if (!(raw_value = g_mime_header_get_raw_value (header)))
			throw (exception_new ("raw_value is null after changing mailbox"));
		if (strcmp (" Eva Lucy-Ann Tester <*****@*****.**>\n", raw_value) != 0)
			throw (exception_new ("unexpected raw_value after changing mailbox"));
		
		/* now let's try inserting a group address */
		g_object_unref (addr);
		addr = internet_address_group_new ("Group");
		internet_address_list_insert (list, 0, addr);
		
		if (!(value = g_mime_object_get_header (object, "To")))
			throw (exception_new ("address list header unexpectedly null after inserting group"));
		if (strcmp ("Group: ;, Eva Lucy-Ann Tester <*****@*****.**>", value) != 0)
			throw (exception_new ("unexpected address list header after inserting group"));
		header = g_mime_header_list_get_header (headers, "To");
		if (!(raw_value = g_mime_header_get_raw_value (header)))
			throw (exception_new ("raw_value is null after inserting group"));
		if (strcmp (" Group: ;, Eva Lucy-Ann Tester <*****@*****.**>\n", raw_value) != 0)
			throw (exception_new ("unexpected raw_value after inserting group"));
		
		/* now let's try removing the original recipient */
		internet_address_list_remove_at (list, 1);
		if (!(value = g_mime_object_get_header (object, "To")))
			throw (exception_new ("address list header unexpectedly null after removing recipient"));
		if (strcmp ("Group: ;", value) != 0)
			throw (exception_new ("unexpected address list header after removing recipient"));
		header = g_mime_header_list_get_header (headers, "To");
		if (!(raw_value = g_mime_header_get_raw_value (header)))
			throw (exception_new ("raw_value is null after removing recipient"));
		if (strcmp (" Group: ;\n", raw_value) != 0)
			throw (exception_new ("unexpected raw_value after removing recipient"));
		
		/* now let's try adding an address to the group... */
		ia = internet_address_mailbox_new ("Tester", "*****@*****.**");
		internet_address_list_add (((InternetAddressGroup *) addr)->members, ia);
		if (!(value = g_mime_object_get_header (object, "To")))
			throw (exception_new ("address list header unexpectedly null after adding addr to group"));
		if (strcmp ("Group: Tester <*****@*****.**>;", value) != 0)
			throw (exception_new ("unexpected address list header after adding addr to group"));
		header = g_mime_header_list_get_header (headers, "To");
		if (!(raw_value = g_mime_header_get_raw_value (header)))
			throw (exception_new ("raw_value is null after adding addr to group"));
		if (strcmp (" Group: Tester <*****@*****.**>;\n", raw_value) != 0)
			throw (exception_new ("unexpected raw_value after adding addr to group"));
		
		/* let's try this in reverse... set the header value and make sure InternetAddressList gets updated */
		g_mime_object_set_header (object, "To", "[email protected] (=?iso-8859-1?q?Fran=E7ois?= Pons)", NULL);
		if (internet_address_list_length (list) != 1)
			throw (exception_new ("unexpected number of addresses in addrlist after setting header value"));
		ia = internet_address_list_get_address (list, 0);
		value = internet_address_get_name (ia);
		if (strcmp ("Fran\xc3\xa7ois Pons", value) != 0)
			throw (exception_new ("unexpected name after setting header value"));
		value = internet_address_mailbox_get_addr ((InternetAddressMailbox *) ia);
		if (strcmp ("*****@*****.**", value) != 0)
			throw (exception_new ("unexpected addr after setting header value"));
		header = g_mime_header_list_get_header (headers, "To");
		if (!(raw_value = g_mime_header_get_raw_value (header)))
			throw (exception_new ("raw_value is null after setting header value"));
		if (strcmp (" =?iso-8859-1?q?Fran=E7ois?= Pons <*****@*****.**>\n", raw_value) != 0)
			throw (exception_new ("unexpected raw_value after setting header value: %s", raw_value));
		
		testsuite_check_passed ();
	} catch (ex) {
		testsuite_check_failed ("address header not synchronized: %s", ex->message);
	} finally;
	
	g_object_unref (message);
}
Exemple #2
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;
}