Ejemplo n.º 1
0
wi_boolean_t wi_settings_read_file(wi_settings_t *settings, wi_boolean_t chroot) {
	wi_file_t		*file;
	wi_string_t		*path, *string;
	wi_boolean_t	result = true;
	
	path = wi_full_path(wi_settings_config_path);
	file = wi_file_for_reading(path);
	
	if(!file) {
		wi_log_err(WI_STR("Could not open %@: %s"),
			path, strerror(errno));
		
		return false;
	}
	
	wi_log_info(WI_STR("Reading %@"), path);
	_wi_settings_clear(settings);
	
	settings->file		= path;
	settings->line		= 0;
	settings->chroot	= chroot;
	
	while((string = wi_file_read_line(file))) {
		settings->line++;

		if(wi_string_length(string) > 0 && !wi_string_has_prefix(string, WI_STR("#"))) {
			if(!_wi_settings_parse_setting(settings, string))
				result = false;
		}
	}

	settings->file = NULL;

	return result;
}
Ejemplo n.º 2
0
wi_data_t * wi_data_init_with_contents_of_file(wi_data_t *data, wi_string_t *path) {
	wi_file_t		*file;
	wi_fs_stat_t	sb;
	char			buffer[WI_FILE_BUFFER_SIZE];
	wi_integer_t	bytes;
	
	if(!wi_fs_stat_path(path, &sb)) {
		wi_release(data);
		
		return NULL;
	}
	
	file = wi_file_for_reading(path);
	
	if(!file) {
		wi_release(data);
		
		return NULL;
	}
	
	data = wi_data_init_with_capacity(data, sb.size);
	
	while((bytes = wi_file_read_buffer(file, buffer, sizeof(buffer))))
		_wi_data_append_bytes(data, buffer, bytes);
	
	return data;
}
Ejemplo n.º 3
0
wi_boolean_t wi_settings_read_file(wi_settings_t *settings) {
	wi_file_t		*file;
	wi_string_t		*string;
	
	file = wi_file_for_reading(wi_settings_config_path);
	
	if(!file) {
		wi_log_err(WI_STR("Could not open %@: %s"),
			wi_settings_config_path, strerror(errno));
		
		return false;
	}
	
	wi_log_info(WI_STR("Reading %@"), wi_settings_config_path);
	_wi_settings_clear(settings);
	
	settings->file		= wi_settings_config_path;
	settings->line		= 0;
	
	while((string = wi_file_read_line(file))) {
		settings->line++;

		if(wi_string_length(string) > 0 && !wi_string_has_prefix(string, WI_STR("#")))
			_wi_settings_parse_setting(settings, string);
	}

	settings->file = NULL;

	return true;
}
Ejemplo n.º 4
0
wi_boolean_t wd_banlist_ip_is_banned(wi_string_t *ip) {
	wi_file_t			*file;
	wi_string_t			*string;
	wi_boolean_t		banned = false;

	wi_dictionary_rdlock(wd_tempbans);
	banned = (wi_dictionary_data_for_key(wd_tempbans, ip) != NULL);
	wi_dictionary_unlock(wd_tempbans);
	
	if(banned)
		return banned;

	if(wd_settings.banlist) {
		file = wi_file_for_reading(wd_settings.banlist);
		
		if(!file) {
			wi_log_err(WI_STR("Could not open %@: %m"), wd_settings.banlist);
		} else {
			while((string = wi_file_read_config_line(file))) {
				if(wi_ip_matches_string(ip, string)) {
					banned = true;
					
					break;
				}
			}
		}
	}
	
	return banned;
}
Ejemplo n.º 5
0
wi_boolean_t wt_category_is_valid(wi_string_t *category) {
	wi_file_t		*file;
	wi_string_t		*string;
	wi_boolean_t	result = false;

	if(wi_string_length(category) == 0)
		return true;

	file = wi_file_for_reading(wt_settings.categories);
	
	if(!file) {
		wi_log_err(WI_STR("Could not open %@: %m"), wt_settings.categories);
		
		return true;
	}
	
	while((string = wi_file_read_config_line(file))) {
		if(wi_is_equal(category, string)) {
			result = true;
			
			break;
		}
	}

	return result;
}
Ejemplo n.º 6
0
static void wr_command_load(wi_array_t *arguments) {
	wi_mutable_string_t		*path;
	wi_file_t				*file;
	
	path = wi_user_home();
	path = wi_string_by_appending_path_component(path, WI_STR(WB_WIREBOT_USER_PATH));
	path = wi_string_by_appending_path_component(path, WI_ARRAY(arguments, 0));
	
	file = wi_file_for_reading(path);
	
	if(file)
		wr_commands_parse_file(file);
	else
		wr_printf_prefix(WI_STR("load: %@: %m"), path);
}
Ejemplo n.º 7
0
wi_string_t * wi_string_init_with_contents_of_file(wi_string_t *string, wi_string_t *path) {
	wi_file_t       *file;
	
	wi_release(string);
		
	file = wi_file_for_reading(path);
	
	if(!file)
		return NULL;
	
	string = wi_file_read_to_end_of_file(file);
	
	wi_file_close(file);
	
	return wi_retain(string);
}
Ejemplo n.º 8
0
static void wt_cmd_categories(wi_array_t *arguments) {
	wi_file_t		*file;
	wi_string_t		*string;

	file = wi_file_for_reading(wt_settings.categories);
	
	if(!file) {
		wt_reply(500, WI_STR("Command Failed"));
		wi_log_err(WI_STR("Could not open %@: %m"), wt_settings.categories);

		return;
	}

	while((string = wi_file_read_config_line(file)))
		wt_reply(710, WI_STR("%@"), string);

	wt_reply(711, WI_STR("Done"));
}
Ejemplo n.º 9
0
static wi_boolean_t wd_banlist_convert_banlist(wi_string_t *path) {
	wi_file_t			*file;
	wi_string_t			*string;
	wi_boolean_t		result;
	
	file = wi_file_for_reading(path);
	
	if(file) {
		result = true;
		
		wi_sqlite3_begin_immediate_transaction(wd_database);
		
		while((string = wi_file_read_config_line(file))) {
			if(!wi_sqlite3_execute_statement(wd_database, WI_STR("INSERT INTO banlist "
																 "(ip, expiration_date) "
																 "VALUES "
																 "(?, NULL)"),
											 string,
											 NULL)) {
				wi_log_error(WI_STR("Could not execute database statement: %m"));
				
				result = false;
				break;
			}
		}
		
		if(result)
			wi_sqlite3_commit_transaction(wd_database);
		else
			wi_sqlite3_rollback_transaction(wd_database);
		
		return result;
	} else {
		wi_log_error(WI_STR("Could not open \"%@\": %m"), path);
		
		return false;
	}
}
Ejemplo n.º 10
0
int main(int argc, const char **argv) {
	wi_pool_t			*pool;
	wi_string_t			*homepath, *wirepath, *path, *component;
	wi_file_t			*file;	
	int					ch;

	/* init libwired */
	wi_initialize();
	wi_load(argc, argv);
	
	pool				= wi_pool_init(wi_pool_alloc());
	wi_log_callback		= wr_wi_log_callback;
	
	/* init core systems */
	wr_version_init();
	wr_start_date		= wi_date_init(wi_date_alloc());
	
	/* set defaults */
	wr_nick				= wi_retain(wi_user_name());
	homepath			= wi_user_home();
	wr_timestamp_format	= wi_retain(WI_STR("%H:%M"));

	/* parse command line switches */
	while((ch = getopt(argc, (char * const *) argv, "DhVv")) != -1) {
		switch(ch) {
			case 'D':
				wr_debug = true;

				wi_log_level = WI_LOG_DEBUG;
				wi_log_file = true;
				wi_log_path = WI_STR("wire.out");
				wi_log_callback = NULL;
				break;

			case 'V':
			case 'v':
				wr_version();
				break;

			case '?':
			case 'h':
			default:
				wr_usage();
				break;
		}
	}

	argc -= optind;
	argv += optind;

	/* open log */
	wi_log_open();
	
	/* create ~/.wire */
	wirepath = wi_string_by_appending_path_component(homepath, WI_STR(WR_WIRE_PATH));
	wi_file_create_directory(wirepath, 0700);
	
	/* init subsystems */
	wr_signals_init();
	wr_terminal_init();
	wr_readline_init();
	wr_chats_init();
	wr_windows_init();
	wr_client_init();
	wr_runloop_init();
	wr_users_init();
	wr_ignores_init();
	wr_files_init();
	wr_transfers_init();
	wr_servers_init();
	
	/* open default settings */
	path = wi_string_by_appending_path_component(homepath, WI_STR(WR_WIRE_CONFIG_PATH));
	file = wi_file_for_reading(path);

	if(file)
		wr_parse_file(file);
	else
		wr_printf_prefix(WI_STR("%@: %m"), path);

	/* read specified bookmark */
	if(*argv) {
		component	= wi_string_with_cstring(*argv);
		path		= wi_string_by_appending_path_component(wirepath, component);
		file		= wi_file_for_reading(path);

		if(file)
			wr_parse_file(file);
		else
			wr_printf_prefix(WI_STR("%@: %m"), path);
	}
	
	/* clean up pool after startup */
	wi_pool_drain(pool);
	
	/* enter event loop */
	wr_runloop_run();

	/* clean up */
	wr_cleanup();
	wi_release(pool);
	
	return 0;
}
Ejemplo n.º 11
0
wi_boolean_t wi_config_read_file(wi_config_t *config) {
	wi_enumerator_t				*enumerator;
	wi_runtime_instance_t		*instance;
	wi_file_t					*file;
	wi_mutable_array_t			*array;
	wi_mutable_dictionary_t		*previous_values;
	wi_string_t					*string, *name, *value;
	wi_config_type_t			type;
	
	file = wi_file_for_reading(config->path);
	
	if(!file) {
		wi_log_err(WI_STR("Could not open %@: %m"),
			config->path);
		
		return false;
	}
	
	wi_log_info(WI_STR("Reading %@"), config->path);
	
	config->line = 0;
	
	wi_lock_lock(config->lock);
	
	previous_values = config->values;
	
	config->values = wi_dictionary_init(wi_mutable_dictionary_alloc());
	
	if(config->defaults) {
		enumerator = wi_dictionary_key_enumerator(config->defaults);
		
		while((name = wi_enumerator_next_data(enumerator))) {
//			instance = wi_mutable_copy(wi_dictionary_data_for_key(config->defaults, name));
			instance = wi_dictionary_data_for_key(config->defaults, name);
			if(wi_runtime_id(instance) == wi_array_runtime_id())
				instance = wi_autorelease(wi_mutable_copy(instance));
			wi_mutable_dictionary_set_data_for_key(config->values, instance, name);
//			wi_release(instance);
		}
	}
	
	while((string = wi_file_read_line(file))) {
		config->line++;

		if(wi_string_length(string) > 0 && !wi_string_has_prefix(string, WI_STR("#"))) {
			if(_wi_config_parse_string(config, string, &name, &value)) {
				instance = _wi_config_instance_for_setting(config, name, value, &type);
				
				if(instance) {
					wi_log_debug(WI_STR("  %@ = %@"), name, value);
					
					if(type == WI_CONFIG_STRINGLIST) {
						array = wi_dictionary_data_for_key(config->values, name);
						
						if(!array) {
							array = wi_mutable_array();
							
							wi_mutable_dictionary_set_data_for_key(config->values, array, name);
						}
						
						wi_mutable_array_add_data(array, instance);
					} else {
						wi_mutable_dictionary_set_data_for_key(config->values, instance, name);
					}
				} else {
					_wi_config_log_error(config, name);
				}
			} else {
				wi_error_set_libwired_error(WI_ERROR_SETTINGS_SYNTAXERROR);
				
				_wi_config_log_error(config, string);
			}
		}
	}
	
	enumerator = wi_dictionary_key_enumerator(config->values);
	
	while((name = wi_enumerator_next_data(enumerator))) {
		instance = wi_dictionary_data_for_key(config->values, name);
		
		if(!previous_values || !wi_is_equal(instance, wi_dictionary_data_for_key(previous_values, name)))
			wi_mutable_set_add_data(config->changes, name);
	}
	
	wi_release(previous_values);

	wi_lock_unlock(config->lock);

	return true;
}
Ejemplo n.º 12
0
void wr_transfer_request(wr_transfer_t *transfer) {
    
	wi_string_t			*local_path;
	wi_fs_stat_t		sb;
    
	if(wi_array_count(transfer->local_paths) == 0) {
		wr_transfer_start_next_or_stop(transfer);
		
		return;
	}
    
	if(transfer->type == WR_TRANSFER_DOWNLOAD) {
		local_path = wi_array_first_data(transfer->local_paths);
		
		if(wi_fs_stat_path(local_path, &sb)) {
			if(!transfer->recursive) {
				wr_printf_prefix(WI_STR("get: File already exists at %@"),
                                 local_path);
			}
			
			wr_transfer_close(transfer);
			
			if(transfer->recursive) {
				transfer->total_transferred += sb.size;
				wr_transfer_start_next_or_stop(transfer);
			}
			
			return;
		}
		
		if(!wi_string_has_suffix(local_path, WI_STR(WR_TRANSFERS_SUFFIX))) {
			local_path = wi_string_by_appending_string(local_path, WI_STR(WR_TRANSFERS_SUFFIX));
			
			wi_mutable_array_replace_data_at_index(transfer->local_paths, local_path, 0);
		}
		
		if(wi_fs_stat_path(local_path, &sb)) {
			transfer->file_offset = sb.size;
			
			if(sb.size >= WR_CHECKSUM_SIZE)
				transfer->checksum = wi_retain(wi_fs_sha1_for_path(local_path, WR_CHECKSUM_SIZE));
		}
		
		transfer->file = wi_retain(wi_file_for_updating(local_path));
		
		if(!transfer->file) {
			wr_printf_prefix(WI_STR("get: Could not open %@: %m"),
                             local_path);
			
			wr_transfer_close(transfer);
			
			if(transfer->recursive)
				wr_transfer_start_next_or_stop(transfer);
			
			return;
		}
		
		transfer->file_size = wr_file_size(wi_array_first_data(transfer->files));
        
		// wr_send_command(WI_STR("GET %#@%c%llu"),
        //                wi_array_first_data(transfer->remote_paths),	WR_FIELD_SEPARATOR,
        //                transfer->file_offset);
        
	} else {
		local_path = wi_array_first_data(transfer->local_paths);
        
		transfer->file = wi_retain(wi_file_for_reading(local_path));
        
		if(!transfer->file) {
			wr_printf_prefix(WI_STR("put: Could not open %@: %m"),
                             local_path);
			
			wr_transfer_close(transfer);
			
			if(transfer->recursive)
				wr_transfer_start_next_or_stop(transfer);
			
			return;
		}
		
		if(!wi_fs_stat_path(local_path, &sb)) {
			wr_printf_prefix(WI_STR("put: Could not open %@: %m"),
                             local_path);
			
			wr_transfer_close(transfer);
			
			if(transfer->recursive)
				wr_transfer_start_next_or_stop(transfer);
			
			return;
		}
        
		transfer->file_size = sb.size;
		transfer->checksum = wi_retain(wi_fs_sha1_for_path(local_path, WR_CHECKSUM_SIZE));
		
		// wr_send_command(WI_STR("PUT %#@%c%llu%c%#@"),
		//				wi_array_first_data(transfer->remote_paths),	WR_FIELD_SEPARATOR,
		//				transfer->file_size,							WR_FIELD_SEPARATOR,
		//				transfer->checksum);
	}
     
}