static void run_scripts(ScriptActionType type, struct game* g, struct server* s) { GSList* l; debug(4, "%s", action_key[type]); for (l = action[type]; l; l = g_slist_next(l)) { char path[PATH_MAX] = ""; const char* cmd[3]; Script* script = l->data; GSList* dir; if (!((ScriptOption*)(script->options->data))->enable) { continue; } for (dir = scriptdirs; dir; dir = g_slist_next(dir)) { snprintf(path, sizeof(path), "%s/%s", (char*)dir->data, script->name); debug(4, "check %s", path); if (access(path, X_OK)) { if (access(path, R_OK) == 0) { xqf_warning("%s not executable, not running it", path); } path[0] = '\0'; } else break; } if (path[0]) { struct script_env_callback_data data; cmd[0] = path; cmd[1] = action_key[type]; cmd[2] = NULL; data.s = s; data.g = g; data.script = script; debug(3, "running script %s %s", cmd[0], cmd[1]); run_program_sync_callback(cmd, script_set_env, &data); } } }
static animation *tray_icon_load_animation (gchar *name, gboolean loop) { gint i=0; gint line_number=0; gboolean eof=FALSE; gchar *ani_file; gchar *content; gchar *begin; gchar *line; gchar *png_start; gchar *delay_string=NULL; gchar *png_filename; animation *ani; frame tmp_frame; if (!name) return NULL; { char* tmp = g_strconcat("trayicon", G_DIR_SEPARATOR_S, name, NULL); ani_file = find_pixmap_directory(tmp); g_free(tmp); } if (!ani_file || !g_file_get_contents (ani_file, &content, NULL, NULL)) { xqf_warning("Could not load animation file '%s'", name); g_free(ani_file); return NULL; } ani=g_new(animation, 1); ani->array = g_array_new (FALSE, FALSE, sizeof (frame)); ani->frame_counter = 0; ani->time_counter = 0; ani->loop = loop; begin=content; while (!eof) { if ((content[i] == '\n') || (content[i] == '\0') ) { if ( content[i-1] != '\n') { line_number++; do { line= g_strndup (begin, &content[i]-begin); if (strlen (line) <= 1) { xqf_warning (_("Error in file: %s line: %d\n"), ani_file, line_number); break; } png_start =g_strrstr (line, ".png"); if (!png_start) { xqf_warning (_("Error in file: %s line: %d\n"), ani_file, line_number); break; } delay_string=g_strdup (png_start+4); g_strstrip(delay_string); if (strlen (delay_string) < 1) { xqf_warning (_("Error in file: %s line: %d\n"), ani_file, line_number); break; } tmp_frame.delay= strtol(delay_string, NULL, 10); if (tmp_frame.delay== 0) { xqf_warning (_("Error in file: %s line: %d\n"), ani_file, line_number); break; } if (tmp_frame.delay != 1) tmp_frame.delay=tmp_frame.delay / 2; *(png_start+4)=0; /*cut anything behind ".png" */ png_filename= g_strconcat(PACKAGE_DATA_DIR, G_DIR_SEPARATOR_S,"default", G_DIR_SEPARATOR_S, "trayicon", G_DIR_SEPARATOR_S, line, NULL); tmp_frame.pix = load_pixmap_as_pixbuf (png_filename); if (tmp_frame.pix == NULL) { xqf_warning (_("Error in file: %s line: %d\n"), ani_file, line_number); break; } if (png_filename) g_free (png_filename); g_array_append_val (ani->array, tmp_frame); } while (0); if (line) g_free(line); if (delay_string) g_free(delay_string); } if (content[i] == '\0' || (content[i] == '\n' && content[i+1] == '\0' )) eof=TRUE; else begin=&content[i]+1; } i++; } if (content) g_free(content); if (ani_file) g_free(ani_file); return ani; }
void scripts_load() { GSList* dir; GList* s; unsigned i; char path[PATH_MAX]; if (!scriptdata) { g_datalist_init(&scriptdata); } /* g_datalist_get_data(&scriptdata, "foo"); g_datalist_set_data_full(&scriptdata,"foo",value,g_free); */ for (dir = scriptdirs; dir; dir = g_slist_next(dir)) { GList* s = dir_to_list(dir->data, script_filter); scripts = merge_sorted_string_lists(scripts, s); } for (s = scripts; s; s = g_list_next(s)) { unsigned version; config_section_iterator* sit; Script* script; const char* filename = s->data; char* errtitle = _("Script error"); // already known? if (g_datalist_get_data(&scriptdata, filename)) { continue; } script = script_new(); snprintf(path, sizeof(path), "/scripts/%s/General", filename); config_push_prefix(path); version = config_get_int("xqf version"); script->summary = config_get_string("summary"); script->author = config_get_string("author"); script->license = config_get_string("license"); config_pop_prefix(); if (version > MAX_SCRIPT_VERSION) { dialog_ok(errtitle, _("Script %s has version %d, xqf only supports version %d."), filename, version, MAX_SCRIPT_VERSION); script_free(script); continue; } if (!script->summary) { dialog_ok(errtitle, _("Script %s missing summary."), filename); script_free(script); continue; } if (!script->author) { dialog_ok(errtitle, _("Script %s missing author."), filename); script_free(script); continue; } if (!script->license) { dialog_ok(errtitle, _("Script %s missing license."), filename); script_free(script); continue; } script->name = g_strdup(filename); snprintf(path, sizeof(path), "/scripts/%s/Action", filename); config_push_prefix(path); for (i = 0; i < NUM_ACTIONS; ++i) { gboolean on = config_get_bool(action_key[i]); if (on) { action[i] = g_slist_prepend(action[i], script); } } config_pop_prefix(); // treat script property 'enabled' as option as it has a widget // so it's easier to handle later { ScriptOption* opt; snprintf(path, sizeof(path), "/" CONFIG_FILE "/scripts/%s/enabled=false", filename); opt = scriptoption_new("bool"); opt->enable = config_get_bool(path); // Translator: whether this plugin script is enabled opt->name = _("Enabled"); opt->section = g_strdup("enabled"); script->options = g_slist_prepend(script->options, opt); } snprintf(path, sizeof(path), "/scripts/%s", filename); sit = config_init_section_iterator(path); while (sit) { char* sname = NULL; sit = config_section_iterator_next(sit, &sname); if (strlen(sname) > 7 && !strncmp(sname, "option ", 7)) { char* typestr; char* name; ScriptOption* opt; char settings_path[PATH_MAX]; snprintf(settings_path, sizeof(settings_path), "/" CONFIG_FILE "/scripts/%s/%s", filename, sname); snprintf(path, sizeof(path), "/scripts/%s/%s", filename, sname); config_push_prefix(path); typestr = config_get_string("type"); name = config_get_string("name"); opt = scriptoption_new(typestr); g_free(typestr); if (!opt || !name) { xqf_warning("script %s: invalid option %s", filename, sname+7); goto next; } opt->name = name; opt->section = sname; switch(opt->type) { case SCRIPT_OPTION_TYPE_LIST: { config_key_iterator* it; it = config_init_iterator(path); if (!opt->list) { opt->list = g_ptr_array_new(); } while (it) { char* key = NULL; char* val = NULL; it = config_iterator_next(it, &key, &val); if (!strncmp(key, "value",5)) { g_ptr_array_add(opt->list, val); } else { g_free(val); } g_free(key); } } // fall through case SCRIPT_OPTION_TYPE_STRING: case SCRIPT_OPTION_TYPE_INT: { char* defval = NULL; char* curval = NULL; defval = config_get_string("default"); curval = config_get_string(settings_path); if (curval) { opt->defval = g_strdup(curval); } else if (defval) { opt->defval = g_strdup(defval); } g_free(defval); g_free(curval); } break; case SCRIPT_OPTION_TYPE_BOOL: { gboolean defval; gboolean curval; int is_deflt = 0; defval = config_get_bool("default=false"); curval = config_get_bool_with_default(settings_path, &is_deflt); if (is_deflt) { opt->enable = defval; } else { opt->enable = curval; } } break; case SCRIPT_OPTION_TYPE_INVALID: xqf_error("unreachable code"); break; } script->options = g_slist_prepend(script->options, opt); next: config_pop_prefix(); } } script->options = g_slist_reverse(script->options); g_datalist_set_data_full(&scriptdata, filename, script, (GDestroyNotify)script_free); snprintf(path, sizeof(path), "/scripts/%s", filename); config_drop_file(path); } }
void props_load (void) { FILE *f; char *fn; struct host *h; struct server_props *p = NULL; char *addr; unsigned short port; char buf[1024]; char *ptr; props_free_all (); fn = file_in_dir (user_rcdir, SERVERS_FILE); f = fopen (fn, "r"); g_free (fn); if (!f) return; while (!feof (f)) { fgets (buf, 1024, f); if (buf[0] == '\n') { if (p) p = NULL; } else if (buf[0] == '[') { if (p) continue; ptr = strchr (&buf[1], ']'); if (!ptr) continue; *ptr = '\0'; if (!parse_address (&buf[1], &addr, &port)) continue; if (port == 0) { g_free (addr); continue; } h = host_add (addr); g_free (addr); if (!h) continue; p = __properties (h, port); if (!p) p = properties_new (h, port); } else { if (!p) continue; ptr = strchr (buf, ' '); if (!ptr) continue; *ptr++ = '\0'; if (strcmp (buf, "custom_cfg") == 0) { g_free (p->custom_cfg); p->custom_cfg = strdup_strip (ptr); } else if (strcmp (buf, "password") == 0) { g_free (p->server_password); p->server_password = strdup_strip (ptr); } else if (strcmp (buf, "spectator_password") == 0) { g_free (p->spectator_password); p->spectator_password = strdup_strip (ptr); } else if (strcmp (buf, "rcon_password") == 0) { g_free (p->rcon_password); p->rcon_password = strdup_strip (ptr); } else if (strcmp (buf, "reserved_slots") == 0) { p->reserved_slots= atoi(ptr); } else if (strcmp (buf, "sucks") == 0) { p->sucks = atoi(ptr); } else if (strcmp (buf, "comment") == 0) { unsigned di, si; size_t slen = strlen(ptr); int quote = 0; g_free (p->comment); p->comment = g_malloc0 (slen + 1); for (si = 0, di = 0; si < slen; ++si) { if (quote) { quote = 0; if (ptr[si] == 'n') p->comment[di++] = '\n'; else if (ptr[si] == '\\') p->comment[di++] = '\\'; else xqf_warning("unknown control sequence \\%c", ptr[si]); } else if (ptr[si] == '\\') { quote = 1; } else { p->comment[di++] = ptr[si]; } } } } } fclose (f); }