示例#1
0
文件: tts.c 项目: sardemff7/eventd
static EventdPluginAction *
_eventd_tts_action_parse(EventdPluginContext *context, GKeyFile *config_file)
{
    gboolean disable = FALSE;
    FormatString *message = NULL;

    if ( ! g_key_file_has_group(config_file, "TTS") )
        return NULL;

    if ( evhelpers_config_key_file_get_boolean(config_file, "TTS", "Disable", &disable) < 0 )
        return NULL;

    if ( disable )
        return NULL;

    if ( evhelpers_config_key_file_get_locale_format_string_with_default(config_file, "TTS", "Message", NULL, "${message}", &message) < 0 )
        return NULL;

    EventdPluginAction *action;
    action = g_slice_new(EventdPluginAction);
    action->message = message;

    context->actions = g_slist_prepend(context->actions, action);

    return action;
}
示例#2
0
static EventdPluginAction *
_eventd_exec_action_parse(EventdPluginContext *context, GKeyFile *config_file)
{
    gboolean disable;
    FormatString *command = NULL;

    if ( ! g_key_file_has_group(config_file, "Exec") )
        return NULL;

    if ( evhelpers_config_key_file_get_boolean(config_file, "Exec", "Disable", &disable) < 0 )
        return NULL;

    if ( disable )
        return NULL;

    if ( evhelpers_config_key_file_get_format_string(config_file, "Exec", "Command", &command) < 0 )
        return NULL;

    EventdPluginAction *action;
    action = g_slice_new(EventdPluginAction);
    action->command = command;

    context->actions = g_slist_prepend(context->actions, action);

    return action;
}
示例#3
0
文件: events.c 项目: sardemff7/eventd
static void
_eventd_events_parse_group(EventdEvents *self, const gchar *group, GKeyFile *config_file)
{
    gchar *name = NULL;

    const gchar *id, *s;
    id = group + strlen("Event ");
    if ( ( s = g_utf8_strchr(id, -1, ' ') ) != NULL )
    {
        const gchar *e = s;
        s = g_utf8_next_char(s);
        gunichar c = g_utf8_get_char(s);
        if ( c == '*' )
        {
            c = g_utf8_get_char(g_utf8_next_char(s));
            if ( ( c != ' ' ) && ( c != '\0' ) )
            {
                g_warning("Wrong event specification '%s': * should be alone", id);
                return;
            }
            name = g_strndup(id, e - id);
        }
        else if ( ( s = g_utf8_strchr(g_utf8_next_char(s), -1, ' ') ) != NULL )
            name = g_strndup(id, s - id);
        else
            name = g_strdup(id);
    }
    else
        name = g_strdup(id);
    g_strstrip(name);

    gboolean disable = FALSE;

    if ( ( evhelpers_config_key_file_get_boolean(config_file, group, "Disable", &disable) < 0 ) || disable )
        goto fail;

    gchar **actions = NULL;

    if ( evhelpers_config_key_file_get_string_list(config_file, group, "Actions", &actions, NULL) < 0 )
        goto fail;

    eventd_debug("Parsing event: %s", id);

    EventdEventsEvent *event;
    event = g_new0(EventdEventsEvent, 1);

    if ( actions != NULL )
    {
        gchar **action;
        for ( action = actions ; *action != NULL ; ++action )
            event->actions = g_list_prepend(event->actions, *action);
        g_free(actions);
    }


    gchar **if_data;
    gchar **if_data_matches;
    gchar **if_data_regexes;
    gchar **flags;
    gsize length;

    if ( evhelpers_config_key_file_get_string_list(config_file, group, "IfData", &if_data, NULL) == 0 )
        event->if_data = if_data;

    if ( evhelpers_config_key_file_get_string_list(config_file, group, "IfDataMatches", &if_data_matches, &length) == 0 )
    {
        gchar **if_data_match;
        EventdEventsEventDataMatch *match;
        gchar *tmp, *data, *key = NULL, *operator, *value_;
        gint accepted[2];
        GVariant *value;
        GError *error = NULL;

        event->if_data_matches = g_new0(EventdEventsEventDataMatch, length + 1);
        match = event->if_data_matches;

        for ( if_data_match = if_data_matches ; *if_data_match != NULL ; ++if_data_match )
        {
            data = *if_data_match;
            tmp = g_utf8_strchr(data, -1, ',');
            if ( tmp == NULL )
            {
                g_warning("Data matches must be of the form 'data-name,operator,value'");
                g_free(data);
                continue;
            }
            operator = g_utf8_next_char(tmp);
            *tmp = '\0';

            tmp = g_utf8_strchr(data, -1, '[');
            if ( tmp != NULL )
            {
                key = g_utf8_next_char(tmp);
                *tmp = '\0';
                tmp = g_utf8_strchr(key, -1, ']');
                if ( ( tmp == NULL ) || ( g_utf8_get_char(g_utf8_next_char(tmp)) != '\0' ) )
                {
                    g_warning("Data matches must be of the form 'data-name[key],operator,value'");
                    g_free(data);
                    continue;
                }
                *tmp = '\0';
            }

            tmp = g_utf8_strchr(operator, -1, ',');
            if ( tmp == NULL )
            {
                g_warning("Data matches must be of the form 'data-name,operator,value'");
                g_free(data);
                continue;
            }
            value_ = g_utf8_next_char(tmp);
            *tmp = '\0';

            gsize l = g_utf8_strlen(operator, -1);
            if ( ( l > 2 ) || ( l < 1 ) )
            {
                g_warning("Unsupported operator: %s", operator);
                g_free(data);
                continue;
            }
            accepted[0] = -2;
            switch ( g_utf8_get_char(g_utf8_next_char(operator)) )
            {
            case '=':
                accepted[1] = 0;
                switch ( g_utf8_get_char(operator) )
                {
                case '<':
                    accepted[0] = -1;
                break;
                case '>':
                    accepted[0] = 1;
                break;
                case '=':
                    accepted[0] = 0;
                break;
                case '!':
                    accepted[0] = -1;
                    accepted[1] = 1;
                break;
                }
            break;
            case '\0':
                switch ( g_utf8_get_char(operator) )
                {
                case '<':
                    accepted[0] = accepted[1] = -1;
                break;
                case '>':
                    accepted[0] = accepted[1] = 1;
                break;
                }
            }
            if ( accepted[0] == -2 )
            {
                g_warning("Unsupported operator: %s", operator);
                g_free(data);
                continue;
            }

            value = g_variant_parse(NULL, value_, NULL, NULL, &error);
            if ( value == NULL )
            {
                g_warning("Could not parse variant '%s': %s", value_, error->message);
                g_clear_error(&error);
                g_free(data);
                continue;
            }

            match->data = data;
            match->key = key;
            match->accepted[0] = accepted[0];
            match->accepted[1] = accepted[1];
            match->value = value;
            ++match;
        }
        match->data = NULL;
        g_free(if_data_matches);
    }

    if ( evhelpers_config_key_file_get_string_list(config_file, group, "IfDataRegex", &if_data_regexes, &length) == 0 )
    {
        gchar **if_data_regex;
        EventdEventsEventDataRegex *match;
        gchar *data, *regex_;
        GError *error = NULL;
        GRegex *regex;

        event->if_data_regexes = g_new0(EventdEventsEventDataRegex, length + 1);
        match = event->if_data_regexes;

        for ( if_data_regex = if_data_regexes ; *if_data_regex != NULL ; ++if_data_regex )
        {
            data = *if_data_regex;
            regex_ = g_utf8_strchr(data, -1, ',');
            if ( regex_ == NULL )
            {
                g_warning("Data matches must be of the form 'data-name,regex'");
                g_free(data);
                continue;
            }
            *regex_ = '\0';
            ++regex_;

            regex = g_regex_new(regex_, G_REGEX_OPTIMIZE, 0, &error);
            if ( regex == NULL )
            {
                g_warning("Could not compile regex '%s': %s", regex_, error->message);
                g_clear_error(&error);
                g_free(data);
                continue;
            }

            match->data = data;
            match->regex = regex;
            ++match;
        }
        match->data = NULL;
        g_free(if_data_regexes);
    }

    if ( evhelpers_config_key_file_get_string_list(config_file, group, "OnlyIfFlags", &flags, &length) == 0 )
        event->flags_whitelist = _eventd_events_parse_event_flags(flags, length);

    if ( evhelpers_config_key_file_get_string_list(config_file, group, "NotIfFlags", &flags, &length) == 0 )
        event->flags_blacklist = _eventd_events_parse_event_flags(flags, length);

    gint64 default_importance, importance;

    if ( ( event->if_data != NULL ) || ( event->if_data_matches != NULL ) || ( event->if_data_regexes != NULL ) || ( event->flags_whitelist != NULL ) || ( event->flags_blacklist != NULL ) )
        default_importance = 0;
    else
        default_importance = G_MAXINT64;
    if ( evhelpers_config_key_file_get_int_with_default(config_file, group, "Importance", default_importance, &importance) >= 0 )
        event->importance = importance;

    GList *list = NULL;
    gchar *old_key = NULL;
    g_hash_table_lookup_extended(self->events, name, (gpointer *)&old_key, (gpointer *)&list);
    g_hash_table_steal(self->events, name);
    g_free(old_key);
    list = g_list_insert_sorted(list, event, _eventd_events_compare_event);
    g_hash_table_insert(self->events, name, list);
    name = NULL;

fail:
    g_free(name);
}