rosenrot/plugins/shortcuts/shortcuts.c

77 lines
1.9 KiB
C
Raw Normal View History

2023-05-14 03:24:34 +00:00
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
2023-05-14 03:50:14 +00:00
#include "../libre_redirect/str_init.h"
2023-06-17 06:29:27 +00:00
#include "../libre_redirect/str_replace_start.h"
2023-05-14 03:24:34 +00:00
2023-05-14 03:50:14 +00:00
#define SHORTCUT_N 41
2023-05-14 03:24:34 +00:00
/* Uncomment for debug */
/* #define DEBUG */
/* Inspired by https://duckduckgo.com/bangs */
int shortcut_expand(const char* uri, char* output)
{
printf("SHORTCUT EXPAND!\n");
2023-05-14 03:24:34 +00:00
int l1 = strlen(uri);
int l2 = strlen(output);
int len;
char tmp_uri[l2++];
char tmp_output[l2++];
if ((l2 - l1) < SHORTCUT_N) {
#ifdef DEBUG
printf("Not enough memory\n");
#endif
return 1; // not enough memory.
} else {
strcpy(tmp_uri, uri); // strcpy also copies the terminating '\0'
strcpy(tmp_output, output);
char* shortcuts[] = {
"!x",
2023-05-14 03:24:34 +00:00
"!fnf",
"!fnc",
2023-06-17 06:29:27 +00:00
"!hn",
"!hnb"
2023-05-14 03:24:34 +00:00
};
char* expansions[] = {
"https://twitter.com"
2023-05-14 03:24:34 +00:00
"https://forum.nunosempere.com/frontpage",
"https://forum.nunosempere.com/comments",
"https://news.ycombinator.com",
"https://news.ycombinator.com/best",
};
// len = sizeof(shortcuts) / sizeof(shortcuts[0]);
2023-06-17 06:29:27 +00:00
len = sizeof(shortcuts) / sizeof(char*);
2023-05-14 03:24:34 +00:00
for (int i = 0; i < len; i++) {
int replace_check = str_replace_start(tmp_uri, shortcuts[i],
expansions[i], output);
if (replace_check == 2) {
#ifdef DEBUG
printf("tmp_uri: %s\n", tmp_uri);
printf("output: %s\n", output);
#endif
return 2;
} else if (replace_check == 1) {
#ifdef DEBUG
printf("replace_check failed\n");
#endif
return 1;
}
strcpy(tmp_uri, output);
str_init(output, l2);
}
strcpy(output, tmp_uri);
}
#ifdef DEBUG
printf("No match found\n\n");
#endif
return 0;
}