Added number of matches
This commit is contained in:
parent
e1918e694c
commit
4b947dba10
|
@ -199,7 +199,7 @@ gm_trigger_set_actions(GmTrigger *trigger, GList *actions) {
|
||||||
trigger->actions = actions;
|
trigger->actions = actions;
|
||||||
}
|
}
|
||||||
|
|
||||||
gboolean
|
gint
|
||||||
gm_trigger_match_user(GmTrigger *trigger, gchar const *username,
|
gm_trigger_match_user(GmTrigger *trigger, gchar const *username,
|
||||||
GmTriggerConditionType condition, regmatch_t *matches, gint nmatch) {
|
GmTriggerConditionType condition, regmatch_t *matches, gint nmatch) {
|
||||||
GmTriggerData *data;
|
GmTriggerData *data;
|
||||||
|
@ -214,7 +214,7 @@ gm_trigger_match_user(GmTrigger *trigger, gchar const *username,
|
||||||
data = (GmTriggerData *)(item->data);
|
data = (GmTriggerData *)(item->data);
|
||||||
|
|
||||||
if (cmatch >= nmatch) {
|
if (cmatch >= nmatch) {
|
||||||
return FALSE;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (data->type == condition) {
|
if (data->type == condition) {
|
||||||
|
@ -230,17 +230,17 @@ gm_trigger_match_user(GmTrigger *trigger, gchar const *username,
|
||||||
cmatch++;
|
cmatch++;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
return FALSE;
|
return 0;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
return FALSE;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return TRUE;
|
return cmatch;
|
||||||
}
|
}
|
||||||
|
|
||||||
gboolean
|
gint
|
||||||
gm_trigger_match(GmTrigger *trigger, gchar *text, regmatch_t *matches,
|
gm_trigger_match(GmTrigger *trigger, gchar *text, regmatch_t *matches,
|
||||||
gint nmatch) {
|
gint nmatch) {
|
||||||
GmTriggerData *data;
|
GmTriggerData *data;
|
||||||
|
@ -250,7 +250,7 @@ gm_trigger_match(GmTrigger *trigger, gchar *text, regmatch_t *matches,
|
||||||
gint cmatch = 0, ret, offset, len;
|
gint cmatch = 0, ret, offset, len;
|
||||||
|
|
||||||
if (!trigger->conditions) {
|
if (!trigger->conditions) {
|
||||||
return FALSE;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (item = trigger->conditions; item; item = item->next) {
|
for (item = trigger->conditions; item; item = item->next) {
|
||||||
|
@ -258,7 +258,7 @@ gm_trigger_match(GmTrigger *trigger, gchar *text, regmatch_t *matches,
|
||||||
len = g_utf8_strlen(data->data, -1);
|
len = g_utf8_strlen(data->data, -1);
|
||||||
|
|
||||||
if (cmatch >= nmatch) {
|
if (cmatch >= nmatch) {
|
||||||
return FALSE;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (data->type) {
|
switch (data->type) {
|
||||||
|
@ -280,7 +280,7 @@ gm_trigger_match(GmTrigger *trigger, gchar *text, regmatch_t *matches,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
return FALSE;
|
return 0;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case TCT_NOT_CONTAINS:
|
case TCT_NOT_CONTAINS:
|
||||||
|
@ -288,7 +288,7 @@ gm_trigger_match(GmTrigger *trigger, gchar *text, regmatch_t *matches,
|
||||||
matches[cmatch].rm_so = 0;
|
matches[cmatch].rm_so = 0;
|
||||||
matches[cmatch].rm_eo = len;
|
matches[cmatch].rm_eo = len;
|
||||||
} else {
|
} else {
|
||||||
return FALSE;
|
return 0;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case TCT_NOT_BEGINS:
|
case TCT_NOT_BEGINS:
|
||||||
|
@ -296,7 +296,7 @@ gm_trigger_match(GmTrigger *trigger, gchar *text, regmatch_t *matches,
|
||||||
matches[cmatch].rm_so = 0;
|
matches[cmatch].rm_so = 0;
|
||||||
matches[cmatch].rm_eo = g_utf8_strlen(text, -1);
|
matches[cmatch].rm_eo = g_utf8_strlen(text, -1);
|
||||||
} else {
|
} else {
|
||||||
return FALSE;
|
return 0;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
|
@ -311,7 +311,7 @@ gm_trigger_match(GmTrigger *trigger, gchar *text, regmatch_t *matches,
|
||||||
matches[cmatch].rm_so = 0;
|
matches[cmatch].rm_so = 0;
|
||||||
matches[cmatch].rm_eo = g_utf8_strlen(text, -1);
|
matches[cmatch].rm_eo = g_utf8_strlen(text, -1);
|
||||||
} else {
|
} else {
|
||||||
return FALSE;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -350,16 +350,16 @@ gm_trigger_match(GmTrigger *trigger, gchar *text, regmatch_t *matches,
|
||||||
matches[cmatch].rm_so = 0;
|
matches[cmatch].rm_so = 0;
|
||||||
matches[cmatch].rm_eo = g_utf8_strlen(text, -1);
|
matches[cmatch].rm_eo = g_utf8_strlen(text, -1);
|
||||||
} else {
|
} else {
|
||||||
return FALSE;
|
return 0;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
return FALSE;
|
return 0;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return TRUE;
|
return cmatch;
|
||||||
}
|
}
|
||||||
|
|
||||||
gint
|
gint
|
||||||
|
|
|
@ -117,9 +117,9 @@ void gm_trigger_add_action(GmTrigger *trigger, GmTriggerData *action);
|
||||||
void gm_trigger_set_conditions(GmTrigger *trigger, GList *conditions);
|
void gm_trigger_set_conditions(GmTrigger *trigger, GList *conditions);
|
||||||
void gm_trigger_set_actions(GmTrigger *trigger, GList *actions);
|
void gm_trigger_set_actions(GmTrigger *trigger, GList *actions);
|
||||||
|
|
||||||
gboolean gm_trigger_match_user(GmTrigger *trigger, gchar const *username,
|
gint gm_trigger_match_user(GmTrigger *trigger, gchar const *username,
|
||||||
GmTriggerConditionType condition, regmatch_t *matches, gint nmatch);
|
GmTriggerConditionType condition, regmatch_t *matches, gint nmatch);
|
||||||
gboolean gm_trigger_match(GmTrigger *trigger, gchar *text, regmatch_t *matches,
|
gint gm_trigger_match(GmTrigger *trigger, gchar *text, regmatch_t *matches,
|
||||||
gint nmatch);
|
gint nmatch);
|
||||||
|
|
||||||
GmTriggerData *gm_trigger_data_new(gint type, gchar *data);
|
GmTriggerData *gm_trigger_data_new(gint type, gchar *data);
|
||||||
|
|
|
@ -743,7 +743,7 @@ gm_world_parse_legacy_editing_start(GmWorld *world, gchar *line) {
|
||||||
|
|
||||||
gchar *
|
gchar *
|
||||||
gm_world_triggers_subst(gchar const *data, gchar const *text,
|
gm_world_triggers_subst(gchar const *data, gchar const *text,
|
||||||
regmatch_t *matches) {
|
regmatch_t *matches, gint nummatches) {
|
||||||
gchar *tmp = (gchar *)data;
|
gchar *tmp = (gchar *)data;
|
||||||
gchar *p, *ptr, *tmp2;
|
gchar *p, *ptr, *tmp2;
|
||||||
GString *result = NULL;
|
GString *result = NULL;
|
||||||
|
@ -766,9 +766,10 @@ gm_world_triggers_subst(gchar const *data, gchar const *text,
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Replace it */
|
/* Replace it */
|
||||||
if (n < MAX_MATCHES) {
|
if (n < nummatches) {
|
||||||
tmp2 = g_strndup(g_utf8_offset_to_pointer(text,
|
tmp2 = g_strndup(g_utf8_offset_to_pointer(text,
|
||||||
matches[n].rm_so), matches[n].rm_eo);
|
matches[n].rm_so), matches[n].rm_eo);
|
||||||
|
|
||||||
g_string_append(result, tmp2);
|
g_string_append(result, tmp2);
|
||||||
g_free(tmp2);
|
g_free(tmp2);
|
||||||
}
|
}
|
||||||
|
@ -787,7 +788,7 @@ gm_world_triggers_subst(gchar const *data, gchar const *text,
|
||||||
|
|
||||||
void
|
void
|
||||||
gm_world_apply_trigger(GmWorld *world, GmTrigger *trigger, gchar const *text,
|
gm_world_apply_trigger(GmWorld *world, GmTrigger *trigger, gchar const *text,
|
||||||
regmatch_t *matches) {
|
regmatch_t *matches, gint nummatches) {
|
||||||
GList *item;
|
GList *item;
|
||||||
GmTriggerData *data;
|
GmTriggerData *data;
|
||||||
gint i, nargs;
|
gint i, nargs;
|
||||||
|
@ -814,18 +815,21 @@ gm_world_apply_trigger(GmWorld *world, GmTrigger *trigger, gchar const *text,
|
||||||
gdk_beep();
|
gdk_beep();
|
||||||
break;
|
break;
|
||||||
case TAT_PLAY_SOUND:
|
case TAT_PLAY_SOUND:
|
||||||
tmp = gm_world_triggers_subst(data->data, text, matches);
|
tmp = gm_world_triggers_subst(data->data, text, matches,
|
||||||
|
nummatches);
|
||||||
// TODO
|
// TODO
|
||||||
g_free(tmp);
|
g_free(tmp);
|
||||||
break;
|
break;
|
||||||
case TAT_NOTIFY:
|
case TAT_NOTIFY:
|
||||||
tmp = gm_world_triggers_subst(data->data, text, matches);
|
tmp = gm_world_triggers_subst(data->data, text, matches,
|
||||||
|
nummatches);
|
||||||
g_signal_emit(world, world_signals[NOTIFY_MESSAGE], 0, tmp);
|
g_signal_emit(world, world_signals[NOTIFY_MESSAGE], 0, tmp);
|
||||||
g_free(tmp);
|
g_free(tmp);
|
||||||
break;
|
break;
|
||||||
case TAT_RUN_SCRIPT:
|
case TAT_RUN_SCRIPT:
|
||||||
#ifdef HAVE_RUBY
|
#ifdef HAVE_RUBY
|
||||||
tmp = gm_world_triggers_subst(data->data, text, matches);
|
tmp = gm_world_triggers_subst(data->data, text, matches,
|
||||||
|
nummatches);
|
||||||
argstr = g_utf8_strchr(tmp, -1, ' ');
|
argstr = g_utf8_strchr(tmp, -1, ' ');
|
||||||
|
|
||||||
if (argstr) {
|
if (argstr) {
|
||||||
|
@ -840,7 +844,8 @@ gm_world_apply_trigger(GmWorld *world, GmTrigger *trigger, gchar const *text,
|
||||||
#endif
|
#endif
|
||||||
break;
|
break;
|
||||||
case TAT_RUN:
|
case TAT_RUN:
|
||||||
tmp = gm_world_triggers_subst(data->data, text, matches);
|
tmp = gm_world_triggers_subst(data->data, text, matches,
|
||||||
|
nummatches);
|
||||||
|
|
||||||
if (g_shell_parse_argv(tmp, &nargs, &spawn_args, NULL)) {
|
if (g_shell_parse_argv(tmp, &nargs, &spawn_args, NULL)) {
|
||||||
g_spawn_async(NULL, spawn_args, NULL, G_SPAWN_SEARCH_PATH,
|
g_spawn_async(NULL, spawn_args, NULL, G_SPAWN_SEARCH_PATH,
|
||||||
|
@ -861,14 +866,15 @@ gm_world_process_triggers(GmWorld *world, gchar *text) {
|
||||||
GList const *item;
|
GList const *item;
|
||||||
GmTrigger *trigger;
|
GmTrigger *trigger;
|
||||||
regmatch_t matches[MAX_MATCHES];
|
regmatch_t matches[MAX_MATCHES];
|
||||||
|
gint num;
|
||||||
|
|
||||||
for (item = gm_triggers_list(world->priv->triggers); item;
|
for (item = gm_triggers_list(world->priv->triggers); item;
|
||||||
item = item->next) {
|
item = item->next) {
|
||||||
trigger = (GmTrigger *)(item->data);
|
trigger = (GmTrigger *)(item->data);
|
||||||
|
|
||||||
if (trigger->event == TT_OUTPUT) {
|
if (trigger->event == TT_OUTPUT) {
|
||||||
if (gm_trigger_match(trigger, text, matches, MAX_MATCHES)) {
|
if ((num = gm_trigger_match(trigger, text, matches, MAX_MATCHES))) {
|
||||||
gm_world_apply_trigger(world, trigger, text, matches);
|
gm_world_apply_trigger(world, trigger, text, matches, num);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -136,7 +136,7 @@ void gm_world_writeln(GmWorld *world, gchar *text);
|
||||||
void gm_world_process_input(GmWorld *world, gchar *text);
|
void gm_world_process_input(GmWorld *world, gchar *text);
|
||||||
void gm_world_log(GmWorld *world, GmLogType type, gchar *text);
|
void gm_world_log(GmWorld *world, GmLogType type, gchar *text);
|
||||||
void gm_world_apply_trigger(GmWorld *world, GmTrigger *trigger,
|
void gm_world_apply_trigger(GmWorld *world, GmTrigger *trigger,
|
||||||
gchar const *text, regmatch_t *matches);
|
gchar const *text, regmatch_t *matches, gint nummatches);
|
||||||
|
|
||||||
G_END_DECLS
|
G_END_DECLS
|
||||||
#endif /* __GM_WORLD_H__ */
|
#endif /* __GM_WORLD_H__ */
|
||||||
|
|
Reference in New Issue