void wr_wprint_me(wr_window_t *window, wi_string_t *nick, wi_string_t *chat) { wi_string_t *prefix; const char *color; wi_uinteger_t length; length = wi_string_length(wr_nick); if(length > 4) length = 4; prefix = wi_string_substring_to_index(wr_nick, length); if(wi_string_has_prefix(chat, prefix)) { color = WR_HIGHLIGHT_COLOR; if(window->status < WR_WINDOW_STATUS_HIGHLIGHT) window->status = WR_WINDOW_STATUS_HIGHLIGHT; } else { color = NULL; if(window->status < WR_WINDOW_STATUS_CHAT) window->status = WR_WINDOW_STATUS_CHAT; } wr_wprintf(window, WI_STR("%s*%s %s%@%s %@"), WR_ME_COLOR, WR_TERMINATE_COLOR, color ? color : "", nick, color ? WR_TERMINATE_COLOR : "", chat); }
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; }
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; }
wi_date_t * wi_date_init_with_string(wi_date_t *date, wi_string_t *string, wi_string_t *format) { wi_regexp_t *regexp; wi_string_t *substring; struct tm tm; time_t clock; wi_uinteger_t count, offset, hours, minutes; memset(&tm, 0, sizeof(tm)); if(!strptime(wi_string_utf8_string(string), wi_string_utf8_string(format), &tm)) { wi_release(date); return NULL; } offset = 0; if(wi_string_contains_string(format, WI_STR("%z"), WI_STRING_CASE_INSENSITIVE)) { regexp = wi_regexp_with_pattern(WI_STR("((\\+|\\-)[0-9]{4})"), 0); substring = wi_regexp_string_of_first_match_in_string(regexp, string); if(substring) { hours = wi_string_uinteger(wi_string_substring_with_range(substring, wi_make_range(1, 2))); minutes = wi_string_uinteger(wi_string_substring_with_range(substring, wi_make_range(3, 2))); offset = (hours * 3600) + (minutes * 60); if(wi_string_has_prefix(substring, WI_STR("-"))) offset = -offset; } } clock = wi_timegm(&tm) - offset; return wi_date_init_with_time(date, clock); }
static wi_p7_message_t * _wi_p7_socket_read_xml_message(wi_p7_socket_t *p7_socket, wi_time_interval_t timeout, wi_string_t *prefix) { wi_string_t *string; wi_p7_message_t *p7_message; p7_message = wi_autorelease(wi_p7_message_init(wi_p7_message_alloc(), p7_socket)); while(true) { string = wi_socket_read_to_string(p7_socket->socket, timeout, WI_STR(">")); if(!string || wi_string_length(string) == 0) return NULL; wi_string_delete_surrounding_whitespace(string); if(!p7_message->xml_string) p7_message->xml_string = wi_copy(string); else wi_string_append_string(p7_message->xml_string, string); if(wi_string_has_suffix(string, WI_STR("</p7:message>")) || (wi_string_has_suffix(string, WI_STR("/>")) && wi_string_has_prefix(string, WI_STR("<p7:message")))) { break; } } wi_retain(p7_message->xml_string); if(prefix) wi_string_insert_string_at_index(p7_message->xml_string, prefix, 0); wi_string_delete_surrounding_whitespace(p7_message->xml_string); return p7_message; }
void wr_wprint_say(wr_window_t *window, wi_string_t *nick, wi_string_t *chat) { wi_string_t *prefix; const char *color; unsigned int length; length = wi_string_length(wr_nick); if(length > 4) length = 4; prefix = wi_string_substring_to_index(wr_nick, length); if(wi_string_has_prefix(chat, prefix)) { color = WR_HIGHLIGHT_COLOR; if(window->status < WR_WINDOW_STATUS_HIGHLIGHT) window->status = WR_WINDOW_STATUS_HIGHLIGHT; } else { color = WR_NICK_COLOR; if(window->status < WR_WINDOW_STATUS_CHAT) window->status = WR_WINDOW_STATUS_CHAT; } wr_wprintf(window, WI_STR("%s<%s%s%@%s%s>%s %@"), WR_SAY_COLOR, WR_END_COLOR, color, nick, WR_END_COLOR, WR_SAY_COLOR, WR_END_COLOR, chat); }
void wr_commands_parse_command(wi_string_t *buffer, wi_boolean_t chat) { wi_array_t *array = NULL; wi_string_t *command = NULL, *arguments; wi_uinteger_t index; if(chat && !wi_string_has_prefix(buffer, WI_STR("/"))) buffer = wi_string_by_inserting_string_at_index(buffer, wr_connected ? WI_STR("say ") : WI_STR("echo "), 0); wr_commands_split_command(buffer, &command, &arguments); index = wr_commands_index_for_command(command); if(index == WI_NOT_FOUND) { wi_log_error(WI_STR("%@: Command not recognized"), command); return; } if(wr_commands[index].connected && !wr_connected) { wi_log_error(WI_STR("%@: %@"), command, WI_STR("Not connected")); return; } array = wi_autorelease(wi_array_init_with_argument_string(wi_array_alloc(), arguments, wr_commands[index].optindex)); if(wi_array_count(array) < wr_commands[index].optargs) { wr_commands_print_usage_for_command(command); return; } ((*wr_commands[index].action) (array)); }
wi_string_t * wi_file_read_config_line(wi_file_t *file) { wi_string_t *string; while((string = wi_file_read_line(file))) { if(wi_string_length(string) == 0 || wi_string_has_prefix(string, WI_STR("#"))) continue; return string; } return NULL; }
void wi_parse_wire_command(wi_string_t *buffer, wi_string_t **out_command, wi_string_t **out_arguments) { wi_uinteger_t index; index = wi_string_index_of_string(buffer, WI_STR(" "), 0); if(index != WI_NOT_FOUND) { *out_command = wi_string_substring_to_index(buffer, index); *out_arguments = wi_string_substring_from_index(buffer, index + 1); } else { *out_command = wi_autorelease(wi_copy(buffer)); *out_arguments = wi_string(); } if(wi_string_has_prefix(*out_command, WI_STR("/"))) *out_command = wi_string_by_deleting_characters_to_index(*out_command, 1); }
wi_boolean_t _wi_settings_set_path(wi_settings_t *settings, uint32_t index, wi_string_t *name, wi_string_t *value) { wi_string_t **string = (wi_string_t **) settings->spec[index].setting; if(*string) wi_release(*string); if(wi_string_has_prefix(value, WI_STR("/"))) *string = wi_retain(value); else if(settings->chroot) *string = wi_string_init_with_format(wi_string_alloc(), WI_STR("/%@"), value); else *string = wi_string_init_with_format(wi_string_alloc(), WI_STR("%@/%@"), wi_root_path, value); wi_string_normalize_path(*string); return true; }
wr_completer_t wr_commands_completer_for_command(wi_string_t *buffer) { wi_string_t *command, *arguments; wr_completer_t completer; wi_uinteger_t index; if(wi_string_length(buffer) == 0 || !wi_string_has_prefix(buffer, WI_STR("/"))) return WR_COMPLETER_NICKNAME; if(wi_string_index_of_string(buffer, WI_STR(" "), 0) == WI_NOT_FOUND) return WR_COMPLETER_COMMAND; wi_parse_wire_command(buffer, &command, &arguments); index = wr_commands_index_for_command(command); if(index == WI_NOT_FOUND) completer = WR_COMPLETER_NONE; else completer = wr_commands[index].completer; return completer; }
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)); } }
wi_boolean_t wi_config_write_file(wi_config_t *config) { wi_enumerator_t *enumerator; wi_file_t *file, *tmpfile; wi_string_t *string, *name, *value; wi_mutable_set_t *keys; wi_boolean_t write; file = wi_file_for_updating(config->path); if(!file) { wi_log_err(WI_STR("Could not open %@: %m"), config->path); return false; } tmpfile = wi_file_temporary_file(); wi_lock_lock(config->lock); keys = wi_autorelease(wi_mutable_copy(config->changes)); while((string = wi_file_read_line(file))) { if(wi_string_length(string) == 0) { wi_file_write_format(tmpfile, WI_STR("\n")); } else if(wi_string_has_prefix(string, WI_STR("#"))) { write = true; string = wi_string_substring_from_index(string, 1); if(_wi_config_parse_string(config, string, &name, &value)) { if(wi_set_contains_data(keys, name)) { if(_wi_config_write_setting_to_file(config, name, tmpfile)) { write = false; wi_mutable_set_remove_data(keys, name); } } } if(write) wi_file_write_format(tmpfile, WI_STR("#%@\n"), string); } else { write = true; if(_wi_config_parse_string(config, string, &name, &value)) { if(wi_set_contains_data(keys, name)) { if(_wi_config_write_setting_to_file(config, name, tmpfile)) { write = false; wi_mutable_set_remove_data(keys, name); } } } if(write) wi_file_write_format(tmpfile, WI_STR("%@\n"), string); } } enumerator = wi_set_data_enumerator(keys); while((name = wi_enumerator_next_data(enumerator))) _wi_config_write_setting_to_file(config, name, tmpfile); wi_mutable_set_remove_all_data(config->changes); wi_lock_unlock(config->lock); wi_file_truncate(file, 0); wi_file_seek(tmpfile, 0); while((string = wi_file_read_line(tmpfile))) wi_file_write_format(file, WI_STR("%@\n"), string); return true; }
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; }