aboutsummaryrefslogtreecommitdiff
path: root/config.c
diff options
context:
space:
mode:
authorDaniel Eklöf <daniel@ekloef.se>2025-02-03 12:56:57 +0000
committerDaniel Eklöf <daniel@ekloef.se>2025-02-05 12:35:17 +0000
commita984531ce57f30bc9ec7f664e7c2e8c77b3f439e (patch)
treec9533c6289d41e1b34e80bb003bf89ba6ffe4556 /config.c
parentconfig: remove debug logging (diff)
url-mode: use the first *sub* expression as URL
When auto-matching URLs (or custom regular expressions), use the first *subexpression* as URL, rather than the while regex match. This allows us to write custom regular expressions with prefix/suffix strings that should not be included in the presented match.
Diffstat (limited to 'config.c')
-rw-r--r--config.c56
1 files changed, 36 insertions, 20 deletions
diff --git a/config.c b/config.c
index 82a4811f..604c0a76 100644
--- a/config.c
+++ b/config.c
@@ -1256,6 +1256,12 @@ parse_section_url(struct context *ctx)
return false;
}
+ if (preg.re_nsub == 0) {
+ LOG_CONTEXTUAL_ERR("invalid regex: no marked subexpression(s)");
+ regfree(&preg);
+ return false;
+ }
+
regfree(&conf->url.preg);
free(conf->url.regex);
@@ -1300,6 +1306,12 @@ parse_section_regex(struct context *ctx)
return false;
}
+ if (preg.re_nsub == 0) {
+ LOG_CONTEXTUAL_ERR("invalid regex: no marked subexpression(s)");
+ regfree(&preg);
+ return false;
+ }
+
if (regex == NULL) {
tll_push_back(conf->custom_regexes,
((struct custom_regex){.name = xstrdup(regex_name)}));
@@ -3426,33 +3438,37 @@ config_load(struct config *conf, const char *conf_path,
*/
const char *url_regex_string =
"("
- "[a-z][[:alnum:]-]+:" // protocol
"("
- "/{1,3}|[a-z0-9%]" // slashes (what's the OR part for?)
+ "[a-z][[:alnum:]-]+:" // protocol
+ "("
+ "/{1,3}|[a-z0-9%]" // slashes (what's the OR part for?)
+ ")"
+ "|"
+ "www[:digit:]{0,3}[.]"
+ //"|"
+ //"[a-z0-9.\\-]+[.][a-z]{2,4}/" /* "looks like domain name followed by a slash" - remove? */
+ ")"
+ "("
+ "[^[:space:](){}<>]+"
+ "|"
+ "\\(([^[:space:](){}<>]+|(\\([^[:space:](){}<>]+\\)))*\\)"
+ "|"
+ "\\[([^]\\[[:space:](){}<>]+|(\\[[^]\\[[:space:](){}<>]+\\]))*\\]"
+ ")+"
+ "("
+ "\\(([^[:space:](){}<>]+|(\\([^[:space:](){}<>]+\\)))*\\)"
+ "|"
+ "\\[([^]\\[[:space:](){}<>]+|(\\[[^]\\[[:space:](){}<>]+\\]))*\\]"
+ "|"
+ "[^]\\[[:space:]`!(){};:'\".,<>?«»“”‘’]"
")"
- "|"
- "www[:digit:]{0,3}[.]"
- //"|"
- //"[a-z0-9.\\-]+[.][a-z]{2,4}/" /* "looks like domain name followed by a slash" - remove? */
- ")"
- "("
- "[^[:space:](){}<>]+"
- "|"
- "\\(([^[:space:](){}<>]+|(\\([^[:space:](){}<>]+\\)))*\\)"
- "|"
- "\\[([^]\\[[:space:](){}<>]+|(\\[[^]\\[[:space:](){}<>]+\\]))*\\]"
- ")+"
- "("
- "\\(([^[:space:](){}<>]+|(\\([^[:space:](){}<>]+\\)))*\\)"
- "|"
- "\\[([^]\\[[:space:](){}<>]+|(\\[[^]\\[[:space:](){}<>]+\\]))*\\]"
- "|"
- "[^]\\[[:space:]`!(){};:'\".,<>?«»“”‘’]"
")"
;
+
int r = regcomp(&conf->url.preg, url_regex_string, REG_EXTENDED);
xassert(r == 0);
conf->url.regex = xstrdup(url_regex_string);
+ xassert(conf->url.preg.re_nsub >= 1);
}
tll_foreach(*initial_user_notifications, it) {