diff --git a/modules/appmodel/CMakeLists.txt b/modules/appmodel/CMakeLists.txt index e49323bb0d4..2bb5efe5d1a 100644 --- a/modules/appmodel/CMakeLists.txt +++ b/modules/appmodel/CMakeLists.txt @@ -9,6 +9,7 @@ set (APPMODEL_SOURCES appmodel-parser.c appmodel-plugin.c appmodel-context.c + app-object-generator.c app-parser-generator.c application.c ) diff --git a/modules/appmodel/Makefile.am b/modules/appmodel/Makefile.am index d237555ce1b..f87ac90a786 100644 --- a/modules/appmodel/Makefile.am +++ b/modules/appmodel/Makefile.am @@ -10,6 +10,8 @@ modules_appmodel_libappmodel_la_SOURCES = \ modules/appmodel/appmodel-plugin.c \ modules/appmodel/appmodel-context.c \ modules/appmodel/appmodel-context.h \ + modules/appmodel/app-object-generator.c \ + modules/appmodel/app-object-generator.h \ modules/appmodel/app-parser-generator.c \ modules/appmodel/app-parser-generator.h \ modules/appmodel/application.c \ diff --git a/modules/appmodel/app-object-generator.c b/modules/appmodel/app-object-generator.c new file mode 100644 index 00000000000..9393e37e7dc --- /dev/null +++ b/modules/appmodel/app-object-generator.c @@ -0,0 +1,113 @@ +/* + * Copyright (c) 2017 Balabit + * Copyright (c) 2017 Balazs Scheidler + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + * As an additional exemption you are allowed to compile & link against the + * OpenSSL libraries as published by the OpenSSL project. See the file + * COPYING for details. + * + */ + +#include "app-object-generator.h" +#include "appmodel.h" + +#include + +gboolean +app_object_generator_is_application_included(AppObjectGenerator *self, const gchar *app_name) +{ + /* include everything if we don't have the option */ + if (!self->included_apps) + return TRUE; + return strstr(self->included_apps, app_name) != NULL; +} + +gboolean +app_object_generator_is_application_excluded(AppObjectGenerator *self, const gchar *app_name) +{ + if (!self->excluded_apps) + return FALSE; + return strstr(self->excluded_apps, app_name) != NULL; +} + +static gboolean +_parse_auto_parse_arg(AppObjectGenerator *self, CfgArgs *args, const gchar *reference) +{ + const gchar *v = cfg_args_get(args, "auto-parse"); + + if (v) + self->is_parsing_enabled = cfg_process_yesno(v); + else + self->is_parsing_enabled = TRUE; + return TRUE; +} + +static gboolean +_parse_auto_parse_exclude_arg(AppObjectGenerator *self, CfgArgs *args, const gchar *reference) +{ + const gchar *v = cfg_args_get(args, "auto-parse-exclude"); + if (!v) + return TRUE; + self->excluded_apps = g_strdup(v); + return TRUE; +} + +static gboolean +_parse_auto_parse_include_arg(AppObjectGenerator *self, CfgArgs *args, const gchar *reference) +{ + const gchar *v = cfg_args_get(args, "auto-parse-include"); + if (!v) + return TRUE; + self->included_apps = g_strdup(v); + return TRUE; +} + +gboolean +app_object_generator_parse_arguments_method(AppObjectGenerator *self, CfgArgs *args, const gchar *reference) +{ + g_assert(args != NULL); + + if (!_parse_auto_parse_arg(self, args, reference)) + return FALSE; + if (!_parse_auto_parse_exclude_arg(self, args, reference)) + return FALSE; + if (!_parse_auto_parse_include_arg(self, args, reference)) + return FALSE; + return TRUE; +} + +static gboolean +_generate(CfgBlockGenerator *s, GlobalConfig *cfg, gpointer args, GString *result, const gchar *reference) +{ + AppObjectGenerator *self = (AppObjectGenerator *) s; + CfgArgs *cfgargs = (CfgArgs *)args; + + if (!self->parse_arguments(self, cfgargs, reference)) + return FALSE; + + self->generate_config(self, cfg, result); + + return TRUE; +} + +void +app_object_generator_init_instance(AppObjectGenerator *self, gint context, const gchar *name) +{ + cfg_block_generator_init_instance(&self->super, context, name); + self->super.generate = _generate; + self->parse_arguments = app_object_generator_parse_arguments_method; +} diff --git a/modules/appmodel/app-object-generator.h b/modules/appmodel/app-object-generator.h new file mode 100644 index 00000000000..330db1344e6 --- /dev/null +++ b/modules/appmodel/app-object-generator.h @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2017 Balabit + * Copyright (c) 2017 Balazs Scheidler + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + * As an additional exemption you are allowed to compile & link against the + * OpenSSL libraries as published by the OpenSSL project. See the file + * COPYING for details. + * + */ + +#ifndef APPMODEL_APP_OBJECT_GENERATOR_H_INCLUDED +#define APPMODEL_APP_OBJECT_GENERATOR_H_INCLUDED + +#include "plugin.h" + +typedef struct _AppObjectGenerator AppObjectGenerator; + +struct _AppObjectGenerator +{ + CfgBlockGenerator super; + gboolean (*parse_arguments)(AppObjectGenerator *self, CfgArgs *args, const gchar *reference); + void (*generate_config)(AppObjectGenerator *self, GlobalConfig *cfg, GString *result); + const gchar *included_apps; + const gchar *excluded_apps; + gboolean is_parsing_enabled; +}; + +gboolean app_object_generator_is_application_included(AppObjectGenerator *self, const gchar *app_name); +gboolean app_object_generator_is_application_excluded(AppObjectGenerator *self, const gchar *app_name); + +gboolean app_object_generator_parse_arguments_method(AppObjectGenerator *self, CfgArgs *args, const gchar *reference); +void app_object_generator_init_instance(AppObjectGenerator *self, gint context, const gchar *name); + + +#endif diff --git a/modules/appmodel/app-parser-generator.c b/modules/appmodel/app-parser-generator.c index 1399d562ff1..26821d0c21d 100644 --- a/modules/appmodel/app-parser-generator.c +++ b/modules/appmodel/app-parser-generator.c @@ -22,109 +22,9 @@ * */ -#include "app-parser-generator.h" +#include "app-object-generator.h" #include "appmodel.h" -#include - -typedef struct _AppObjectGenerator AppObjectGenerator; - -struct _AppObjectGenerator -{ - CfgBlockGenerator super; - gboolean (*parse_arguments)(AppObjectGenerator *self, CfgArgs *args, const gchar *reference); - void (*generate_config)(AppObjectGenerator *self, GlobalConfig *cfg, GString *result); - const gchar *included_apps; - const gchar *excluded_apps; - gboolean is_parsing_enabled; -}; - -static gboolean -_is_application_included(AppObjectGenerator *self, const gchar *app_name) -{ - /* include everything if we don't have the option */ - if (!self->included_apps) - return TRUE; - return strstr(self->included_apps, app_name) != NULL; -} - -static gboolean -_is_application_excluded(AppObjectGenerator *self, const gchar *app_name) -{ - if (!self->excluded_apps) - return FALSE; - return strstr(self->excluded_apps, app_name) != NULL; -} - -static gboolean -_parse_auto_parse_arg(AppObjectGenerator *self, CfgArgs *args, const gchar *reference) -{ - const gchar *v = cfg_args_get(args, "auto-parse"); - - if (v) - self->is_parsing_enabled = cfg_process_yesno(v); - else - self->is_parsing_enabled = TRUE; - return TRUE; -} - -static gboolean -_parse_auto_parse_exclude_arg(AppObjectGenerator *self, CfgArgs *args, const gchar *reference) -{ - const gchar *v = cfg_args_get(args, "auto-parse-exclude"); - if (!v) - return TRUE; - self->excluded_apps = g_strdup(v); - return TRUE; -} - -static gboolean -_parse_auto_parse_include_arg(AppObjectGenerator *self, CfgArgs *args, const gchar *reference) -{ - const gchar *v = cfg_args_get(args, "auto-parse-include"); - if (!v) - return TRUE; - self->included_apps = g_strdup(v); - return TRUE; -} - - -static gboolean -app_object_generator_parse_arguments_method(AppObjectGenerator *self, CfgArgs *args, const gchar *reference) -{ - g_assert(args != NULL); - - if (!_parse_auto_parse_arg(self, args, reference)) - return FALSE; - if (!_parse_auto_parse_exclude_arg(self, args, reference)) - return FALSE; - if (!_parse_auto_parse_include_arg(self, args, reference)) - return FALSE; - return TRUE; -} - -static gboolean -_generate(CfgBlockGenerator *s, GlobalConfig *cfg, gpointer args, GString *result, const gchar *reference) -{ - AppObjectGenerator *self = (AppObjectGenerator *) s; - CfgArgs *cfgargs = (CfgArgs *)args; - - if (!self->parse_arguments(self, cfgargs, reference)) - return FALSE; - - self->generate_config(self, cfg, result); - - return TRUE; -} - -void -app_object_generator_init_instance(AppObjectGenerator *self, gint context, const gchar *name) -{ - cfg_block_generator_init_instance(&self->super, context, name); - self->super.generate = _generate; - self->parse_arguments = app_object_generator_parse_arguments_method; -} - /* app-parser() */ typedef struct _AppParserGenerator @@ -220,10 +120,10 @@ _generate_application(Application *app, gpointer user_data) if (strcmp(self->topic, app->super.instance) != 0) return; - if (!_is_application_included(&self->super, app->super.name)) + if (!app_object_generator_is_application_included(&self->super, app->super.name)) return; - if (_is_application_excluded(&self->super, app->super.name)) + if (app_object_generator_is_application_excluded(&self->super, app->super.name)) return; if (self->first_app_generated)