summaryrefslogtreecommitdiff
path: root/load_webhooks.h
blob: 5c3a60b74297c34a2389ff65c34c9ef18fbabc8a (about) (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
#define WH_LOAD_SUCCESS 0
#define WH_LOAD_FAILED -1

#define WH_PARSE_SUCCESS 0
#define WH_PARSE_FAILED -1

int parse_webhooks( webhooks_data_t *whdt, size_t wh_file_len ){
    if ( whdt->webhooks_config == NULL || (uint64_t)strlen(whdt->webhooks_config) <= 0 )
        return WH_PARSE_FAILED;
    if ( wh_file_len <= 0 )
        return WH_PARSE_FAILED;
    whdt->webhooks_parsed = cJSON_ParseWithLength( whdt->webhooks_config, wh_file_len );
    if ( whdt->webhooks_parsed == NULL )
        return WH_PARSE_FAILED;
    return WH_PARSE_SUCCESS;
}

int load_webhooks( char *wh_file_name, webhooks_data_t *wdt ){
    if ( wh_file_name == NULL || (uint64_t)strlen(wh_file_name) <= 0 )
        return WH_LOAD_FAILED;
    FILE *whconf = fopen( wh_file_name, "r" );
    if ( whconf == NULL )
        return WH_LOAD_FAILED;
    if ( fseek(whconf, 0, SEEK_END) == -1 ){
        fclose( whconf );
        return WH_LOAD_FAILED;
    }
    long long int wh_file_size = (long long int)ftell( whconf );
    if ( wh_file_size <= 0 ){
        wh_file_size = 0;
        fclose( whconf );
        return WH_LOAD_FAILED;
    }
    rewind( whconf );
    wdt->webhooks_config = (char*)calloc( (size_t)wh_file_size, sizeof(char) );
    if ( wdt->webhooks_config == NULL ){
        wh_file_size = 0;
        fclose( whconf );
        return WH_LOAD_FAILED;
    }
    if ( (long long int)fread(wdt->webhooks_config, sizeof(char), (size_t)(wh_file_size * sizeof(char)), whconf) == (long long int)-1 ){
        wh_file_size = 0;
        free( wdt->webhooks_config );
        fclose( whconf );
        return WH_LOAD_FAILED;
    }
    fclose( whconf );
    if ( parse_webhooks(wdt, wh_file_size) != WH_PARSE_SUCCESS ){
        free( wdt->webhooks_config );
        wh_file_size = 0;
        return WH_LOAD_FAILED;
    }
    wh_file_size = 0;
    free( wdt->webhooks_config );
    return WH_LOAD_SUCCESS;
}