summaryrefslogtreecommitdiff
path: root/webhook_exec.h
blob: 48b9a4bdafbff4f65267782ce60ecad6a89bf29a (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
57
58
59
60
61
62
63
char *webhook_exec( webhooks_data_t *wh_data, char *wh_endpoint ){
    if ( wh_data->webhooks_parsed == NULL )
        return "Error: webhooks haven't been parsed";
    wh_data->wh_exec_data = cJSON_GetObjectItemCaseSensitive( wh_data->webhooks_parsed, wh_endpoint );
    if ( wh_data->wh_exec_data == NULL )
        return "Error: no such webhook found";
    wh_data->wh_command = cJSON_GetObjectItemCaseSensitive( wh_data->wh_exec_data, "cmd" );
    if ( wh_data->wh_command == NULL || (uint64_t)strlen(wh_data->wh_command->valuestring) <= 0 ){
        wh_data->wh_exec_data = NULL;
        return "Error: no command set for given webhook";
    }
    wh_data->wh_response = cJSON_GetObjectItemCaseSensitive( wh_data->wh_exec_data, "response" );
    if ( wh_data->wh_response == NULL || (uint64_t)strlen(wh_data->wh_response->valuestring) <= 0 ){
        wh_data->wh_command = NULL;
        wh_data->wh_exec_data = NULL;
        return "Error: no response specified";
    }
    if ( strcmp((const char*)wh_data->wh_response->valuestring, "output") != 0 ){
        system( wh_data->wh_command->valuestring );
        wh_data->wh_command = NULL;
        wh_data->wh_exec_data = NULL;
        return wh_data->wh_response->valuestring;
    }
    wh_data->wh_response = NULL;
    wh_data->wh_output = (char*)calloc( (size_t)MAX_WEBHOOK_OUTPUT_LENGTH, sizeof(char) );
    if ( wh_data->wh_output == NULL ){
        wh_data->wh_command = NULL;
        wh_data->wh_output = NULL;
        return "Error: failed to allocate memory for command output";
    }
    FILE *wh_proc = popen( wh_data->wh_command->valuestring, "r" );
    if ( wh_proc == NULL ){
        free( wh_data->wh_output );
        wh_data->wh_output = NULL;
        wh_data->wh_command = NULL;
        wh_data->wh_exec_data = NULL;
        return "Error: failed to execute webhook command";
    }
    size_t read_bytes = 0;
    char *current_line = (char*)calloc( 8192, sizeof(char) );
    while ( fgets(current_line, 8192 * sizeof(char), wh_proc) != NULL ){
        if ( (size_t)((size_t)strlen(current_line) + read_bytes) > (size_t)MAX_WEBHOOK_OUTPUT_LENGTH )
            break;
        strncat( wh_data->wh_output, current_line, (size_t)strlen(current_line) );
        read_bytes += (size_t)strlen( current_line );
        memset( current_line, 0, 8192 * sizeof(char) );
    }
    free( current_line );
    pclose( wh_proc );
    if ( wh_data->wh_output == NULL || (size_t)strlen(wh_data->wh_output) <= 0 )
        return "Error: failed to get output";
    return wh_data->wh_output;
}

void free_wh_output( webhooks_data_t *hooksdata ){
    if ( hooksdata->wh_output != NULL ){
        free( hooksdata->wh_output );
        hooksdata->wh_output = NULL;
    }
    if ( hooksdata->wh_response != NULL )
        hooksdata->wh_response = NULL;
    return;
}