2022-12-17 19:47:44 +00:00
|
|
|
#include <stdbool.h>
|
2023-03-28 16:16:14 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2022-12-18 16:54:07 +00:00
|
|
|
|
2023-05-14 03:50:14 +00:00
|
|
|
#include "str_init.h"
|
2022-12-18 16:54:07 +00:00
|
|
|
#include "str_replace_start.h"
|
|
|
|
|
2022-12-17 19:47:44 +00:00
|
|
|
#define LIBRE_N 19
|
2022-12-18 16:54:07 +00:00
|
|
|
|
|
|
|
/* Uncomment for debug */
|
|
|
|
/* #define DEBUG */
|
2022-12-17 19:47:44 +00:00
|
|
|
|
|
|
|
/* Inspired by https://libredirect.github.io/, but in C. */
|
|
|
|
|
2022-12-18 16:54:07 +00:00
|
|
|
int libre_redirect(const char* uri, char* output)
|
|
|
|
{
|
2023-03-28 16:16:14 +00:00
|
|
|
int l1 = strlen(uri);
|
|
|
|
int l2 = strlen(output);
|
|
|
|
int len;
|
|
|
|
char tmp_uri[l2++];
|
|
|
|
char tmp_output[l2++];
|
2022-12-18 16:54:07 +00:00
|
|
|
|
2023-03-28 16:16:14 +00:00
|
|
|
if ((l2 - l1) < LIBRE_N) {
|
2022-12-18 16:54:07 +00:00
|
|
|
#ifdef DEBUG
|
2023-03-28 16:16:14 +00:00
|
|
|
printf("Not enough memory\n");
|
2022-12-18 16:54:07 +00:00
|
|
|
#endif
|
2023-03-28 16:16:14 +00:00
|
|
|
return 1; // not enough memory.
|
|
|
|
} else {
|
|
|
|
strcpy(tmp_uri, uri); // strcpy also copies the terminating '\0'
|
|
|
|
strcpy(tmp_output, output);
|
2022-12-18 16:54:07 +00:00
|
|
|
|
2023-03-28 16:16:14 +00:00
|
|
|
char* annoying_sites[] = {
|
|
|
|
"https://www.reddit.com",
|
2023-05-03 22:02:49 +00:00
|
|
|
"https://www.youtube.com",
|
2023-06-25 22:22:15 +00:00
|
|
|
"https://google.com",
|
2023-03-28 16:16:14 +00:00
|
|
|
"https://medium.com",
|
|
|
|
"https://translate.google.com",
|
|
|
|
"https://www.bloomberg.com",
|
2023-06-05 01:15:32 +00:00
|
|
|
"https://www.royalroad.com",
|
2023-05-05 23:42:19 +00:00
|
|
|
// "https://twitter.com"
|
2023-03-28 16:16:14 +00:00
|
|
|
};
|
2022-12-18 16:54:07 +00:00
|
|
|
|
2023-03-28 16:16:14 +00:00
|
|
|
char* alternatives[] = {
|
|
|
|
"https://teddit.nunosempere.com",
|
2023-05-03 22:02:49 +00:00
|
|
|
"https://yt.artemislena.eu",
|
2023-06-25 22:22:15 +00:00
|
|
|
"https://search.nunosempere.com",
|
2023-03-28 16:16:14 +00:00
|
|
|
"https://scribe.rip",
|
2023-05-07 15:53:51 +00:00
|
|
|
"https://translate.riverside.rocks/",
|
2023-03-28 16:16:14 +00:00
|
|
|
"https://archive.is/https://www.bloomberg.com",
|
2023-06-05 01:11:45 +00:00
|
|
|
"https://royalread.nunosempere.com",
|
2023-05-05 23:42:19 +00:00
|
|
|
// "https://nitter.net"
|
2023-03-28 16:16:14 +00:00
|
|
|
};
|
2022-12-18 16:54:07 +00:00
|
|
|
|
2023-03-28 16:16:14 +00:00
|
|
|
len = sizeof(annoying_sites) / sizeof(annoying_sites[0]);
|
2022-12-18 16:54:07 +00:00
|
|
|
|
2023-03-28 16:16:14 +00:00
|
|
|
for (int i = 0; i < len; i++) {
|
|
|
|
int replace_check = str_replace_start(tmp_uri, annoying_sites[i],
|
|
|
|
alternatives[i], output);
|
|
|
|
if (replace_check == 2) {
|
2022-12-18 16:54:07 +00:00
|
|
|
#ifdef DEBUG
|
2023-03-28 16:16:14 +00:00
|
|
|
printf("tmp_uri: %s\n", tmp_uri);
|
|
|
|
printf("output: %s\n", output);
|
2022-12-18 16:54:07 +00:00
|
|
|
#endif
|
2023-03-28 16:16:14 +00:00
|
|
|
return 2;
|
|
|
|
} else if (replace_check == 1) {
|
2022-12-18 16:54:07 +00:00
|
|
|
#ifdef DEBUG
|
2023-03-28 16:16:14 +00:00
|
|
|
printf("replace_check failed\n");
|
2022-12-18 16:54:07 +00:00
|
|
|
#endif
|
2023-03-28 16:16:14 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
strcpy(tmp_uri, output);
|
|
|
|
str_init(output, l2);
|
|
|
|
}
|
|
|
|
strcpy(output, tmp_uri);
|
|
|
|
}
|
2022-12-18 16:54:07 +00:00
|
|
|
#ifdef DEBUG
|
2023-03-28 16:16:14 +00:00
|
|
|
printf("No match found\n\n");
|
2022-12-18 16:54:07 +00:00
|
|
|
#endif
|
2023-03-28 16:16:14 +00:00
|
|
|
return 0;
|
2022-12-17 19:47:44 +00:00
|
|
|
}
|