diff options
Diffstat (limited to 'mcin')
-rw-r--r-- | mcin/mc_regex.h | 55 | ||||
-rw-r--r-- | mcin/mcin.c | 94 | ||||
-rw-r--r-- | mcin/mcin.h | 17 |
3 files changed, 166 insertions, 0 deletions
diff --git a/mcin/mc_regex.h b/mcin/mc_regex.h new file mode 100644 index 0000000..31f5c0e --- /dev/null +++ b/mcin/mc_regex.h @@ -0,0 +1,55 @@ +#ifndef _MC_REGEX_H +#define _MC_REGEX_H +#define MC_REGEX \ +".* joined the game," \ +".* lost connection: .*," \ +"Stopping server," \ +"Starting minecraft server version .*," \ +"Done \\(.*s\\)! For help type \"help\"," \ +"There are .* of a max of .* players online:.*," \ +".* has made the advancement \\[.*\\]," \ +".* has completed the challenge \\[.*\\]," \ +".* has reached the goal \\[.*\\]," \ +".* was shot by .*," \ +".* was pummeled by .*," \ +".* was pricked to death," \ +".* walked into a cactus whilst trying to escape .*," \ +".* drowned.*," \ +".* experienced kinetic energy.*," \ +".* blew up," \ +".* was blown up by .*," \ +".* was killed by \\[Intentional Game Design\\]," \ +".* hit the ground too hard.*," \ +".* fell from a high place," \ +".* fell off.*," \ +".* fell while.*," \ +".* was squashed by a falling .*," \ +".* went up in flames," \ +".* walked into fire whilst fighting .*," \ +".* burned to death," \ +".* was burnt to a crisp whilst fighting .*," \ +".* went off with a bang.*," \ +".* tried to swim in lava.*," \ +".* was struck by lightning.*," \ +".* discovered the floor was lava," \ +".* walked into danger zone due to .*," \ +".* was killed by magic.*," \ +".* was killed by .* using .*," \ +".* froze to death.*," \ +".* was slain by .*," \ +".* was fireballed by .*," \ +".* was stung to death," \ +".* was shot by a skull from .*," \ +".* starved to death.*," \ +".* suffocated in a wall.*," \ +".* was squished too much," \ +".* was squashed by .*," \ +".* was poked to death by a sweet berry bush.*," \ +".* was killed trying to hurt .*," \ +".* was killed by .* trying to hurt .*," \ +".* was impaled by .*," \ +".* fell out of the world," \ +".* didn't want to live in the same world as.*," \ +".* withered away.*," \ +"<.*> .*" +#endif // _MC_REGEX_H diff --git a/mcin/mcin.c b/mcin/mcin.c new file mode 100644 index 0000000..1430415 --- /dev/null +++ b/mcin/mcin.c @@ -0,0 +1,94 @@ +#include "mc_regex.h" +#include "mcin.h" +#include "../common.h" + +#include <stdio.h> +#include <string.h> +#include <stdlib.h> + +int mcin_matcher_init(MCINMatcher *out) +{ + int r = 0; + r = regcomp(&out->regex_master, ".*\\[([0-9][0-9]:[0-9][0-9]:[0-9][0-9])\\] \\[(.*)\\/(.*)\\]: (.*)", REG_EXTENDED); + if(r) + { + fprintf(stderr, _("Cannot compile master regex: %d.\n"), r); + return r; + } + int line = 0; + char str[] = MC_REGEX; + char *token; + char *rest = str; + while ((token = strtok_r(rest, ",", &rest))) + { + int l = line++; + regex_t *reg = &out->regex[l]; + r = regcomp(reg, token, REG_EXTENDED); + if(r) + { + fprintf(stderr, _("Cannot compile regex %s: %d.\n"), token, r); + return r; + } + } + return r; +} + +void mcin_matcher_free(MCINMatcher *matcher) +{ + regfree(&matcher->regex_master); + for(int i = 0; i < MCIN_REGEX_COUNT; i ++) + { + regfree(&matcher->regex[i]); + } +} + +char *mcin_matcher_match(MCINMatcher *matcher, const char *str) +{ + regmatch_t pmatch[5]; + if(regexec(&matcher->regex_master, str, 5, pmatch, 0)) return 0; + char *temp_str = calloc(strlen(str) + 1, sizeof(char)); + for(int i = 1 /* Ignore the string itself */; i < 5; i ++) + { + const regmatch_t match = pmatch[i]; + if(match.rm_so == -1) + { + // Shouldn't happen if the string is valid. + free(temp_str); + return NULL; + } + if(i != 2 && i != 3 && i != 4) continue; // We don't care. + int length = match.rm_eo - match.rm_so; + for(int j = 0; j < length; j ++) + { + temp_str[j] = str[match.rm_so + j]; + } + temp_str[length] = '\0'; + if(i == 2) /* Tag */ + { + if(strcmp(temp_str, "Server thread")) + { + free(temp_str); + return NULL; + } + } + if(i == 3) /* Level */ + { + if(strcmp(temp_str, "INFO")) + { + free(temp_str); + return NULL; + } + } + if(i == 4) // Data + break; + } + for(int i = 0; i < MCIN_REGEX_COUNT; i ++) + { + if(!regexec(&matcher->regex[i], str, 0, NULL, 0)) + { + return temp_str; + } + } + free(temp_str); + return NULL; +} diff --git a/mcin/mcin.h b/mcin/mcin.h new file mode 100644 index 0000000..5b6f78c --- /dev/null +++ b/mcin/mcin.h @@ -0,0 +1,17 @@ +#ifndef _MCIN_H +#define _MCIN_H + +#include <regex.h> + +#define MCIN_REGEX_COUNT 51 + +typedef struct mcin_matcher { + regex_t regex_master; + regex_t regex[MCIN_REGEX_COUNT]; +} MCINMatcher; + +int mcin_matcher_init(MCINMatcher *out); +void mcin_matcher_free(MCINMatcher *matcher); +char *mcin_matcher_match(MCINMatcher *matcher, const char *str); + +#endif // _MCIN_H |