aboutsummaryrefslogtreecommitdiff
path: root/utils.c
diff options
context:
space:
mode:
authorgrunfink <grunfink@noreply.codeberg.org>2025-04-27 03:11:57 +0000
committergrunfink <grunfink@noreply.codeberg.org>2025-04-27 03:11:57 +0000
commit169c5c8c070c527e7462e6ac86b916e94649c1ac (patch)
treef1ce03b950a278a1985526cb58dc4e879c3ab046 /utils.c
parent2e39e4e3c36dc23598173f3b6fe64a103574d089 (diff)
parent7f936d2d1a88840209895c6091801e8c4cfdaaa1 (diff)
Merge pull request 'do email notifications with CURL' (#283) from shtrophic/snac2:curl-smtp into master
Reviewed-on: https://codeberg.org/grunfink/snac2/pulls/283
Diffstat (limited to 'utils.c')
-rw-r--r--utils.c57
1 files changed, 57 insertions, 0 deletions
diff --git a/utils.c b/utils.c
index 5d54b8b..bf55b9f 100644
--- a/utils.c
+++ b/utils.c
@@ -912,3 +912,60 @@ void import_csv(snac *user)
else
snac_log(user, xs_fmt("Cannot open file %s", fn));
}
+
+static const struct {
+ const char *proto;
+ unsigned short default_port;
+} FALLBACK_PORTS[] = {
+ /* caution: https > http, smpts > smtp */
+ {"https", 443},
+ {"http", 80},
+ {"smtps", 465},
+ {"smtp", 25}
+};
+
+int parse_port(const char *url, const char **errstr)
+{
+ const char *col, *rcol;
+ int tmp, ret = -1;
+
+ if (errstr)
+ *errstr = NULL;
+
+ if (!(col = strchr(url, ':'))) {
+ if (errstr)
+ *errstr = "bad url";
+
+ return -1;
+ }
+
+ for (size_t i = 0; i < sizeof(FALLBACK_PORTS) / sizeof(*FALLBACK_PORTS); ++i) {
+ if (memcmp(url, FALLBACK_PORTS[i].proto, strlen(FALLBACK_PORTS[i].proto)) == 0) {
+ ret = FALLBACK_PORTS[i].default_port;
+ break;
+ }
+ }
+
+ if (!(rcol = strchr(col + 1, ':')))
+ rcol = col;
+
+ if (rcol) {
+ tmp = atoi(rcol + 1);
+ if (tmp == 0) {
+ if (ret != -1)
+ return ret;
+
+ if (errstr)
+ *errstr = strerror(errno);
+
+ return -1;
+ }
+
+ return tmp;
+ }
+
+ if (errstr)
+ *errstr = "unknown protocol";
+
+ return -1;
+}