Exemple #1
0
static void wd_users_update_idle(wi_timer_t *timer) {
	wi_enumerator_t		*enumerator;
	wd_user_t			*user;
	wi_time_interval_t	interval;

	wi_dictionary_rdlock(wd_users);

	if(wi_dictionary_count(wd_users) > 0) {
		interval = wi_time_interval();

		enumerator = wi_dictionary_data_enumerator(wd_users);
		
		while((user = wi_enumerator_next_data(enumerator))) {
			wi_recursive_lock_lock(user->user_lock);
			
			if(user->state == WD_USER_LOGGED_IN && !user->idle &&
			   wi_date_time_interval(user->idle_time) + WD_USERS_IDLE_TIME < interval) {
				user->idle = true;
				
				wd_user_broadcast_status(user);
			}

			wi_recursive_lock_unlock(user->user_lock);
		}
	}
		
	wi_dictionary_unlock(wd_users);
}
Exemple #2
0
wi_boolean_t wi_p7_message_set_date_for_name(wi_p7_message_t *p7_message, wi_date_t *date, wi_string_t *field_name) {
	wi_time_interval_t	interval;
	
	if(!date)
		date = wi_date();
	
	interval = wi_date_time_interval(date);
	
	return wi_p7_message_set_double_for_name(p7_message, interval, field_name);
}
Exemple #3
0
wi_boolean_t wd_banlist_add_ban(wi_string_t *ip, wi_date_t *expiration_date, wd_user_t *user, wi_p7_message_t *message) {
	wi_dictionary_t		*results;
	wi_string_t			*string;
	
    // check for negative expiration date
	if(expiration_date && wi_date_time_interval(expiration_date) - wi_time_interval() < 1.0) {
		wi_log_error(WI_STR("Could not add ban for \"%@\" expiring at %@: Negative expiration date"),
			ip, wi_date_string_with_format(expiration_date, WI_STR("%Y-%m-%d %H:%M:%S")));
		wd_user_reply_internal_error(user, WI_STR("Ban has negative expiration date"), message);
		
		return false;
	}
	
    // check if an entry already exists for this IP address in the database
	results = wi_sqlite3_execute_statement(wd_database, WI_STR("SELECT ip FROM banlist WHERE ip = ?"),
										   ip,
										   NULL);
    
	// prevents for database statement error
    if(!results) {
		wi_log_error(WI_STR("Could not execute database statement: %m"));
		wd_user_reply_internal_error(user, wi_error_string(), message);
		return false;
	}
	
    // if an entry already exists, replay ban_exists error message and stop
	if(wi_dictionary_count(results) > 0) {
		wd_user_reply_error(user, WI_STR("wired.error.ban_exists"), message);
		
		return false;
	}
	
    // check if the ban is limited in time or not
	string = expiration_date ? wi_date_sqlite3_string(expiration_date) : NULL;
    
    // finally add the new ban entry to the database
	if(!wi_sqlite3_execute_statement(wd_database, WI_STR("INSERT INTO banlist "
														 "(ip, expiration_date) "
														 "VALUES "
														 "(?, ?)"),
									 ip,
									 string,
									 NULL)) {
		wi_log_error(WI_STR("Could not execute database statement: %m"));
		wd_user_reply_internal_error(user, wi_error_string(), message);
		
		return false;
	}
	
	return true;
}
Exemple #4
0
void wi_p7_message_serialize(wi_p7_message_t *p7_message, wi_p7_serialization_t serialization) {
	xmlDocPtr				doc;
	xmlNsPtr				ns;
	xmlNodePtr				root_node, list_node, item_node;
	wi_p7_spec_field_t		*field;
	wi_p7_spec_type_t		*type;
	wi_runtime_instance_t	*instance;
	wi_string_t				*field_name, *field_value;
	unsigned char			*buffer, *start;
	wi_uuid_t				*uuid;
	wi_date_t				*date;
	wi_p7_boolean_t			p7_bool;
	wi_p7_int32_t			p7_int32;
	wi_p7_uint32_t			p7_uint32;
	wi_p7_int64_t			p7_int64;
	wi_p7_uint64_t			p7_uint64;
	wi_p7_double_t			p7_double;
	wi_p7_oobdata_t			p7_oobdata;
	wi_string_t				*string;
	wi_data_t				*data;
	wi_array_t				*list;
	wi_uinteger_t			i, count;
	uint32_t				message_size, field_id, field_size;
	
	if(serialization == WI_P7_XML && !p7_message->xml_buffer) {
		doc			= xmlNewDoc((xmlChar *) "1.0");
		root_node	= xmlNewNode(NULL, (xmlChar *) "message");
		
		xmlDocSetRootElement(doc, root_node);
		
		ns = xmlNewNs(root_node, (xmlChar *) "http://www.zankasoftware.com/P7/Message", (xmlChar *) "p7");
		xmlSetNs(root_node, ns);
		
		xmlSetProp(root_node, (xmlChar *) "name", (xmlChar *) wi_string_cstring(p7_message->name));
		
		message_size = p7_message->binary_size - WI_P7_MESSAGE_BINARY_HEADER_SIZE;
		buffer = start = p7_message->binary_buffer + WI_P7_MESSAGE_BINARY_HEADER_SIZE;
		
		while((uint32_t) (buffer - start) < message_size) {
			field_id		= wi_read_swap_big_to_host_int32(buffer, 0);
			buffer			+= sizeof(field_id);
			field			= wi_p7_spec_field_with_id(p7_message->spec, field_id);

			if(!field)
				continue;
			
			field_size		= wi_p7_spec_field_size(field);
			
			if(field_size == 0) {
				field_size = wi_read_swap_big_to_host_int32(buffer, 0);
				
				buffer += sizeof(field_size);
			}
			
			field_name		= wi_p7_spec_field_name(field);
			field_value		= NULL;
			type			= wi_p7_spec_field_type(field);

			switch(wi_p7_spec_type_id(type)) {
				case WI_P7_BOOL:
					if(wi_p7_message_get_bool_for_name(p7_message, &p7_bool, field_name))
						field_value = wi_string_with_format(WI_STR("%u"), p7_bool ? 1 : 0);
					break;
					
				case WI_P7_ENUM:
					string = wi_p7_message_enum_name_for_name(p7_message, field_name);
					
					if(string)
						field_value = string;
					break;
					
				case WI_P7_INT32:
					if(wi_p7_message_get_int32_for_name(p7_message, &p7_int32, field_name))
						field_value = wi_string_with_format(WI_STR("%u"), p7_int32);
					break;
					
				case WI_P7_UINT32:
					if(wi_p7_message_get_uint32_for_name(p7_message, &p7_uint32, field_name))
						field_value = wi_string_with_format(WI_STR("%u"), p7_uint32);
					break;
					
				case WI_P7_INT64:
					if(wi_p7_message_get_int64_for_name(p7_message, &p7_int64, field_name))
						field_value = wi_string_with_format(WI_STR("%lld"), p7_int64);
					break;
					
				case WI_P7_UINT64:
					if(wi_p7_message_get_uint64_for_name(p7_message, &p7_uint64, field_name))
						field_value = wi_string_with_format(WI_STR("%llu"), p7_uint64);
					break;
					
				case WI_P7_DOUBLE:
					if(wi_p7_message_get_double_for_name(p7_message, &p7_double, field_name))
						field_value = wi_string_with_format(WI_STR("%f"), p7_double);
					break;
					
				case WI_P7_STRING:
					string = wi_p7_message_string_for_name(p7_message, field_name);
					
					if(string)
						field_value = string;
					break;
					
				case WI_P7_UUID:
					uuid = wi_p7_message_uuid_for_name(p7_message, field_name);
					
					if(uuid)
						field_value = wi_uuid_string(uuid);
					break;
					
				case WI_P7_DATE:
					date = wi_p7_message_date_for_name(p7_message, field_name);
					
					if(date)
						field_value = wi_string_with_format(WI_STR("%f"), wi_date_time_interval(date));
					break;
					
				case WI_P7_DATA:
					data = wi_p7_message_data_for_name(p7_message, field_name);
					
					if(data)
						field_value = wi_data_base64(data);
					break;
					
				case WI_P7_OOBDATA:
					if(wi_p7_message_get_oobdata_for_name(p7_message, &p7_oobdata, field_name))
						field_value = wi_string_with_format(WI_STR("%llu"), p7_oobdata);
					break;
					
				case WI_P7_LIST:
					list = wi_p7_message_list_for_name(p7_message, field_name);
					
					if(list) {
						list_node = wi_xml_node_child_with_name(root_node, field_name);
						
						if(!list_node) {
							list_node = xmlNewNode(ns, (xmlChar *) "field");
							xmlSetProp(list_node, (xmlChar *) "name", (xmlChar *) wi_string_cstring(field_name));
							xmlAddChild(root_node, list_node);
						}
						
						count = wi_array_count(list);
						
						for(i = 0; i < count; i++) {
							item_node = xmlNewNode(ns, (xmlChar *) "item");
							instance = WI_ARRAY(list, i);

							if(wi_runtime_id(instance) == wi_string_runtime_id())
								xmlNodeSetContent(item_node, (xmlChar *) wi_string_cstring(instance));
							
							xmlAddChild(list_node, item_node);
						}
					}
					break;
			}
			
			if(field_value) {
				item_node = wi_xml_node_child_with_name(root_node, field_name);
				
				if(!item_node) {
					item_node = xmlNewNode(ns, (xmlChar *) "field");
					xmlSetProp(item_node, (xmlChar *) "name", (xmlChar *) wi_string_cstring(field_name));
					xmlAddChild(root_node, item_node);
				}
				
				xmlNodeSetContent(item_node, (xmlChar *) wi_string_cstring(field_value));

			}
			
			buffer += field_size;
		}
		
		xmlDocDumpMemoryEnc(doc, &p7_message->xml_buffer, &p7_message->xml_length, "UTF-8");
		xmlFreeDoc(doc);
	}
}
void wi_test_filesystem_successes(void) {
    wi_array_t              *contents;
    wi_string_t             *path, *otherpath;
    wi_file_stats_t         file_stats;
    wi_filesystem_stats_t   filesystem_stats;
    wi_boolean_t            result, is_directory;
    
    path = wi_filesystem_temporary_path_with_template(WI_STR("/tmp/libwired-test-filesystem.XXXXXXX"));
    
    WI_TEST_ASSERT_NOT_NULL(path, "");
    
    result = wi_filesystem_create_directory_at_path(path);
    
    WI_TEST_ASSERT_TRUE(result, "");
    
    result = wi_filesystem_file_exists_at_path(path, &is_directory);
    
    WI_TEST_ASSERT_TRUE(result, "");
    WI_TEST_ASSERT_TRUE(is_directory, "");
    
    result = wi_filesystem_change_current_directory_to_path(path);
    
    WI_TEST_ASSERT_TRUE(result, "");
    
    otherpath = wi_filesystem_current_directory_path();
    
    WI_TEST_ASSERT_NOT_NULL(otherpath, "");
    WI_TEST_ASSERT_EQUAL_INSTANCES(otherpath, wi_string_by_resolving_symbolic_links_in_path(path), "");
    
    contents = wi_filesystem_directory_contents_at_path(path);
    
    WI_TEST_ASSERT_EQUAL_INSTANCES(contents, wi_array(), "");
    
    result = wi_filesystem_create_directory_at_path(WI_STR("foobar"));
    
    WI_TEST_ASSERT_TRUE(result, "");
    
    contents = wi_filesystem_directory_contents_at_path(path);
    
    WI_TEST_ASSERT_EQUAL_INSTANCES(contents, wi_array_with_data(WI_STR("foobar"), NULL), "");
    
    result = wi_string_write_utf8_string_to_path(WI_STR("hello world"), WI_STR("foobar/foobar"));
    
    WI_TEST_ASSERT_TRUE(result, "");
    
    result = wi_filesystem_get_file_stats_for_path(WI_STR("foobar/foobar"), &file_stats);
    
    WI_TEST_ASSERT_TRUE(result, "");
    WI_TEST_ASSERT_TRUE(file_stats.filesystem_id > 0, "");
    WI_TEST_ASSERT_TRUE(file_stats.file_id > 0, "");
    WI_TEST_ASSERT_EQUALS(file_stats.file_type, WI_FILE_REGULAR, "");
    WI_TEST_ASSERT_TRUE(file_stats.posix_permissions > 0, "");
    WI_TEST_ASSERT_EQUALS(file_stats.reference_count, 1, "");
    WI_TEST_ASSERT_EQUALS(file_stats.size, 11, "");
    WI_TEST_ASSERT_NOT_NULL(file_stats.user, "");
    WI_TEST_ASSERT_TRUE(wi_string_length(file_stats.user) > 0, "");
    WI_TEST_ASSERT_NOT_NULL(file_stats.group, "");
    WI_TEST_ASSERT_TRUE(wi_string_length(file_stats.group) > 0, "");
    WI_TEST_ASSERT_NOT_NULL(file_stats.creation_date, "");
    WI_TEST_ASSERT_TRUE(wi_date_time_interval(file_stats.creation_date) > 0, "");
    WI_TEST_ASSERT_NOT_NULL(file_stats.modification_date, "");
    WI_TEST_ASSERT_TRUE(wi_date_time_interval(file_stats.modification_date) > 0, "");
    
    result = wi_filesystem_get_filesystem_stats_for_path(WI_STR("foobar/foobar"), &filesystem_stats);
    
    WI_TEST_ASSERT_TRUE(result, "");
    
    WI_TEST_ASSERT_TRUE(filesystem_stats.size > 0, "");
    WI_TEST_ASSERT_TRUE(filesystem_stats.free_size > 0, "");
    WI_TEST_ASSERT_TRUE(filesystem_stats.free_size < filesystem_stats.size, "");
    WI_TEST_ASSERT_TRUE(filesystem_stats.nodes > 0, "");
    WI_TEST_ASSERT_TRUE(filesystem_stats.free_nodes > 0, "");
    WI_TEST_ASSERT_TRUE(filesystem_stats.free_nodes < filesystem_stats.nodes, "");
    
    result = wi_filesystem_create_symbolic_link_from_path(WI_STR("foobar"), WI_STR("foobar/foobaz"));
    
    WI_TEST_ASSERT_TRUE(result, "");
    
    result = wi_filesystem_copy_path_with_callback(WI_STR("foobar"), WI_STR("foobaz"), _wi_test_filesystem_successes_copy_callback);
    
    WI_TEST_ASSERT_TRUE(result, "");
    
    contents = wi_filesystem_directory_contents_at_path(path);
    
    WI_TEST_ASSERT_EQUAL_INSTANCES(wi_set_with_array(contents), wi_set_with_data(WI_STR("foobar"), WI_STR("foobaz"), NULL), "");
    
    result = wi_filesystem_delete_path_with_callback(path, _wi_test_filesystem_successes_delete_callback);
    
    WI_TEST_ASSERT_TRUE(result, "");
}