2022-12-13 20:04:18 +00:00
|
|
|
#include "str_replace_start.h"
|
|
|
|
#include <string.h>
|
2022-12-13 21:26:53 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdbool.h>
|
2022-12-14 14:00:43 +00:00
|
|
|
#define LIBRE_N 19
|
2022-12-14 17:02:52 +00:00
|
|
|
#define DEBUG false
|
2022-12-13 20:04:18 +00:00
|
|
|
|
|
|
|
/* Inspired by https://libredirect.github.io/, but in C. */
|
|
|
|
|
2022-12-13 20:18:23 +00:00
|
|
|
void str_init(char* str, int n){
|
|
|
|
for(int i=0; i<n; i++){
|
|
|
|
str[i] = ' ';
|
|
|
|
}
|
|
|
|
str[n] = '\0';
|
|
|
|
} // could also use <https://manpages.ubuntu.com/manpages/impish/man3/strinit.3pub.html>
|
|
|
|
|
2022-12-13 20:04:18 +00:00
|
|
|
int libre_redirect(const char* uri, char* output){
|
|
|
|
int l1 = strlen(uri);
|
|
|
|
int l2 = strlen(output);
|
|
|
|
|
|
|
|
if((l2 - l1) < LIBRE_N){
|
2022-12-13 21:26:53 +00:00
|
|
|
if(DEBUG) printf("Not enough memory\n");
|
2022-12-13 20:04:18 +00:00
|
|
|
return 1; // not enough memory.
|
|
|
|
}else{
|
|
|
|
char tmp_uri[l2++];
|
2022-12-13 21:26:53 +00:00
|
|
|
char tmp_output[l2++];
|
2022-12-13 20:25:35 +00:00
|
|
|
strcpy(tmp_uri, uri); // strcpy also copies the terminating '\0'
|
2022-12-13 21:26:53 +00:00
|
|
|
strcpy(tmp_output, output);
|
2022-12-14 17:23:19 +00:00
|
|
|
|
|
|
|
char* annoying_sites[] = {
|
2022-12-13 22:10:34 +00:00
|
|
|
"https://www.youtube.com",
|
2022-12-13 21:26:53 +00:00
|
|
|
"https://www.reddit.com",
|
2022-12-13 20:04:18 +00:00
|
|
|
"https://medium.com",
|
2022-12-13 22:50:59 +00:00
|
|
|
"https://translate.google.com",
|
2022-12-14 14:00:43 +00:00
|
|
|
"https://forum.effectivealtruism.org",
|
2022-12-15 20:44:53 +00:00
|
|
|
"https://www.bloomberg.com",
|
2022-12-27 14:39:18 +00:00
|
|
|
"https://twitter.com",
|
|
|
|
"https://wikipedia.org"
|
2022-12-13 20:04:18 +00:00
|
|
|
};
|
|
|
|
char* alternatives[] = {
|
2023-01-06 23:31:12 +00:00
|
|
|
"https://invidious.flokinet.to", /* "https://yt.artemislena.eu", */
|
2022-12-13 20:04:18 +00:00
|
|
|
"https://teddit.nunosempere.com",
|
|
|
|
"https://scribe.rip",
|
2022-12-13 22:50:59 +00:00
|
|
|
"https://simplytranslate.org/",
|
2022-12-14 14:00:43 +00:00
|
|
|
"https://ea.greaterwrong.com",
|
2022-12-15 20:44:53 +00:00
|
|
|
"https://archive.is/https://www.bloomberg.com",
|
2022-12-27 14:39:18 +00:00
|
|
|
"https://nitter.net",
|
|
|
|
"https://wikiless.org"
|
2022-12-13 20:04:18 +00:00
|
|
|
};
|
2022-12-14 17:23:19 +00:00
|
|
|
int n = sizeof(annoying_sites)/sizeof(annoying_sites[0]);
|
2022-12-14 14:00:43 +00:00
|
|
|
for(int i=0; i<n ; i++){
|
2022-12-14 17:23:19 +00:00
|
|
|
int replace_check = str_replace_start(tmp_uri, annoying_sites[i], alternatives[i], output);
|
2022-12-13 21:26:53 +00:00
|
|
|
if(replace_check == 2){
|
|
|
|
if(DEBUG) printf("tmp_uri: %s\n", tmp_uri);
|
|
|
|
if(DEBUG) printf("output: %s\n", output);
|
|
|
|
// strcpy(output, tmp_uri);
|
|
|
|
// break;
|
2022-12-13 21:59:04 +00:00
|
|
|
return 2;
|
2022-12-13 21:26:53 +00:00
|
|
|
}else if(replace_check == 1){
|
|
|
|
if(DEBUG) printf("replace_check failed\n");
|
2022-12-13 20:04:18 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
strcpy(tmp_uri, output);
|
2022-12-13 21:26:53 +00:00
|
|
|
str_init(output, l2);
|
2022-12-13 20:04:18 +00:00
|
|
|
}
|
|
|
|
strcpy(output, tmp_uri);
|
|
|
|
}
|
2022-12-13 22:31:21 +00:00
|
|
|
if(DEBUG) printf("No match found\n\n");
|
2022-12-13 20:04:18 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|