Exemplo n.º 1
0
char * wr_readline_nickname_generator(const char *text, int state) {
	static wi_uinteger_t	index;
	wi_array_t				*users;
	wi_string_t				*name;
	char					*cname;
	wr_user_t				*user;
	wi_uinteger_t			count;
	
	if(!wr_window_is_chat(wr_current_window))
		return NULL;
	
	users = wr_chat_users(wr_window_chat(wr_console_window));
	
	if(state == 0)
		index = 0;

	cname = ((*rl_filename_dequoting_function) ((char *) text, 0));
	name = wi_string_with_cstring(cname);
	free(cname);
	
	count = wi_array_count(users);
	
	while(index < count) {
		user = WI_ARRAY(users, index++);
		
		if(wi_string_index_of_string(user->nick, name, WI_STRING_SMART_CASE_INSENSITIVE) == 0)
			return strdup(wi_string_cstring(user->nick));
	}
	
	return NULL;
}
Exemplo n.º 2
0
wi_string_t * wi_socket_cipher_version(wi_socket_t *socket) {
#ifdef WI_SSL
	return wi_string_with_cstring(SSL_get_cipher_version(socket->ssl));
#else
	return NULL;
#endif
}
Exemplo n.º 3
0
wi_string_t * wi_socket_cipher_name(wi_socket_t *socket) {
#ifdef HAVE_OPENSSL_SSL_H
	return wi_string_with_cstring(SSL_get_cipher_name(socket->ssl));
#else
	return NULL;
#endif
}
Exemplo n.º 4
0
void wr_draw_transfers(wi_boolean_t force) {
	static wi_time_interval_t	update;
	wi_enumerator_t				*enumerator;
	wi_mutable_string_t			*string;
    wi_string_t					*status;
	wr_transfer_t				*transfer;
	wi_time_interval_t			interval;
	wi_uinteger_t				i = 0;
	
	interval = wi_time_interval();
	
	if(!force && interval - update < 1.0)
		return;
	
	update = interval;
	
	wi_terminal_set_scroll(wr_terminal, wi_make_range(1 + wi_array_count(wr_transfers),
													  wi_terminal_size(wr_terminal).height - 4));
	
	enumerator = wi_array_data_enumerator(wr_transfers);
	
	while((transfer = wi_enumerator_next_data(enumerator))) {
		wi_terminal_move(wr_terminal, wi_make_point(0, i + 1));
		wi_terminal_clear_line(wr_terminal);
		
		if(transfer->state == WR_TRANSFER_RUNNING && interval - transfer->start_time > 0.0) {
			transfer->speed = ((double) transfer->total_transferred - transfer->total_offset) / (interval - transfer->start_time);
			
			status = wi_string_with_format(WI_STR("%@/%@, %@/s"),
                                           wr_files_string_for_size(transfer->total_transferred),
                                           wr_files_string_for_size(transfer->total_size),
                                           wr_files_string_for_size(transfer->speed));
		}
		else if(transfer->state == WR_TRANSFER_QUEUED) {
			status = wi_string_with_format(WI_STR("queued at %u"),
                                           transfer->queue);
		}
		else {
			status = wi_string_with_cstring("waiting");
		}
		
		string = wi_mutable_string_with_format(WI_STR("%u %3.0f%%  %@"),
                                               transfer->tid,
                                               transfer->total_size > 0
                                               ? 100 * ((double) transfer->total_transferred / (double) transfer->total_size)
                                               : 0,
                                               transfer->name);
		
		wi_terminal_adjust_string_to_fit_width(wr_terminal, string);
		wi_mutable_string_delete_characters_from_index(string, wi_string_length(string) - wi_string_length(status));
		wi_mutable_string_append_string(string, status);
		
		wi_terminal_printf(wr_terminal, WI_STR("%@"), string);
		
		i++;
	}
	
	wr_terminal_reset_location();
}
Exemplo n.º 5
0
wi_string_t * wi_process_hostname(wi_process_t *process) {
	char	hostname[NI_MAXHOST];
	
	if(gethostname(hostname, sizeof(hostname)) < 0)
		return NULL;
	
	return wi_string_with_cstring(hostname);
}
Exemplo n.º 6
0
void wi_error_set_libxml2_error(void) {
	xmlErrorPtr		xml_error;

	xml_error = xmlGetLastError();
	
	wi_error_set_error_with_string(WI_ERROR_DOMAIN_LIBXML2,
								   xml_error->code,
								   wi_string_by_deleting_surrounding_whitespace(wi_string_with_cstring(xml_error->message)));
}
Exemplo n.º 7
0
void wi_error_set_regex_error(regex_t *regex, int code) {
	char	string[256];
	
	regerror(code, regex, string, sizeof(string));
	
	wi_error_set_error_with_string(WI_ERROR_DOMAIN_REGEX,
								   code,
								   wi_string_with_cstring(string));
}
Exemplo n.º 8
0
void wr_client_init(void) {
	wr_server_string_encoding = wi_string_encoding_init_with_charset(
		wi_string_encoding_alloc(),
		WI_STR("UTF-8"),
		WI_STRING_ENCODING_IGNORE | WI_STRING_ENCODING_TRANSLITERATE);
	
	wr_client_set_charset(WI_STR("UTF-8"));
	
	wr_nick = wi_retain(wi_user_name());
	wr_icon = wi_data_init_with_base64(wi_data_alloc(), wi_string_with_cstring(wr_default_icon));
}
Exemplo n.º 9
0
wi_fsenumerator_status_t wi_fsenumerator_get_next_path(wi_fsenumerator_t *fsenumerator, wi_string_t **path) {
	while((fsenumerator->ftsent = wi_fts_read(fsenumerator->fts))) {
		if(fsenumerator->ftsent->fts_level == 0)
			continue;

		if(fsenumerator->ftsent->fts_name[0] == '.') {
			wi_fts_set(fsenumerator->fts, fsenumerator->ftsent, WI_FTS_SKIP);
			
			continue;
		}
		
		switch(fsenumerator->ftsent->fts_info) {
			case WI_FTS_DC:
				*path = wi_string_with_cstring(fsenumerator->ftsent->fts_path);
				wi_error_set_errno(ELOOP);

				return WI_FSENUMERATOR_ERROR;
				break;

			case WI_FTS_DNR:
			case WI_FTS_ERR:
				*path = wi_string_with_cstring(fsenumerator->ftsent->fts_path);
				wi_error_set_errno(fsenumerator->ftsent->fts_errno);

				return WI_FSENUMERATOR_ERROR;
				break;

			case WI_FTS_DP:
				continue;
				break;

			default:
				*path = wi_string_with_cstring(fsenumerator->ftsent->fts_path);

				return WI_FSENUMERATOR_PATH;
				break;
		}
	}

	return WI_FSENUMERATOR_EOF;
}
Exemplo n.º 10
0
void wi_tests_run_test(const char *name, wi_run_test_func_t *function) {
	wi_pool_t					*pool;
	wi_assert_handler_func_t	*handler;
	wi_time_interval_t			interval;
	
	if(wi_string_has_suffix(wi_string_with_cstring(name), WI_STR("initialize"))) {
		(*function)();
	} else {
		_wi_tests_current_test = _wi_test_init_with_function(_wi_test_alloc(), wi_string_with_cstring(name), function);
		
		handler = wi_assert_handler;
		wi_assert_handler = _wi_tests_assert_handler;
		
		interval = wi_time_interval();
		
		pool = wi_pool_init(wi_pool_alloc());

		if(setjmp(_wi_tests_jmp_buf) == 0)
			(*_wi_tests_current_test->function)();

		wi_release(pool);
		
		_wi_tests_current_test->interval = wi_time_interval() - interval;
		
		wi_assert_handler = handler;

		if(_wi_tests_current_test->passed) {
			wi_log_info(WI_STR("Test \"%@\" passed (%.3f seconds)"),
				_wi_tests_current_test->name, _wi_tests_current_test->interval);
			
			wi_tests_passed++;
		} else {
			wi_log_info(WI_STR("Test \"%@\" failed (%.3f seconds)"),
				_wi_tests_current_test->name, _wi_tests_current_test->interval);
			
			wi_tests_failed++;
		}
		
		wi_release(_wi_tests_current_test);
	}
}
Exemplo n.º 11
0
void wr_draw_transfers(wi_boolean_t force) {
	static wi_time_interval_t	update;
	wi_list_node_t				*node;
	wi_string_t					*string, *status;
	wr_transfer_t				*transfer;
	wi_time_interval_t			interval;
	unsigned int				i = 0;
	
	interval = wi_time_interval();
	
	if(!force && interval - update < 1.0)
		return;
	
	update = interval;
	
	wi_terminal_set_scroll(wr_terminal, wi_make_range(1 + wi_list_count(wr_transfers),
													  wi_terminal_size(wr_terminal).height - 3));
	
	WI_LIST_FOREACH(wr_transfers, node, transfer) {
		wi_terminal_move(wr_terminal, wi_make_point(0, i + 1));
		wi_terminal_clear_line(wr_terminal);
		
		if(transfer->state == WR_TRANSFER_RUNNING && interval - transfer->start_time > 0.0) {
			transfer->speed = ((double) transfer->transferred - transfer->offset) / (interval - transfer->start_time);
			
			status = wi_string_with_format(WI_STR("%@/%@, %@/s"),
				wr_files_string_for_size(transfer->transferred),
				wr_files_string_for_size(transfer->size),
				wr_files_string_for_size(transfer->speed));
		}
		else if(transfer->state == WR_TRANSFER_QUEUED) {
			status = wi_string_with_format(WI_STR("queued at %u"),
				transfer->queue);
		}
		else {
			status = wi_string_with_cstring("waiting");
		}
		
		string = wi_string_with_format(WI_STR("%u %3.0f%%  %@"),
			transfer->tid,
			transfer->size > 0
				? 100 * ((double) transfer->transferred / (double) transfer->size)
				: 0,
			transfer->name);
		
		wi_terminal_adjust_string_to_fit_width(wr_terminal, string);
		wi_string_delete_characters_from_index(string, wi_string_length(string) - wi_string_length(status));
		wi_string_append_string(string, status);
		
		wi_terminal_printf(wr_terminal, WI_STR("%@"), string);
		
		i++;
	}
Exemplo n.º 12
0
wi_string_t * wi_address_hostname(wi_address_t *address) {
	char	string[NI_MAXHOST];
	int		err;
	
	err = getnameinfo(wi_address_sa(address), wi_address_sa_length(address), string, sizeof(string), NULL, 0, NI_NAMEREQD);
	
	if(err != 0) {
		wi_error_set_error(WI_ERROR_DOMAIN_GAI, err);
		
		return NULL;
	}

	return wi_string_with_cstring(string);
}
Exemplo n.º 13
0
static wi_p7_message_t * wr_client_info_message(void) {
	wi_p7_message_t		*message;
	
	message = wi_p7_message_with_name(WI_STR("wired.client_info"), wr_p7_spec);
	wi_p7_message_set_string_for_name(message, WI_STR("Wire"), WI_STR("wired.info.application.name"));
	wi_p7_message_set_string_for_name(message, wi_string_with_cstring(WR_VERSION), WI_STR("wired.info.application.version"));
	wi_p7_message_set_uint32_for_name(message, WI_REVISION, WI_STR("wired.info.application.build"));
	wi_p7_message_set_string_for_name(message, wi_process_os_name(wi_process()), WI_STR("wired.info.os.name"));
	wi_p7_message_set_string_for_name(message, wi_process_os_release(wi_process()), WI_STR("wired.info.os.version"));
	wi_p7_message_set_string_for_name(message, wi_process_os_arch(wi_process()), WI_STR("wired.info.arch"));
	wi_p7_message_set_bool_for_name(message, false, WI_STR("wired.info.supports_rsrc"));

	return message;
}
Exemplo n.º 14
0
void wi_test_runtime_info(void) {
	_wi_runtimetest_t		*runtimetest;
	
	WI_TEST_ASSERT_EQUALS(wi_runtime_class_with_name(WI_STR("_wi_runtimetest_t")), &_wi_runtimetest_runtime_class, "");
	WI_TEST_ASSERT_EQUALS(wi_runtime_class_with_id(_wi_runtimetest_runtime_id), &_wi_runtimetest_runtime_class, "");
	
	runtimetest = _wi_runtimetest_init_with_value(_wi_runtimetest_alloc(), 42);
	
	WI_TEST_ASSERT_EQUALS(wi_runtime_id(runtimetest), _wi_runtimetest_runtime_id, "");
	WI_TEST_ASSERT_EQUALS(wi_runtime_class(runtimetest), &_wi_runtimetest_runtime_class, "");
	WI_TEST_ASSERT_EQUAL_INSTANCES(wi_runtime_class_name(runtimetest), wi_string_with_cstring(_wi_runtimetest_runtime_class.name), "");
	
	wi_release(runtimetest);
}
Exemplo n.º 15
0
wi_string_t * wi_file_sha1(wi_string_t *path, wi_file_offset_t offset) {
	static unsigned char	hex[] = "0123456789abcdef";
	FILE					*fp;
	SHA_CTX					c;
	char					buffer[WI_FILE_BUFFER_SIZE];
	unsigned char			sha1[SHA_DIGEST_LENGTH];
	char					sha1_hex[sizeof(sha1) * 2 + 1];
	size_t					bytes;
	uint32_t				i;
	wi_boolean_t			all;
	
	fp = fopen(wi_string_cstring(path), "r");
	
	if(!fp) {
		wi_error_set_errno(errno);
		
		return NULL;
	}
	
	all = (offset == 0);
	
	SHA1_Init(&c);

	while((bytes = fread(buffer, 1, sizeof(buffer), fp)) > 0) {
		if(!all)
			bytes = bytes > offset ? offset : bytes;

		SHA1_Update(&c, buffer, bytes);
		
		if(!all) {
			offset -= bytes;
			
			if(offset == 0)
				break;
		}
	}
		
	fclose(fp);

	SHA1_Final(sha1, &c);

	for(i = 0; i < SHA_DIGEST_LENGTH; i++) {
		sha1_hex[i+i]	= hex[sha1[i] >> 4];
		sha1_hex[i+i+1]	= hex[sha1[i] & 0x0F];
	}

	sha1_hex[i+i] = '\0';

	return wi_string_with_cstring(sha1_hex);
}
Exemplo n.º 16
0
wi_string_t * wi_date_string_with_format(wi_date_t *date, wi_string_t *format) {
	struct tm	tm;
	char		string[1024];
	time_t		time;
	
	time = (time_t) date->interval;

	memset(&tm, 0, sizeof(tm));
	
	localtime_r(&time, &tm);
	
	(void) strftime(string, sizeof(string), wi_string_cstring(format), &tm);
	
	return wi_string_with_cstring(string);
}
Exemplo n.º 17
0
void wi_error_set_sqlite3_error_with_description(void *db, wi_string_t *description) {
	wi_string_t		*string;
	
	string = wi_string_with_cstring(sqlite3_errmsg(db));
	
	if(description)
		string = wi_string_by_appending_format(string, WI_STR(" : %@"), description);
	
	wi_error_set_error_with_string(WI_ERROR_DOMAIN_SQLITE3,
#ifdef HAVE_SQLITE3_EXTENDED_ERRCODE
								   sqlite3_extended_errcode(db),
#else
								   sqlite3_errcode(db),
#endif
								   string);
}
Exemplo n.º 18
0
static void _wi_tests_assert_handler(const char *file, unsigned int line, wi_string_t *fmt, ...) {
	wi_string_t		*string;
	va_list			ap;
	
	va_start(ap, fmt);
	string = wi_string_init_with_format_and_arguments(wi_string_alloc(), fmt, ap);
	va_end(ap);
	
	wi_log_warn(WI_STR("%@:%u: %@"), wi_string_last_path_component(wi_string_with_cstring(file)), line, string);
	
	wi_release(string);
	
	_wi_tests_current_test->passed = false;
	
	longjmp(_wi_tests_jmp_buf, 1);
}
Exemplo n.º 19
0
void wi_error_set_libxml2_error(void) {
	wi_error_t		*error;
	wi_string_t		*string;
	xmlErrorPtr		xml_error;

	xml_error = xmlGetLastError();

	error = _wi_error_get_error();
	error->domain = WI_ERROR_DOMAIN_REGEX;
	error->code = xml_error->code;
	
	string = wi_string_by_deleting_surrounding_whitespace(wi_string_with_cstring(xml_error->message));
	
	wi_release(error->string);
	error->string = wi_retain(string);
}
Exemplo n.º 20
0
static wi_string_t * _wi_p7_message_xml_value_for_name(wi_p7_message_t *p7_message, wi_string_t *field_name) {
	wi_string_t		*string;
	xmlNodePtr		node;
	xmlChar			*content;
	
	node = _wi_p7_message_xml_node_for_name(p7_message, field_name);
	
	if(!node)
		return NULL;
	
	content = xmlNodeGetContent(node);
	
	if(!content)
		return NULL;
	
	string = wi_string_with_cstring((const char *) content);
	
	xmlFree(content);
	
	return string;
}
Exemplo n.º 21
0
wr_user_t * wr_chat_user_with_nick(wr_chat_t *chat, wi_string_t *nick) {
	wi_enumerator_t	*enumerator;
	wi_string_t		*name;
	wr_user_t       *user, *value = NULL;
	char			*cname;

	cname = ((*rl_filename_dequoting_function) ((char *) wi_string_cstring(nick), 0));
	name = wi_string_with_cstring(cname);
	free(cname);

	enumerator = wi_array_data_enumerator(chat->users_array);
	
	while((user = wi_enumerator_next_data(enumerator))) {
		if(wi_is_equal(wr_user_nick(user), name)) {
			value = user;

			break;
		}
	}

	return value;
}
Exemplo n.º 22
0
void wr_client_reload_icon(void) {
    wi_data_t 		*data;
    wi_string_t 	*icon_path;

    if(wr_icon)
        wi_release(wr_icon), wr_icon = NULL;

    if(wr_icon_path)
        wi_release(wr_icon_path), wr_icon_path = NULL;

    icon_path = wi_config_path_for_name(wd_config, WI_STR("icon path"));

    if(!wi_string_has_prefix(icon_path, WI_STR("/")))
        wr_icon_path = wi_retain(wi_string_by_appending_path_component(wi_string_by_appending_path_component(wi_user_home(), WI_STR(".wirebot")), icon_path));
    else {
        wr_icon_path = wi_retain(icon_path);
    }

    if(wi_fs_path_exists(wr_icon_path, false))	{
        wr_icon = wi_data_init_with_contents_of_file(wi_data_alloc(), wr_icon_path);
    } else {
        wr_icon = wi_data_init_with_base64(wi_data_alloc(), wi_string_with_cstring(wr_default_icon));
    }
}
Exemplo n.º 23
0
static void wr_command_open(wi_array_t *arguments) {
	wi_mutable_array_t		*argv;
	wi_string_t				*login, *password, *host;
	wi_url_t				*url;
	const char				**xargv;
	wi_uinteger_t			port;
	int						ch;
	
	url			= NULL;
	login		= NULL;
	password	= NULL;
	port		= 0;

	argv = wi_autorelease(wi_mutable_copy(arguments));
	
	wi_mutable_array_insert_data_at_index(argv, WI_STR("open"), 0);
	
	xargv = wi_array_create_argv(argv);
	
	wi_getopt_reset();

	while((ch = getopt(wi_array_count(argv), (char **) xargv, "hl:P:p:")) != -1) {
		switch(ch) {
			case 'l':
				login = wi_string_with_cstring(optarg);
				break;

			case 'P':
				port = wi_string_uinteger(wi_string_with_cstring(optarg));
				break;

			case 'p':
				password = wi_string_with_cstring(optarg);
				break;

			case '?':
			case 'h':
			default:
				wr_commands_print_usage_for_command(WI_STR("open"));

				return;
				break;
		}
	}
	
	wi_array_destroy_argv(wi_array_count(argv), xargv);
	
	if((wi_uinteger_t) optind >= wi_array_count(argv)) {
		wr_commands_print_usage_for_command(WI_STR("open"));
		
		return;
	}
	
	url = wi_autorelease(wi_url_init_with_string(wi_url_alloc(), WI_ARRAY(argv, optind)));
	host = wi_url_host(url);
	
	if(port == 0)
		port = wi_url_port(url);
	
	if(wi_string_length(host) == 0) {
		wr_commands_print_usage_for_command(WI_STR("open"));
		
		return;
	}
	
	wr_client_connect(host, port, login, password);
}
Exemplo n.º 24
0
void wr_draw_divider(void) {
	wi_enumerator_t			*enumerator;
	wi_mutable_string_t		*string, *action;
	wi_string_t				*position;
	wr_window_t				*window;
	wi_size_t				size;
	wi_range_t				scroll;
	wi_point_t				location;
	const char				*color;
	wi_uinteger_t			users, line, lines, windows;
	
	string = wi_mutable_string_with_format(WI_STR("%s%@"), WR_PREFIX, wr_nick);
	
	if(wr_connected && wr_server) {
		users = wi_array_count(wr_chat_users(wr_console_window->chat));
		
		wi_mutable_string_append_format(string, WI_STR(" - %@ - %u %@"),
										wr_server_name(wr_server),
										users,
										users == 1
											? WI_STR("user")
											: WI_STR("users"));
	}
	
	action = wi_mutable_string();
	windows = 0;
	
	enumerator = wi_array_data_enumerator(wr_windows);
	
	while((window = wi_enumerator_next_data(enumerator))) {
		switch(window->status) {
			case WR_WINDOW_STATUS_ACTION:
				color = WR_INTERFACE_COLOR;
				break;

			case WR_WINDOW_STATUS_CHAT:
				color = WR_STATUS_COLOR;
				break;

			case WR_WINDOW_STATUS_HIGHLIGHT:
				color = WR_HIGHLIGHT_COLOR;
				break;
				
			default:
				color = NULL;
				break;
		}
		
		if(color) {
			if(windows > 0)
				wi_mutable_string_append_string(action, WI_STR(","));
			
			wi_mutable_string_append_format(action, WI_STR("%s%s%s%u%s%s"),
				WR_TERMINATE_COLOR,
				WR_INTERFACE_COLOR,
				color,
				window->wid,
				WR_TERMINATE_COLOR,
				WR_INTERFACE_COLOR);
			
			windows++;
		}
	}

	if(windows > 0)
		wi_mutable_string_append_format(string, WI_STR(" [%@]"), action);
	
	line = wi_terminal_buffer_current_line(wr_current_window->buffer);
	lines = wi_terminal_buffer_lines(wr_current_window->buffer);
	scroll = wi_terminal_scroll(wr_terminal);
	
	if(lines == 0 || line == lines)
		position = NULL;
	else if(line <= scroll.length)
		position = wi_string_with_cstring("TOP");
	else
		position = wi_string_with_format(WI_STR("%.0f%%"), 100 * ((double) (line - scroll.length)  / (double) lines));
	
	wi_terminal_adjust_string_to_fit_width(wr_terminal, string);
	
	if(position) {
		wi_mutable_string_delete_characters_from_index(string, wi_string_length(string) - wi_string_length(position));
		wi_mutable_string_append_string(string, position);
	}

	size = wi_terminal_size(wr_terminal);

	location = wi_terminal_location(wr_terminal);
	wi_terminal_move_printf(wr_terminal, wi_make_point(0, size.height - 3), WI_STR("%s%@%s"),
		WR_INTERFACE_COLOR,
		string,
		WR_TERMINATE_COLOR);
	wi_terminal_move(wr_terminal, location);
}
Exemplo n.º 25
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;
}
Exemplo n.º 26
0
wi_dictionary_t * wi_sqlite3_fetch_statement_results(wi_sqlite3_database_t *database, wi_sqlite3_statement_t *statement) {
	wi_mutable_dictionary_t		*results;
	wi_runtime_instance_t		*instance;
	int							i, count, length, result;
	
	wi_recursive_lock_lock(database->lock);
	
	result = sqlite3_step(statement->statement);
	
	switch(result) {
		case SQLITE_DONE:
			results = wi_dictionary();

			sqlite3_finalize(statement->statement);
			statement->statement = NULL;
			break;
			
		case SQLITE_ROW:
			results			= wi_mutable_dictionary();
			count			= sqlite3_column_count(statement->statement);
			
			for(i = 0; i < count; i++) {
				switch(sqlite3_column_type(statement->statement, i)) {
					case SQLITE_INTEGER:
						instance	= wi_number_with_int64(sqlite3_column_int64(statement->statement, i));
						break;
						
					case SQLITE_FLOAT:
						instance	= wi_number_with_double(sqlite3_column_double(statement->statement, i));
						break;
						
					case SQLITE_TEXT:
						instance	= wi_string_with_cstring((const char *) sqlite3_column_text(statement->statement, i));
						break;
						
					case SQLITE_BLOB:
						length		= sqlite3_column_bytes(statement->statement, i);
						instance	= wi_data_with_bytes(sqlite3_column_blob(statement->statement, i), length);
						break;
						
					case SQLITE_NULL:
						instance	= wi_null();
						break;
					
					default:
						instance	= NULL;
						break;
				}
				
				if(instance)
					wi_mutable_dictionary_set_data_for_key(results, instance, wi_string_with_cstring(sqlite3_column_name(statement->statement, i)));
			}
	
			wi_runtime_make_immutable(results);
			break;
			
		default:
			wi_error_set_sqlite3_error_with_description(database->database, wi_description(statement));

			sqlite3_finalize(statement->statement);
			statement->statement = NULL;

			results = NULL;
			break;
	}
	
	wi_recursive_lock_unlock(database->lock);
		
	return results;
}
Exemplo n.º 27
0
int main(int argc, const char **argv) {
	wi_pool_t			*pool;
	wi_string_t			*user, *password, *root_path;
	wi_mutable_url_t	*url;
	int					ch;
	
	wi_initialize();
	wi_load(argc, argv);
	
	wi_log_tool 	= true;
	wi_log_level 	= WI_LOG_DEBUG;
	
	pool			= wi_pool_init(wi_pool_alloc());
	
	user 			= WI_STR("guest");
	password		= WI_STR("");
	root_path		= WI_STR(WD_ROOT);
	
	while((ch = getopt(argc, (char * const *) argv, "d:p:u:")) != -1) {
		switch(ch) {
			case 'd':
				root_path = wi_string_with_cstring(optarg);
				break;
				
			case 'p':
				password = wi_string_with_cstring(optarg);
				break;
				
			case 'u':
				user = wi_string_with_cstring(optarg);
				break;
				
			case '?':
			case 'h':
			default:
				wc_usage();
				break;
		}
	}
	
	argc -= optind;
	argv += optind;
	
	if(argc != 1)
		wc_usage();
	
	if(!wi_fs_change_directory(root_path))
		wi_log_fatal(WI_STR("Could not change directory to %@: %m"), root_path);
	
	wc_spec = wi_p7_spec_init_with_file(wi_p7_spec_alloc(), WI_STR("wired.xml"), WI_P7_CLIENT);
	
	if(!wc_spec)
		wi_log_fatal(WI_STR("Could not open wired.xml: %m"));
	
	url = wi_url_init_with_string(wi_mutable_url_alloc(), wi_string_with_cstring(argv[0]));
	wi_mutable_url_set_scheme(url, WI_STR("wired"));
	
	if(!url)
		wc_usage();
	
	wi_mutable_url_set_user(url, user);
	wi_mutable_url_set_password(url, password);
	
	if(wi_url_port(url) == 0)
		wi_mutable_url_set_port(url, 4871);
	
	if(!wi_url_is_valid(url))
		wc_usage();
	
	signal(SIGPIPE, SIG_IGN);
	
	wc_test(url, 10, WI_STR("/transfertest"));
	
	wi_release(pool);
	
	return 0;
}
Exemplo n.º 28
0
int main(int argc, const char **argv) {
	wi_mutable_array_t		*arguments;
	wi_pool_t				*pool;
	wi_string_t				*string, *root_path;
	int						ch, facility;
	wi_boolean_t			test_config, daemonize, change_directory, switch_user;

	/* init libwired */
	wi_initialize();
	wi_load(argc, argv);
	
	pool					= wi_pool_init(wi_pool_alloc());
	wi_log_syslog			= true;
	wi_log_syslog_facility	= LOG_DAEMON;

	/* init core systems */
	wt_version_init();
	wt_status_lock			= wi_lock_init(wi_lock_alloc());
	wt_start_date			= wi_date_init(wi_date_alloc());
	
	/* set defaults */
	root_path				= WI_STR(WT_ROOT);
	wi_settings_config_path	= wi_string_init_with_cstring(wi_string_alloc(), WT_CONFIG_PATH);
	test_config				= false;
	daemonize				= true;
	change_directory		= true;
	switch_user				= true;

	/* init reexec argument list */
	arguments				= wi_array_init(wi_mutable_array_alloc());

	/* parse command line switches */
	while((ch = getopt(argc, (char * const *) argv, "46Dd:f:hi:L:ls:tuVvXx")) != -1) {
		switch(ch) {
			case '4':
				wt_address_family = WI_ADDRESS_IPV4;
				break;

			case '6':
				wt_address_family = WI_ADDRESS_IPV6;
				break;

			case 'D':
				daemonize = false;
				wi_log_stderr = true;
				break;

			case 'd':
				root_path = wi_string_with_cstring(optarg);
				break;

			case 'f':
				wi_release(wi_settings_config_path);
				wi_settings_config_path = wi_string_init_with_cstring(wi_string_alloc(), optarg);
				break;

			case 'i':
				wi_log_limit = wi_string_uint32(wi_string_with_cstring(optarg));
				break;

			case 'L':
				wi_log_syslog = false;
				wi_log_file = true;

				wi_release(wi_log_path);
				wi_log_path = wi_string_init_with_cstring(wi_string_alloc(), optarg);
				break;

			case 'l':
				wi_log_level++;
				break;

			case 's':
				string = wi_string_with_cstring(optarg);
				facility = wi_log_syslog_facility_with_name(string);
				
				if(facility < 0)
					wi_log_fatal(WI_STR("Could not find syslog facility \"%@\": %m"), string);
				
				wi_log_syslog_facility = facility;
				break;

			case 't':
				test_config = true;
				break;

			case 'u':
				break;

			case 'V':
			case 'v':
				wt_version();
				break;
				
			case 'X':
				daemonize = false;
				break;
				
			case 'x':
				daemonize = false;
				change_directory = false;
				switch_user = false;
				break;
			
			case '?':
			case 'h':
			default:
				wt_usage();
				break;
		}
		
		wi_mutable_array_add_data(arguments, wi_string_with_format(WI_STR("-%c"), ch));
		
		if(optarg)
			wi_mutable_array_add_data(arguments, wi_string_with_cstring(optarg));
	}
	
	/* detach */
	if(daemonize) {
		wi_mutable_array_add_data(arguments, WI_STR("-X"));
		
		switch(wi_fork()) {
			case -1:
				wi_log_fatal(WI_STR("Could not fork: %m"));
				break;
				
			case 0:
				if(!wi_execv(wi_string_with_cstring(argv[0]), arguments))
					wi_log_fatal(WI_STR("Could not execute %s: %m"), argv[0]);
				break;
				
				default:
				_exit(0);
				break;
		}
	}
	
	wi_release(arguments);
	
	/* change directory */
	if(change_directory) {
		if(!wi_fs_change_directory(root_path))
			wi_log_error(WI_STR("Could not change directory to %@: %m"), root_path);
	}
	
	/* open log */
	wi_log_open();

	/* init subsystems */
	wt_ssl_init();
	wt_clients_init();
	wt_servers_init();

	/* read the config file */
	wt_settings_init();

	if(!wt_settings_read_config())
		exit(1);

	/* apply settings */
	wt_settings_apply_settings();

	if(test_config) {
		printf("Config OK\n");

		exit(0);
	}

	/* dump command line */
	wi_log_info(WI_STR("Started as %@ %@"),
		wi_process_path(wi_process()),
		wi_array_components_joined_by_string(wi_process_arguments(wi_process()), WI_STR(" ")));

	/* init tracker */
	wi_log_info(WI_STR("Starting Wired Tracker version %@"), wt_version_string);
	wt_tracker_init();

	/* switch user/group */
	if(switch_user)
		wi_switch_user(wt_settings.user, wt_settings.group);
		
	/* create tracker threads after privilege drop */
	wt_signals_init();
	wt_block_signals();
	wt_servers_schedule();
	wt_tracker_create_threads();
	wt_write_pid();
	wt_write_status(true);
	
	/* clean up pool after startup */
	wi_pool_drain(pool);
	
	/* enter the signal handling thread in the main thread */
	wt_signal_thread(NULL);

	/* dropped out */
	wt_cleanup();
	wi_log_close();
	wi_release(pool);

	return 0;
}
Exemplo n.º 29
0
int main(int argc, const char **argv) {
	wi_pool_t		*pool;
	wi_string_t		*user, *password;
	wi_url_t		*url;
	int				ch;
	
	wi_initialize();
	wi_load(argc, argv);
	
	wi_log_tool 	= true;
	wi_log_level 	= WI_LOG_DEBUG;
	
	pool			= wi_pool_init(wi_pool_alloc());
	
	user 			= WI_STR("guest");
	password		= WI_STR("");
	
	wc_spec			= wi_p7_spec_init_with_file(wi_p7_spec_alloc(), WI_STR("wired.xml"), WI_P7_CLIENT);
	
	while((ch = getopt(argc, (char * const *) argv, "D:U:c:p:u:")) != -1) {
		switch(ch) {
			case 'p':
				password = wi_string_with_cstring(optarg);
				break;
				
			case 'u':
				user = wi_string_with_cstring(optarg);
				break;
				
			case '?':
			case 'h':
			default:
				wc_usage();
				break;
		}
	}
	
	argc -= optind;
	argv += optind;
	
	if(argc != 1)
		wc_usage();
	
	url = wi_url_init_with_string(wi_url_alloc(), wi_string_with_cstring(argv[0]));
	wi_url_set_scheme(url, WI_STR("wired"));
	
	if(!url)
		wc_usage();
	
	wi_url_set_user(url, user);
	wi_url_set_password(url, password);
	
	if(wi_url_port(url) == 0)
		wi_url_set_port(url, 4871);
	
	if(!wi_url_is_valid(url))
		wc_usage();
	
	signal(SIGPIPE, SIG_IGN);
	
	wc_client(url);
	
	wi_release(pool);
	
	return 0;
}
Exemplo n.º 30
0
void wi_test_string_compare(void) {
	WI_TEST_ASSERT_TRUE(wi_is_equal(wi_string_with_cstring("hello world"), wi_string_with_cstring("hello world")), "");
	WI_TEST_ASSERT_FALSE(wi_is_equal(wi_string_with_cstring("hello world"), wi_string_with_cstring("hello another world")), "");
	WI_TEST_ASSERT_TRUE(wi_string_compare(wi_string_with_cstring("hello world"), wi_string_with_cstring("hello world")) == 0, "");
	WI_TEST_ASSERT_FALSE(wi_string_compare(wi_string_with_cstring("hello world"), wi_string_with_cstring("Hello world")) == 0, "");
	WI_TEST_ASSERT_FALSE(wi_string_compare(wi_string_with_cstring("hello world"), wi_string_with_cstring("hello another world")) == 0, "");
	WI_TEST_ASSERT_TRUE(wi_string_case_insensitive_compare(wi_string_with_cstring("hello world"), wi_string_with_cstring("Hello world")) == 0, "");
	WI_TEST_ASSERT_FALSE(wi_string_case_insensitive_compare(wi_string_with_cstring("hello world"), wi_string_with_cstring("Hello another world")) == 0, "");
}