From 5d34ac70aab7e23fcf871c84ee1a230e4f9ded83 Mon Sep 17 00:00:00 2001 From: "me@ow.nekobit.net" Date: Fri, 25 Feb 2022 20:26:41 +0000 Subject: [PATCH] Regex stuff FossilOrigin-Name: 6ed1d24098367d00abf2401fc05e9f4c8c101d90a4a7fb5d8f611a8a029539c0 --- src/reply.c | 46 +++++++++++++++++++++++++++++++++++++--------- src/reply.h | 5 +---- src/status.c | 2 -- 3 files changed, 38 insertions(+), 15 deletions(-) diff --git a/src/reply.c b/src/reply.c index 5b99e89..e44a105 100644 --- a/src/reply.c +++ b/src/reply.c @@ -16,6 +16,7 @@ * along with this program. If not, see . */ +#include #include #include #include "reply.h" @@ -44,30 +45,57 @@ char* construct_post_box(char* reply_id, return reply_html; } -char* reply_status(char* id, - struct mstdnt_status* statuses_before, - size_t before_len, - struct mstdnt_status* status) -{ - char* stat_reply; +#define REGEX_REPLY "@(.*)<\\/span>" +//#define REGEX_REPLY "@(.*)<\\/span>" +char* reply_status(char* id, struct mstdnt_status* status) +{ + char* content = status->content; + char* stat_reply; + // Regex + regex_t regex; + regmatch_t pmatch[2]; + regoff_t off_url, len_url, off_name, len_name; + // Replies size_t replies_size, replies_size_orig; char* replies = malloc(replies_size = strlen(status->account.acct)+2); + + // Load first reply replies[0] = '@'; strcpy(replies+1, status->account.acct); replies[replies_size-1] = ' '; - for (int i = before_len-1; i >= 1; --i) + // Compile regex + if (regcomp(®ex, REGEX_REPLY, REG_EXTENDED)) { + fputs("Invalid regex!", stderr); + free(replies); + } + + int j = 0; + + for (int i = 0;; ++i) + { + if (regexec(®ex, content + j, 2, pmatch, 0)) + break; + + off_url = pmatch[0].rm_so + j; + len_url = pmatch[0].rm_eo - pmatch[0].rm_so; + + off_name = pmatch[1].rm_so + j; + len_name = pmatch[1].rm_eo - pmatch[1].rm_so; + replies_size_orig = replies_size; - replies_size += strlen(statuses_before[i].account.acct)+2; + replies_size += len_url+2; // Realloc string replies = realloc(replies, replies_size+1); + + j += off_url + len_url + off_name + len_name; replies[replies_size_orig] = '@'; - strcpy(replies + replies_size_orig + 1, statuses_before[i].account.acct); + memcpy(replies + replies_size_orig + 1, content + off_url, len_url); replies[replies_size-1] = ' '; } diff --git a/src/reply.h b/src/reply.h index 09c8629..44e37a9 100644 --- a/src/reply.h +++ b/src/reply.h @@ -24,10 +24,7 @@ char* construct_post_box(char* reply_id, char* default_content, int* size); -char* reply_status(char* id, - struct mstdnt_status* statuses_before, - size_t before_len, - struct mstdnt_status* status); +char* reply_status(char* id, struct mstdnt_status* status); #endif // REPLY_H diff --git a/src/status.c b/src/status.c index 1aff811..b9939de 100644 --- a/src/status.c +++ b/src/status.c @@ -164,8 +164,6 @@ void content_status(mastodont_t* api, char** data, size_t data_size, int is_repl if (is_reply) { stat_reply = reply_status(data[0], - statuses_before, - stat_before_len, &status); }