aboutsummaryrefslogtreecommitdiff
path: root/plugins/shortcuts/shortcuts.c
blob: 7ee9e2ead6afe9be0edb88d1b8751f3fa39ed12e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <stdbool.h>
#include <stdio.h>
#include <string.h>

#include "../libre_redirect/str_init.h"
#include "../libre_redirect/str_replace_start.h"

#define SHORTCUT_N 41

/* Uncomment for debug */
/* #define DEBUG */

/* Inspired by https://duckduckgo.com/bangs */

int shortcut_expand(const char* uri, char* output)
{
    printf("SHORTCUT EXPAND!");
    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[] = {
            "!fnf",
            "!fnc",
            "!hn",
            "!hnb"
        };

        char* expansions[] = {
            "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]);
        len = sizeof(shortcuts) / sizeof(char*);

        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;
}